KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ra > TestAddLotsofUsers


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.ra;
15
16 import java.util.Calendar JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Random JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.log4j.Logger;
26 import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
27 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
28 import org.ejbca.core.model.SecConst;
29 import org.ejbca.core.model.log.Admin;
30 import org.ejbca.util.CertTools;
31
32
33 /**
34  * Tests the UserData entity bean and some parts of UserAdminSession.
35  *
36  * @version $Id: TestAddLotsofUsers.java,v 1.5 2006/12/22 09:29:59 herrvendil Exp $
37  */

38 public class TestAddLotsofUsers extends TestCase {
39     private static Logger log = Logger.getLogger(TestAddLotsofUsers.class);
40     /**
41      * UserAdminSession handle, not static since different object should go to different session
42      * beans concurrently
43      */

44     private IUserAdminSessionRemote cacheAdmin;
45
46     /** Handle to AdminSessionHome */
47     private static IUserAdminSessionHome cacheHome;
48
49     //private static UserDataHome home;
50
private static String JavaDoc baseUsername;
51     private static String JavaDoc pwd;
52     private static int userNo = 0;
53     private static int caid;
54
55     /**
56      * Creates a new TestAddLotsofUsers object.
57      *
58      * @param name name
59      */

60     public TestAddLotsofUsers(String JavaDoc name) {
61         super(name);
62     }
63
64     protected void setUp() throws Exception JavaDoc {
65
66         log.debug(">setUp()");
67         //Object obj = ctx.lookup("UserData");
68
//home = (UserDataHome) javax.rmi.PortableRemoteObject.narrow(obj, UserDataHome.class);
69
if (cacheAdmin == null) {
70             if (cacheHome == null) {
71                 Context JavaDoc jndiContext = getInitialContext();
72                 Object JavaDoc obj1 = jndiContext.lookup("UserAdminSession");
73                 cacheHome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IUserAdminSessionHome.class);
74                 caid = "CN=TEST".hashCode();
75
76             }
77
78             cacheAdmin = cacheHome.create();
79         }
80
81         Calendar JavaDoc cal = Calendar.getInstance();
82         baseUsername = "lotsausers" + cal.get(Calendar.SECOND) + "-";
83
84
85         log.debug("<setUp()");
86     }
87
88     protected void tearDown() throws Exception JavaDoc {
89     }
90
91     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
92         log.debug(">getInitialContext");
93
94         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
95         log.debug("<getInitialContext");
96
97         return ctx;
98     }
99
100     private String JavaDoc genUserName() throws Exception JavaDoc {
101         // Gen new user
102
userNo++;
103
104         return baseUsername + userNo;
105     } // genRandomUserName
106

107     private String JavaDoc genRandomPwd() throws Exception JavaDoc {
108         // Gen random pwd
109
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4812);
110         String JavaDoc password = "";
111
112         for (int i = 0; i < 8; i++) {
113             int randint = rand.nextInt(9);
114             password += (new Integer JavaDoc(randint)).toString();
115         }
116
117         //log.debug("Generated random pwd: password=" + password);
118
return password;
119     } // genRandomPwd
120

121
122     /**
123      * tests creating 2000 users
124      *
125      * @throws Exception error
126      */

127     public void test01Create2000Users() throws Exception JavaDoc {
128         log.debug(">test01Create2000Users()");
129
130         //UserDataRemote data1=null;
131
Admin administrator = new Admin(Admin.TYPE_INTERNALUSER);
132
133         for (int i = 0; i < 2000; i++) {
134             String JavaDoc username = genUserName();
135             pwd = genRandomPwd();
136
137             /*
138             data1 = home.create(username, pwd, "C=SE, O=AnaTom, CN="+username);
139             assertNotNull("Error creating", data1);
140             */

141             int type = SecConst.USER_ENDUSER;
142             int token = SecConst.TOKEN_SOFT_P12;
143             int profileid = SecConst.EMPTY_ENDENTITYPROFILE;
144             int certificatetypeid = SecConst.CERTPROFILE_FIXED_ENDUSER;
145             int hardtokenissuerid = SecConst.NO_HARDTOKENISSUER;
146             String JavaDoc dn = "C=SE, O=AnaTom, CN=" + username;
147             String JavaDoc subjectaltname = "rfc822Name=" + username + "@foo.se";
148             String JavaDoc email = username + "@foo.se";
149             if (cacheAdmin.findUser(administrator, username) != null) {
150                 System.out.println("Error : User already exists in the database.");
151             }
152             cacheAdmin.addUser(administrator, username, pwd, CertTools.stringToBCDNString(dn), subjectaltname, email, false, profileid, certificatetypeid,
153                     type, token, hardtokenissuerid, caid);
154             cacheAdmin.setClearTextPassword(administrator, username, pwd);
155             if (i % 100 == 0) {
156                 log.debug("Created " + i + " users...");
157             }
158         }
159         log.debug("Created 2000 users!");
160         log.debug("<test01Create2000Users()");
161     }
162 }
163
Popular Tags