1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.server.core.jndi;
21  
22  
23  import org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.server.core.integ.CiRunner;
25  import org.junit.Test;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertEquals;
29  import org.junit.runner.RunWith;
30  
31  import javax.naming.Context;
32  import javax.naming.NamingEnumeration;
33  import javax.naming.NamingException;
34  import javax.naming.directory.Attribute;
35  import javax.naming.directory.Attributes;
36  import javax.naming.directory.BasicAttribute;
37  import javax.naming.directory.BasicAttributes;
38  import javax.naming.directory.DirContext;
39  import javax.naming.directory.InitialDirContext;
40  import javax.naming.directory.ModificationItem;
41  import javax.naming.directory.SearchControls;
42  import javax.naming.directory.SearchResult;
43  
44  import java.util.Hashtable;
45  
46  /**
47   * Tries to demonstrate DIRSERVER-783 ("Adding another value to an attribute
48   * results in the value to be added twice").
49   * 
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  @RunWith ( CiRunner.class )
53  public class DIRSERVER783IT
54  {
55      public static DirectoryService service;
56  
57  
58      /**
59       * Try to add entry with required attribute missing.
60       * 
61       * @throws NamingException if there are errors
62       */
63      @Test
64      public void testAddAnotherValueToAnAttribute() throws NamingException
65      {
66          // create a person without sn
67          Attributes attrs = new BasicAttributes( true );
68          Attribute ocls = new BasicAttribute("objectClass");
69  
70          ocls.add("top");
71          ocls.add("person");
72          attrs.put(ocls);
73          attrs.put("cn", "Fiona Apple");
74          attrs.put("sn", "Apple");
75  
76          String rdn = "cn=Fiona Apple";
77  
78          Hashtable<String,Object> env = new Hashtable<String, Object>();
79          env.put( DirectoryService.JNDI_KEY, service );
80          env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
81          env.put( Context.PROVIDER_URL, "ou=system" );
82  
83          DirContext ctx = new InitialDirContext( env );
84  
85          ctx.createSubcontext( rdn, attrs );
86  
87          // Add the first value for description
88          String description1 = "an American singer-songwriter";
89          Attribute firstDescr = new BasicAttribute( "description", description1 );
90          ModificationItem modification = new ModificationItem(DirContext.ADD_ATTRIBUTE, firstDescr);
91          ctx.modifyAttributes(rdn, new ModificationItem[] { modification });
92  
93          // Add a second value to description
94          String description2 = "Grammy award winning";
95          Attribute otherDescr = new BasicAttribute( "description", description2 );
96  
97          modification = new ModificationItem(DirContext.ADD_ATTRIBUTE, otherDescr );
98          ctx.modifyAttributes(rdn, new ModificationItem[] { modification } );
99        
100         // Add a third value to description
101         String description3 = "MTV Music Award winning";
102         Attribute thirdDescr = new BasicAttribute( "description", description3 );
103 
104         modification = new ModificationItem(DirContext.ADD_ATTRIBUTE, thirdDescr );
105         ctx.modifyAttributes(rdn, new ModificationItem[] { modification });
106 
107         // Search Entry
108         SearchControls sctls = new SearchControls();
109         sctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
110         String filter = '(' + rdn + ')';
111         String base = "";
112 
113         // Check entry
114         NamingEnumeration<SearchResult> enm = ctx.search(base, filter, sctls);
115         assertTrue(enm.hasMore());
116 
117         while (enm.hasMore()) 
118         {
119             SearchResult sr = enm.next();
120             Attribute desc = sr.getAttributes().get("description");
121             assertNotNull(desc);
122             assertTrue(desc.contains(description1));
123             assertTrue(desc.contains(description2));
124             assertTrue(desc.contains(description3));
125             assertEquals(3, desc.size());
126         }
127 
128         // Remove the person entry
129         ctx.destroySubcontext(rdn);
130     }
131 }
132