KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > uniqueid > test > UniqueIDServiceTest


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.services.uniqueid.test;
19
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.sape.carbon.core.component.Lookup;
27 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor;
28 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum;
29 import org.sape.carbon.core.component.lifecycle.StateTransitionException;
30 import org.sape.carbon.core.config.InvalidConfigurationException;
31 import org.sape.carbon.core.exception.ExceptionUtility;
32 import org.sape.carbon.services.sql.StatementFactory;
33 import org.sape.carbon.services.sql.StatementFactoryException;
34 import org.sape.carbon.services.uniqueid.UniqueIDNotFoundException;
35 import org.sape.carbon.services.uniqueid.UniqueIDService;
36 import org.sape.carbon.services.uniqueid.UniqueIDServiceException;
37
38 import junit.extensions.ActiveTestSuite;
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43
44 /**
45  * Test harness for the DefaultUniqueIDServiceImpl implementation of the
46  * UniqueIDService
47  *
48  * Copyright 2002 Sapient
49  * @since carbon 1.0
50  * @author Douglas Voet, July 2002
51  * @version $Revision: 1.6 $($Author: dvoet $ / $Date: 2003/05/05 21:21:38 $)
52  */

53 public class UniqueIDServiceTest extends TestCase {
54
55     private static Set JavaDoc usedIDs =
56         Collections.synchronizedSet(new HashSet JavaDoc());
57     private static final int NUM_ITERATIONS = 100;
58     private static final int NUM_THREADS = 2;
59
60     private static final String JavaDoc ID_NOT_FOUND_NAME =
61         "/uniqueid/test/NotFoundUniqueIDService";
62     private static final String JavaDoc INVALID_ID_SERVICE =
63         "/uniqueid/test/InvalidUniqueIDService";
64     private static final String JavaDoc INVALID_CONNECTION_SERVICE =
65         "/uniqueid/test/InvalidConnectionUniqueIDService";
66     private static final String JavaDoc ID_SERVICE_BLOCK_SIZE_1 =
67         "/uniqueid/test/BlockSize1UniqueIDService";
68     private static final String JavaDoc ID_SERVICE_1 =
69         "/uniqueid/test/UniqueIDService1";
70     private static final String JavaDoc ID_SERVICE_2 =
71         "/uniqueid/test/UniqueIDService2";
72     private static final String JavaDoc ID_SERVICE_3 =
73         "/uniqueid/test/UniqueIDService3";
74     private static final String JavaDoc UNIQUE_ID_STATEMENT_FACTORY =
75         "/uniqueid/test/UniqueIDStatementFactory";
76     private static final String JavaDoc CREATE_TABLE_UNIQUE_ID_QUERY =
77         "CreateTableUniqueID";
78     private static final String JavaDoc DROP_TABLE_UNIQUE_ID_QUERY =
79         "DropTableUniqueID";
80
81
82     public UniqueIDServiceTest(String JavaDoc name) {
83         super(name);
84     }
85
86     /**
87      * Method create table for unique id service to work in case it is
88      * not already there.
89      */

90     public void createUniqueIdTable() {
91         PreparedStatement JavaDoc preparedStatement=null;
92
93         try {
94
95             StatementFactory sf = (StatementFactory)
96             Lookup.getInstance().fetchComponent(UNIQUE_ID_STATEMENT_FACTORY);
97             preparedStatement =
98                 sf.createPreparedStatement(CREATE_TABLE_UNIQUE_ID_QUERY);
99             preparedStatement.executeUpdate();
100         } catch (SQLException JavaDoc se) {
101           // expected. this case will arise when table already existed
102
} catch (StatementFactoryException sfe) {
103           // expected. this case will arise when table already existed
104
}
105
106     }
107
108
109     /**
110      * Tests that the appropriate exception is thrown when an ID is not
111      * found and AutoCreate is false
112      */

113     public void testIDNotFound() {
114         UniqueIDService uniqueIDService =
115             (UniqueIDService) Lookup.getInstance().fetchComponent(
116                 ID_NOT_FOUND_NAME);
117
118         try {
119             uniqueIDService.getNextID();
120
121             fail("Did not catch expected UniqueIDNotFoundException");
122         } catch (UniqueIDNotFoundException uidnfe) {
123             // expected
124
} catch (UniqueIDServiceException uidse) {
125             fail(
126                 "Did not catch expected UniqueIDNotFoundException, "
127                     + "caught UniqueIDServiceException: "
128                     + uidse
129                     + ExceptionUtility.captureStackTrace(uidse));
130         }
131     }
132
133     /**
134      * Tests to make sure that the service does not skip IDs under single
135      * threaded, single client conditions
136      */

137     public void testIDsNotSkipped() {
138         UniqueIDService uniqueIDService =
139             (UniqueIDService) Lookup.getInstance().fetchComponent(
140                 ID_SERVICE_BLOCK_SIZE_1);
141
142         try {
143             // initialize ID trackers
144
long currentID = uniqueIDService.getNextID();
145             long lastID = currentID;
146
147             for (int i = 0; i < 10; i++) {
148                 currentID = uniqueIDService.getNextID();
149
150                 if (currentID <= lastID) {
151                     fail(
152                         "UniqueIDService returned an ID out of sequence. "
153                             + "IDs should be in sequence under test conditions");
154                 }
155
156                 lastID = currentID;
157             }
158
159         } catch (UniqueIDServiceException uidse) {
160             fail(
161                 "Caught UniqueIDServiceException: "
162                     + uidse
163                     + ExceptionUtility.captureStackTrace(uidse));
164         }
165     }
166
167     /**
168      * Tests that the appropriate exception is thrown when an invalid
169      * configuration is used
170      */

171     public void testInvalidConfiguration() {
172         try {
173             Lookup.getInstance().fetchComponent(INVALID_ID_SERVICE);
174
175             fail("Did not catch expected StateTransition or "
176                 + "InvalidConfiguration exception");
177         } catch (InvalidConfigurationException ice) {
178             // expected
179
} catch (StateTransitionException ste) {
180             //expected
181
}
182
183     }
184
185     /**
186      * Tests that an appropriate exception is thrown when an invalid
187      * connection is used.
188      */

189     public void testInvalidConnectionFactory() throws Exception JavaDoc {
190         try {
191             UniqueIDService uniqueIDService =
192                 (UniqueIDService) Lookup.getInstance().fetchComponent(
193                     INVALID_CONNECTION_SERVICE);
194
195             uniqueIDService.getNextID();
196
197             fail("Did not catch expected UniqueIDServiceException "
198                 + "when using an invalid connection to the database.");
199
200         } catch (UniqueIDServiceException uidse) {
201             // expected
202
}
203     }
204
205     /**
206      * Tests that returned IDs are unique in multi-threaded, multi-client
207      * conditions. Used in conjunction with testMultiComponentAccess2
208      * and testMultiComponentAccess3 started in an ActiveTestSuite
209      */

210     public void testMultiComponentAccess1() {
211         testMultiComponentAccess(ID_SERVICE_1);
212     }
213
214     /** see testMultiComponentAccess1 */
215     public void testMultiComponentAccess2() {
216         testMultiComponentAccess(ID_SERVICE_2);
217     }
218
219     /** see testMultiComponentAccess1 */
220     public void testMultiComponentAccess3() {
221         testMultiComponentAccess(ID_SERVICE_3);
222     }
223
224     /** Called by testMultiComponentAccess1 2 & 3 to to run the test */
225     private void testMultiComponentAccess(String JavaDoc uniqueIDServiceName) {
226         UniqueIDService uniqueIDService =
227             (UniqueIDService) Lookup.getInstance().fetchComponent(
228                 uniqueIDServiceName);
229
230         while (((LifecycleInterceptor) uniqueIDService).getLifecycleState()
231             != LifecycleStateEnum.RUNNING) {
232
233             try {
234                 Thread.sleep(100);
235             } catch (InterruptedException JavaDoc e) {
236             }
237         }
238
239         try {
240             for (int i = 0; i < NUM_ITERATIONS; i++) {
241                 boolean idNotUsed =
242                     UniqueIDServiceTest.usedIDs.add(
243                         new Long JavaDoc(uniqueIDService.getNextID()));
244
245                 if (!idNotUsed) {
246                     fail("UniqueIDService returned a non-unique ID");
247                 }
248             }
249
250         } catch (UniqueIDServiceException uidse) {
251             fail(
252                 "Caught UniqueIDServiceException: "
253                     + uidse
254                     + ExceptionUtility.captureStackTrace(uidse));
255         }
256     }
257
258     /**
259      * Method called by jUnit to get all the tests in this test case.
260      * @return Test the suite of tests in this test case
261      */

262     public static Test suite() {
263         TestSuite masterSuite = new TestSuite();
264         // add single threaded tests
265
Test singleThreadedTests = getSingleThreadedTests();
266         if (singleThreadedTests != null) {
267             masterSuite.addTest(singleThreadedTests);
268         }
269         // add multi threaded tests
270
Test multiThreadedTests = getMultiThreadedTests();
271         if (multiThreadedTests != null) {
272             masterSuite.addTest(multiThreadedTests);
273         }
274         return masterSuite;
275     }
276
277     /**
278      * Single threaded tests:
279      *
280      * testIDNotFound
281      * testIDsNotSkipped
282      * testInvalidConfiguration
283      * testInvalidConnectionFactory
284      *
285      * @return Test the suite of single threaded tests in this test case
286      */

287     private static Test getSingleThreadedTests() {
288         TestSuite suite = new TestSuite();
289         suite.addTest(new UniqueIDServiceTest("createUniqueIdTable"));
290         suite.addTest(new UniqueIDServiceTest("testIDNotFound"));
291         suite.addTest(new UniqueIDServiceTest("testIDsNotSkipped"));
292         suite.addTest(new UniqueIDServiceTest("testInvalidConfiguration"));
293         suite.addTest(new UniqueIDServiceTest("testInvalidConnectionFactory"));
294
295         return suite;
296     }
297
298     /**
299      * Multi-Threaded tests
300      *
301      * testMultiComponentAccess1
302      * testMultiComponentAccess2
303      * testMultiComponentAccess3
304      *
305      * @return Test the suite of multi-threaded tests in this test case
306      */

307     private static Test getMultiThreadedTests() {
308         TestSuite suite = new ActiveTestSuite();
309
310         addTest(
311             suite,
312             "testMultiComponentAccess1",
313             UniqueIDServiceTest.NUM_THREADS);
314
315
316         // Akash:
317
// Commented out to as these test cases were failing
318
// MultiComponentAccess1 already checks for synchronization by using
319
// multiple threads to call getNextID() method of uniqueId Service.
320
// Providing 3 MultiComponent accesses leads to synchronization problem
321
// for the same id name block in the table at the time of creation of this
322
// ID name. This scenario is not realistic from usage point of view as no
323
// two uniqueId components should work on the same ID name.
324

325         addTest(
326             suite,
327             "testMultiComponentAccess2",
328             UniqueIDServiceTest.NUM_THREADS);
329         addTest(
330             suite,
331             "testMultiComponentAccess3",
332             UniqueIDServiceTest.NUM_THREADS);
333
334         return suite;
335     }
336
337     /**
338      * This method will add the give test to the give suite the specified
339      * number of times. This is best used for multi-threaded tests where
340      * suite is an instance of ActiveTestSuite and you want to run the same test in
341      * multiple threads.
342      * @param suite the suite to add the test to.
343      * @param testName the name of the test to add.
344      * @param number the number of times to add the test to the suite
345      */

346     private static void addTest(
347         TestSuite suite,
348         String JavaDoc testName,
349         int number) {
350         for (int count = 0; count < number; count++) {
351             suite.addTest(new UniqueIDServiceTest(testName));
352         }
353     }
354 }
Popular Tags