Path: blob/master/test/jdk/javax/management/openmbean/IsValueTest.java
41152 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 507200426* @summary Test new rules for isValue27* @author Eamonn McManus28*/2930import javax.management.openmbean.*;3132public class IsValueTest {33private static String failed;3435public static void main(String[] args) throws Exception {36CompositeType ctOld =37new CompositeType("same.type.name", "old",38new String[] {"int", "string"},39new String[] {"an int", "a string"},40new OpenType[] {SimpleType.INTEGER, SimpleType.STRING});41CompositeType ctNew =42new CompositeType("same.type.name", "new",43new String[] {"int", "int2", "string"},44new String[] {"an int", "another int", "a string"},45new OpenType[] {SimpleType.INTEGER, SimpleType.INTEGER, SimpleType.STRING});46CompositeData cdOld =47new CompositeDataSupport(ctOld,48new String[] {"string", "int"},49new Object[] {"bar", 17});50CompositeData cdNew =51new CompositeDataSupport(ctNew,52new String[] {"int2", "int", "string"},53new Object[] {4, 3, "foo"});5455// Check that adding fields doesn't make isValue return false56check(ctOld.isValue(cdNew), "isValue: " + ctOld + "[" + cdNew + "]");5758// Check that removing fields does make isValue return false59check(!ctNew.isValue(cdOld), "isValue: " + ctNew + "[" + cdOld + "]");6061// Check that we can add a contained CompositeData with extra fields62// inside another CompositeData63CompositeType ctWrapOld =64new CompositeType("wrapper", "wrapper",65new String[] {"wrapped"},66new String[] {"wrapped"},67new OpenType[] {ctOld});68try {69new CompositeDataSupport(ctWrapOld,70new String[] {"wrapped"},71new Object[] {cdNew});72check(true, "CompositeDataSupport containing CompositeDataSupport");73} catch (Exception e) {74e.printStackTrace(System.out);75check(false, "CompositeDataSupport containing CompositeDataSupport: " + e);76}7778// ...but not the contrary79CompositeType ctWrapNew =80new CompositeType("wrapper", "wrapper",81new String[] {"wrapped"},82new String[] {"wrapped"},83new OpenType[] {ctNew});84try {85new CompositeDataSupport(ctWrapNew,86new String[] {"wrapped"},87new Object[] {cdOld});88check(false, "CompositeDataSupport containing old did not get exception");89} catch (OpenDataException e) {90check(true, "CompositeDataSupport containing old got expected exception: " + e);91}9293// Check that a TabularData can get an extended CompositeData row94TabularType ttOld =95new TabularType("tabular", "tabular", ctOld, new String[] {"int"});96TabularData tdOld =97new TabularDataSupport(ttOld);98try {99tdOld.put(cdNew);100check(true, "TabularDataSupport adding extended CompositeData");101} catch (Exception e) {102e.printStackTrace(System.out);103check(false, "TabularDataSupport adding extended CompositeData: " + e);104}105106// Check that an extended TabularData can be put into a CompositeData107TabularType ttNew =108new TabularType("tabular", "tabular", ctNew, new String[] {"int"});109TabularData tdNew =110new TabularDataSupport(ttNew);111CompositeType cttWrap =112new CompositeType("wrapTT", "wrapTT",113new String[] {"wrapped"},114new String[] {"wrapped"},115new OpenType[] {ttOld});116try {117new CompositeDataSupport(cttWrap,118new String[] {"wrapped"},119new Object[] {tdNew});120check(true, "CompositeDataSupport adding extended TabularData");121} catch (Exception e) {122e.printStackTrace(System.out);123check(false, "CompositeDataSupport adding extended TabularData: " + e);124}125126if (failed != null)127throw new Exception("TEST FAILED: " + failed);128}129130private static void check(boolean value, String what) {131if (value)132System.out.println("OK: " + what);133else {134failed = what;135System.out.println("FAILED: " + what);136}137}138}139140141