3063N/A/*
3063N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3063N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3063N/A *
3063N/A * This code is free software; you can redistribute it and/or modify it
3063N/A * under the terms of the GNU General Public License version 2 only, as
3063N/A * published by the Free Software Foundation.
3063N/A *
3063N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3063N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3063N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3063N/A * version 2 for more details (a copy is included in the LICENSE file that
3063N/A * accompanied this code).
3063N/A *
3063N/A * You should have received a copy of the GNU General Public License version
3063N/A * 2 along with this work; if not, write to the Free Software Foundation,
3063N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3063N/A *
3063N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3063N/A * or visit www.oracle.com if you need additional information or have any
3063N/A * questions.
3063N/A */
3063N/A
3063N/A/*
3063N/A * @test
3063N/A * @bug 4957393
3063N/A * @summary Test that DescriptorSupport.toXMLString() can be used to
3063N/A * reconstruct an equivalent DescriptorSupport
3063N/A * @author Eamonn McManus
3063N/A * @run clean DescriptorSupportXMLTest
3063N/A * @run build DescriptorSupportXMLTest
3063N/A * @run main DescriptorSupportXMLTest
3063N/A */
3063N/A
3063N/Aimport java.util.Arrays;
3063N/A
3063N/Aimport javax.management.RuntimeOperationsException;
3063N/Aimport javax.management.modelmbean.DescriptorSupport;
3063N/A
3063N/Apublic class DescriptorSupportXMLTest {
3063N/A public static void main(String[] args) throws Exception {
3063N/A System.out.println("Testing that DescriptorSupport.toXMLString() " +
3063N/A "can be used to reconstruct an equivalent " +
3063N/A "DescriptorSupport");
3063N/A int failed = 0;
3063N/A
3063N/A final Object[] testValues = {
3063N/A // Values that should be encodable.
3063N/A "",
3063N/A "ok",
3063N/A "null",
3063N/A "(open",
3063N/A "close)",
3063N/A "(parens)",
3063N/A "quote\"quote",
3063N/A "a description with several words",
3063N/A "magic&\"\\<> \r\t\n\f;&;magic",
3063N/A "&lt;descriptor&gt;&&&&lt;/descriptor&gt;",
3063N/A "&lt;descriptor&gt;&&&&lt;/blahblahblah&gt;",
3063N/A null,
3063N/A new Integer(10),
3063N/A Boolean.TRUE,
3063N/A new Float(1.0f),
3063N/A
3063N/A // Values that are not encodable: it is important that we throw
3063N/A // an exception during encoding rather than waiting until decode
3063N/A // time to discover the problem. These classes are not encodable
3063N/A // because they don't have a (String) constructor.
3067N/A new Character('!'),
3063N/A new java.util.HashMap(),
3067N/A };
3067N/A
3119N/A for (int i = 0; i < testValues.length; i++) {
3119N/A final Object v = testValues[i];
3063N/A final String what =
3063N/A (v == null) ? "null" :
3063N/A (v.getClass().getName() + "{" + v + "}");
3067N/A
3067N/A final DescriptorSupport in =
3119N/A new DescriptorSupport(new String[] {"bloo"}, new Object[] {v});
3119N/A
3067N/A final String xml;
3063N/A try {
3067N/A xml = in.toXMLString();
3119N/A } catch (RuntimeOperationsException e) {
3119N/A final Throwable cause = e.getCause();
3063N/A if (cause instanceof IllegalArgumentException) {
3067N/A System.out.println("OK: " + what + ": got a " +
3063N/A "RuntimeOperationsException wrapping " +
3063N/A "an IllegalArgumentException: " +
3063N/A cause.getMessage());
3063N/A } else {
3063N/A final String causeString =
3063N/A (cause == null) ? "null" : cause.getClass().getName();
3063N/A System.out.println("FAILED: " + what + ": got a " +
3063N/A "RuntimeOperationException wrapping " +
3063N/A causeString);
3063N/A failed++;
3063N/A }
3063N/A continue;
3063N/A }
3063N/A
3063N/A System.out.println("Encoded " + what + " as " + xml);
3063N/A
3063N/A final DescriptorSupport out;
3063N/A try {
3063N/A out = new DescriptorSupport(xml);
3063N/A } catch (Exception e) {
3063N/A System.out.println("FAILED: " + what + ": got an exception:");
3063N/A e.printStackTrace(System.out);
3063N/A failed++;
3063N/A continue;
3063N/A }
3067N/A
3067N/A final String[] names = out.getFieldNames();
3067N/A if (names.length != 1 || !"bloo".equals(names[0])) {
3067N/A System.out.println("FAILED: decoded names wrong: " +
3063N/A Arrays.asList(names));
3067N/A failed++;
3067N/A continue;
3067N/A }
3067N/A
3067N/A final Object[] values = out.getFieldValues(names);
3067N/A if (values.length != 1) {
3067N/A System.out.println("FAILED: wrong number of values: " +
3067N/A Arrays.asList(values));
3067N/A failed++;
3067N/A continue;
3067N/A }
3120N/A
3063N/A final Object outValue = values[0];
3067N/A
3063N/A if (v == null) {
3063N/A if (outValue == null)
3063N/A System.out.println("OK: decoded null value");
3063N/A else {
3063N/A System.out.println("FAILED: decoded null value as " +
3063N/A outValue.getClass().getName() + "{" +
3063N/A outValue + "}");
3063N/A failed++;
3063N/A }
3063N/A continue;
3063N/A }
3063N/A
3063N/A if (outValue == null) {
3063N/A System.out.println("FAILED: decoded non-null value as null");
3063N/A failed++;
3063N/A continue;
3063N/A }
3063N/A
3063N/A if (v.getClass() != outValue.getClass()) {
3067N/A System.out.println("FAILED: decoded value has class " +
3063N/A outValue.getClass().getName() + "{" +
3063N/A outValue + "}");
3063N/A failed++;
3063N/A continue;
3063N/A }
3063N/A
3063N/A if (v.equals(outValue))
3063N/A System.out.println("OK: decoded value is equal to original");
3063N/A else {
3119N/A System.out.println("FAILED: decoded value is different: {" +
3063N/A outValue + "}");
3063N/A failed++;
3063N/A }
3119N/A }
3119N/A
3063N/A if (failed == 0)
3119N/A System.out.println("OK: all tests passed");
3063N/A else {
3063N/A System.out.println("TEST FAILED: fail count: " + failed);
3067N/A System.exit(1);
3067N/A }
3067N/A }
3063N/A}
3063N/A