Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/InfoSupportTest.java
38840 views
/*1* Copyright (c) 2004, 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 4967769 4980655 4980668 498121426* @summary Test that ModelMBeanInfoSupport.setDescriptor rejects27* null descriptor, that empty arrays are handled correctly,28* that getDescriptors("mbean") works, and that default values for29* MBean descriptors are correctly assigned.30* @author Eamonn McManus31* @run clean InfoSupportTest32* @run build InfoSupportTest33* @run main InfoSupportTest34*/3536import java.util.*;37import javax.management.Descriptor;38import javax.management.RuntimeOperationsException;39import javax.management.modelmbean.*;4041public class InfoSupportTest {42public static void main(String[] args) throws Exception {43boolean ok = true;4445ok &= testSetDescriptorNull();46ok &= testEmptyArrayParameters();47ok &= testGetDescriptorsForMBean();4849if (ok)50System.out.println("Test passed");51else {52System.out.println("TEST FAILED");53System.exit(1);54}55}5657private static boolean testSetDescriptorNull() {58System.out.println("Testing that " +59"ModelMBeanInfoSupport.setDescriptor(null, \"x\")" +60" throws an exception");6162ModelMBeanAttributeInfo mmbai =63new ModelMBeanAttributeInfo("attribute",64"java.lang.String",65"an attribute",66true, true, false);67ModelMBeanInfo mmbi =68new ModelMBeanInfoSupport("bogus.class.name",69"an MBean",70new ModelMBeanAttributeInfo[] {mmbai},71null, null, null);72try {73mmbi.setDescriptor(null, "attribute");74} catch (RuntimeOperationsException e) {75if (e.getTargetException() instanceof IllegalArgumentException) {76System.out.println("Test passes: got a " +77"RuntimeOperationsException wrapping an " +78"IllegalArgumentException");79return true;80} else {81System.out.println("TEST FAILS: wrong exception:");82e.printStackTrace(System.out);83return false;84}85} catch (Exception e) {86System.out.println("TEST FAILS: wrong exception:");87e.printStackTrace(System.out);88return false;89}9091System.out.println("TEST FAILS: exception not thrown");92return false;93}9495private static boolean testEmptyArrayParameters()96throws Exception {9798System.out.println("Test that empty and null array parameters " +99"produce the right type from getters");100101boolean ok = true;102103ModelMBeanInfoSupport mmbi;104105mmbi =106new ModelMBeanInfoSupport("bogus.class.name", "description",107null, null, null, null);108ok &= checkMMBI(mmbi, "null parameters, no descriptor");109110mmbi =111new ModelMBeanInfoSupport("bogus.class.name", "description",112null, null, null, null, null);113ok &= checkMMBI(mmbi, "null parameters, null descriptor");114115mmbi =116new ModelMBeanInfoSupport("bogus.class.name", "description",117new ModelMBeanAttributeInfo[0],118new ModelMBeanConstructorInfo[0],119new ModelMBeanOperationInfo[0],120new ModelMBeanNotificationInfo[0]);121ok &= checkMMBI(mmbi, "empty parameters, no descriptor");122123mmbi =124new ModelMBeanInfoSupport("bogus.class.name", "description",125new ModelMBeanAttributeInfo[0],126new ModelMBeanConstructorInfo[0],127new ModelMBeanOperationInfo[0],128new ModelMBeanNotificationInfo[0],129null);130ok &= checkMMBI(mmbi, "empty parameters, null descriptor");131132return ok;133}134135private static boolean checkMMBI(ModelMBeanInfoSupport mmbi, String what)136throws Exception {137String bad = "";138139if (!(mmbi.getAttributes() instanceof ModelMBeanAttributeInfo[]))140bad += " attributes";141if (!(mmbi.getConstructors() instanceof ModelMBeanConstructorInfo[]))142bad += " constructors";143if (!(mmbi.getOperations() instanceof ModelMBeanOperationInfo[]))144bad += " operations";145if (!(mmbi.getNotifications() instanceof ModelMBeanNotificationInfo[]))146bad += " notifications";147148if (bad.equals("")) {149System.out.println("..." + what + ": OK");150return true;151} else {152System.out.println("..." + what + ": FAILS for:" + bad);153return false;154}155}156157private static boolean testGetDescriptorsForMBean()158throws Exception {159System.out.println("Test getDescriptors(\"mbean\")");160161boolean ok = true;162163ModelMBeanInfo mmbi;164Descriptor[] mbeanDescrs;165166mmbi = new ModelMBeanInfoSupport("bogus.class.name", "description",167null, null, null, null);168try {169mbeanDescrs = mmbi.getDescriptors("mbean");170if (mbeanDescrs.length == 1 && mbeanDescrs[0] != null)171System.out.println("...default MBean descriptor: OK");172else {173System.out.println("...default MBean descriptor: bad array: " +174Arrays.asList(mbeanDescrs));175ok = false;176}177} catch (Exception e) {178System.out.println("...default MBean descriptor: got exception:");179e.printStackTrace(System.out);180ok = false;181}182183String[] fields = new String[] {"descriptorType", "name"};184String[] values = new String[] {"mbean", "whatsit"};185String[] defaultFields = new String[] {186"displayName", "persistPolicy", "log", "visibility",187};188String[] defaultValues = new String[] {189"bogus.class.name", "never", "F", "1",190};191Descriptor d = new DescriptorSupport(fields, values);192193mmbi = new ModelMBeanInfoSupport("bogus.class.name", "description",194null, null, null, null, d);195try {196mbeanDescrs = mmbi.getDescriptors("mbean");197} catch (Exception e) {198System.out.println("...explicit MBean descriptor: got exception:");199e.printStackTrace(System.out);200mbeanDescrs = new Descriptor[] {mmbi.getMBeanDescriptor()};201}202203if (mbeanDescrs.length == 1) {204Descriptor dd = mbeanDescrs[0];205boolean thisok = true;206207// Check that the fields we supplied survived in the copy208// and were not changed in the original209for (int i = 0; i < fields.length; i++) {210String field = fields[i];211String value;212value = (String) dd.getFieldValue(field);213if (!values[i].equals(value)) {214System.out.println("...explicit MBean descriptor: " +215"value of " + field + " mutated: " +216value);217thisok = false;218}219value = (String) d.getFieldValue(field);220if (!values[i].equals(value)) {221System.out.println("...explicit MBean descriptor: " +222"value of " + field + " changed in " +223"original: " + value);224thisok = false;225}226}227228// Check that the default fields were added229for (int i = 0; i < defaultFields.length; i++) {230String field = defaultFields[i];231String value = (String) dd.getFieldValue(field);232if (!defaultValues[i].equals(value)) {233System.out.println("...explicit MBean descriptor: " +234"default value of " + field +235" wrong: " + value + " should be " +236defaultValues[i]);237thisok = false;238}239}240241// Check that the original did not acquire new fields242if (d.getFieldNames().length != fields.length) {243Collection c = new TreeSet(Arrays.asList(d.getFieldNames()));244c.removeAll(Arrays.asList(fields));245System.out.println("...explicit MBean descriptor: " +246"acquired new fields: " + c);247thisok = false;248}249250if (thisok)251System.out.println("...explicit MBean descriptor: OK");252else253ok = false;254} else {255System.out.println("...explicit MBean descriptor: bad array: " +256Arrays.asList(mbeanDescrs));257ok = false;258}259260return ok;261}262}263264265