1 20 package org.apache.cactus.internal.util; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashSet ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 import org.apache.cactus.ServletTestCase; 29 import org.apache.cactus.util.ChainedRuntimeException; 30 31 import junit.framework.TestCase; 32 33 38 public class TestUniqueGenerator extends TestCase 39 { 40 43 protected void setUp() 44 { 45 UniqueGenerator.generate(new ServletTestCase("foo")); 47 } 48 49 53 public void testThatSimultaneouslyGeneratedIdsAreUnique() 54 { 55 final ServletTestCase aTestCase = new ServletTestCase("foo"); 56 57 Thread [] threads = new Thread [10]; 58 final List results = Collections.synchronizedList(new ArrayList ()); 59 for (int i = 0; i < threads.length; i++) 60 { 61 threads[i] = new Thread () 62 { 63 public void run() 64 { 65 results.add(UniqueGenerator.generate(aTestCase)); 66 } 67 }; 68 } 69 70 for (int i = 0; i < threads.length; i++) 73 { 74 threads[i].run(); 75 } 76 77 try 78 { 79 Thread.sleep(200); 81 } 82 catch (InterruptedException e) 83 { 84 throw new ChainedRuntimeException(e); 85 } 86 87 Set resultSet = new HashSet (results); 88 assertEquals( 89 "Results contained duplicate ids.", 90 results.size(), 91 resultSet.size()); 92 } 93 94 98 public void testThatGeneratedIdsForDifferentTestCasesAreUnique() 99 { 100 final ServletTestCase firstTestCase = new ServletTestCase("foo"); 101 final ServletTestCase secondTestCase = new ServletTestCase("foo"); 102 103 String firstId = UniqueGenerator.generate(firstTestCase, 0); 104 String secondId = UniqueGenerator.generate(secondTestCase, 0); 105 106 assertFalse("IDs not unique", firstId.equals(secondId)); 107 } 108 109 113 public void testThatGeneratedIdsForDifferentTestMethodsAreUnique() 114 { 115 final ServletTestCase aTestCase = new ServletTestCase("foo"); 116 117 String firstId = UniqueGenerator.generate(aTestCase, 0); 118 aTestCase.setName("bar"); 119 String secondId = UniqueGenerator.generate(aTestCase, 0); 120 121 assertFalse("IDs not unique", firstId.equals(secondId)); 122 } 123 124 } 125 | Popular Tags |