001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.idgenerator;
018:
019: // Java imports
020: import java.util.HashMap;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: /**
027: * TestIdGenerator
028: *
029: * @author <a href="paulsp@apache.org">Paul Spencer</a>
030: * @version $Id: TestIdGenerator.java 516448 2007-03-09 16:25:47Z ate $
031: */
032: public class TestIdGenerator extends TestCase {
033:
034: private static int ID_TEST_TRIES = 10000;
035:
036: /**
037: * Start the tests.
038: *
039: * @param args the arguments. Not used
040: */
041: public static void main(String args[]) {
042: junit.awtui.TestRunner
043: .main(new String[] { TestIdGenerator.class.getName() });
044: }
045:
046: /**
047: * Creates the test suite.
048: *
049: * @return a test suite (<code>TestSuite</code>) that includes all methods
050: * starting with "test"
051: */
052: public static Test suite() {
053: // All methods starting with "test" will be executed in the test suite.
054: return new TestSuite(TestIdGenerator.class);
055: }
056:
057: /**
058: * Simple test that verify the PEID are unique. This test will generate
059: * <CODE>ID_TEST_TRIES<CODE> PEIDs. It will test for a NULL PEID.
060: *
061: * Granted, passing this test does <B>not</B> guarantee that a duplicate
062: * PEID will not be generated.
063: *
064: * @throws Exception
065: */
066: public void testVerifyUniquePeid() throws Exception {
067: IdGenerator generator = new JetspeedIdGenerator(65536, "P-", "");
068:
069: HashMap generatedIds = new HashMap(ID_TEST_TRIES + 1);
070: String newId;
071:
072: // Add a NULL to verify a NULL is not being generated.
073: generatedIds.put(null, null);
074:
075: for (int counter = 1; counter <= ID_TEST_TRIES; counter++) {
076: newId = generator.getNextPeid();
077: assertTrue("PEID already generated. PEID = " + newId,
078: !generatedIds.containsKey(newId));
079: generatedIds.put(newId, null);
080: }
081: }
082:
083: /**
084: * Simple test that verify the PEIDs are increasing. Although this is not a
085: * requirement of the IdGenerator, it is recommended
086: *
087: * @throws Exception
088: */
089: public void testVerifyIncreasingPeid() throws Exception {
090: IdGenerator generator = new JetspeedIdGenerator(65536, "P-", "");
091: assertNotNull("idgenerator service is null", generator);
092:
093: String newId;
094: String lastId = null;
095:
096: for (int counter = 1; counter <= ID_TEST_TRIES; counter++) {
097: newId = generator.getNextPeid();
098: if (lastId == null) {
099: lastId = newId;
100: continue;
101: }
102: assertTrue(
103: "PEID is not greater then last generated PEID. PEID = "
104: + newId, (lastId.compareTo(newId) < 0));
105: lastId = newId;
106: }
107: }
108: }
|