FileDocCategorySizeDatePackage
ProxyTest.javaAPI DocAndroid 1.5 API14065Wed May 06 22:41:04 BST 2009tests.api.java.lang.reflect

ProxyTest

public class ProxyTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
protected voidsetUp()

    
protected voidtearDown()

    
public voidtest_ProxyLjava_lang_reflect_InvocationHandler()

tests
java.lang.reflect.Proxy#Proxy(java.lang.reflect.InvocationHandler)

        assertNotNull(new ProxyCoonstructorTest(new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                return null;
            }
        }));
    
public voidtest_getInvocationHandlerLjava_lang_Object()

tests
java.lang.reflect.Proxy#getInvocationHandler(java.lang.Object)

        InvocationHandler handler = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                return null;
            }
        };

        Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
                .getClassLoader(), new Class[] { Support_Proxy_I1.class },
                handler);

        assertTrue("Did not return invocation handler ", Proxy
                .getInvocationHandler(p) == handler);
        boolean aborted = false;
        try {
            Proxy.getInvocationHandler("");
        } catch (IllegalArgumentException e) {
            aborted = true;
        }
        assertTrue("Did not detect non proxy object ", aborted);
    
public voidtest_getProxyClassLjava_lang_ClassLoader$Ljava_lang_Class()

tests
java.lang.reflect.Proxy#getProxyClass(java.lang.ClassLoader, java.lang.Class[])

        Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
                .getClassLoader(), new Class[] { Support_Proxy_I1.class });

        assertTrue("Did not create a Proxy subclass ",
                proxy.getSuperclass() == Proxy.class);
        assertTrue("Does not believe its a Proxy class ", Proxy
                .isProxyClass(proxy));

        assertTrue("Does not believe it's a Proxy class ", Proxy
                .isProxyClass(Proxy.getProxyClass(null,
                        new Class[] { Comparable.class })));

        boolean aborted = false;
        try {
            Proxy.getProxyClass(null, new Class[] { Support_Proxy_I1.class,
                    Support_Proxy_I2.class });
        } catch (IllegalArgumentException e) {
            aborted = true;
        }
        assertTrue("Default classLoader should not see app class ", aborted);
        
        aborted = false;
        try {
            Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(),
                    (Class<?>[]) null);
            fail("NPE expected");
        } catch (NullPointerException e) {
            aborted = true;
        }
        assertTrue("NPE not thrown", aborted);
        
        aborted = false;
        try {
            Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(),
                    new Class<?>[] {Support_Proxy_I1.class, null});
            fail("NPE expected");
        } catch (NullPointerException e) {
            aborted = true;
        }
        assertTrue("NPE not thrown", aborted);
    
public voidtest_isProxyClassLjava_lang_Class()

tests
java.lang.reflect.Proxy#isProxyClass(java.lang.Class)

        Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
                .getClassLoader(), new Class[] { Support_Proxy_I1.class });

        class Fake extends Proxy {
            Fake() {
                super(null);
            }
        }

        Proxy fake = new Proxy(new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                return null;
            }
        }) {
        };

        assertTrue("Does not believe its a Proxy class ", Proxy
                .isProxyClass(proxy));
        assertTrue("Proxy subclasses do not count ", !Proxy
                .isProxyClass(Fake.class));
        assertTrue("Is not a runtime generated Proxy class ", !Proxy
                .isProxyClass(fake.getClass()));
        boolean thrown = false;
        try{
        Proxy.isProxyClass(null);
        } catch (NullPointerException ex){
            thrown = true;
        }
        assertTrue("NPE not thrown.", thrown);
    
public voidtest_newProxyInstanceLjava_lang_ClassLoader$Ljava_lang_ClassLjava_lang_reflect_InvocationHandler()

tests
java.lang.reflect.Proxy#newProxyInstance(java.lang.ClassLoader, java.lang.Class[], java.lang.reflect.InvocationHandler)

        Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
                .getClassLoader(), new Class[] { Support_Proxy_I1.class,
                Support_Proxy_I2.class }, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                if (method.getName().equals("equals"))
                    return new Boolean(proxy == args[0]);
                if (method.getName().equals("array"))
                    return new int[] { (int) ((long[]) args[0])[1], -1 };
                if (method.getName().equals("string")) {
                    if ("".equals(args[0]))
                        throw new Support_Proxy_SubException();
                    if ("clone".equals(args[0]))
                        throw new Support_Proxy_ParentException();
                    if ("error".equals(args[0]))
                        throw new ArrayStoreException();
                    if ("any".equals(args[0]))
                        throw new IllegalAccessException();
                }
                return null;
            }
        });

        Support_Proxy_I1 proxy = (Support_Proxy_I1) p;
        assertTrue("Failed identity test ", proxy.equals(proxy));
        assertTrue("Failed not equals test ", !proxy.equals(""));
        int[] result = (int[]) proxy.array(new long[] { 100L, -200L });
        assertEquals("Failed primitive type conversion test ", -200, result[0]);

        boolean worked = false;
        try {
            proxy.string("");
        } catch (Support_Proxy_SubException e) {
            worked = true;
        } catch (Support_Proxy_ParentException e) { // is never thrown
        }
        assertTrue("Problem converting exception ", worked);

        worked = false;
        try {
            proxy.string("clone");
        } catch (Support_Proxy_ParentException e) { // is never thrown
        } catch (UndeclaredThrowableException e) {
            worked = true;
        }
        assertTrue("Problem converting exception ", worked);

        worked = false;
        try {
            proxy.string("error");
        } catch (Support_Proxy_ParentException e) { // is never thrown
        } catch (UndeclaredThrowableException e) {
        } catch (RuntimeException e) {
            worked = e.getClass() == ArrayStoreException.class;
        }
        assertTrue("Problem converting exception ", worked);

        worked = false;
        try {
            proxy.string("any");
        } catch (Support_Proxy_ParentException e) { // is never thrown
        } catch (UndeclaredThrowableException e) {
            worked = true;
        }
        assertTrue("Problem converting exception ", worked);

        Broken1 proxyObject = null;
        try {
            proxyObject = (Broken1) Proxy.newProxyInstance(Broken1.class
                    .getClassLoader(), new Class[] { Broken1.class },
                    new Broken1Invoke());
        } catch (Throwable e) {
            fail("Failed to create proxy for class: " + Broken1.class + " - "
                    + e);
        }
        float brokenResult = proxyObject.method(2.1f, 5.8f);
        assertTrue("Invalid invoke result", brokenResult == 5.8f);
    
public voidtest_newProxyInstance_withCompatibleReturnTypes()

        Object o = Proxy
                .newProxyInstance(this.getClass().getClassLoader(),
                        new Class[] { ITestReturnObject.class,
                                ITestReturnString.class },
                        new TestProxyHandler(new TestProxyImpl()));
        assertNotNull(o);
    
public voidtest_newProxyInstance_withNonCompatibleReturnTypes()

        try {
            Proxy.newProxyInstance(this.getClass().getClassLoader(),
                    new Class[] { ITestReturnInteger.class,
                            ITestReturnString.class }, new TestProxyHandler(
                            new TestProxyImpl()));
            fail("should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }