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