Path: blob/master/test/jdk/javax/management/modelmbean/DescriptorSupportXMLTest.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 495739326* @summary Test that DescriptorSupport.toXMLString() can be used to27* reconstruct an equivalent DescriptorSupport28* @author Eamonn McManus29*30* @run clean DescriptorSupportXMLTest31* @run build DescriptorSupportXMLTest32* @run main DescriptorSupportXMLTest33*/3435import java.util.Arrays;3637import javax.management.RuntimeOperationsException;38import javax.management.modelmbean.DescriptorSupport;3940public class DescriptorSupportXMLTest {41public static void main(String[] args) throws Exception {42System.out.println("Testing that DescriptorSupport.toXMLString() " +43"can be used to reconstruct an equivalent " +44"DescriptorSupport");45int failed = 0;4647final Object[] testValues = {48// Values that should be encodable.49"",50"ok",51"null",52"(open",53"close)",54"(parens)",55"quote\"quote",56"a description with several words",57"magic&\"\\<> \r\t\n\f;&;magic",58"<descriptor>&&&</descriptor>",59"<descriptor>&&&</blahblahblah>",60null,61new Integer(10),62Boolean.TRUE,63new Float(1.0f),6465// Values that are not encodable: it is important that we throw66// an exception during encoding rather than waiting until decode67// time to discover the problem. These classes are not encodable68// because they don't have a (String) constructor.69new Character('!'),70new java.util.HashMap(),71};7273for (int i = 0; i < testValues.length; i++) {74final Object v = testValues[i];75final String what =76(v == null) ? "null" :77(v.getClass().getName() + "{" + v + "}");7879final DescriptorSupport in =80new DescriptorSupport(new String[] {"bloo"}, new Object[] {v});8182final String xml;83try {84xml = in.toXMLString();85} catch (RuntimeOperationsException e) {86final Throwable cause = e.getCause();87if (cause instanceof IllegalArgumentException) {88System.out.println("OK: " + what + ": got a " +89"RuntimeOperationsException wrapping " +90"an IllegalArgumentException: " +91cause.getMessage());92} else {93final String causeString =94(cause == null) ? "null" : cause.getClass().getName();95System.out.println("FAILED: " + what + ": got a " +96"RuntimeOperationException wrapping " +97causeString);98failed++;99}100continue;101}102103System.out.println("Encoded " + what + " as " + xml);104105final DescriptorSupport out;106try {107out = new DescriptorSupport(xml);108} catch (Exception e) {109System.out.println("FAILED: " + what + ": got an exception:");110e.printStackTrace(System.out);111failed++;112continue;113}114115final String[] names = out.getFieldNames();116if (names.length != 1 || !"bloo".equals(names[0])) {117System.out.println("FAILED: decoded names wrong: " +118Arrays.asList(names));119failed++;120continue;121}122123final Object[] values = out.getFieldValues(names);124if (values.length != 1) {125System.out.println("FAILED: wrong number of values: " +126Arrays.asList(values));127failed++;128continue;129}130131final Object outValue = values[0];132133if (v == null) {134if (outValue == null)135System.out.println("OK: decoded null value");136else {137System.out.println("FAILED: decoded null value as " +138outValue.getClass().getName() + "{" +139outValue + "}");140failed++;141}142continue;143}144145if (outValue == null) {146System.out.println("FAILED: decoded non-null value as null");147failed++;148continue;149}150151if (v.getClass() != outValue.getClass()) {152System.out.println("FAILED: decoded value has class " +153outValue.getClass().getName() + "{" +154outValue + "}");155failed++;156continue;157}158159if (v.equals(outValue))160System.out.println("OK: decoded value is equal to original");161else {162System.out.println("FAILED: decoded value is different: {" +163outValue + "}");164failed++;165}166}167168if (failed == 0)169System.out.println("OK: all tests passed");170else {171System.out.println("TEST FAILED: fail count: " + failed);172System.exit(1);173}174}175}176177178