KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ca > sign > TestSernoGenerator


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package se.anatom.ejbca.ca.sign;
15
16 import java.math.BigInteger JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import org.apache.log4j.Logger;
23 import org.ejbca.core.ejb.ca.sign.ISernoGenerator;
24 import org.ejbca.core.ejb.ca.sign.SernoGenerator;
25
26
27 /**
28  * Tests generation of serial numbers.
29  *
30  * @version $Id: TestSernoGenerator.java,v 1.2 2006/01/17 20:33:58 anatom Exp $
31  */

32 public class TestSernoGenerator extends TestCase {
33     private static Logger log = Logger.getLogger(TestSernoGenerator.class);
34
35     /**
36      * Creates a new TestSernoGenerator object.
37      *
38      * @param name name
39      */

40     public TestSernoGenerator(String JavaDoc name) {
41         super(name);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45     }
46
47     protected void tearDown() throws Exception JavaDoc {
48     }
49
50     /* Generates many serial numbers...
51     */

52     public void test01GenerateSernos() throws Exception JavaDoc {
53         log.debug(">test01GenerateSernos()");
54
55         ISernoGenerator gen = SernoGenerator.instance();
56         HashMap JavaDoc map = new HashMap JavaDoc(300000);
57         String JavaDoc hex = null;
58
59         for (int j = 0; j < 300; j++) {
60             for (int i = 0; i < 1000; i++) {
61                 BigInteger JavaDoc bi = gen.getSerno();
62
63                 //hex = Hex.encode(serno);
64
hex = bi.toString();
65
66                 if (map.put(hex, hex) != null) {
67                     System.out.println("Duplicate serno produced: " + hex);
68                 }
69             }
70
71             System.out.println(((j + 1) * 1000) + " sernos produced: " + hex);
72
73             long seed = Math.abs((new Date JavaDoc().getTime()) + this.hashCode());
74             gen.setSeed(seed);
75             System.out.println("Reseeding: " + seed);
76         }
77
78         System.out.println("Map now contains " + map.size() + " serial numbers.");
79         log.debug("<test01GenerateSernos()");
80     }
81 }
82
Popular Tags