View Javadoc

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