1 /*
   2  * Copyright (c) 2003, 2015, 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  * @modules java.management
  31  * @run clean DescriptorSupportXMLTest
  32  * @run build DescriptorSupportXMLTest
  33  * @run main DescriptorSupportXMLTest
  34  */
  35 
  36 import java.util.Arrays;
  37 
  38 import javax.management.RuntimeOperationsException;
  39 import javax.management.modelmbean.DescriptorSupport;
  40 
  41 public class DescriptorSupportXMLTest {
  42     public static void main(String[] args) throws Exception {
  43         System.out.println("Testing that DescriptorSupport.toXMLString() " +
  44                            "can be used to reconstruct an equivalent " +
  45                            "DescriptorSupport");
  46         int failed = 0;
  47 
  48         final Object[] testValues = {
  49             // Values that should be encodable.
  50             "",
  51             "ok",
  52             "null",
  53             "(open",
  54             "close)",
  55             "(parens)",
  56             "quote\"quote",
  57             "a description with several words",
  58             "magic&\"\\<> \r\t\n\f;&;magic",
  59             "&lt;descriptor&gt;&&&&lt;/descriptor&gt;",
  60             "&lt;descriptor&gt;&&&&lt;/blahblahblah&gt;",
  61             null,
  62             new Integer(10),
  63             Boolean.TRUE,
  64             new Float(1.0f),
  65 
  66             // Values that are not encodable: it is important that we throw
  67             // an exception during encoding rather than waiting until decode
  68             // time to discover the problem.  These classes are not encodable
  69             // because they don't have a (String) constructor.
  70             new Character('!'),
  71             new java.util.HashMap(),
  72         };
  73 
  74         for (int i = 0; i < testValues.length; i++) {
  75             final Object v = testValues[i];
  76             final String what =
  77                 (v == null) ? "null" :
  78                 (v.getClass().getName() + "{" + v + "}");
  79 
  80             final DescriptorSupport in =
  81                 new DescriptorSupport(new String[] {"bloo"}, new Object[] {v});
  82 
  83             final String xml;
  84             try {
  85                 xml = in.toXMLString();
  86             } catch (RuntimeOperationsException e) {
  87                 final Throwable cause = e.getCause();
  88                 if (cause instanceof IllegalArgumentException) {
  89                     System.out.println("OK: " + what + ": got a " +
  90                                        "RuntimeOperationsException wrapping " +
  91                                        "an IllegalArgumentException: " +
  92                                        cause.getMessage());
  93                 } else {
  94                     final String causeString =
  95                         (cause == null) ? "null" : cause.getClass().getName();
  96                     System.out.println("FAILED: " + what + ": got a " +
  97                                        "RuntimeOperationException wrapping " +
  98                                        causeString);
  99                     failed++;
 100                 }
 101                 continue;
 102             }
 103 
 104             System.out.println("Encoded " + what + " as " + xml);
 105 
 106             final DescriptorSupport out;
 107             try {
 108                 out = new DescriptorSupport(xml);
 109             } catch (Exception e) {
 110                 System.out.println("FAILED: " + what + ": got an exception:");
 111                 e.printStackTrace(System.out);
 112                 failed++;
 113                 continue;
 114             }
 115 
 116             final String[] names = out.getFieldNames();
 117             if (names.length != 1 || !"bloo".equals(names[0])) {
 118                 System.out.println("FAILED: decoded names wrong: " +
 119                                    Arrays.asList(names));
 120                 failed++;
 121                 continue;
 122             }
 123 
 124             final Object[] values = out.getFieldValues(names);
 125             if (values.length != 1) {
 126                 System.out.println("FAILED: wrong number of values: " +
 127                                    Arrays.asList(values));
 128                 failed++;
 129                 continue;
 130             }
 131 
 132             final Object outValue = values[0];
 133 
 134             if (v == null) {
 135                 if (outValue == null)
 136                     System.out.println("OK: decoded null value");
 137                 else {
 138                     System.out.println("FAILED: decoded null value as " +
 139                                        outValue.getClass().getName() + "{" +
 140                                        outValue + "}");
 141                     failed++;
 142                 }
 143                 continue;
 144             }
 145 
 146             if (outValue == null) {
 147                 System.out.println("FAILED: decoded non-null value as null");
 148                 failed++;
 149                 continue;
 150             }
 151 
 152             if (v.getClass() != outValue.getClass()) {
 153                 System.out.println("FAILED: decoded value has class " +
 154                                    outValue.getClass().getName() + "{" +
 155                                    outValue + "}");
 156                 failed++;
 157                 continue;
 158             }
 159 
 160             if (v.equals(outValue))
 161                 System.out.println("OK: decoded value is equal to original");
 162             else {
 163                 System.out.println("FAILED: decoded value is different: {" +
 164                                    outValue + "}");
 165                 failed++;
 166             }
 167         }
 168 
 169         if (failed == 0)
 170             System.out.println("OK: all tests passed");
 171         else {
 172             System.out.println("TEST FAILED: fail count: " + failed);
 173             System.exit(1);
 174         }
 175     }
 176 }