Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/DescriptorSupportTest.java
38840 views
/*1* Copyright (c) 2003, 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 4883712 4869006 4894856 501668526* @summary Test that DescriptorSupport correctly validates fields27* @author Eamonn McManus28* @run clean DescriptorSupportTest29* @run build DescriptorSupportTest30* @run main DescriptorSupportTest31*/3233import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.io.ObjectInputStream;36import java.io.ObjectOutputStream;37import java.util.ArrayList;38import java.util.Arrays;39import java.util.List;40import javax.management.Descriptor;41import javax.management.RuntimeOperationsException;42import javax.management.modelmbean.DescriptorSupport;43import javax.management.modelmbean.ModelMBeanInfo;44import javax.management.modelmbean.ModelMBeanInfoSupport;4546public class DescriptorSupportTest {47private static final Object[] goodFields = {48"value", "",49"severity", "0",50"severity", "6",51};5253private static final Object[] badFields = {54"name", null,55"name", "",56"descriptorType", null,57"descriptorType", "",58"setMethod", null,59"getMethod", null,60"role", null,61"class", null,62"visibility", null,63"visibility", new Integer(0),64"visibility", "0",65"visibility", new Integer(5),66"visibility", "5",67"severity", null,68"severity", new Integer(-1),69"severity", "-1",70"severity", new Integer(7),71"severity", "7",72"persistPolicy", null,73"persistPolicy", "bogusPersistPolicy",74"persistPeriod", null,75"persistPeriod", "not a number",76"currencyTimeLimit", null,77"currencyTimeLimit", "not a number",78"lastUpdatedTimeStamp", null,79"lastUpdatedTimeStamp", "not a number",80"lastReturnedTimeStamp", null,81"lastReturnedTimeStamp", "not a number",82"log", null,83"log", "not T or F or true or false",84"log", new Object[0],85};868788public static void main(String[] args) throws Exception {89boolean ok = true;9091System.out.println("Checking that name and descriptorType are " +92"mandatory");93// Try omitting name and/or descriptorType94for (int i = 0; i < 3; i++) {95final boolean addName = ((i & 1) != 0);96final boolean addDescriptorType = ((i & 2) != 0);97final List fields = new ArrayList();98if (addName)99fields.add("name=something");100if (addDescriptorType)101fields.add("descriptorType=something-else");102final String[] fs = (String[]) fields.toArray(new String[0]);103final String what =104"DescriptorSupport with " +105(addName ? "" : "no ") + "name and " +106(addDescriptorType ? "" : "no ") + "descriptorType";107DescriptorSupport ds = new DescriptorSupport(fs);108if (ds.isValid()) {109System.out.println("INCORRECTLY ACCEPTED: " + what);110ok = false;111} else112System.out.println("OK: rejected " + what);113}114115for (int pass = 0; pass < 2; pass++) {116boolean shouldAccept = (pass == 0);117System.out.println("Trying out " +118(shouldAccept ? "correct" : "bogus") +119" DescriptorSupport fields");120Object[] fields = shouldAccept ? goodFields : badFields;121for (int i = 0; i < fields.length; i += 2) {122String[] names = {"name", "descriptorType"};123String[] values = {"some-name", "some-type"};124DescriptorSupport d = new DescriptorSupport(names, values);125final String name = (String) fields[i];126final Object value = fields[i + 1];127final String valueS =128(value instanceof String) ? ("\"" + value + "\"") :129(value == null) ? "null" : value.toString();130final String what =131"DescriptorSupport with " + name + " = " + valueS;132try {133d.setField(name, value);134if (shouldAccept)135System.out.println("OK: accepted " + what);136else {137System.out.println("INCORRECTLY ACCEPTED: " + what);138ok = false;139}140} catch (RuntimeOperationsException e) {141if (shouldAccept) {142System.out.println("INCORRECTLY REJECTED: " + what +143": " + e);144ok = false;145} else {146System.out.println("OK: rejected " + what);147// OK: this is what should happen148}149} catch (Exception e) {150System.out.println("WRONG EXCEPTION: " + what + ": " + e);151ok = false;152}153}154}155156// 4894856: ModelMBeanInfoSupport.setDescriptor(d, "mbean") fails157System.out.println("Checking that setDescriptor(d, \"mbean\") works");158ModelMBeanInfo mmbi =159new ModelMBeanInfoSupport("x", "descr", null, null, null, null);160Descriptor d = mmbi.getDescriptor("x", "mbean");161try {162mmbi.setDescriptor(d, "mbean");163} catch (Exception e) {164System.out.println("Unexpected exception:");165e.printStackTrace(System.out);166ok = false;167}168169// 5016685: DescriptorSupport forces field names to lower case170System.out.println("Checking that field name case is ignored " +171"but preserved");172ok &= caseTest(new DescriptorSupport(new String[] {"NAME=blah"}),173"DescriptorSupport(String[])");174ok &= caseTest(new DescriptorSupport(new String[] {"NAME"},175new String[] {"blah"}),176"DescriptorSupport(String[], Object[])");177DescriptorSupport d1 = new DescriptorSupport();178d1.setField("NAME", "blah");179ok &= caseTest(d1, "DescriptorSupport.setField");180d1 = new DescriptorSupport(new String[] {"NAME=blah"});181ok &= caseTest(new DescriptorSupport(d1),182"DescriptorSupport(Descriptor)");183d1 = new DescriptorSupport(new String[] {"NAME=blah"});184ok &= caseTest(new DescriptorSupport(d1.toXMLString()),185"DescriptorSupport(String)");186d1 = new DescriptorSupport(new String[] {"NAME=blah"});187ByteArrayOutputStream bos = new ByteArrayOutputStream();188ObjectOutputStream oos = new ObjectOutputStream(bos);189oos.writeObject(d1);190oos.close();191bos.close();192ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());193ObjectInputStream ois = new ObjectInputStream(bis);194d1 = (DescriptorSupport) ois.readObject();195ok &= caseTest(d1, "serialized DescriptorSupport");196197if (ok)198System.out.println("Test passed");199else {200System.out.println("TEST FAILED");201System.exit(1);202}203}204205private static boolean caseTest(Descriptor d, String what) {206boolean ok = true;207208System.out.println("..." + what);209210String[] names = d.getFieldNames();211if (names.length != 1 || !names[0].equals("NAME")) {212ok = false;213System.out.println("...getFieldNames() fails: " +214Arrays.asList(names));215}216217String[] fields = d.getFields();218if (fields.length != 1 || !fields[0].equals("NAME=blah")) {219ok = false;220System.out.println("...getFields() fails: " +221Arrays.asList(fields));222}223224Object value = d.getFieldValue("namE");225if (!"blah".equals(value)) {226ok = false;227System.out.println("...getFieldValue(\"namE\") fails: " + value);228}229230Object[] values = d.getFieldValues(new String[] {"namE"});231if (values.length != 1 || !"blah".equals(values[0])) {232ok = false;233System.out.println("...getFieldValues({\"namE\"}) fails: " +234Arrays.asList(values));235}236237d.setField("namE", "newblah");238Object newblah = d.getFieldValue("Name");239if (!"newblah".equals(newblah)) {240ok = false;241System.out.println("...setField value not returned: " + newblah);242}243244d.setFields(new String[] {"NaMe"}, new Object[] {"newerblah"});245Object newerblah = d.getFieldValue("naMe");246if (!"newerblah".equals(newerblah)) {247ok = false;248System.out.println("...setFields value not returned: " +249newerblah);250}251252Descriptor d1 = (Descriptor) d.clone();253newerblah = d1.getFieldValue("NAMe");254if (!"newerblah".equals(newerblah)) {255ok = false;256System.out.println("...clone incorrect: " + newerblah);257}258259d.removeField("NAme");260names = d.getFieldNames();261if (names.length != 0) {262ok = false;263System.out.println("...removeField failed: " +264Arrays.asList(names));265}266267if (ok)268System.out.println("...succeeded");269270return ok;271}272}273274275