KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.ldap.server.AbstractCoreTest;
21
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.directory.*;
24
25
26 /**
27  * Tests the creation of contexts in various ways.
28  *
29  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
30  * @version $Rev: 169198 $
31  */

32 public class CreateContextTest extends AbstractCoreTest
33 {
34     /**
35      * Tests the creation and subsequent read of a new JNDI context under the
36      * system context root.
37      *
38      * @throws javax.naming.NamingException if there are failures
39      */

40     public void testCreateContexts() throws NamingException JavaDoc
41     {
42         /*
43          * create ou=testing00,ou=system
44          */

45         Attributes attributes = new BasicAttributes();
46         Attribute attribute = new BasicAttribute( "objectClass" );
47         attribute.add( "top" );
48         attribute.add( "organizationalUnit" );
49         attributes.put( attribute );
50         attributes.put( "ou", "testing00" );
51         DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
52         assertNotNull( ctx );
53
54         ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
55         assertNotNull( ctx );
56
57         attributes = ctx.getAttributes( "" );
58         assertNotNull( attributes );
59         assertEquals( "testing00", attributes.get( "ou" ).get() );
60         attribute = attributes.get( "objectClass" );
61         assertNotNull( attribute );
62         assertTrue( attribute.contains( "top" ) );
63         assertTrue( attribute.contains( "organizationalUnit" ) );
64
65         /*
66          * create ou=testing01,ou=system
67          */

68         attributes = new BasicAttributes();
69         attribute = new BasicAttribute( "objectClass" );
70         attribute.add( "top" );
71         attribute.add( "organizationalUnit" );
72         attributes.put( attribute );
73         attributes.put( "ou", "testing01" );
74         ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
75         assertNotNull( ctx );
76
77         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
78         assertNotNull( ctx );
79
80         attributes = ctx.getAttributes( "" );
81         assertNotNull( attributes );
82         assertEquals( "testing01", attributes.get( "ou" ).get() );
83         attribute = attributes.get( "objectClass" );
84         assertNotNull( attribute );
85         assertTrue( attribute.contains( "top" ) );
86         assertTrue( attribute.contains( "organizationalUnit" ) );
87
88         /*
89          * create ou=testing02,ou=system
90          */

91         attributes = new BasicAttributes();
92         attribute = new BasicAttribute( "objectClass" );
93         attribute.add( "top" );
94         attribute.add( "organizationalUnit" );
95         attributes.put( attribute );
96         attributes.put( "ou", "testing02" );
97         ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
98         assertNotNull( ctx );
99
100         ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
101         assertNotNull( ctx );
102
103         attributes = ctx.getAttributes( "" );
104         assertNotNull( attributes );
105         assertEquals( "testing02", attributes.get( "ou" ).get() );
106         attribute = attributes.get( "objectClass" );
107         assertNotNull( attribute );
108         assertTrue( attribute.contains( "top" ) );
109         assertTrue( attribute.contains( "organizationalUnit" ) );
110
111         /*
112          * create ou=subtest,ou=testing01,ou=system
113          */

114         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
115
116         attributes = new BasicAttributes();
117         attribute = new BasicAttribute( "objectClass" );
118         attribute.add( "top" );
119         attribute.add( "organizationalUnit" );
120         attributes.put( attribute );
121         attributes.put( "ou", "subtest" );
122         ctx = ctx.createSubcontext( "ou=subtest", attributes );
123         assertNotNull( ctx );
124
125         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
126         assertNotNull( ctx );
127
128         attributes = ctx.getAttributes( "" );
129         assertNotNull( attributes );
130         assertEquals( "subtest", attributes.get( "ou" ).get() );
131         attribute = attributes.get( "objectClass" );
132         assertNotNull( attribute );
133         assertTrue( attribute.contains( "top" ) );
134         assertTrue( attribute.contains( "organizationalUnit" ) );
135     }
136
137
138     public void testFailCreateExisting() throws NamingException JavaDoc
139     {
140         Attribute attribute;
141         Attributes attributes;
142         DirContext ctx = null;
143
144         /*
145          * create ou=testing00,ou=system
146          */

147         attributes = new BasicAttributes();
148         attribute = new BasicAttribute( "objectClass" );
149         attribute.add( "top" );
150         attribute.add( "organizationalUnit" );
151         attributes.put( attribute );
152         attributes.put( "ou", "testing00" );
153         ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
154         assertNotNull( ctx );
155
156         ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
157         assertNotNull( ctx );
158
159         attributes = ctx.getAttributes( "" );
160         assertNotNull( attributes );
161         assertEquals( "testing00", attributes.get( "ou" ).get() );
162         attribute = attributes.get( "objectClass" );
163         assertNotNull( attribute );
164         assertTrue( attribute.contains( "top" ) );
165         assertTrue( attribute.contains( "organizationalUnit" ) );
166
167
168         /*
169          * fail on recreate attempt for ou=testing00,ou=system
170          */

171         attributes = new BasicAttributes();
172         attribute = new BasicAttribute( "objectClass" );
173         attribute.add( "top" );
174         attribute.add( "organizationalUnit" );
175         attributes.put( attribute );
176         attributes.put( "ou", "testing00" );
177
178         ctx = null;
179         try
180         {
181             ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
182             fail( "Attempt to create exiting context should fail!" );
183         }
184         catch ( NamingException JavaDoc e )
185         {
186             assertNotNull( e );
187         }
188
189         assertNull( ctx );
190     }
191 }
192
Popular Tags