View Javadoc

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 java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.ArrayList;
26  import java.util.Hashtable;
27  import java.util.List;
28  
29  import javax.naming.Context;
30  import javax.naming.InitialContext;
31  import javax.naming.NamingException;
32  import javax.naming.directory.Attributes;
33  import javax.naming.ldap.InitialLdapContext;
34  import javax.naming.ldap.LdapContext;
35  
36  import junit.framework.TestCase;
37  
38  import org.apache.commons.io.FileUtils;
39  import org.apache.commons.lang.exception.NestableRuntimeException;
40  import org.apache.ldap.common.ldif.LdifIterator;
41  import org.apache.ldap.common.ldif.LdifParserImpl;
42  import org.apache.ldap.common.message.LockableAttributesImpl;
43  import org.apache.ldap.server.configuration.Configuration;
44  import org.apache.ldap.server.configuration.MutableStartupConfiguration;
45  import org.apache.ldap.server.configuration.ShutdownConfiguration;
46  
47                                                                                                              
48  /***
49   * A simple testcase for testing JNDI provider functionality.
50   *
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   * @version $Rev: 264732 $
53   */
54  public abstract class AbstractTestCase extends TestCase
55  {
56      public static final String LDIF = "dn: uid=akarasulu,ou=users,ou=system\n" +
57              "cn: Alex Karasulu\n" +
58              "sn: Karasulu\n" +
59              "givenname: Alex\n" +
60              "objectclass: top\n" +
61              "objectclass: person\n" +
62              "objectclass: organizationalPerson\n" +
63              "objectclass: inetOrgPerson\n" +
64              "ou: Engineering\n" +
65              "ou: People\n" +
66              "l: Bogusville\n" +
67              "uid: akarasulu\n" +
68              "mail: akarasulu@apache.org\n" +
69              "telephonenumber: +1 408 555 4798\n" +
70              "facsimiletelephonenumber: +1 408 555 9751\n" +
71              "roomnumber: 4612\n" +
72              "userpassword: test\n";
73      
74      private final String username;
75      
76      private final String password;
77  
78      /*** the context root for the system partition */
79      protected LdapContext sysRoot;
80      
81      /*** flag whether to delete database files for each test or not */
82      protected boolean doDelete = true;
83      
84      protected MutableStartupConfiguration configuration = new MutableStartupConfiguration();
85  
86      /*** A testEntries of entries as Attributes to add to the DIT for testing */
87      protected List testEntries = new ArrayList();
88  
89      /*** An optional LDIF file path if set and present is read to add more test entries */
90      private String ldifPath;
91  
92      /*** Load resources relative to this class */
93      private Class loadClass;
94  
95      private Hashtable overrides = new Hashtable(); 
96  
97      protected AbstractTestCase( String username, String password )
98      {
99          if( username == null || password == null )
100         {
101             throw new NullPointerException();
102         }
103 
104         this.username = username;
105         this.password = password;
106     }
107 
108     /***
109      * Sets the LDIF path as a relative resource path to use with the
110      * loadClass parameter to load the resource.
111      *
112      * @param ldifPath the relative resource path to the LDIF file
113      * @param loadClass the class used to load the LDIF as a resource stream
114      */
115     protected void setLdifPath( String ldifPath, Class loadClass )
116     {
117         this.loadClass = loadClass;
118 
119         this.ldifPath = ldifPath;
120     }
121 
122 
123     /***
124      * Sets the LDIF path to use.  If the path is relative to this class then it
125      * is first tested
126      *
127      * @param ldifPath the path to the LDIF file
128      */
129     protected void setLdifPath( String ldifPath )
130     {
131         this.ldifPath = ldifPath;
132     }
133 
134 
135     /***
136      * Get's the initial context factory for the provider's ou=system context
137      * root.
138      *
139      * @see junit.framework.TestCase#setUp()
140      */
141     protected void setUp() throws Exception
142     {
143         super.setUp();
144 
145         // -------------------------------------------------------------------
146         // Add a single test entry
147         // -------------------------------------------------------------------
148 
149         Attributes attributes = new LockableAttributesImpl();
150 
151         LdifParserImpl parser = new LdifParserImpl();
152 
153         try
154         {
155             parser.parse( attributes, LDIF );
156         }
157         catch ( NamingException e )
158         {
159             e.printStackTrace();
160 
161             throw new NestableRuntimeException( e );
162         }
163 
164         testEntries.add( attributes );
165 
166         // -------------------------------------------------------------------
167         // Add more from an optional LDIF file if they exist
168         // -------------------------------------------------------------------
169 
170         InputStream in = null;
171 
172         if ( loadClass == null && ldifPath != null )
173         {
174             File ldifFile = new File( ldifPath );
175 
176             if ( ldifFile.exists() )
177             {
178                 in = new FileInputStream( ldifPath );
179             }
180             else
181             {
182                 in = getClass().getResourceAsStream( ldifPath );
183             }
184 
185             throw new FileNotFoundException( ldifPath );
186         }
187         else if ( loadClass != null && ldifPath != null )
188         {
189             in = loadClass.getResourceAsStream( ldifPath );
190         }
191 
192         if ( in != null )
193         {
194             LdifIterator list = new LdifIterator( in );
195 
196             while ( list.hasNext() )
197             {
198                 String ldif = ( String ) list.next();
199 
200                 attributes = new LockableAttributesImpl();
201 
202                 parser.parse( attributes, ldif );
203 
204                 testEntries.add( attributes );
205             }
206         }
207 
208         // -------------------------------------------------------------------
209         // Add key for extra entries to the testEntries of extras
210         // -------------------------------------------------------------------
211 
212         configuration.setTestEntries( testEntries );
213         doDelete( configuration.getWorkingDirectory() );
214         setSysRoot( username, password, configuration );
215     }
216 
217 
218     /***
219      * Deletes the Eve working directory.
220      */
221     protected void doDelete( File wkdir ) throws IOException
222     {
223         if ( doDelete )
224         {
225             if ( wkdir.exists() )
226             {
227                 FileUtils.deleteDirectory( wkdir );
228             }
229             if ( wkdir.exists() )
230             {
231                 throw new IOException( "Failed to delete: " + wkdir );
232             }
233         }
234     }
235 
236 
237     /***
238      * Sets and returns the system root.  Values of user and password used to
239      * set the respective JNDI properties.  These values can be overriden by the
240      * overrides properties.
241      *
242      * @param user the username for authenticating as this user
243      * @param passwd the password of the user
244      * @return the sysRoot context which is also set
245      * @throws NamingException if there is a failure of any kind
246      */
247     protected LdapContext setSysRoot( String user, String passwd, Configuration cfg ) throws NamingException
248     {
249         Hashtable env = new Hashtable( cfg.toJndiEnvironment() );
250         env.put( Context.SECURITY_PRINCIPAL, user );
251         env.put( Context.SECURITY_CREDENTIALS, passwd );
252         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
253         return setSysRoot( env );
254     }
255 
256 
257     /***
258      * Sets the system root taking into account the extras and overrides
259      * properties.  In between these it sets the properties for the working
260      * directory, the provider URL and the JNDI InitialContexFactory to use.
261      *
262      * @param env an environment to use while setting up the system root.
263      * @return the sysRoot context which is also set
264      * @throws NamingException if there is a failure of any kind
265      */
266     protected LdapContext setSysRoot( Hashtable env ) throws NamingException
267     {
268         Hashtable envFinal = new Hashtable( env );
269         if ( ! envFinal.containsKey( Context.PROVIDER_URL ) )
270         {
271             envFinal.put( Context.PROVIDER_URL, "ou=system" );
272         }
273         
274         envFinal.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
275         envFinal.putAll( overrides );
276         
277         // We have to initiate the first run as an admin at least.
278         Hashtable adminEnv = new Hashtable( envFinal );
279         adminEnv.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
280         adminEnv.put( Context.SECURITY_CREDENTIALS, "secret" );
281         adminEnv.put( Context.SECURITY_AUTHENTICATION, "simple" );
282         new InitialLdapContext( adminEnv, null );
283 
284         // OK, now let's get an appropriate context.
285         return sysRoot = new InitialLdapContext( envFinal, null );
286     }
287 
288     /***
289      * Overrides default JNDI environment properties.  Please call this method
290      * to override any JNDI environment properties this test case will set.
291      */
292     protected void overrideEnvironment( String key, Object value )
293     {
294         overrides.put( key, value );
295     }
296 
297 
298     protected Hashtable getOverriddenEnvironment()
299     {
300         return ( Hashtable ) overrides.clone();
301     }
302 
303 
304     /***
305      * Sets the system context root to null.
306      *
307      * @see junit.framework.TestCase#tearDown()
308      */
309     protected void tearDown() throws Exception
310     {
311         super.tearDown();
312 
313         Hashtable env = new Hashtable();
314 
315         env.put( Context.PROVIDER_URL, "ou=system" );
316         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
317         env.putAll( new ShutdownConfiguration().toJndiEnvironment() );
318         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
319         env.put( Context.SECURITY_CREDENTIALS, "secret" );
320         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
321 
322         try { new InitialContext( env ); } catch( Exception e ) {}
323 
324         sysRoot = null;
325 
326         Runtime.getRuntime().gc();
327 
328         testEntries.clear();
329 
330         ldifPath = null;
331 
332         loadClass = null;
333         
334         overrides.clear();
335         
336         configuration = new MutableStartupConfiguration();
337         
338         doDelete( configuration.getWorkingDirectory() );
339     }
340 }