2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package tests.api.java.lang.reflect;
20 import dalvik.annotation.KnownFailure;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
25 import java.lang.reflect.InvocationHandler;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Proxy;
28 import java.lang.reflect.UndeclaredThrowableException;
30 import tests.support.Support_Proxy_I1;
31 import tests.support.Support_Proxy_I2;
32 import tests.support.Support_Proxy_ParentException;
33 import tests.support.Support_Proxy_SubException;
39 level = TestLevel.NOT_NECESSARY,
40 notes = "Interface without implementation. Whether method is " +
41 "called from proxy is tested by ProxyTest.",
42 clazz = InvocationHandler.class,
44 args = { Object.class, Method.class, Object[].class }
48 public class ProxyTest extends junit.framework.TestCase {
51 * When multiple interfaces define the same method, the list of thrown
52 * exceptions are those which can be mapped to another exception in the
55 * String foo(String s) throws SubException, LinkageError;
57 * UndeclaredThrowableException wrappers any checked exception which is not
58 * in the merged list. So ParentException would be wrapped, BUT LinkageError
59 * would not be since its not an Error/RuntimeException.
61 * interface I1 { String foo(String s) throws ParentException, LinkageError; }
62 * interface I2 { String foo(String s) throws SubException, Error; }
66 public float method(float _number0, float _number1);
69 class Broken1Invoke implements InvocationHandler {
70 public Object invoke(Object proxy, Method method, Object[] args)
76 class ProxyCoonstructorTest extends Proxy {
77 protected ProxyCoonstructorTest(InvocationHandler h) {
83 * @tests java.lang.reflect.Proxy#getProxyClass(java.lang.ClassLoader,
87 level = TestLevel.COMPLETE,
89 method = "getProxyClass",
90 args = {java.lang.ClassLoader.class, java.lang.Class[].class}
92 public void test_getProxyClassLjava_lang_ClassLoader$Ljava_lang_Class() {
93 Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
94 .getClassLoader(), new Class[] { Support_Proxy_I1.class });
96 assertTrue("Did not create a Proxy subclass ",
97 proxy.getSuperclass() == Proxy.class);
98 assertTrue("Does not believe its a Proxy class ", Proxy
99 .isProxyClass(proxy));
101 assertTrue("Does not believe it's a Proxy class ", Proxy
102 .isProxyClass(Proxy.getProxyClass(null,
103 new Class[] { Comparable.class })));
105 boolean aborted = false;
107 Proxy.getProxyClass(null, new Class[] { Support_Proxy_I1.class,
108 Support_Proxy_I2.class });
109 } catch (IllegalArgumentException e) {
112 assertTrue("Default classLoader should not see app class ", aborted);
116 Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(),
118 fail("NPE expected");
119 } catch (NullPointerException e) {
122 assertTrue("NPE not thrown", aborted);
126 Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(),
127 new Class<?>[] {Support_Proxy_I1.class, null});
128 fail("NPE expected");
129 } catch (NullPointerException e) {
132 assertTrue("NPE not thrown", aborted);
136 * @tests java.lang.reflect.Proxy#Proxy(java.lang.reflect.InvocationHandler)
139 level = TestLevel.COMPLETE,
142 args = {java.lang.reflect.InvocationHandler.class}
144 public void test_ProxyLjava_lang_reflect_InvocationHandler() {
145 assertNotNull(new ProxyCoonstructorTest(new InvocationHandler() {
146 public Object invoke(Object proxy, Method method, Object[] args)
156 * @tests java.lang.reflect.Proxy#newProxyInstance(java.lang.ClassLoader,
157 * java.lang.Class[], java.lang.reflect.InvocationHandler)
160 level = TestLevel.COMPLETE,
162 method = "newProxyInstance",
163 args = {java.lang.ClassLoader.class, java.lang.Class[].class, java.lang.reflect.InvocationHandler.class}
165 public void test_newProxyInstanceLjava_lang_ClassLoader$Ljava_lang_ClassLjava_lang_reflect_InvocationHandler() {
166 Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
167 .getClassLoader(), new Class[] { Support_Proxy_I1.class,
168 Support_Proxy_I2.class }, new InvocationHandler() {
169 public Object invoke(Object proxy, Method method, Object[] args)
171 if (method.getName().equals("equals"))
172 return new Boolean(proxy == args[0]);
173 if (method.getName().equals("array"))
174 return new int[] { (int) ((long[]) args[0])[1], -1 };
175 if (method.getName().equals("string")) {
176 if ("".equals(args[0]))
177 throw new Support_Proxy_SubException();
178 if ("clone".equals(args[0]))
179 throw new Support_Proxy_ParentException();
180 if ("error".equals(args[0]))
181 throw new ArrayStoreException();
182 if ("any".equals(args[0]))
183 throw new IllegalAccessException();
189 Support_Proxy_I1 proxy = (Support_Proxy_I1) p;
190 assertTrue("Failed identity test ", proxy.equals(proxy));
191 assertTrue("Failed not equals test ", !proxy.equals(""));
192 int[] result = (int[]) proxy.array(new long[] { 100L, -200L });
193 assertEquals("Failed primitive type conversion test ", -200, result[0]);
195 boolean worked = false;
198 } catch (Support_Proxy_SubException e) {
200 } catch (Support_Proxy_ParentException e) { // is never thrown
202 assertTrue("Problem converting exception ", worked);
206 proxy.string("clone");
207 } catch (Support_Proxy_ParentException e) { // is never thrown
208 } catch (UndeclaredThrowableException e) {
211 assertTrue("Problem converting exception ", worked);
215 proxy.string("error");
216 } catch (Support_Proxy_ParentException e) { // is never thrown
217 } catch (UndeclaredThrowableException e) {
218 } catch (RuntimeException e) {
219 worked = e.getClass() == ArrayStoreException.class;
221 assertTrue("Problem converting exception ", worked);
226 } catch (Support_Proxy_ParentException e) { // is never thrown
227 } catch (UndeclaredThrowableException e) {
230 assertTrue("Problem converting exception ", worked);
232 Broken1 proxyObject = null;
234 proxyObject = (Broken1) Proxy.newProxyInstance(Broken1.class
235 .getClassLoader(), new Class[] { Broken1.class },
236 new Broken1Invoke());
237 } catch (Throwable e) {
238 fail("Failed to create proxy for class: " + Broken1.class + " - "
241 float brokenResult = proxyObject.method(2.1f, 5.8f);
242 assertTrue("Invalid invoke result", brokenResult == 5.8f);
246 * @tests java.lang.reflect.Proxy#isProxyClass(java.lang.Class)
249 level = TestLevel.COMPLETE,
251 method = "isProxyClass",
252 args = {java.lang.Class.class}
254 public void test_isProxyClassLjava_lang_Class() {
255 Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
256 .getClassLoader(), new Class[] { Support_Proxy_I1.class });
258 class Fake extends Proxy {
264 Proxy fake = new Proxy(new InvocationHandler() {
265 public Object invoke(Object proxy, Method method, Object[] args)
272 assertTrue("Does not believe its a Proxy class ", Proxy
273 .isProxyClass(proxy));
274 assertTrue("Proxy subclasses do not count ", !Proxy
275 .isProxyClass(Fake.class));
276 assertTrue("Is not a runtime generated Proxy class ", !Proxy
277 .isProxyClass(fake.getClass()));
278 boolean thrown = false;
280 Proxy.isProxyClass(null);
281 } catch (NullPointerException ex){
284 assertTrue("NPE not thrown.", thrown);
288 * @tests java.lang.reflect.Proxy#getInvocationHandler(java.lang.Object)
291 level = TestLevel.COMPLETE,
293 method = "getInvocationHandler",
294 args = {java.lang.Object.class}
296 public void test_getInvocationHandlerLjava_lang_Object() {
297 InvocationHandler handler = new InvocationHandler() {
298 public Object invoke(Object proxy, Method method, Object[] args)
304 Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
305 .getClassLoader(), new Class[] { Support_Proxy_I1.class },
308 assertTrue("Did not return invocation handler ", Proxy
309 .getInvocationHandler(p) == handler);
310 boolean aborted = false;
312 Proxy.getInvocationHandler("");
313 } catch (IllegalArgumentException e) {
316 assertTrue("Did not detect non proxy object ", aborted);
319 //Regression Test for HARMONY-2355
321 level = TestLevel.PARTIAL,
322 notes = "Regression test. Exceptions are not verified.",
323 method = "newProxyInstance",
324 args = {java.lang.ClassLoader.class, java.lang.Class[].class, java.lang.reflect.InvocationHandler.class}
326 public void test_newProxyInstance_withCompatibleReturnTypes() {
328 .newProxyInstance(this.getClass().getClassLoader(),
329 new Class[] { ITestReturnObject.class,
330 ITestReturnString.class },
331 new TestProxyHandler(new TestProxyImpl()));
336 level = TestLevel.PARTIAL,
337 notes = "IllegalArgumentException is verified.",
338 method = "newProxyInstance",
339 args = {java.lang.ClassLoader.class, java.lang.Class[].class, java.lang.reflect.InvocationHandler.class}
341 public void test_newProxyInstance_withNonCompatibleReturnTypes() {
343 Proxy.newProxyInstance(this.getClass().getClassLoader(),
344 new Class[] { ITestReturnInteger.class,
345 ITestReturnString.class }, new TestProxyHandler(
346 new TestProxyImpl()));
347 fail("should throw IllegalArgumentException");
348 } catch (IllegalArgumentException e) {
354 public static interface ITestReturnObject {
358 public static interface ITestReturnString {
362 public static interface ITestReturnInteger {
366 public static class TestProxyImpl implements ITestReturnObject,
374 public static class TestProxyHandler implements InvocationHandler {
375 private Object proxied;
377 public TestProxyHandler(Object object) {
381 public Object invoke(Object object, Method method, Object[] args)
384 return method.invoke(proxied, args);
389 protected void setUp() {
392 protected void tearDown() {