1 /*
   2  * Copyright (c) 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 import javax.swing.SwingUtilities;
  24 import javax.swing.UIDefaults;
  25 /*
  26  * @test
  27  * @bug 8080972
  28  * @summary Audit Core Reflection in module java.desktop for places that will
  29  *          require changes to work with modules
  30  * @author Alexander Scherbatiy
  31  */
  32 
  33 public class TestProxyLazyValue {
  34 
  35     public static void main(String[] args) throws Exception {
  36         SwingUtilities.invokeAndWait(TestProxyLazyValue::testUserProxyLazyValue);
  37         SwingUtilities.invokeAndWait(TestProxyLazyValue::testProxyLazyValue);
  38         System.setSecurityManager(new SecurityManager());
  39         SwingUtilities.invokeAndWait(TestProxyLazyValue::testUserProxyLazyValue);
  40         SwingUtilities.invokeAndWait(TestProxyLazyValue::testProxyLazyValue);
  41     }
  42 
  43     private static void testUserProxyLazyValue() {
  44 
  45         Object obj = new UserProxyLazyValue(
  46                 UserLazyClass.class.getName()).createValue(null);
  47 
  48         if (!(obj instanceof UserLazyClass)) {
  49             throw new RuntimeException("Object is not UserLazyClass!");
  50         }
  51 
  52         obj = new UserProxyLazyValue(UserLazyClass.class.getName(),
  53                 new Object[]{UserLazyClass.CONSTRUCTOR_ARG}).createValue(null);
  54 
  55         if (!(obj instanceof UserLazyClass)) {
  56             throw new RuntimeException("Object is not UserLazyClass!");
  57         }
  58 
  59         if (((UserLazyClass) obj).arg != UserLazyClass.CONSTRUCTOR_ARG) {
  60             throw new RuntimeException("Constructt argument is wrong!");
  61         }
  62 
  63         obj = new UserProxyLazyValue(UserLazyClass.class.getName(),
  64                 "method1").createValue(null);
  65 
  66         if (!UserLazyClass.RESULT_1.equals(obj)) {
  67             throw new RuntimeException("Result is wrong!");
  68         }
  69 
  70         obj = new UserProxyLazyValue(UserLazyClass.class.getName(),
  71                 "method2", new Object[]{UserLazyClass.RESULT_2}).createValue(null);
  72 
  73         if (!UserLazyClass.RESULT_2.equals(obj)) {
  74             throw new RuntimeException("Result is wrong!");
  75         }
  76     }
  77 
  78     private static void testProxyLazyValue() {
  79 
  80         Object obj = new UIDefaults.ProxyLazyValue(
  81                 UserLazyClass.class.getName()).createValue(null);
  82 
  83         if (!(obj instanceof UserLazyClass)) {
  84             throw new RuntimeException("Object is not UserLazyClass!");
  85         }
  86 
  87         obj = new UIDefaults.ProxyLazyValue(UserLazyClass.class.getName(),
  88                 new Object[]{UserLazyClass.CONSTRUCTOR_ARG}).createValue(null);
  89 
  90         if (!(obj instanceof UserLazyClass)) {
  91             throw new RuntimeException("Object is not UserLazyClass!");
  92         }
  93 
  94         if (((UserLazyClass) obj).arg != UserLazyClass.CONSTRUCTOR_ARG) {
  95             throw new RuntimeException("Constructt argument is wrong!");
  96         }
  97 
  98         obj = new UIDefaults.ProxyLazyValue(UserLazyClass.class.getName(),
  99                 "method1").createValue(null);
 100 
 101         if (!UserLazyClass.RESULT_1.equals(obj)) {
 102             throw new RuntimeException("Result is wrong!");
 103         }
 104 
 105         obj = new UIDefaults.ProxyLazyValue(UserLazyClass.class.getName(),
 106                 "method2", new Object[]{UserLazyClass.RESULT_2}).createValue(null);
 107 
 108         if (!UserLazyClass.RESULT_2.equals(obj)) {
 109             throw new RuntimeException("Result is wrong!");
 110         }
 111     }
 112 
 113     public static class UserLazyClass {
 114 
 115         static final int CONSTRUCTOR_ARG = 100;
 116         static final String RESULT_1 = "1";
 117         static final String RESULT_2 = "2";
 118 
 119         int arg;
 120 
 121         public UserLazyClass() {
 122         }
 123 
 124         public UserLazyClass(int arg) {
 125             this.arg = arg;
 126         }
 127 
 128         public static String method1() {
 129             return RESULT_1;
 130         }
 131 
 132         public static String method2(String arg) {
 133             return arg;
 134         }
 135     }
 136 
 137     public static class UserProxyLazyValue extends UIDefaults.ProxyLazyValue {
 138 
 139         public UserProxyLazyValue(String className) {
 140             super(className);
 141         }
 142 
 143         public UserProxyLazyValue(String className, Object[] constructorArgs) {
 144             super(className, constructorArgs);
 145         }
 146 
 147         public UserProxyLazyValue(String className, String methodName) {
 148             super(className, methodName);
 149         }
 150 
 151         public UserProxyLazyValue(String className, String methodName,
 152                 Object[] methodArgs) {
 153             super(className, methodName, methodArgs);
 154         }
 155     }
 156 }