KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > jndi > RootDSETest


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.jndi;
18
19
20 import junit.framework.TestCase;
21 import org.apache.commons.io.FileUtils;
22 import org.apache.ldap.common.exception.LdapNoPermissionException;
23 import org.apache.mina.util.AvailablePortFinder;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.naming.directory.Attributes JavaDoc;
29 import javax.naming.directory.DirContext JavaDoc;
30 import javax.naming.directory.ModificationItem JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35
36 /**
37  * Testing RootDSE lookups and context creation using the empty string.
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev: 169198 $
41  */

42 public class RootDSETest extends TestCase
43 {
44     /** flag whether to delete database files for each test or not */
45     protected boolean doDelete = true;
46
47
48     /**
49      * Get's the initial context factory for the provider's ou=system context
50      * root.
51      *
52      * @see junit.framework.TestCase#setUp()
53      */

54     protected void setUp() throws Exception JavaDoc
55     {
56         super.setUp();
57
58         doDelete( new File JavaDoc( "target" + File.separator + "eve" ) );
59     }
60
61
62     /**
63      * Deletes the Eve working directory.
64      *
65      * @throws java.io.IOException if there are failures while deleting.
66      */

67     protected void doDelete( File JavaDoc wkdir ) throws IOException JavaDoc
68     {
69         if ( doDelete )
70         {
71             if ( wkdir.exists() )
72             {
73                 FileUtils.deleteDirectory( wkdir );
74             }
75         }
76     }
77
78
79     /**
80      * Sets the system context root to null.
81      *
82      * @see junit.framework.TestCase#tearDown()
83      */

84     protected void tearDown() throws Exception JavaDoc
85     {
86         super.tearDown();
87
88         Hashtable JavaDoc env = new Hashtable JavaDoc();
89
90         env.put( Context.PROVIDER_URL, "ou=system" );
91
92         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
93
94         env.put( EnvKeys.SHUTDOWN, "" );
95
96         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
97
98         env.put( Context.SECURITY_CREDENTIALS, "secret" );
99
100         try { new InitialContext JavaDoc( env ); } catch( Exception JavaDoc e ) {}
101     }
102
103
104     /**
105      * Creates an initial context using the empty string for the provider URL.
106      * This should work.
107      *
108      * @throws NamingException if there are any problems
109      */

110     public void testGetInitialContext() throws NamingException JavaDoc
111     {
112         Hashtable JavaDoc env = new Hashtable JavaDoc();
113
114         env.put( EnvKeys.WKDIR, "target/server" );
115
116         env.put( Context.PROVIDER_URL, "" );
117
118         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
119
120         env.put( Context.SECURITY_CREDENTIALS, "secret" );
121
122         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
123
124         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
125
126         assertNotNull( initCtx );
127     }
128
129
130     /**
131      * Gets a DirContext from the InitialContext for the empty string or RootDSE
132      * and checks that none of the operational attributes are returned.
133      *
134      * @throws NamingException if there are any problems
135      */

136     public void testGetInitialContextLookupAttributes() throws NamingException JavaDoc
137     {
138         Hashtable JavaDoc env = new Hashtable JavaDoc();
139
140         env.put( EnvKeys.WKDIR, "target/server" );
141
142         env.put( Context.PROVIDER_URL, "" );
143
144         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
145
146         env.put( Context.SECURITY_CREDENTIALS, "secret" );
147
148         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
149
150         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
151
152         assertNotNull( initCtx );
153
154         DirContext JavaDoc ctx = ( DirContext JavaDoc ) initCtx.lookup( "" );
155
156         Attributes JavaDoc attributes = ctx.getAttributes( "" );
157
158         // Added some objectClass attributes to the rootDSE
159

160         assertEquals( 1, attributes.size() );
161     }
162
163
164     /**
165      * Checks for namingContexts and vendorName attributes.
166      *
167      * @throws NamingException if there are any problems
168      */

169     public void testGetInitialContextLookupAttributesByName() throws NamingException JavaDoc
170     {
171         Hashtable JavaDoc env = new Hashtable JavaDoc();
172
173         env.put( EnvKeys.WKDIR, "target/server" );
174
175         env.put( Context.PROVIDER_URL, "" );
176
177         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
178
179         env.put( Context.SECURITY_CREDENTIALS, "secret" );
180
181         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
182
183         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
184
185         assertNotNull( initCtx );
186
187         DirContext JavaDoc ctx = ( DirContext JavaDoc ) initCtx.lookup( "" );
188
189         Attributes JavaDoc attributes = ctx.getAttributes( "", new String JavaDoc[]{ "namingContexts", "vendorName" });
190
191         assertEquals( 2, attributes.size() );
192
193         assertEquals( "Apache Software Foundation", attributes.get( "vendorName" ).get() );
194
195         assertTrue( attributes.get( "namingContexts" ).contains( "ou=system" ) );
196     }
197
198
199     /**
200      * Checks for lack of permissions to delete this entry.
201      *
202      * @throws NamingException if there are any problems
203      */

204     public void testDelete() throws NamingException JavaDoc
205     {
206         Hashtable JavaDoc env = new Hashtable JavaDoc();
207
208         env.put( EnvKeys.WKDIR, "target/server" );
209
210         env.put( Context.PROVIDER_URL, "" );
211
212         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
213
214         env.put( Context.SECURITY_CREDENTIALS, "secret" );
215
216         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
217
218         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
219
220         assertNotNull( initCtx );
221
222         DirContext JavaDoc ctx = ( DirContext JavaDoc ) initCtx.lookup( "" );
223
224         LdapNoPermissionException notNull = null;
225
226         try
227         {
228             ctx.destroySubcontext( "" );
229
230             fail( "we should never get here" );
231         }
232         catch ( LdapNoPermissionException e )
233         {
234             notNull = e;
235         }
236
237         assertNotNull( notNull );
238     }
239
240
241     /**
242      * Checks for lack of permissions to rename or move this entry.
243      *
244      * @throws NamingException if there are any problems
245      */

246     public void testRename() throws NamingException JavaDoc
247     {
248         Hashtable JavaDoc env = new Hashtable JavaDoc();
249
250         env.put( EnvKeys.WKDIR, "target/server" );
251
252         env.put( Context.PROVIDER_URL, "" );
253
254         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
255
256         env.put( Context.SECURITY_CREDENTIALS, "secret" );
257
258         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
259
260         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
261
262         assertNotNull( initCtx );
263
264         DirContext JavaDoc ctx = ( DirContext JavaDoc ) initCtx.lookup( "" );
265
266         LdapNoPermissionException notNull = null;
267
268         try
269         {
270             ctx.rename( "", "ou=system" );
271
272             fail( "we should never get here" );
273         }
274         catch ( LdapNoPermissionException e )
275         {
276             notNull = e;
277         }
278
279         assertNotNull( notNull );
280     }
281
282
283     /**
284      * Checks for lack of permissions to modify this entry.
285      *
286      * @throws NamingException if there are any problems
287      */

288     public void testModify() throws NamingException JavaDoc
289     {
290         Hashtable JavaDoc env = new Hashtable JavaDoc();
291
292         env.put( EnvKeys.WKDIR, "target/server" );
293
294         env.put( Context.PROVIDER_URL, "" );
295
296         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
297
298         env.put( Context.SECURITY_CREDENTIALS, "secret" );
299
300         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
301
302         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
303
304         assertNotNull( initCtx );
305
306         DirContext JavaDoc ctx = ( DirContext JavaDoc ) initCtx.lookup( "" );
307
308         LdapNoPermissionException notNull = null;
309
310         try
311         {
312             ctx.modifyAttributes( "", 0, null );
313
314             fail( "we should never get here" );
315         }
316         catch ( LdapNoPermissionException e )
317         {
318             notNull = e;
319         }
320
321         assertNotNull( notNull );
322     }
323
324
325     /**
326      * Checks for lack of permissions to modify this entry.
327      *
328      * @throws NamingException if there are any problems
329      */

330     public void testModify2() throws NamingException JavaDoc
331     {
332         Hashtable JavaDoc env = new Hashtable JavaDoc();
333
334         env.put( EnvKeys.WKDIR, "target/server" );
335
336         env.put( Context.PROVIDER_URL, "" );
337
338         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
339
340         env.put( Context.SECURITY_CREDENTIALS, "secret" );
341
342         env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
343
344         InitialContext JavaDoc initCtx = new InitialContext JavaDoc( env );
345
346         assertNotNull( initCtx );
347
348         DirContext JavaDoc ctx = ( DirContext JavaDoc ) initCtx.lookup( "" );
349
350         LdapNoPermissionException notNull = null;
351
352         try
353         {
354             ctx.modifyAttributes( "", new ModificationItem JavaDoc[]{} );
355
356             fail( "we should never get here" );
357         }
358         catch ( LdapNoPermissionException e )
359         {
360             notNull = e;
361         }
362
363         assertNotNull( notNull );
364     }
365 }
366
Popular Tags