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;
18  
19  
20  import javax.naming.directory.*;
21  import javax.naming.NamingException;
22  import java.util.Hashtable;
23  
24  
25  /***
26   * Test case to verify DIREVE-216.  Starts up the server binds via SUN JNDI provider
27   * to perform add modify operations on entries.
28   * 
29   * @author szoerner
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev$
32   */
33  public class AddObjectClassesToEntryTest extends AbstractServerTest
34  {
35      private static final String RDN = "cn=The Person";
36  
37      private DirContext ctx = null;
38  
39  
40      /***
41       * Create an entry for a person.
42       */
43      public void setUp() throws Exception
44      {
45          super.setUp();
46  
47          Hashtable env = new Hashtable();
48          env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
49          env.put( "java.naming.provider.url", "ldap://localhost:" + port + "/ou=system" );
50          env.put( "java.naming.security.principal", "uid=admin,ou=system" );
51          env.put( "java.naming.security.credentials", "secret" );
52          env.put( "java.naming.security.authentication", "simple" );
53          ctx = new InitialDirContext( env );
54  
55          // Create a person
56          Attributes attributes = new BasicAttributes( true );
57          Attribute attribute = new BasicAttribute( "objectClass" );
58          attribute.add( "top" );
59          attribute.add( "person" );
60          attributes.put( attribute );
61          attributes.put( "cn", "The Person" );
62          attributes.put( "sn", "Person" );
63          attributes.put( "description", "this is a person" );
64          DirContext person = ctx.createSubcontext( RDN, attributes );
65  
66          assertNotNull( person );
67      }
68  
69  
70      /***
71       * Remove the person.
72       */
73      public void tearDown() throws Exception
74      {
75          ctx.unbind(RDN);
76          ctx.close();
77          ctx = null;
78          super.tearDown();
79      }
80  
81  
82      /***
83       * Just a little test to check wether the person is created correctly after
84       * setup.
85       * 
86       * @throws NamingException
87       */
88      public void testSetUpTearDown() throws NamingException
89      {
90          DirContext person = (DirContext) ctx.lookup(RDN);
91          assertNotNull(person);
92  
93          // Check object classes
94  
95          Attributes attributes = person.getAttributes("");
96          Attribute ocls = attributes.get("objectClass");
97  
98          String[] expectedOcls = { "top", "person" };
99          for ( int i = 0; i < expectedOcls.length; i++ )
100         {
101             String name = expectedOcls[i];
102             assertTrue( "object class " + name + " is NOT present when it should be!",
103                     ocls.contains( name ) );
104         }
105     }
106 
107 
108     /***
109      * This is the original defect as in JIRA DIREVE-216.
110      * 
111      * @throws NamingException
112      */
113     public void testAddObjectClasses() throws NamingException
114     {
115 
116         // modify object classes, add two more
117         Attributes attributes = new BasicAttributes( true );
118         Attribute ocls = new BasicAttribute("objectClass");
119         ocls.add("organizationalPerson");
120         ocls.add("inetOrgPerson");
121         attributes.put(ocls);
122 
123         DirContext person = (DirContext) ctx.lookup(RDN);
124         person.modifyAttributes("", DirContext.ADD_ATTRIBUTE, attributes);
125 
126         // Read again from directory
127         person = (DirContext) ctx.lookup(RDN);
128         attributes = person.getAttributes("");
129         Attribute newOcls = attributes.get("objectClass");
130 
131         String[] expectedOcls = { "top", "person", "organizationalPerson",
132                 "inetOrgPerson" };
133         for (int i = 0; i < expectedOcls.length; i++) {
134             String name = expectedOcls[i];
135             assertTrue("object class " + name + " is present", newOcls
136                     .contains(name));
137         }
138     }
139 
140 
141     /***
142      * This changes a single attribute value. Just as a reference.
143      * 
144      * @throws NamingException
145      */
146     public void testModifyDescription() throws NamingException
147     {
148         String newDescription = "More info on the user ...";
149 
150         // modify object classes, add two more
151         Attributes attributes = new BasicAttributes( true );
152         Attribute desc = new BasicAttribute("description", newDescription);
153         attributes.put(desc);
154 
155         DirContext person = (DirContext) ctx.lookup(RDN);
156         person.modifyAttributes("", DirContext.REPLACE_ATTRIBUTE, attributes);
157 
158         // Read again from directory
159         person = (DirContext) ctx.lookup(RDN);
160         attributes = person.getAttributes("");
161         Attribute newDesc = attributes.get("description");
162 
163         assertTrue("new Description", newDesc.contains(newDescription));
164     }
165 }