|
1 /* |
|
2 * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* @test |
|
25 * @bug 4227192 |
|
26 * @summary This is a basic functional test of the dynamic proxy API (part 1). |
|
27 * @author Peter Jones |
|
28 * |
|
29 * @build Basic1 |
|
30 * @run main Basic1 |
|
31 */ |
|
32 |
|
33 import java.lang.reflect.*; |
|
34 import java.security.*; |
|
35 import java.util.*; |
|
36 |
|
37 public class Basic1 { |
|
38 |
|
39 public static void main(String[] args) { |
|
40 |
|
41 System.err.println( |
|
42 "\nBasic functional test of dynamic proxy API, part 1\n"); |
|
43 |
|
44 try { |
|
45 Class[] interfaces = |
|
46 new Class[] { Runnable.class, Observer.class }; |
|
47 |
|
48 ClassLoader loader = ClassLoader.getSystemClassLoader(); |
|
49 |
|
50 /* |
|
51 * Generate a proxy class. |
|
52 */ |
|
53 Class proxyClass = Proxy.getProxyClass(loader, interfaces); |
|
54 System.err.println("+ generated proxy class: " + proxyClass); |
|
55 |
|
56 /* |
|
57 * Verify that it is public, final, and not abstract. |
|
58 */ |
|
59 int flags = proxyClass.getModifiers(); |
|
60 System.err.println( |
|
61 "+ proxy class's modifiers: " + Modifier.toString(flags)); |
|
62 if (!Modifier.isPublic(flags)) { |
|
63 throw new RuntimeException("proxy class in not public"); |
|
64 } |
|
65 if (!Modifier.isFinal(flags)) { |
|
66 throw new RuntimeException("proxy class in not final"); |
|
67 } |
|
68 if (Modifier.isAbstract(flags)) { |
|
69 throw new RuntimeException("proxy class in abstract"); |
|
70 } |
|
71 |
|
72 /* |
|
73 * Verify that it is assignable to the proxy interfaces. |
|
74 */ |
|
75 for (int i = 0; i < interfaces.length; i++) { |
|
76 if (!interfaces[i].isAssignableFrom(proxyClass)) { |
|
77 throw new RuntimeException( |
|
78 "proxy class not assignable to proxy interface " + |
|
79 interfaces[i].getName()); |
|
80 } |
|
81 } |
|
82 |
|
83 /* |
|
84 * Verify that it has the given permutation of interfaces. |
|
85 */ |
|
86 List l1 = Arrays.asList(interfaces); |
|
87 List l2 = Arrays.asList(proxyClass.getInterfaces()); |
|
88 System.err.println("+ proxy class's interfaces: " + l2); |
|
89 if (!l1.equals(l2)) { |
|
90 throw new RuntimeException( |
|
91 "proxy class interfaces are " + l2 + |
|
92 " (expected " + l1 + ")"); |
|
93 } |
|
94 |
|
95 /* |
|
96 * Verify that system agress that it is a proxy class. |
|
97 */ |
|
98 if (Proxy.isProxyClass(Object.class)) { |
|
99 throw new RuntimeException( |
|
100 "Proxy.isProxyClass returned true for java.lang.Object"); |
|
101 } |
|
102 if (!Proxy.isProxyClass(proxyClass)) { |
|
103 throw new RuntimeException( |
|
104 "Proxy.isProxyClass returned false for proxy class"); |
|
105 } |
|
106 |
|
107 /* |
|
108 * Verify that its protection domain is the bootstrap domain. |
|
109 */ |
|
110 ProtectionDomain pd = proxyClass.getProtectionDomain(); |
|
111 System.err.println("+ proxy class's protextion domain: " + pd); |
|
112 if (!pd.implies(new AllPermission())) { |
|
113 throw new RuntimeException( |
|
114 "proxy class does not have AllPermission"); |
|
115 } |
|
116 |
|
117 /* |
|
118 * Verify that it has a constructor that takes an |
|
119 * InvocationHandler instance. |
|
120 */ |
|
121 Constructor cons = proxyClass.getConstructor( |
|
122 new Class[] { InvocationHandler.class }); |
|
123 |
|
124 /* |
|
125 * Construct a proxy instance. |
|
126 */ |
|
127 Handler handler = new Handler(); |
|
128 Object proxy = cons.newInstance(new Object[] { handler }); |
|
129 handler.currentProxy = proxy; |
|
130 |
|
131 /* |
|
132 * Invoke a method on a proxy instance. |
|
133 */ |
|
134 Method m = Runnable.class.getMethod("run", null); |
|
135 ((Runnable) proxy).run(); |
|
136 if (!handler.lastMethod.equals(m)) { |
|
137 throw new RuntimeException( |
|
138 "proxy method invocation failure (lastMethod = " + |
|
139 handler.lastMethod + ")"); |
|
140 } |
|
141 |
|
142 System.err.println("\nTEST PASSED"); |
|
143 |
|
144 } catch (Exception e) { |
|
145 System.err.println("\nTEST FAILED:"); |
|
146 e.printStackTrace(); |
|
147 throw new RuntimeException("TEST FAILED: " + e.toString()); |
|
148 } |
|
149 } |
|
150 |
|
151 public static class Handler implements InvocationHandler { |
|
152 |
|
153 Object currentProxy; |
|
154 Method lastMethod; |
|
155 |
|
156 public Object invoke(Object proxy, Method method, Object[] args) |
|
157 throws Throwable |
|
158 { |
|
159 if (proxy != currentProxy) { |
|
160 throw new RuntimeException( |
|
161 "wrong proxy instance passed to invoke method"); |
|
162 } |
|
163 lastMethod = method; |
|
164 return null; |
|
165 } |
|
166 } |
|
167 } |