KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > TestUniqueGenerator


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

20 package org.apache.cactus.internal.util;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.cactus.ServletTestCase;
29 import org.apache.cactus.util.ChainedRuntimeException;
30
31 import junit.framework.TestCase;
32
33 /**
34  * Smoke test for the unique id generator.
35  *
36  * @version $Id: TestUniqueGenerator.java,v 1.1 2004/05/22 11:34:46 vmassol Exp $
37  */

38 public class TestUniqueGenerator extends TestCase
39 {
40     /**
41      * @see TestCase#setUp
42      */

43     protected void setUp()
44     {
45         // let the generator initialize
46
UniqueGenerator.generate(new ServletTestCase("foo"));
47     }
48
49     /**
50      * Simulates several simultaneous id generations using threads.
51      * Verifies that there are no duplicates among the generated ids.
52      */

53     public void testThatSimultaneouslyGeneratedIdsAreUnique()
54     {
55         final ServletTestCase aTestCase = new ServletTestCase("foo");
56
57         Thread JavaDoc[] threads = new Thread JavaDoc[10];
58         final List JavaDoc results = Collections.synchronizedList(new ArrayList JavaDoc());
59         for (int i = 0; i < threads.length; i++)
60         {
61             threads[i] = new Thread JavaDoc()
62             {
63                 public void run()
64                 {
65                     results.add(UniqueGenerator.generate(aTestCase));
66                 }
67             };
68         }
69
70         // loops separate to make their beginning as simultaneous
71
// as possible
72
for (int i = 0; i < threads.length; i++)
73         {
74             threads[i].run();
75         }
76
77         try
78         {
79             // in case the threads need time to finish
80
Thread.sleep(200);
81         }
82         catch (InterruptedException JavaDoc e)
83         {
84             throw new ChainedRuntimeException(e);
85         }
86
87         Set JavaDoc resultSet = new HashSet JavaDoc(results);
88         assertEquals(
89             "Results contained duplicate ids.",
90             results.size(),
91             resultSet.size());
92     }
93
94     /**
95      * Sanity check to verify that different IDs are generated for different
96      * instances of the test class.
97      */

98     public void testThatGeneratedIdsForDifferentTestCasesAreUnique()
99     {
100         final ServletTestCase firstTestCase = new ServletTestCase("foo");
101         final ServletTestCase secondTestCase = new ServletTestCase("foo");
102         
103         String JavaDoc firstId = UniqueGenerator.generate(firstTestCase, 0);
104         String JavaDoc secondId = UniqueGenerator.generate(secondTestCase, 0);
105
106         assertFalse("IDs not unique", firstId.equals(secondId));
107     }
108
109     /**
110      * Sanity check to verify that different IDs are generated for different
111      * test methods/names.
112      */

113     public void testThatGeneratedIdsForDifferentTestMethodsAreUnique()
114     {
115         final ServletTestCase aTestCase = new ServletTestCase("foo");
116         
117         String JavaDoc firstId = UniqueGenerator.generate(aTestCase, 0);
118         aTestCase.setName("bar");
119         String JavaDoc secondId = UniqueGenerator.generate(aTestCase, 0);
120
121         assertFalse("IDs not unique", firstId.equals(secondId));
122     }
123
124 }
125
Popular Tags