1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.idgenerator;
18  
19  // Java imports
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          // All methods starting with "test" will be executed in the test suite.
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          // Add a NULL  to verify a NULL is not being generated.
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 }