0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4883712 4869006 4894856 5016685
0N/A * @summary Test that DescriptorSupport correctly validates fields
0N/A * @author Eamonn McManus
0N/A * @run clean DescriptorSupportTest
0N/A * @run build DescriptorSupportTest
0N/A * @run main DescriptorSupportTest
0N/A */
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.List;
0N/Aimport javax.management.Descriptor;
0N/Aimport javax.management.RuntimeOperationsException;
0N/Aimport javax.management.modelmbean.DescriptorSupport;
0N/Aimport javax.management.modelmbean.ModelMBeanInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfoSupport;
0N/A
0N/Apublic class DescriptorSupportTest {
0N/A private static final Object[] goodFields = {
0N/A "value", "",
0N/A "severity", "0",
0N/A "severity", "6",
0N/A };
0N/A
0N/A private static final Object[] badFields = {
0N/A "name", null,
0N/A "name", "",
0N/A "descriptorType", null,
0N/A "descriptorType", "",
0N/A "setMethod", null,
0N/A "getMethod", null,
0N/A "role", null,
0N/A "class", null,
0N/A "visibility", null,
0N/A "visibility", new Integer(0),
0N/A "visibility", "0",
0N/A "visibility", new Integer(5),
0N/A "visibility", "5",
0N/A "severity", null,
0N/A "severity", new Integer(-1),
0N/A "severity", "-1",
0N/A "severity", new Integer(7),
0N/A "severity", "7",
0N/A "persistPolicy", null,
0N/A "persistPolicy", "bogusPersistPolicy",
0N/A "persistPeriod", null,
0N/A "persistPeriod", "not a number",
0N/A "currencyTimeLimit", null,
0N/A "currencyTimeLimit", "not a number",
0N/A "lastUpdatedTimeStamp", null,
0N/A "lastUpdatedTimeStamp", "not a number",
0N/A "lastReturnedTimeStamp", null,
0N/A "lastReturnedTimeStamp", "not a number",
0N/A "log", null,
0N/A "log", "not T or F or true or false",
0N/A "log", new Object[0],
0N/A };
0N/A
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A boolean ok = true;
0N/A
0N/A System.out.println("Checking that name and descriptorType are " +
0N/A "mandatory");
0N/A // Try omitting name and/or descriptorType
0N/A for (int i = 0; i < 3; i++) {
0N/A final boolean addName = ((i & 1) != 0);
0N/A final boolean addDescriptorType = ((i & 2) != 0);
0N/A final List fields = new ArrayList();
0N/A if (addName)
0N/A fields.add("name=something");
0N/A if (addDescriptorType)
0N/A fields.add("descriptorType=something-else");
0N/A final String[] fs = (String[]) fields.toArray(new String[0]);
0N/A final String what =
0N/A "DescriptorSupport with " +
0N/A (addName ? "" : "no ") + "name and " +
0N/A (addDescriptorType ? "" : "no ") + "descriptorType";
0N/A DescriptorSupport ds = new DescriptorSupport(fs);
0N/A if (ds.isValid()) {
0N/A System.out.println("INCORRECTLY ACCEPTED: " + what);
0N/A ok = false;
0N/A } else
0N/A System.out.println("OK: rejected " + what);
0N/A }
0N/A
0N/A for (int pass = 0; pass < 2; pass++) {
0N/A boolean shouldAccept = (pass == 0);
0N/A System.out.println("Trying out " +
0N/A (shouldAccept ? "correct" : "bogus") +
0N/A " DescriptorSupport fields");
0N/A Object[] fields = shouldAccept ? goodFields : badFields;
0N/A for (int i = 0; i < fields.length; i += 2) {
0N/A String[] names = {"name", "descriptorType"};
0N/A String[] values = {"some-name", "some-type"};
0N/A DescriptorSupport d = new DescriptorSupport(names, values);
0N/A final String name = (String) fields[i];
0N/A final Object value = fields[i + 1];
0N/A final String valueS =
0N/A (value instanceof String) ? ("\"" + value + "\"") :
0N/A (value == null) ? "null" : value.toString();
0N/A final String what =
0N/A "DescriptorSupport with " + name + " = " + valueS;
0N/A try {
0N/A d.setField(name, value);
0N/A if (shouldAccept)
0N/A System.out.println("OK: accepted " + what);
0N/A else {
0N/A System.out.println("INCORRECTLY ACCEPTED: " + what);
0N/A ok = false;
0N/A }
0N/A } catch (RuntimeOperationsException e) {
0N/A if (shouldAccept) {
0N/A System.out.println("INCORRECTLY REJECTED: " + what +
0N/A ": " + e);
0N/A ok = false;
0N/A } else {
0N/A System.out.println("OK: rejected " + what);
0N/A // OK: this is what should happen
0N/A }
0N/A } catch (Exception e) {
0N/A System.out.println("WRONG EXCEPTION: " + what + ": " + e);
0N/A ok = false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // 4894856: ModelMBeanInfoSupport.setDescriptor(d, "mbean") fails
0N/A System.out.println("Checking that setDescriptor(d, \"mbean\") works");
0N/A ModelMBeanInfo mmbi =
0N/A new ModelMBeanInfoSupport("x", "descr", null, null, null, null);
0N/A Descriptor d = mmbi.getDescriptor("x", "mbean");
0N/A try {
0N/A mmbi.setDescriptor(d, "mbean");
0N/A } catch (Exception e) {
0N/A System.out.println("Unexpected exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A // 5016685: DescriptorSupport forces field names to lower case
0N/A System.out.println("Checking that field name case is ignored " +
0N/A "but preserved");
0N/A ok &= caseTest(new DescriptorSupport(new String[] {"NAME=blah"}),
0N/A "DescriptorSupport(String[])");
0N/A ok &= caseTest(new DescriptorSupport(new String[] {"NAME"},
0N/A new String[] {"blah"}),
0N/A "DescriptorSupport(String[], Object[])");
0N/A DescriptorSupport d1 = new DescriptorSupport();
0N/A d1.setField("NAME", "blah");
0N/A ok &= caseTest(d1, "DescriptorSupport.setField");
0N/A d1 = new DescriptorSupport(new String[] {"NAME=blah"});
0N/A ok &= caseTest(new DescriptorSupport(d1),
0N/A "DescriptorSupport(Descriptor)");
0N/A d1 = new DescriptorSupport(new String[] {"NAME=blah"});
0N/A ok &= caseTest(new DescriptorSupport(d1.toXMLString()),
0N/A "DescriptorSupport(String)");
0N/A d1 = new DescriptorSupport(new String[] {"NAME=blah"});
0N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
0N/A ObjectOutputStream oos = new ObjectOutputStream(bos);
0N/A oos.writeObject(d1);
0N/A oos.close();
0N/A bos.close();
0N/A ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
0N/A ObjectInputStream ois = new ObjectInputStream(bis);
0N/A d1 = (DescriptorSupport) ois.readObject();
0N/A ok &= caseTest(d1, "serialized DescriptorSupport");
0N/A
0N/A if (ok)
0N/A System.out.println("Test passed");
0N/A else {
0N/A System.out.println("TEST FAILED");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A private static boolean caseTest(Descriptor d, String what) {
0N/A boolean ok = true;
0N/A
0N/A System.out.println("..." + what);
0N/A
0N/A String[] names = d.getFieldNames();
0N/A if (names.length != 1 || !names[0].equals("NAME")) {
0N/A ok = false;
0N/A System.out.println("...getFieldNames() fails: " +
0N/A Arrays.asList(names));
0N/A }
0N/A
0N/A String[] fields = d.getFields();
0N/A if (fields.length != 1 || !fields[0].equals("NAME=blah")) {
0N/A ok = false;
0N/A System.out.println("...getFields() fails: " +
0N/A Arrays.asList(fields));
0N/A }
0N/A
0N/A Object value = d.getFieldValue("namE");
0N/A if (!"blah".equals(value)) {
0N/A ok = false;
0N/A System.out.println("...getFieldValue(\"namE\") fails: " + value);
0N/A }
0N/A
0N/A Object[] values = d.getFieldValues(new String[] {"namE"});
0N/A if (values.length != 1 || !"blah".equals(values[0])) {
0N/A ok = false;
0N/A System.out.println("...getFieldValues({\"namE\"}) fails: " +
0N/A Arrays.asList(values));
0N/A }
0N/A
0N/A d.setField("namE", "newblah");
0N/A Object newblah = d.getFieldValue("Name");
0N/A if (!"newblah".equals(newblah)) {
0N/A ok = false;
0N/A System.out.println("...setField value not returned: " + newblah);
0N/A }
0N/A
0N/A d.setFields(new String[] {"NaMe"}, new Object[] {"newerblah"});
0N/A Object newerblah = d.getFieldValue("naMe");
0N/A if (!"newerblah".equals(newerblah)) {
0N/A ok = false;
0N/A System.out.println("...setFields value not returned: " +
0N/A newerblah);
0N/A }
0N/A
0N/A Descriptor d1 = (Descriptor) d.clone();
0N/A newerblah = d1.getFieldValue("NAMe");
0N/A if (!"newerblah".equals(newerblah)) {
0N/A ok = false;
0N/A System.out.println("...clone incorrect: " + newerblah);
0N/A }
0N/A
0N/A d.removeField("NAme");
0N/A names = d.getFieldNames();
0N/A if (names.length != 0) {
0N/A ok = false;
0N/A System.out.println("...removeField failed: " +
0N/A Arrays.asList(names));
0N/A }
0N/A
0N/A if (ok)
0N/A System.out.println("...succeeded");
0N/A
0N/A return ok;
0N/A }
0N/A}