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