This project has retired. For details please refer to its
Attic page.
TestIdGenerator xref
1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */161718packageorg.apache.jetspeed.services.idgenerator;
1920// Java imports21import java.util.HashMap;
2223// Cactus and Junit imports24import junit.framework.Test;
25import junit.framework.TestSuite;
2627// Jetspeed imports28import org.apache.jetspeed.test.JetspeedTestCase;
29import org.apache.jetspeed.services.idgenerator.JetspeedIdGenerator;
3031import org.apache.turbine.util.TurbineConfig;
32import org.apache.turbine.util.StringUtils;
3334/***35 * TestIdGenerator36 *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 */40publicclassTestIdGeneratorextendsJetspeedTestCase {
4142privatestaticint ID_TEST_TRIES = 10000;
4344/***45 * Configuration object to run Turbine outside a servlet container46 * ( uses turbine.properties )47 */48privatestatic TurbineConfig config = null;
4950/***51 * Sets up TurbineConfig using the system property:52 * <pre>turbine.properties</pre>53 */54static55 {
56try57 {
58 config = new TurbineConfig( "webapp",
59"/WEB-INF/conf/TurbineResources.properties");
60 config.init();
61 }
62catch (Exception e)
63 {
64 fail(StringUtils.stackTrace(e));
65 }
66 }
6768/***69 * Defines the testcase name for JUnit.70 *71 * @param name the testcase's name.72 */73publicTestIdGenerator(String name) {
74super( name );
75 }
7677/***78 * Start the tests.79 *80 * @param args the arguments. Not used81 */82publicstaticvoid main(String args[]) {
83 junit.awtui.TestRunner.main( new String[] { TestIdGenerator.class.getName() } );
84 }
8586publicvoid setup() {
87 }
88/***89 * Creates the test suite.90 *91 * @return a test suite (<code>TestSuite</code>) that includes all methods92 * starting with "test"93 */94publicstatic Test suite() {
95// All methods starting with "test" will be executed in the test suite.96returnnew TestSuite( TestIdGenerator.class );
97 }
9899/***100 * Simple test that verify the PEID are unique. This test will generate101 * <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 duplicate104 * PEID will not be generated.105 *106 * @throws Exception107 */108publicvoid testVerifyUniquePeid() throws Exception
109 {
110 HashMap generatedIds = new HashMap( ID_TEST_TRIES + 1);
111 String newId;
112113// Add a NULL to verify a NULL is not being generated.114 generatedIds.put(null, null);
115116for (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 }
123124/***125 * Simple test that verify the PEIDs are increasing. Although this is not a 126 * requirement of the IdGenerator, it is recommended127 *128 * @throws Exception129 */130publicvoid testVerifyIncreasingPeid() throws Exception
131 {
132 String newId;
133 String lastId = null;
134135for (int counter = 1; counter <= ID_TEST_TRIES; counter++)
136 {
137 newId = JetspeedIdGenerator.getNextPeid();
138if (lastId == null)
139 {
140 lastId = newId;
141continue;
142 }
143 assertTrue( "PEID is not greater then last generated PEID. PEID = " + newId, (lastId.compareTo(newId)<0));
144 lastId = newId;
145 }
146 }
147 }