KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > component > proxy > test > ExternalComponentProxyTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.component.proxy.test;
19
20 import java.util.Set JavaDoc;
21 import java.util.TreeSet JavaDoc;
22
23 import org.sape.carbon.core.component.Component;
24 import org.sape.carbon.core.component.Lookup;
25
26 import junit.extensions.ActiveTestSuite;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 //end temp
31

32 /**
33  * Tests the ComponentProxy behaving as the Functional Implementation.
34  * <p>
35  * <code>testRetrieval()</code>: Confirms that a retrieved Component implements
36  * its Functional Interface, and sets the Component that gets used for the
37  * remaining tests.<br/>
38  * <code>testCorrectMethodCalled()</code>: Confirms that the correct method is
39  * called on the underlying Functional Implementation. Uses a callback for
40  * confirmation, since return-type is isolated in its own test.<br/>
41  * <code>testReturnType()</code>: Confirms that the correct type is returned
42  * when a method is called.
43  * <code>testException()</code>: Confirms that an Exception is properly passed
44  * through.
45  * <code>testReentrantMethod()</code>: Calls a long-duration method. Intended
46  * to be used in a multithreaded test to confirm that the container does not
47  * introduce any extra blocking.
48  * </p>
49  *
50  * Copyright 2002 Sapient
51  * @since carbon 1.0
52  * @author Chris Herron, Febuary 2002
53  * @version $Revision: 1.11 $($Author: ghinkl $ / $Date: 2003/10/10 14:55:33 $)
54  */

