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.entry;
21  
22  import java.util.Arrays;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Set;
27  
28  import javax.naming.InvalidNameException;
29  import javax.naming.NamingEnumeration;
30  import javax.naming.NamingException;
31  import javax.naming.directory.Attributes;
32  import javax.naming.directory.BasicAttributes;
33  import javax.naming.directory.NoSuchAttributeException;
34  
35  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
36  import org.apache.directory.server.schema.bootstrap.ApachemetaSchema;
37  import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
38  import org.apache.directory.server.schema.bootstrap.CoreSchema;
39  import org.apache.directory.server.schema.bootstrap.CosineSchema;
40  import org.apache.directory.server.schema.bootstrap.InetorgpersonSchema;
41  import org.apache.directory.server.schema.bootstrap.Schema;
42  import org.apache.directory.server.schema.bootstrap.SystemSchema;
43  import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
44  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
45  import org.apache.directory.server.schema.registries.DefaultRegistries;
46  import org.apache.directory.server.schema.registries.OidRegistry;
47  import org.apache.directory.server.schema.registries.Registries;
48  import org.apache.directory.shared.ldap.constants.SchemaConstants;
49  import org.apache.directory.shared.ldap.entry.Entry;
50  import org.apache.directory.shared.ldap.entry.EntryAttribute;
51  import org.apache.directory.shared.ldap.entry.Value;
52  import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
53  import org.apache.directory.shared.ldap.entry.client.ClientEntry;
54  import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
55  import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
56  import org.apache.directory.shared.ldap.name.LdapDN;
57  import org.apache.directory.shared.ldap.schema.AttributeType;
58  import org.apache.directory.shared.ldap.util.StringTools;
59  
60  import static org.junit.Assert.assertEquals;
61  import static org.junit.Assert.assertFalse;
62  import static org.junit.Assert.assertNotNull;
63  import static org.junit.Assert.assertNotSame;
64  import static org.junit.Assert.assertNull;
65  import static org.junit.Assert.assertTrue;
66  import static org.junit.Assert.fail;
67  
68  import org.junit.BeforeClass;
69  import org.junit.Test;
70  
71  
72  /**
73   * Test the DefaultServerEntry class.
74   *
75   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
76   * @version $Rev$, $Date$
77   */
78  public class DefaultServerEntryTest
79  {
80      private static final byte[] BYTES1 = new byte[]{ 'a', 'b' };
81      private static final byte[] BYTES2 = new byte[]{ 'b' };
82      private static final byte[] BYTES3 = new byte[]{ 'c' };
83  
84      private static BootstrapSchemaLoader loader;
85      private static Registries registries;
86      private static AttributeTypeRegistry atr;
87      private static OidRegistry oidRegistry;
88      
89      private static AttributeType atObjectClass;
90      private static AttributeType atCN;
91      private static AttributeType atSN;
92      private static AttributeType atC;   
93      private static AttributeType atL;   
94      private static AttributeType atOC;   
95      
96      // A Binary attribute
97      private static AttributeType atPwd;
98  
99      private static LdapDN EXAMPLE_DN;
100     
101     /**
102      * Initialize the registries once for the whole test suite
103      */
104     @BeforeClass
105     public static void setup() throws Exception
106     {
107         loader = new BootstrapSchemaLoader();
108         oidRegistry = new DefaultOidRegistry();
109         registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
110         
111         // load essential bootstrap schemas 
112         Set<Schema> bootstrapSchemas = new HashSet<Schema>();
113         bootstrapSchemas.add( new ApachemetaSchema() );
114         bootstrapSchemas.add( new ApacheSchema() );
115         bootstrapSchemas.add( new CoreSchema() );
116         bootstrapSchemas.add( new SystemSchema() );
117         bootstrapSchemas.add( new InetorgpersonSchema() );
118         bootstrapSchemas.add( new CosineSchema() );
119         loader.loadWithDependencies( bootstrapSchemas, registries );
120 
121         atr = registries.getAttributeTypeRegistry();
122 
123         atObjectClass = registries.getAttributeTypeRegistry().lookup( "objectClass" );
124         atCN = registries.getAttributeTypeRegistry().lookup( "cn" );
125         atC = registries.getAttributeTypeRegistry().lookup( "c" );
126         atL = registries.getAttributeTypeRegistry().lookup( "l" );
127         atOC = registries.getAttributeTypeRegistry().lookup( "objectClass" );
128         atSN = registries.getAttributeTypeRegistry().lookup( "sn" );
129         atPwd = registries.getAttributeTypeRegistry().lookup( "userpassword" );
130         
131         EXAMPLE_DN = new LdapDN( "dc=example,dc=com" );
132     }
133 
134 
135     //-------------------------------------------------------------------------
136     // Test the Constructors
137     //-------------------------------------------------------------------------
138     /**
139      * Test for method DefaultServerEntry()
140      */
141     @Test
142     public void testDefaultServerEntry() throws Exception
143     {
144         Entry entry = new DefaultServerEntry();
145         assertNotNull( entry );
146         assertEquals( LdapDN.EMPTY_LDAPDN, entry.getDn() );
147         assertEquals( 0, entry.size() );
148     }
149     
150     
151     /**
152      * Test for method DefaultServerEntry( registries )
153      */
154     @Test
155     public void testDefaultServerEntryRegistries() throws Exception
156     {
157         Entry entry = new DefaultServerEntry( registries );
158         assertNotNull( entry );
159         assertEquals( LdapDN.EMPTY_LDAPDN, entry.getDn() );
160         assertEquals( 0, entry.size() );
161     }
162     
163     
164     /**
165      * Test for method DefaultServerEntry( registries, LdapDN )
166      */
167     @Test
168     public void testDefaultServerEntryRegistriesDN() throws Exception
169     {
170         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
171         assertNotNull( entry );
172         assertEquals( EXAMPLE_DN, entry.getDn() );
173         assertEquals( 0, entry.size() );
174     }
175     
176     
177     /**
178      * Test for method DefaultServerEntry( registries, LdapDN, AttributeType... )
179      */
180     @Test
181     public void testDefaultServerEntryRegistriesDNAttributeTypeArray() throws Exception
182     {
183         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN, atOC, atPwd, atCN );
184         assertNotNull( entry );
185         assertEquals( EXAMPLE_DN, entry.getDn() );
186         assertEquals( 3, entry.size() );
187         assertTrue( entry.containsAttribute( atOC ) );
188         assertTrue( entry.containsAttribute( atPwd ) );
189         assertTrue( entry.containsAttribute( atCN ) );
190     }
191     
192     
193     /**
194      * Test for method DefaultServerEntry( registries, LdapDN, AttributeType, upId )
195      */
196     @Test
197     public void testDefaultServerEntryRegistriesDNAttributeTypeUpId() throws Exception
198     {
199         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN, atOC, "  OBJECTCLASS  " );
200         assertNotNull( entry );
201         assertEquals( EXAMPLE_DN, entry.getDn() );
202         assertEquals( 1, entry.size() );
203         assertTrue( entry.containsAttribute( atOC ) );
204         assertEquals( "objectclass", entry.get( atOC ).getId() );
205         assertEquals( "OBJECTCLASS", entry.get( atOC ).getUpId() );
206     }
207     
208     
209     /**
210      * Test for method DefaultServerEntry( registries, LdapDN, AttributeType, upId )
211      */
212     @Test
213     public void testDefaultServerEntryRegistriesDNUpIdArray() throws Exception
214     {
215         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN, "  OBJECTCLASS  ", " Cn " );
216         assertNotNull( entry );
217         assertEquals( EXAMPLE_DN, entry.getDn() );
218         assertEquals( 2, entry.size() );
219         assertTrue( entry.containsAttribute( "objectClass" ) );
220         assertEquals( "objectclass", entry.get( atOC ).getId() );
221         assertEquals( "OBJECTCLASS", entry.get( atOC ).getUpId() );
222         assertTrue( entry.containsAttribute( "2.5.4.3" ) );
223         assertEquals( "cn", entry.get( atCN ).getId() );
224         assertEquals( "Cn", entry.get( atCN ).getUpId() );
225     }
226     
227     
228     //-------------------------------------------------------------------------
229     // Test the Add methods
230     //-------------------------------------------------------------------------
231     /**
232      * Test for method add( EntryAttribute...)
233      */
234     @Test
235     public void testAddEntryAttribute() throws Exception
236     {
237         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
238         
239         EntryAttribute oc = new DefaultServerAttribute( atObjectClass, "top", "person" );
240         EntryAttribute cn = new DefaultServerAttribute( atCN, "test1", "test2" );
241         EntryAttribute sn = new DefaultServerAttribute( atSN, "Test1", "Test2" );
242         EntryAttribute up = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
243         EntryAttribute c = new DefaultServerAttribute( atC, "FR", "US" );
244         
245         entry.add( oc, cn, sn, c );
246         
247         assertEquals( 4, entry.size() );
248         assertTrue( entry.containsAttribute( "ObjectClass" ) );
249         assertTrue( entry.containsAttribute( "CN" ) );
250         assertTrue( entry.containsAttribute( "  sn  " ) );
251         assertTrue( entry.containsAttribute( " countryName  " ) );
252     
253         EntryAttribute attr = entry.get( "objectclass" );
254         assertEquals( 2, attr.size() );
255         
256         EntryAttribute c2 = new DefaultServerAttribute( atC, "UK", "DE" );
257         entry.add( c2, up );
258         assertEquals( 5, entry.size() );
259         
260         assertTrue( entry.containsAttribute( "userPassword" ) );
261         assertTrue( entry.containsAttribute( " countryName " ) );
262 
263         EntryAttribute attrC = entry.get( "countryName" );
264         assertEquals( 4, attrC.size() );
265         
266         entry.clear();
267     }
268 
269     
270     /**
271      * Test for method add( String, byte[]...)
272      */
273     @Test
274     public void testAddStringByteArrayArray() throws Exception
275     {
276         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
277         
278         entry.add( "userPassword", (byte[])null );
279         assertEquals( 1, entry.size() );
280         EntryAttribute attributePWD = entry.get( "userPassword" );
281         assertEquals( 1, attributePWD.size() );
282         assertNotNull( attributePWD.get() );
283         assertNull( attributePWD.get().get() );
284         
285         entry.clear();
286         
287         entry.add( "jpegPhoto", BYTES1, BYTES1, BYTES2 );
288         assertEquals( 1, entry.size() );
289         EntryAttribute attributeJPG = entry.get( "jpegPhoto" );
290         assertEquals( 2, attributeJPG.size() );
291         assertNotNull( attributeJPG.get() );
292         assertTrue( attributeJPG.contains( BYTES1 ) );
293         assertTrue( attributeJPG.contains( BYTES2 ) );
294         
295         entry.clear();
296         
297         try
298         {
299             // Cannot add an attribute which does not exist
300             entry.add( "wrongAT", BYTES1, BYTES2 );
301             fail();
302          }
303          catch ( NoSuchAttributeException nsae )
304          {
305              assertTrue( true );
306          }
307 
308          // Cannot add String values into a binary attribute
309          entry.add( "jpegPhoto", "test", "test2" );
310          assertEquals( 0, entry.get( "jpegPhoto" ).size() );
311     }
312      
313 
314     /**
315      * Test for method add( String, String...)
316      */
317     @Test
318     public void testAddStringStringArray() throws Exception
319     {
320         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
321         
322         entry.add( "cn", (String)null );
323         assertEquals( 1, entry.size() );
324         EntryAttribute attributeCN = entry.get( "cn" );
325         
326         assertEquals( 1, attributeCN.size() );
327         assertNotNull( attributeCN.get() );
328         assertNull( attributeCN.get().get() );
329          
330         entry.add( "sn", "test", "test", "TEST" );
331         assertEquals( 2, entry.size() );
332         EntryAttribute attributeSN = entry.get( "sn" );
333          
334         // 'TEST' and 'test' are the same value for 'sn' (this is a case insensitive attributeType)
335         assertEquals( 1, attributeSN.size() );
336         assertNotNull( attributeSN.get() );
337         assertTrue( attributeSN.contains( "test" ) );
338         assertTrue( attributeSN.contains( "TEST" ) );
339          
340         entry.clear();
341 
342         try
343         {
344             // Cannot add an attribute which does not exist
345             entry.add( "wrongAT", "wrong", "wrong" );
346             fail();
347         }
348         catch ( NoSuchAttributeException nsae )
349         {
350             assertTrue( true );
351         }
352 
353         // Cannot add binary values into a String attribute
354         entry.add( "sn",BYTES1, BYTES2 );
355         assertEquals( 0, entry.get( "sn" ).size() );
356     }
357      
358 
359     /**
360      * Test for method add( String, Value<?>...)
361      */
362     @Test
363     public void testAddStringValueArray() throws Exception
364     {
365         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
366         Value<String> value = new ServerStringValue( atCN, (String)null );
367         
368         entry.add( "cn", value );
369         assertEquals( 1, entry.size() );
370         EntryAttribute attributeCN = entry.get( "cn" );
371         assertEquals( 1, attributeCN.size() );
372         assertNotNull( attributeCN.get() );
373         assertNull( attributeCN.get().get() );
374          
375         Value<String> value1 = new ServerStringValue( atCN, "test1" );
376         Value<String> value2 = new ServerStringValue( atCN, "test2" );
377         Value<String> value3 = new ServerStringValue( atCN, "test1" );
378 
379         entry.add( "sn", value1, value2, value3 );
380         assertEquals( 2, entry.size() );
381         EntryAttribute attributeSN = entry.get( "sn" );
382         assertEquals( 2, attributeSN.size() );
383         assertNotNull( attributeSN.get() );
384         assertTrue( attributeSN.contains( value1 ) );
385         assertTrue( attributeSN.contains( value2 ) );
386          
387         Value<byte[]> value4 = new ServerBinaryValue( atPwd, BYTES1 );
388         entry.add( "l", value1, value4 );
389         assertEquals( 3, entry.size() );
390         EntryAttribute attributeL = entry.get( "l" );
391          
392         // Cannot store a binary value in a String attribute
393         assertEquals( 1, attributeL.size() );
394         assertNotNull( attributeL.get() );
395         assertTrue( attributeL.contains( value1 ) );
396 
397         entry.clear();
398 
399         try
400         {
401             // Cannot add an attribute which does not exist
402             entry.add( "wrongAT", value1, value2 );
403             fail();
404         }
405         catch ( NoSuchAttributeException nsae )
406         {
407             assertTrue( true );
408         }
409     }
410 
411 
412     /**
413      * Test method for add( AttributeType, byte[]... )
414      */
415     @Test
416     public void testAddAttributeTypeByteArrayArray() throws Exception
417     {
418         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
419         
420         entry.add( atPwd, BYTES1, BYTES2 );
421         assertEquals( 1, entry.size() );
422         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
423         
424         entry.add( atPwd, (byte[])null, BYTES1 );
425         assertEquals( 1, entry.size() );
426         
427         EntryAttribute attribute = entry.get( atPwd );
428         assertEquals( 3, attribute.size() );
429         assertTrue( attribute.contains( BYTES1 ) );
430         assertTrue( attribute.contains( BYTES2 ) );
431         assertTrue( attribute.contains( (byte[])null ) );
432     }
433     
434      
435     /**
436      * Test method for add( AttributeType, String... )
437      */
438     @Test
439     public void testAddAttributeTypeStringArray() throws Exception
440     {
441         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
442         
443         entry.add( atC, "us", "fr" );
444         assertEquals( 1, entry.size() );
445         assertTrue( entry.contains( atC, "fr", "us" ) );
446         
447         entry.add( atC, (String)null, "de", "fr" );
448         assertEquals( 1, entry.size() );
449         
450         EntryAttribute attribute = entry.get( atC );
451         assertEquals( 4, attribute.size() );
452         assertTrue( attribute.contains( "de" ) );
453         assertTrue( attribute.contains( "fr" ) );
454         assertTrue( attribute.contains( (String)null ) );
455         assertTrue( attribute.contains( "us" ) );
456         
457         entry.clear();
458         
459         assertEquals( 0, entry.size() );
460     }
461     
462      
463     /**
464      * Test method for add( AttributeType, Value<?>... )
465      */
466     @Test
467     public void testAddAttributeTypeValueArray() throws Exception
468     {
469         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
470         
471         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
472         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
473         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
474         Value<String> strNullValue = new ServerStringValue( atCN, null);
475 
476         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
477         Value<byte[]> binValue2 = new ServerBinaryValue( atPwd, BYTES2 );
478         Value<byte[]> binValue3 = new ServerBinaryValue( atPwd, BYTES3 );
479         
480         try
481         {
482             entry.add( (AttributeType)null, strValue1 );
483             fail();
484         }
485         catch( IllegalArgumentException iae )
486         {
487             assertTrue( true );
488         }
489         
490         entry.add( atCN, strValue1, strValue2, strValue1 );
491         entry.add( atPwd, binValue1, binValue2, binValue1 );
492         
493         assertEquals( 2, entry.size() );
494         assertTrue( entry.contains( atCN, "test1", "test2" ) );
495         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
496         
497         entry.add( atCN, strValue3, strNullValue );
498         
499         assertEquals( 4, entry.get( atCN ).size() );
500         assertTrue( entry.contains( atCN, strNullValue ) );
501         
502         entry.add( atCN, binValue3 );
503         assertFalse( entry.contains( atCN, binValue3 ) );
504     }
505     
506     
507 
508 
509     /**
510      * Test method for add( String, AttributeType, byte[]... )
511      */
512     @Test
513     public void testAddStringAttributeTypeByteArrayArray() throws Exception
514     {
515         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
516         
517         entry.add( "UserPassword", atPwd, BYTES1, BYTES2 );
518         assertEquals( 1, entry.size() );
519         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
520         assertEquals( "UserPassword", entry.get( atPwd ).getUpId() );
521         assertEquals( "userpassword", entry.get( atPwd ).getId() );
522         
523         entry.add( "  UserPassword  ", atPwd, (byte[])null, BYTES1 );
524         assertEquals( 1, entry.size() );
525         
526         EntryAttribute attribute = entry.get( atPwd );
527         assertEquals( 3, attribute.size() );
528         assertTrue( attribute.contains( BYTES1 ) );
529         assertTrue( attribute.contains( BYTES2 ) );
530         assertTrue( attribute.contains( (byte[])null ) );
531         assertEquals( "UserPassword", attribute.getUpId() );
532         assertEquals( "userpassword", attribute.getId() );
533 
534         try
535         {
536             entry.add( "  ObjectClass  ", atOC, BYTES1 );
537             fail();
538         }
539         catch( UnsupportedOperationException uoe )
540         {
541             assertTrue( true );
542         }
543     }
544     
545      
546     /**
547      * Test method for add( String, AttributeType, String... )
548      */
549     @Test
550     public void testAddStringAttributeTypeStringArray() throws Exception
551     {
552         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
553         
554         entry.add( "CommonName", atCN, "test1", "test2" );
555         assertEquals( 1, entry.size() );
556         assertTrue( entry.contains( atCN, "test1", "test2" ) );
557         assertEquals( "CommonName", entry.get( atCN ).getUpId() );
558         assertEquals( "commonname", entry.get( atCN ).getId() );
559         
560         entry.add( "  CN  ", atCN, (String)null, "test1" );
561         assertEquals( 1, entry.size() );
562         
563         EntryAttribute attribute = entry.get( atCN );
564         assertEquals( 3, attribute.size() );
565         assertTrue( attribute.contains( "test1" ) );
566         assertTrue( attribute.contains( (String)null ) );
567         assertTrue( attribute.contains( "test2" ) );
568         assertEquals( "CN", attribute.getUpId() );
569         assertEquals( "cn", attribute.getId() );
570 
571         entry.clear();
572         
573         // Binary values are not allowed
574         entry.add( "  CN  ", atCN, BYTES1 );
575         assertEquals( 1, entry.size() );
576         assertNotNull( entry.get( atCN ) );
577         assertEquals( 0, entry.get( atCN ).size() );
578     }
579     
580      
581     /**
582      * Test method for add( String, AttributeType, Value<?>... )
583      */
584     @Test
585     public void testAddStringAttributeTypeValueArray() throws Exception
586     {
587         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
588         
589         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
590         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
591         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
592         Value<String> strNullValue = new ServerStringValue( atCN, null);
593 
594         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
595         Value<byte[]> binValue2 = new ServerBinaryValue( atPwd, BYTES2 );
596         Value<byte[]> binValue3 = new ServerBinaryValue( atPwd, BYTES3 );
597         
598         try
599         {
600             entry.add( "cn", (AttributeType)null, strValue1 );
601             fail();
602         }
603         catch( IllegalArgumentException iae )
604         {
605             assertTrue( true );
606         }
607         
608         entry.add( "CN", atCN, strValue1, strValue2, strValue1 );
609         entry.add( "UserPassword", atPwd, binValue1, binValue2, binValue1 );
610         
611         assertEquals( 2, entry.size() );
612         assertTrue( entry.contains( atCN, "test1", "test2" ) );
613         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
614         assertEquals( "CN", entry.get( atCN ).getUpId() );
615         assertEquals( "cn", entry.get( atCN ).getId() );
616         assertEquals( "UserPassword", entry.get( atPwd ).getUpId() );
617         assertEquals( "userpassword", entry.get( atPwd ).getId() );
618         
619         entry.add( "CN", atCN, strValue3, strNullValue );
620         
621         assertEquals( 4, entry.get( atCN ).size() );
622         assertTrue( entry.contains( atCN, strNullValue ) );
623         
624         entry.add( atCN, binValue3 );
625         assertFalse( entry.contains( atCN, binValue3 ) );
626         
627         try
628         {
629             entry.add( "SN", atCN, "test" );
630             fail();
631         }
632         catch ( IllegalArgumentException iae )
633         {
634             assertTrue( true );
635         }
636     }
637     
638     
639     /**
640      * Test the add( AT, String... ) method
641      */
642     @Test public void testAddAtStringElipsis() throws Exception
643     {
644         LdapDN dn = new LdapDN( "cn=test" );
645         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
646         
647         // Test a simple addition
648         entry.add( atCN, "test1" );
649         assertNotNull( entry.get( atCN ) );
650         assertEquals( 1, entry.get( atCN ).size() );
651         assertEquals( "test1", entry.get( atCN ).get().get() );
652         
653         // Test some more addition
654         entry.add( atCN, "test2", "test3" );
655         assertNotNull( entry.get( atCN ) );
656         assertEquals( 3, entry.get( atCN ).size() );
657         assertTrue( entry.contains( atCN, "test1" ) );
658         assertTrue( entry.contains( atCN, "test2" ) );
659         assertTrue( entry.contains( atCN, "test3" ) );
660         
661         // Test some addition of existing values
662         entry.add( atCN, "test2" );
663         assertNotNull( entry.get( atCN ) );
664         assertEquals( 3, entry.get( atCN ).size() );
665         assertTrue( entry.contains( atCN, "test1" ) );
666         assertTrue( entry.contains( atCN, "test2" ) );
667         assertTrue( entry.contains( atCN, "test3" ) );
668         
669         // Test the addition of a null value
670         entry.add( atCN, (String)null );
671         assertNotNull( entry.get( atCN ) );
672         assertEquals( 4, entry.get( atCN ).size() );
673         assertTrue( entry.contains( atCN, "test1" ) );
674         assertTrue( entry.contains( atCN, "test2" ) );
675         assertTrue( entry.contains( atCN, "test3" ) );
676         assertTrue( entry.contains( atCN, (String )null ) ); 
677         
678         entry.clear();
679         
680         // Test the addition of a binary value
681         byte[] test4 = StringTools.getBytesUtf8( "test4" );
682         
683         entry.add( atCN, test4 );
684         assertFalse( entry.get( atCN ).contains( test4 ) );
685     }
686 
687 
688     /**
689      * Test the add( AT, byte[]... ) method
690      */
691     @Test public void testAddAtBytesElipsis() throws Exception
692     {
693         LdapDN dn = new LdapDN( "cn=test" );
694         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
695         
696         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
697         
698         byte[] test1 = StringTools.getBytesUtf8( "test1" );
699         byte[] test2 = StringTools.getBytesUtf8( "test2" );
700         byte[] test3 = StringTools.getBytesUtf8( "test3" );
701         
702         // Test a simple addition
703         entry.add( atPassword, test1 );
704         assertNotNull( entry.get( atPassword ) );
705         assertEquals( 1, entry.get( atPassword ).size() );
706         assertTrue( Arrays.equals( test1, (byte[])entry.get( atPassword ).get().get() ) );
707         
708         // Test some more addition
709         entry.add( atPassword, test2, test3 );
710         assertNotNull( entry.get( atPassword ) );
711         assertEquals( 3, entry.get( atPassword ).size() );
712         assertTrue( entry.contains( atPassword, test1 ) );
713         assertTrue( entry.contains( atPassword, test2 ) );
714         assertTrue( entry.contains( atPassword, test3 ) );
715         
716         // Test some addition of existing values
717         entry.add( atPassword, test2 );
718         assertNotNull( entry.get( atPassword ) );
719         assertEquals( 3, entry.get( atPassword ).size() );
720         assertTrue( entry.contains( atPassword, test1 ) );
721         assertTrue( entry.contains( atPassword, test2 ) );
722         assertTrue( entry.contains( atPassword, test3 ) );
723         
724         // Test the addition of a null value
725         entry.add( atPassword, (byte[])null );
726         assertNotNull( entry.get( atPassword ) );
727         assertEquals( 4, entry.get( atPassword ).size() );
728         assertTrue( entry.contains( atPassword, test1 ) );
729         assertTrue( entry.contains( atPassword, test2 ) );
730         assertTrue( entry.contains( atPassword, test3 ) );
731         assertTrue( entry.contains( atPassword, (byte[] )null ) ); 
732         
733         entry.clear();
734         
735         // Test the addition of a String value. It should be converted to a byte array
736         byte[] test4 = StringTools.getBytesUtf8( "test4" );
737 
738         entry.add( atPassword, "test4" );
739         assertNotNull( entry.get( atPassword ) );
740         assertEquals( 0, entry.get( atPassword ).size() );
741         assertFalse( entry.contains( atPassword, test4 ) );
742     }
743 
744 
745     /**
746      * Test the add( AT, SV... ) method
747      */
748     @Test public void testAddAtServerValueElipsis() throws Exception
749     {
750         LdapDN dn = new LdapDN( "cn=test" );
751         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
752         
753         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
754         
755         byte[] b1 = StringTools.getBytesUtf8( "test1" );
756         byte[] b2 = StringTools.getBytesUtf8( "test2" );
757         byte[] b3 = StringTools.getBytesUtf8( "test3" );
758 
759         Value<String> test1 = new ServerStringValue( atCN, "test1" );
760         Value<String> test2 = new ServerStringValue( atCN, "test2" );
761         Value<String> test3 = new ServerStringValue( atCN, "test3" );
762         
763         Value<byte[]> testB1 = new ServerBinaryValue( atPassword, b1 );
764         Value<byte[]> testB2 = new ServerBinaryValue( atPassword, b2 );
765         Value<byte[]> testB3 = new ServerBinaryValue( atPassword, b3 );
766         
767         // Test a simple addition in atCN
768         entry.add( atCN, test1 );
769         assertNotNull( entry.get( atCN ) );
770         assertEquals( 1, entry.get( atCN ).size() );
771         assertEquals( "test1", entry.get( atCN ).get().get() );
772         
773         // Test some more addition
774         entry.add( atCN, test2, test3 );
775         assertNotNull( entry.get( atCN ) );
776         assertEquals( 3, entry.get( atCN ).size() );
777         assertTrue( entry.contains( atCN, "test1" ) );
778         assertTrue( entry.contains( atCN, "test2" ) );
779         assertTrue( entry.contains( atCN, "test3" ) );
780         
781         // Test some addition of existing values
782         entry.add( atCN, test2 );
783         assertNotNull( entry.get( atCN ) );
784         assertEquals( 3, entry.get( atCN ).size() );
785         assertTrue( entry.contains( atCN, "test1" ) );
786         assertTrue( entry.contains( atCN, "test2" ) );
787         assertTrue( entry.contains( atCN, "test3" ) );
788         
789         // Test the addition of a null value
790         entry.add( atCN, (String)null );
791         assertNotNull( entry.get( atCN ) );
792         assertEquals( 4, entry.get( atCN ).size() );
793         assertTrue( entry.contains( atCN, "test1" ) );
794         assertTrue( entry.contains( atCN, "test2" ) );
795         assertTrue( entry.contains( atCN, "test3" ) );
796         assertTrue( entry.contains( atCN, (String )null ) ); 
797         
798         entry.clear();
799         
800         // Test the addition of a String value. It should be converted to a byte array
801         byte[] test4 = StringTools.getBytesUtf8( "test4" );
802 
803         entry.add( atCN, test4 );
804         assertFalse( entry.contains( atCN, test4 ) );
805 
806         // Now, work with a binary attribute
807         // Test a simple addition
808         entry.add( atPassword, testB1 );
809         assertNotNull( entry.get( atPassword ) );
810         assertEquals( 1, entry.get( atPassword ).size() );
811         assertTrue( Arrays.equals( b1, (byte[])entry.get( atPassword ).get().get() ) );
812         
813         // Test some more addition
814         entry.add( atPassword, testB2, testB3 );
815         assertNotNull( entry.get( atPassword ) );
816         assertEquals( 3, entry.get( atPassword ).size() );
817         assertTrue( entry.contains( atPassword, b1 ) );
818         assertTrue( entry.contains( atPassword, b2 ) );
819         assertTrue( entry.contains( atPassword, b3 ) );
820         
821         // Test some addition of existing values
822         entry.add( atPassword, testB2 );
823         assertNotNull( entry.get( atPassword ) );
824         assertEquals( 3, entry.get( atPassword ).size() );
825         assertTrue( entry.contains( atPassword, b1 ) );
826         assertTrue( entry.contains( atPassword, b2 ) );
827         assertTrue( entry.contains( atPassword, b3 ) );
828         
829         // Test the addition of a null value
830         entry.add( atPassword, (byte[])null );
831         assertNotNull( entry.get( atPassword ) );
832         assertEquals( 4, entry.get( atPassword ).size() );
833         assertTrue( entry.contains( atPassword, b1 ) );
834         assertTrue( entry.contains( atPassword, b2 ) );
835         assertTrue( entry.contains( atPassword, b3 ) );
836         assertTrue( entry.contains( atPassword, (byte[] )null ) ); 
837         
838         entry.clear();
839         
840         // Test the addition of a String value. It should be converted to a byte array
841         byte[] b4 = StringTools.getBytesUtf8( "test4" );
842 
843         entry.add( atPassword, "test4" );
844         assertNotNull( entry.get( atPassword ) );
845         assertEquals( 0, entry.get( atPassword ).size() );
846         assertFalse( entry.contains( atPassword, b4 ) );
847     }
848 
849 
850     /**
851      * Test the add( upId, String... ) method
852      */
853     @Test public void testAddUpIdStringElipsis() throws Exception
854     {
855         LdapDN dn = new LdapDN( "cn=test" );
856         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
857         
858         // Test a simple addition
859         entry.add( "CN", "test1" );
860         assertNotNull( entry.get( atCN ) );
861         assertTrue( entry.containsAttribute( atCN ) );
862         assertEquals( "cn", entry.get( atCN ).getId() );
863         assertEquals( "CN", entry.get( atCN ).getUpId() );
864         assertEquals( 1, entry.get( atCN ).size() );
865         assertEquals( "test1", entry.get( atCN ).get().get() );
866         
867         // Test some more addition
868         entry.add( "CN", "test2", "test3" );
869         assertNotNull( entry.get( atCN ) );
870         assertEquals( 3, entry.get( atCN ).size() );
871         assertTrue( entry.contains( atCN, "test1" ) );
872         assertTrue( entry.contains( atCN, "test2" ) );
873         assertTrue( entry.contains( atCN, "test3" ) );
874         
875         // Test some addition of existing values
876         entry.add( "CN", "test2" );
877         assertNotNull( entry.get( atCN ) );
878         assertEquals( 3, entry.get( atCN ).size() );
879         assertTrue( entry.contains( atCN, "test1" ) );
880         assertTrue( entry.contains( atCN, "test2" ) );
881         assertTrue( entry.contains( atCN, "test3" ) );
882         
883         // Test the addition of a null value
884         entry.add( "CN", (String)null );
885         assertNotNull( entry.get( atCN ) );
886         assertEquals( 4, entry.get( atCN ).size() );
887         assertTrue( entry.contains( atCN, "test1" ) );
888         assertTrue( entry.contains( atCN, "test2" ) );
889         assertTrue( entry.contains( atCN, "test3" ) );
890         assertTrue( entry.contains( atCN, (String )null ) ); 
891         
892         entry.clear();
893         
894         // Test the addition of a binary value
895         byte[] test4 = StringTools.getBytesUtf8( "test4" );
896         
897         entry.add( "CN", test4 );
898         assertFalse( entry.contains(  "CN", test4 ) );
899     }
900 
901 
902     /**
903      * Test the add( upId, byte[]... ) method
904      */
905     @Test public void testAddUpIdBytesElipsis() throws Exception
906     {
907         LdapDN dn = new LdapDN( "cn=test" );
908         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
909         
910         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
911         
912         byte[] test1 = StringTools.getBytesUtf8( "test1" );
913         byte[] test2 = StringTools.getBytesUtf8( "test2" );
914         byte[] test3 = StringTools.getBytesUtf8( "test3" );
915         
916         // Test a simple addition
917         entry.add( "userPassword", test1 );
918         assertNotNull( entry.get( atPassword ) );
919         assertEquals( 1, entry.get( atPassword ).size() );
920         assertTrue( Arrays.equals( test1, (byte[])entry.get( atPassword ).get().get() ) );
921         
922         // Test some more addition
923         entry.add( "userPassword", test2, test3 );
924         assertNotNull( entry.get( atPassword ) );
925         assertEquals( 3, entry.get( atPassword ).size() );
926         assertTrue( entry.contains( atPassword, test1 ) );
927         assertTrue( entry.contains( atPassword, test2 ) );
928         assertTrue( entry.contains( atPassword, test3 ) );
929         
930         // Test some addition of existing values
931         entry.add( "userPassword", test2 );
932         assertNotNull( entry.get( atPassword ) );
933         assertEquals( 3, entry.get( atPassword ).size() );
934         assertTrue( entry.contains( atPassword, test1 ) );
935         assertTrue( entry.contains( atPassword, test2 ) );
936         assertTrue( entry.contains( atPassword, test3 ) );
937         
938         // Test the addition of a null value
939         entry.add( "userPassword", (byte[])null );
940         assertNotNull( entry.get( atPassword ) );
941         assertEquals( 4, entry.get( atPassword ).size() );
942         assertTrue( entry.contains( atPassword, test1 ) );
943         assertTrue( entry.contains( atPassword, test2 ) );
944         assertTrue( entry.contains( atPassword, test3 ) );
945         assertTrue( entry.contains( atPassword, (byte[] )null ) ); 
946         
947         entry.clear();
948         
949         // Test the addition of a String value. It should be converted to a byte array
950         byte[] test4 = StringTools.getBytesUtf8( "test4" );
951 
952         entry.add( "userPassword", "test4" );
953         assertNotNull( entry.get( atPassword ) );
954         assertEquals( 0, entry.get( atPassword ).size() );
955         assertFalse( entry.contains( atPassword, test4 ) );
956     }
957 
958 
959     /**
960      * Test the add( upId, SV... ) method
961      */
962     @Test public void testAddUpIdServerValueElipsis() throws Exception
963     {
964         LdapDN dn = new LdapDN( "cn=test" );
965         ServerEntry entry = new DefaultServerEntry( registries, dn );
966         
967         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
968         
969         byte[] b1 = StringTools.getBytesUtf8( "test1" );
970         byte[] b2 = StringTools.getBytesUtf8( "test2" );
971         byte[] b3 = StringTools.getBytesUtf8( "test3" );
972 
973         Value<String> test1 = new ServerStringValue( atCN, "test1" );
974         Value<String> test2 = new ServerStringValue( atCN, "test2" );
975         Value<String> test3 = new ServerStringValue( atCN, "test3" );
976         
977         Value<byte[]> testB1 = new ServerBinaryValue( atPassword, b1 );
978         Value<byte[]> testB2 = new ServerBinaryValue( atPassword, b2 );
979         Value<byte[]> testB3 = new ServerBinaryValue( atPassword, b3 );
980         
981         // Test a simple addition in atCN
982         entry.add( "cN", test1 );
983         assertNotNull( entry.get( atCN ) );
984         assertEquals( 1, entry.get( atCN ).size() );
985         assertEquals( "test1", entry.get( atCN ).get().get() );
986         assertTrue( entry.containsAttribute( atCN ) );
987         assertEquals( "cN", entry.get( atCN ).getUpId() );
988         
989         // Test some more addition
990         entry.add( "cN", test2, test3 );
991         assertNotNull( entry.get( atCN ) );
992         assertEquals( 3, entry.get( atCN ).size() );
993         assertTrue( entry.contains( atCN, "test1" ) );
994         assertTrue( entry.contains( atCN, "test2" ) );
995         assertTrue( entry.contains( atCN, "test3" ) );
996         assertTrue( entry.containsAttribute( atCN ) );
997         assertEquals( "cN", entry.get( atCN ).getUpId() );
998         
999         // Test some addition of existing values
1000         entry.add( "cN", test2 );
1001         assertNotNull( entry.get( atCN ) );
1002         assertEquals( 3, entry.get( atCN ).size() );
1003         assertTrue( entry.contains( atCN, "test1" ) );
1004         assertTrue( entry.contains( atCN, "test2" ) );
1005         assertTrue( entry.contains( atCN, "test3" ) );
1006         
1007         // Test the addition of a null value
1008         entry.add( "cN", (String)null );
1009         assertNotNull( entry.get( atCN ) );
1010         assertEquals( 4, entry.get( atCN ).size() );
1011         assertTrue( entry.contains( atCN, "test1" ) );
1012         assertTrue( entry.contains( atCN, "test2" ) );
1013         assertTrue( entry.contains( atCN, "test3" ) );
1014         assertTrue( entry.contains( atCN, (String )null ) ); 
1015         
1016         entry.clear();
1017         
1018         // Test the addition of a String value. It should be converted to a byte array
1019         byte[] test4 = StringTools.getBytesUtf8( "test4" );
1020 
1021         entry.add( "cN", test4 );
1022         assertFalse( entry.contains( "cN", test4 ) );
1023 
1024         // Now, work with a binary attribute
1025         // Test a simple addition
1026         entry.add( "userPASSWORD", testB1 );
1027         assertNotNull( entry.get( atPassword ) );
1028         assertEquals( 1, entry.get( atPassword ).size() );
1029         assertTrue( Arrays.equals( b1, (byte[])entry.get( atPassword ).get().get() ) );
1030         assertTrue( entry.containsAttribute( atPassword ) );
1031         assertEquals( "userPASSWORD", entry.get( atPassword ).getUpId() );
1032         
1033         // Test some more addition
1034         entry.add( "userPASSWORD", testB2, testB3 );
1035         assertNotNull( entry.get( atPassword ) );
1036         assertEquals( 3, entry.get( atPassword ).size() );
1037         assertTrue( entry.contains( atPassword, b1 ) );
1038         assertTrue( entry.contains( atPassword, b2 ) );
1039         assertTrue( entry.contains( atPassword, b3 ) );
1040         
1041         // Test some addition of existing values
1042         entry.add( "userPASSWORD", testB2 );
1043         assertNotNull( entry.get( atPassword ) );
1044         assertEquals( 3, entry.get( atPassword ).size() );
1045         assertTrue( entry.contains( atPassword, b1 ) );
1046         assertTrue( entry.contains( atPassword, b2 ) );
1047         assertTrue( entry.contains( atPassword, b3 ) );
1048         
1049         // Test the addition of a null value
1050         entry.add( "userPASSWORD", (byte[])null );
1051         assertNotNull( entry.get( atPassword ) );
1052         assertEquals( 4, entry.get( atPassword ).size() );
1053         assertTrue( entry.contains( atPassword, b1 ) );
1054         assertTrue( entry.contains( atPassword, b2 ) );
1055         assertTrue( entry.contains( atPassword, b3 ) );
1056         assertTrue( entry.contains( atPassword, (byte[] )null ) ); 
1057         
1058         entry.clear();
1059         
1060         // Test the addition of a String value. It should be converted to a byte array
1061         byte[] b4 = StringTools.getBytesUtf8( "test4" );
1062 
1063         entry.add( "userPASSWORD", "test4" );
1064         assertNotNull( entry.get( atPassword ) );
1065         assertEquals( 0, entry.get( atPassword ).size() );
1066         assertFalse( entry.contains( atPassword, b4 ) );
1067     }
1068 
1069 
1070     /**
1071      * Test the add( UpId, AT, String... ) method
1072      */
1073     @Test public void testAddUpIdAtStringElipsis() throws Exception
1074     {
1075         LdapDN dn = new LdapDN( "cn=test" );
1076         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
1077         
1078         // Test a simple addition
1079         entry.add( "cn", atCN, "test1" );
1080         assertNotNull( entry.get( atCN ) );
1081         assertEquals( 1, entry.get( atCN ).size() );
1082         assertEquals( "test1", entry.get( atCN ).get().get() );
1083         
1084         // Test some more addition
1085         entry.add( "CN", atCN, "test2", "test3" );
1086         assertNotNull( entry.get( atCN ) );
1087         assertEquals( 3, entry.get( atCN ).size() );
1088         assertTrue( entry.contains( atCN, "test1" ) );
1089         assertTrue( entry.contains( atCN, "test2" ) );
1090         assertTrue( entry.contains( atCN, "test3" ) );
1091         
1092         // Test some addition of existing values
1093         entry.add( "commonName", atCN, "test2" );
1094         assertNotNull( entry.get( atCN ) );
1095         assertEquals( 3, entry.get( atCN ).size() );
1096         assertTrue( entry.contains( atCN, "test1" ) );
1097         assertTrue( entry.contains( atCN, "test2" ) );
1098         assertTrue( entry.contains( atCN, "test3" ) );
1099         
1100         // Test the addition of a null value
1101         entry.add( "COMMONname", atCN, (String)null );
1102         assertNotNull( entry.get( atCN ) );
1103         assertEquals( 4, entry.get( atCN ).size() );
1104         assertTrue( entry.contains( atCN, "test1" ) );
1105         assertTrue( entry.contains( atCN, "test2" ) );
1106         assertTrue( entry.contains( atCN, "test3" ) );
1107         assertTrue( entry.contains( atCN, (String )null ) ); 
1108         
1109         entry.clear();
1110         
1111         // Test the addition of a binary value
1112         byte[] test4 = StringTools.getBytesUtf8( "test4" );
1113         
1114         entry.add( "cn", atCN, test4 );
1115         assertFalse( entry.contains( "cn", test4 ) );
1116     }
1117 
1118 
1119     /**
1120      * Test the add( upId, AT, byte[]... ) method
1121      */
1122     @Test public void testAddUpIdAtBytesElipsis() throws Exception
1123     {
1124         LdapDN dn = new LdapDN( "cn=test" );
1125         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
1126         
1127         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
1128         
1129         byte[] test1 = StringTools.getBytesUtf8( "test1" );
1130         byte[] test2 = StringTools.getBytesUtf8( "test2" );
1131         byte[] test3 = StringTools.getBytesUtf8( "test3" );
1132         
1133         // Test a simple addition
1134         entry.add( "userPassword", atPassword, test1 );
1135         assertNotNull( entry.get( atPassword ) );
1136         assertEquals( 1, entry.get( atPassword ).size() );
1137         assertTrue( Arrays.equals( test1, (byte[])entry.get( atPassword ).get().get() ) );
1138         
1139         // Test some more addition
1140         entry.add( "userPassword", atPassword, test2, test3 );
1141         assertNotNull( entry.get( atPassword ) );
1142         assertEquals( 3, entry.get( atPassword ).size() );
1143         assertTrue( entry.contains( atPassword, test1 ) );
1144         assertTrue( entry.contains( atPassword, test2 ) );
1145         assertTrue( entry.contains( atPassword, test3 ) );
1146         
1147         // Test some addition of existing values
1148         entry.add( "userPassword", atPassword, test2 );
1149         assertNotNull( entry.get( atPassword ) );
1150         assertEquals( 3, entry.get( atPassword ).size() );
1151         assertTrue( entry.contains( atPassword, test1 ) );
1152         assertTrue( entry.contains( atPassword, test2 ) );
1153         assertTrue( entry.contains( atPassword, test3 ) );
1154         
1155         // Test the addition of a null value
1156         entry.add( "userPassword", atPassword, (byte[])null );
1157         assertNotNull( entry.get( atPassword ) );
1158         assertEquals( 4, entry.get( atPassword ).size() );
1159         assertTrue( entry.contains( atPassword, test1 ) );
1160         assertTrue( entry.contains( atPassword, test2 ) );
1161         assertTrue( entry.contains( atPassword, test3 ) );
1162         assertTrue( entry.contains( atPassword, (byte[] )null ) ); 
1163         
1164         entry.clear();
1165         
1166         // Test the addition of a String value. It should be converted to a byte array
1167         byte[] test4 = StringTools.getBytesUtf8( "test4" );
1168 
1169         entry.add( "userPassword", atPassword, "test4" );
1170         assertNotNull( entry.get( atPassword ) );
1171         assertEquals( 0, entry.get( atPassword ).size() );
1172         assertFalse( entry.contains( atPassword, test4 ) );
1173     }
1174 
1175 
1176     /**
1177      * Test the add( upId, AT, SV... ) method
1178      */
1179     @Test public void testAddUpIdAtServerValueElipsis() throws Exception
1180     {
1181         LdapDN dn = new LdapDN( "cn=test" );
1182         ServerEntry entry = new DefaultServerEntry( registries, dn );
1183         
1184         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
1185         
1186         byte[] b1 = StringTools.getBytesUtf8( "test1" );
1187         byte[] b2 = StringTools.getBytesUtf8( "test2" );
1188         byte[] b3 = StringTools.getBytesUtf8( "test3" );
1189 
1190         Value<String> test1 = new ServerStringValue( atCN, "test1" );
1191         Value<String> test2 = new ServerStringValue( atCN, "test2" );
1192         Value<String> test3 = new ServerStringValue( atCN, "test3" );
1193         
1194         Value<byte[]> testB1 = new ServerBinaryValue( atPassword, b1 );
1195         Value<byte[]> testB2 = new ServerBinaryValue( atPassword, b2 );
1196         Value<byte[]> testB3 = new ServerBinaryValue( atPassword, b3 );
1197         
1198         // Test a simple addition in atCN
1199         entry.add( "cN", atCN, test1 );
1200         assertNotNull( entry.get( atCN ) );
1201         assertEquals( 1, entry.get( atCN ).size() );
1202         assertEquals( "test1", entry.get( atCN ).get().get() );
1203         assertTrue( entry.containsAttribute( atCN ) );
1204         assertEquals( "cN", entry.get( atCN ).getUpId() );
1205         
1206         // Test some more addition
1207         entry.add( "cN", atCN, test2, test3 );
1208         assertNotNull( entry.get( atCN ) );
1209         assertEquals( 3, entry.get( atCN ).size() );
1210         assertTrue( entry.contains( atCN, "test1" ) );
1211         assertTrue( entry.contains( atCN, "test2" ) );
1212         assertTrue( entry.contains( atCN, "test3" ) );
1213         assertTrue( entry.containsAttribute( atCN ) );
1214         assertEquals( "cN", entry.get( atCN ).getUpId() );
1215         
1216         // Test some addition of existing values
1217         entry.add( "cN", atCN, test2 );
1218         assertNotNull( entry.get( atCN ) );
1219         assertEquals( 3, entry.get( atCN ).size() );
1220         assertTrue( entry.contains( atCN, "test1" ) );
1221         assertTrue( entry.contains( atCN, "test2" ) );
1222         assertTrue( entry.contains( atCN, "test3" ) );
1223         
1224         // Test the addition of a null value
1225         entry.add( "cN", atCN, (String)null );
1226         assertNotNull( entry.get( atCN ) );
1227         assertEquals( 4, entry.get( atCN ).size() );
1228         assertTrue( entry.contains( atCN, "test1" ) );
1229         assertTrue( entry.contains( atCN, "test2" ) );
1230         assertTrue( entry.contains( atCN, "test3" ) );
1231         assertTrue( entry.contains( atCN, (String )null ) ); 
1232         
1233         entry.clear();
1234         
1235         // Test the addition of a String value. It should be converted to a byte array
1236         byte[] test4 = StringTools.getBytesUtf8( "test4" );
1237 
1238         entry.add( "cN", atCN, test4 );
1239         assertFalse( entry.contains( "cN", test4 ) );
1240 
1241         // Now, work with a binary attribute
1242         // Test a simple addition
1243         entry.add( "userPASSWORD", atPassword, testB1 );
1244         assertNotNull( entry.get( atPassword ) );
1245         assertEquals( 1, entry.get( atPassword ).size() );
1246         assertTrue( Arrays.equals( b1, (byte[])entry.get( atPassword ).get().get() ) );
1247         assertTrue( entry.containsAttribute( atPassword ) );
1248         assertEquals( "userPASSWORD", entry.get( atPassword ).getUpId() );
1249         
1250         // Test some more addition
1251         entry.add( "userPASSWORD", atPassword, testB2, testB3 );
1252         assertNotNull( entry.get( atPassword ) );
1253         assertEquals( 3, entry.get( atPassword ).size() );
1254         assertTrue( entry.contains( atPassword, b1 ) );
1255         assertTrue( entry.contains( atPassword, b2 ) );
1256         assertTrue( entry.contains( atPassword, b3 ) );
1257         
1258         // Test some addition of existing values
1259         entry.add( "userPASSWORD", atPassword, testB2 );
1260         assertNotNull( entry.get( atPassword ) );
1261         assertEquals( 3, entry.get( atPassword ).size() );
1262         assertTrue( entry.contains( atPassword, b1 ) );
1263         assertTrue( entry.contains( atPassword, b2 ) );
1264         assertTrue( entry.contains( atPassword, b3 ) );
1265         
1266         // Test the addition of a null value
1267         entry.add( "userPASSWORD", atPassword, (byte[])null );
1268         assertNotNull( entry.get( atPassword ) );
1269         assertEquals( 4, entry.get( atPassword ).size() );
1270         assertTrue( entry.contains( atPassword, b1 ) );
1271         assertTrue( entry.contains( atPassword, b2 ) );
1272         assertTrue( entry.contains( atPassword, b3 ) );
1273         assertTrue( entry.contains( atPassword, (byte[] )null ) ); 
1274         
1275         entry.clear();
1276         
1277         // Test the addition of a String value. It should be converted to a byte array
1278         byte[] b4 = StringTools.getBytesUtf8( "test4" );
1279 
1280         entry.add( "userPASSWORD", atPassword, "test4" );
1281         assertNotNull( entry.get( atPassword ) );
1282         assertEquals( 0, entry.get( atPassword ).size() );
1283         assertFalse( entry.contains( atPassword, b4 ) );
1284     }
1285 
1286 
1287     /**
1288      * Test method for clear()
1289      */
1290     @Test
1291     public void testClear() throws Exception
1292     {
1293         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1294          
1295         assertEquals( 0, entry.size() );
1296         assertNull( entry.get( "ObjectClass" ) );
1297         entry.clear();
1298         assertEquals( 0, entry.size() );
1299         assertNull( entry.get( "ObjectClass" ) );
1300          
1301         entry.add( "ObjectClass", "top", "person" );
1302         assertEquals( 1, entry.size() );
1303         assertNotNull( entry.get( "ObjectClass" ) );
1304         
1305         entry.clear();
1306         assertEquals( 0, entry.size() );
1307         assertNull( entry.get( "ObjectClass" ) );
1308     }
1309 
1310 
1311     /**
1312      * Test method for clone()
1313      */
1314     @Test
1315     public void testClone() throws Exception
1316     {
1317         Entry entry1 = new DefaultServerEntry( registries );
1318         
1319         Entry entry2 = entry1.clone();
1320         
1321         assertEquals( entry1, entry2 );
1322         entry2.setDn( EXAMPLE_DN );
1323         
1324         assertEquals( LdapDN.EMPTY_LDAPDN,entry1.getDn() );
1325         
1326         entry1.setDn( EXAMPLE_DN );
1327         entry2 = entry1.clone();
1328         assertEquals( entry1, entry2 );
1329         
1330         entry1.add( "objectClass", "top", "person" );
1331         entry1.add( "cn", "test1", "test2" );
1332         
1333         entry2 = entry1.clone();
1334         assertEquals( entry1, entry2 );
1335         
1336         entry1.add( "cn", "test3" );
1337         assertEquals( 2, entry2.get( "cn" ).size() );
1338         assertFalse( entry2.contains( "cn", "test3" ) );
1339         
1340         entry1.add( "sn", (String)null );
1341         assertFalse( entry2.containsAttribute( "sn" ) );
1342     }
1343      
1344     
1345     //-------------------------------------------------------------------------
1346     // Test the Contains methods
1347     //-------------------------------------------------------------------------
1348     /**
1349      * Test for method contains( AttributeType, byte[]... )
1350      */
1351     @Test
1352     public void testContainsAttributeTypeByteArrayArray() throws Exception
1353     {
1354         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1355         
1356         assertFalse( entry.contains( (AttributeType )null, BYTES1 ) );
1357         assertFalse( entry.contains( atPwd, BYTES1 ) );
1358         
1359         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1360 
1361         assertFalse( entry.contains( attrPWD ) );
1362         
1363         entry.add( attrPWD );
1364         
1365         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
1366         assertFalse( entry.contains( atPwd, BYTES1, BYTES2, BYTES3 ) );
1367         assertFalse( entry.contains( atPwd, "ab" ) );
1368     }
1369     
1370     
1371     /**
1372      * Test for method contains( AttributeType, String... )
1373      */
1374     @Test
1375     public void testContainsAttributeTypeStringArray() throws Exception
1376     {
1377         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1378         
1379         assertFalse( entry.contains( (AttributeType )null, "test" ) );
1380         assertFalse( entry.contains( atCN, "test" ) );
1381         
1382         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1383 
1384         assertFalse( entry.contains( attrCN ) );
1385         
1386         entry.add( attrCN );
1387         
1388         assertTrue( entry.contains( atCN, "test1", "test2" ) );
1389         assertFalse( entry.contains( atCN, "test1", "test2", "test3" ) );
1390         assertFalse( entry.contains( atCN, BYTES1 ) );
1391     }
1392     
1393     
1394     /**
1395      * Test for method contains( AttributeType, Value<?>... )
1396      */
1397     @Test
1398     public void testContainsAttributeTypeValuesArray() throws Exception
1399     {
1400         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1401         
1402         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
1403         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
1404         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
1405         Value<String> strNullValue = new ServerStringValue( atCN, null);
1406 
1407         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
1408         Value<byte[]> binValue2 = new ServerBinaryValue( atPwd, BYTES2 );
1409         Value<byte[]> binValue3 = new ServerBinaryValue( atPwd, BYTES3 );
1410         Value<byte[]> binNullValue = new ServerBinaryValue( atPwd, null );
1411 
1412         assertFalse( entry.contains( (String)null, strValue1 ) );
1413         assertFalse( entry.contains( atCN, binValue1 ) );
1414         
1415         EntryAttribute attrCN = new DefaultServerAttribute( atCN, strValue1, strValue2 );
1416         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, binValue1, binValue2, binNullValue );
1417 
1418         entry.add( attrCN, attrPWD );
1419         
1420         assertTrue( entry.contains( atCN, strValue1, strValue2 ) );
1421         assertTrue( entry.contains( atPwd, binValue1, binValue2, binNullValue ) );
1422         
1423         assertFalse( entry.contains( atCN, strValue3 ) );
1424         assertFalse( entry.contains( atCN, strNullValue ) );
1425         assertFalse( entry.contains( atPwd, binValue3 ) );
1426     }
1427     
1428     
1429     /**
1430      * Test for method contains( EntryAttribute... )
1431      */
1432     @Test
1433     public void testContainsEntryAttributeArray() throws Exception
1434     {
1435         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1436         
1437         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1438         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1439         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1440         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1441 
1442         assertFalse( entry.contains( attrOC, attrCN ) );
1443         
1444         entry.add( attrOC, attrCN );
1445 
1446         assertTrue( entry.contains( attrOC, attrCN ) );
1447         assertFalse( entry.contains( attrOC, attrCN, attrSN ) );
1448         
1449         entry.add( attrSN, attrPWD );
1450 
1451         assertTrue( entry.contains( attrSN, attrPWD ) );
1452         
1453         assertFalse( entry.contains( (EntryAttribute)null ) );
1454         entry.clear();
1455         assertTrue( entry.contains( (EntryAttribute)null ) );
1456     }
1457     
1458     
1459     /**
1460      * Test for method contains( String, byte[]... )
1461      */
1462     @Test
1463     public void testContainsStringByteArrayArray() throws Exception
1464     {
1465         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1466         
1467         assertFalse( entry.contains( (String)null, BYTES3 ) );
1468         assertFalse( entry.containsAttribute( "objectClass" ) );
1469         
1470         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, (byte[])null, BYTES2 );
1471 
1472         entry.add( attrPWD );
1473         
1474         assertTrue( entry.contains( "  userPASSWORD  ", BYTES1, BYTES2 ) );
1475         assertTrue( entry.contains( "  userPASSWORD  ", (byte[])null ) );
1476         
1477         assertFalse( entry.contains( "  userPASSWORD  ", "ab", "b" ) );
1478         assertFalse( entry.contains( "  userPASSWORD  ", BYTES3 ) );
1479         assertFalse( entry.contains( "  userASSWORD  ", BYTES3 ) );
1480     }
1481     
1482     
1483     /**
1484      * Test for method contains( String, String... )
1485      */
1486     @Test
1487     public void testContainsStringStringArray() throws Exception
1488     {
1489         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1490         
1491         assertFalse( entry.contains( (String)null, "test" ) );
1492         assertFalse( entry.containsAttribute( "objectClass" ) );
1493         
1494         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", (String)null, "test2" );
1495 
1496         entry.add( attrCN );
1497         
1498         assertTrue( entry.contains( "  CN  ", "test1", "test2" ) );
1499         
1500         assertTrue( entry.contains( "  CN  ", (String)null ) );
1501         assertFalse( entry.contains( "  CN  ", BYTES1, BYTES2 ) );
1502         assertFalse( entry.contains( "  CN  ", "test3" ) );
1503         assertFalse( entry.contains( "  CNN  ", "test3" ) );
1504     }
1505     
1506     
1507     /**
1508      * Test for method contains( String, Value<?>... )
1509      */
1510     @Test
1511     public void testContainsStringValueArray() throws Exception
1512     {
1513         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1514         
1515         assertFalse( entry.contains( (String)null, "test" ) );
1516         assertFalse( entry.containsAttribute( "objectClass" ) );
1517         
1518         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2", (String)null );
1519         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2, (byte[])null );
1520 
1521         entry.add( attrCN, attrPWD );
1522         
1523         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
1524         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
1525         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
1526         Value<String> strNullValue = new ServerStringValue( atCN, null);
1527 
1528         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
1529         Value<byte[]> binValue2 = new ServerBinaryValue( atPwd, BYTES2 );
1530         Value<byte[]> binValue3 = new ServerBinaryValue( atPwd, BYTES3 );
1531         Value<byte[]> binNullValue = new ServerBinaryValue( atPwd, null );
1532 
1533         assertTrue( entry.contains( "CN", strValue1, strValue2 ) );
1534         assertTrue( entry.contains( "userpassword", binValue1, binValue2, binNullValue ) );
1535         
1536         assertFalse( entry.contains( "cn", strValue3 ) );
1537         assertTrue( entry.contains( "cn", strNullValue ) );
1538         assertFalse( entry.contains( "UserPassword", binValue3 ) );
1539     }
1540     
1541     
1542     /**
1543      * Test method for containsAttribute( AttributeType )
1544      */
1545     @Test
1546     public void testContainsAttributeAttributeType() throws Exception
1547     {
1548         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1549         
1550         assertFalse( entry.containsAttribute( atOC ) );
1551         
1552         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1553         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1554         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1555         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1556 
1557         entry.add( attrOC, attrCN, attrSN, attrPWD );
1558         
1559         assertTrue( entry.containsAttribute( atOC ) );
1560         assertTrue( entry.containsAttribute( atCN ) );
1561         assertTrue( entry.containsAttribute( atSN ) );
1562         assertTrue( entry.containsAttribute( atPwd ) );
1563         
1564         entry.clear();
1565 
1566         assertFalse( entry.containsAttribute( atOC ) );
1567         assertFalse( entry.containsAttribute( atCN ) );
1568         assertFalse( entry.containsAttribute( atSN ) );
1569         assertFalse( entry.containsAttribute( atPwd ) );
1570     }
1571 
1572 
1573     /**
1574      * Test method for containsAttribute( String )
1575      */
1576     @Test
1577     public void testContainsAttributeString() throws Exception
1578     {
1579         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1580         
1581         assertFalse( entry.containsAttribute( "objectClass" ) );
1582         
1583         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1584         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1585         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1586         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1587 
1588         entry.add( attrOC, attrCN, attrSN, attrPWD );
1589         
1590         assertTrue( entry.containsAttribute( "OBJECTCLASS", " cn ", "Sn", "  userPASSWORD  " ) );
1591         
1592         entry.clear();
1593 
1594         assertFalse( entry.containsAttribute( "OBJECTCLASS" ) );
1595         assertFalse( entry.containsAttribute( " cn " ) );
1596         assertFalse( entry.containsAttribute( "Sn" ) );
1597         assertFalse( entry.containsAttribute( "  userPASSWORD  " ) );
1598         assertFalse( entry.containsAttribute( "  userASSWORD  " ) );
1599     }
1600 
1601     
1602     /**
1603      * Test method for equals()
1604      */
1605     @Test
1606     public void testEqualsObject() throws Exception
1607     {
1608         Entry entry1 = new DefaultServerEntry( registries );
1609         Entry entry2 = new DefaultServerEntry( registries );
1610         
1611         assertEquals( entry1, entry2 );
1612         
1613         entry1.setDn( EXAMPLE_DN );
1614         assertNotSame( entry1, entry2 );
1615         
1616         entry2.setDn( EXAMPLE_DN );
1617         assertEquals( entry1, entry2 );
1618 
1619         EntryAttribute attrOC = new DefaultServerAttribute( "objectClass", atOC, "top", "person" );
1620         EntryAttribute attrCN = new DefaultServerAttribute( "cn", atCN, "test1", "test2" );
1621         EntryAttribute attrSN = new DefaultServerAttribute( "sn", atSN, "Test1", "Test2" );
1622         EntryAttribute attrPWD = new DefaultServerAttribute( "userPassword", atPwd, BYTES1, BYTES2 );
1623         
1624         entry1.put( attrOC, attrCN, attrSN, attrPWD );
1625         entry2.put( attrOC, attrCN, attrSN );
1626         assertNotSame( entry1, entry2 );
1627         
1628         entry2.put( attrPWD );
1629         assertEquals( entry1, entry2 );
1630         
1631         EntryAttribute attrL1 = new DefaultServerAttribute( "l", atL, "Paris", "New-York" );
1632         EntryAttribute attrL2 = new DefaultServerAttribute( "l", atL, "Paris", "Tokyo" );
1633         
1634         entry1.put( attrL1 );
1635         entry2.put( attrL1 );
1636         assertEquals( entry1, entry2 );
1637         
1638         entry1.add( "l", "London" );
1639         assertNotSame( entry1, entry2 );
1640 
1641         entry2.add( attrL2 );
1642         assertNotSame( entry1, entry2 );
1643 
1644         entry1.clear();
1645         entry2.clear();
1646         assertEquals( entry1, entry2 );
1647     }
1648 
1649 
1650     /**
1651      * Test method for getAttributeTypes()
1652      */
1653     @Test
1654     public void testGetAttributeTypes() throws Exception
1655     {
1656         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1657         
1658         assertEquals( 0, entry.getAttributeTypes().size() );
1659 
1660         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1661         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1662         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1663         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1664 
1665         entry.add( attrOC, attrCN, attrSN, attrPWD );
1666         
1667         Set<AttributeType> attributeTypes = entry.getAttributeTypes();
1668         
1669         assertEquals( 4, attributeTypes.size() );
1670         assertTrue( attributeTypes.contains( atOC ) );
1671         assertTrue( attributeTypes.contains( atCN ) );
1672         assertTrue( attributeTypes.contains( atSN ) );
1673         assertTrue( attributeTypes.contains( atPwd ) );
1674         assertFalse( attributeTypes.contains( atC ) );
1675     }
1676 
1677 
1678     /**
1679      * Test method for get( AttributeType )
1680      */
1681     @Test
1682     public void testGetAttributeType() throws Exception 
1683     {
1684         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1685 
1686         assertNull( entry.get( atCN ) );
1687         assertNull( entry.get( (AttributeType)null ) );
1688         
1689         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1690         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1691         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1692         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1693         
1694         entry.add( attrOC, attrCN, attrSN, attrPWD );
1695         
1696         assertNotNull( entry.get( atCN ) );
1697         
1698         assertEquals( attrCN, entry.get( atCN ) );
1699         assertEquals( attrOC, entry.get( atOC ) );
1700         assertEquals( attrSN, entry.get( atSN ) );
1701         assertEquals( attrPWD, entry.get( atPwd ) );
1702     }
1703         
1704 
1705     /**
1706      * Test method for get( String )
1707      */
1708     @Test
1709     public void testGetString() throws Exception 
1710     {
1711         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1712 
1713         assertNull( entry.get( "cn" ) );
1714         assertNull( entry.get( "badId" ) );
1715         
1716         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1717         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1718         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1719         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1720         
1721         entry.add( attrOC, attrCN, attrSN, attrPWD );
1722         
1723         assertNotNull( entry.get( "CN" ) );
1724         assertNotNull( entry.get( " commonName " ) );
1725         assertNotNull( entry.get( "2.5.4.3" ) );
1726         
1727         assertEquals( attrCN, entry.get( "2.5.4.3" ) );
1728         assertEquals( attrOC, entry.get( " OBJECTCLASS" ) );
1729         assertEquals( attrSN, entry.get( "sn" ) );
1730         assertEquals( attrPWD, entry.get( "  userPassword  " ) );
1731     }
1732         
1733 
1734     /**
1735      * Test method for getDN()
1736      */
1737     @Test
1738     public void testGetDn() throws InvalidNameException 
1739     {
1740         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1741          
1742         assertEquals( EXAMPLE_DN, entry.getDn() );
1743          
1744         LdapDN testDn = new LdapDN( "cn=test" );
1745         entry.setDn( testDn );
1746          
1747         assertEquals( testDn, entry.getDn() );
1748     }
1749 
1750 
1751     /**
1752      * Test method for hashcode()
1753      */
1754     @Test
1755     public void testHashCode() throws InvalidNameException, Exception
1756     {
1757         Entry entry1 = new DefaultServerEntry( registries, EXAMPLE_DN );
1758         Entry entry2 = new DefaultServerEntry( registries, EXAMPLE_DN );
1759         
1760         assertEquals( entry1.hashCode(), entry2.hashCode() );
1761         
1762         entry2.setDn( new LdapDN( "ou=system,dc=com" ) );
1763         assertNotSame( entry1.hashCode(), entry2.hashCode() );
1764         
1765         entry2.setDn( EXAMPLE_DN );
1766         assertEquals( entry1.hashCode(), entry2.hashCode() );
1767         
1768         
1769         EntryAttribute attrOC = new DefaultServerAttribute( "objectClass", atOC, "top", "person" );
1770         EntryAttribute attrCN = new DefaultServerAttribute( "cn", atCN, "test1", "test2" );
1771         EntryAttribute attrSN = new DefaultServerAttribute( "sn", atSN, "Test1", "Test2" );
1772         EntryAttribute attrPWD = new DefaultServerAttribute( "userPassword", atPwd, BYTES1, BYTES2 );
1773 
1774         entry1.add( attrOC, attrCN, attrSN, attrPWD );
1775         entry2.add( attrOC, attrCN, attrSN, attrPWD );
1776 
1777         assertEquals( entry1.hashCode(), entry2.hashCode() );
1778         
1779         Entry entry3 = new DefaultServerEntry( registries, EXAMPLE_DN );
1780         entry3.add( attrOC, attrSN, attrCN, attrPWD );
1781 
1782         assertEquals( entry1.hashCode(), entry3.hashCode() );
1783     }
1784 
1785     
1786     /**
1787      * Test method for hasObjectClass( EntryAttribute )
1788      */
1789     @Test
1790     public void testHasObjectClassEntryAttribute() throws Exception
1791     {
1792         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1793         
1794         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1795         
1796         assertFalse( entry.contains( attrOC ) );
1797         assertFalse( entry.hasObjectClass( attrOC ) );
1798         
1799         entry.add( attrOC );
1800         
1801         assertTrue( entry.hasObjectClass( attrOC ) );
1802 
1803         EntryAttribute attrOC2 = new DefaultServerAttribute( atOC, "person" );
1804         assertTrue( entry.hasObjectClass( attrOC2 ) );
1805 
1806         EntryAttribute attrOC3 = new DefaultServerAttribute( atOC, "inetOrgPerson" );
1807         assertFalse( entry.hasObjectClass( attrOC3 ) );
1808         assertFalse( entry.hasObjectClass( (EntryAttribute)null ) );
1809 
1810         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "top" );
1811         assertFalse( entry.hasObjectClass( attrCN ) );
1812     }
1813 
1814     
1815     /**
1816      * Test method for hasObjectClass( String )
1817      */
1818     @Test
1819     public void testHasObjectClassString() throws Exception
1820     {
1821         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1822         
1823         assertFalse( entry.containsAttribute( "objectClass" ) );
1824         assertFalse( entry.hasObjectClass( "top" ) );
1825         
1826         entry.add( new DefaultServerAttribute( atOC, "top", "person" ) );
1827         
1828         assertTrue( entry.hasObjectClass( "top" ) );
1829         assertTrue( entry.hasObjectClass( "person" ) );
1830         assertFalse( entry.hasObjectClass( "inetorgperson" ) );
1831         assertFalse( entry.hasObjectClass( null ) );
1832         assertFalse( entry.hasObjectClass( "" ) );
1833     }
1834 
1835     
1836     /**
1837      * Test method for isValid()
1838      */
1839     @Test
1840     public void testIsValid()
1841     {
1842         // @TODO Implement me !
1843         assertTrue( true );
1844     }
1845 
1846 
1847     /**
1848      * Test method for isValid( AttributeType )
1849      */
1850     @Test
1851     public void testIsValidAttributeType()
1852     {
1853         // @TODO Implement me !
1854         assertTrue( true );
1855     }
1856     
1857     
1858     /**
1859      * Test method for isValid( String )
1860      */
1861     @Test
1862     public void testIsValidString()
1863     {
1864         // @TODO Implement me !
1865         assertTrue( true );
1866     }
1867     
1868     
1869     /**
1870      * Test method for Iterator()
1871      */
1872     @Test
1873     public void testIterator() throws Exception
1874     {
1875         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1876         
1877         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
1878         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
1879         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
1880         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
1881         
1882         entry.put( attrOC, attrCN, attrSN, attrPWD );
1883         
1884         Iterator<EntryAttribute> iterator = entry.iterator();
1885         
1886         assertTrue( iterator.hasNext() );
1887         
1888         Set<AttributeType> expectedIds = new HashSet<AttributeType>();
1889         expectedIds.add( atOC );
1890         expectedIds.add( atCN );
1891         expectedIds.add( atSN );
1892         expectedIds.add( atPwd );
1893         
1894         while ( iterator.hasNext() )
1895         {
1896             EntryAttribute attribute = iterator.next();
1897             
1898             AttributeType attributeType = ((ServerAttribute)attribute).getAttributeType();
1899             assertTrue( expectedIds.contains( attributeType ) );
1900             expectedIds.remove( attributeType );
1901         }
1902         
1903         assertEquals( 0, expectedIds.size() );
1904     }
1905 
1906     
1907     //-------------------------------------------------------------------------
1908     // Test the Put methods
1909     //-------------------------------------------------------------------------
1910     /**
1911      * Test for method put( AttributeType, byte[]... )
1912      */
1913     @Test
1914     public void testPutAttributeTypeByteArrayArray() throws Exception
1915     {
1916         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1917         
1918         try
1919         {
1920             entry.put( (AttributeType)null, BYTES1 );
1921             fail();
1922         }
1923         catch ( IllegalArgumentException iae )
1924         {
1925             assertTrue( true );
1926         }
1927         
1928         entry.put( atPwd, (byte[])null );
1929         assertEquals( 1, entry.size() );
1930         assertTrue( entry.containsAttribute( atPwd ) );
1931         assertTrue( entry.contains( atPwd, (byte[])null ) );
1932         
1933         EntryAttribute replaced = entry.put( atPwd, BYTES1, BYTES2, BYTES1 );
1934         assertNotNull( replaced );
1935         assertEquals( atPwd, ((ServerAttribute)replaced).getAttributeType() );
1936         assertTrue( replaced.contains( (byte[])null ) );
1937         assertEquals( 1, entry.size() );
1938         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
1939         assertFalse( entry.contains( atPwd, BYTES3 ) );
1940         assertEquals( 2, entry.get( atPwd ).size() );
1941         
1942         replaced = entry.put( atPwd, "test" );
1943         assertNotNull( replaced );
1944         assertTrue( replaced.contains( BYTES1, BYTES2 ) );
1945         
1946         EntryAttribute attribute = entry.get( atPwd );
1947         assertEquals( 0, attribute.size() );
1948     }
1949     
1950     
1951     /**
1952      * Test for method put( AttributeType, String... )
1953      */
1954     @Test
1955     public void testPutAttributeTypeStringArray() throws Exception
1956     {
1957         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1958         
1959         try
1960         {
1961             entry.put( (AttributeType)null, "test" );
1962             fail();
1963         }
1964         catch ( IllegalArgumentException iae )
1965         {
1966             assertTrue( true );
1967         }
1968         
1969         entry.put( atCN, (String)null );
1970         assertEquals( 1, entry.size() );
1971         assertTrue( entry.containsAttribute( atCN) );
1972         assertTrue( entry.contains( atCN, (String)null ) );
1973         
1974         EntryAttribute replaced = entry.put( atCN, "test1", "test2", "test1" );
1975         assertNotNull( replaced );
1976         assertEquals( atCN, ((ServerAttribute)replaced).getAttributeType() );
1977         assertTrue( replaced.contains( (String)null ) );
1978         assertEquals( 1, entry.size() );
1979         assertTrue( entry.contains( atCN, "test1", "test2" ) );
1980         assertFalse( entry.contains( atCN, "test3" ) );
1981         assertEquals( 2, entry.get( atCN ).size() );
1982         
1983         replaced = entry.put( atCN, BYTES1 );
1984         assertNotNull( replaced );
1985         assertTrue( replaced.contains( "test1", "test2" ) );
1986         
1987         EntryAttribute attribute = entry.get( atCN );
1988         assertEquals( 0, attribute.size() );
1989     }
1990     
1991     
1992     /**
1993      * Test for method put( AttributeType, Value<?>... )
1994      */
1995     @Test
1996     public void testPutAttributeTypeValueArray() throws Exception
1997     {
1998         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
1999         
2000         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
2001         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
2002         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
2003         Value<String> strNullValue = new ServerStringValue( atCN, null);
2004 
2005         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
2006 
2007         try
2008         {
2009             entry.put( (AttributeType)null, strValue1 );
2010             fail();
2011         }
2012         catch ( IllegalArgumentException iae )
2013         {
2014             assertTrue( true );
2015         }
2016         
2017         entry.put( atCN, strNullValue );
2018         assertEquals( 1, entry.size() );
2019         assertTrue( entry.containsAttribute( atCN) );
2020         assertTrue( entry.contains( atCN, (String)null ) );
2021         
2022         EntryAttribute replaced = entry.put( atCN, strValue1, strValue2, strValue1 );
2023         assertNotNull( replaced );
2024         assertEquals( atCN, ((ServerAttribute)replaced).getAttributeType() );
2025         assertTrue( replaced.contains( (String)null ) );
2026         assertEquals( 1, entry.size() );
2027         assertTrue( entry.contains( atCN, strValue1, strValue2 ) );
2028         assertFalse( entry.contains( atCN, strValue3 ) );
2029         assertEquals( 2, entry.get( atCN ).size() );
2030         
2031         replaced = entry.put( atCN, binValue1 );
2032         assertNotNull( replaced );
2033         assertTrue( replaced.contains( strValue1, strValue2 ) );
2034         
2035         EntryAttribute attribute = entry.get( atCN );
2036         assertEquals( 0, attribute.size() );
2037     }
2038     
2039     
2040     /**
2041      * Test for method put( EntryAttribute...)
2042      */
2043     @Test
2044     public void testPutEntryAttribute() throws Exception
2045     {
2046         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2047         
2048         EntryAttribute oc = new DefaultServerAttribute( atObjectClass, "top", "person" );
2049         EntryAttribute cn = new DefaultServerAttribute( atCN, "test1", "test2" );
2050         EntryAttribute sn = new DefaultServerAttribute( atSN, "Test1", "Test2" );
2051         EntryAttribute up = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
2052         EntryAttribute c = new DefaultServerAttribute( atC, "FR", "US" );
2053         
2054         List<EntryAttribute> removed = entry.put( oc, cn, sn, c );
2055         
2056         assertEquals( 4, entry.size() );
2057         assertEquals( 0, removed.size() );
2058         assertTrue( entry.containsAttribute( "ObjectClass" ) );
2059         assertTrue( entry.containsAttribute( "CN" ) );
2060         assertTrue( entry.containsAttribute( "  sn  " ) );
2061         assertTrue( entry.containsAttribute( " countryName  " ) );
2062     
2063         EntryAttribute attr = entry.get( "objectclass" );
2064         assertEquals( 2, attr.size() );
2065         
2066         EntryAttribute c2 = new DefaultServerAttribute( atC, "UK", "DE" );
2067         removed = entry.put( c2, up );
2068         assertEquals( 1, removed.size() );
2069         assertEquals( c, removed.get( 0 ) );
2070         assertTrue( removed.get( 0 ).contains( "FR" ) );
2071         assertTrue( removed.get( 0 ).contains( "US" ) );
2072         
2073         assertEquals( 5, entry.size() );
2074         
2075         assertTrue( entry.containsAttribute( "userPassword" ) );
2076         assertTrue( entry.containsAttribute( " countryName " ) );
2077 
2078         EntryAttribute attrC = entry.get( "countryName" );
2079         assertEquals( 2, attrC.size() );
2080         assertTrue( attrC.contains( "UK", "DE" ) );
2081 
2082         c2.clear();
2083         entry.put( c2 );
2084         assertEquals( 5, entry.size() );
2085         attrC = entry.get( "countryName" );
2086         assertEquals( 0, attrC.size() );
2087     }
2088 
2089     
2090     /**
2091      * Test for method put( String, AttributeType, byte[]... )
2092      */
2093     @Test
2094     public void testPutStringAttributeTypeByteArrayArray() throws Exception
2095     {
2096         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2097         
2098         try
2099         {
2100             entry.put( (String)null, (AttributeType)null, BYTES1 );
2101             fail();
2102         }
2103         catch ( IllegalArgumentException iae )
2104         {
2105             assertTrue( true );
2106         }
2107         
2108         try
2109         {
2110             entry.put( " ", (AttributeType)null, BYTES1 );
2111             fail();
2112         }
2113         catch ( IllegalArgumentException iae )
2114         {
2115             assertTrue( true );
2116         }
2117         
2118         try
2119         {
2120             entry.put( "badAttr", (AttributeType)null, BYTES1 );
2121             fail();
2122         }
2123         catch ( IllegalArgumentException iae )
2124         {
2125             assertTrue( true );
2126         }
2127         
2128         try
2129         {
2130             entry.put( "badAttr", atPwd, BYTES1 );
2131             fail();
2132         }
2133         catch ( NoSuchAttributeException nsae )
2134         {
2135             assertTrue( true );
2136         }
2137         
2138         entry.put( "UserPassword", atPwd, (byte[])null );
2139         assertEquals( 1, entry.size() );
2140         assertTrue( entry.containsAttribute( atPwd ) );
2141         assertTrue( entry.contains( atPwd, (byte[])null ) );
2142         
2143         assertEquals( "UserPassword", entry.get( atPwd ).getUpId() );
2144         
2145         EntryAttribute replaced = entry.put( "USERpassword ", atPwd, BYTES1, BYTES2, BYTES1 );
2146         assertNotNull( replaced );
2147         assertEquals( atPwd, ((ServerAttribute)replaced).getAttributeType() );
2148         assertTrue( replaced.contains( (byte[])null ) );
2149         assertEquals( 1, entry.size() );
2150         assertTrue( entry.contains( atPwd, BYTES1, BYTES2 ) );
2151         assertFalse( entry.contains( atPwd, BYTES3 ) );
2152         assertEquals( 2, entry.get( atPwd ).size() );
2153         assertEquals( "USERpassword", entry.get( atPwd ).getUpId() );
2154         
2155         replaced = entry.put( "userpassword", atPwd, "test" );
2156         assertNotNull( replaced );
2157         assertTrue( replaced.contains( BYTES1, BYTES2 ) );
2158         assertEquals( "userpassword", entry.get( atPwd ).getUpId() );
2159         
2160         EntryAttribute attribute = entry.get( atPwd );
2161         assertEquals( 0, attribute.size() );
2162     }
2163     
2164     
2165     /**
2166      * Test for method put( String, AttributeType, String... )
2167      */
2168     @Test
2169     public void testPutStringAttributeTypeStringArray() throws Exception
2170     {
2171         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2172         
2173         try
2174         {
2175             entry.put( (String)null, (AttributeType)null, "test" );
2176             fail();
2177         }
2178         catch ( IllegalArgumentException iae )
2179         {
2180             assertTrue( true );
2181         }
2182         
2183         try
2184         {
2185             entry.put( " ", (AttributeType)null, "test" );
2186             fail();
2187         }
2188         catch ( IllegalArgumentException iae )
2189         {
2190             assertTrue( true );
2191         }
2192         
2193         try
2194         {
2195             entry.put( "badAttr", (AttributeType)null, "test" );
2196             fail();
2197         }
2198         catch ( NoSuchAttributeException nsae )
2199         {
2200             assertTrue( true );
2201         }
2202         
2203         try
2204         {
2205             entry.put( "badAttr", atCN, "test" );
2206             fail();
2207         }
2208         catch ( NoSuchAttributeException nsae )
2209         {
2210             assertTrue( true );
2211         }
2212         
2213         entry.put( "CN", atCN, (String)null );
2214         assertEquals( 1, entry.size() );
2215         assertTrue( entry.containsAttribute( atCN) );
2216         assertTrue( entry.contains( atCN, (String)null ) );
2217         assertEquals( "CN", entry.get( atCN ).getUpId() );
2218         
2219         EntryAttribute replaced = entry.put( "commonName", atCN, "test1", "test2", "test1" );
2220         assertNotNull( replaced );
2221         assertEquals( atCN, ((ServerAttribute)replaced).getAttributeType() );
2222         assertEquals( "commonName", entry.get( atCN).getUpId() );
2223         assertTrue( replaced.contains( (String)null ) );
2224         assertEquals( 1, entry.size() );
2225         assertTrue( entry.contains( atCN, "test1", "test2" ) );
2226         assertFalse( entry.contains( atCN, "test3" ) );
2227         assertEquals( 2, entry.get( atCN ).size() );
2228         
2229         replaced = entry.put( "2.5.4.3", atCN, BYTES1 );
2230         assertNotNull( replaced );
2231         assertTrue( replaced.contains( "test1", "test2" ) );
2232         assertEquals( "2.5.4.3", entry.get( atCN).getUpId() );
2233         
2234         EntryAttribute attribute = entry.get( atCN );
2235         assertEquals( 0, attribute.size() );
2236     }
2237     
2238     
2239     /**
2240      * Test for method put( String, AttributeType, Value<?>... )
2241      */
2242     @Test
2243     public void testPutStringAttributeTypeValueArray() throws Exception
2244     {
2245         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2246         
2247         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
2248         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
2249         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
2250         Value<String> strNullValue = new ServerStringValue( atCN, null);
2251 
2252         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
2253 
2254         try
2255         {
2256             entry.put( (String)null, (AttributeType)null, strValue1 );
2257             fail();
2258         }
2259         catch ( IllegalArgumentException iae )
2260         {
2261             assertTrue( true );
2262         }
2263         
2264         try
2265         {
2266             entry.put( " ", (AttributeType)null, strValue1 );
2267             fail();
2268         }
2269         catch ( IllegalArgumentException iae )
2270         {
2271             assertTrue( true );
2272         }
2273         
2274         try
2275         {
2276             entry.put( "badAttr", (AttributeType)null, strValue1 );
2277             fail();
2278         }
2279         catch ( IllegalArgumentException iae )
2280         {
2281             assertTrue( true );
2282         }
2283         
2284         try
2285         {
2286             entry.put( "badAttr", atCN, strValue1 );
2287             fail();
2288         }
2289         catch ( NoSuchAttributeException nsae )
2290         {
2291             assertTrue( true );
2292         }
2293         
2294         entry.put( "Cn", atCN, strNullValue );
2295         assertEquals( 1, entry.size() );
2296         assertTrue( entry.containsAttribute( atCN) );
2297         assertTrue( entry.contains( atCN, (String)null ) );
2298         assertEquals( "Cn", entry.get( atCN ).getUpId() );
2299         
2300         EntryAttribute replaced = entry.put( "commonName", atCN, strValue1, strValue2, strValue1 );
2301         assertNotNull( replaced );
2302         assertEquals( atCN, ((ServerAttribute)replaced).getAttributeType() );
2303         assertTrue( replaced.contains( (String)null ) );
2304         assertEquals( 1, entry.size() );
2305         assertTrue( entry.contains( atCN, strValue1, strValue2 ) );
2306         assertFalse( entry.contains( atCN, strValue3 ) );
2307         assertEquals( 2, entry.get( atCN ).size() );
2308         assertEquals( "commonName", entry.get( atCN ).getUpId() );
2309         
2310         replaced = entry.put( "2.5.4.3", atCN, binValue1 );
2311         assertNotNull( replaced );
2312         assertTrue( replaced.contains( strValue1, strValue2 ) );
2313         
2314         EntryAttribute attribute = entry.get( atCN );
2315         assertEquals( 0, attribute.size() );
2316         assertEquals( "2.5.4.3", entry.get( atCN ).getUpId() );
2317     }
2318     
2319     
2320     /**
2321      * Test method for put( String, byte[]... )
2322      */
2323     @Test
2324     public void testPutStringByteArrayArray()
2325     {
2326         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2327         
2328         try
2329         {
2330             entry.put( (String)null, BYTES1 );
2331             fail();
2332         }
2333         catch ( IllegalArgumentException iae)
2334         {
2335             assertTrue( true );
2336         }
2337         
2338         try
2339         {
2340             entry.put( "   ", BYTES1 );
2341             fail();
2342         }
2343         catch ( IllegalArgumentException iae)
2344         {
2345             assertTrue( true );
2346         }
2347         
2348         try
2349         {
2350             entry.put( "userAssword", BYTES1 );
2351             fail();
2352         }
2353         catch ( IllegalArgumentException iae )
2354         {
2355             assertTrue( true );
2356         }
2357         
2358         EntryAttribute replaced = entry.put( "userPassword", (byte[])null );
2359         assertNull( replaced );
2360         assertEquals( 1, entry.size() );
2361         assertNotNull( entry.get( "userPassword" ) );
2362         assertEquals( 1, entry.get( "userPassword" ).size() );
2363         assertNull( entry.get( "userPassword" ).get().get() );
2364         
2365         replaced = entry.put( "UserPassword", BYTES1 );
2366         assertNotNull( replaced );
2367         assertEquals( atPwd, ((ServerAttribute)replaced).getAttributeType() );
2368         assertTrue( replaced.contains( (byte[] )null ) );
2369         assertEquals( 1, entry.size() );
2370         assertNotNull( entry.get( "userPassword" ) );
2371         assertEquals( 1, entry.get( "userPassword" ).size() );
2372         assertNotNull( entry.get( "userPassword" ).get().get() );
2373         assertTrue( entry.get( "userPassword" ).contains( BYTES1 ) );
2374         
2375         replaced = entry.put(  "jpegPhoto", BYTES1, BYTES2, BYTES1 );
2376         assertNull( replaced );
2377         assertEquals( 2, entry.size() );
2378         assertNotNull( entry.get( "jpegPhoto" ) );
2379         assertEquals( 2, entry.get( "JPEGPhoto" ).size() );
2380         EntryAttribute attribute = entry.get( "jpegPhoto" );
2381         assertTrue( attribute.contains( BYTES1 ) );
2382         assertTrue( attribute.contains( BYTES2 ) );
2383         assertEquals( "jpegphoto", attribute.getId() );
2384         assertEquals( "jpegPhoto", attribute.getUpId() );
2385     }
2386 
2387 
2388     /**
2389      * Test method for put( String, String... )
2390      */
2391     @Test
2392     public void testPutStringStringArray()
2393     {
2394         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2395         
2396         try
2397         {
2398             entry.put( (String)null, "test" );
2399             fail();
2400         }
2401         catch ( IllegalArgumentException iae)
2402         {
2403             assertTrue( true );
2404         }
2405         
2406         try
2407         {
2408             entry.put( "   ", "test" );
2409             fail();
2410         }
2411         catch ( IllegalArgumentException iae)
2412         {
2413             assertTrue( true );
2414         }
2415         
2416         try
2417         {
2418             entry.put( "cnn", "test" );
2419             fail();
2420         }
2421         catch ( IllegalArgumentException iae )
2422         {
2423             assertTrue( true );
2424         }
2425         
2426         EntryAttribute replaced = entry.put( "description", (String)null );
2427         assertNull( replaced );
2428         assertEquals( 1, entry.size() );
2429         assertNotNull( entry.get( "description" ) );
2430         assertEquals( 1, entry.get( "description" ).size() );
2431         assertNotNull( entry.get( "description" ).get() );
2432         assertNull( entry.get( "description" ).get().get() );
2433         
2434         replaced = entry.put( "CN", "test" );
2435         assertNull( replaced );
2436         assertEquals( 2, entry.size() );
2437         assertNotNull( entry.get( "cn" ) );
2438         assertEquals( 1, entry.get( "cn" ).size() );
2439         assertNotNull( entry.get( "cn" ).get().get() );
2440         assertTrue( entry.get( "cn" ).contains( "test" ) );
2441         
2442         replaced = entry.put(  "cN", "test1", "test2", "test1" );
2443         assertNotNull( replaced );
2444         assertEquals( "test", replaced.get().get() );
2445         
2446         assertEquals( 2, entry.size() );
2447         assertNotNull( entry.get( "cn" ) );
2448         assertEquals( 2, entry.get( "CN" ).size() );
2449         
2450         EntryAttribute attribute = entry.get( "cn" );
2451         assertTrue( attribute.contains( "test1" ) );
2452         assertTrue( attribute.contains( "test2" ) );
2453         assertEquals( "cn", attribute.getId() );
2454         assertEquals( "cN", attribute.getUpId() );
2455     }
2456 
2457 
2458     /**
2459      * Test method for put( String, Value<?>... )
2460      */
2461     @Test
2462     public void testPutStringValueArray()
2463     {
2464         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
2465         
2466         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
2467         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
2468         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
2469         Value<String> strNullValue = new ServerStringValue( atCN, null);
2470 
2471         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
2472 
2473         try
2474         {
2475             entry.put( (String)null, strValue1 );
2476             fail();
2477         }
2478         catch ( IllegalArgumentException iae)
2479         {
2480             assertTrue( true );
2481         }
2482         
2483         try
2484         {
2485             entry.put( "   ", strValue1 );
2486             fail();
2487         }
2488         catch ( IllegalArgumentException iae)
2489         {
2490             assertTrue( true );
2491         }
2492         
2493         try
2494         {
2495             entry.put( "cnn", strValue1 );
2496             fail();
2497         }
2498         catch ( IllegalArgumentException iae )
2499         {
2500             assertTrue( true );
2501         }
2502         
2503         EntryAttribute replaced = entry.put( "description", strNullValue );
2504         assertNull( replaced );
2505         assertEquals( 1, entry.size() );
2506         assertNotNull( entry.get( "description" ) );
2507         assertEquals( 1, entry.get( "description" ).size() );
2508         assertNotNull( entry.get( "description" ).get() );
2509         assertNull( entry.get( "description" ).get().get() );
2510         
2511         replaced = entry.put( "CN", strValue3 );
2512         assertNull( replaced );
2513         assertEquals( 2, entry.size() );
2514         assertNotNull( entry.get( "cn" ) );
2515         assertEquals( 1, entry.get( "cn" ).size() );
2516         assertNotNull( entry.get( "cn" ).get().get() );
2517         assertTrue( entry.get( "cn" ).contains( strValue3 ) );
2518         
2519         replaced = entry.put(  "cN", strValue1, strValue2, strValue1 );
2520         assertNotNull( replaced );
2521         assertEquals( strValue3, replaced.get() );
2522         
2523         assertEquals( 2, entry.size() );
2524         assertNotNull( entry.get( "cn" ) );
2525         assertEquals( 2, entry.get( "CN" ).size() );
2526         
2527         EntryAttribute attribute = entry.get( "cn" );
2528         assertTrue( attribute.contains( strValue1 ) );
2529         assertTrue( attribute.contains( strValue2 ) );
2530         assertEquals( "cn", attribute.getId() );
2531         assertEquals( "cN", attribute.getUpId() );
2532         
2533         // Bin values are not allowed, so the new CN will be empty
2534         entry.put( "cn", binValue1 );
2535         assertNull( entry.get( "cn" ).get() );
2536     }
2537 
2538 
2539     /**
2540      * Test the put( SA... ) method
2541      */
2542     @Test public void tesPutServerAttributeElipsis() throws Exception
2543     {
2544         LdapDN dn = new LdapDN( "cn=test" );
2545         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2546 
2547         // first test a null SA addition. It should be allowed.
2548         try
2549         {
2550             entry.put( (ServerAttribute)null );
2551             fail();
2552         }
2553         catch ( IllegalArgumentException iae )
2554         {
2555             assertTrue( true );
2556         }
2557         
2558         // Adding some serverAttributes
2559         //AttributeType atCo = registries.getAttributeTypeRegistry().lookup( "countryName" );
2560         AttributeType atGN = registries.getAttributeTypeRegistry().lookup( "givenname" );
2561         AttributeType atStreet = registries.getAttributeTypeRegistry().lookup( "2.5.4.9" );
2562 
2563         ServerAttribute sa = new DefaultServerAttribute( atL, "france" );
2564         entry.put( sa );
2565         
2566         assertEquals( 1, entry.size() );
2567         assertNotNull( entry.get( "l" ) );
2568         assertEquals( "france", entry.get( "l" ).get().get() );
2569         
2570         ServerAttribute sb = new DefaultServerAttribute( atC, "countryTest" );
2571         ServerAttribute sc = new DefaultServerAttribute( atGN, "test" );
2572         ServerAttribute sd = new DefaultServerAttribute( atStreet, "testStreet" );
2573         entry.put( sb, sc, sd );
2574 
2575         assertEquals( 4, entry.size() );
2576         assertNotNull( entry.get( atC ) );
2577         assertEquals( "countryTest", entry.get( atC ).get().get() );
2578         assertNotNull( entry.get( atGN ) );
2579         assertEquals( "test", entry.get( atGN ).get().get() );
2580         assertNotNull( entry.get( atStreet) );
2581         assertEquals( "testStreet", entry.get( atStreet ).get().get() );
2582         
2583         // Test a replacement
2584         EntryAttribute sbb = new DefaultServerAttribute( atC, "countryTestTest" );
2585         EntryAttribute scc = new DefaultServerAttribute( atGN, "testtest" );
2586         List<EntryAttribute> result = entry.put( sbb, scc );
2587         
2588         assertEquals( 2, result.size() );
2589         assertEquals( "countryTest", result.get(0).get().get() );
2590         assertEquals( "test", result.get(1).get().get() );
2591         assertEquals( 4, entry.size() );
2592         assertNotNull( entry.get( atC ) );
2593         assertEquals( "countryTestTest", entry.get( atC ).get().get() );
2594         assertNotNull( entry.get( atGN ) );
2595         assertEquals( "testtest", entry.get( atGN ).get().get() );
2596         assertNotNull( entry.get( atStreet) );
2597         assertEquals( "testStreet", entry.get( atStreet ).get().get() );
2598         
2599         // test an ObjectClass replacement
2600         AttributeType OBJECT_CLASS_AT = registries.getAttributeTypeRegistry().lookup( SchemaConstants.OBJECT_CLASS_AT );
2601         ServerAttribute oc = new DefaultServerAttribute( "OBJECTCLASS", OBJECT_CLASS_AT, "person", "inetorgperson" );
2602         List<EntryAttribute> oldOc = entry.put( oc );
2603         
2604         assertNotNull( oldOc );
2605         assertEquals( 0, oldOc.size() );
2606         
2607         assertNotNull( entry.get( "objectClass" ) );
2608 
2609         EntryAttribute newOc = entry.get( "objectClass" );
2610         
2611         assertNotNull( newOc );
2612         assertEquals( OBJECT_CLASS_AT, ((ServerAttribute)newOc).getAttributeType() );
2613         assertEquals( 2, newOc.size() );
2614         assertEquals( "OBJECTCLASS", newOc.getUpId() );
2615         assertTrue( newOc.contains( "person", "inetOrgPerson" ) );
2616     }
2617 
2618     
2619     /**
2620      * Test the put( AT, String... ) method
2621      */
2622     @Test public void tesPutAtStringElipsis() throws Exception
2623     {
2624         LdapDN dn = new LdapDN( "cn=test" );
2625         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2626         
2627         // Test an empty AT
2628         entry.put( atCN, (String)null );
2629         assertEquals( 1, entry.size() );
2630         assertEquals( "cn", entry.get( atCN ).getUpId() );
2631         assertNull( entry.get( atCN ).get().get() );
2632         
2633         // Check that we can't use invalid arguments
2634         try
2635         {
2636             entry.put( (AttributeType)null, (String)null );
2637             fail();
2638         }
2639         catch( IllegalArgumentException iae )
2640         {
2641             assertTrue( true );
2642         }
2643         
2644         // Add a single value
2645         atCN = registries.getAttributeTypeRegistry().lookup( "cn" );
2646         entry.put( atCN, "test" );
2647         
2648         assertEquals( 1, entry.size() );
2649         assertEquals( "cn", entry.get( atCN ).getUpId() );
2650         assertEquals( 1, entry.get( atCN ).size() );
2651         assertEquals( "test", entry.get( atCN ).get().get() );
2652         
2653         // Add more than one value
2654         entry.put( atCN, "test1", "test2", "test3" );
2655         
2656         assertEquals( 1, entry.size() );
2657         assertEquals( "cn", entry.get( atCN ).getUpId() );
2658         assertEquals( 3, entry.get( atCN ).size() );
2659         assertTrue( entry.contains( "cn", "test1" ) );
2660         assertTrue( entry.contains( "cn", "test2" ) );
2661         assertTrue( entry.contains( "cn", "test3" ) );
2662         
2663         // Add twice the same value
2664         EntryAttribute sa = entry.put( atCN, "test1", "test2", "test1" );
2665         
2666         assertEquals( 3, sa.size() );
2667         assertTrue( sa.contains( "test1", "test2", "test3" ) );
2668         assertEquals( 1, entry.size() );
2669         assertEquals( "cn", entry.get( atCN ).getUpId() );
2670         assertEquals( 2, entry.get( atCN ).size() );
2671         assertTrue( entry.contains( "cn", "test1" ) );
2672         assertTrue( entry.contains( "cn", "test2" ) );
2673     }
2674     
2675 
2676     /**
2677      * Test the put( AT, Byte[]... ) method
2678      */
2679     @Test public void tesPutAtByteElipsis() throws Exception
2680     {
2681         LdapDN dn = new LdapDN( "cn=test" );
2682         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2683         
2684         // Test an empty AT
2685         entry.put( atPwd, (byte[])null );
2686         assertEquals( 1, entry.size() );
2687         assertEquals( "userPassword", entry.get( atPwd ).getUpId() );
2688         assertNull( entry.get( atPwd ).get().get() );
2689         
2690         // Check that we can't use invalid arguments
2691         try
2692         {
2693             entry.put( (AttributeType)null, (byte[])null );
2694             fail();
2695         }
2696         catch( IllegalArgumentException iae )
2697         {
2698             assertTrue( true );
2699         }
2700         
2701         byte[] password = StringTools.getBytesUtf8( "test" );
2702         byte[] test1 = StringTools.getBytesUtf8( "test1" );
2703         byte[] test2 = StringTools.getBytesUtf8( "test2" );
2704         byte[] test3 = StringTools.getBytesUtf8( "test3" );
2705         
2706         // Add a single value
2707         atPwd = registries.getAttributeTypeRegistry().lookup( "userPassword" );
2708         entry.put( atPwd, password );
2709         
2710         assertEquals( 1, entry.size() );
2711         assertEquals( "userPassword", entry.get( atPwd ).getUpId() );
2712         assertEquals( 1, entry.get( atPwd ).size() );
2713         assertTrue( Arrays.equals( password, (byte[])entry.get( atPwd ).get().get() ) );
2714         
2715         // Add more than one value
2716         entry.put( atPwd, test1, test2, test3 );
2717         
2718         assertEquals( 1, entry.size() );
2719         assertEquals( "userPassword", entry.get( atPwd ).getUpId() );
2720         assertEquals( 3, entry.get( atPwd ).size() );
2721         assertTrue( entry.contains( "userpassword", test1 ) );
2722         assertTrue( entry.contains( "userpassword", test2 ) );
2723         assertTrue( entry.contains( "userpassword", test3 ) );
2724         
2725         // Add twice the same value
2726         EntryAttribute sa = entry.put( atPwd, test1, test2, test1 );
2727         
2728         assertEquals( 3, sa.size() );
2729         assertTrue( sa.contains( test1, test2, test3 ) );
2730         assertEquals( 1, entry.size() );
2731         assertEquals( "userPassword", entry.get( atPwd ).getUpId() );
2732         assertEquals( 2, entry.get( atPwd ).size() );
2733         assertTrue( entry.contains( "userpassword", test1 ) );
2734         assertTrue( entry.contains( "userpassword", test2 ) );
2735     }
2736     
2737 
2738     /**
2739      * Test the put( AT, Value... ) method
2740      */
2741     @Test public void tesPutAtSVs() throws Exception
2742     {
2743         LdapDN dn = new LdapDN( "cn=test" );
2744         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2745         
2746         // Adding a null value to an attribute
2747         entry.put( atCN, (Value<?>)null );
2748         
2749         assertEquals( 1, entry.size() );
2750         assertEquals( "cn", entry.get( atCN ).getUpId() );
2751         
2752         // Check that we can't use invalid arguments
2753         try
2754         {
2755             entry.put( (AttributeType)null, (Value<?>)null );
2756             fail();
2757         }
2758         catch( IllegalArgumentException iae )
2759         {
2760             assertTrue( true );
2761         }
2762         
2763         // Add a single value
2764         atCN = registries.getAttributeTypeRegistry().lookup( "cn" );
2765         Value<?> ssv = new ServerStringValue( atCN, "test" );
2766         entry.put( atCN, ssv );
2767         
2768         assertEquals( 1, entry.size() );
2769         assertEquals( "cn", entry.get( atCN ).getUpId() );
2770         assertEquals( 1, entry.get( atCN ).size() );
2771         assertEquals( "test", entry.get( atCN ).get().get() );
2772         
2773         // Add more than one value
2774         entry.put( atCN, new ServerStringValue( atCN, "test1" ),
2775                          new ServerStringValue( atCN, "test2" ), 
2776                          new ServerStringValue( atCN, "test3" ));
2777         
2778         assertEquals( 1, entry.size() );
2779         assertEquals( "cn", entry.get( atCN ).getUpId() );
2780         assertEquals( 3, entry.get( atCN ).size() );
2781         assertTrue( entry.contains( "cn", "test1" ) );
2782         assertTrue( entry.contains( "cn", "test2" ) );
2783         assertTrue( entry.contains( "cn", "test3" ) );
2784         
2785         // Add twice the same value
2786         EntryAttribute sa = entry.put( atCN, new ServerStringValue( atCN, "test1" ),
2787                          new ServerStringValue( atCN, "test2" ), 
2788                          new ServerStringValue( atCN, "test1" ));
2789         
2790         assertEquals( 3, sa.size() );
2791         assertTrue( sa.contains( "test1", "test2", "test3" ) );
2792         assertEquals( 1, entry.size() );
2793         assertEquals( "cn", entry.get( atCN ).getUpId() );
2794         assertEquals( 2, entry.get( atCN ).size() );
2795         assertTrue( entry.contains( "cn", "test1" ) );
2796         assertTrue( entry.contains( "cn", "test2" ) );
2797     }
2798 
2799 
2800     /**
2801      * Test the put( upId, String... ) method
2802      */
2803     @Test public void tesPutUpIdStringElipsis() throws Exception
2804     {
2805         LdapDN dn = new LdapDN( "cn=test" );
2806         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2807         
2808         // Adding a null value should be possible
2809         entry.put( "cn", (String)null );
2810         
2811         assertEquals( 1, entry.size() );
2812         assertEquals( "cn", entry.get( atCN ).getUpId() );
2813         assertNull( entry.get( atCN ).get().get() );
2814         
2815         // Check that we can't use invalid arguments
2816         try
2817         {
2818             entry.put( (String)null, (String)null );
2819             fail();
2820         }
2821         catch( IllegalArgumentException iae )
2822         {
2823             assertTrue( true );
2824         }
2825         
2826         // Add a single value
2827         atCN = registries.getAttributeTypeRegistry().lookup( "cn" );
2828         entry.put( "cn", "test" );
2829         
2830         assertEquals( 1, entry.size() );
2831         assertEquals( "cn", entry.get( atCN ).getUpId() );
2832         assertEquals( 1, entry.get( atCN ).size() );
2833         assertEquals( "test", entry.get( atCN ).get().get() );
2834         
2835         // Add more than one value
2836         entry.put( "cn", "test1", "test2", "test3" );
2837         
2838         assertEquals( 1, entry.size() );
2839         assertEquals( "cn", entry.get( atCN ).getUpId() );
2840         assertEquals( 3, entry.get( atCN ).size() );
2841         assertTrue( entry.contains( "cn", "test1" ) );
2842         assertTrue( entry.contains( "cn", "test2" ) );
2843         assertTrue( entry.contains( "cn", "test3" ) );
2844         
2845         // Add twice the same value
2846         EntryAttribute sa = entry.put( "cn", "test1", "test2", "test1" );
2847         
2848         assertEquals( 3, sa.size() );
2849         assertTrue( sa.contains( "test1", "test2", "test3" ) );
2850         assertEquals( 1, entry.size() );
2851         assertEquals( "cn", entry.get( atCN ).getUpId() );
2852         assertEquals( 2, entry.get( atCN ).size() );
2853         assertTrue( entry.contains( "cn", "test1" ) );
2854         assertTrue( entry.contains( "cn", "test2" ) );
2855         
2856         // Check the UpId
2857         entry.put( "CN", "test4" );
2858         assertEquals( "CN", entry.get( atCN ).getUpId() );
2859     }
2860     
2861 
2862     /**
2863      * Test the put( upId, byte[]... ) method
2864      */
2865     @Test public void tesPutUpIdBytesElipsis() throws Exception
2866     {
2867         LdapDN dn = new LdapDN( "cn=test" );
2868         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2869         
2870         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
2871         
2872         // Adding a null value should be possible
2873         entry.put( "userPassword", (byte[])null );
2874         assertEquals( 1, entry.size() );
2875         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
2876         assertNull( entry.get( atPassword ).get().get() );
2877         
2878         // Check that we can't use invalid arguments
2879         try
2880         {
2881             entry.put( (String)null, (String)null );
2882             fail();
2883         }
2884         catch( IllegalArgumentException iae )
2885         {
2886             assertTrue( true );
2887         }
2888         
2889         // Add a single value
2890         byte[] test = StringTools.getBytesUtf8( "test" );
2891         byte[] test1 = StringTools.getBytesUtf8( "test1" );
2892         byte[] test2 = StringTools.getBytesUtf8( "test2" );
2893         byte[] test3 = StringTools.getBytesUtf8( "test3" );
2894         
2895         entry.put( "userPassword", test );
2896         
2897         assertEquals( 1, entry.size() );
2898         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
2899         assertEquals( 1, entry.get( atPassword ).size() );
2900         assertTrue( Arrays.equals( test, (byte[])entry.get( atPassword ).get().get() ) );
2901         
2902         // Add more than one value
2903         entry.put( "userPassword", test1, test2, test3 );
2904         
2905         assertEquals( 1, entry.size() );
2906         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
2907         assertEquals( 3, entry.get( atPassword ).size() );
2908         assertTrue( entry.contains( "userPassword", test1 ) );
2909         assertTrue( entry.contains( "userPassword", test2 ) );
2910         assertTrue( entry.contains( "userPassword", test3 ) );
2911         
2912         // Add twice the same value
2913         EntryAttribute sa = entry.put( "userPassword", test1, test2, test1 );
2914         
2915         assertEquals( 3, sa.size() );
2916         assertTrue( sa.contains( test1, test2, test3 ) );
2917         assertEquals( 1, entry.size() );
2918         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
2919         assertEquals( 2, entry.get( atPassword ).size() );
2920         assertTrue( entry.contains( "userPassword", test1 ) );
2921         assertTrue( entry.contains( "userPassword", test2 ) );
2922     }
2923 
2924 
2925     /**
2926      * Test the put( upId, AT, String... ) method
2927      */
2928     @Test public void tesPutUpIDAtStringElipsis() throws Exception
2929     {
2930         LdapDN dn = new LdapDN( "cn=test" );
2931         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2932         
2933         // Test that we get an error when the ID or AT are null
2934         try
2935         {
2936             entry.put( null, (AttributeType)null, (String)null );
2937             fail();
2938         }
2939         catch( IllegalArgumentException iae )
2940         {
2941             assertTrue( true );
2942         }
2943         
2944         // Test an empty AT
2945         entry.put( "commonName", atCN, (String)null );
2946         assertEquals( 1, entry.size() );
2947         assertEquals( "commonName", entry.get( atCN ).getUpId() );
2948         assertTrue( entry.containsAttribute( "cn" ) );
2949         assertNull( entry.get( atCN ).get().get() );
2950         
2951         // Check that we can use a null AttributeType
2952         entry.put( "commonName", (AttributeType)null, (String)null );
2953         assertEquals( 1, entry.size() );
2954         assertEquals( "commonName", entry.get( atCN ).getUpId() );
2955         assertTrue( entry.containsAttribute( "cn" ) );
2956         assertNull( entry.get( atCN ).get().get() );
2957         
2958         // Test that we can use a null upId
2959         entry.put( null, atCN, (String)null );
2960         assertEquals( 1, entry.size() );
2961         assertEquals( "cn", entry.get( atCN ).getUpId() );
2962         assertTrue( entry.containsAttribute( "cn" ) );
2963         assertNull( entry.get( atCN ).get().get() );
2964 
2965         try
2966         {
2967             entry.put( "sn", atCN, (String)null );
2968             fail();
2969         }
2970         catch ( IllegalArgumentException iae )
2971         {
2972             assertTrue( true );
2973         }
2974         
2975         // Test that we can add some new attributes with values
2976         EntryAttribute result = entry.put( "CN", atCN, "test1", "test2", "test3" );
2977         assertNotNull( result );
2978         assertEquals( "cn", result.getUpId() );
2979         assertEquals( 1, entry.size() );
2980         assertEquals( "CN", entry.get( atCN ).getUpId() );
2981         assertNotNull( entry.get( atCN ).get() );
2982         assertTrue( entry.contains( "cn", "test1" ) );
2983         assertTrue( entry.contains( "CN", "test2" ) );
2984         assertTrue( entry.contains( "commonName", "test3" ) );
2985     }
2986 
2987 
2988     /**
2989      * Test the put( upId, AT, byte[]... ) method
2990      */
2991     @Test public void tesPutUpIDAtBytesElipsis() throws Exception
2992     {
2993         LdapDN dn = new LdapDN( "cn=test" );
2994         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
2995         
2996         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
2997         
2998         // Test that we get an error when the ID or AT are null
2999         try
3000         {
3001             entry.put( null, (AttributeType)null, (String)null );
3002             fail();
3003         }
3004         catch( IllegalArgumentException iae )
3005         {
3006             assertTrue( true );
3007         }
3008         
3009         // Test an empty AT
3010         entry.put( "userPassword", atPassword, (byte[])null );
3011         assertEquals( 1, entry.size() );
3012         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
3013         assertTrue( entry.containsAttribute( "userPassword" ) );
3014         assertNull( entry.get( atPassword ).get().get() );
3015         
3016         // Check that we can use a null AttributeType
3017         try
3018         {
3019             entry.put( "userPassword", (AttributeType)null, (byte[])null );
3020             fail();
3021         }
3022         catch ( IllegalArgumentException iae )
3023         {
3024             assertTrue( true );
3025         }
3026         
3027         assertEquals( 1, entry.size() );
3028         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
3029         assertTrue( entry.containsAttribute( "userPassword" ) );
3030         assertNull( entry.get( atPassword ).get().get() );
3031         
3032         // Test that we can use a null upId
3033         entry.put( null, atPassword, (byte[])null );
3034         assertEquals( 1, entry.size() );
3035         assertEquals( "userPassword", entry.get( atPassword ).getUpId() );
3036         assertTrue( entry.containsAttribute( "userPassword" ) );
3037         assertNull( entry.get( atPassword ).get().get() );
3038         
3039         // Test that if we use an upId which is not compatible
3040         // with the AT, it is changed to the AT default name
3041         try
3042         {
3043             entry.put( "sn", atPassword, (byte[])null );
3044             fail();
3045         }
3046         catch( IllegalArgumentException iae )
3047         {
3048             assertTrue( true );
3049         }
3050         
3051         assertEquals( "userpassword", entry.get( atPassword ).getId() );
3052         
3053         // Test that we can add some new attributes with values
3054         byte[] test1 = StringTools.getBytesUtf8( "test1" );
3055         byte[] test2 = StringTools.getBytesUtf8( "test2" );
3056         byte[] test3 = StringTools.getBytesUtf8( "test3" );
3057 
3058         EntryAttribute result = entry.put( "UserPassword", atPassword, test1, test2, test3 );
3059         assertNotNull( result );
3060         assertEquals( "userPassword", result.getUpId() );
3061         assertEquals( 1, entry.size() );
3062         assertEquals( "UserPassword", entry.get( atPassword ).getUpId() );
3063         assertNotNull( entry.get( atPassword ).get() );
3064         assertEquals( 3, entry.get( atPassword ).size() );
3065         assertTrue( entry.contains( "UserPassword", test1 ) );
3066         assertTrue( entry.contains( "userPassword", test2 ) );
3067         assertTrue( entry.contains( "2.5.4.35", test3 ) );
3068     }
3069 
3070 
3071     /**
3072      * Test the put( upId, AT, SV... ) method
3073      */
3074     @Test public void tesPutUpIDAtSVElipsis() throws Exception
3075     {
3076         LdapDN dn = new LdapDN( "cn=test" );
3077         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
3078         
3079         // Test that we get an error when the ID or AT are null
3080         try
3081         {
3082             entry.put( null, (AttributeType)null, (Value<?>)null );
3083             fail();
3084         }
3085         catch( IllegalArgumentException iae )
3086         {
3087             assertTrue( true );
3088         }
3089         
3090         // Test an empty AT
3091         entry.put( "commonName", atCN, (Value<?>)null );
3092         assertEquals( 1, entry.size() );
3093         assertEquals( "commonName", entry.get( atCN ).getUpId() );
3094         assertTrue( entry.containsAttribute( "cn" ) );
3095         assertNull( entry.get( atCN ).get().get() );
3096         
3097         // Check that we can use a null AttributeType
3098         try
3099         {
3100             entry.put( "commonName", (AttributeType)null, (Value<?>)null );
3101             fail();
3102         }
3103         catch( IllegalArgumentException iae )
3104         {
3105             assertTrue( true );
3106         }
3107 
3108         assertEquals( 1, entry.size() );
3109         assertEquals( "commonName", entry.get( atCN ).getUpId() );
3110         assertTrue( entry.containsAttribute( "cn" ) );
3111         assertNull( entry.get( atCN ).get().get() );
3112         
3113         // Test that we can use a null upId
3114         entry.put( null, atCN, (Value<?>)null );
3115         assertEquals( 1, entry.size() );
3116         assertEquals( "cn", entry.get( atCN ).getUpId() );
3117         assertTrue( entry.containsAttribute( "cn" ) );
3118         assertNull( entry.get( atCN ).get().get() );
3119         
3120         // Test that we can't use an upId which is not compatible
3121         // with the AT
3122         try
3123         {
3124             entry.put( "sn", atCN, (Value<?>)null );
3125             fail();
3126         }
3127         catch( IllegalArgumentException iae )
3128         {
3129             assertTrue( true );
3130         }
3131         
3132         // Test that we can add some new attributes with values
3133         Value<String> test1 = new ServerStringValue( atCN, "test1" );
3134         Value<String> test2 = new ServerStringValue( atCN, "test2" );
3135         Value<String> test3 = new ServerStringValue( atCN, "test3" );
3136 
3137         EntryAttribute result = entry.put( "CN", atCN, test1, test2, test3 );
3138         assertNotNull( result );
3139         assertEquals( "cn", result.getUpId() );
3140         assertEquals( 1, entry.size() );
3141         assertEquals( "CN", entry.get( atCN ).getUpId() );
3142         assertNotNull( entry.get( atCN ).get() );
3143         assertTrue( entry.contains( "cn", "test1" ) );
3144         assertTrue( entry.contains( "CN", "test2" ) );
3145         assertTrue( entry.contains( "commonName", "test3" ) );
3146     }
3147 
3148 
3149     /**
3150      * Test the put( upId, SV... ) method
3151      */
3152     @Test public void tesPutUpIDSVElipsis() throws Exception
3153     {
3154         LdapDN dn = new LdapDN( "cn=test" );
3155         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
3156         
3157         // Test that we get an error when the ID or AT are null
3158         try
3159         {
3160             entry.put( (String)null, (Value<?>)null );
3161             fail();
3162         }
3163         catch( IllegalArgumentException iae )
3164         {
3165             assertTrue( true );
3166         }
3167         
3168         // Test an null valued AT
3169         entry.put( "commonName", (Value<?>)null );
3170         assertEquals( 1, entry.size() );
3171         assertEquals( "commonName", entry.get( atCN ).getUpId() );
3172         assertTrue( entry.containsAttribute( "cn" ) );
3173         assertNull( entry.get( atCN ).get().get() );
3174 
3175         // Test that we can add some new attributes with values
3176         Value<String> test1 = new ServerStringValue( atCN, "test1" );
3177         Value<String> test2 = new ServerStringValue( atCN, "test2" );
3178         Value<String> test3 = new ServerStringValue( atCN, "test3" );
3179 
3180         EntryAttribute result = entry.put( "CN", test1, test2, test3 );
3181         assertNotNull( result );
3182         assertEquals( "commonName", result.getUpId() );
3183         assertEquals( 1, entry.size() );
3184         assertEquals( "CN", entry.get( atCN ).getUpId() );
3185         assertNotNull( entry.get( atCN ).get() );
3186         assertTrue( entry.contains( "cn", "test1" ) );
3187         assertTrue( entry.contains( "CN", "test2" ) );
3188         assertTrue( entry.contains( "commonName", "test3" ) );
3189     }
3190 
3191     
3192     //-------------------------------------------------------------------------
3193     // Test the Remove methods
3194     //-------------------------------------------------------------------------
3195     /**
3196      * Test method for remove( AttributeType, byte[]... )
3197      */
3198     @Test
3199     public void testRemoveAttributeTypeByteArrayArray() throws Exception
3200     {
3201         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3202         
3203         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, (byte[])null, BYTES2 );
3204 
3205         entry.put( attrPWD );
3206         assertTrue( entry.remove( atPwd, (byte[])null ) );
3207         assertTrue( entry.remove( atPwd, BYTES1, BYTES2 ) );
3208         assertFalse( entry.containsAttribute( atPwd ) );
3209         
3210         entry.add( atPwd, BYTES1, (byte[])null, BYTES2 );
3211         assertTrue( entry.remove( atPwd, (byte[])null ) );
3212         assertEquals( 2, entry.get( atPwd ).size() );
3213         assertFalse( entry.contains( atPwd, (byte[])null ) );
3214         assertTrue( entry.remove( atPwd, BYTES1, BYTES3 ) );
3215         assertEquals( 1, entry.get( atPwd ).size() );
3216         assertTrue( entry.contains( atPwd, BYTES2 ) );
3217         assertFalse( entry.contains( atPwd, BYTES1 ) );
3218         
3219         assertFalse( entry.remove( atPwd, BYTES3 ) );
3220         assertFalse( entry.remove( atPwd, new byte[]{ 0x00 } ) );
3221     }
3222 
3223     
3224     /**
3225      * Test method for remove( AttributeType, String... )
3226      */
3227     @Test
3228     public void testRemoveAttributeTypeStringArray() throws Exception
3229     {
3230         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3231         
3232         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", (String)null, "test2" );
3233 
3234         entry.put( attrCN );
3235         assertTrue( entry.remove( atCN, (String)null ) );
3236         assertTrue( entry.remove( atCN, "test1", "test2" ) );
3237         assertFalse( entry.containsAttribute( atCN ) );
3238         
3239         entry.add( atCN, "test1", (String)null, "test2" );
3240         assertTrue( entry.remove( atCN, (String)null ) );
3241         assertEquals( 2, entry.get( atCN ).size() );
3242         assertFalse( entry.contains( atCN, (String)null ) );
3243         assertTrue( entry.remove( atCN, "test1", "test3" ) );
3244         assertEquals( 1, entry.get( atCN ).size() );
3245         assertTrue( entry.contains( atCN, "test2" ) );
3246         assertFalse( entry.contains( atCN, "test1" ) );
3247         
3248         assertFalse( entry.remove( atCN, "test3" ) );
3249         assertFalse( entry.remove( atCN, "test" ) );
3250     }
3251 
3252     
3253     /**
3254      * Test method for remove( AttributeType, Value<?>... )
3255      */
3256     @Test
3257     public void testRemoveAttributeTypeValueArray() throws Exception
3258     {
3259         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3260         
3261         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
3262         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
3263         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
3264         Value<String> strNullValue = new ServerStringValue( atCN, null);
3265 
3266         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
3267 
3268         EntryAttribute attrPWD = new DefaultServerAttribute( atCN, "test1", (String)null, "test2" );
3269 
3270         entry.put( attrPWD );
3271         assertTrue( entry.remove( atCN, strNullValue ) );
3272         assertTrue( entry.remove( atCN, strValue1, strValue2 ) );
3273         assertFalse( entry.containsAttribute( atCN ) );
3274         
3275         entry.add( atCN, strValue1, strNullValue, strValue2 );
3276         assertTrue( entry.remove( atCN, strNullValue ) );
3277         assertEquals( 2, entry.get( atCN ).size() );
3278         assertFalse( entry.contains( atCN, strNullValue ) );
3279         assertTrue( entry.remove( atCN, strValue1, strValue3 ) );
3280         assertEquals( 1, entry.get( atCN ).size() );
3281         assertTrue( entry.contains( atCN, strValue2 ) );
3282         assertFalse( entry.contains( atCN, strValue1 ) );
3283         
3284         assertFalse( entry.remove( atCN, strValue3 ) );
3285         assertFalse( entry.remove( atCN, binValue1 ) );
3286     }
3287 
3288     
3289     /**
3290      * Test method for remove( EntryAttribute... )
3291      */
3292     @Test
3293     public void testRemoveEntryAttribute() throws Exception
3294     {
3295         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3296         
3297         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
3298         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
3299         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
3300         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
3301         
3302         entry.put( attrOC, attrCN, attrSN, attrPWD );
3303         
3304         List<EntryAttribute> removed = entry.remove( attrSN, attrPWD );
3305         
3306         assertEquals( 2, removed.size() ); 
3307         assertEquals( 2, entry.size() );
3308         assertTrue( removed.contains( attrSN ) );
3309         assertTrue( removed.contains( attrPWD ) );
3310         assertTrue( entry.contains( "objectClass", "top", "person" ) );
3311         assertTrue( entry.contains( "cn", "test1", "test2" ) );
3312         assertFalse( entry.containsAttribute( atSN ) );
3313         assertFalse( entry.containsAttribute( "userPassword" ) );
3314 
3315         removed = entry.remove( attrSN, attrPWD );
3316         
3317         assertEquals( 0, removed.size() );
3318     }
3319 
3320 
3321     /**
3322      * Test method for removeAttributes( AttributeType... )
3323      */
3324     @Test
3325     public void testRemoveAttributesAttributeTypeArray() throws Exception
3326     {
3327         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3328 
3329         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
3330         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
3331         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
3332         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
3333         
3334         entry.put( attrOC, attrCN, attrSN, attrPWD );
3335         
3336         entry.removeAttributes( atCN, atSN );
3337         
3338         assertFalse( entry.containsAttribute( "cn", "sn" ) );
3339         assertTrue( entry.containsAttribute( "objectclass", "userpassword" ) );
3340         
3341         List<EntryAttribute> removed = entry.removeAttributes( (AttributeType)null );
3342         assertNull( removed );
3343 
3344         removed = entry.removeAttributes( atC );
3345         assertNull( removed );
3346     }
3347     
3348     
3349     /**
3350      * Test method for removeAttributes( String... )
3351      */
3352     @Test
3353     public void testRemoveAttributesStringArray() throws Exception
3354     {
3355         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3356 
3357         EntryAttribute attrOC = new DefaultServerAttribute( atOC, "top", "person" );
3358         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", "test2" );
3359         EntryAttribute attrSN = new DefaultServerAttribute( atSN, "Test1", "Test2" );
3360         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, BYTES2 );
3361         
3362         entry.put( attrOC, attrCN, attrSN, attrPWD );
3363         
3364         entry.removeAttributes( "CN", "SN" );
3365         
3366         assertFalse( entry.containsAttribute( "cn", "sn" ) );
3367         assertTrue( entry.containsAttribute( "objectclass", "userpassword" ) );
3368         
3369         List<EntryAttribute> removed = entry.removeAttributes( "badId" );
3370         assertNull( removed );
3371         
3372         removed = entry.removeAttributes( "l" );
3373         assertNull( removed );
3374         
3375         removed = entry.removeAttributes( (String )null );
3376         assertNull( removed );
3377     }
3378     
3379     
3380     /**
3381      * Test method for remove( String, byte[]... )
3382      */
3383     @Test
3384     public void testRemoveStringByteArrayArray() throws Exception
3385     {
3386         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3387         
3388         EntryAttribute attrPWD = new DefaultServerAttribute( atPwd, BYTES1, (byte[])null, BYTES2 );
3389 
3390         assertFalse( entry.remove( (String)null, BYTES1 ) );
3391         assertFalse( entry.remove( " ", BYTES1 ) );
3392         assertFalse( entry.remove( "badId", BYTES1 ) );
3393 
3394         entry.put( attrPWD );
3395         assertTrue( entry.remove( "userPassword", (byte[])null ) );
3396         assertTrue( entry.remove( "UserPassword", BYTES1, BYTES2 ) );
3397         assertFalse( entry.containsAttribute( atPwd ) );
3398         
3399         entry.add( atPwd, BYTES1, (byte[])null, BYTES2 );
3400         assertTrue( entry.remove( "userPassword", (byte[])null ) );
3401         assertEquals( 2, entry.get( atPwd ).size() );
3402         assertFalse( entry.contains( atPwd, (byte[])null ) );
3403         assertTrue( entry.remove( "userPassword", BYTES1, BYTES3 ) );
3404         assertEquals( 1, entry.get( atPwd ).size() );
3405         assertTrue( entry.contains( atPwd, BYTES2 ) );
3406         assertFalse( entry.contains( atPwd, BYTES1 ) );
3407         
3408         assertFalse( entry.remove( "userPassword", BYTES3 ) );
3409         assertFalse( entry.remove( "userPassword", new byte[]{ 0x00 } ) );
3410     }
3411 
3412     
3413     /**
3414      * Test method for remove( String, String... )
3415      */
3416     @Test
3417     public void testRemoveStringStringArray() throws Exception
3418     {
3419         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3420         
3421         EntryAttribute attrCN = new DefaultServerAttribute( atCN, "test1", (String)null, "test2" );
3422 
3423         assertFalse( entry.remove( (String)null, "test1" ) );
3424         assertFalse( entry.remove( " ", "test1" ) );
3425         assertFalse( entry.remove( "badId", "test1" ) );
3426 
3427         entry.put( attrCN );
3428         assertTrue( entry.remove( "cn", (String)null ) );
3429         assertTrue( entry.remove( "commonName", "test1", "test2" ) );
3430         assertFalse( entry.containsAttribute( atCN ) );
3431         
3432         entry.add( atCN, "test1", (String)null, "test2" );
3433         assertTrue( entry.remove( "2.5.4.3", (String)null ) );
3434         assertEquals( 2, entry.get( atCN ).size() );
3435         assertFalse( entry.contains( atCN, (byte[])null ) );
3436         assertTrue( entry.remove( "COMMONNAME", "test1", "test3" ) );
3437         assertEquals( 1, entry.get( atCN ).size() );
3438         assertTrue( entry.contains( atCN, "test2" ) );
3439         assertFalse( entry.contains( atCN, "test1" ) );
3440         
3441         assertFalse( entry.remove( "Cn", "test3" ) );
3442         assertFalse( entry.remove( "cN", "whatever" ) );
3443     }
3444 
3445     
3446     /**
3447      * Test method for remove( String, Value<?>... )
3448      */
3449     @Test
3450     public void testRemoveStringValueArray() throws Exception
3451     {
3452         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3453         
3454         Value<String> strValue1 = new ServerStringValue( atCN, "test1" );
3455         Value<String> strValue2 = new ServerStringValue( atCN, "test2" );
3456         Value<String> strValue3 = new ServerStringValue( atCN, "test3" );
3457         Value<String> strNullValue = new ServerStringValue( atCN, null);
3458 
3459         Value<byte[]> binValue1 = new ServerBinaryValue( atPwd, BYTES1 );
3460 
3461         EntryAttribute attrPWD = new DefaultServerAttribute( atCN, "test1", (String)null, "test2" );
3462 
3463         entry.put( attrPWD );
3464         assertTrue( entry.remove( "CN", strNullValue ) );
3465         assertTrue( entry.remove( "CommonName", strValue1, strValue2 ) );
3466         assertFalse( entry.containsAttribute( atCN ) );
3467         
3468         entry.add( atCN, strValue1, strNullValue, strValue2 );
3469         assertTrue( entry.remove( "2.5.4.3", strNullValue ) );
3470         assertEquals( 2, entry.get( atCN ).size() );
3471         assertFalse( entry.contains( atCN, strNullValue ) );
3472         assertTrue( entry.remove( "  cn", strValue1, strValue3 ) );
3473         assertEquals( 1, entry.get( atCN ).size() );
3474         assertTrue( entry.contains( atCN, strValue2 ) );
3475         assertFalse( entry.contains( atCN, strValue1 ) );
3476         
3477         assertFalse( entry.remove( " Cn", strValue3 ) );
3478         assertFalse( entry.remove( "cN ", binValue1 ) );
3479     }
3480 
3481     
3482     /**
3483      * Test the remove( upId...) method
3484      */
3485     @Test public void testRemoveUpIdElipsis() throws Exception
3486     {
3487         LdapDN dn = new LdapDN( "cn=test" );
3488         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
3489         
3490         AttributeType atPassword = registries.getAttributeTypeRegistry().lookup( "userPassword" );
3491         
3492         byte[] b1 = StringTools.getBytesUtf8( "test1" );
3493         byte[] b2 = StringTools.getBytesUtf8( "test2" );
3494 
3495         Value<String> test1 = new ServerStringValue( atCN, "test1" );
3496         Value<String> test2 = new ServerStringValue( atCN, "test2" );
3497         
3498         Value<byte[]> testB1 = new ServerBinaryValue( atPassword, b1 );
3499         Value<byte[]> testB2 = new ServerBinaryValue( atPassword, b2 );
3500         
3501         // test a removal of an non existing attribute
3502         List<EntryAttribute> removed = entry.removeAttributes( atCN );
3503         assertNull( removed );
3504         
3505         // Test a simple removal
3506         entry.add( "cN", atCN, test1 );
3507         assertEquals( 1, entry.size() );
3508         assertNotNull( entry.get( atCN ) );
3509         entry.removeAttributes( "CN" );
3510         assertEquals( 0, entry.size() );
3511         assertNull( entry.get( atCN ) );
3512         
3513         // Test a removal of many elements
3514         entry.put( "CN", test1, test2 );
3515         entry.put( "userPassword", testB1, testB2 );
3516         assertEquals( 2, entry.size() );
3517         assertNotNull( entry.get( atCN ) );
3518         assertNotNull( entry.get( atPassword ) );
3519         
3520         AttributeType OBJECT_CLASS_AT = registries.getAttributeTypeRegistry().lookup( SchemaConstants.OBJECT_CLASS_AT );
3521         
3522         entry.removeAttributes( "cN", "UsErPaSsWoRd" );
3523         assertEquals( 0, entry.size() );
3524         assertNull( entry.get( atCN ) );
3525         assertNull( entry.get( atPassword ) );
3526         assertFalse( entry.contains( OBJECT_CLASS_AT, "top" ) );
3527         
3528         // test the removal of a bad Attribute
3529         entry.put( "CN", test1, test2 );
3530         entry.put( "userPassword", testB1, testB2 );
3531         assertEquals( 2, entry.size() );
3532         assertNotNull( entry.get( atCN ) );
3533         assertNotNull( entry.get( atPassword ) );
3534         
3535         removed = entry.removeAttributes( "badAttribute" );
3536         
3537         assertNull( removed );
3538     }
3539 
3540     
3541     /**
3542      * Test the set(AT...) method
3543      */
3544     @Test public void testSetATElipsis() throws Exception
3545     {
3546         LdapDN dn = new LdapDN( "cn=test" );
3547         ServerEntry entry = new DefaultServerEntry( registries, dn );
3548         
3549         List<EntryAttribute> result = null;
3550         
3551         // First check that this method fails if we pass an empty list of ATs
3552         result = entry.set( (AttributeType)null);
3553         assertNull( result );
3554         
3555         // Now, check what we get when adding one existing AT
3556         result = entry.set( atSN );
3557         
3558         assertNull( result );
3559         EntryAttribute sa = entry.get( "sn" );
3560         assertNotNull( sa );
3561         assertEquals( atSN, ((ServerAttribute)sa).getAttributeType() );
3562         assertEquals( "sn", ((ServerAttribute)sa).getAttributeType().getName() );
3563         
3564         // Add two AT now
3565         AttributeType atGN = registries.getAttributeTypeRegistry().lookup( "givenname" );
3566         AttributeType atStreet = registries.getAttributeTypeRegistry().lookup( "2.5.4.9" );
3567         result = entry.set( atL, atC, atGN, atStreet );
3568         
3569         assertNull( result );
3570 
3571         sa = entry.get( "l" );
3572         assertNotNull( sa );
3573         assertEquals( atL, ((ServerAttribute)sa).getAttributeType() );
3574         assertEquals( "l", ((ServerAttribute)sa).getAttributeType().getName() );
3575 
3576         sa = entry.get( "c" );
3577         assertNotNull( sa );
3578         assertEquals( atC, ((ServerAttribute)sa).getAttributeType() );
3579         assertEquals( "c", ((ServerAttribute)sa).getAttributeType().getName() );
3580 
3581         sa = entry.get( "2.5.4.9" );
3582         assertNotNull( sa );
3583         assertEquals( atStreet, ((ServerAttribute)sa).getAttributeType() );
3584         assertEquals( "street", ((ServerAttribute)sa).getAttributeType().getName() );
3585 
3586         sa = entry.get( "givenName" );
3587         assertNotNull( sa );
3588         assertEquals( atGN, ((ServerAttribute)sa).getAttributeType() );
3589         assertEquals( "givenName", ((ServerAttribute)sa).getAttributeType().getName() );
3590         
3591         // Now try to add existing ATs
3592         // First, set some value to the modified AT
3593         sa = entry.get( "sn" );
3594         sa.add( "test" );
3595         
3596         // Check that the value has been added to the entry
3597         assertEquals( "test", entry.get( "sn" ).get().get() ); 
3598         
3599         // Now add a new SN empty AT : it should replace the existing one.
3600         AttributeType atSNEmpty = registries.getAttributeTypeRegistry().lookup( "sn" );
3601         sa = entry.set( atSNEmpty ).get( 0 );
3602         assertEquals( "test", sa.get().get() ); 
3603         assertNotNull( entry.get(  "sn" ) );
3604         assertNull( entry.get(  "sn" ).get() );
3605         
3606         // Last, not least, put an ObjectClass AT
3607         AttributeType OBJECT_CLASS_AT = registries.getAttributeTypeRegistry().lookup( SchemaConstants.OBJECT_CLASS_AT );
3608         
3609         entry.set( OBJECT_CLASS_AT );
3610         
3611         assertNotNull( entry.get( "objectClass" ) );
3612 
3613         EntryAttribute oc = entry.get( "objectClass" );
3614         
3615         assertEquals( OBJECT_CLASS_AT, ((ServerAttribute)oc).getAttributeType() );
3616         assertNull( oc.get() );
3617     }
3618     
3619     
3620     /**
3621      * Test the set( upId ) method
3622      */
3623     @Test public void testSetUpID() throws Exception
3624     {
3625         LdapDN dn = new LdapDN( "cn=test" );
3626         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
3627         
3628         // First check that this method fails if we pass a null or empty ID
3629         List<EntryAttribute> result = entry.set( (String)null );
3630         assertNull( result );
3631         
3632         result = entry.set( "  " );
3633         assertNull( result );
3634         
3635         // Now check that we can't put invalid IDs
3636         result = entry.set( "ThisIsNotAnAttributeType" );
3637         assertNull( result );
3638         
3639         // Now, check what we get when adding one existing AT
3640         result = entry.set( "sn" );
3641         
3642         assertNull( result );
3643 
3644         EntryAttribute sa = entry.get( "sn" );
3645         assertNotNull( sa );
3646         assertEquals( "sn", sa.getId() );
3647         
3648         // Add different upIds now
3649         AttributeType atGN = registries.getAttributeTypeRegistry().lookup( "givenname" );
3650         AttributeType atStreet = registries.getAttributeTypeRegistry().lookup( "2.5.4.9" );
3651         
3652         entry.set( "L" );
3653         entry.set( "CountryName" );
3654         entry.set( "gn" );
3655         entry.set( "2.5.4.9" );
3656         
3657 
3658         sa = entry.get( "l" );
3659         assertNotNull( sa );
3660         assertEquals( atL, ((ServerAttribute)sa).getAttributeType() );
3661         assertEquals( "l", sa.getId() );
3662         assertEquals( "L", sa.getUpId() );
3663 
3664         sa = entry.get( "c" );
3665         assertNotNull( sa );
3666         assertEquals( atC, ((ServerAttribute)sa).getAttributeType() );
3667         assertEquals( "countryname", sa.getId() );
3668         assertEquals( "CountryName", sa.getUpId() );
3669 
3670         sa = entry.get( "2.5.4.9" );
3671         assertNotNull( sa );
3672         assertEquals( atStreet, ((ServerAttribute)sa).getAttributeType() );
3673         assertEquals( "2.5.4.9", sa.getId() );
3674         assertEquals( "2.5.4.9", sa.getUpId() );
3675 
3676         sa = entry.get( "givenName" );
3677         assertNotNull( sa );
3678         assertEquals( atGN, ((ServerAttribute)sa).getAttributeType() );
3679         assertEquals( "gn", sa.getId() );
3680         assertEquals( "gn", sa.getUpId() );
3681         
3682         // Now try to add existing ATs
3683         // First, set some value to the modified AT
3684         sa = entry.get( "sn" );
3685         sa.add( "test" );
3686         
3687         // Check that the value has been added to the entry
3688         assertEquals( "test", entry.get( "sn" ).get().get() ); 
3689         
3690         // Now add a new SN empty AT : it should replace the existing one.
3691         AttributeType atSNEmpty = registries.getAttributeTypeRegistry().lookup( "sn" );
3692         sa = entry.set( atSNEmpty ).get( 0 );
3693         assertEquals( "test", sa.get().get() ); 
3694         assertNotNull( entry.get(  "sn" ) );
3695         assertNull( entry.get(  "sn" ).get() );
3696     }
3697 
3698     
3699     /**
3700      * Test method for set( AttributeType... )
3701      */
3702     @Test
3703     public void testSetAttributeTypeArray() throws Exception
3704     {
3705         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3706 
3707         entry.add( "ObjectClass", "top", "person" );
3708         entry.add( "cn", "test1", "test2" );
3709         entry.add( "sn", "Test" );
3710         
3711         List<EntryAttribute> removed = entry.set( atOC, atCN, atPwd );
3712         
3713         assertEquals( 4, entry.size() );
3714         assertNotNull( entry.get( "objectclass" ) );
3715         assertNotNull( entry.get( "cn" ) );
3716         assertNotNull( entry.get( "userPassword" ) );
3717         assertNotNull( entry.get( "sn" ) );
3718         
3719         assertNull( entry.get( "objectclass" ).get() );
3720         assertNull( entry.get( "cn" ).get() );
3721         assertNull( entry.get( "userPassword" ).get() );
3722         assertNotNull( entry.get( "sn" ).get() );
3723         
3724         assertNotNull( removed );
3725         assertEquals( 2, removed.size() );
3726     }
3727 
3728 
3729     /**
3730      * Test method for set( String... )
3731      */
3732     @Test
3733     public void testSetStringArray() throws Exception
3734     {
3735         Entry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3736 
3737         entry.add( "ObjectClass", "top", "person" );
3738         entry.add( "cn", "test1", "test2" );
3739         entry.add( "sn", "Test" );
3740         
3741         List<EntryAttribute> removed = entry.set( "objectClass", "CN", "givenName" );
3742         
3743         assertEquals( 4, entry.size() );
3744         assertNotNull( entry.get( "objectclass" ) );
3745         assertNotNull( entry.get( "cn" ) );
3746         assertNotNull( entry.get( "givenname" ) );
3747         assertNotNull( entry.get( "sn" ) );
3748         
3749         assertNull( entry.get( "objectclass" ).get() );
3750         assertNull( entry.get( "cn" ).get() );
3751         assertNull( entry.get( "givenname" ).get() );
3752         assertNotNull( entry.get( "sn" ).get() );
3753         
3754         assertNotNull( removed );
3755         assertEquals( 2, removed.size() );
3756     }
3757 
3758 
3759     /**
3760      * Test method for setDN( LdapDN )
3761      */
3762     @Test
3763     public void testSetDn()
3764     {
3765         Entry entry = new DefaultServerEntry( registries );
3766          
3767         assertEquals( LdapDN.EMPTY_LDAPDN, entry.getDn() );
3768          
3769         entry.setDn( EXAMPLE_DN );
3770         assertEquals( EXAMPLE_DN, entry.getDn() );
3771     }
3772      
3773      
3774     /**
3775      * Test for method size()
3776      */
3777      @Test
3778      public void testSize() throws Exception
3779      {
3780          ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3781           
3782          assertEquals( 0, entry.size() );
3783          entry.add( "ObjectClass", atr.lookup( "ObjectClass" ), "top", "person" );
3784          entry.add( "CN", atr.lookup( "Cn" ), "test" );
3785          entry.add( "SN", atr.lookup( "Sn" ), "Test" );
3786           
3787          assertEquals( 3, entry.size() );
3788          
3789          entry.clear();
3790          assertEquals( 0, entry.size() );
3791      }
3792       
3793     
3794     /**
3795      * Test a conversion from a ServerEntry to an BasicAttributes
3796      */
3797     @Test public void testToBasicAttributes() throws InvalidNameException, Exception
3798     {
3799         LdapDN dn = new LdapDN( "cn=test" );
3800         DefaultServerEntry entry = new DefaultServerEntry( registries, dn );
3801         
3802         AttributeType OBJECT_CLASS_AT = registries.getAttributeTypeRegistry().lookup( SchemaConstants.OBJECT_CLASS_AT );
3803         
3804         entry.put( "objectClass", OBJECT_CLASS_AT, "top", "person", "inetOrgPerson", "organizationalPerson" );
3805         entry.put( "cn", registries.getAttributeTypeRegistry().lookup( "cn" ), "test" );
3806         
3807         Attributes attributes = ServerEntryUtils.toBasicAttributes( entry );
3808         
3809         assertNotNull( attributes );
3810         assertTrue( attributes instanceof BasicAttributes );
3811         
3812         Set<String> expected = new HashSet<String>();
3813         expected.add( "objectClass" );
3814         expected.add( "cn" );
3815      
3816         for ( NamingEnumeration<String> ids = attributes.getIDs(); ids.hasMoreElements();)
3817         {
3818             String id = ids.nextElement();
3819             
3820             assertTrue( expected.contains( id ) );
3821             expected.remove( id );
3822             
3823         }
3824 
3825         // It should be empty
3826         assertEquals( 0, expected.size() );
3827     }
3828 
3829 
3830     /**
3831      * Test method for toString().
3832      */
3833     @Test
3834     public void testToString() throws Exception
3835     {
3836         ServerEntry entry = new DefaultServerEntry( registries, EXAMPLE_DN );
3837         
3838         assertEquals( "ServerEntry\n    dn[]: dc=example,dc=com\n", entry.toString() );
3839         
3840         Value<String> strValueTop = new ClientStringValue( "top" );
3841         Value<String> strValuePerson = new ClientStringValue( "person" );
3842         Value<String> strNullValue = new ClientStringValue( null);
3843 
3844         Value<byte[]> binValue1 = new ClientBinaryValue( BYTES1 );
3845         Value<byte[]> binValue2 = new ClientBinaryValue( BYTES2 );
3846         Value<byte[]> binNullValue = new ClientBinaryValue( null );
3847         
3848         entry.put( "ObjectClass", atOC, strValueTop, strValuePerson, strNullValue );
3849         entry.put( "UserPassword", atPwd, binValue1, binValue2, binNullValue );
3850 
3851         String expected = 
3852             "ServerEntry\n" +
3853             "    dn[]: dc=example,dc=com\n" +
3854             "    ObjectClass: top\n" +
3855             "    ObjectClass: person\n" +
3856             "    ObjectClass: ''\n" +
3857             "    UserPassword: '0x61 0x62 '\n" +
3858             "    UserPassword: '0x62 '\n" +
3859             "    UserPassword: ''\n";
3860 
3861         assertEquals( expected, entry.toString() );
3862     }
3863 
3864     
3865     
3866     /**
3867      * Test the copy constructor of a ServerEntry
3868      */
3869     @Test 
3870     public void testCopyConstructorServerEntry() throws NamingException
3871     {
3872         Entry serverEntry = new DefaultServerEntry( registries );
3873         serverEntry.add( "cn", "test1", "test2" );
3874         serverEntry.add( "objectClass", "top", "person" );
3875         
3876         Entry copyEntry = new DefaultServerEntry( registries, serverEntry );
3877         
3878         assertEquals( copyEntry, serverEntry );
3879         assertTrue( copyEntry.contains( "objectClass", "top", "person" ) );
3880         assertTrue( copyEntry.contains( "cn", "test1", "test2" ) );
3881         
3882         serverEntry.removeAttributes( "cn" );
3883 
3884         assertNotSame( copyEntry, serverEntry );
3885         assertTrue( copyEntry.contains( "objectClass", "top", "person" ) );
3886         assertTrue( copyEntry.contains( "cn", "test1", "test2" ) );
3887     }
3888     
3889     
3890     /**
3891      * Test the copy constructor of a ClientEntry
3892      */
3893     @Test 
3894     public void testCopyConstructorClientEntry() throws NamingException
3895     {
3896         Entry clientEntry = new DefaultClientEntry();
3897         clientEntry.setDn( new LdapDN( "ou=system" ) );
3898         clientEntry.add( "cn", "test1", "test2" );
3899         clientEntry.add( "objectClass", "top", "person" );
3900         
3901         Entry copyEntry = new DefaultServerEntry( registries, clientEntry );
3902         
3903         assertTrue( copyEntry instanceof ServerEntry );
3904         assertTrue( copyEntry.contains( "objectClass", "top", "person" ) );
3905         assertTrue( copyEntry.contains( "cn", "test1", "test2" ) );
3906         
3907         clientEntry.removeAttributes( "cn" );
3908 
3909         assertTrue( copyEntry.contains( "objectClass", "top", "person" ) );
3910         assertTrue( copyEntry.contains( "cn", "test1", "test2" ) );
3911     }
3912     
3913     
3914     /**
3915      * Test the conversion method 
3916      */
3917     @Test 
3918     public void testToClientEntry() throws NamingException
3919     {
3920         LdapDN dn = new LdapDN( "ou=system" );
3921         ServerEntry serverEntry = new DefaultServerEntry( registries );
3922         serverEntry.setDn( dn );
3923         serverEntry.add( "cn", "test1", "test2" );
3924         serverEntry.add( "objectClass", "top", "person" );
3925         
3926         Entry clientEntry = serverEntry.toClientEntry();
3927         
3928         assertTrue( clientEntry instanceof ClientEntry );
3929         assertFalse( clientEntry instanceof ServerEntry );
3930         
3931         assertTrue( clientEntry.containsAttribute( "cn", "objectClass" ) );
3932         assertEquals( dn, clientEntry.getDn() );
3933         
3934         serverEntry.removeAttributes( "cn" );
3935         assertTrue( clientEntry
3936             .contains( "cn", "test1", "test2" ) );
3937         
3938         serverEntry.remove(  "objectClass", "person" );
3939         assertTrue( clientEntry
3940             .contains( "objectClass", "top", "person" ) );
3941     }
3942 }
3943