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.Context;
23  import javax.naming.InitialContext;
24  import javax.naming.NamingException;
25  import javax.naming.directory.*;
26  import java.util.Hashtable;
27  
28  
29  /***
30   * Tests to see if we can fire up the Eve directory server via JNDI.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 165308 $
34   */
35  public class ServerContextFactoryTest extends AbstractCoreTest
36  {
37      public ServerContextFactoryTest()
38      {
39          BasicAttributes attrs = new BasicAttributes( true );
40  
41          BasicAttribute attr = new BasicAttribute( "objectClass" );
42  
43          attr.add( "top" );
44  
45          attr.add( "organizationalUnit" );
46  
47          attr.add( "extensibleObject" );
48  
49          attrs.put( attr );
50  
51          attr = new BasicAttribute( "ou" );
52  
53          attr.add( "testing" );
54  
55          attrs.put( attr );
56  
57          extras.put( EnvKeys.PARTITIONS, "testing example MixedCase" );
58  
59          extras.put( EnvKeys.SUFFIX + "testing", "ou=testing" );
60  
61          extras.put( EnvKeys.INDICES + "testing", "ou objectClass" );
62  
63          extras.put( EnvKeys.ATTRIBUTES + "testing", attrs );
64  
65          attrs = new BasicAttributes( true );
66  
67          attr = new BasicAttribute( "objectClass" );
68  
69          attr.add( "top" );
70  
71          attr.add( "domain" );
72  
73          attr.add( "extensibleObject" );
74  
75          attrs.put( attr );
76  
77          attr = new BasicAttribute( "dc" );
78  
79          attr.add( "example" );
80  
81          attrs.put( attr );
82  
83          extras.put( EnvKeys.SUFFIX + "example", "dc=example" );
84  
85          extras.put( EnvKeys.INDICES + "example", "ou dc objectClass" );
86  
87          extras.put( EnvKeys.ATTRIBUTES + "example", attrs );
88  
89          attrs = new BasicAttributes( true );
90  
91          attr = new BasicAttribute( "objectClass" );
92  
93          attr.add( "top" );
94  
95          attr.add( "domain" );
96  
97          attr.add( "extensibleObject" );
98  
99          attrs.put( attr );
100 
101         attr = new BasicAttribute( "dc" );
102 
103         attr.add( "MixedCase" );
104 
105         attrs.put( attr );
106 
107         extras.put( EnvKeys.SUFFIX + "MixedCase", "dc=MixedCase" );
108 
109         extras.put( EnvKeys.INDICES + "MixedCase", "dc objectClass" );
110 
111         extras.put( EnvKeys.ATTRIBUTES + "MixedCase", attrs );
112     }
113 
114 
115     /***
116      * Makes sure the system context has the right attributes and values.
117      *
118      * @throws NamingException if there are failures
119      */
120     public void testSystemContext() throws NamingException
121     {
122         assertNotNull( sysRoot );
123 
124         Attributes attributes = sysRoot.getAttributes( "" );
125 
126         assertNotNull( attributes );
127 
128         assertEquals( "system", attributes.get( "ou" ).get() );
129 
130         Attribute attribute = attributes.get( "objectClass" );
131 
132         assertNotNull( attribute );
133 
134         assertTrue( attribute.contains( "top" ) );
135 
136         assertTrue( attribute.contains( "organizationalUnit" ) );
137     }
138 
139 
140     /***
141      * Tests to make sure tearDown is working correctly.
142      *
143      * @throws NamingException if there are failures
144      */
145     public void testSetupTeardown() throws NamingException
146     {
147         assertNotNull( sysRoot );
148 
149         Attributes attributes = sysRoot.getAttributes( "" );
150 
151         assertNotNull( attributes );
152 
153         assertEquals( "system", attributes.get( "ou" ).get() );
154 
155         Attribute attribute = attributes.get( "objectClass" );
156 
157         assertNotNull( attribute );
158 
159         assertTrue( attribute.contains( "top" ) );
160 
161         assertTrue( attribute.contains( "organizationalUnit" ) );
162     }
163 
164 
165     public void testAppPartitionExample() throws NamingException
166     {
167         Hashtable env = new Hashtable();
168 
169         env.put( Context.PROVIDER_URL, "dc=example" );
170 
171         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
172 
173         env.put( Context.SECURITY_CREDENTIALS, "secret" );
174 
175         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
176 
177         InitialContext initialContext = new InitialContext( env );
178 
179         DirContext appRoot = ( DirContext ) initialContext.lookup( "" );
180 
181         assertNotNull( appRoot );
182 
183         Attributes attributes = appRoot.getAttributes( "" );
184 
185         assertNotNull( attributes );
186 
187         assertEquals( "example", attributes.get( "dc" ).get() );
188 
189         Attribute attribute = attributes.get( "objectClass" );
190 
191         assertNotNull( attribute );
192 
193         assertTrue( attribute.contains( "top" ) );
194 
195         assertTrue( attribute.contains( "domain" ) );
196     }
197 
198 
199     public void testAppPartitionTesting() throws NamingException
200     {
201         Hashtable env = new Hashtable();
202 
203         env.put( Context.PROVIDER_URL, "ou=testing" );
204 
205         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
206 
207         env.put( Context.SECURITY_CREDENTIALS, "secret" );
208 
209         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
210 
211         InitialContext initialContext = new InitialContext( env );
212 
213         DirContext appRoot = ( DirContext ) initialContext.lookup( "" );
214 
215         assertNotNull( appRoot );
216 
217         Attributes attributes = appRoot.getAttributes( "" );
218 
219         assertNotNull( attributes );
220 
221         assertEquals( "testing", attributes.get( "ou" ).get() );
222 
223         Attribute attribute = attributes.get( "objectClass" );
224 
225         assertNotNull( attribute );
226 
227         assertTrue( attribute.contains( "top" ) );
228 
229         assertTrue( attribute.contains( "organizationalUnit" ) );
230     }
231 
232 
233     public void testAppPartitionMixedCase() throws NamingException
234     {
235         Hashtable env = new Hashtable();
236 
237         env.put( Context.PROVIDER_URL, "dc=MixedCase" );
238 
239         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
240 
241         env.put( Context.SECURITY_CREDENTIALS, "secret" );
242 
243         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
244 
245         InitialContext initialContext = new InitialContext( env );
246 
247         DirContext appRoot = ( DirContext ) initialContext.lookup( "" );
248 
249         assertNotNull( appRoot );
250 
251         Attributes attributes = appRoot.getAttributes( "" );
252 
253         assertNotNull( attributes );
254 
255         assertEquals( "MixedCase", attributes.get( "dc" ).get() );
256 
257         Attribute attribute = attributes.get( "objectClass" );
258 
259         assertNotNull( attribute );
260 
261         assertTrue( attribute.contains( "top" ) );
262 
263         assertTrue( attribute.contains( "domain" ) );
264     }
265 }