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 org.apache.ldap.common.exception.LdapNameNotFoundException;
21  import org.apache.ldap.server.AbstractCoreTest;
22  
23  import javax.naming.NamingException;
24  import javax.naming.directory.*;
25  
26  
27  /***
28   * Tests the destroyContext methods of the provider.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev: 165059 $
32   */
33  public class DestroyContextTest extends AbstractCoreTest
34  {
35      protected void setUp() throws Exception
36      {
37          super.setUp();
38  
39          /*
40           * create ou=testing00,ou=system
41           */
42          Attributes attributes = new BasicAttributes();
43          Attribute attribute = new BasicAttribute( "objectClass" );
44          attribute.add( "top" );
45          attribute.add( "organizationalUnit" );
46          attributes.put( attribute );
47          attributes.put( "ou", "testing00" );
48          DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
49          assertNotNull( ctx );
50  
51          ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
52          assertNotNull( ctx );
53  
54          attributes = ctx.getAttributes( "" );
55          assertNotNull( attributes );
56          assertEquals( "testing00", attributes.get( "ou" ).get() );
57          attribute = attributes.get( "objectClass" );
58          assertNotNull( attribute );
59          assertTrue( attribute.contains( "top" ) );
60          assertTrue( attribute.contains( "organizationalUnit" ) );
61  
62          /*
63           * create ou=testing01,ou=system
64           */
65          attributes = new BasicAttributes();
66          attribute = new BasicAttribute( "objectClass" );
67          attribute.add( "top" );
68          attribute.add( "organizationalUnit" );
69          attributes.put( attribute );
70          attributes.put( "ou", "testing01" );
71          ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
72          assertNotNull( ctx );
73  
74          ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
75          assertNotNull( ctx );
76  
77          attributes = ctx.getAttributes( "" );
78          assertNotNull( attributes );
79          assertEquals( "testing01", attributes.get( "ou" ).get() );
80          attribute = attributes.get( "objectClass" );
81          assertNotNull( attribute );
82          assertTrue( attribute.contains( "top" ) );
83          assertTrue( attribute.contains( "organizationalUnit" ) );
84  
85          /*
86           * create ou=testing02,ou=system
87           */
88          attributes = new BasicAttributes();
89          attribute = new BasicAttribute( "objectClass" );
90          attribute.add( "top" );
91          attribute.add( "organizationalUnit" );
92          attributes.put( attribute );
93          attributes.put( "ou", "testing02" );
94          ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
95          assertNotNull( ctx );
96  
97          ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
98          assertNotNull( ctx );
99  
100         attributes = ctx.getAttributes( "" );
101         assertNotNull( attributes );
102         assertEquals( "testing02", attributes.get( "ou" ).get() );
103         attribute = attributes.get( "objectClass" );
104         assertNotNull( attribute );
105         assertTrue( attribute.contains( "top" ) );
106         assertTrue( attribute.contains( "organizationalUnit" ) );
107 
108         /*
109          * create ou=subtest,ou=testing01,ou=system
110          */
111         ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
112 
113         attributes = new BasicAttributes();
114         attribute = new BasicAttribute( "objectClass" );
115         attribute.add( "top" );
116         attribute.add( "organizationalUnit" );
117         attributes.put( attribute );
118         attributes.put( "ou", "subtest" );
119         ctx = ctx.createSubcontext( "ou=subtest", attributes );
120         assertNotNull( ctx );
121 
122         ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
123         assertNotNull( ctx );
124 
125         attributes = ctx.getAttributes( "" );
126         assertNotNull( attributes );
127         assertEquals( "subtest", 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      * Tests the creation and subsequent read of a new JNDI context under the
137      * system context root.
138      *
139      * @throws NamingException if there are failures
140      */
141     public void testDestroyContext() throws NamingException
142     {
143         /*
144          * delete ou=testing00,ou=system
145          */
146         sysRoot.destroySubcontext( "ou=testing00");
147 
148         try
149         {
150             sysRoot.lookup( "ou=testing00" );
151             fail( "ou=testing00, ou=system should not exist" );
152         }
153         catch( NamingException e )
154         {
155             assertTrue( e instanceof LdapNameNotFoundException );
156         }
157 
158         /*
159          * delete ou=subtest,ou=testing01,ou=system
160          */
161         sysRoot.destroySubcontext( "ou=subtest,ou=testing01");
162 
163         try
164         {
165             sysRoot.lookup( "ou=subtest,ou=testing01" );
166             fail( "ou=subtest,ou=testing01,ou=system should not exist" );
167         }
168         catch( NamingException e )
169         {
170             assertTrue( e instanceof LdapNameNotFoundException );
171         }
172 
173         /*
174          * delete ou=testing01,ou=system
175          */
176         sysRoot.destroySubcontext( "ou=testing01");
177 
178         try
179         {
180             sysRoot.lookup( "ou=testing01" );
181             fail( "ou=testing01, ou=system should not exist" );
182         }
183         catch( NamingException e )
184         {
185             assertTrue( e instanceof LdapNameNotFoundException );
186         }
187 
188 
189         /*
190          * delete ou=testing01,ou=system
191          */
192         sysRoot.destroySubcontext( "ou=testing02");
193 
194         try
195         {
196             sysRoot.lookup( "ou=testing02" );
197             fail( "ou=testing02, ou=system should not exist" );
198         }
199         catch( NamingException e )
200         {
201             assertTrue( e instanceof LdapNameNotFoundException );
202         }
203     }
204 
205 
206 }