View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import java.util.ArrayList;
12  import java.util.Collections;
13  import java.util.Comparator;
14  import java.util.HashSet;
15  import java.util.List;
16  import java.util.Set;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  
24  public class UUIDTestCase extends AbstractMuleTestCase
25  {
26  
27      @Test
28      public void testGenerateUniqueAndIncrementalIds() throws Exception
29      {
30          final Set<String> ids = new HashSet<String>();
31          final List<Object[]> idsWithIndexes = new ArrayList<Object[]>(1000);
32          final int numberOfIdsToGenerate = 10000;
33          for (int index = 0; index < numberOfIdsToGenerate; index++)
34          {
35              String generatedId = UUID.getUUID();
36              idsWithIndexes.add(new Object[]{generatedId, Integer.valueOf(index)});
37              if (ids.contains(generatedId))
38              {
39                  fail("REPEATED ID :" + index + ": " + generatedId);
40              }
41              else
42              {
43                  ids.add(generatedId);
44              }
45          }
46          final Comparator<Object[]> comparatorById = new Comparator<Object[]>()
47          {
48              public int compare(Object[] o1, Object[] o2)
49              {
50                  return ((String) o1[0]).compareTo((String) o2[0]);
51              }
52          };
53          Collections.sort(idsWithIndexes, comparatorById);
54          for (int index = 0; index < numberOfIdsToGenerate; index++)
55          {
56              assertEquals(Integer.valueOf(index), idsWithIndexes.get(index)[1]);
57          }
58      }
59  
60  }
61  
62