1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.idgenerator;
18
19
20 import java.util.HashMap;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 /***
27 * TestIdGenerator
28 *
29 * @author <a href="paulsp@apache.org">Paul Spencer</a>
30 * @version $Id: TestIdGenerator.java 516448 2007-03-09 16:25:47Z ate $
31 */
32 public class TestIdGenerator extends TestCase
33 {
34
35 private static int ID_TEST_TRIES = 10000;
36
37
38 /***
39 * Start the tests.
40 *
41 * @param args the arguments. Not used
42 */
43 public static void main(String args[])
44 {
45 junit.awtui.TestRunner.main( new String[] { TestIdGenerator.class.getName() } );
46 }
47
48 /***
49 * Creates the test suite.
50 *
51 * @return a test suite (<code>TestSuite</code>) that includes all methods
52 * starting with "test"
53 */
54 public static Test suite()
55 {
56
57 return new TestSuite( TestIdGenerator.class );
58 }
59
60 /***
61 * Simple test that verify the PEID are unique. This test will generate
62 * <CODE>ID_TEST_TRIES<CODE> PEIDs. It will test for a NULL PEID.
63 *
64 * Granted, passing this test does <B>not</B> guarantee that a duplicate
65 * PEID will not be generated.
66 *
67 * @throws Exception
68 */
69 public void testVerifyUniquePeid() throws Exception
70 {
71 IdGenerator generator = new JetspeedIdGenerator(65536, "P-", "");
72
73 HashMap generatedIds = new HashMap( ID_TEST_TRIES + 1);
74 String newId;
75
76
77 generatedIds.put(null, null);
78
79 for (int counter = 1; counter <= ID_TEST_TRIES; counter++)
80 {
81 newId = generator.getNextPeid();
82 assertTrue( "PEID already generated. PEID = " + newId, !generatedIds.containsKey(newId));
83 generatedIds.put(newId, null);
84 }
85 }
86
87 /***
88 * Simple test that verify the PEIDs are increasing. Although this is not a
89 * requirement of the IdGenerator, it is recommended
90 *
91 * @throws Exception
92 */
93 public void testVerifyIncreasingPeid() throws Exception
94 {
95 IdGenerator generator = new JetspeedIdGenerator(65536, "P-", "");
96 assertNotNull("idgenerator service is null", generator);
97
98 String newId;
99 String lastId = null;
100
101 for (int counter = 1; counter <= ID_TEST_TRIES; counter++)
102 {
103 newId = generator.getNextPeid();
104 if (lastId == null)
105 {
106 lastId = newId;
107 continue;
108 }
109 assertTrue( "PEID is not greater then last generated PEID. PEID = " + newId, (lastId.compareTo(newId)<0));
110 lastId = newId;
111 }
112 }
113 }