1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.jndi;
21
22
23 import org.apache.directory.server.core.DefaultDirectoryService;
24 import org.apache.directory.server.core.DirectoryService;
25 import org.apache.directory.server.core.entry.ServerEntry;
26 import org.apache.directory.server.core.integ.CiRunner;
27 import org.apache.directory.server.core.integ.DirectoryServiceFactory;
28 import static org.apache.directory.server.core.integ.IntegrationUtils.getContext;
29 import org.apache.directory.server.core.integ.Level;
30 import org.apache.directory.server.core.integ.annotations.Factory;
31 import org.apache.directory.server.core.integ.annotations.CleanupLevel;
32 import org.apache.directory.server.core.partition.Partition;
33 import org.apache.directory.server.xdbm.Index;
34 import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
35 import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
36 import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
37 import org.apache.directory.shared.ldap.name.LdapDN;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertTrue;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertNotNull;
43 import static org.junit.Assert.fail;
44
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48
49 import javax.naming.NamingEnumeration;
50 import javax.naming.NamingException;
51 import javax.naming.directory.Attribute;
52 import javax.naming.directory.Attributes;
53 import javax.naming.directory.BasicAttribute;
54 import javax.naming.directory.BasicAttributes;
55 import javax.naming.directory.DirContext;
56 import javax.naming.directory.ModificationItem;
57 import javax.naming.directory.SearchControls;
58 import javax.naming.directory.SearchResult;
59 import javax.naming.ldap.LdapContext;
60 import java.util.HashSet;
61 import java.util.Set;
62
63
64
65
66
67
68
69
70 @RunWith ( CiRunner.class )
71 @CleanupLevel ( Level.CLASS )
72 @Factory ( MixedCaseITest.MyFactory.class )
73 public class MixedCaseITest
74 {
75 public static DirectoryService service;
76
77 private static final String SUFFIX_DN = "dc=Apache,dc=Org";
78
79
80 public static class MyFactory implements DirectoryServiceFactory
81 {
82 public DirectoryService newInstance() throws NamingException
83 {
84 DirectoryService service = new DefaultDirectoryService();
85 service.getChangeLog().setEnabled( true );
86
87 JdbmPartition partition = new JdbmPartition();
88 partition.setId( "apache" );
89 partition.setSuffix( SUFFIX_DN );
90
91 HashSet<Index<?, ServerEntry>> indexedAttributes = new HashSet<Index<?, ServerEntry>>();
92 indexedAttributes.add( new JdbmIndex<String,ServerEntry>( "objectClass" ) );
93 indexedAttributes.add( new JdbmIndex<String,ServerEntry>( "ou" ) );
94 indexedAttributes.add( new JdbmIndex<String,ServerEntry>( "uid" ) );
95 partition.setIndexedAttributes( indexedAttributes );
96
97 Set<Partition> partitions = new HashSet<Partition>();
98 partitions.add( partition );
99
100 service.setPartitions( partitions );
101 return service;
102 }
103 }
104
105
106 @Before
107 public void setUp() throws Exception
108 {
109 LdapDN dn = new LdapDN( "dc=Apache,dc=Org" );
110 ServerEntry entry = service.newEntry( dn );
111 entry.add( "objectClass", "top", "domain", "extensibleObject" );
112 entry.add( "dc", "Apache" );
113 service.getAdminSession().add( entry );
114 }
115
116
117 @Test
118 public void testSearch() throws Exception
119 {
120 LdapContext ctxRoot = getContext( "uid=admin,ou=system", service, SUFFIX_DN );
121
122 SearchControls sc = new SearchControls();
123 sc.setSearchScope( SearchControls.SUBTREE_SCOPE );
124
125 NamingEnumeration<SearchResult> ne = ctxRoot.search( "", "(objectClass=*)", sc );
126 assertTrue( "Search should return at least one entry.", ne.hasMore() );
127
128 SearchResult sr = ne.next();
129 assertEquals( "The entry returned should be the root entry.", SUFFIX_DN, sr.getName() );
130 assertFalse( "Search should return no more entries.", ne.hasMore() );
131 }
132
133
134 @Test
135 public void testAdd() throws Exception
136 {
137 LdapContext ctxRoot = getContext( "uid=admin,ou=system", service, SUFFIX_DN );
138
139 String dn = "ou=Test";
140
141 Attributes attributes = new BasicAttributes( true );
142 Attribute attribute = new BasicAttribute( "objectClass" );
143 attribute.add( "top" );
144 attribute.add( "organizationalUnit" );
145 attributes.put( attribute );
146 attributes.put( "ou", "Test" );
147
148 DirContext ctx = ctxRoot.createSubcontext( dn, attributes );
149 assertNotNull( ctx );
150
151 SearchControls sc = new SearchControls();
152 sc.setSearchScope( SearchControls.OBJECT_SCOPE );
153
154 NamingEnumeration<SearchResult> ne = ctxRoot.search( dn, "(objectClass=*)", sc );
155 assertTrue( "Search should return at least one entry.", ne.hasMore() );
156
157 SearchResult sr = ne.next();
158 assertEquals( "The entry returned should be the entry added earlier.", dn + "," + SUFFIX_DN, sr.getName() );
159 assertFalse( "Search should return no more entries.", ne.hasMore() );
160 }
161
162
163 @Test
164 public void testModify() throws Exception
165 {
166 LdapContext ctxRoot = getContext( "uid=admin,ou=system", service, SUFFIX_DN );
167
168 String dn = "ou=Test";
169 String description = "New Value";
170
171 Attributes attributes = new BasicAttributes( true );
172 Attribute attribute = new BasicAttribute( "objectClass" );
173 attribute.add( "top" );
174 attribute.add( "organizationalUnit" );
175 attributes.put( attribute );
176 attributes.put( "ou", "Test" );
177 attributes.put( "description", "Old Value" );
178
179 DirContext ctx = ctxRoot.createSubcontext( dn, attributes );
180 assertNotNull( ctx );
181
182 ModificationItem[] mods = new ModificationItem[1];
183 mods[0] = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( "description", description ) );
184
185 ctxRoot.modifyAttributes( dn, mods );
186
187 SearchControls sc = new SearchControls();
188 sc.setSearchScope( SearchControls.OBJECT_SCOPE );
189
190 NamingEnumeration<SearchResult> ne = ctxRoot.search( dn, "(objectClass=*)", sc );
191 assertTrue( "Search should return at least one entry.", ne.hasMore() );
192
193 SearchResult sr = ( SearchResult ) ne.next();
194 assertEquals( "The entry returned should be the entry added earlier.", dn + "," + SUFFIX_DN, sr.getName() );
195
196 attributes = sr.getAttributes();
197 attribute = attributes.get( "description" );
198
199 assertEquals( "The description attribute should contain the new value.", description, attribute.get() );
200 assertFalse( "Search should return no more entries.", ne.hasMore() );
201 }
202
203
204 @Test
205 public void testDelete() throws Exception
206 {
207 LdapContext ctxRoot = getContext( "uid=admin,ou=system", service, SUFFIX_DN );
208
209 String dn = "ou=Test";
210
211 Attributes attributes = new BasicAttributes( true );
212 Attribute attribute = new BasicAttribute( "objectClass" );
213 attribute.add( "top" );
214 attribute.add( "organizationalUnit" );
215 attributes.put( attribute );
216 attributes.put( "ou", "Test" );
217
218 DirContext ctx = ctxRoot.createSubcontext( dn, attributes );
219 assertNotNull( ctx );
220
221 ctxRoot.destroySubcontext( dn );
222
223 SearchControls sc = new SearchControls();
224 sc.setSearchScope( SearchControls.OBJECT_SCOPE );
225
226 try
227 {
228 ctxRoot.search( dn, "(objectClass=*)", sc );
229 fail( "Search should throw exception." );
230 }
231 catch ( LdapNameNotFoundException e )
232 {
233
234 }
235 }
236 }