1 /*
   2  * Copyright (c) 2005, 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 6292705
  27  * @summary Test support for arrays in parameterized types.
  28  * @author Luis-Miguel Alventosa
  29  * @modules java.management
  30  * @run clean GenericArrayTypeTest
  31  * @run build GenericArrayTypeTest
  32  * @run main GenericArrayTypeTest
  33  */
  34 
  35 import java.lang.management.ManagementFactory;
  36 import java.util.ArrayList;
  37 import java.util.HashMap;
  38 import java.util.HashSet;
  39 import java.util.List;
  40 import java.util.Map;
  41 import java.util.Set;
  42 import javax.management.Attribute;
  43 import javax.management.JMX;
  44 import javax.management.MBeanServer;
  45 import javax.management.MBeanServerConnection;
  46 import javax.management.ObjectName;
  47 import javax.management.StandardMBean;
  48 import javax.management.openmbean.CompositeData;
  49 import javax.management.remote.JMXConnector;
  50 import javax.management.remote.JMXConnectorFactory;
  51 import javax.management.remote.JMXConnectorServer;
  52 import javax.management.remote.JMXConnectorServerFactory;
  53 import javax.management.remote.JMXServiceURL;
  54 
  55 public class GenericArrayTypeTest {
  56     // A version of java.lang.management.MonitorInfo so we can run this test
  57     // on JDK 5, where that class didn't exist.
  58     public static class MonitorInfo {
  59         private final String className;
  60         private final int identityHashCode;
  61         private final int lockedStackDepth;
  62         private final StackTraceElement lockedStackFrame;
  63 
  64         public MonitorInfo(
  65                 String className, int identityHashCode,
  66                 int lockedStackDepth, StackTraceElement lockedStackFrame) {
  67             this.className = className;
  68             this.identityHashCode = identityHashCode;
  69             this.lockedStackDepth = lockedStackDepth;
  70             this.lockedStackFrame = lockedStackFrame;
  71         }
  72 
  73         public static MonitorInfo from(CompositeData cd) {
  74             try {
  75                 CompositeData stecd = (CompositeData) cd.get("lockedStackFrame");
  76                 StackTraceElement ste = new StackTraceElement(
  77                         (String) stecd.get("className"),
  78                         (String) stecd.get("methodName"),
  79                         (String) stecd.get("fileName"),
  80                         (Integer) stecd.get("lineNumber"));
  81                 return new MonitorInfo(
  82                         (String) cd.get("className"),
  83                         (Integer) cd.get("identityHashCode"),
  84                         (Integer) cd.get("lockedStackDepth"),
  85                         ste);
  86             } catch (Exception e) {
  87                 throw new RuntimeException(e);
  88             }
  89         }
  90 
  91         public String getClassName() {
  92             return className;
  93         }
  94 
  95         public int getIdentityHashCode() {
  96             return identityHashCode;
  97         }
  98 
  99         public int getLockedStackDepth() {
 100             return lockedStackDepth;
 101         }
 102 
 103         public StackTraceElement getLockedStackFrame() {
 104             return lockedStackFrame;
 105         }
 106     }
 107 
 108 
 109     public interface TestMXBean {
 110 
 111         public String[] getT1();
 112         public void setT1(String[] v);
 113 
 114         public MonitorInfo[] getT2();
 115         public void setT2(MonitorInfo[] v);
 116 
 117         public Map<String,String[]> getT3();
 118         public void setT3(Map<String,String[]> v);
 119 
 120         public Map<String,MonitorInfo[]> getT4();
 121         public void setT4(Map<String,MonitorInfo[]> v);
 122 
 123         public Set<String[]> getT5();
 124         public void setT5(Set<String[]> v);
 125 
 126         public Set<MonitorInfo[]> getT6();
 127         public void setT6(Set<MonitorInfo[]> v);
 128 
 129         public List<String[]> getT7();
 130         public void setT7(List<String[]> v);
 131 
 132         public List<MonitorInfo[]> getT8();
 133         public void setT8(List<MonitorInfo[]> v);
 134 
 135         public Set<List<String[]>> getT9();
 136         public void setT9(Set<List<String[]>> v);
 137 
 138         public Set<List<MonitorInfo[]>> getT10();
 139         public void setT10(Set<List<MonitorInfo[]>> v);
 140 
 141         public Map<String,Set<List<String[]>>> getT11();
 142         public void setT11(Map<String,Set<List<String[]>>> v);
 143 
 144         public Map<String,Set<List<MonitorInfo[]>>> getT12();
 145         public void setT12(Map<String,Set<List<MonitorInfo[]>>> v);
 146     }
 147 
 148     public static class Test implements TestMXBean {
 149 
 150         public String[] getT1() {
 151             return t1;
 152         }
 153         public void setT1(String[] v) {
 154             t1 = v;
 155         }
 156         private String[] t1;
 157 
 158         public MonitorInfo[] getT2() {
 159             return t2;
 160         }
 161         public void setT2(MonitorInfo[] v) {
 162             t2 = v;
 163         }
 164         private MonitorInfo[] t2;
 165 
 166         public Map<String,String[]> getT3() {
 167             return t3;
 168         }
 169         public void setT3(Map<String,String[]> v) {
 170             t3 = v;
 171         }
 172         private Map<String,String[]> t3;
 173 
 174         public Map<String,MonitorInfo[]> getT4() {
 175             return t4;
 176         }
 177         public void setT4(Map<String,MonitorInfo[]> v) {
 178             t4 = v;
 179         }
 180         private Map<String,MonitorInfo[]> t4;
 181 
 182         public Set<String[]> getT5() {
 183             return t5;
 184         }
 185         public void setT5(Set<String[]> v) {
 186             t5 = v;
 187         }
 188         private Set<String[]> t5;
 189 
 190         public Set<MonitorInfo[]> getT6() {
 191             return t6;
 192         }
 193         public void setT6(Set<MonitorInfo[]> v) {
 194             t6 = v;
 195         }
 196         private Set<MonitorInfo[]> t6;
 197 
 198         public List<String[]> getT7() {
 199             return t7;
 200         }
 201         public void setT7(List<String[]> v) {
 202             t7 = v;
 203         }
 204         private List<String[]> t7;
 205 
 206         public List<MonitorInfo[]> getT8() {
 207             return t8;
 208         }
 209         public void setT8(List<MonitorInfo[]> v) {
 210             t8 = v;
 211         }
 212         private List<MonitorInfo[]> t8;
 213 
 214         public Set<List<String[]>> getT9() {
 215             return t9;
 216         }
 217         public void setT9(Set<List<String[]>> v) {
 218             t9 = v;
 219         }
 220         private Set<List<String[]>> t9;
 221 
 222         public Set<List<MonitorInfo[]>> getT10() {
 223             return t10;
 224         }
 225         public void setT10(Set<List<MonitorInfo[]>> v) {
 226             t10 = v;
 227         }
 228         private Set<List<MonitorInfo[]>> t10;
 229 
 230         public Map<String,Set<List<String[]>>> getT11() {
 231             return t11;
 232         }
 233         public void setT11(Map<String,Set<List<String[]>>> v) {
 234             t11 = v;
 235         }
 236         private Map<String,Set<List<String[]>>> t11;
 237 
 238         public Map<String,Set<List<MonitorInfo[]>>> getT12() {
 239             return t12;
 240         }
 241         public void setT12(Map<String,Set<List<MonitorInfo[]>>> v) {
 242             t12 = v;
 243         }
 244         private Map<String,Set<List<MonitorInfo[]>>> t12;
 245     }
 246 
 247     public static void main(String[] args) throws Exception {
 248 
 249         int error = 0;
 250         JMXConnector cc = null;
 251         JMXConnectorServer cs = null;
 252 
 253         try {
 254             // Instantiate the MBean server
 255             //
 256             echo("\n>>> Create the MBean server");
 257             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 258 
 259             // Get default domain
 260             //
 261             echo("\n>>> Get the MBean server's default domain");
 262             String domain = mbs.getDefaultDomain();
 263             echo("\tDefault Domain = " + domain);
 264 
 265             // Register TestMXBean
 266             //
 267             echo("\n>>> Register TestMXBean");
 268             ObjectName name =
 269                 ObjectName.getInstance(domain + ":type=" + TestMXBean.class);
 270             mbs.createMBean(Test.class.getName(), name);
 271 
 272             // Create an RMI connector server
 273             //
 274             echo("\n>>> Create an RMI connector server");
 275             JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
 276             cs =
 277                 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
 278 
 279             // Start the RMI connector server
 280             //
 281             echo("\n>>> Start the RMI connector server");
 282             cs.start();
 283 
 284             // Create an RMI connector client
 285             //
 286             echo("\n>>> Create an RMI connector client");
 287             cc = JMXConnectorFactory.connect(cs.getAddress(), null);
 288             MBeanServerConnection mbsc = cc.getMBeanServerConnection();
 289 
 290             // Create TestMXBean proxy
 291             //
 292             echo("\n>>> Create the TestMXBean proxy");
 293             TestMXBean test = JMX.newMXBeanProxy(mbsc, name, TestMXBean.class);
 294 
 295             // Play with proxy getters and setters
 296             //
 297             echo("\n>>> Play with proxy getters and setters");
 298             String[] t1 = new String[] { "t1" };
 299             MonitorInfo[] t2 = new MonitorInfo[] {
 300                 new MonitorInfo("dummy", 0, 0,
 301                     new StackTraceElement("dummy", "dummy", "dummy", 0)) };
 302             Map<String,String[]> t3 = new HashMap<String,String[]>();
 303             t3.put("key", t1);
 304             Map<String,MonitorInfo[]> t4 = new HashMap<String,MonitorInfo[]>();
 305             t4.put("key", t2);
 306             Set<String[]> t5 = new HashSet<String[]>();
 307             t5.add(t1);
 308             Set<MonitorInfo[]> t6 = new HashSet<MonitorInfo[]>();
 309             t6.add(t2);
 310             List<String[]> t7 = new ArrayList<String[]>();
 311             t7.add(t1);
 312             List<MonitorInfo[]> t8 = new ArrayList<MonitorInfo[]>();
 313             t8.add(t2);
 314             Set<List<String[]>> t9 = new HashSet<List<String[]>>();
 315             t9.add(t7);
 316             Set<List<MonitorInfo[]>> t10 = new HashSet<List<MonitorInfo[]>>();
 317             t10.add(t8);
 318             Map<String,Set<List<String[]>>> t11 = new HashMap<String,Set<List<String[]>>>();
 319             t11.put("key", t9);
 320             Map<String,Set<List<MonitorInfo[]>>> t12 = new HashMap<String,Set<List<MonitorInfo[]>>>();
 321             t12.put("key", t10);
 322 
 323             test.setT1(t1);
 324             test.setT2(t2);
 325             test.setT3(t3);
 326             test.setT4(t4);
 327             test.setT5(t5);
 328             test.setT6(t6);
 329             test.setT7(t7);
 330             test.setT8(t8);
 331             test.setT9(t9);
 332             test.setT10(t10);
 333             test.setT11(t11);
 334             test.setT12(t12);
 335 
 336             String r;
 337             String e1 = "t1";
 338             String e2 = "dummy";
 339             echo("\tT1 = " + test.getT1());
 340             r = ((String[])test.getT1())[0];
 341             echo("\tR1 = " + r);
 342             if (!e1.equals(r)) error++;
 343             echo("\tT2 = " + test.getT2());
 344             r = ((MonitorInfo[])test.getT2())[0].getClassName();
 345             echo("\tR2 = " + r);
 346             if (!e2.equals(r)) error++;
 347             echo("\tT3 = " + test.getT3());
 348             r = ((String[])((Map<String,String[]>)test.getT3()).get("key"))[0];
 349             echo("\tR3 = " + r);
 350             if (!e1.equals(r)) error++;
 351             echo("\tT4 = " + test.getT4());
 352             r = ((MonitorInfo[])((Map<String,MonitorInfo[]>)test.getT4()).get("key"))[0].getClassName();
 353             echo("\tR4 = " + r);
 354             if (!e2.equals(r)) error++;
 355             echo("\tT5 = " + test.getT5());
 356             r = ((String[])((Set<String[]>)test.getT5()).iterator().next())[0];
 357             echo("\tR5 = " + r);
 358             if (!e1.equals(r)) error++;
 359             echo("\tT6 = " + test.getT6());
 360             r = ((MonitorInfo[])((Set<MonitorInfo[]>)test.getT6()).iterator().next())[0].getClassName();
 361             echo("\tR6 = " + r);
 362             if (!e2.equals(r)) error++;
 363             echo("\tT7 = " + test.getT7());
 364             r = ((String[])((List<String[]>)test.getT7()).get(0))[0];
 365             echo("\tR7 = " + r);
 366             if (!e1.equals(r)) error++;
 367             echo("\tT8 = " + test.getT8());
 368             r = ((MonitorInfo[])((List<MonitorInfo[]>)test.getT8()).get(0))[0].getClassName();
 369             echo("\tR8 = " + r);
 370             if (!e2.equals(r)) error++;
 371             echo("\tT9 = " + test.getT9());
 372             r = ((String[])((List<String[]>)((Set<List<String[]>>)test.getT9()).iterator().next()).get(0))[0];
 373             echo("\tR9 = " + r);
 374             if (!e1.equals(r)) error++;
 375             echo("\tT10 = " + test.getT10());
 376             r = ((MonitorInfo[])((List<MonitorInfo[]>)((Set<List<MonitorInfo[]>>)test.getT10()).iterator().next()).get(0))[0].getClassName();
 377             echo("\tR10 = " + r);
 378             if (!e2.equals(r)) error++;
 379             echo("\tT11 = " + test.getT11());
 380             r = ((String[])((List<String[]>)((Set<List<String[]>>)((Map<String,Set<List<String[]>>>)test.getT11()).get("key")).iterator().next()).get(0))[0];
 381             echo("\tR11 = " + r);
 382             if (!e1.equals(r)) error++;
 383             echo("\tT12 = " + test.getT12());
 384             r = ((MonitorInfo[])((List<MonitorInfo[]>)((Set<List<MonitorInfo[]>>)((Map<String,Set<List<MonitorInfo[]>>>)test.getT12()).get("key")).iterator().next()).get(0))[0].getClassName();
 385             echo("\tR12 = " + r);
 386             if (!e2.equals(r)) error++;
 387         } catch (Exception e) {
 388             e.printStackTrace(System.out);
 389             error++;
 390         } finally {
 391             // Close client
 392             //
 393             echo("\n>>> Close the RMI connector client");
 394             if (cc != null)
 395                 cc.close();
 396 
 397             // Stop server
 398             //
 399             echo("\n>>> Stop the RMI connector server");
 400             if (cs != null)
 401                 cs.stop();
 402 
 403             echo("\n>>> Bye! Bye!");
 404         }
 405 
 406         if (error > 0) {
 407             echo("\nTest failed! " + error + " errors.\n");
 408             throw new IllegalArgumentException("Test failed");
 409         } else {
 410             echo("\nTest passed!\n");
 411         }
 412     }
 413 
 414     private static void echo(String msg) {
 415         System.out.println(msg);
 416     }
 417 }