1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.AbstractMuleTestCase;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22
23 public class UUIDTestCase extends AbstractMuleTestCase
24 {
25
26 public void testGenerateUniqueAndIncrementalIds() throws Exception
27 {
28 final Set<String> ids = new HashSet<String>();
29 final List<Object[]> idsWithIndexes = new ArrayList<Object[]>(1000);
30 final int numberOfIdsToGenerate = 10000;
31 for (int index = 0; index < numberOfIdsToGenerate; index++)
32 {
33 String generatedId = UUID.getUUID();
34 idsWithIndexes.add(new Object[]{generatedId, Integer.valueOf(index)});
35 if (ids.contains(generatedId))
36 {
37 fail("REPEATED ID :" + index + ": " + generatedId);
38 }
39 else
40 {
41 ids.add(generatedId);
42 }
43 }
44 final Comparator<Object[]> comparatorById = new Comparator<Object[]>()
45 {
46 public int compare(Object[] o1, Object[] o2)
47 {
48 return ((String) o1[0]).compareTo((String) o2[0]);
49 }
50 };
51 Collections.sort(idsWithIndexes, comparatorById);
52 for (int index = 0; index < numberOfIdsToGenerate; index++)
53 {
54 assertEquals(Integer.valueOf(index), idsWithIndexes.get(index)[1]);
55 }
56 }
57
58 }
59
60