KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.ServerException JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Random JavaDoc;
19
20 import javax.ejb.DuplicateKeyException JavaDoc;
21 import javax.naming.Context JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.transaction.TransactionRolledbackException JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.log4j.Logger;
28 import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
29 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
30 import org.ejbca.core.model.SecConst;
31 import org.ejbca.core.model.log.Admin;
32 import org.ejbca.core.model.ra.NotFoundException;
33
34
35 /** Tests the UserData entity bean and some parts of UserAdminSession.
36  *
37  * @version $Id: TestUserAdminSession.java,v 1.4.2.1 2007/02/23 15:44:37 anatom Exp $
38  */

39 public class TestUserAdminSession extends TestCase {
40
41     private static Logger log = Logger.getLogger(TestUserAdminSession.class);
42     private static Context JavaDoc ctx;
43     private static IUserAdminSessionRemote usersession;
44     private static String JavaDoc username;
45     private static String JavaDoc pwd;
46     private static int caid;
47     private static Admin admin = null;
48
49     /**
50      * Creates a new TestUserData object.
51      *
52      * @param name DOCUMENT ME!
53      */

54     public TestUserAdminSession(String JavaDoc name) {
55         super(name);
56     }
57
58     protected void setUp() throws Exception JavaDoc {
59
60         log.debug(">setUp()");
61         ctx = getInitialContext();
62         caid = "CN=TEST".hashCode();
63         Object JavaDoc obj = ctx.lookup("UserAdminSession");
64         IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class);
65         usersession = userhome.create();
66         admin = new Admin(Admin.TYPE_INTERNALUSER);
67
68         log.debug("<setUp()");
69     }
70
71     protected void tearDown() throws Exception JavaDoc {
72     }
73
74     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
75         log.debug(">getInitialContext");
76
77         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
78         log.debug("<getInitialContext");
79
80         return ctx;
81     }
82
83     private String JavaDoc genRandomUserName() throws Exception JavaDoc {
84         // Gen random user
85
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4711);
86         String JavaDoc username = "";
87         for (int i = 0; i < 6; i++) {
88             int randint = rand.nextInt(9);
89             username += (new Integer JavaDoc(randint)).toString();
90         }
91         log.debug("Generated random username: username =" + username);
92
93         return username;
94     } // genRandomUserName
95

96     private String JavaDoc genRandomPwd() throws Exception JavaDoc {
97         // Gen random pwd
98
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4812);
99         String JavaDoc password = "";
100
101         for (int i = 0; i < 8; i++) {
102             int randint = rand.nextInt(9);
103             password += (new Integer JavaDoc(randint)).toString();
104         }
105
106         log.debug("Generated random pwd: password=" + password);
107
108         return password;
109     } // genRandomPwd
110

111
112     /**
113      * tests creation of new user and duplicate user
114      *
115      * @throws Exception error
116      */

117     public void test01AddUser() throws Exception JavaDoc {
118         log.debug(">test01AddUser()");
119
120         // Make user that we know later...
121
username = genRandomUserName();
122         pwd = genRandomPwd();
123         String JavaDoc email = username + "@anatom.se";
124         usersession.addUser(admin, username, pwd, "C=SE, O=AnaTom, CN=" + username, "rfc822name=" + email, email, false, SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, caid);
125         log.debug("created user: " + username + ", " + pwd + ", C=SE, O=AnaTom, CN=" + username);
126         // Add the same user again
127
boolean userexists = false;
128         try {
129             usersession.addUser(admin, username, pwd, "C=SE, O=AnaTom, CN=" + username, "rfc822name=" + email, email, false, SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, caid);
130         } catch (DuplicateKeyException JavaDoc e) {
131             // This is what we want
132
userexists = true;
133         } catch (TransactionRolledbackException JavaDoc e) {
134             // weblogic throws transactionrolledbackexception instead wrapping the duplicatekey ex
135
if (e.getCause() instanceof DuplicateKeyException JavaDoc) {
136                 userexists = true;
137             }
138         } catch (ServerException JavaDoc e) {
139             // glassfish throws serverexception, can you believe this?
140
userexists = true;
141         }
142         assertTrue("User already exist does not throw DuplicateKeyException", userexists);
143
144         log.debug("<test01AddUser()");
145     }
146
147     /**
148      * tests deletion of user, and user that does not exist
149      *
150      * @throws Exception error
151      */

152     public void test01DeleteUser() throws Exception JavaDoc {
153         log.debug(">test01DeleteUser()");
154
155         usersession.deleteUser(admin, username);
156         log.debug("deleted user: " + username);
157         // Delete the the same user again
158
boolean removed = false;
159         try {
160             usersession.deleteUser(admin, username);
161         } catch (NotFoundException e) {
162             removed = true;
163         }
164         assertTrue("User does not exist does not throw NotFoundException", removed);
165
166         log.debug("<test01DeleteUser()");
167     }
168 }
169
Popular Tags