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.DirectoryService;
24 import org.apache.directory.server.core.integ.CiRunner;
25 import static org.apache.directory.server.core.integ.IntegrationUtils.getSystemContext;
26 import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33
34 import javax.naming.NamingException;
35 import javax.naming.directory.Attribute;
36 import javax.naming.directory.Attributes;
37 import javax.naming.directory.BasicAttribute;
38 import javax.naming.directory.BasicAttributes;
39 import javax.naming.directory.DirContext;
40 import javax.naming.ldap.LdapContext;
41
42
43
44
45
46
47
48
49 @RunWith ( CiRunner.class )
50 public class DestroyContextIT
51 {
52 public static DirectoryService service;
53
54
55
56
57
58
59
60 public void createEntries() throws Exception
61 {
62 LdapContext sysRoot = getSystemContext( service );
63
64
65
66
67 Attributes attributes = new BasicAttributes( true );
68 Attribute attribute = new BasicAttribute( "objectClass" );
69 attribute.add( "top" );
70 attribute.add( "organizationalUnit" );
71 attributes.put( attribute );
72 attributes.put( "ou", "testing00" );
73 DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
74 assertNotNull( ctx );
75
76 ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
77 assertNotNull( ctx );
78
79 attributes = ctx.getAttributes( "" );
80 assertNotNull( attributes );
81 assertEquals( "testing00", attributes.get( "ou" ).get() );
82 attribute = attributes.get( "objectClass" );
83 assertNotNull( attribute );
84 assertTrue( attribute.contains( "top" ) );
85 assertTrue( attribute.contains( "organizationalUnit" ) );
86
87
88
89
90 attributes = new BasicAttributes( true );
91 attribute = new BasicAttribute( "objectClass" );
92 attribute.add( "top" );
93 attribute.add( "organizationalUnit" );
94 attributes.put( attribute );
95 attributes.put( "ou", "testing01" );
96 ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
97 assertNotNull( ctx );
98
99 ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
100 assertNotNull( ctx );
101
102 attributes = ctx.getAttributes( "" );
103 assertNotNull( attributes );
104 assertEquals( "testing01", attributes.get( "ou" ).get() );
105 attribute = attributes.get( "objectClass" );
106 assertNotNull( attribute );
107 assertTrue( attribute.contains( "top" ) );
108 assertTrue( attribute.contains( "organizationalUnit" ) );
109
110
111
112
113 attributes = new BasicAttributes( true );
114 attribute = new BasicAttribute( "objectClass" );
115 attribute.add( "top" );
116 attribute.add( "organizationalUnit" );
117 attributes.put( attribute );
118 attributes.put( "ou", "testing02" );
119 ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
120 assertNotNull( ctx );
121
122 ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
123 assertNotNull( ctx );
124
125 attributes = ctx.getAttributes( "" );
126 assertNotNull( attributes );
127 assertEquals( "testing02", attributes.get( "ou" ).get() );
128 attribute = attributes.get( "objectClass" );
129 assertNotNull( attribute );
130 assertTrue( attribute.contains( "top" ) );
131 assertTrue( attribute.contains( "organizationalUnit" ) );
132
133
134
135
136 ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
137
138 attributes = new BasicAttributes( true );
139 attribute = new BasicAttribute( "objectClass" );
140 attribute.add( "top" );
141 attribute.add( "organizationalUnit" );
142 attributes.put( attribute );
143 attributes.put( "ou", "subtest" );
144 ctx = ctx.createSubcontext( "ou=subtest", attributes );
145 assertNotNull( ctx );
146
147 ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
148 assertNotNull( ctx );
149
150 attributes = ctx.getAttributes( "" );
151 assertNotNull( attributes );
152 assertEquals( "subtest", attributes.get( "ou" ).get() );
153 attribute = attributes.get( "objectClass" );
154 assertNotNull( attribute );
155 assertTrue( attribute.contains( "top" ) );
156 assertTrue( attribute.contains( "organizationalUnit" ) );
157 }
158
159
160
161
162
163
164
165
166 @Test
167 public void testDestroyContext() throws Exception
168 {
169 LdapContext sysRoot = getSystemContext( service );
170
171 createEntries();
172
173
174
175
176 sysRoot.destroySubcontext( "ou=testing00" );
177
178 try
179 {
180 sysRoot.lookup( "ou=testing00" );
181 fail( "ou=testing00, ou=system should not exist" );
182 }
183 catch ( NamingException e )
184 {
185 assertTrue( e instanceof LdapNameNotFoundException );
186 }
187
188
189
190
191 sysRoot.destroySubcontext( "ou=subtest,ou=testing01" );
192
193 try
194 {
195 sysRoot.lookup( "ou=subtest,ou=testing01" );
196 fail( "ou=subtest,ou=testing01,ou=system should not exist" );
197 }
198 catch ( NamingException e )
199 {
200 assertTrue( e instanceof LdapNameNotFoundException );
201 }
202
203
204
205
206 sysRoot.destroySubcontext( "ou=testing01" );
207
208 try
209 {
210 sysRoot.lookup( "ou=testing01" );
211 fail( "ou=testing01, ou=system should not exist" );
212 }
213 catch ( NamingException e )
214 {
215 assertTrue( e instanceof LdapNameNotFoundException );
216 }
217
218
219
220
221 sysRoot.destroySubcontext( "ou=testing02" );
222
223 try
224 {
225 sysRoot.lookup( "ou=testing02" );
226 fail( "ou=testing02, ou=system should not exist" );
227 }
228 catch ( NamingException e )
229 {
230 assertTrue( e instanceof LdapNameNotFoundException );
231 }
232 }
233
234 }