Path: blob/master/test/jdk/javax/management/modelmbean/DescriptorSupportTest.java
41152 views
/*1* Copyright (c) 2003, 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 4883712 4869006 4894856 501668526* @summary Test that DescriptorSupport correctly validates fields27* @author Eamonn McManus28*29* @run clean DescriptorSupportTest30* @run build DescriptorSupportTest31* @run main DescriptorSupportTest32*/3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.ObjectInputStream;37import java.io.ObjectOutputStream;38import java.util.ArrayList;39import java.util.Arrays;40import java.util.List;41import javax.management.Descriptor;42import javax.management.RuntimeOperationsException;43import javax.management.modelmbean.DescriptorSupport;44import javax.management.modelmbean.ModelMBeanInfo;45import javax.management.modelmbean.ModelMBeanInfoSupport;4647public class DescriptorSupportTest {48private static final Object[] goodFields = {49"value", "",50"severity", "0",51"severity", "6",52};5354private static final Object[] badFields = {55"name", null,56"name", "",57"descriptorType", null,58"descriptorType", "",59"setMethod", null,60"getMethod", null,61"role", null,62"class", null,63"visibility", null,64"visibility", new Integer(0),65"visibility", "0",66"visibility", new Integer(5),67"visibility", "5",68"severity", null,69"severity", new Integer(-1),70"severity", "-1",71"severity", new Integer(7),72"severity", "7",73"persistPolicy", null,74"persistPolicy", "bogusPersistPolicy",75"persistPeriod", null,76"persistPeriod", "not a number",77"currencyTimeLimit", null,78"currencyTimeLimit", "not a number",79"lastUpdatedTimeStamp", null,80"lastUpdatedTimeStamp", "not a number",81"lastReturnedTimeStamp", null,82"lastReturnedTimeStamp", "not a number",83"log", null,84"log", "not T or F or true or false",85"log", new Object[0],86};878889public static void main(String[] args) throws Exception {90boolean ok = true;9192System.out.println("Checking that name and descriptorType are " +93"mandatory");94// Try omitting name and/or descriptorType95for (int i = 0; i < 3; i++) {96final boolean addName = ((i & 1) != 0);97final boolean addDescriptorType = ((i & 2) != 0);98final List fields = new ArrayList();99if (addName)100fields.add("name=something");101if (addDescriptorType)102fields.add("descriptorType=something-else");103final String[] fs = (String[]) fields.toArray(new String[0]);104final String what =105"DescriptorSupport with " +106(addName ? "" : "no ") + "name and " +107(addDescriptorType ? "" : "no ") + "descriptorType";108DescriptorSupport ds = new DescriptorSupport(fs);109if (ds.isValid()) {110System.out.println("INCORRECTLY ACCEPTED: " + what);111ok = false;112} else113System.out.println("OK: rejected " + what);114}115116for (int pass = 0; pass < 2; pass++) {117boolean shouldAccept = (pass == 0);118System.out.println("Trying out " +119(shouldAccept ? "correct" : "bogus") +120" DescriptorSupport fields");121Object[] fields = shouldAccept ? goodFields : badFields;122for (int i = 0; i < fields.length; i += 2) {123String[] names = {"name", "descriptorType"};124String[] values = {"some-name", "some-type"};125DescriptorSupport d = new DescriptorSupport(names, values);126final String name = (String) fields[i];127final Object value = fields[i + 1];128final String valueS =129(value instanceof String) ? ("\"" + value + "\"") :130(value == null) ? "null" : value.toString();131final String what =132"DescriptorSupport with " + name + " = " + valueS;133try {134d.setField(name, value);135if (shouldAccept)136System.out.println("OK: accepted " + what);137else {138System.out.println("INCORRECTLY ACCEPTED: " + what);139ok = false;140}141} catch (RuntimeOperationsException e) {142if (shouldAccept) {143System.out.println("INCORRECTLY REJECTED: " + what +144": " + e);145ok = false;146} else {147System.out.println("OK: rejected " + what);148// OK: this is what should happen149}150} catch (Exception e) {151System.out.println("WRONG EXCEPTION: " + what + ": " + e);152ok = false;153}154}155}156157// 4894856: ModelMBeanInfoSupport.setDescriptor(d, "mbean") fails158System.out.println("Checking that setDescriptor(d, \"mbean\") works");159ModelMBeanInfo mmbi =160new ModelMBeanInfoSupport("x", "descr", null, null, null, null);161Descriptor d = mmbi.getDescriptor("x", "mbean");162try {163mmbi.setDescriptor(d, "mbean");164} catch (Exception e) {165System.out.println("Unexpected exception:");166e.printStackTrace(System.out);167ok = false;168}169170// 5016685: DescriptorSupport forces field names to lower case171System.out.println("Checking that field name case is ignored " +172"but preserved");173ok &= caseTest(new DescriptorSupport(new String[] {"NAME=blah"}),174"DescriptorSupport(String[])");175ok &= caseTest(new DescriptorSupport(new String[] {"NAME"},176new String[] {"blah"}),177"DescriptorSupport(String[], Object[])");178DescriptorSupport d1 = new DescriptorSupport();179d1.setField("NAME", "blah");180ok &= caseTest(d1, "DescriptorSupport.setField");181d1 = new DescriptorSupport(new String[] {"NAME=blah"});182ok &= caseTest(new DescriptorSupport(d1),183"DescriptorSupport(Descriptor)");184d1 = new DescriptorSupport(new String[] {"NAME=blah"});185ok &= caseTest(new DescriptorSupport(d1.toXMLString()),186"DescriptorSupport(String)");187d1 = new DescriptorSupport(new String[] {"NAME=blah"});188ByteArrayOutputStream bos = new ByteArrayOutputStream();189ObjectOutputStream oos = new ObjectOutputStream(bos);190oos.writeObject(d1);191oos.close();192bos.close();193ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());194ObjectInputStream ois = new ObjectInputStream(bis);195d1 = (DescriptorSupport) ois.readObject();196ok &= caseTest(d1, "serialized DescriptorSupport");197198if (ok)199System.out.println("Test passed");200else {201System.out.println("TEST FAILED");202System.exit(1);203}204}205206private static boolean caseTest(Descriptor d, String what) {207boolean ok = true;208209System.out.println("..." + what);210211String[] names = d.getFieldNames();212if (names.length != 1 || !names[0].equals("NAME")) {213ok = false;214System.out.println("...getFieldNames() fails: " +215Arrays.asList(names));216}217218String[] fields = d.getFields();219if (fields.length != 1 || !fields[0].equals("NAME=blah")) {220ok = false;221System.out.println("...getFields() fails: " +222Arrays.asList(fields));223}224225Object value = d.getFieldValue("namE");226if (!"blah".equals(value)) {227ok = false;228System.out.println("...getFieldValue(\"namE\") fails: " + value);229}230231Object[] values = d.getFieldValues(new String[] {"namE"});232if (values.length != 1 || !"blah".equals(values[0])) {233ok = false;234System.out.println("...getFieldValues({\"namE\"}) fails: " +235Arrays.asList(values));236}237238d.setField("namE", "newblah");239Object newblah = d.getFieldValue("Name");240if (!"newblah".equals(newblah)) {241ok = false;242System.out.println("...setField value not returned: " + newblah);243}244245d.setFields(new String[] {"NaMe"}, new Object[] {"newerblah"});246Object newerblah = d.getFieldValue("naMe");247if (!"newerblah".equals(newerblah)) {248ok = false;249System.out.println("...setFields value not returned: " +250newerblah);251}252253Descriptor d1 = (Descriptor) d.clone();254newerblah = d1.getFieldValue("NAMe");255if (!"newerblah".equals(newerblah)) {256ok = false;257System.out.println("...clone incorrect: " + newerblah);258}259260d.removeField("NAme");261names = d.getFieldNames();262if (names.length != 0) {263ok = false;264System.out.println("...removeField failed: " +265Arrays.asList(names));266}267268if (ok)269System.out.println("...succeeded");270271return ok;272}273}274275276