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