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 javax.naming.NamingException;
21  import javax.naming.Context;
22  import javax.naming.NamingEnumeration;
23  import javax.naming.directory.*;
24  
25  import org.apache.ldap.server.AbstractAdminTestCase;
26  import org.apache.ldap.server.configuration.MutableDirectoryPartitionConfiguration;
27  import org.apache.ldap.common.exception.LdapNameNotFoundException;
28  
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  
33  /***
34   * Tests various operations against a partition whose suffix contains both upper and lower case letters.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev$
38   */
39  public class MixedCaseTest extends AbstractAdminTestCase
40  {
41      String suffix = "dc=Apache,dc=Org";
42  
43      public void setUp() throws Exception
44      {
45  
46          MutableDirectoryPartitionConfiguration partition = new MutableDirectoryPartitionConfiguration();
47          partition.setName( "apache" );
48          partition.setSuffix( suffix );
49  
50          Set indexedAttributes = new HashSet();
51          indexedAttributes.add( "objectClass" );
52          indexedAttributes.add( "ou" );
53          indexedAttributes.add( "uid" );
54          partition.setIndexedAttributes( indexedAttributes );
55  
56          Attributes attrs = new BasicAttributes( true );
57          Attribute objectClass = new BasicAttribute( "objectClass" );
58          objectClass.add( "top" );
59          objectClass.add( "domain" );
60          objectClass.add( "extensibleObject" );
61          attrs.put( objectClass );
62          attrs.put( "dc", "Apache" );
63  
64          partition.setContextEntry( attrs );
65  
66          Set partitions = new HashSet();
67          partitions.add( partition );
68  
69          configuration.setContextPartitionConfigurations( partitions );
70          super.overrideEnvironment( Context.PROVIDER_URL, suffix );
71  
72          super.setUp();
73      }
74  
75      public void testSearch() throws NamingException
76      {
77          SearchControls sc = new SearchControls();
78          sc.setSearchScope( SearchControls.SUBTREE_SCOPE );
79  
80          NamingEnumeration ne = sysRoot.search( "", "(objectClass=*)", sc );
81  
82          assertTrue( "Search should return at least one entry.", ne.hasMore() );
83  
84          SearchResult sr = (SearchResult) ne.next();
85  
86          assertEquals( "The entry returned should be the root entry.", suffix, sr.getName() );
87  
88          assertFalse( "Search should return no more entries.", ne.hasMore() );
89      }
90  
91      public void testAdd() throws NamingException
92      {
93          String dn = "ou=Test";
94  
95          Attributes attributes = new BasicAttributes( true );
96          Attribute attribute = new BasicAttribute( "objectClass" );
97          attribute.add( "top" );
98          attribute.add( "organizationalUnit" );
99          attributes.put( attribute );
100         attributes.put( "ou", "Test" );
101 
102         DirContext ctx = sysRoot.createSubcontext( dn, attributes );
103         assertNotNull( ctx );
104 
105         SearchControls sc = new SearchControls();
106         sc.setSearchScope( SearchControls.OBJECT_SCOPE );
107 
108         NamingEnumeration ne = sysRoot.search( dn, "(objectClass=*)", sc );
109 
110         assertTrue( "Search should return at least one entry.", ne.hasMore() );
111 
112         SearchResult sr = (SearchResult) ne.next();
113 
114         assertEquals( "The entry returned should be the entry added earlier.", dn+","+suffix, sr.getName() );
115 
116         assertFalse( "Search should return no more entries.", ne.hasMore() );
117     }
118 
119     public void testModify() throws NamingException
120     {
121         String dn = "ou=Test";
122         String description = "New Value";
123 
124         Attributes attributes = new BasicAttributes( true );
125         Attribute attribute = new BasicAttribute( "objectClass" );
126         attribute.add( "top" );
127         attribute.add( "organizationalUnit" );
128         attributes.put( attribute );
129         attributes.put( "ou", "Test" );
130         attributes.put( "description", "Old Value" );
131 
132         DirContext ctx = sysRoot.createSubcontext( dn, attributes );
133         assertNotNull( ctx );
134 
135         ModificationItem[] mods = new ModificationItem[1];
136         mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( "description", description ));
137 
138         sysRoot.modifyAttributes( dn, mods );
139 
140         SearchControls sc = new SearchControls();
141         sc.setSearchScope( SearchControls.OBJECT_SCOPE );
142 
143         NamingEnumeration ne = sysRoot.search( dn, "(objectClass=*)", sc );
144 
145         assertTrue( "Search should return at least one entry.", ne.hasMore() );
146 
147         SearchResult sr = (SearchResult) ne.next();
148 
149         assertEquals( "The entry returned should be the entry added earlier.", dn+","+suffix, sr.getName() );
150 
151         attributes = sr.getAttributes();
152         attribute = attributes.get( "description" );
153 
154         assertEquals( "The description attribute should contain the new value.", description, attribute.get() );
155 
156         assertFalse( "Search should return no more entries.", ne.hasMore() );
157     }
158 
159     public void testDelete() throws NamingException
160     {
161         String dn = "ou=Test";
162 
163         Attributes attributes = new BasicAttributes( true );
164         Attribute attribute = new BasicAttribute( "objectClass" );
165         attribute.add( "top" );
166         attribute.add( "organizationalUnit" );
167         attributes.put( attribute );
168         attributes.put( "ou", "Test" );
169 
170         DirContext ctx = sysRoot.createSubcontext( dn, attributes );
171         assertNotNull( ctx );
172 
173         sysRoot.destroySubcontext( dn );
174 
175         SearchControls sc = new SearchControls();
176         sc.setSearchScope( SearchControls.OBJECT_SCOPE );
177 
178         try {
179             sysRoot.search( dn, "(objectClass=*)", sc );
180 
181             fail( "Search should throw exception.");
182 
183         } catch (LdapNameNotFoundException e) {
184             // ignore
185         }
186     }
187 
188 }
189