Path: blob/master/test/jdk/javax/management/modelmbean/InfoSupportTest.java
41149 views
/*1* Copyright (c) 2004, 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 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*32* @run clean InfoSupportTest33* @run build InfoSupportTest34* @run main InfoSupportTest35*/3637import java.util.*;38import javax.management.Descriptor;39import javax.management.RuntimeOperationsException;40import javax.management.modelmbean.*;4142public class InfoSupportTest {43public static void main(String[] args) throws Exception {44boolean ok = true;4546ok &= testSetDescriptorNull();47ok &= testEmptyArrayParameters();48ok &= testGetDescriptorsForMBean();4950if (ok)51System.out.println("Test passed");52else {53System.out.println("TEST FAILED");54System.exit(1);55}56}5758private static boolean testSetDescriptorNull() {59System.out.println("Testing that " +60"ModelMBeanInfoSupport.setDescriptor(null, \"x\")" +61" throws an exception");6263ModelMBeanAttributeInfo mmbai =64new ModelMBeanAttributeInfo("attribute",65"java.lang.String",66"an attribute",67true, true, false);68ModelMBeanInfo mmbi =69new ModelMBeanInfoSupport("bogus.class.name",70"an MBean",71new ModelMBeanAttributeInfo[] {mmbai},72null, null, null);73try {74mmbi.setDescriptor(null, "attribute");75} catch (RuntimeOperationsException e) {76if (e.getTargetException() instanceof IllegalArgumentException) {77System.out.println("Test passes: got a " +78"RuntimeOperationsException wrapping an " +79"IllegalArgumentException");80return true;81} else {82System.out.println("TEST FAILS: wrong exception:");83e.printStackTrace(System.out);84return false;85}86} catch (Exception e) {87System.out.println("TEST FAILS: wrong exception:");88e.printStackTrace(System.out);89return false;90}9192System.out.println("TEST FAILS: exception not thrown");93return false;94}9596private static boolean testEmptyArrayParameters()97throws Exception {9899System.out.println("Test that empty and null array parameters " +100"produce the right type from getters");101102boolean ok = true;103104ModelMBeanInfoSupport mmbi;105106mmbi =107new ModelMBeanInfoSupport("bogus.class.name", "description",108null, null, null, null);109ok &= checkMMBI(mmbi, "null parameters, no descriptor");110111mmbi =112new ModelMBeanInfoSupport("bogus.class.name", "description",113null, null, null, null, null);114ok &= checkMMBI(mmbi, "null parameters, null descriptor");115116mmbi =117new ModelMBeanInfoSupport("bogus.class.name", "description",118new ModelMBeanAttributeInfo[0],119new ModelMBeanConstructorInfo[0],120new ModelMBeanOperationInfo[0],121new ModelMBeanNotificationInfo[0]);122ok &= checkMMBI(mmbi, "empty parameters, no descriptor");123124mmbi =125new ModelMBeanInfoSupport("bogus.class.name", "description",126new ModelMBeanAttributeInfo[0],127new ModelMBeanConstructorInfo[0],128new ModelMBeanOperationInfo[0],129new ModelMBeanNotificationInfo[0],130null);131ok &= checkMMBI(mmbi, "empty parameters, null descriptor");132133return ok;134}135136private static boolean checkMMBI(ModelMBeanInfoSupport mmbi, String what)137throws Exception {138String bad = "";139140if (!(mmbi.getAttributes() instanceof ModelMBeanAttributeInfo[]))141bad += " attributes";142if (!(mmbi.getConstructors() instanceof ModelMBeanConstructorInfo[]))143bad += " constructors";144if (!(mmbi.getOperations() instanceof ModelMBeanOperationInfo[]))145bad += " operations";146if (!(mmbi.getNotifications() instanceof ModelMBeanNotificationInfo[]))147bad += " notifications";148149if (bad.equals("")) {150System.out.println("..." + what + ": OK");151return true;152} else {153System.out.println("..." + what + ": FAILS for:" + bad);154return false;155}156}157158private static boolean testGetDescriptorsForMBean()159throws Exception {160System.out.println("Test getDescriptors(\"mbean\")");161162boolean ok = true;163164ModelMBeanInfo mmbi;165Descriptor[] mbeanDescrs;166167mmbi = new ModelMBeanInfoSupport("bogus.class.name", "description",168null, null, null, null);169try {170mbeanDescrs = mmbi.getDescriptors("mbean");171if (mbeanDescrs.length == 1 && mbeanDescrs[0] != null)172System.out.println("...default MBean descriptor: OK");173else {174System.out.println("...default MBean descriptor: bad array: " +175Arrays.asList(mbeanDescrs));176ok = false;177}178} catch (Exception e) {179System.out.println("...default MBean descriptor: got exception:");180e.printStackTrace(System.out);181ok = false;182}183184String[] fields = new String[] {"descriptorType", "name"};185String[] values = new String[] {"mbean", "whatsit"};186String[] defaultFields = new String[] {187"displayName", "persistPolicy", "log", "visibility",188};189String[] defaultValues = new String[] {190"bogus.class.name", "never", "F", "1",191};192Descriptor d = new DescriptorSupport(fields, values);193194mmbi = new ModelMBeanInfoSupport("bogus.class.name", "description",195null, null, null, null, d);196try {197mbeanDescrs = mmbi.getDescriptors("mbean");198} catch (Exception e) {199System.out.println("...explicit MBean descriptor: got exception:");200e.printStackTrace(System.out);201mbeanDescrs = new Descriptor[] {mmbi.getMBeanDescriptor()};202}203204if (mbeanDescrs.length == 1) {205Descriptor dd = mbeanDescrs[0];206boolean thisok = true;207208// Check that the fields we supplied survived in the copy209// and were not changed in the original210for (int i = 0; i < fields.length; i++) {211String field = fields[i];212String value;213value = (String) dd.getFieldValue(field);214if (!values[i].equals(value)) {215System.out.println("...explicit MBean descriptor: " +216"value of " + field + " mutated: " +217value);218thisok = false;219}220value = (String) d.getFieldValue(field);221if (!values[i].equals(value)) {222System.out.println("...explicit MBean descriptor: " +223"value of " + field + " changed in " +224"original: " + value);225thisok = false;226}227}228229// Check that the default fields were added230for (int i = 0; i < defaultFields.length; i++) {231String field = defaultFields[i];232String value = (String) dd.getFieldValue(field);233if (!defaultValues[i].equals(value)) {234System.out.println("...explicit MBean descriptor: " +235"default value of " + field +236" wrong: " + value + " should be " +237defaultValues[i]);238thisok = false;239}240}241242// Check that the original did not acquire new fields243if (d.getFieldNames().length != fields.length) {244Collection c = new TreeSet(Arrays.asList(d.getFieldNames()));245c.removeAll(Arrays.asList(fields));246System.out.println("...explicit MBean descriptor: " +247"acquired new fields: " + c);248thisok = false;249}250251if (thisok)252System.out.println("...explicit MBean descriptor: OK");253else254ok = false;255} else {256System.out.println("...explicit MBean descriptor: bad array: " +257Arrays.asList(mbeanDescrs));258ok = false;259}260261return ok;262}263}264265266