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