KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > AbstractCoreTest


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.Name JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.directory.Attributes JavaDoc;
36 import javax.naming.ldap.InitialLdapContext JavaDoc;
37 import javax.naming.ldap.LdapContext JavaDoc;
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.ArrayList JavaDoc;
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: 169198 $
50  */

51 public abstract class AbstractCoreTest extends TestCase
52 {
53     public static final String JavaDoc 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 JavaDoc 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 JavaDoc extras = new Hashtable JavaDoc();
79
80     /** extra environment parameters that can be added before setUp to override values */
81     protected Hashtable JavaDoc overrides = new Hashtable JavaDoc();
82
83
84     private ArrayList JavaDoc list = null;
85
86
87     public AbstractCoreTest()
88     {
89         list = new ArrayList JavaDoc();
90
91         Attributes JavaDoc attributes = new LockableAttributesImpl();
92
93         LdifParserImpl parser = new LdifParserImpl();
94
95         try
96         {
97             parser.parse( attributes, LDIF );
98         }
99         catch ( NamingException JavaDoc 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 JavaDoc
117     {
118         super.setUp();
119
120         extras.put( EnvKeys.TEST_ENTRIES, list );
121         
122         if ( overrides.containsKey( EnvKeys.WKDIR ) )
123         {
124             doDelete( new File JavaDoc( ( String JavaDoc ) overrides.get( EnvKeys.WKDIR ) ) );
125         }
126         else
127         {
128             doDelete( new File JavaDoc( "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 JavaDoc wkdir ) throws IOException JavaDoc
139     {
140         if ( doDelete )
141         {
142             if ( wkdir.exists() )
143             {
144                 FileUtils.deleteDirectory( wkdir );
145             }
146             if ( wkdir.exists() )
147             {
148                 throw new IOException JavaDoc( "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 JavaDoc setSysRoot( String JavaDoc user, String JavaDoc passwd ) throws NamingException JavaDoc
165     {
166         Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc setSysRoot( Hashtable JavaDoc env ) throws NamingException JavaDoc
186     {
187         Hashtable JavaDoc envFinal = new Hashtable JavaDoc();
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 JavaDoc( 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 JavaDoc
212     {
213         super.tearDown();
214
215         Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc( env ); } catch( Exception JavaDoc 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 JavaDoc in ) throws NamingException JavaDoc
245     {
246         Hashtable JavaDoc env = new Hashtable JavaDoc();
247
248         env.putAll( sysRoot.getEnvironment() );
249
250         LdapContext JavaDoc ctx = new InitialLdapContext JavaDoc( 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 JavaDoc attributes = new LockableAttributesImpl();
261
262                 String JavaDoc ldif = ( String JavaDoc ) iterator.next();
263
264                 parser.parse( attributes, ldif );
265
266                 Name dn = new LdapName( ( String JavaDoc ) attributes.remove( "dn" ).get() );
267
268                 dn.remove( 0 );
269
270                 ctx.createSubcontext( dn, attributes );
271             }
272         }
273         catch ( Exception JavaDoc e )
274         {
275             String JavaDoc msg = "failed while trying to parse system ldif file";
276
277             NamingException JavaDoc ne = new LdapConfigurationException( msg );
278
279             ne.setRootCause( e );
280
281             throw ne;
282         }
283     }
284 }
285
Popular Tags