55 public class ExternalComponentProxyTest extends TestCase {
56     public ExternalComponentProxyTest(String JavaDoc name) {
57         super(name);
58     }
59
60     /**
61      * Confirms that a retrieved Component implements its Functional Interface,
62      * and sets the Component that gets used for the remaining tests.
63      */

64     public void testRetrieval() {
65         Object JavaDoc component =
66             Lookup.getInstance().fetchComponent(
67             TEST_COMPONENT_NAME);
68
69         assertTrue("Component lookup returned null", component != null);
70         assertTrue("Component does not implement the Functional" +
71                    "Interface", component instanceof TestComponent);
72
73     }
74
75     public void testCallSpeed() {
76         TestComponent component =
77             (TestComponent)
78             Lookup.getInstance().fetchComponent(
79             TEST_COMPONENT_NAME);
80         long start = System.currentTimeMillis();
81         for (int i=0; i < 500000;i++) {
82             component.doNothing();
83         }
84         System.out.println("Timed component calls took: " + (System.currentTimeMillis() - start));
85
86     }
87
88     /**
89      * Confirms that the correct method is called on the underlying Functional
90      * Implementation. Uses a callback for confirmation, since return-type is
91      * isolated in its own test.
92      */

93     public void testCorrectMethodCalled() {
94         int counterVal = ExternalComponentProxyTest.callBackCounter++;
95
96         TestComponent component =
97             (TestComponent) Lookup.getInstance().fetchComponent(
98             TEST_COMPONENT_NAME);
99
100         assertTrue("Component lookup returned null", component != null);
101         assertTrue("Component does not implement the Functional" +
102                     "Interface", component instanceof TestComponent);
103         String JavaDoc workToken = ""+counterVal;
104         component.doSomeWork(workToken);
105         assertTrue("No confirmation from Component call was received",
106             ExternalComponentProxyTest.callBackConfirmSet.contains(workToken));
107         ExternalComponentProxyTest.callBackConfirmSet.remove(workToken);
108     }
109
110
111     /**
112      * Confirms that an Exception is properly passed through.
113      */

114     public void testException() {
115         TestComponent component =
116             (TestComponent) Lookup.getInstance().fetchComponent(
117             TEST_COMPONENT_NAME);
118
119         boolean expectedExceptionCaught = false;
120         try{
121             component.intentionalException();
122         }
123         catch(Exception JavaDoc e){
124             expectedExceptionCaught = true;
125         }
126         finally{
127             assertTrue("The expected Exception was not thrown",
128                         expectedExceptionCaught);
129         }
130     }
131
132     /**
133      * Confirms that the correct type is returned when a method is called.
134      */

135     public void testReturnType() {
136         TestComponent component =
137             (TestComponent) Lookup.getInstance().fetchComponent(
138             TEST_COMPONENT_NAME);
139
140         Object JavaDoc result = component.returnTestReturnType();
141         assertTrue("Method returned the wrong type",
142             result instanceof TestReturnType);
143     }
144
145     /**
146      * Calls a long-duration method. Intended to be used in a multithreaded test
147      * to confirm that the container does not introduce any extra blocking.
148      * Expect a timeout as the only failure.
149      */

150     public void testReentrantMethod(){
151         TestComponent component =
152             (TestComponent) Lookup.getInstance().fetchComponent(
153             TEST_COMPONENT_NAME);
154         assertTrue("Method returned false - this should never happen",
155             component.reentrantMethod());
156     }
157
158     /**
159      * Calls a long-duration method. Intended to be used in a multithreaded test
160      * to confirm that the container does not introduce any extra blocking.
161      * Expect a timeout as the only failure.
162      */

163     public void testGetComponentName(){
164         Component component = Lookup.getInstance().fetchComponent(
165             TEST_COMPONENT_NAME);
166
167         assertTrue("Component name was incorrect, expected [" +
168             TEST_COMPONENT_NAME + "] got [" +
169             component.getComponentName() + "]",
170             TEST_COMPONENT_NAME.equals(component.getComponentName()));
171     }
172
173     //Static members and methods from here on
174
private static int callBackCounter = 0;
175     private static Set JavaDoc callBackConfirmSet = new TreeSet JavaDoc();
176     private static final String JavaDoc TEST_COMPONENT_NAME =
177         "/core/component/proxy/test/TestComponent";
178
179     public static void calleeConfirm(String JavaDoc callBackID){
180         ExternalComponentProxyTest.callBackConfirmSet.add(callBackID);
181     }
182
183     /** Method called by jUnit to get all the tests in this test case */
184     public static Test suite() {
185         TestSuite masterSuite = new TestSuite("ExternalComponentProxyTest");
186         // add single threaded tests
187
Test singleThreadedTests = getSingleThreadedTests();
188         if (singleThreadedTests != null) {
189             masterSuite.addTest(singleThreadedTests);
190         }
191         // add multi threaded tests
192
Test multiThreadedTests = getMultiThreadedTests();
193         if (multiThreadedTests != null) {
194             masterSuite.addTest(multiThreadedTests);
195         }
196         return masterSuite;
197     }
198
199     /**
200      * This method is used within the suite method to get all of the single
201      * threaded tests. Add all your single threaded tests in this method with a
202      * line like:
203      * suite.addTest(new ExternalComponentProxyTest("testFunction1"));
204      */

205     private static Test getSingleThreadedTests() {
206         TestSuite suite = new TestSuite();
207
208         for (int i = 1; i <= 8; i++) {
209             TestSuite subtest = new ActiveTestSuite();
210             addTest(subtest, "testCallSpeed",i);
211             suite.addTest(subtest);
212         }
213
214
215         suite.addTest(new ExternalComponentProxyTest("testRetrieval"));
216         suite.addTest(
217             new ExternalComponentProxyTest("testCorrectMethodCalled"));
218         suite.addTest(new ExternalComponentProxyTest("testReturnType"));
219         suite.addTest(new ExternalComponentProxyTest("testException"));
220         suite.addTest(new ExternalComponentProxyTest("testGetComponentName"));
221
222         return suite;
223     }
224
225     /**
226      * This method is used within the suite method to get all of the multi
227      * threaded tests. Add all your multi threaded tests in this method with a
228      * line like: addTest(suite, "testFunction1", 5);
229      */

230     private static Test getMultiThreadedTests() {
231         TestSuite suite = new ActiveTestSuite();
232         addTest(suite, "testReentrantMethod", 10);
233         return suite;
234     }
235
236     /**
237      * This method will add the give test to the give suite the specified
238      * number of times. This is best used for multi-threaded tests where
239      * suite is an instance of ActiveTestSuite and you want to run the same
240      * test in multiple threads.
241      */

242     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
243         for (int count = 0; count < number; count++) {
244             suite.addTest(new ExternalComponentProxyTest(testName));
245         }
246     }
247 }
248
Popular Tags