KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ra > raadmin > TestAdminPreference


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.raadmin;
15
16 import java.util.Date JavaDoc;
17 import java.util.Random JavaDoc;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.NamingException JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.apache.log4j.Logger;
25 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionHome;
26 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionRemote;
27 import org.ejbca.core.model.log.Admin;
28 import org.ejbca.core.model.ra.raadmin.AdminPreference;
29
30 /**
31  * Tests the admin preference entity bean.
32  *
33  * @version $Id: TestAdminPreference.java,v 1.5 2007/01/11 09:35:04 anatom Exp $
34  */

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

41     private IRaAdminSessionRemote cacheAdmin;
42
43     /** Handle to AdminSessionHome */
44     private static IRaAdminSessionHome cacheHome;
45
46     private static final String JavaDoc user = genRandomUserName();
47
48     /**
49      * Creates a new AdminPreference object.
50      *
51      * @param name name
52      */

53     public TestAdminPreference(String JavaDoc name) {
54         super(name);
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58         log.debug(">setUp()");
59         if (cacheAdmin == null) {
60             if (cacheHome == null) {
61                 Context JavaDoc jndiContext = getInitialContext();
62                 Object JavaDoc obj1 = jndiContext.lookup("RaAdminSession");
63                 cacheHome = (IRaAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IRaAdminSessionHome.class);
64             }
65             cacheAdmin = cacheHome.create();
66         }
67         log.debug("<setUp()");
68     }
69
70     protected void tearDown() throws Exception JavaDoc {
71     }
72
73     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
74         log.debug(">getInitialContext");
75         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
76         log.debug("<getInitialContext");
77         return ctx;
78     }
79
80     /**
81      * tests adding an administrator preference
82      *
83      * @throws Exception error
84      */

85     public void test01AddAdminPreference() throws Exception JavaDoc {
86         log.debug(">test01AddAdminPreference()");
87         Admin administrator = new Admin(Admin.TYPE_INTERNALUSER);
88         AdminPreference pref = new AdminPreference();
89         pref.setPreferedLanguage(1);
90         pref.setTheme("TEST");
91         boolean ret = this.cacheAdmin.addAdminPreference(administrator, user, pref);
92         assertTrue("Adminpref for "+user+" should not exist", ret);
93         ret = this.cacheAdmin.addAdminPreference(administrator, user, pref);
94         assertFalse("Adminpref for "+user+" should exist", ret);
95         log.debug("<test01AddAdminPreference()");
96     }
97
98     /**
99      * tests modifying an administrator preference
100      *
101      * @throws Exception error
102      */

103     public void test02ModifyAdminPreference() throws Exception JavaDoc {
104         log.debug(">test02ModifyAdminPreference()");
105         Admin administrator = new Admin(Admin.TYPE_INTERNALUSER);
106         AdminPreference pref = this.cacheAdmin.getAdminPreference(administrator, user);
107         assertTrue("Error Retreiving Administrator Preference.", pref.getPreferedLanguage() == 1);
108         assertTrue("Error Retreiving Administrator Preference.", pref.getTheme().equals("TEST"));
109         pref.setPreferedLanguage(2);
110         boolean ret = this.cacheAdmin.changeAdminPreference(administrator, user, pref);
111         assertTrue("Adminpref for "+user+" should exist", ret);
112         String JavaDoc newuser = genRandomUserName();
113         ret = this.cacheAdmin.changeAdminPreference(administrator, newuser, pref);
114         assertFalse("Adminpref for "+newuser+" should not exist", ret);
115         log.debug("<test02ModifyAdminPreference()");
116     }
117
118     private static String JavaDoc genRandomUserName() {
119         // Gen random user
120
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4711);
121         String JavaDoc username = "";
122         for (int i = 0; i < 6; i++) {
123             int randint = rand.nextInt(9);
124             username += (new Integer JavaDoc(randint)).toString();
125         }
126         //log.debug("Generated random username: username =" + username);
127
return username;
128     } // genRandomUserName
129

130 }
131
Popular Tags