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