1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jetspeed.services.idgenerator;
19
20
21 import java.util.HashMap;
22
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27
28 import org.apache.jetspeed.test.JetspeedTestCase;
29 import org.apache.jetspeed.services.idgenerator.JetspeedIdGenerator;
30
31 import org.apache.turbine.util.TurbineConfig;
32 import org.apache.turbine.util.StringUtils;
33
34 /***
35 * TestIdGenerator
36 *
37 * @author <a href="paulsp@apache.org">Paul Spencer</a>
38 * @version $Id: TestIdGenerator.java,v 1.1 2004/04/07 22:02:42 jford Exp $
39 */
40 public class TestIdGenerator extends JetspeedTestCase {
41
42 private static int ID_TEST_TRIES = 10000;
43
44 /***
45 * Configuration object to run Turbine outside a servlet container
46 * ( uses turbine.properties )
47 */
48 private static TurbineConfig config = null;
49
50 /***
51 * Sets up TurbineConfig using the system property:
52 * <pre>turbine.properties</pre>
53 */
54 static
55 {
56 try
57 {
58 config = new TurbineConfig( "webapp",
59 "/WEB-INF/conf/TurbineResources.properties");
60 config.init();
61 }
62 catch (Exception e)
63 {
64 fail(StringUtils.stackTrace(e));
65 }
66 }
67
68 /***
69 * Defines the testcase name for JUnit.
70 *
71 * @param name the testcase's name.
72 */
73 public TestIdGenerator(String name) {
74 super( name );
75 }
76
77 /***
78 * Start the tests.
79 *
80 * @param args the arguments. Not used
81 */
82 public static void main(String args[]) {
83 junit.awtui.TestRunner.main( new String[] { TestIdGenerator.class.getName() } );
84 }
85
86 public void setup() {
87 }
88 /***
89 * Creates the test suite.
90 *
91 * @return a test suite (<code>TestSuite</code>) that includes all methods
92 * starting with "test"
93 */
94 public static Test suite() {
95
96 return new TestSuite( TestIdGenerator.class );
97 }
98
99 /***
100 * Simple test that verify the PEID are unique. This test will generate
101 * <CODE>ID_TEST_TRIES<CODE> PEIDs. It will test for a NULL PEID.
102 *
103 * Granted, passing this test does <B>not</B> guarantee that a duplicate
104 * PEID will not be generated.
105 *
106 * @throws Exception
107 */
108 public void testVerifyUniquePeid() throws Exception
109 {
110 HashMap generatedIds = new HashMap( ID_TEST_TRIES + 1);
111 String newId;
112
113
114 generatedIds.put(null, null);
115
116 for (int counter = 1; counter <= ID_TEST_TRIES; counter++)
117 {
118 newId = JetspeedIdGenerator.getNextPeid();
119 assertTrue( "PEID already generated. PEID = " + newId, !generatedIds.containsKey(newId));
120 generatedIds.put(newId, null);
121 }
122 }
123
124 /***
125 * Simple test that verify the PEIDs are increasing. Although this is not a
126 * requirement of the IdGenerator, it is recommended
127 *
128 * @throws Exception
129 */
130 public void testVerifyIncreasingPeid() throws Exception
131 {
132 String newId;
133 String lastId = null;
134
135 for (int counter = 1; counter <= ID_TEST_TRIES; counter++)
136 {
137 newId = JetspeedIdGenerator.getNextPeid();
138 if (lastId == null)
139 {
140 lastId = newId;
141 continue;
142 }
143 assertTrue( "PEID is not greater then last generated PEID. PEID = " + newId, (lastId.compareTo(newId)<0));
144 lastId = newId;
145 }
146 }
147 }