1 /* 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4957393 27 * @summary Test that DescriptorSupport.toXMLString() can be used to 28 * reconstruct an equivalent DescriptorSupport 29 * @author Eamonn McManus 30 * @run clean DescriptorSupportXMLTest 31 * @run build DescriptorSupportXMLTest 32 * @run main DescriptorSupportXMLTest 33 */ 34 35 import java.util.Arrays; 36 37 import javax.management.RuntimeOperationsException; 38 import javax.management.modelmbean.DescriptorSupport; 39 40 public class DescriptorSupportXMLTest { 41 public static void main(String[] args) throws Exception { 42 System.out.println("Testing that DescriptorSupport.toXMLString() " + 43 "can be used to reconstruct an equivalent " + 44 "DescriptorSupport"); 45 int failed = 0; 46 47 final 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>", 60 null, 61 new Integer(10), 62 Boolean.TRUE, 63 new Float(1.0f), 64 65 // Values that are not encodable: it is important that we throw 66 // an exception during encoding rather than waiting until decode 67 // time to discover the problem. These classes are not encodable 68 // because they don't have a (String) constructor. 69 new Character('!'), 70 new java.util.HashMap(), 71 }; 72 73 for (int i = 0; i < testValues.length; i++) { 74 final Object v = testValues[i]; 75 final String what = 76 (v == null) ? "null" : 77 (v.getClass().getName() + "{" + v + "}"); 78 79 final DescriptorSupport in = 80 new DescriptorSupport(new String[] {"bloo"}, new Object[] {v}); 81 82 final String xml; 83 try { 84 xml = in.toXMLString(); 85 } catch (RuntimeOperationsException e) { 86 final Throwable cause = e.getCause(); 87 if (cause instanceof IllegalArgumentException) { 88 System.out.println("OK: " + what + ": got a " + 89 "RuntimeOperationsException wrapping " + 90 "an IllegalArgumentException: " + 91 cause.getMessage()); 92 } else { 93 final String causeString = 94 (cause == null) ? "null" : cause.getClass().getName(); 95 System.out.println("FAILED: " + what + ": got a " + 96 "RuntimeOperationException wrapping " + 97 causeString); 98 failed++; 99 } 100 continue; 101 } 102 103 System.out.println("Encoded " + what + " as " + xml); 104 105 final DescriptorSupport out; 106 try { 107 out = new DescriptorSupport(xml); 108 } catch (Exception e) { 109 System.out.println("FAILED: " + what + ": got an exception:"); 110 e.printStackTrace(System.out); 111 failed++; 112 continue; 113 } 114 115 final String[] names = out.getFieldNames(); 116 if (names.length != 1 || !"bloo".equals(names[0])) { 117 System.out.println("FAILED: decoded names wrong: " + 118 Arrays.asList(names)); 119 failed++; 120 continue; 121 } 122 123 final Object[] values = out.getFieldValues(names); 124 if (values.length != 1) { 125 System.out.println("FAILED: wrong number of values: " + 126 Arrays.asList(values)); 127 failed++; 128 continue; 129 } 130 131 final Object outValue = values[0]; 132 133 if (v == null) { 134 if (outValue == null) 135 System.out.println("OK: decoded null value"); 136 else { 137 System.out.println("FAILED: decoded null value as " + 138 outValue.getClass().getName() + "{" + 139 outValue + "}"); 140 failed++; 141 } 142 continue; 143 } 144 145 if (outValue == null) { 146 System.out.println("FAILED: decoded non-null value as null"); 147 failed++; 148 continue; 149 } 150 151 if (v.getClass() != outValue.getClass()) { 152 System.out.println("FAILED: decoded value has class " + 153 outValue.getClass().getName() + "{" + 154 outValue + "}"); 155 failed++; 156 continue; 157 } 158 159 if (v.equals(outValue)) 160 System.out.println("OK: decoded value is equal to original"); 161 else { 162 System.out.println("FAILED: decoded value is different: {" + 163 outValue + "}"); 164 failed++; 165 } 166 } 167 168 if (failed == 0) 169 System.out.println("OK: all tests passed"); 170 else { 171 System.out.println("TEST FAILED: fail count: " + failed); 172 System.exit(1); 173 } 174 } 175 }