1
2
3
4
5
6
7 package org.codehaus.activemq.util;
8
9 import junit.framework.TestCase;
10
11 import java.util.Hashtable;
12
13 /***
14 * To change the template for this generated type comment go to
15 * Window - Preferences - Java - Code Generation - Code and Comments
16 */
17 public class IdGeneratorTest extends TestCase {
18
19 public static void main(String[] args) {
20 junit.textui.TestRunner.run(IdGeneratorTest.class);
21 }
22
23
24
25
26 protected void setUp() throws Exception {
27 super.setUp();
28 }
29
30
31
32
33 protected void tearDown() throws Exception {
34 super.tearDown();
35 }
36
37 /***
38 * Constructor for IdGeneratorTest.
39 *
40 * @param arg0
41 */
42 public IdGeneratorTest(String arg0) {
43 super(arg0);
44 }
45
46 /***
47 * This is not a good test!
48 */
49
50 public void testGenerateId() {
51 IdGenerator idGen = new IdGenerator();
52 int count = 100000;
53 Hashtable table = new Hashtable();
54 String str = "";
55 for (int i = 0; i < count; i++) {
56 str = idGen.generateId();
57 assertTrue(str != null);
58 assertTrue(table.get(str) == null);
59 table.put(str, str);
60 }
61 }
62
63 public void testGetSeedFromId() {
64 IdGenerator idGen = new IdGenerator();
65 String seed = idGen.getSeed();
66 String id = idGen.generateId();
67 String generatorId = IdGenerator.getSeedFromId(id);
68 for (int i = 0; i < 1000; i++) {
69 assertTrue(generatorId.equals(seed));
70 }
71 }
72
73 public void testGetCountFromId() {
74 IdGenerator idGen = new IdGenerator();
75 String seed = idGen.getSeed();
76 for (int i = 0; i < 100; i++) {
77 String id = idGen.generateId();
78 long count = IdGenerator.getCountFromId(id);
79 assertTrue(count == i);
80 }
81 }
82
83 public void testCompare() {
84 IdGenerator idGen = new IdGenerator();
85 String seed = idGen.getSeed();
86 String lastId = null;
87 for (int i = 0; i < 100; i++) {
88 String id = idGen.generateId();
89 if (lastId != null) {
90 assertTrue(IdGenerator.compare(lastId, id) == -1);
91 }
92 lastId = id;
93 }
94 }
95
96 }