KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > proxyfactory > AbstractProxyTest


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.proxyfactory;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.jboss.aop.metadata.SimpleMetaData;
28 import org.jboss.aop.proxy.container.AOPProxyFactory;
29 import org.jboss.aop.proxy.container.AOPProxyFactoryMixin;
30 import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
31 import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
32 import org.jboss.test.AbstractTestCaseWithSetup;
33 import org.jboss.test.AbstractTestDelegate;
34
35 /**
36  * AbstractProxyTest.
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 44806 $
40  */

41 public abstract class AbstractProxyTest extends AbstractTestCaseWithSetup
42 {
43    /** The proxy factory */
44    protected AOPProxyFactory proxyFactory;
45
46    /**
47     * Get the test delegate
48     *
49     * @param clazz the test class
50     * @return the delegate
51     * @throws Exception for any error
52     */

53    public static AbstractTestDelegate getDelegate(Class JavaDoc clazz) throws Exception JavaDoc
54    {
55       return new AbstractProxyTestDelegate(clazz);
56    }
57
58    /**
59     * Create a new AbstractProxyTest.
60     *
61     * @param name the test name
62     */

63    public AbstractProxyTest(String JavaDoc name)
64    {
65       super(name);
66    }
67    
68    protected void setUp() throws Exception JavaDoc
69    {
70       super.setUp();
71       configureLogging();
72       proxyFactory = new GeneratedAOPProxyFactory();
73    }
74
75    /**
76     * Create a proxy
77     *
78     * @param target the target
79     * @return the proxy
80     * @throws Exception for any error
81     */

82    protected Object JavaDoc createProxy(Object JavaDoc target) throws Exception JavaDoc
83    {
84       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
85       params.setProxiedClass(target.getClass());
86       params.setTarget(target);
87       return proxyFactory.createAdvisedProxy(params);
88    }
89
90    /**
91     * Create a proxy
92     *
93     * @param target the target
94     * @param expected the expected class
95     * @return the proxy
96     * @throws Exception for any error
97     */

98    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc expected) throws Exception JavaDoc
99    {
100       Object JavaDoc proxy = createProxy(target);
101       assertNotNull(proxy);
102       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
103       return proxy;
104    }
105
106    /**
107     * Create a proxy
108     *
109     * @param target the target
110     * @param interfaces the interfaces
111     * @return the proxy
112     * @throws Exception for any error
113     */

114    protected Object JavaDoc createProxy(Object JavaDoc target, Class JavaDoc[] interfaces) throws Exception JavaDoc
115    {
116       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
117       params.setProxiedClass(target.getClass());
118       params.setInterfaces(interfaces);
119       params.setTarget(target);
120       return proxyFactory.createAdvisedProxy(params);
121    }
122
123    /**
124     * Create a proxy
125     *
126     * @param target the target
127     * @param mixins the mixins
128     * @return the proxy
129     * @throws Exception for any error
130     */

131    protected Object JavaDoc createProxy(Object JavaDoc target, AOPProxyFactoryMixin[] mixins) throws Exception JavaDoc
132    {
133       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
134       params.setProxiedClass(target.getClass());
135       params.setMixins(mixins);
136       params.setTarget(target);
137       return proxyFactory.createAdvisedProxy(params);
138    }
139
140    /**
141     * Create a proxy
142     *
143     * @param target the target
144     * @param interfaces the interfaces
145     * @param mixins the mixins
146     * @return the proxy
147     * @throws Exception for any error
148     */

149    protected Object JavaDoc createProxy(Object JavaDoc target, Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins) throws Exception JavaDoc
150    {
151       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
152       params.setProxiedClass(target.getClass());
153       params.setInterfaces(interfaces);
154       params.setMixins(mixins);
155       params.setTarget(target);
156       return proxyFactory.createAdvisedProxy(params);
157    }
158    /**
159     * Create a proxy
160     *
161     * @param target the target
162     * @param mixins the mixins
163     * @param expected the expected class
164     * @return the proxy
165     * @throws Exception for any error
166     */

167    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, AOPProxyFactoryMixin[] mixins, Class JavaDoc expected) throws Exception JavaDoc
168    {
169       Object JavaDoc proxy = createProxy(target, mixins);
170       assertNotNull(proxy);
171       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
172       return proxy;
173    }
174
175    /**
176     * Create a proxy
177     *
178     * @param target the target
179     * @param interfaces the interfaces
180     * @param mixins the mixins
181     * @param expected the expected class
182     * @return the proxy
183     * @throws Exception for any error
184     */

185    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, Class JavaDoc[] expected) throws Exception JavaDoc
186    {
187       Object JavaDoc proxy = createProxy(target, interfaces, mixins);
188       assertNotNull(proxy);
189       for (int i = 0 ; i < expected.length ; i++)
190       {
191          assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy));
192       }
193       return proxy;
194    }
195
196    /**
197     * Create a proxy
198     *
199     * @param target the target
200     * @param interfaces the interfaces
201     * @param expected the expected class
202     * @return the proxy
203     * @throws Exception for any error
204     */

205    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc[] interfaces, Class JavaDoc expected) throws Exception JavaDoc
206    {
207       Object JavaDoc proxy = createProxy(target, interfaces);
208       assertNotNull(proxy);
209       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
210       return proxy;
211    }
212
213    /**
214     * Create a proxy
215     *
216     * @param target the target
217     * @param interfaces the interfaces
218     * @param metadata the metadata
219     * @return the proxy
220     * @throws Exception for any error
221     */

