1 13 14 package se.anatom.ejbca.ra.raadmin; 15 16 import java.util.Date ; 17 import java.util.Random ; 18 19 import javax.naming.Context ; 20 import javax.naming.NamingException ; 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 35 public class TestAdminPreference extends TestCase { 36 private static Logger log = Logger.getLogger(TestAdminPreference.class); 37 41 private IRaAdminSessionRemote cacheAdmin; 42 43 44 private static IRaAdminSessionHome cacheHome; 45 46 private static final String user = genRandomUserName(); 47 48 53 public TestAdminPreference(String name) { 54 super(name); 55 } 56 57 protected void setUp() throws Exception { 58 log.debug(">setUp()"); 59 if (cacheAdmin == null) { 60 if (cacheHome == null) { 61 Context jndiContext = getInitialContext(); 62 Object 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 { 71 } 72 73 private Context getInitialContext() throws NamingException { 74 log.debug(">getInitialContext"); 75 Context ctx = new javax.naming.InitialContext (); 76 log.debug("<getInitialContext"); 77 return ctx; 78 } 79 80 85 public void test01AddAdminPreference() throws Exception { 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 103 public void test02ModifyAdminPreference() throws Exception { 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 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 genRandomUserName() { 119 Random rand = new Random (new Date ().getTime() + 4711); 121 String username = ""; 122 for (int i = 0; i < 6; i++) { 123 int randint = rand.nextInt(9); 124 username += (new Integer (randint)).toString(); 125 } 126 return username; 128 } 130 } 131 | Popular Tags |