1 16 17 package org.springframework.aop.framework; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Proxy ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 25 import junit.framework.TestCase; 26 27 import org.springframework.beans.ITestBean; 28 import org.springframework.beans.TestBean; 29 30 34 public class AopProxyUtilsTests extends TestCase { 35 36 public void testCompleteProxiedInterfacesWorksWithNull() { 37 AdvisedSupport as = new AdvisedSupport(); 38 Class [] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); 39 assertEquals(1, completedInterfaces.length); 40 assertEquals(Advised.class, completedInterfaces[0]); 41 } 42 43 public void testCompleteProxiedInterfacesWorksWithNullOpaque() { 44 AdvisedSupport as = new AdvisedSupport(); 45 as.setOpaque(true); 46 Class [] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); 47 assertEquals(0, completedInterfaces.length); 48 } 49 50 public void testCompleteProxiedInterfacesAdvisedNotIncluded() { 51 AdvisedSupport as = new AdvisedSupport(); 52 as.addInterface(ITestBean.class); 53 as.addInterface(Comparable .class); 54 Class [] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); 55 assertEquals(3, completedInterfaces.length); 56 57 58 List l = Arrays.asList(completedInterfaces); 60 assertTrue(l.contains(Advised.class)); 61 assertTrue(l.contains(ITestBean.class)); 62 assertTrue(l.contains(Comparable .class)); 63 } 64 65 public void testCompleteProxiedInterfacesAdvisedIncluded() { 66 AdvisedSupport as = new AdvisedSupport(); 67 as.addInterface(ITestBean.class); 68 as.addInterface(Comparable .class); 69 as.addInterface(Advised.class); 70 Class [] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); 71 assertEquals(3, completedInterfaces.length); 72 73 List l = Arrays.asList(completedInterfaces); 75 assertTrue(l.contains(Advised.class)); 76 assertTrue(l.contains(ITestBean.class)); 77 assertTrue(l.contains(Comparable .class)); 78 } 79 80 public void testCompleteProxiedInterfacesAdvisedNotIncludedOpaque() { 81 AdvisedSupport as = new AdvisedSupport(); 82 as.setOpaque(true); 83 as.addInterface(ITestBean.class); 84 as.addInterface(Comparable .class); 85 Class [] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); 86 assertEquals(2, completedInterfaces.length); 87 88 List l = Arrays.asList(completedInterfaces); 90 assertFalse(l.contains(Advised.class)); 91 assertTrue(l.contains(ITestBean.class)); 92 assertTrue(l.contains(Comparable .class)); 93 } 94 95 public void testProxiedUserInterfacesWithSingleInterface() { 96 ProxyFactory pf = new ProxyFactory(); 97 pf.setTarget(new TestBean()); 98 pf.addInterface(ITestBean.class); 99 Object proxy = pf.getProxy(); 100 Class [] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); 101 assertEquals(1, userInterfaces.length); 102 assertEquals(ITestBean.class, userInterfaces[0]); 103 } 104 105 public void testProxiedUserInterfacesWithMultipleInterfaces() { 106 ProxyFactory pf = new ProxyFactory(); 107 pf.setTarget(new TestBean()); 108 pf.addInterface(ITestBean.class); 109 pf.addInterface(Comparable .class); 110 Object proxy = pf.getProxy(); 111 Class [] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); 112 assertEquals(2, userInterfaces.length); 113 assertEquals(ITestBean.class, userInterfaces[0]); 114 assertEquals(Comparable .class, userInterfaces[1]); 115 } 116 117 public void testProxiedUserInterfacesWithNoInterface() { 118 Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class [0], 119 new InvocationHandler () { 120 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 121 return null; 122 } 123 }); 124 try { 125 Class [] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); 126 fail("Should have thrown IllegalArgumentException"); 127 } 128 catch (IllegalArgumentException ex) { 129 } 131 } 132 133 } | Popular Tags |