001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package se.anatom.ejbca.ra.userdatasource;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.Iterator;
017:
018: import javax.naming.Context;
019: import javax.naming.NamingException;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.log4j.Logger;
024: import org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionHome;
025: import org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionRemote;
026: import org.ejbca.core.model.log.Admin;
027: import org.ejbca.core.model.ra.userdatasource.BaseUserDataSource;
028: import org.ejbca.core.model.ra.userdatasource.CustomUserDataSourceContainer;
029: import org.ejbca.core.model.ra.userdatasource.UserDataSourceExistsException;
030: import org.ejbca.core.model.ra.userdatasource.UserDataSourceVO;
031: import org.ejbca.util.CertTools;
032:
033: /**
034: * Tests User Data Sources.
035: *
036: * @version $Id: TestUserDataSource.java,v 1.1 2006/07/20 17:50:18 herrvendil Exp $
037: */
038: public class TestUserDataSource extends TestCase {
039:
040: private static Logger log = Logger
041: .getLogger(TestUserDataSource.class);
042: private static Context ctx;
043: private static IUserDataSourceSessionRemote pub;
044:
045: private static final Admin admin = new Admin(
046: Admin.TYPE_INTERNALUSER);
047:
048: /**
049: * Creates a new TestUserDataSource object.
050: *
051: * @param name name
052: */
053: public TestUserDataSource(String name) {
054: super (name);
055: }
056:
057: protected void setUp() throws Exception {
058: log.debug(">setUp()");
059: ctx = getInitialContext();
060:
061: Object obj = ctx.lookup("UserDataSourceSession");
062: IUserDataSourceSessionHome home = (IUserDataSourceSessionHome) javax.rmi.PortableRemoteObject
063: .narrow(obj, IUserDataSourceSessionHome.class);
064: pub = home.create();
065:
066: CertTools.installBCProvider();
067:
068: log.debug("<setUp()");
069:
070: }
071:
072: protected void tearDown() throws Exception {
073: }
074:
075: private Context getInitialContext() throws NamingException {
076: log.debug(">getInitialContext");
077: Context ctx = new javax.naming.InitialContext();
078: log.debug("<getInitialContext");
079:
080: return ctx;
081: }
082:
083: /**
084: * adds custom userdatasource
085: *
086: * @throws Exception error
087: */
088: public void test01AddCustomUserDataSource() throws Exception {
089: log.debug(">test01AddCustomUserDataSource()");
090: boolean ret = false;
091: try {
092: CustomUserDataSourceContainer userdatasource = new CustomUserDataSourceContainer();
093: userdatasource
094: .setClassPath("org.ejbca.core.model.ra.userdatasource.DummyCustomUserDataSource");
095: userdatasource
096: .setDescription("Used in Junit Test, Remove this one");
097: pub.addUserDataSource(admin, "TESTDUMMYCUSTOM",
098: userdatasource);
099: ret = true;
100: } catch (UserDataSourceExistsException pee) {
101: }
102:
103: assertTrue("Creating Custom UserDataSource failed", ret);
104:
105: log.debug("<test01AddCustomUserDataSource()");
106: }
107:
108: /**
109: * renames userdatasource
110: *
111: * @throws Exception error
112: */
113: public void test02RenameUserDataSource() throws Exception {
114: log.debug(">test02RenameUserDataSource()");
115:
116: boolean ret = false;
117: try {
118: pub.renameUserDataSource(admin, "TESTDUMMYCUSTOM",
119: "TESTNEWDUMMYCUSTOM");
120: ret = true;
121: } catch (UserDataSourceExistsException pee) {
122: }
123: assertTrue("Renaming Custom UserDataSource failed", ret);
124:
125: log.debug("<test02RenameUserDataSource()");
126: }
127:
128: /**
129: * clones userdatasource
130: *
131: * @throws Exception error
132: */
133: public void test03CloneUserDataSource() throws Exception {
134: log.debug(">test03CloneUserDataSource()");
135:
136: boolean ret = false;
137: pub.cloneUserDataSource(admin, "TESTNEWDUMMYCUSTOM",
138: "TESTCLONEDUMMYCUSTOM");
139: ret = true;
140: assertTrue("Cloning Custom UserDataSource failed", ret);
141:
142: log.debug("<test03CloneUserDataSource()");
143: }
144:
145: /**
146: * edits userdatasource
147: *
148: * @throws Exception error
149: */
150: public void test04EditUserDataSource() throws Exception {
151: log.debug(">test04EditUserDataSource()");
152:
153: boolean ret = false;
154:
155: BaseUserDataSource userdatasource = pub.getUserDataSource(
156: admin, "TESTCLONEDUMMYCUSTOM");
157: userdatasource.setDescription(userdatasource.getDescription()
158: .toUpperCase());
159: pub.changeUserDataSource(admin, "TESTCLONEDUMMYCUSTOM",
160: userdatasource);
161: ret = true;
162:
163: assertTrue("Editing Custom UserDataSource failed", ret);
164:
165: log.debug("<test04EditUserDataSource()");
166: }
167:
168: /**
169: * Tries to retrieve userdata from dummy user data source
170: *
171: * @throws Exception error
172: */
173: public void test05FetchFromDummy() throws Exception {
174: log.debug(">test05FetchFromDummy()");
175:
176: ArrayList userdatasources = new ArrayList();
177: userdatasources.add(new Integer(pub.getUserDataSourceId(admin,
178: "TESTNEWDUMMYCUSTOM")));
179:
180: Collection ret = pub.fetch(admin, userdatasources, "per");
181: assertTrue("Fetching data from dummy userdatasource failed",
182: ret.size() == 1);
183:
184: Iterator iter = ret.iterator();
185: UserDataSourceVO next = (UserDataSourceVO) iter.next();
186: assertTrue("Didn't get epected user data", next.getUserDataVO()
187: .getUsername().equals("PER"));
188:
189: log.debug("<test05FetchFromDummy()");
190: }
191:
192: /**
193: * removes all userdatasources
194: *
195: * @throws Exception error
196: */
197: public void test06removeUserDataSources() throws Exception {
198: log.debug(">test06removeUserDataSources()");
199: boolean ret = false;
200: try {
201: pub.removeUserDataSource(admin, "TESTNEWDUMMYCUSTOM");
202: pub.removeUserDataSource(admin, "TESTCLONEDUMMYCUSTOM");
203: ret = true;
204: } catch (Exception pee) {
205: }
206: assertTrue("Removing UserDataSource failed", ret);
207:
208: log.debug("<test06removeUserDataSources()");
209: }
210: }
|