OSDN Git Service

AI 147127: am: CL 147126 am: CL 147121 Fixes for tests in the luni module.
[android-x86/dalvik.git] / libcore / luni / src / test / java / tests / api / java / lang / reflect / ProxyTest.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package tests.api.java.lang.reflect;
19
20 import dalvik.annotation.KnownFailure;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
24
25 import java.lang.reflect.InvocationHandler;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Proxy;
28 import java.lang.reflect.UndeclaredThrowableException;
29
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;
34
35 @TestTargetClass(
36         value = Proxy.class,
37         untestedMethods= {
38             @TestTargetNew(
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,
43                 method = "invoke",
44                 args = { Object.class, Method.class, Object[].class }
45             )
46         }
47 )
48 public class ProxyTest extends junit.framework.TestCase {
49
50     /*
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
53      * other method:
54      * 
55      * String foo(String s) throws SubException, LinkageError;
56      * 
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.
60      * 
61      * interface I1 { String foo(String s) throws ParentException, LinkageError; }
62      * interface I2 { String foo(String s) throws SubException, Error; }
63      */
64
65     interface Broken1 {
66         public float method(float _number0, float _number1);
67     }
68
69     class Broken1Invoke implements InvocationHandler {
70         public Object invoke(Object proxy, Method method, Object[] args)
71                 throws Throwable {
72             return args[1];
73         }
74     }
75     
76     class ProxyCoonstructorTest extends Proxy {
77         protected ProxyCoonstructorTest(InvocationHandler h) {
78             super(h);
79         }
80     }
81
82     /**
83      * @tests java.lang.reflect.Proxy#getProxyClass(java.lang.ClassLoader,
84      *        java.lang.Class[])
85      */
86     @TestTargetNew(
87         level = TestLevel.COMPLETE,
88         notes = "",
89         method = "getProxyClass",
90         args = {java.lang.ClassLoader.class, java.lang.Class[].class}
91     )
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 });
95
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));
100
101         assertTrue("Does not believe it's a Proxy class ", Proxy
102                 .isProxyClass(Proxy.getProxyClass(null,
103                         new Class[] { Comparable.class })));
104
105         boolean aborted = false;
106         try {
107             Proxy.getProxyClass(null, new Class[] { Support_Proxy_I1.class,
108                     Support_Proxy_I2.class });
109         } catch (IllegalArgumentException e) {
110             aborted = true;
111         }
112         assertTrue("Default classLoader should not see app class ", aborted);
113         
114         aborted = false;
115         try {
116             Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(),
117                     (Class<?>[]) null);
118             fail("NPE expected");
119         } catch (NullPointerException e) {
120             aborted = true;
121         }
122         assertTrue("NPE not thrown", aborted);
123         
124         aborted = false;
125         try {
126             Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(),
127                     new Class<?>[] {Support_Proxy_I1.class, null});
128             fail("NPE expected");
129         } catch (NullPointerException e) {
130             aborted = true;
131         }
132         assertTrue("NPE not thrown", aborted);
133     }
134     
135     /**
136      * @tests java.lang.reflect.Proxy#Proxy(java.lang.reflect.InvocationHandler)
137      */
138     @TestTargetNew(
139         level = TestLevel.COMPLETE,
140         notes = "",
141         method = "Proxy",
142         args = {java.lang.reflect.InvocationHandler.class}
143     )
144     public void test_ProxyLjava_lang_reflect_InvocationHandler() {
145         assertNotNull(new ProxyCoonstructorTest(new InvocationHandler() {
146             public Object invoke(Object proxy, Method method, Object[] args)
147                     throws Throwable {
148                 return null;
149             }
150         }));
151     }
152     
153     
154
155     /**
156      * @tests java.lang.reflect.Proxy#newProxyInstance(java.lang.ClassLoader,
157      *        java.lang.Class[], java.lang.reflect.InvocationHandler)
158      */
159     @TestTargetNew(
160         level = TestLevel.COMPLETE,
161         notes = "",
162         method = "newProxyInstance",
163         args = {java.lang.ClassLoader.class, java.lang.Class[].class, java.lang.reflect.InvocationHandler.class}
164     )
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)
170                     throws Throwable {
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();
184                 }
185                 return null;
186             }
187         });
188
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]);
194
195         boolean worked = false;
196         try {
197             proxy.string("");
198         } catch (Support_Proxy_SubException e) {
199             worked = true;
200         } catch (Support_Proxy_ParentException e) { // is never thrown
201         }
202         assertTrue("Problem converting exception ", worked);
203
204         worked = false;
205         try {
206             proxy.string("clone");
207         } catch (Support_Proxy_ParentException e) { // is never thrown
208         } catch (UndeclaredThrowableException e) {
209             worked = true;
210         }
211         assertTrue("Problem converting exception ", worked);
212
213         worked = false;
214         try {
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;
220         }
221         assertTrue("Problem converting exception ", worked);
222
223         worked = false;
224         try {
225             proxy.string("any");
226         } catch (Support_Proxy_ParentException e) { // is never thrown
227         } catch (UndeclaredThrowableException e) {
228             worked = true;
229         }
230         assertTrue("Problem converting exception ", worked);
231
232         Broken1 proxyObject = null;
233         try {
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 + " - "
239                     + e);
240         }
241         float brokenResult = proxyObject.method(2.1f, 5.8f);
242         assertTrue("Invalid invoke result", brokenResult == 5.8f);
243     }
244
245     /**
246      * @tests java.lang.reflect.Proxy#isProxyClass(java.lang.Class)
247      */
248     @TestTargetNew(
249         level = TestLevel.COMPLETE,
250         notes = "",
251         method = "isProxyClass",
252         args = {java.lang.Class.class}
253     )
254     public void test_isProxyClassLjava_lang_Class() {
255         Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
256                 .getClassLoader(), new Class[] { Support_Proxy_I1.class });
257
258         class Fake extends Proxy {
259             Fake() {
260                 super(null);
261             }
262         }
263
264         Proxy fake = new Proxy(new InvocationHandler() {
265             public Object invoke(Object proxy, Method method, Object[] args)
266                     throws Throwable {
267                 return null;
268             }
269         }) {
270         };
271
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;
279         try{
280         Proxy.isProxyClass(null);
281         } catch (NullPointerException ex){
282             thrown = true;
283         }
284         assertTrue("NPE not thrown.", thrown);
285     }
286
287     /**
288      * @tests java.lang.reflect.Proxy#getInvocationHandler(java.lang.Object)
289      */
290     @TestTargetNew(
291         level = TestLevel.COMPLETE,
292         notes = "",
293         method = "getInvocationHandler",
294         args = {java.lang.Object.class}
295     )
296     public void test_getInvocationHandlerLjava_lang_Object() {
297         InvocationHandler handler = new InvocationHandler() {
298             public Object invoke(Object proxy, Method method, Object[] args)
299                     throws Throwable {
300                 return null;
301             }
302         };
303
304         Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
305                 .getClassLoader(), new Class[] { Support_Proxy_I1.class },
306                 handler);
307
308         assertTrue("Did not return invocation handler ", Proxy
309                 .getInvocationHandler(p) == handler);
310         boolean aborted = false;
311         try {
312             Proxy.getInvocationHandler("");
313         } catch (IllegalArgumentException e) {
314             aborted = true;
315         }
316         assertTrue("Did not detect non proxy object ", aborted);
317     }
318         
319     //Regression Test for HARMONY-2355
320     @TestTargetNew(
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}
325     )
326     public void test_newProxyInstance_withCompatibleReturnTypes() {
327         Object o = Proxy
328                 .newProxyInstance(this.getClass().getClassLoader(),
329                         new Class[] { ITestReturnObject.class,
330                                 ITestReturnString.class },
331                         new TestProxyHandler(new TestProxyImpl()));
332         assertNotNull(o);
333     }
334     
335     @TestTargetNew(
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}
340     )
341     public void test_newProxyInstance_withNonCompatibleReturnTypes() {
342         try {
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) {
349             // expected
350         }
351
352     }
353
354     public static interface ITestReturnObject {
355         Object f();
356     }
357
358     public static interface ITestReturnString {
359         String f();
360     }
361
362     public static interface ITestReturnInteger {
363         Integer f();
364     }
365
366     public static class TestProxyImpl implements ITestReturnObject,
367             ITestReturnString {
368         public String f() {
369             // do nothing
370             return null;
371         }
372     }
373
374     public static class TestProxyHandler implements InvocationHandler {
375         private Object proxied;
376
377         public TestProxyHandler(Object object) {
378             proxied = object;
379         }
380
381         public Object invoke(Object object, Method method, Object[] args)
382                 throws Throwable {
383             // do nothing
384             return method.invoke(proxied, args);
385         }
386
387     }
388
389     protected void setUp() {
390     }
391
392     protected void tearDown() {
393     }
394 }