Author: veithen
Date: Sun May 23 21:50:17 2010
New Revision: 947500

URL: http://svn.apache.org/viewvc?rev=947500&view=rev
Log:
Converted the test case provided in WSCOMMONS-201 by Chathura Ekanayake into a 
unit test for the new UIDGenerator.

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/UIDGeneratorTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/UIDGeneratorTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/UIDGeneratorTest.java?rev=947500&r1=947499&r2=947500&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/UIDGeneratorTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/UIDGeneratorTest.java
 Sun May 23 21:50:17 2010
@@ -19,8 +19,10 @@
 
 package org.apache.axiom.util;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
@@ -42,4 +44,34 @@ public class UIDGeneratorTest extends Te
     public void testGenerateMimeBoundaryLength() {
         assertTrue(UIDGenerator.generateMimeBoundary().length() <= 70);
     }
+    
+    public void testThreadSafety() {
+        final Set generatedIds = Collections.synchronizedSet(new HashSet());
+        final AtomicInteger errorCount = new AtomicInteger(0);
+        Thread[] threads = new Thread[100];
+        for (int i = 0; i < threads.length; i++) {
+            threads[i] = new Thread(new Runnable() {
+                public void run() {
+                    for (int i=0; i<1000; i++) {
+                        String id = UIDGenerator.generateUID();
+                        if (!generatedIds.add(id)) {
+                            System.out.println("ERROR - Same UID has been 
generated before. UID: " + id);
+                            errorCount.incrementAndGet();
+                        }
+                    }
+                }
+            });
+            threads[i].start();
+        }
+
+        for (int i = 0; i < threads.length; i++) {
+            try {
+                threads[i].join();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+        
+        assertEquals(0, errorCount.get());
+    }
 }


Reply via email to