KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ra > userdatasource > TestUserDataSource


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.userdatasource;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
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 /**
37  * Tests User Data Sources.
38  *
39  * @version $Id: TestUserDataSource.java,v 1.1 2006/07/20 17:50:18 herrvendil Exp $
40  */

41 public class TestUserDataSource extends TestCase {
42         
43     private static Logger log = Logger.getLogger(TestUserDataSource.class);
44     private static Context JavaDoc ctx;
45     private static IUserDataSourceSessionRemote pub;
46     
47     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
48     
49     /**
50      * Creates a new TestUserDataSource object.
51      *
52      * @param name name
53      */

54     public TestUserDataSource(String JavaDoc name) {
55         super(name);
56     }
57     
58     protected void setUp() throws Exception JavaDoc {
59         log.debug(">setUp()");
60         ctx = getInitialContext();
61         
62         Object JavaDoc 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 JavaDoc {
74     }
75     
76     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
77         log.debug(">getInitialContext");
78         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
79         log.debug("<getInitialContext");
80         
81         return ctx;
82     }
83     
84     
85     /**
86      * adds custom userdatasource
87      *
88      * @throws Exception error
89      */

90     public void test01AddCustomUserDataSource() throws Exception JavaDoc {
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     /**
108      * renames userdatasource
109      *
110      * @throws Exception error
111      */

112     public void test02RenameUserDataSource() throws Exception JavaDoc {
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     /**
128      * clones userdatasource
129      *
130      * @throws Exception error
131      */

132     public void test03CloneUserDataSource() throws Exception JavaDoc {
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     /**
145      * edits userdatasource
146      *
147      * @throws Exception error
148      */

149     public void test04EditUserDataSource() throws Exception JavaDoc {
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     /**
166      * Tries to retrieve userdata from dummy user data source
167      *
168      * @throws Exception error
169      */

170     public void test05FetchFromDummy() throws Exception JavaDoc {
171         log.debug(">test05FetchFromDummy()");
172         
173         ArrayList JavaDoc userdatasources = new ArrayList JavaDoc();
174         userdatasources.add(new Integer JavaDoc(pub.getUserDataSourceId(admin, "TESTNEWDUMMYCUSTOM")));
175         
176         Collection JavaDoc ret = pub.fetch(admin,userdatasources,"per");
177         assertTrue("Fetching data from dummy userdatasource failed", ret.size() ==1);
178         
179         Iterator JavaDoc 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     /**
188      * removes all userdatasources
189      *
190      * @throws Exception error
191      */

192     public void test06removeUserDataSources() throws Exception JavaDoc {
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 JavaDoc pee) {
200         }
201         assertTrue("Removing UserDataSource failed", ret);
202         
203         log.debug("<test06removeUserDataSources()");
204     }
205 }
206
Popular Tags