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  */
  30 
  31 import javax.management.openmbean.*;
  32 
  33 public class IsValueTest {
  34     private static String failed;
  35 
  36     public static void main(String[] args) throws Exception {
  37         CompositeType ctOld =
  38             new CompositeType("same.type.name", "old",
  39                 new String[] {"int", "string"},
  40                 new String[] {"an int", "a string"},
  41                 new OpenType[] {SimpleType.INTEGER, SimpleType.STRING});
  42         CompositeType ctNew =
  43             new CompositeType("same.type.name", "new",
  44                 new String[] {"int", "int2", "string"},
  45                 new String[] {"an int", "another int", "a string"},
  46                 new OpenType[] {SimpleType.INTEGER, SimpleType.INTEGER, SimpleType.STRING});
  47         CompositeData cdOld =
  48             new CompositeDataSupport(ctOld,
  49                 new String[] {"string", "int"},
  50                 new Object[] {"bar", 17});
  51         CompositeData cdNew =
  52             new CompositeDataSupport(ctNew,
  53                 new String[] {"int2", "int", "string"},
  54                 new Object[] {4, 3, "foo"});
  55 
  56         // Check that adding fields doesn't make isValue return false
  57         check(ctOld.isValue(cdNew), "isValue: " + ctOld + "[" + cdNew + "]");
  58 
  59         // Check that removing fields does make isValue return false
  60         check(!ctNew.isValue(cdOld), "isValue: " + ctNew + "[" + cdOld + "]");
  61 
  62         // Check that we can add a contained CompositeData with extra fields
  63         // inside another CompositeData
  64         CompositeType ctWrapOld =
  65             new CompositeType("wrapper", "wrapper",
  66                 new String[] {"wrapped"},
  67                 new String[] {"wrapped"},
  68                 new OpenType[] {ctOld});
  69         try {
  70             new CompositeDataSupport(ctWrapOld,
  71                 new String[] {"wrapped"},
  72                 new Object[] {cdNew});
  73             check(true, "CompositeDataSupport containing CompositeDataSupport");
  74         } catch (Exception e) {
  75             e.printStackTrace(System.out);
  76             check(false, "CompositeDataSupport containing CompositeDataSupport: " + e);
  77         }
  78 
  79         // ...but not the contrary
  80         CompositeType ctWrapNew =
  81             new CompositeType("wrapper", "wrapper",
  82                 new String[] {"wrapped"},
  83                 new String[] {"wrapped"},
  84                 new OpenType[] {ctNew});
  85         try {
  86             new CompositeDataSupport(ctWrapNew,
  87                 new String[] {"wrapped"},
  88                 new Object[] {cdOld});
  89             check(false, "CompositeDataSupport containing old did not get exception");
  90         } catch (OpenDataException e) {
  91             check(true, "CompositeDataSupport containing old got expected exception: " + e);
  92         }
  93 
  94         // Check that a TabularData can get an extended CompositeData row
  95         TabularType ttOld =
  96             new TabularType("tabular", "tabular", ctOld, new String[] {"int"});
  97         TabularData tdOld =
  98             new TabularDataSupport(ttOld);
  99         try {
 100             tdOld.put(cdNew);
 101             check(true, "TabularDataSupport adding extended CompositeData");
 102         } catch (Exception e) {
 103             e.printStackTrace(System.out);
 104             check(false, "TabularDataSupport adding extended CompositeData: " + e);
 105         }
 106 
 107         // Check that an extended TabularData can be put into a CompositeData
 108         TabularType ttNew =
 109             new TabularType("tabular", "tabular", ctNew, new String[] {"int"});
 110         TabularData tdNew =
 111             new TabularDataSupport(ttNew);
 112         CompositeType cttWrap =
 113             new CompositeType("wrapTT", "wrapTT",
 114                 new String[] {"wrapped"},
 115                 new String[] {"wrapped"},
 116                 new OpenType[] {ttOld});
 117         try {
 118             new CompositeDataSupport(cttWrap,
 119                 new String[] {"wrapped"},
 120                 new Object[] {tdNew});
 121             check(true, "CompositeDataSupport adding extended TabularData");
 122         } catch (Exception e) {
 123             e.printStackTrace(System.out);
 124             check(false, "CompositeDataSupport adding extended TabularData: " + e);
 125         }
 126 
 127         if (failed != null)
 128             throw new Exception("TEST FAILED: " + failed);
 129     }
 130 
 131     private static void check(boolean value, String what) {
 132         if (value)
 133             System.out.println("OK: " + what);
 134         else {
 135             failed = what;
 136             System.out.println("FAILED: " + what);
 137         }
 138     }
 139 }