1 /*
   2  * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5072004
  27  * @summary Test new rules for isValue
  28  * @author Eamonn McManus
  29  * @modules java.management
  30  */
  31 
  32 import javax.management.openmbean.*;
  33 
  34 public class IsValueTest {
  35     private static String failed;
  36 
  37     public static void main(String[] args) throws Exception {
  38         CompositeType ctOld =
  39             new CompositeType("same.type.name", "old",
  40                 new String[] {"int", "string"},
  41                 new String[] {"an int", "a string"},
  42                 new OpenType[] {SimpleType.INTEGER, SimpleType.STRING});
  43         CompositeType ctNew =
  44             new CompositeType("same.type.name", "new",
  45                 new String[] {"int", "int2", "string"},
  46                 new String[] {"an int", "another int", "a string"},
  47                 new OpenType[] {SimpleType.INTEGER, SimpleType.INTEGER, SimpleType.STRING});
  48         CompositeData cdOld =
  49             new CompositeDataSupport(ctOld,
  50                 new String[] {"string", "int"},
  51                 new Object[] {"bar", 17});
  52         CompositeData cdNew =
  53             new CompositeDataSupport(ctNew,
  54                 new String[] {"int2", "int", "string"},
  55                 new Object[] {4, 3, "foo"});
  56 
  57         // Check that adding fields doesn't make isValue return false
  58         check(ctOld.isValue(cdNew), "isValue: " + ctOld + "[" + cdNew + "]");
  59 
  60         // Check that removing fields does make isValue return false
  61         check(!ctNew.isValue(cdOld), "isValue: " + ctNew + "[" + cdOld + "]");
  62 
  63         // Check that we can add a contained CompositeData with extra fields
  64         // inside another CompositeData
  65         CompositeType ctWrapOld =
  66             new CompositeType("wrapper", "wrapper",
  67                 new String[] {"wrapped"},
  68                 new String[] {"wrapped"},
  69                 new OpenType[] {ctOld});
  70         try {
  71             new CompositeDataSupport(ctWrapOld,
  72                 new String[] {"wrapped"},
  73                 new Object[] {cdNew});
  74             check(true, "CompositeDataSupport containing CompositeDataSupport");
  75         } catch (Exception e) {
  76             e.printStackTrace(System.out);
  77             check(false, "CompositeDataSupport containing CompositeDataSupport: " + e);
  78         }
  79 
  80         // ...but not the contrary
  81         CompositeType ctWrapNew =
  82             new CompositeType("wrapper", "wrapper",
  83                 new String[] {"wrapped"},
  84                 new String[] {"wrapped"},
  85                 new OpenType[] {ctNew});
  86         try {
  87             new CompositeDataSupport(ctWrapNew,
  88                 new String[] {"wrapped"},
  89                 new Object[] {cdOld});
  90             check(false, "CompositeDataSupport containing old did not get exception");
  91         } catch (OpenDataException e) {
  92             check(true, "CompositeDataSupport containing old got expected exception: " + e);
  93         }
  94 
  95         // Check that a TabularData can get an extended CompositeData row
  96         TabularType ttOld =
  97             new TabularType("tabular", "tabular", ctOld, new String[] {"int"});
  98         TabularData tdOld =
  99             new TabularDataSupport(ttOld);
 100         try {
 101             tdOld.put(cdNew);
 102             check(true, "TabularDataSupport adding extended CompositeData");
 103         } catch (Exception e) {
 104             e.printStackTrace(System.out);
 105             check(false, "TabularDataSupport adding extended CompositeData: " + e);
 106         }
 107 
 108         // Check that an extended TabularData can be put into a CompositeData
 109         TabularType ttNew =
 110             new TabularType("tabular", "tabular", ctNew, new String[] {"int"});
 111         TabularData tdNew =
 112             new TabularDataSupport(ttNew);
 113         CompositeType cttWrap =
 114             new CompositeType("wrapTT", "wrapTT",
 115                 new String[] {"wrapped"},
 116                 new String[] {"wrapped"},
 117                 new OpenType[] {ttOld});
 118         try {
 119             new CompositeDataSupport(cttWrap,
 120                 new String[] {"wrapped"},
 121                 new Object[] {tdNew});
 122             check(true, "CompositeDataSupport adding extended TabularData");
 123         } catch (Exception e) {
 124             e.printStackTrace(System.out);
 125             check(false, "CompositeDataSupport adding extended TabularData: " + e);
 126         }
 127 
 128         if (failed != null)
 129             throw new Exception("TEST FAILED: " + failed);
 130     }
 131 
 132     private static void check(boolean value, String what) {
 133         if (value)
 134             System.out.println("OK: " + what);
 135         else {
 136             failed = what;
 137             System.out.println("FAILED: " + what);
 138         }
 139     }
 140 }