1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server;
18
19
20 import junit.framework.TestCase;
21 import org.apache.commons.io.FileUtils;
22 import org.apache.commons.lang.exception.NestableRuntimeException;
23 import org.apache.ldap.common.exception.LdapConfigurationException;
24 import org.apache.ldap.common.ldif.LdifIterator;
25 import org.apache.ldap.common.ldif.LdifParser;
26 import org.apache.ldap.common.ldif.LdifParserImpl;
27 import org.apache.ldap.common.message.LockableAttributesImpl;
28 import org.apache.ldap.common.name.LdapName;
29 import org.apache.ldap.server.jndi.EnvKeys;
30
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.Name;
34 import javax.naming.NamingException;
35 import javax.naming.directory.Attributes;
36 import javax.naming.ldap.InitialLdapContext;
37 import javax.naming.ldap.LdapContext;
38 import java.io.File;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.util.Hashtable;
42 import java.util.ArrayList;
43
44
45 /***
46 * A simple testcase for testing JNDI provider functionality.
47 *
48 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49 * @version $Rev: 169088 $
50 */
51 public abstract class AbstractCoreTest extends TestCase
52 {
53 public static final String LDIF = "dn: uid=akarasulu,ou=users,ou=system\n" +
54 "cn: Alex Karasulu\n" +
55 "sn: Karasulu\n" +
56 "givenname: Alex\n" +
57 "objectclass: top\n" +
58 "objectclass: person\n" +
59 "objectclass: organizationalPerson\n" +
60 "objectclass: inetOrgPerson\n" +
61 "ou: Engineering\n" +
62 "ou: People\n" +
63 "l: Bogusville\n" +
64 "uid: akarasulu\n" +
65 "mail: akarasulu@apache.org\n" +
66 "telephonenumber: +1 408 555 4798\n" +
67 "facsimiletelephonenumber: +1 408 555 9751\n" +
68 "roomnumber: 4612\n" +
69 "userpassword: test\n";
70
71 /*** the context root for the system partition */
72 protected LdapContext sysRoot;
73
74 /*** flag whether to delete database files for each test or not */
75 protected boolean doDelete = true;
76
77 /*** extra environment parameters that can be added before setUp */
78 protected Hashtable extras = new Hashtable();
79
80 /*** extra environment parameters that can be added before setUp to override values */
81 protected Hashtable overrides = new Hashtable();
82
83
84 private ArrayList list = null;
85
86
87 public AbstractCoreTest()
88 {
89 list = new ArrayList();
90
91 Attributes attributes = new LockableAttributesImpl();
92
93 LdifParserImpl parser = new LdifParserImpl();
94
95 try
96 {
97 parser.parse( attributes, LDIF );
98 }
99 catch ( NamingException e )
100 {
101 e.printStackTrace();
102
103 throw new NestableRuntimeException( e );
104 }
105
106 list.add( attributes );
107 }
108
109
110 /***
111 * Get's the initial context factory for the provider's ou=system context
112 * root.
113 *
114 * @see junit.framework.TestCase#setUp()
115 */
116 protected void setUp() throws Exception
117 {
118 super.setUp();
119
120 extras.put( EnvKeys.TEST_ENTRIES, list );
121
122 if ( overrides.containsKey( EnvKeys.WKDIR ) )
123 {
124 doDelete( new File( ( String ) overrides.get( EnvKeys.WKDIR ) ) );
125 }
126 else
127 {
128 doDelete( new File( "target" + File.separator + "apacheds" ) );
129 }
130
131 setSysRoot( "uid=admin,ou=system", "secret" );
132 }
133
134
135 /***
136 * Deletes the Eve working directory.
137 */
138 protected void doDelete( File wkdir ) throws IOException
139 {
140 if ( doDelete )
141 {
142 if ( wkdir.exists() )
143 {
144 FileUtils.deleteDirectory( wkdir );
145 }
146 if ( wkdir.exists() )
147 {
148 throw new IOException( "Failed to delete: " + wkdir );
149 }
150 }
151 }
152
153
154 /***
155 * Sets and returns the system root. Values of user and password used to
156 * set the respective JNDI properties. These values can be overriden by the
157 * overrides properties.
158 *
159 * @param user the username for authenticating as this user
160 * @param passwd the password of the user
161 * @return the sysRoot context which is also set
162 * @throws NamingException if there is a failure of any kind
163 */
164 protected LdapContext setSysRoot( String user, String passwd ) throws NamingException
165 {
166 Hashtable env = new Hashtable();
167
168 env.put( Context.SECURITY_PRINCIPAL, user );
169
170 env.put( Context.SECURITY_CREDENTIALS, passwd );
171
172 return setSysRoot( env );
173 }
174
175
176 /***
177 * Sets the system root taking into account the extras and overrides
178 * properties. In between these it sets the properties for the working
179 * directory, the provider URL and the JNDI InitialContexFactory to use.
180 *
181 * @param env an environment to use while setting up the system root.
182 * @return the sysRoot context which is also set
183 * @throws NamingException if there is a failure of any kind
184 */
185 protected LdapContext setSysRoot( Hashtable env ) throws NamingException
186 {
187 Hashtable envFinal = new Hashtable();
188
189 envFinal.putAll( extras );
190
191 envFinal.putAll( env );
192
193 envFinal.put( Context.PROVIDER_URL, "ou=system" );
194
195 envFinal.put( EnvKeys.WKDIR, "target" + File.separator + "apacheds" );
196
197 envFinal.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
198
199 envFinal.putAll( overrides );
200
201 return sysRoot = new InitialLdapContext( envFinal, null );
202 }
203
204
205
206 /***
207 * Sets the system context root to null.
208 *
209 * @see junit.framework.TestCase#tearDown()
210 */
211 protected void tearDown() throws Exception
212 {
213 super.tearDown();
214
215 Hashtable env = new Hashtable();
216
217 env.put( Context.PROVIDER_URL, "ou=system" );
218
219 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
220
221 env.put( EnvKeys.SHUTDOWN, "" );
222
223 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
224
225 env.put( Context.SECURITY_CREDENTIALS, "secret" );
226
227 try { new InitialContext( env ); } catch( Exception e ) {}
228
229 sysRoot = null;
230
231 Runtime.getRuntime().gc();
232 }
233
234
235 /***
236 * Imports the LDIF entries packaged with the Eve JNDI provider jar into
237 * the newly created system partition to prime it up for operation. Note
238 * that only ou=system entries will be added - entries for other partitions
239 * cannot be imported and will blow chunks.
240 *
241 * @throws NamingException if there are problems reading the ldif file and
242 * adding those entries to the system partition
243 */
244 protected void importLdif( InputStream in ) throws NamingException
245 {
246 Hashtable env = new Hashtable();
247
248 env.putAll( sysRoot.getEnvironment() );
249
250 LdapContext ctx = new InitialLdapContext( env, null );
251
252 LdifParser parser = new LdifParserImpl();
253
254 try
255 {
256 LdifIterator iterator = new LdifIterator( in );
257
258 while ( iterator.hasNext() )
259 {
260 Attributes attributes = new LockableAttributesImpl();
261
262 String ldif = ( String ) iterator.next();
263
264 parser.parse( attributes, ldif );
265
266 Name dn = new LdapName( ( String ) attributes.remove( "dn" ).get() );
267
268 dn.remove( 0 );
269
270 ctx.createSubcontext( dn, attributes );
271 }
272 }
273 catch ( Exception e )
274 {
275 String msg = "failed while trying to parse system ldif file";
276
277 NamingException ne = new LdapConfigurationException( msg );
278
279 ne.setRootCause( e );
280
281 throw ne;
282 }
283 }
284 }