Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/DescriptorSupportXMLTest.java
38840 views
/*1* Copyright (c) 2003, 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 495739326* @summary Test that DescriptorSupport.toXMLString() can be used to27* reconstruct an equivalent DescriptorSupport28* @author Eamonn McManus29* @run clean DescriptorSupportXMLTest30* @run build DescriptorSupportXMLTest31* @run main DescriptorSupportXMLTest32*/3334import java.util.Arrays;3536import javax.management.RuntimeOperationsException;37import javax.management.modelmbean.DescriptorSupport;3839public class DescriptorSupportXMLTest {40public static void main(String[] args) throws Exception {41System.out.println("Testing that DescriptorSupport.toXMLString() " +42"can be used to reconstruct an equivalent " +43"DescriptorSupport");44int failed = 0;4546final Object[] testValues = {47// Values that should be encodable.48"",49"ok",50"null",51"(open",52"close)",53"(parens)",54"quote\"quote",55"a description with several words",56"magic&\"\\<> \r\t\n\f;&;magic",57"<descriptor>&&&</descriptor>",58"<descriptor>&&&</blahblahblah>",59null,60new Integer(10),61Boolean.TRUE,62new Float(1.0f),6364// Values that are not encodable: it is important that we throw65// an exception during encoding rather than waiting until decode66// time to discover the problem. These classes are not encodable67// because they don't have a (String) constructor.68new Character('!'),69new java.util.HashMap(),70};7172for (int i = 0; i < testValues.length; i++) {73final Object v = testValues[i];74final String what =75(v == null) ? "null" :76(v.getClass().getName() + "{" + v + "}");7778final DescriptorSupport in =79new DescriptorSupport(new String[] {"bloo"}, new Object[] {v});8081final String xml;82try {83xml = in.toXMLString();84} catch (RuntimeOperationsException e) {85final Throwable cause = e.getCause();86if (cause instanceof IllegalArgumentException) {87System.out.println("OK: " + what + ": got a " +88"RuntimeOperationsException wrapping " +89"an IllegalArgumentException: " +90cause.getMessage());91} else {92final String causeString =93(cause == null) ? "null" : cause.getClass().getName();94System.out.println("FAILED: " + what + ": got a " +95"RuntimeOperationException wrapping " +96causeString);97failed++;98}99continue;100}101102System.out.println("Encoded " + what + " as " + xml);103104final DescriptorSupport out;105try {106out = new DescriptorSupport(xml);107} catch (Exception e) {108System.out.println("FAILED: " + what + ": got an exception:");109e.printStackTrace(System.out);110failed++;111continue;112}113114final String[] names = out.getFieldNames();115if (names.length != 1 || !"bloo".equals(names[0])) {116System.out.println("FAILED: decoded names wrong: " +117Arrays.asList(names));118failed++;119continue;120}121122final Object[] values = out.getFieldValues(names);123if (values.length != 1) {124System.out.println("FAILED: wrong number of values: " +125Arrays.asList(values));126failed++;127continue;128}129130final Object outValue = values[0];131132if (v == null) {133if (outValue == null)134System.out.println("OK: decoded null value");135else {136System.out.println("FAILED: decoded null value as " +137outValue.getClass().getName() + "{" +138outValue + "}");139failed++;140}141continue;142}143144if (outValue == null) {145System.out.println("FAILED: decoded non-null value as null");146failed++;147continue;148}149150if (v.getClass() != outValue.getClass()) {151System.out.println("FAILED: decoded value has class " +152outValue.getClass().getName() + "{" +153outValue + "}");154failed++;155continue;156}157158if (v.equals(outValue))159System.out.println("OK: decoded value is equal to original");160else {161System.out.println("FAILED: decoded value is different: {" +162outValue + "}");163failed++;164}165}166167if (failed == 0)168System.out.println("OK: all tests passed");169else {170System.out.println("TEST FAILED: fail count: " + failed);171System.exit(1);172}173}174}175176177