001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.util;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Set;
027:
028: import org.apache.cactus.ServletTestCase;
029: import org.apache.cactus.util.ChainedRuntimeException;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * Smoke test for the unique id generator.
035: *
036: * @version $Id: TestUniqueGenerator.java 238991 2004-05-22 11:34:50Z vmassol $
037: */
038: public class TestUniqueGenerator extends TestCase {
039: /**
040: * @see TestCase#setUp
041: */
042: protected void setUp() {
043: // let the generator initialize
044: UniqueGenerator.generate(new ServletTestCase("foo"));
045: }
046:
047: /**
048: * Simulates several simultaneous id generations using threads.
049: * Verifies that there are no duplicates among the generated ids.
050: */
051: public void testThatSimultaneouslyGeneratedIdsAreUnique() {
052: final ServletTestCase aTestCase = new ServletTestCase("foo");
053:
054: Thread[] threads = new Thread[10];
055: final List results = Collections
056: .synchronizedList(new ArrayList());
057: for (int i = 0; i < threads.length; i++) {
058: threads[i] = new Thread() {
059: public void run() {
060: results.add(UniqueGenerator.generate(aTestCase));
061: }
062: };
063: }
064:
065: // loops separate to make their beginning as simultaneous
066: // as possible
067: for (int i = 0; i < threads.length; i++) {
068: threads[i].run();
069: }
070:
071: try {
072: // in case the threads need time to finish
073: Thread.sleep(200);
074: } catch (InterruptedException e) {
075: throw new ChainedRuntimeException(e);
076: }
077:
078: Set resultSet = new HashSet(results);
079: assertEquals("Results contained duplicate ids.",
080: results.size(), resultSet.size());
081: }
082:
083: /**
084: * Sanity check to verify that different IDs are generated for different
085: * instances of the test class.
086: */
087: public void testThatGeneratedIdsForDifferentTestCasesAreUnique() {
088: final ServletTestCase firstTestCase = new ServletTestCase("foo");
089: final ServletTestCase secondTestCase = new ServletTestCase(
090: "foo");
091:
092: String firstId = UniqueGenerator.generate(firstTestCase, 0);
093: String secondId = UniqueGenerator.generate(secondTestCase, 0);
094:
095: assertFalse("IDs not unique", firstId.equals(secondId));
096: }
097:
098: /**
099: * Sanity check to verify that different IDs are generated for different
100: * test methods/names.
101: */
102: public void testThatGeneratedIdsForDifferentTestMethodsAreUnique() {
103: final ServletTestCase aTestCase = new ServletTestCase("foo");
104:
105: String firstId = UniqueGenerator.generate(aTestCase, 0);
106: aTestCase.setName("bar");
107: String secondId = UniqueGenerator.generate(aTestCase, 0);
108:
109: assertFalse("IDs not unique", firstId.equals(secondId));
110: }
111:
112: }
|