1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * Tests the destroyContext methods of the provider.
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   * @version $Rev: 691024 $
48   */
49  @RunWith ( CiRunner.class )
50  public class DestroyContextIT
51  {
52      public static DirectoryService service;
53  
54      
55      /**
56       * @todo Replace this with an LDIF directive!!!!!!
57       *
58       * @throws NamingException on error
59       */
60      public void createEntries() throws Exception
61      {
62          LdapContext sysRoot = getSystemContext( service );
63  
64          /*
65           * create ou=testing00,ou=system
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           * create ou=testing01,ou=system
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          * create ou=testing02,ou=system
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          * create ou=subtest,ou=testing01,ou=system
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      * Tests the creation and subsequent read of a new JNDI context under the
162      * system context root.
163      *
164      * @throws NamingException if there are failures
165      */
166     @Test
167     public void testDestroyContext() throws Exception
168     {
169         LdapContext sysRoot = getSystemContext( service );
170 
171         createEntries();
172 
173         /*
174          * delete ou=testing00,ou=system
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          * delete ou=subtest,ou=testing01,ou=system
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          * delete ou=testing01,ou=system
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          * delete ou=testing01,ou=system
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 }