1 13 14 package se.anatom.ejbca.ra.userdatasource; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 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.userdatasource.IUserDataSourceSessionHome; 27 import org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionRemote; 28 import org.ejbca.core.model.log.Admin; 29 import org.ejbca.core.model.ra.userdatasource.BaseUserDataSource; 30 import org.ejbca.core.model.ra.userdatasource.CustomUserDataSourceContainer; 31 import org.ejbca.core.model.ra.userdatasource.UserDataSourceExistsException; 32 import org.ejbca.core.model.ra.userdatasource.UserDataSourceVO; 33 import org.ejbca.util.CertTools; 34 35 36 41 public class TestUserDataSource extends TestCase { 42 43 private static Logger log = Logger.getLogger(TestUserDataSource.class); 44 private static Context ctx; 45 private static IUserDataSourceSessionRemote pub; 46 47 private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER); 48 49 54 public TestUserDataSource(String name) { 55 super(name); 56 } 57 58 protected void setUp() throws Exception { 59 log.debug(">setUp()"); 60 ctx = getInitialContext(); 61 62 Object obj = ctx.lookup("UserDataSourceSession"); 63 IUserDataSourceSessionHome home = (IUserDataSourceSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, 64 IUserDataSourceSessionHome.class); 65 pub = home.create(); 66 67 CertTools.installBCProvider(); 68 69 log.debug("<setUp()"); 70 71 } 72 73 protected void tearDown() throws Exception { 74 } 75 76 private Context getInitialContext() throws NamingException { 77 log.debug(">getInitialContext"); 78 Context ctx = new javax.naming.InitialContext (); 79 log.debug("<getInitialContext"); 80 81 return ctx; 82 } 83 84 85 90 public void test01AddCustomUserDataSource() throws Exception { 91 log.debug(">test01AddCustomUserDataSource()"); 92 boolean ret = false; 93 try { 94 CustomUserDataSourceContainer userdatasource = new CustomUserDataSourceContainer(); 95 userdatasource.setClassPath("org.ejbca.core.model.ra.userdatasource.DummyCustomUserDataSource"); 96 userdatasource.setDescription("Used in Junit Test, Remove this one"); 97 pub.addUserDataSource(admin, "TESTDUMMYCUSTOM", userdatasource); 98 ret = true; 99 } catch (UserDataSourceExistsException pee) { 100 } 101 102 assertTrue("Creating Custom UserDataSource failed", ret); 103 104 log.debug("<test01AddCustomUserDataSource()"); 105 } 106 107 112 public void test02RenameUserDataSource() throws Exception { 113 log.debug(">test02RenameUserDataSource()"); 114 115 boolean ret = false; 116 try { 117 pub.renameUserDataSource(admin, "TESTDUMMYCUSTOM", "TESTNEWDUMMYCUSTOM"); 118 ret = true; 119 } catch (UserDataSourceExistsException pee) { 120 } 121 assertTrue("Renaming Custom UserDataSource failed", ret); 122 123 124 log.debug("<test02RenameUserDataSource()"); 125 } 126 127 132 public void test03CloneUserDataSource() throws Exception { 133 log.debug(">test03CloneUserDataSource()"); 134 135 boolean ret = false; 136 pub.cloneUserDataSource(admin, "TESTNEWDUMMYCUSTOM", "TESTCLONEDUMMYCUSTOM"); 137 ret = true; 138 assertTrue("Cloning Custom UserDataSource failed", ret); 139 140 log.debug("<test03CloneUserDataSource()"); 141 } 142 143 144 149 public void test04EditUserDataSource() throws Exception { 150 log.debug(">test04EditUserDataSource()"); 151 152 boolean ret = false; 153 154 BaseUserDataSource userdatasource = pub.getUserDataSource(admin, "TESTCLONEDUMMYCUSTOM"); 155 userdatasource.setDescription(userdatasource.getDescription().toUpperCase()); 156 pub.changeUserDataSource(admin, "TESTCLONEDUMMYCUSTOM", userdatasource); 157 ret = true; 158 159 assertTrue("Editing Custom UserDataSource failed", ret); 160 161 162 log.debug("<test04EditUserDataSource()"); 163 } 164 165 170 public void test05FetchFromDummy() throws Exception { 171 log.debug(">test05FetchFromDummy()"); 172 173 ArrayList userdatasources = new ArrayList (); 174 userdatasources.add(new Integer (pub.getUserDataSourceId(admin, "TESTNEWDUMMYCUSTOM"))); 175 176 Collection ret = pub.fetch(admin,userdatasources,"per"); 177 assertTrue("Fetching data from dummy userdatasource failed", ret.size() ==1); 178 179 Iterator iter = ret.iterator(); 180 UserDataSourceVO next = (UserDataSourceVO) iter.next(); 181 assertTrue("Didn't get epected user data", next.getUserDataVO().getUsername().equals("PER")); 182 183 log.debug("<test05FetchFromDummy()"); 184 } 185 186 187 192 public void test06removeUserDataSources() throws Exception { 193 log.debug(">test06removeUserDataSources()"); 194 boolean ret = false; 195 try { 196 pub.removeUserDataSource(admin, "TESTNEWDUMMYCUSTOM"); 197 pub.removeUserDataSource(admin, "TESTCLONEDUMMYCUSTOM"); 198 ret = true; 199 } catch (Exception pee) { 200 } 201 assertTrue("Removing UserDataSource failed", ret); 202 203 log.debug("<test06removeUserDataSources()"); 204 } 205 } 206 | Popular Tags |