author | mchung |
Thu, 16 May 2013 15:08:24 -0700 | |
changeset 17497 | e65d73b7ae54 |
parent 5506 | 202f599c92aa |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
25 |
* @bug 4227192 4487672 |
2 | 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 { |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
45 |
Class<?>[] interfaces = |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
46 |
new Class<?>[] { Runnable.class, Observer.class }; |
2 | 47 |
|
48 |
ClassLoader loader = ClassLoader.getSystemClassLoader(); |
|
49 |
||
50 |
/* |
|
51 |
* Generate a proxy class. |
|
52 |
*/ |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
53 |
Class<?> proxyClass = Proxy.getProxyClass(loader, interfaces); |
2 | 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 |
*/ |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
75 |
for (Class<?> intf : interfaces) { |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
76 |
if (!intf.isAssignableFrom(proxyClass)) { |
2 | 77 |
throw new RuntimeException( |
78 |
"proxy class not assignable to proxy interface " + |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
79 |
intf.getName()); |
2 | 80 |
} |
81 |
} |
|
82 |
||
83 |
/* |
|
84 |
* Verify that it has the given permutation of interfaces. |
|
85 |
*/ |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
86 |
List<Class<?>> l1 = Arrays.asList(interfaces); |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
87 |
List<Class<?>> l2 = Arrays.asList(proxyClass.getInterfaces()); |
2 | 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 |
*/ |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
121 |
Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class); |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
122 |
|
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
123 |
/* |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
124 |
* Test constructor with null InvocationHandler |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
125 |
*/ |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
126 |
try { |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
127 |
cons.newInstance(new Object[] { null }); |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
128 |
throw new RuntimeException("Expected NullPointerException thrown"); |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
129 |
} catch (InvocationTargetException e) { |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
130 |
Throwable t = e.getTargetException(); |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
131 |
if (!(t instanceof NullPointerException)) { |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
132 |
throw t; |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
133 |
} |
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
134 |
} |
2 | 135 |
|
136 |
/* |
|
137 |
* Construct a proxy instance. |
|
138 |
*/ |
|
139 |
Handler handler = new Handler(); |
|
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
140 |
Object proxy = cons.newInstance(handler); |
2 | 141 |
handler.currentProxy = proxy; |
142 |
||
143 |
/* |
|
144 |
* Invoke a method on a proxy instance. |
|
145 |
*/ |
|
146 |
Method m = Runnable.class.getMethod("run", null); |
|
147 |
((Runnable) proxy).run(); |
|
148 |
if (!handler.lastMethod.equals(m)) { |
|
149 |
throw new RuntimeException( |
|
150 |
"proxy method invocation failure (lastMethod = " + |
|
151 |
handler.lastMethod + ")"); |
|
152 |
} |
|
153 |
||
154 |
System.err.println("\nTEST PASSED"); |
|
155 |
||
17497
e65d73b7ae54
4487672: (proxy) Proxy constructor should check for null argument
mchung
parents:
5506
diff
changeset
|
156 |
} catch (Throwable e) { |
2 | 157 |
System.err.println("\nTEST FAILED:"); |
158 |
e.printStackTrace(); |
|
159 |
throw new RuntimeException("TEST FAILED: " + e.toString()); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
public static class Handler implements InvocationHandler { |
|
164 |
||
165 |
Object currentProxy; |
|
166 |
Method lastMethod; |
|
167 |
||
168 |
public Object invoke(Object proxy, Method method, Object[] args) |
|
169 |
throws Throwable |
|
170 |
{ |
|
171 |
if (proxy != currentProxy) { |
|
172 |
throw new RuntimeException( |
|
173 |
"wrong proxy instance passed to invoke method"); |
|
174 |
} |
|
175 |
lastMethod = method; |
|
176 |
return null; |
|
177 |
} |
|
178 |
} |
|
179 |
} |