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.ldap.common.message.LockableAttributeImpl;
22  import org.apache.ldap.common.message.LockableAttributesImpl;
23  import org.apache.ldap.common.util.ArrayUtils;
24  import org.apache.ldap.server.ContextPartitionConfig;
25  
26  import javax.naming.NamingException;
27  import java.util.Hashtable;
28  
29  
30  /***
31   * Testcase which tests the correct operation of the PartitionContextBuilder.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev$
35   */
36  public class PartitionContextBuilderTest extends TestCase
37  {
38      /***
39       * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
40       * using an empty Hashtable.
41       */
42      public void testEmptyEnvironment() throws NamingException
43      {
44          Hashtable env = new Hashtable();
45          ContextPartitionConfig[] configs = null;
46  
47          configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
48          assertNotNull( configs );
49          assertEquals( 0, configs.length );
50      }
51  
52  
53      /***
54       * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
55       * using a Hashtable with only partition names.
56       */
57      public void testPartialConfig() throws NamingException
58      {
59          Hashtable env = new Hashtable();
60          ContextPartitionConfig[] configs = null;
61  
62          // setup everything and build config bean
63          env.put( EnvKeys.PARTITIONS, "apache test" );
64          configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
65  
66          // start testing return values
67          assertNotNull( configs );
68          assertEquals( 2, configs.length );
69  
70          // test the apache config bean
71          assertEquals( "apache", configs[0].getId() );
72          assertNull( configs[0].getSuffix() );
73          assertNotNull( configs[0].getAttributes() );
74          assertEquals( 0, configs[0].getAttributes().size() );
75          assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
76                  configs[0].getIndices() ) );
77  
78          // test the 'test' config bean
79          assertEquals( "test", configs[1].getId() );
80          assertNull( configs[1].getSuffix() );
81          assertNotNull( configs[1].getAttributes() );
82          assertEquals( 0, configs[1].getAttributes().size() );
83          assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
84                  configs[1].getIndices() ) );
85      }
86  
87  
88      /***
89       * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
90       * using a Hashtable with only partition names but the list property has
91       * extra spaces in between and at the ends.
92       */
93      public void testPartialConfigWithExtraWhitespace() throws NamingException
94      {
95          Hashtable env = new Hashtable();
96          ContextPartitionConfig[] configs = null;
97  
98          // setup everything and build config bean
99          env.put( EnvKeys.PARTITIONS, "  apache        test " );
100         configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
101 
102         // start testing return values
103         assertNotNull( configs );
104         assertEquals( 2, configs.length );
105 
106         // test the apache config bean
107         assertEquals( "apache", configs[0].getId() );
108         assertNull( configs[0].getSuffix() );
109         assertNotNull( configs[0].getAttributes() );
110         assertEquals( 0, configs[0].getAttributes().size() );
111         assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
112                 configs[0].getIndices() ) );
113 
114         // test the 'test' config bean
115         assertEquals( "test", configs[1].getId() );
116         assertNull( configs[1].getSuffix() );
117         assertNotNull( configs[1].getAttributes() );
118         assertEquals( 0, configs[1].getAttributes().size() );
119         assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
120                 configs[1].getIndices() ) );
121     }
122 
123 
124     /***
125      * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
126      * using a Hashtable with partitions that have a suffix.  Correctness with
127      * whitespace varience is tested.
128      */
129     public void testSuffixKeys() throws NamingException
130     {
131         Hashtable env = new Hashtable();
132         ContextPartitionConfig[] configs = null;
133 
134         // setup everything and build config bean
135         env.put( EnvKeys.PARTITIONS, "apache test" );
136         env.put( EnvKeys.SUFFIX + "apache", " dc= apache,    dc=org" );
137         env.put( EnvKeys.SUFFIX + "test", "   ou   =  test " );
138         configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
139 
140         // start testing return values
141         assertNotNull( configs );
142         assertEquals( 2, configs.length );
143 
144         // test the apache config bean
145         assertEquals( "apache", configs[0].getId() );
146         assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
147         assertNotNull( configs[0].getAttributes() );
148         assertEquals( 0, configs[0].getAttributes().size() );
149         assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
150                 configs[0].getIndices() ) );
151 
152         // test the 'test' config bean
153         assertEquals( "test", configs[1].getId() );
154         assertEquals( "ou=test", configs[1].getSuffix() );
155         assertNotNull( configs[1].getAttributes() );
156         assertEquals( 0, configs[1].getAttributes().size() );
157         assertTrue( ArrayUtils.isEquals( ArrayUtils.EMPTY_STRING_ARRAY,
158                 configs[1].getIndices() ) );
159     }
160 
161 
162     /***
163      * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
164      * using a Hashtable with partitions that have malformed suffix
165      * distinguished names.  We test for failure.
166      */
167     public void testSuffixKeysWithMalformedDN()
168     {
169         Hashtable env = new Hashtable();
170 
171         // setup everything and build config bean
172         env.put( EnvKeys.PARTITIONS, "apache test" );
173         env.put( EnvKeys.SUFFIX + "apache", " dcapachedcorg" );
174 
175         try
176         {
177             PartitionConfigBuilder.getContextPartitionConfigs( env );
178             fail( "should never get here due to an exception" );
179         }
180         catch( NamingException e )
181         {
182         }
183     }
184 
185 
186     /***
187      * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
188      * using a Hashtable with partitions that have suffixes and indices set.
189      */
190     public void testIndexKeys() throws NamingException
191     {
192         Hashtable env = new Hashtable();
193         ContextPartitionConfig[] configs = null;
194 
195         // setup everything and build config bean
196         env.put( EnvKeys.PARTITIONS, "apache test" );
197         env.put( EnvKeys.SUFFIX  + "apache", "dc=apache,dc=org" );
198         env.put( EnvKeys.SUFFIX  + "test", "ou=test" );
199         env.put( EnvKeys.INDICES + "apache", "ou objectClass      uid" );
200         env.put( EnvKeys.INDICES + "test", "ou objectClass  " );
201         configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
202 
203         // start testing return values
204         assertNotNull( configs );
205         assertEquals( 2, configs.length );
206 
207         // test the apache config bean
208         assertEquals( "apache", configs[0].getId() );
209         assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
210         assertNotNull( configs[0].getAttributes() );
211         assertEquals( 0, configs[0].getAttributes().size() );
212         assertEquals( 3, configs[0].getIndices().length );
213         assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass", "uid" },
214                 configs[0].getIndices() ) );
215 
216         // test the 'test' config bean
217         assertEquals( "test", configs[1].getId() );
218         assertEquals( "ou=test", configs[1].getSuffix() );
219         assertNotNull( configs[1].getAttributes() );
220         assertEquals( 0, configs[1].getAttributes().size() );
221         assertEquals( 2, configs[1].getIndices().length );
222         assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass" },
223                 configs[1].getIndices() ) );
224     }
225 
226 
227 
228     /***
229      * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
230      * using a Hashtable with partitions that have suffixes, indices and
231      * attributes set.
232      */
233     public void testAttributeKeys() throws NamingException
234     {
235         Hashtable env = new Hashtable();
236         ContextPartitionConfig[] configs = null;
237 
238         // setup everything and build config bean
239         env.put( EnvKeys.PARTITIONS, "apache test" );
240         env.put( EnvKeys.SUFFIX  + "apache", "dc=apache,dc=org" );
241         env.put( EnvKeys.SUFFIX  + "test", "ou=test" );
242         env.put( EnvKeys.INDICES + "apache", "ou objectClass      uid" );
243         env.put( EnvKeys.INDICES + "test", "ou objectClass  " );
244         env.put( EnvKeys.ATTRIBUTES + "apache" + ".dc", "apache" );
245         env.put( EnvKeys.ATTRIBUTES + "apache" + ".objectClass", "top domain extensibleObject" );
246         env.put( EnvKeys.ATTRIBUTES + "test" + ".ou", "test" );
247         env.put( EnvKeys.ATTRIBUTES + "test" + ".objectClass", "top extensibleObject organizationalUnit" );
248         configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
249 
250         // start testing return values
251         assertNotNull( configs );
252         assertEquals( 2, configs.length );
253 
254         // test the apache config bean
255         assertEquals( "apache", configs[0].getId() );
256         assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
257         assertNotNull( configs[0].getAttributes() );
258         assertEquals( 2, configs[0].getAttributes().size() );
259         assertEquals( 3, configs[0].getIndices().length );
260         assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass", "uid" },
261                 configs[0].getIndices() ) );
262         LockableAttributesImpl attrs = new LockableAttributesImpl();
263         LockableAttributeImpl attr = new LockableAttributeImpl( "dc" );
264         attrs.put( attr );
265         attr.add( "apache" );
266         attr = new LockableAttributeImpl( "objectClass" );
267         attrs.put( attr );
268         attr.add( "top" );
269         attr.add( "domain" );
270         attr.add( "extensibleObject" );
271         assertTrue( attrs.equals( configs[0].getAttributes() ) );
272 
273         // test the 'test' config bean
274         assertEquals( "test", configs[1].getId() );
275         assertEquals( "ou=test", configs[1].getSuffix() );
276         assertNotNull( configs[1].getAttributes() );
277         assertEquals( 2, configs[1].getAttributes().size() );
278         assertEquals( 2, configs[1].getIndices().length );
279         assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass" },
280                 configs[1].getIndices() ) );
281         attrs = new LockableAttributesImpl();
282         attr = new LockableAttributeImpl( "ou" );
283         attrs.put( attr );
284         attr.add( "test" );
285         attr = new LockableAttributeImpl( "objectClass" );
286         attrs.put( attr );
287         attr.add( "top" );
288         attr.add( "extensibleObject" );
289         attr.add( "organizationalUnit" );
290         assertTrue( attrs.equals( configs[1].getAttributes() ) );
291     }
292 
293 
294     /***
295      * Tests {@link PartitionConfigBuilder#getContextPartitionConfigs(Hashtable)}
296      * using a Hashtable with partitions that have suffixes, indices and
297      * attributes set however values have some space variance.
298      */
299     public void testAttributeValuesWithWhitespace() throws NamingException
300     {
301         Hashtable env = new Hashtable();
302         ContextPartitionConfig[] configs = null;
303 
304         // setup everything and build config bean
305         env.put( EnvKeys.PARTITIONS, "apache test" );
306         env.put( EnvKeys.SUFFIX  + "apache", "dc=apache,dc=org" );
307         env.put( EnvKeys.SUFFIX  + "test", "ou=test" );
308         env.put( EnvKeys.INDICES + "apache", "ou objectClass      uid" );
309         env.put( EnvKeys.INDICES + "test", "ou objectClass  " );
310         env.put( EnvKeys.ATTRIBUTES + "apache" + ".dc", "apache" );
311         env.put( EnvKeys.ATTRIBUTES + "apache" + ".objectClass",
312                 "     top    domain    extensibleObject " );
313         env.put( EnvKeys.ATTRIBUTES + "test" + ".ou", "test" );
314         env.put( EnvKeys.ATTRIBUTES + "test" + ".objectClass",
315                 "top extensibleObject    organizationalUnit" );
316         configs = PartitionConfigBuilder.getContextPartitionConfigs( env );
317 
318         // start testing return values
319         assertNotNull( configs );
320         assertEquals( 2, configs.length );
321 
322         // test the apache config bean
323         assertEquals( "apache", configs[0].getId() );
324         assertEquals( "dc=apache,dc=org", configs[0].getSuffix() );
325         assertNotNull( configs[0].getAttributes() );
326         assertEquals( 2, configs[0].getAttributes().size() );
327         assertEquals( 3, configs[0].getIndices().length );
328         assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass", "uid" },
329                 configs[0].getIndices() ) );
330         LockableAttributesImpl attrs = new LockableAttributesImpl();
331         LockableAttributeImpl attr = new LockableAttributeImpl( "dc" );
332         attrs.put( attr );
333         attr.add( "apache" );
334         attr = new LockableAttributeImpl( "objectClass" );
335         attrs.put( attr );
336         attr.add( "top" );
337         attr.add( "domain" );
338         attr.add( "extensibleObject" );
339         assertTrue( attrs.equals( configs[0].getAttributes() ) );
340 
341         // test the 'test' config bean
342         assertEquals( "test", configs[1].getId() );
343         assertEquals( "ou=test", configs[1].getSuffix() );
344         assertNotNull( configs[1].getAttributes() );
345         assertEquals( 2, configs[1].getAttributes().size() );
346         assertEquals( 2, configs[1].getIndices().length );
347         assertTrue( ArrayUtils.isEquals( new String[]{ "ou", "objectClass" },
348                 configs[1].getIndices() ) );
349         attrs = new LockableAttributesImpl();
350         attr = new LockableAttributeImpl( "ou" );
351         attrs.put( attr );
352         attr.add( "test" );
353         attr = new LockableAttributeImpl( "objectClass" );
354         attrs.put( attr );
355         attr.add( "top" );
356         attr.add( "extensibleObject" );
357         attr.add( "organizationalUnit" );
358         assertTrue( attrs.equals( configs[1].getAttributes() ) );
359     }
360 }