KickJava   Java API By Example, From Geeks To Geeks.

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


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.test;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27
28 import junit.framework.Test;
29
30 import org.jboss.test.proxyfactory.AbstractProxyTest;
31 import org.jboss.test.proxyfactory.support.Simple;
32 import org.jboss.test.proxyfactory.support.SimpleBean;
33 import org.jboss.test.proxyfactory.support.SimpleInterceptor;
34
35 /**
36  * SimpleTestCase.
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 44707 $
40  */

41 public class SimpleTestCase extends AbstractProxyTest
42 {
43    private static final int iterations = 1000;
44    
45    public void testSimple() throws Exception JavaDoc
46    {
47       SimpleBean bean = new SimpleBean();
48       Simple simple = (Simple) assertCreateProxy(bean, Simple.class);
49       SimpleInterceptor.invoked = null;
50       simple.doSomething();
51       assertTrue(bean.invoked);
52       Method JavaDoc invoked = SimpleInterceptor.invoked;
53       assertNotNull(invoked);
54       assertEquals("doSomething", invoked.getName());
55    }
56    
57    public void testStressNoProxy() throws Exception JavaDoc
58    {
59       for (int i = 0; i < iterations; ++i)
60       {
61          SimpleBean bean = new SimpleBean();
62          bean.doSomething();
63       }
64    }
65
66    public class MyInvocationHandler implements InvocationHandler JavaDoc
67    {
68       private Object JavaDoc target;
69
70       public MyInvocationHandler(Object JavaDoc target)
71       {
72          this.target = target;
73       }
74       
75       public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
76       {
77          return method.invoke(target, args);
78       }
79    }
80    
81    public void testStressDynamicProxy() throws Exception JavaDoc
82    {
83       Class JavaDoc[] interfaces = new Class JavaDoc[] { Simple.class };
84       ClassLoader JavaDoc cl = Simple.class.getClassLoader();
85       for (int i = 0; i < iterations; ++i)
86       {
87          SimpleBean bean = new SimpleBean();
88          InvocationHandler JavaDoc ih = new MyInvocationHandler(bean);
89          Simple simple = (Simple) Proxy.newProxyInstance(cl, interfaces, ih);
90          simple.doSomething();
91       }
92    }
93    
94    public void testStressProxyFactory() throws Exception JavaDoc
95    {
96       for (int i = 0; i < iterations; ++i)
97       {
98          SimpleBean bean = new SimpleBean();
99          Simple simple = (Simple) createProxy(bean);
100          simple.doSomething();
101       }
102    }
103    
104    public static Test suite()
105    {
106       return suite(SimpleTestCase.class);
107    }
108
109    public SimpleTestCase(String JavaDoc name)
110    {
111       super(name);
112    }
113 }
114
Popular Tags