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 methods on JNDI contexts that are analogous to entry modify
32   * operations in LDAP.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 264732 $
36   */
37  public class ModifyContextTest extends AbstractAdminTestCase
38  {
39      protected void setUp() throws Exception
40      {
41          super.setUp();
42  
43          try
44          {
45              /*
46               * create ou=testing00,ou=system
47               */
48              Attributes attributes = new BasicAttributes( true );
49              Attribute attribute = new BasicAttribute( "objectClass" );
50              attribute.add( "top" );
51              attribute.add( "organizationalUnit" );
52              attributes.put( attribute );
53              attributes.put( "ou", "testing00" );
54              DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
55              assertNotNull( ctx );
56  
57              ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
58              assertNotNull( ctx );
59  
60              attributes = ctx.getAttributes( "" );
61              assertNotNull( attributes );
62              assertEquals( "testing00", attributes.get( "ou" ).get() );
63              attribute = attributes.get( "objectClass" );
64              assertNotNull( attribute );
65              assertTrue( attribute.contains( "top" ) );
66              assertTrue( attribute.contains( "organizationalUnit" ) );
67  
68              /*
69               * create ou=testing01,ou=system
70               */
71              attributes = new BasicAttributes( true );
72              attribute = new BasicAttribute( "objectClass" );
73              attribute.add( "top" );
74              attribute.add( "organizationalUnit" );
75              attributes.put( attribute );
76              attributes.put( "ou", "testing01" );
77              ctx = sysRoot.createSubcontext( "ou=testing01", attributes );
78              assertNotNull( ctx );
79  
80              ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
81              assertNotNull( ctx );
82  
83              attributes = ctx.getAttributes( "" );
84              assertNotNull( attributes );
85              assertEquals( "testing01", attributes.get( "ou" ).get() );
86              attribute = attributes.get( "objectClass" );
87              assertNotNull( attribute );
88              assertTrue( attribute.contains( "top" ) );
89              assertTrue( attribute.contains( "organizationalUnit" ) );
90  
91              /*
92               * create ou=testing02,ou=system
93               */
94              attributes = new BasicAttributes( true );
95              attribute = new BasicAttribute( "objectClass" );
96              attribute.add( "top" );
97              attribute.add( "organizationalUnit" );
98              attributes.put( attribute );
99              attributes.put( "ou", "testing02" );
100             ctx = sysRoot.createSubcontext( "ou=testing02", attributes );
101             assertNotNull( ctx );
102 
103             ctx = ( DirContext ) sysRoot.lookup( "ou=testing02" );
104             assertNotNull( ctx );
105 
106             attributes = ctx.getAttributes( "" );
107             assertNotNull( attributes );
108             assertEquals( "testing02", attributes.get( "ou" ).get() );
109             attribute = attributes.get( "objectClass" );
110             assertNotNull( attribute );
111             assertTrue( attribute.contains( "top" ) );
112             assertTrue( attribute.contains( "organizationalUnit" ) );
113 
114             /*
115              * create ou=subtest,ou=testing01,ou=system
116              */
117             ctx = ( DirContext ) sysRoot.lookup( "ou=testing01" );
118 
119             attributes = new BasicAttributes( true );
120             attribute = new BasicAttribute( "objectClass" );
121             attribute.add( "top" );
122             attribute.add( "organizationalUnit" );
123             attributes.put( attribute );
124             attributes.put( "ou", "subtest" );
125             ctx = ctx.createSubcontext( "ou=subtest", attributes );
126             assertNotNull( ctx );
127 
128             ctx = ( DirContext ) sysRoot.lookup( "ou=subtest,ou=testing01" );
129             assertNotNull( ctx );
130 
131             attributes = ctx.getAttributes( "" );
132             assertNotNull( attributes );
133             assertEquals( "subtest", attributes.get( "ou" ).get() );
134             attribute = attributes.get( "objectClass" );
135             assertNotNull( attribute );
136             assertTrue( attribute.contains( "top" ) );
137             assertTrue( attribute.contains( "organizationalUnit" ) );
138         }
139         catch( NamingException e )
140         {
141         }
142     }
143 
144 
145     public void testModifyOperation() throws NamingException
146     {
147         Attributes attributes = new BasicAttributes( true );
148         attributes.put( "ou", "testCases" );
149         sysRoot.modifyAttributes( "ou=testing00", DirContext.ADD_ATTRIBUTE, attributes );
150         attributes = null;
151 
152         DirContext ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
153         attributes = ctx.getAttributes( "" );
154         assertTrue( attributes.get( "ou" ).contains( "testCases" ) );
155 
156         Attribute attribute = attributes.get( "creatorsName" );
157         assertNull( attribute );
158 
159         attribute = attributes.get( "createTimestamp" );
160         assertNull( attribute );
161 
162         attribute = attributes.get( "modifiersName" );
163         assertNull( attribute );
164 
165         attributes.get( "modifyTimestamp" );
166         assertNull( attribute );
167     }
168 }