KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > AopProxyUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.framework;
18
19 import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.springframework.beans.ITestBean;
28 import org.springframework.beans.TestBean;
29
30 /**
31  *
32  * @author Rod Johnson
33  */

34 public class AopProxyUtilsTests extends TestCase {
35     
36     public void testCompleteProxiedInterfacesWorksWithNull() {
37         AdvisedSupport as = new AdvisedSupport();
38         Class JavaDoc[] 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 JavaDoc[] 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 JavaDoc.class);
54         Class JavaDoc[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as);
55         assertEquals(3, completedInterfaces.length);
56         
57         
58         // Can't assume ordering for others, so use a list
59
List JavaDoc l = Arrays.asList(completedInterfaces);
60         assertTrue(l.contains(Advised.class));
61         assertTrue(l.contains(ITestBean.class));
62         assertTrue(l.contains(Comparable JavaDoc.class));
63     }
64     
65     public void testCompleteProxiedInterfacesAdvisedIncluded() {
66         AdvisedSupport as = new AdvisedSupport();
67         as.addInterface(ITestBean.class);
68         as.addInterface(Comparable JavaDoc.class);
69         as.addInterface(Advised.class);
70         Class JavaDoc[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as);
71         assertEquals(3, completedInterfaces.length);
72         
73         // Can't assume ordering for others, so use a list
74
List JavaDoc l = Arrays.asList(completedInterfaces);
75         assertTrue(l.contains(Advised.class));
76         assertTrue(l.contains(ITestBean.class));
77         assertTrue(l.contains(Comparable JavaDoc.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 JavaDoc.class);
85         Class JavaDoc[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as);
86         assertEquals(2, completedInterfaces.length);
87         
88         // Can't assume ordering for others, so use a list
89
List JavaDoc l = Arrays.asList(completedInterfaces);
90         assertFalse(l.contains(Advised.class));
91         assertTrue(l.contains(ITestBean.class));
92         assertTrue(l.contains(Comparable JavaDoc.class));
93     }
94
95     public void testProxiedUserInterfacesWithSingleInterface() {
96         ProxyFactory pf = new ProxyFactory();
97         pf.setTarget(new TestBean());
98         pf.addInterface(ITestBean.class);
99         Object JavaDoc proxy = pf.getProxy();
100         Class JavaDoc[] 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 JavaDoc.class);
110         Object JavaDoc proxy = pf.getProxy();
111         Class JavaDoc[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy);
112         assertEquals(2, userInterfaces.length);
113         assertEquals(ITestBean.class, userInterfaces[0]);
114         assertEquals(Comparable JavaDoc.class, userInterfaces[1]);
115     }
116
117     public void testProxiedUserInterfacesWithNoInterface() {
118         Object JavaDoc proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class JavaDoc[0],
119                 new InvocationHandler JavaDoc() {
120                     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
121                         return null;
122                     }
123                 });
124         try {
125             Class JavaDoc[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy);
126             fail("Should have thrown IllegalArgumentException");
127         }
128         catch (IllegalArgumentException JavaDoc ex) {
129             // expected
130
}
131     }
132
133 }
Popular Tags