1 /*
   2  * Copyright (c) 2008, 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 6610174
  27  * @summary Test that CompositeDataSupport.toString() represents arrays correctly
  28  * @author Eamonn McManus
  29  * @modules java.management
  30  */
  31 
  32 import javax.management.openmbean.ArrayType;
  33 import javax.management.openmbean.CompositeData;
  34 import javax.management.openmbean.CompositeDataSupport;
  35 import javax.management.openmbean.CompositeType;
  36 import javax.management.openmbean.OpenType;
  37 import javax.management.openmbean.SimpleType;
  38 
  39 public class CompositeDataStringTest {
  40     public static void main(String[] args) throws Exception {
  41         CompositeType basicCT = new CompositeType(
  42                 "basicCT", "basic CompositeType",
  43                 new String[] {"name", "value"},
  44                 new String[] {"name", "value"},
  45                 new OpenType<?>[] {SimpleType.STRING, SimpleType.INTEGER});
  46         CompositeType ct = new CompositeType(
  47                 "noddy", "descr",
  48                 new String[] {"strings", "ints", "cds"},
  49                 new String[] {"string array", "int array", "composite data array"},
  50                 new OpenType<?>[] {
  51                     ArrayType.getArrayType(SimpleType.STRING),
  52                     ArrayType.getPrimitiveArrayType(int[].class),
  53                     ArrayType.getArrayType(basicCT)
  54                 });
  55         CompositeData basicCD1 = new CompositeDataSupport(
  56                 basicCT, new String[] {"name", "value"}, new Object[] {"ceathar", 4});
  57         CompositeData basicCD2 = new CompositeDataSupport(
  58                 basicCT, new String[] {"name", "value"}, new Object[] {"naoi", 9});
  59         CompositeData cd = new CompositeDataSupport(
  60                 ct,
  61                 new String[] {"strings", "ints", "cds"},
  62                 new Object[] {
  63                     new String[] {"fred", "jim", "sheila"},
  64                     new int[] {2, 3, 5, 7},
  65                     new CompositeData[] {basicCD1, basicCD2}
  66                 });
  67         String s = cd.toString();
  68         System.out.println("CompositeDataSupport.toString(): " + s);
  69         String[] expected = {
  70             "fred, jim, sheila",
  71             "2, 3, 5, 7",
  72             "ceathar",
  73             "naoi",
  74         };
  75         boolean ok = true;
  76         for (String expect : expected) {
  77             if (s.contains(expect))
  78                 System.out.println("OK: string contains <" + expect + ">");
  79             else {
  80                 ok = false;
  81                 System.out.println("NOT OK: string does not contain <" +
  82                         expect + ">");
  83             }
  84         }
  85         if (ok)
  86             System.out.println("TEST PASSED");
  87         else
  88             throw new Exception("TEST FAILED: string did not contain expected substrings");
  89     }
  90 }