222    protected Object JavaDoc createProxy(Object JavaDoc target, Class JavaDoc[] interfaces, SimpleMetaData metaData) throws Exception JavaDoc
223    {
224       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
225       params.setProxiedClass(target.getClass());
226       params.setInterfaces(interfaces);
227       params.setSimpleMetaData(metaData);
228       params.setTarget(target);
229       return proxyFactory.createAdvisedProxy(params);
230    }
231
232    /**
233     * Create a proxy
234     *
235     * @param target the target
236     * @param interfaces the interfaces
237     * @param metadata the metadata
238     * @param expected the expected class
239     * @return the proxy
240     * @throws Exception for any error
241     */

242    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc[] interfaces, SimpleMetaData metaData, Class JavaDoc expected) throws Exception JavaDoc
243    {
244       Object JavaDoc proxy = createProxy(target, interfaces, metaData);
245       assertNotNull(proxy);
246       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
247       return proxy;
248    }
249
250    /**
251     * Create a proxy
252     *
253     * @param interfaces the interfaces
254     * @param metaData the metadata
255     * @return the proxy
256     * @throws Exception for any error
257     */

258    protected Object JavaDoc createHollowProxy(Class JavaDoc[] interfaces, SimpleMetaData metaData) throws Exception JavaDoc
259    {
260       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
261       params.setInterfaces(interfaces);
262       params.setSimpleMetaData(metaData);
263       return proxyFactory.createAdvisedProxy(params);
264    }
265
266    /**
267     * Create a proxy
268     *
269     * @param mixins the mixins
270     * @param metaData the metadata
271     * @return the proxy
272     * @throws Exception for any error
273     */

274    protected Object JavaDoc createHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception JavaDoc
275    {
276       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
277       params.setMixins(mixins);
278       params.setSimpleMetaData(metaData);
279       return proxyFactory.createAdvisedProxy(params);
280    }
281
282    /**
283     * Create a proxy
284     *
285     * @param mixins the mixins
286     * @param metaData the metadata
287     * @return the proxy
288     * @throws Exception for any error
289     */

290    protected Object JavaDoc createHollowProxy(Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception JavaDoc
291    {
292       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
293       params.setInterfaces(interfaces);
294       params.setMixins(mixins);
295       params.setSimpleMetaData(metaData);
296       return proxyFactory.createAdvisedProxy(params);
297    }
298
299    /**
300     * Create a proxy
301     *
302     * @param interfaces the interfaces
303     * @param metadata the metadata
304     * @param expected the expected class
305     * @return the proxy
306     * @throws Exception for any error
307     */

308    protected Object JavaDoc assertCreateHollowProxy(Class JavaDoc[] interfaces, SimpleMetaData metaData, Class JavaDoc expected) throws Exception JavaDoc
309    {
310       Object JavaDoc proxy = createHollowProxy(interfaces, metaData);
311       assertNotNull(proxy);
312       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
313       return proxy;
314    }
315    
316    /**
317     * Create a proxy
318     *
319     * @param interfaces the interfaces
320     * @param metadata the metadata
321     * @param expected the expected class
322     * @return the proxy
323     * @throws Exception for any error
324     */

325    protected Object JavaDoc assertCreateHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class JavaDoc expected) throws Exception JavaDoc
326    {
327       Object JavaDoc proxy = createHollowProxy(mixins, metaData);
328       assertNotNull(proxy);
329       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
330       return proxy;
331    }
332
333    /**
334     * Create a proxy
335     *
336     * @param target the target
337     * @param interfaces the interfaces
338     * @param mixins the mixins
339     * @param expected the expected class
340     * @return the proxy
341     * @throws Exception for any error
342     */

343    protected Object JavaDoc assertCreateHollowProxy(Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class JavaDoc[] expected) throws Exception JavaDoc
344    {
345       Object JavaDoc proxy = createHollowProxy(interfaces, mixins, metaData);
346       assertNotNull(proxy);
347       for (int i = 0 ; i < expected.length ; i++)
348       {
349          assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy));
350       }
351       return proxy;
352    }
353    /**
354     * Get the interfaces for an object
355     *
356     * @param object the object
357     * @return the set of interfaces
358     */

359    protected Set JavaDoc getInterfaces(Object JavaDoc object)
360    {
361       Set JavaDoc interfaces = new HashSet JavaDoc();
362       addInterfaces(interfaces, object.getClass());
363       return interfaces;
364    }
365    
366    /**
367     * Add interfaces
368     *
369     * @param interfaces the interfaces to add to
370     * @param clazz the class
371     */

372    protected void addInterfaces(Set JavaDoc interfaces, Class JavaDoc clazz)
373    {
374       Class JavaDoc[] intfs = clazz.getInterfaces();
375       for (int i = 0; i < intfs.length; ++i)
376          interfaces.add(intfs[i]);
377       Class JavaDoc superClass = clazz.getSuperclass();
378       if (superClass != null)
379          addInterfaces(interfaces, superClass);
380    }
381    
382    /**
383     * Get the delegate
384     *
385     * @return the delegate
386     */

387    protected AbstractProxyTestDelegate getMCDelegate()
388    {
389       return (AbstractProxyTestDelegate) getDelegate();
390    }
391
392 }
393
Popular Tags