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