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.common.message.DerefAliasesEnum;
21  import org.apache.ldap.server.AbstractCoreTest;
22  
23  import javax.naming.NamingEnumeration;
24  import javax.naming.NamingException;
25  import javax.naming.directory.*;
26  import java.util.HashMap;
27  
28  
29  /***
30   * Tests the search() methods of the provider.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 165254 $
34   */
35  public class SearchContextTest extends AbstractCoreTest
36  {
37      protected void setUp() throws Exception
38      {
39          super.setUp();
40  
41          /*
42           * create ou=testing00,ou=system
43           */
44          Attributes attributes = new BasicAttributes();
45  
46          Attribute attribute = new BasicAttribute( "objectClass" );
47  
48          attribute.add( "top" );
49  
50          attribute.add( "organizationalUnit" );
51  
52          attributes.put( attribute );
53  
54          attributes.put( "ou", "testing00" );
55  
56          DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
57  
58          assertNotNull( ctx );
59  
60          ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
61  
62          assertNotNull( ctx );
63  
64          attributes = ctx.getAttributes( "" );
65  
66          assertNotNull( attributes );
67  
68          assertEquals( "testing00", attributes.get( "ou" ).get() );
69  
70          attribute = attributes.get( "objectClass" );
71  
72          assertNotNull( attribute );
73  
74          assertTrue( attribute.contains( "top" ) );
75  
76          assertTrue( attribute.contains( "organizationalUnit" ) );
77  
78          /*
79           * create ou=testing01,ou=system
80           */
81          attributes = new BasicAttributes();
82  
83          attribute = new BasicAttribute( "objectClass" );
84  
85          attribute.add( "top" );
86  
87          attribute.add( "organizationalUnit" );
88  
89          attributes.put( attribute );
90  
91          attributes.put( "ou", "testing01" );
92  
93          ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
94  
95          assertNotNull( ctx );
96  
97          ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
98  
99          assertNotNull( ctx );
100 
101         attributes = ctx.getAttributes( "" );
102 
103         assertNotNull( attributes );
104 
105         assertEquals( "testing01", attributes.get( "ou" ).get() );
106 
107         attribute = attributes.get( "objectClass" );
108 
109         assertNotNull( attribute );
110 
111         assertTrue( attribute.contains( "top" ) );
112 
113         assertTrue( attribute.contains( "organizationalUnit" ) );
114 
115         /*
116          * create ou=testing02,ou=system
117          */
118         attributes = new BasicAttributes();
119 
120         attribute = new BasicAttribute( "objectClass" );
121 
122         attribute.add( "top" );
123 
124         attribute.add( "organizationalUnit" );
125 
126         attributes.put( attribute );
127 
128         attributes.put( "ou", "testing02" );
129 
130         ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
131 
132         assertNotNull( ctx );
133 
134         ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
135 
136         assertNotNull( ctx );
137 
138         attributes = ctx.getAttributes( "" );
139 
140         assertNotNull( attributes );
141 
142         assertEquals( "testing02", attributes.get( "ou" ).get() );
143 
144         attribute = attributes.get( "objectClass" );
145 
146         assertNotNull( attribute );
147 
148         assertTrue( attribute.contains( "top" ) );
149 
150         assertTrue( attribute.contains( "organizationalUnit" ) );
151 
152         /*
153          * create ou=subtest,ou=testing01,ou=system
154          */
155         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
156 
157         attributes = new BasicAttributes();
158 
159         attribute = new BasicAttribute( "objectClass" );
160 
161         attribute.add( "top" );
162 
163         attribute.add( "organizationalUnit" );
164 
165         attributes.put( attribute );
166 
167         attributes.put( "ou", "subtest" );
168 
169         ctx = ctx.createSubcontext( "ou=subtest", attributes );
170 
171         assertNotNull( ctx );
172 
173         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
174 
175         assertNotNull( ctx );
176 
177         attributes = ctx.getAttributes( "" );
178 
179         assertNotNull( attributes );
180 
181         assertEquals( "subtest", attributes.get( "ou" ).get() );
182 
183         attribute = attributes.get( "objectClass" );
184 
185         assertNotNull( attribute );
186 
187         assertTrue( attribute.contains( "top" ) );
188 
189         assertTrue( attribute.contains( "organizationalUnit" ) );
190     }
191 
192 
193     public void testSearchOneLevel() throws NamingException
194     {
195         SearchControls controls = new SearchControls();
196 
197         controls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
198 
199         controls.setDerefLinkFlag( false );
200 
201         sysRoot.addToEnvironment( DerefAliasesEnum.JNDI_PROP, DerefAliasesEnum.NEVERDEREFALIASES.getName() );
202 
203         HashMap map = new HashMap();
204 
205         NamingEnumeration list = sysRoot.search( "", "(ou=*)", controls );
206 
207         while ( list.hasMore() )
208         {
209             SearchResult result = ( SearchResult ) list.next();
210 
211             map.put( result.getName(), result.getAttributes() );
212         }
213 
214         assertEquals( "Expected number of results returned was incorrect!", 5, map.size() );
215 
216         assertTrue( map.containsKey( "ou=testing00,ou=system" ) );
217 
218         assertTrue( map.containsKey( "ou=testing01,ou=system" ) );
219 
220         assertTrue( map.containsKey( "ou=testing02,ou=system" ) );
221     }
222 
223 
224     public void testSearchSubTreeLevel() throws NamingException
225     {
226         SearchControls controls = new SearchControls();
227 
228         controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
229 
230         controls.setDerefLinkFlag( false );
231 
232         sysRoot.addToEnvironment( DerefAliasesEnum.JNDI_PROP, DerefAliasesEnum.NEVERDEREFALIASES.getName() );
233 
234         HashMap map = new HashMap();
235 
236         NamingEnumeration list = sysRoot.search( "", "(ou=*)", controls );
237 
238         while ( list.hasMore() )
239         {
240             SearchResult result = ( SearchResult ) list.next();
241 
242             map.put( result.getName(), result.getAttributes() );
243         }
244 
245         assertEquals( "Expected number of results returned was incorrect", 8, map.size() );
246 
247         assertTrue( map.containsKey( "ou=system" ) );
248 
249         assertTrue( map.containsKey( "ou=testing00,ou=system" ) );
250 
251         assertTrue( map.containsKey( "ou=testing01,ou=system" ) );
252 
253         assertTrue( map.containsKey( "ou=testing02,ou=system" ) );
254 
255         assertTrue( map.containsKey( "ou=subtest,ou=testing01,ou=system" ) );
256     }
257 }