1 13 14 package se.anatom.ejbca.ra; 15 16 import java.util.Calendar ; 17 import java.util.Date ; 18 import java.util.Random ; 19 20 import javax.naming.Context ; 21 import javax.naming.NamingException ; 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 38 public class TestAddLotsofUsers extends TestCase { 39 private static Logger log = Logger.getLogger(TestAddLotsofUsers.class); 40 44 private IUserAdminSessionRemote cacheAdmin; 45 46 47 private static IUserAdminSessionHome cacheHome; 48 49 private static String baseUsername; 51 private static String pwd; 52 private static int userNo = 0; 53 private static int caid; 54 55 60 public TestAddLotsofUsers(String name) { 61 super(name); 62 } 63 64 protected void setUp() throws Exception { 65 66 log.debug(">setUp()"); 67 if (cacheAdmin == null) { 70 if (cacheHome == null) { 71 Context jndiContext = getInitialContext(); 72 Object 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 cal = Calendar.getInstance(); 82 baseUsername = "lotsausers" + cal.get(Calendar.SECOND) + "-"; 83 84 85 log.debug("<setUp()"); 86 } 87 88 protected void tearDown() throws Exception { 89 } 90 91 private Context getInitialContext() throws NamingException { 92 log.debug(">getInitialContext"); 93 94 Context ctx = new javax.naming.InitialContext (); 95 log.debug("<getInitialContext"); 96 97 return ctx; 98 } 99 100 private String genUserName() throws Exception { 101 userNo++; 103 104 return baseUsername + userNo; 105 } 107 private String genRandomPwd() throws Exception { 108 Random rand = new Random (new Date ().getTime() + 4812); 110 String password = ""; 111 112 for (int i = 0; i < 8; i++) { 113 int randint = rand.nextInt(9); 114 password += (new Integer (randint)).toString(); 115 } 116 117 return password; 119 } 121 122 127 public void test01Create2000Users() throws Exception { 128 log.debug(">test01Create2000Users()"); 129 130 Admin administrator = new Admin(Admin.TYPE_INTERNALUSER); 132 133 for (int i = 0; i < 2000; i++) { 134 String username = genUserName(); 135 pwd = genRandomPwd(); 136 137 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 dn = "C=SE, O=AnaTom, CN=" + username; 147 String subjectaltname = "rfc822Name=" + username + "@foo.se"; 148 String 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 |