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  package org.apache.directory.server.core.entry;
20  
21  
22  import static org.junit.Assert.assertNotSame;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNull;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.fail;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.ObjectInputStream;
34  import java.io.ObjectOutputStream;
35  import java.util.ArrayList;
36  import java.util.Arrays;
37  import java.util.HashSet;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Set;
41  
42  import javax.naming.NamingException;
43  import javax.naming.directory.InvalidAttributeValueException;
44  
45  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
46  import org.apache.directory.server.schema.bootstrap.ApachemetaSchema;
47  import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
48  import org.apache.directory.server.schema.bootstrap.CoreSchema;
49  import org.apache.directory.server.schema.bootstrap.CosineSchema;
50  import org.apache.directory.server.schema.bootstrap.InetorgpersonSchema;
51  import org.apache.directory.server.schema.bootstrap.Schema;
52  import org.apache.directory.server.schema.bootstrap.SystemSchema;
53  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
54  import org.apache.directory.server.schema.registries.DefaultRegistries;
55  import org.apache.directory.server.schema.registries.OidRegistry;
56  import org.apache.directory.server.schema.registries.Registries;
57  import org.apache.directory.shared.ldap.entry.EntryAttribute;
58  import org.apache.directory.shared.ldap.entry.Value;
59  import org.apache.directory.shared.ldap.entry.client.ClientAttribute;
60  import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
61  import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
62  import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
63  import org.apache.directory.shared.ldap.schema.AttributeType;
64  import org.apache.directory.shared.ldap.util.StringTools;
65  
66  import org.junit.BeforeClass;
67  import org.junit.Test;
68  
69  
70  /**
71   * Tests for the DefaultServerAttribute class
72   * 
73   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
74   * @version $Rev$, $Date$
75   */
76  public class DefaultServerAttributeTest
77  {
78      private static BootstrapSchemaLoader loader;
79      private static Registries registries;
80      private static OidRegistry oidRegistry;
81      
82      private static AttributeType atCN;
83      private static AttributeType atSN;
84      
85      // A SINGLE-VALUE attribute
86      private static AttributeType atC;   
87      
88      // A Binary attribute
89      private static AttributeType atPwd;
90  
91      private static final Value<String> NULL_STRING_VALUE = new ClientStringValue( null );
92      private static final Value<byte[]> NULL_BINARY_VALUE = new ClientBinaryValue( null );
93      private static final byte[] BYTES1 = new byte[]{ 'a', 'b' };
94      private static final byte[] BYTES2 = new byte[]{ 'b' };
95      private static final byte[] BYTES3 = new byte[]{ 'c' };
96      private static final byte[] BYTES4 = new byte[]{ 'd' };
97      
98      private static final ClientStringValue STR_VALUE1 = new ClientStringValue( "a" );
99      private static final ClientStringValue STR_VALUE2 = new ClientStringValue( "b" );
100     private static final ClientStringValue STR_VALUE3 = new ClientStringValue( "c" );
101     private static final ClientStringValue STR_VALUE4 = new ClientStringValue( "d" );
102 
103     private static final ClientBinaryValue BIN_VALUE1 = new ClientBinaryValue( BYTES1 );
104     private static final ClientBinaryValue BIN_VALUE2 = new ClientBinaryValue( BYTES2 );
105     private static final ClientBinaryValue BIN_VALUE3 = new ClientBinaryValue( BYTES3 );
106     private static final ClientBinaryValue BIN_VALUE4 = new ClientBinaryValue( BYTES4 );
107 
108     /**
109      * Initialize the registries once for the whole test suite
110      */
111     @BeforeClass
112     public static void setup() throws Exception
113     {
114         loader = new BootstrapSchemaLoader();
115         oidRegistry = new DefaultOidRegistry();
116         registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
117         
118         // load essential bootstrap schemas 
119         Set<Schema> bootstrapSchemas = new HashSet<Schema>();
120         bootstrapSchemas.add( new ApachemetaSchema() );
121         bootstrapSchemas.add( new ApacheSchema() );
122         bootstrapSchemas.add( new CoreSchema() );
123         bootstrapSchemas.add( new SystemSchema() );
124         bootstrapSchemas.add( new InetorgpersonSchema() );
125         bootstrapSchemas.add( new CosineSchema() );
126         loader.loadWithDependencies( bootstrapSchemas, registries );
127         
128         atCN = registries.getAttributeTypeRegistry().lookup( "cn" );
129         atC = registries.getAttributeTypeRegistry().lookup( "c" );
130         atSN = registries.getAttributeTypeRegistry().lookup( "sn" );
131         atPwd = registries.getAttributeTypeRegistry().lookup( "userpassword" );
132     }
133 
134     /**
135      * Serialize a DefaultServerAttribute
136      */
137     private ByteArrayOutputStream serializeValue( DefaultServerAttribute value ) throws IOException
138     {
139         ObjectOutputStream oOut = null;
140         ByteArrayOutputStream out = new ByteArrayOutputStream();
141 
142         try
143         {
144             oOut = new ObjectOutputStream( out );
145             value.serialize( oOut );
146         }
147         catch ( IOException ioe )
148         {
149             throw ioe;
150         }
151         finally
152         {
153             try
154             {
155                 if ( oOut != null )
156                 {
157                     oOut.flush();
158                     oOut.close();
159                 }
160             }
161             catch ( IOException ioe )
162             {
163                 throw ioe;
164             }
165         }
166         
167         return out;
168     }
169     
170     
171     /**
172      * Deserialize a DefaultServerAttribute
173      */
174     private DefaultServerAttribute deserializeValue( ByteArrayOutputStream out, AttributeType at ) throws IOException, ClassNotFoundException
175     {
176         ObjectInputStream oIn = null;
177         ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
178 
179         try
180         {
181             oIn = new ObjectInputStream( in );
182 
183             DefaultServerAttribute value = new DefaultServerAttribute( at );
184             value.deserialize( oIn );
185             
186             return value;
187         }
188         catch ( IOException ioe )
189         {
190             throw ioe;
191         }
192         finally
193         {
194             try
195             {
196                 if ( oIn != null )
197                 {
198                     oIn.close();
199                 }
200             }
201             catch ( IOException ioe )
202             {
203                 throw ioe;
204             }
205         }
206     }
207 
208     
209     @Test public void testAddOneValue() throws Exception
210     {
211         AttributeType at = TestServerEntryUtils.getIA5StringAttributeType();
212         
213         DefaultServerAttribute attr = new DefaultServerAttribute( at );
214         
215         // Add a String value
216         attr.add( "test" );
217         
218         assertEquals( 1, attr.size() );
219         
220         assertTrue( attr.getAttributeType().getSyntax().isHumanReadable() );
221         
222         Value<?> value = attr.get();
223         
224         assertTrue( value instanceof ServerStringValue );
225         assertEquals( "test", ((ServerStringValue)value).get() );
226         
227         // Add a binary value
228         assertEquals( 0, attr.add( new byte[]{0x01} ) );
229         
230         // Add a Value
231         Value<?> ssv = new ServerStringValue( at, "test2" );
232         
233         attr.add( ssv );
234         
235         assertEquals( 2, attr.size() );
236         
237         Set<String> expected = new HashSet<String>();
238         expected.add( "test" );
239         expected.add( "test2" );
240         
241         for ( Value<?> val:attr )
242         {
243             if ( expected.contains( val.get() ) )
244             {
245                 expected.remove( val.get() );
246             }
247             else
248             {
249                 fail();
250             }
251         }
252         
253         assertEquals( 0, expected.size() );
254     }
255 
256 
257     @Test public void testAddTwoValue() throws Exception
258     {
259         AttributeType at = TestServerEntryUtils.getIA5StringAttributeType();
260         
261         DefaultServerAttribute attr = new DefaultServerAttribute( at );
262         
263         // Add String values
264         attr.add( "test" );
265         attr.add( "test2" );
266         
267         assertEquals( 2, attr.size() );
268         
269         assertTrue( attr.getAttributeType().getSyntax().isHumanReadable() );
270         
271         Set<String> expected = new HashSet<String>();
272         expected.add( "test" );
273         expected.add( "test2" );
274         
275         for ( Value<?> val:attr )
276         {
277             if ( expected.contains( val.get() ) )
278             {
279                 expected.remove( val.get() );
280             }
281             else
282             {
283                 fail();
284             }
285         }
286         
287         assertEquals( 0, expected.size() );
288     }
289 
290 
291     @Test public void testAddNullValue() throws Exception
292     {
293         AttributeType at = TestServerEntryUtils.getIA5StringAttributeType();
294         
295         DefaultServerAttribute attr = new DefaultServerAttribute( at );
296         
297         // Add a null value
298         attr.add( new ServerStringValue( at, null ) );
299         
300         assertEquals( 1, attr.size() );
301         
302         assertTrue( attr.getAttributeType().getSyntax().isHumanReadable() );
303         
304         Value<?> value = attr.get();
305         
306         assertTrue( value instanceof ServerStringValue );
307         assertNull( ((ServerStringValue)value).get() );
308     }
309     
310 
311     @Test public void testGetAttribute() throws Exception
312     {
313         AttributeType at = TestServerEntryUtils.getIA5StringAttributeType();
314         
315         DefaultServerAttribute attr = new DefaultServerAttribute( at );
316         
317         attr.add( "Test1" );
318         attr.add( "Test2" );
319         attr.add( "Test3" );
320         
321         assertEquals( "1.1",attr.getId() );
322         assertEquals( 3, attr.size() );
323         assertTrue( attr.contains( "Test1" ) );
324         assertTrue( attr.contains( "Test2" ) );
325         assertTrue( attr.contains( "Test3" ) );
326     }
327 
328 
329     /**
330      * Test the contains() method
331      */
332     @Test public void testContains() throws Exception
333     {
334         AttributeType at = TestServerEntryUtils.getIA5StringAttributeType();
335         
336         DefaultServerAttribute attr = new DefaultServerAttribute( at );
337         
338         attr.add( "Test  1" );
339         attr.add( "Test  2" );
340         attr.add( "Test  3" );
341         
342         assertTrue( attr.contains( "test 1" ) );
343         assertTrue( attr.contains( "Test 2" ) );
344         assertTrue( attr.contains( "TEST     3" ) );
345     }
346 
347 
348     /**
349      * Test method getBytes()
350      */
351     @Test
352     public void testGetBytes() throws InvalidAttributeValueException
353     {
354         ServerAttribute attr1 = new DefaultServerAttribute( atPwd );
355         
356         attr1.add( (byte[])null );
357         assertNull( attr1.getBytes() );
358 
359         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
360         
361         attr2.add( BYTES1, BYTES2 );
362         assertTrue( Arrays.equals( BYTES1, attr2.getBytes() ) );
363         
364         ServerAttribute attr3 = new DefaultServerAttribute( atCN );
365         
366         attr3.add( "a", "b" );
367         
368         try
369         {
370             attr3.getBytes();
371             fail();
372         }
373         catch ( InvalidAttributeValueException ivae )
374         {
375             assertTrue( true );
376         }
377     }
378 
379 
380     /**
381      * Test method getId()
382      */
383     @Test
384     public void testGetId()
385     {
386         ServerAttribute attr = new DefaultServerAttribute( atCN );
387 
388         assertEquals( "cn", attr.getId() );
389         
390         attr.setId(  "  CN  " );
391         assertEquals( "cn", attr.getId() );
392 
393         attr.setId(  "  CommonName  " );
394         assertEquals( "commonname", attr.getId() );
395 
396         attr.setId(  "  2.5.4.3  " );
397         assertEquals( "2.5.4.3", attr.getId() );
398     }
399 
400 
401     /**
402      * Test method getString()
403      */
404     @Test
405     public void testGetString() throws InvalidAttributeValueException
406     {
407         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
408         
409         attr1.add( (String)null );
410         assertNull( attr1.getString() );
411 
412         ServerAttribute attr2 = new DefaultServerAttribute( atCN );
413         
414         attr2.add( "a", "b" );
415         assertEquals( "a", attr2.getString() );
416         
417         ServerAttribute attr3 = new DefaultServerAttribute( atPwd );
418         
419         attr3.add( BYTES1, BYTES2 );
420         
421         try
422         {
423             attr3.getString();
424             fail();
425         }
426         catch ( InvalidAttributeValueException ivae )
427         {
428             assertTrue( true );
429         }
430     }
431 
432 
433     /**
434      * Test method getUpId
435      */
436     @Test
437     public void testGetUpId()
438     {
439         ServerAttribute attr = new DefaultServerAttribute( atCN );
440 
441         assertNotNull( attr.getUpId() );
442         assertEquals( "cn", attr.getUpId() );
443         
444         attr.setUpId( "CN" );
445         assertEquals( "CN", attr.getUpId() );
446         
447         attr.setUpId(  "  Cn  " );
448         assertEquals( "Cn", attr.getUpId() );
449 
450         attr.setUpId(  "  2.5.4.3  " );
451         assertEquals( "2.5.4.3", attr.getUpId() );
452     }
453 
454 
455     /**
456      * Test method hashCode()
457      */
458     @Test
459     public void testHashCode()
460     {
461         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
462         ServerAttribute attr2 = new DefaultServerAttribute( atSN );
463         assertNotSame( attr1.hashCode(), attr2.hashCode() );
464         
465         attr2.setAttributeType( atCN );
466         assertEquals( attr1.hashCode(), attr2.hashCode() );
467         
468         attr1.put( (String)null );
469         assertNotSame( attr1.hashCode(), attr2.hashCode() );
470 
471         attr1.clear();
472         assertEquals( attr1.hashCode(), attr2.hashCode() );
473         
474         attr1.put( "a", "b" );
475         assertNotSame( attr1.hashCode(), attr2.hashCode() );
476 
477         attr2.put( "a", "b" );
478         assertEquals( attr1.hashCode(), attr2.hashCode() );
479         
480         // Order matters
481         attr2.put( "b", "a" );
482         assertNotSame( attr1.hashCode(), attr2.hashCode() );
483         
484         ServerAttribute attr3 = new DefaultServerAttribute( atPwd );
485         ServerAttribute attr4 = new DefaultServerAttribute( atPwd );
486         assertNotSame( attr3.hashCode(), attr4.hashCode() );
487         
488         attr3.put( (byte[])null );
489         assertNotSame( attr3.hashCode(), attr4.hashCode() );
490 
491         attr3.clear();
492         assertEquals( attr3.hashCode(), attr4.hashCode() );
493         
494         attr3.put( new byte[]{0x01, 0x02}, new byte[]{0x03, 0x04} );
495         assertNotSame( attr1.hashCode(), attr2.hashCode() );
496 
497         attr4.put( new byte[]{0x01, 0x02}, new byte[]{0x03, 0x04} );
498         assertNotSame( attr1.hashCode(), attr2.hashCode() );
499 
500         // Order matters
501         attr4.put( new byte[]{0x03, 0x04}, new byte[]{0x01, 0x02} );
502         assertNotSame( attr1.hashCode(), attr2.hashCode() );
503     }
504     
505     
506     /**
507      * Test method SetId(String)
508      */
509     @Test
510     public void testSetId()
511     {
512         ServerAttribute attr = new DefaultServerAttribute( atCN );
513 
514         attr.setId( "Cn" );
515         assertEquals( "cn", attr.getId() );
516         
517         attr.setId( " CN " );
518         assertEquals( "cn", attr.getId() );
519         
520         attr.setId( " 2.5.4.3 " );
521         assertEquals( "2.5.4.3", attr.getId() );
522         
523         attr.setId( " commonName " );
524         assertEquals( "commonname", attr.getId() );
525 
526         try
527         {
528             attr.setId( null );
529             fail();
530         }
531         catch ( IllegalArgumentException iae )
532         {
533             assertTrue( true );
534         }
535         
536         try
537         {
538             attr.setId( "" );
539             fail();
540         }
541         catch ( IllegalArgumentException iae )
542         {
543             assertTrue( true );
544         }
545         
546         try
547         {
548             attr.setId( "  " );
549             fail();
550         }
551         catch ( IllegalArgumentException iae )
552         {
553             assertTrue( true );
554         }
555         
556         try
557         {
558             attr.setId( " SN " );
559             fail();
560         }
561         catch ( IllegalArgumentException iae )
562         {
563             assertTrue( true );
564         }
565     }
566 
567 
568     /**
569      * Test method isValid()
570      */
571     @Test
572     public void testIsValid() throws Exception
573     {
574         ServerAttribute attr = new DefaultServerAttribute( atCN );
575         
576         // No value, this should be valid
577         assertTrue( attr.isValid() );
578         
579         attr.add( "test", "test2", "A123\\;" );
580         assertTrue( attr.isValid() );
581 
582         // If we try to add a wrong value, it will not be added. The
583         // attribute remains valid.
584         assertEquals(0, attr.add( new byte[]{0x01} ) );
585         assertTrue( attr.isValid() );
586 
587         // test a SINGLE-VALUE attribute. CountryName is SINGLE-VALUE
588         attr.setAttributeType( atC );
589         attr.put( "FR" );
590         assertTrue( attr.isValid() );
591         attr.add( "US" );
592         assertFalse( attr.isValid() );
593     }
594 
595 
596     /**
597      * Test method add( Value... )
598      */
599     @Test
600     public void testAddValueArray()
601     {
602         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
603         
604         int nbAdded = attr1.add( new ServerStringValue( atCN, null ) );
605         assertEquals( 1, nbAdded );
606         assertTrue( attr1.isHR() );
607         assertEquals( NULL_STRING_VALUE, attr1.get() );
608         
609         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
610         
611         nbAdded = attr2.add( new ServerBinaryValue( atPwd, null ) );
612         assertEquals( 1, nbAdded );
613         assertFalse( attr2.isHR() );
614         assertEquals( NULL_BINARY_VALUE, attr2.get() );
615         
616         ServerAttribute attr3 = new DefaultServerAttribute( atCN );
617         
618         nbAdded = attr3.add( new ServerStringValue( atCN, "a" ), new ServerStringValue( atCN, "b" ) );
619         assertEquals( 2, nbAdded );
620         assertTrue( attr3.isHR() );
621         assertTrue( attr3.contains( "a" ) );
622         assertTrue( attr3.contains( "b" ) );
623         
624         ServerAttribute attr4 = new DefaultServerAttribute( atCN );
625         
626         nbAdded = attr4.add( new ServerBinaryValue( atPwd, BYTES1 ), new ServerBinaryValue( atPwd, BYTES2 ) );
627         assertEquals( 0, nbAdded );
628         assertTrue( attr4.isHR() );
629         assertFalse( attr4.contains( BYTES1 ) );
630         assertFalse( attr4.contains( BYTES2 ) );
631         
632         ServerAttribute attr5 = new DefaultServerAttribute( atCN );
633         
634         nbAdded = attr5.add( new ServerStringValue( atCN, "c" ), new ServerBinaryValue( atPwd, BYTES1 ) );
635         assertEquals( 1, nbAdded );
636         assertTrue( attr5.isHR() );
637         assertFalse( attr5.contains( "ab" ) );
638         assertTrue( attr5.contains( "c" ) );
639 
640         ServerAttribute attr6 = new DefaultServerAttribute( atPwd );
641         
642         nbAdded = attr6.add( new ServerBinaryValue( atPwd, BYTES1 ), new ServerStringValue( atCN, "c" ) );
643         assertEquals( 1, nbAdded );
644         assertFalse( attr6.isHR() );
645         assertTrue( attr6.contains( BYTES1 ) );
646         assertFalse( attr6.contains( BYTES3 ) );
647 
648         ServerAttribute attr7 = new DefaultServerAttribute( atPwd );
649         
650         nbAdded = attr7.add( new ServerBinaryValue( atPwd, null ), new ServerStringValue( atCN, "c" ) );
651         assertEquals( 1, nbAdded );
652         assertFalse( attr7.isHR() );
653         assertTrue( attr7.contains( NULL_BINARY_VALUE ) );
654         assertFalse( attr7.contains( BYTES3 ) );
655 
656         ServerAttribute attr8 = new DefaultServerAttribute( atCN );
657         
658         nbAdded = attr8.add( new ServerStringValue( atCN, null ), new ServerBinaryValue( atPwd, BYTES1 ) );
659         assertEquals( 1, nbAdded );
660         assertTrue( attr8.isHR() );
661         assertTrue( attr8.contains( NULL_STRING_VALUE ) );
662         assertFalse( attr8.contains( "ab" ) );
663 
664     
665         ServerAttribute attr9 = new DefaultServerAttribute( atCN );
666         
667         nbAdded = attr9.add( new ClientStringValue( null ), new ClientStringValue( "ab" ) );
668         assertEquals( 2, nbAdded );
669         assertTrue( attr9.isHR() );
670         assertTrue( attr9.contains( NULL_STRING_VALUE ) );
671         assertTrue( attr9.contains( "ab" ) );
672 
673         ServerAttribute attr10 = new DefaultServerAttribute( atPwd );
674         
675         nbAdded = attr10.add( new ClientBinaryValue( null ), new ClientBinaryValue( BYTES1 ) );
676         assertEquals( 2, nbAdded );
677         assertFalse( attr10.isHR() );
678         assertTrue( attr10.contains( NULL_BINARY_VALUE ) );
679         assertTrue( attr10.contains( BYTES1 ) );
680     }
681 
682 
683     /**
684      * Test method add( String... )
685      */
686     @Test
687     public void testAddStringArray() throws InvalidAttributeValueException
688     {
689         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
690         
691         int nbAdded = attr1.add( (String)null );
692         assertEquals( 1, nbAdded );
693         assertTrue( attr1.isHR() );
694         assertEquals( NULL_STRING_VALUE, attr1.get() );
695         
696         ServerAttribute attr2 = new DefaultServerAttribute( atCN );
697         
698         nbAdded = attr2.add( "" );
699         assertEquals( 1, nbAdded );
700         assertTrue( attr2.isHR() );
701         assertEquals( "", attr2.getString() );
702         
703         ServerAttribute attr3 = new DefaultServerAttribute( atCN );
704         
705         nbAdded = attr3.add( "t" );
706         assertEquals( 1, nbAdded );
707         assertTrue( attr3.isHR() );
708         assertEquals( "t", attr3.getString() );
709         
710         ServerAttribute attr4 = new DefaultServerAttribute( atCN );
711         
712         nbAdded = attr4.add( "a", "b", "c", "d" );
713         assertEquals( 4, nbAdded );
714         assertTrue( attr4.isHR() );
715         assertEquals( "a", attr4.getString() );
716         assertTrue( attr4.contains( "a" ) );
717         assertTrue( attr4.contains( "b" ) );
718         assertTrue( attr4.contains( "c" ) );
719         assertTrue( attr4.contains( "d" ) );
720         
721         nbAdded = attr4.add( "e" );
722         assertEquals( 1, nbAdded );
723         assertTrue( attr4.isHR() );
724         assertEquals( "a", attr4.getString() );
725         assertTrue( attr4.contains( "a" ) );
726         assertTrue( attr4.contains( "b" ) );
727         assertTrue( attr4.contains( "c" ) );
728         assertTrue( attr4.contains( "d" ) );
729         assertTrue( attr4.contains( "e" ) );
730         
731         nbAdded = attr4.add( BYTES1 );
732         assertEquals( 0, nbAdded );
733         assertTrue( attr4.isHR() );
734         assertEquals( "a", attr4.getString() );
735         assertTrue( attr4.contains( "a" ) );
736         assertTrue( attr4.contains( "b" ) );
737         assertTrue( attr4.contains( "c" ) );
738         assertTrue( attr4.contains( "d" ) );
739         assertTrue( attr4.contains( "e" ) );
740         assertFalse( attr4.contains( "ab" ) );
741         
742         ServerAttribute attr5 = new DefaultServerAttribute( atCN );
743         
744         nbAdded = attr5.add( "a", "b", (String)null, "d" );
745         assertEquals( 4, nbAdded );
746         assertTrue( attr5.isHR() );
747         assertTrue( attr5.contains( "a" ) );
748         assertTrue( attr5.contains( "b" ) );
749         assertTrue( attr5.contains( NULL_STRING_VALUE ) );
750         assertTrue( attr5.contains( "d" ) );
751 
752         ServerAttribute attr6 = new DefaultServerAttribute( atPwd );
753         
754         nbAdded = attr6.add( "a", (String)null );
755         assertEquals( 0, nbAdded );
756         assertFalse( attr6.isHR() );
757     }
758 
759 
760     /**
761      * Test method add( byte[]... )
762      */
763     @Test
764     public void testAddByteArray() throws InvalidAttributeValueException
765     {
766         ServerAttribute attr1 = new DefaultServerAttribute( atPwd );
767         
768         int nbAdded = attr1.add( (byte[])null );
769         assertEquals( 1, nbAdded );
770         assertFalse( attr1.isHR() );
771         assertTrue( Arrays.equals( NULL_BINARY_VALUE.get(), attr1.getBytes() ) );
772         
773         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
774         
775         nbAdded = attr2.add( StringTools.EMPTY_BYTES );
776         assertEquals( 1, nbAdded );
777         assertFalse( attr2.isHR() );
778         assertTrue( Arrays.equals( StringTools.EMPTY_BYTES, attr2.getBytes() ) );
779         
780         ServerAttribute attr3 = new DefaultServerAttribute( atPwd );
781         
782         nbAdded = attr3.add( BYTES1 );
783         assertEquals( 1, nbAdded );
784         assertFalse( attr3.isHR() );
785         assertTrue( Arrays.equals( BYTES1, attr3.getBytes() ) );
786         
787         ServerAttribute attr4 = new DefaultServerAttribute( atPwd );
788         
789         nbAdded = attr4.add( BYTES1, BYTES2, BYTES3, BYTES4 );
790         assertEquals( 4, nbAdded );
791         assertFalse( attr4.isHR() );
792         assertTrue( attr4.contains( BYTES1 ) );
793         assertTrue( attr4.contains( BYTES2 ) );
794         assertTrue( attr4.contains( BYTES3 ) );
795         assertTrue( attr4.contains( BYTES4 ) );
796         
797         ServerAttribute attr5 = new DefaultServerAttribute( atPwd );
798         
799         nbAdded = attr5.add( BYTES1, BYTES2, (byte[])null, BYTES3 );
800         assertEquals( 4, nbAdded );
801         assertFalse( attr5.isHR() );
802         assertTrue( attr5.contains( BYTES1 ) );
803         assertTrue( attr5.contains( BYTES2 ) );
804         assertTrue( attr5.contains( (byte[])null ) );
805         assertTrue( attr5.contains( BYTES3 ) );
806 
807         ServerAttribute attr6 = new DefaultServerAttribute( atPwd );
808         
809         nbAdded = attr6.add( "ab", (String)null );
810         assertEquals( 0, nbAdded );
811         assertFalse( attr6.isHR() );
812     }
813 
814 
815     /**
816      * Test method clear()
817      */
818     @Test
819     public void testClear()
820     {
821         ServerAttribute attr = new DefaultServerAttribute( "cn", atCN );
822         
823         assertEquals( 0, attr.size() );
824         
825         attr.add( (String)null, "a", "b" );
826         assertEquals( 3, attr.size() );
827         
828         attr.clear();
829         assertTrue( attr.isHR() );
830         assertEquals( 0, attr.size() );
831         assertEquals( atCN, attr.getAttributeType() );
832     }
833 
834 
835     /**
836      * Test method contains( Value... )
837      */
838     @Test
839     public void testContainsValueArray()
840     {
841         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
842         
843         assertEquals( 0, attr1.size() );
844         assertFalse( attr1.contains( STR_VALUE1 ) );
845         assertFalse( attr1.contains( NULL_STRING_VALUE ) );
846         
847         attr1.add( (String)null );
848         assertEquals( 1, attr1.size() );
849         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
850         
851         attr1.remove( (String)null );
852         assertFalse( attr1.contains( NULL_STRING_VALUE ) );
853         assertEquals( 0, attr1.size() );
854         
855         attr1.add(  "a", "b", "c" );
856         assertEquals( 3, attr1.size() );
857         assertTrue( attr1.contains( STR_VALUE1 ) );
858         assertTrue( attr1.contains( STR_VALUE2 ) );
859         assertTrue( attr1.contains( STR_VALUE3 ) );
860         assertTrue( attr1.contains( STR_VALUE1, STR_VALUE3 ) );
861         assertFalse( attr1.contains( STR_VALUE4 ) );
862         assertFalse( attr1.contains( NULL_STRING_VALUE ) );
863 
864         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
865         assertEquals( 0, attr2.size() );
866         assertFalse( attr2.contains( BYTES1 ) );
867         assertFalse( attr2.contains( NULL_BINARY_VALUE ) );
868         
869         attr2.add( (byte[])null );
870         assertEquals( 1, attr2.size() );
871         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
872         
873         attr2.remove( (byte[])null );
874         assertFalse( attr2.contains( NULL_BINARY_VALUE ) );
875         assertEquals( 0, attr2.size() );
876         
877         attr2.add( BYTES1, BYTES2, BYTES3 );
878         assertEquals( 3, attr2.size() );
879         assertTrue( attr2.contains( BIN_VALUE1 ) );
880         assertTrue( attr2.contains( BIN_VALUE2 ) );
881         assertTrue( attr2.contains( BIN_VALUE3 ) );
882         assertFalse( attr2.contains( NULL_BINARY_VALUE ) );
883     }
884 
885 
886     /**
887      * Test method contains( String... )
888      */
889     @Test
890     public void testContainsStringArray()
891     {
892         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
893         
894         assertEquals( 0, attr1.size() );
895         assertFalse( attr1.contains( "a" ) );
896         assertFalse( attr1.contains( (String)null ) );
897         
898         attr1.add( (String)null );
899         assertEquals( 1, attr1.size() );
900         assertTrue( attr1.contains( (String)null ) );
901         
902         attr1.remove( (String)null );
903         assertFalse( attr1.contains( (String)null ) );
904         assertEquals( 0, attr1.size() );
905         
906         attr1.add(  "a", "b", "c" );
907         assertEquals( 3, attr1.size() );
908         assertTrue( attr1.contains( "a" ) );
909         assertTrue( attr1.contains( "b" ) );
910         assertTrue( attr1.contains( "c" ) );
911         assertFalse( attr1.contains( "e" ) );
912         assertFalse( attr1.contains( (String)null ) );
913     }
914 
915 
916     /**
917      * Test method contains( byte[]... )
918      */
919     @Test
920     public void testContainsByteArray()
921     {
922         ServerAttribute attr1 = new DefaultServerAttribute( atPwd );
923         
924         assertEquals( 0, attr1.size() );
925         assertFalse( attr1.contains( BYTES1 ) );
926         assertFalse( attr1.contains( (byte[])null ) );
927         
928         attr1.add( (byte[])null );
929         assertEquals( 1, attr1.size() );
930         assertTrue( attr1.contains( (byte[])null ) );
931         
932         attr1.remove( (byte[])null );
933         assertFalse( attr1.contains( (byte[])null ) );
934         assertEquals( 0, attr1.size() );
935         
936         attr1.add(  BYTES1, BYTES2, BYTES3 );
937         assertEquals( 3, attr1.size() );
938         assertTrue( attr1.contains( BYTES1 ) );
939         assertTrue( attr1.contains( BYTES2 ) );
940         assertTrue( attr1.contains( BYTES3 ) );
941         assertFalse( attr1.contains( BYTES4 ) );
942         assertFalse( attr1.contains( (byte[])null ) );
943     }
944 
945 
946     /**
947      * Test method testEquals()
948      */
949     @Test
950     public void testEquals()
951     {
952         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
953         
954         assertFalse( attr1.equals( null ) );
955         
956         ServerAttribute attr2 = new DefaultServerAttribute( atCN );
957         
958         assertTrue( attr1.equals( attr2 ) );
959         
960         attr2.setId( "CN" );
961         assertTrue( attr1.equals( attr2 ) );
962 
963         attr1.setId( "CommonName" );
964         assertTrue( attr1.equals( attr2 ) );
965         
966         attr1.setUpId( "CN" );
967         assertTrue( attr1.equals( attr2 ) );
968         
969         attr1.add( "a", "b", "c" );
970         attr2.add( "c", "b", "a" );
971         assertTrue( attr1.equals( attr2 ) );
972         
973         attr1.setHR( true );
974         attr2.setHR( false );
975         assertTrue( attr1.equals( attr2 ) );
976         
977         ServerAttribute attr3 = new DefaultServerAttribute( atPwd );
978         ServerAttribute attr4 = new DefaultServerAttribute( atPwd );
979         
980         attr3.put( NULL_BINARY_VALUE );
981         attr4.put( NULL_BINARY_VALUE );
982         assertTrue( attr3.equals( attr4 ) );
983         
984         ServerAttribute attr5 = new DefaultServerAttribute( atPwd );
985         ServerAttribute attr6 = new DefaultServerAttribute( atCN );
986         assertFalse( attr5.equals( attr6 ) );
987         
988         attr5.put( NULL_BINARY_VALUE );
989         attr6.put( NULL_STRING_VALUE );
990         assertFalse( attr5.equals( attr6 ) );
991 
992         ServerAttribute attr7 = new DefaultServerAttribute( atCN );
993         ServerAttribute attr8 = new DefaultServerAttribute( atPwd );
994         
995         attr7.put( "a" );
996         attr8.put( BYTES2 );
997         assertFalse( attr7.equals( attr8 ) );
998 
999         ServerAttribute attr9 = new DefaultServerAttribute( atCN );
1000         ServerAttribute attr10 = new DefaultServerAttribute( atPwd );
1001         
1002         attr7.put( "a" );
1003         attr7.add( BYTES2 );
1004         attr8.put( "a", "b" );
1005         assertFalse( attr9.equals( attr10 ) );
1006     }
1007 
1008 
1009     /**
1010      * Test method get()
1011      */
1012     @Test
1013     public void testGet()
1014     {
1015         ServerAttribute attr1 = new DefaultServerAttribute( "cn", atCN );
1016         
1017         attr1.add( (String)null );
1018         assertEquals( NULL_STRING_VALUE,attr1.get() );
1019 
1020         ServerAttribute attr2 = new DefaultServerAttribute( "cn", atCN );
1021         
1022         attr2.add( "a", "b", "c" );
1023         assertEquals( "a", attr2.get().get() );
1024         
1025         attr2.remove( "a" );
1026         assertEquals( "b", attr2.get().get() );
1027 
1028         attr2.remove( "b" );
1029         assertEquals( "c", attr2.get().get() );
1030 
1031         attr2.remove( "c" );
1032         assertNull( attr2.get() );
1033 
1034         ServerAttribute attr3 = new DefaultServerAttribute( "userPassword", atPwd );
1035         
1036         attr3.add( BYTES1, BYTES2, BYTES3 );
1037         assertTrue( Arrays.equals( BYTES1, (byte[])attr3.get().get() ) );
1038         
1039         attr3.remove( BYTES1 );
1040         assertTrue( Arrays.equals( BYTES2, (byte[])attr3.get().get() ) );
1041 
1042         attr3.remove( BYTES2 );
1043         assertTrue( Arrays.equals( BYTES3, (byte[])attr3.get().get() ) );
1044 
1045         attr3.remove( BYTES3 );
1046         assertNull( attr2.get() );
1047     }
1048 
1049 
1050     /**
1051      * Test method getAll()
1052      */
1053     @Test
1054     public void testGetAll()
1055     {
1056         ServerAttribute attr = new DefaultServerAttribute( atCN );
1057         
1058         Iterator<Value<?>> iterator = attr.getAll(); 
1059         assertFalse( iterator.hasNext() );
1060         
1061         attr.add( NULL_STRING_VALUE );
1062         iterator = attr.getAll(); 
1063         assertTrue( iterator.hasNext() );
1064         
1065         Value<?> value = iterator.next();
1066         assertEquals( NULL_STRING_VALUE, value );
1067         
1068         attr.clear();
1069         iterator = attr.getAll(); 
1070         assertFalse( iterator.hasNext() );
1071         
1072         attr.add(  "a", "b", "c" );
1073         iterator = attr.getAll(); 
1074         assertTrue( iterator.hasNext() );
1075         assertEquals( "a", iterator.next().get() );
1076         assertEquals( "b", iterator.next().get() );
1077         assertEquals( "c", iterator.next().get() );
1078         assertFalse( iterator.hasNext() );
1079     }
1080 
1081 
1082     /**
1083      * Test method size()
1084      */
1085     @Test
1086     public void testSize() throws Exception
1087     {
1088         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1089 
1090         assertEquals( 0, attr1.size() );
1091         
1092         attr1.add( (String)null );
1093         assertEquals( 1, attr1.size() );
1094 
1095         ServerAttribute attr2 = new DefaultServerAttribute( atCN );
1096         
1097         attr2.add( "a", "b" );
1098         assertEquals( 2, attr2.size() );
1099         
1100         attr2.clear();
1101         assertEquals( 0, attr2.size() );
1102 
1103         ServerAttribute attr3 = new DefaultServerAttribute( atC );
1104         
1105         attr3.add( "US" );
1106         assertEquals( 1, attr3.size() );
1107         
1108         // TODO : forbid addition of more than 1 value for SINGLE-VALUE attributes
1109         attr3.add( "FR" );
1110         assertEquals( 2, attr3.size() );
1111     }
1112 
1113 
1114     /**
1115      * Test method put( byte[]... )
1116      */
1117     @Test
1118     public void testPutByteArray() throws InvalidAttributeValueException, Exception
1119     {
1120         ServerAttribute attr1 = new DefaultServerAttribute( atPwd );
1121         
1122         int nbAdded = attr1.put( (byte[])null );
1123         assertEquals( 1, nbAdded );
1124         assertFalse( attr1.isHR() );
1125         assertTrue( Arrays.equals( NULL_BINARY_VALUE.get(), attr1.getBytes() ) );
1126         
1127         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
1128         
1129         nbAdded = attr2.put( StringTools.EMPTY_BYTES );
1130         assertEquals( 1, nbAdded );
1131         assertFalse( attr2.isHR() );
1132         assertTrue( Arrays.equals( StringTools.EMPTY_BYTES, attr2.getBytes() ) );
1133         
1134         ServerAttribute attr3 = new DefaultServerAttribute( atPwd );
1135         
1136         nbAdded = attr3.put( BYTES1 );
1137         assertEquals( 1, nbAdded );
1138         assertFalse( attr3.isHR() );
1139         assertTrue( Arrays.equals( BYTES1, attr3.getBytes() ) );
1140         
1141         ServerAttribute attr4 = new DefaultServerAttribute( atPwd );
1142         
1143         nbAdded = attr4.put( BYTES1, BYTES2 );
1144         assertEquals( 2, nbAdded );
1145         assertFalse( attr4.isHR() );
1146         assertTrue( attr4.contains( BYTES1 ) );
1147         assertTrue( attr4.contains( BYTES2 ) );
1148         
1149         nbAdded = attr4.put( BYTES3, BYTES4 );
1150         assertEquals( 2, nbAdded );
1151         assertFalse( attr4.isHR() );
1152         assertTrue( attr4.contains( BYTES3 ) );
1153         assertTrue( attr4.contains( BYTES4 ) );
1154         
1155         ServerAttribute attr5 = new DefaultServerAttribute( atPwd );
1156         
1157         nbAdded = attr5.put( BYTES1, BYTES2, (byte[])null, BYTES3 );
1158         assertEquals( 4, nbAdded );
1159         assertFalse( attr5.isHR() );
1160         assertTrue( attr5.contains( BYTES1 ) );
1161         assertTrue( attr5.contains( BYTES2 ) );
1162         assertTrue( attr5.contains( (byte[])null ) );
1163         assertTrue( attr5.contains( BYTES3 ) );
1164 
1165         ServerAttribute attr6 = new DefaultServerAttribute( atPwd );
1166         
1167         attr6.setHR( true );
1168         assertFalse( attr6.isHR() );
1169         nbAdded = attr6.put( BYTES1, (byte[])null );
1170         assertEquals( 2, nbAdded );
1171         assertTrue( attr6.contains( BYTES1 ) );
1172         assertTrue( attr6.contains( (byte[])null ) );
1173     }
1174 
1175 
1176     /**
1177      * Test method put( String... )
1178      */
1179     @Test
1180     public void testPutStringArray() throws InvalidAttributeValueException
1181     {
1182         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1183         
1184         int nbAdded = attr1.put( (String)null );
1185         assertEquals( 1, nbAdded );
1186         assertTrue( attr1.isHR() );
1187         assertEquals( NULL_STRING_VALUE, attr1.get() );
1188         
1189         ServerAttribute attr2 = new DefaultServerAttribute( atCN );
1190         
1191         nbAdded = attr2.put( "" );
1192         assertEquals( 1, nbAdded );
1193         assertTrue( attr2.isHR() );
1194         assertEquals( "", attr2.getString() );
1195         
1196         ServerAttribute attr3 = new DefaultServerAttribute( atCN );
1197         
1198         nbAdded = attr3.put( "t" );
1199         assertEquals( 1, nbAdded );
1200         assertTrue( attr3.isHR() );
1201         assertEquals( "t", attr3.getString() );
1202         
1203         ServerAttribute attr4 = new DefaultServerAttribute( atCN );
1204         
1205         nbAdded = attr4.put( "a", "b", "c", "d" );
1206         assertEquals( 4, nbAdded );
1207         assertTrue( attr4.isHR() );
1208         assertEquals( "a", attr4.getString() );
1209         assertTrue( attr4.contains( "a" ) );
1210         assertTrue( attr4.contains( "b" ) );
1211         assertTrue( attr4.contains( "c" ) );
1212         assertTrue( attr4.contains( "d" ) );
1213         
1214         nbAdded = attr4.put( "e" );
1215         assertEquals( 1, nbAdded );
1216         assertTrue( attr4.isHR() );
1217         assertEquals( "e", attr4.getString() );
1218         assertFalse( attr4.contains( "a" ) );
1219         assertFalse( attr4.contains( "b" ) );
1220         assertFalse( attr4.contains( "c" ) );
1221         assertFalse( attr4.contains( "d" ) );
1222         assertTrue( attr4.contains( "e" ) );
1223         
1224         nbAdded = attr4.put( BYTES1 );
1225         assertEquals( 0, nbAdded );
1226         assertTrue( attr4.isHR() );
1227         
1228         ServerAttribute attr5 = new DefaultServerAttribute( atCN );
1229         
1230         nbAdded = attr5.put( "a", "b", (String)null, "d" );
1231         assertEquals( 4, nbAdded );
1232         assertTrue( attr5.isHR() );
1233         assertTrue( attr5.contains( "a" ) );
1234         assertTrue( attr5.contains( "b" ) );
1235         assertTrue( attr5.contains( NULL_STRING_VALUE ) );
1236         assertTrue( attr5.contains( "d" ) );
1237 
1238         ServerAttribute attr6 = new DefaultServerAttribute( atPwd );
1239         
1240         nbAdded = attr6.put( "a", (String)null );
1241         assertEquals( 0, nbAdded );
1242         assertFalse( attr6.isHR() );
1243     }
1244 
1245 
1246     /**
1247      * Test method put( Value... )
1248      */
1249     @Test
1250     public void testPutValueArray() throws Exception
1251     {
1252         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1253         
1254         assertEquals( 0, attr1.size() );
1255         
1256         attr1.put( NULL_STRING_VALUE );
1257         assertEquals( 1, attr1.size() );
1258         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1259         
1260         attr1.put( STR_VALUE1, STR_VALUE2, STR_VALUE3 );
1261         assertEquals( 3, attr1.size() );
1262         assertTrue( attr1.contains( STR_VALUE1 ) );
1263         assertTrue( attr1.contains( STR_VALUE2 ) );
1264         assertTrue( attr1.contains( STR_VALUE3 ) );
1265 
1266         attr1.put( STR_VALUE1, NULL_STRING_VALUE, STR_VALUE3 );
1267         assertEquals( 3, attr1.size() );
1268         assertTrue( attr1.contains( STR_VALUE1 ) );
1269         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1270         assertTrue( attr1.contains( STR_VALUE3 ) );
1271         
1272         attr1.put( STR_VALUE1, NULL_STRING_VALUE, BIN_VALUE3 );
1273         assertEquals( 2, attr1.size() );
1274         assertTrue( attr1.contains( STR_VALUE1 ) );
1275         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1276         assertFalse( attr1.contains( STR_VALUE3 ) );
1277         
1278 
1279         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
1280         assertEquals( 0, attr2.size() );
1281         
1282         attr2.put( NULL_BINARY_VALUE );
1283         assertEquals( 1, attr2.size() );
1284         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
1285         
1286         attr2.put( BIN_VALUE1, BIN_VALUE2, BIN_VALUE3 );
1287         assertEquals( 3, attr2.size() );
1288         assertTrue( attr2.contains( BIN_VALUE1 ) );
1289         assertTrue( attr2.contains( BIN_VALUE2 ) );
1290         assertTrue( attr2.contains( BIN_VALUE3 ) );
1291         
1292         attr2.put( BIN_VALUE1, NULL_BINARY_VALUE, STR_VALUE3 );
1293         assertEquals( 2, attr2.size() );
1294         assertTrue( attr2.contains( BIN_VALUE1 ) );
1295         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
1296         assertFalse( attr2.contains( BIN_VALUE3 ) );
1297     }
1298 
1299 
1300     /**
1301      * Test method put( List&lt;Value&gt; )
1302      */
1303     @Test
1304     public void testPutListOfValues()
1305     {
1306         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1307         
1308         assertEquals( 0, attr1.size() );
1309         
1310         List<Value<?>> list = new ArrayList<Value<?>>();
1311         list.add( NULL_STRING_VALUE );
1312         
1313         attr1.put( list );
1314         assertEquals( 1, attr1.size() );
1315         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1316         
1317         list.clear();
1318         list.add( STR_VALUE1 );
1319         list.add( STR_VALUE2 );
1320         list.add( STR_VALUE3 );
1321         attr1.put( list );
1322         assertEquals( 3, attr1.size() );
1323         assertTrue( attr1.contains( STR_VALUE1 ) );
1324         assertTrue( attr1.contains( STR_VALUE2 ) );
1325         assertTrue( attr1.contains( STR_VALUE3 ) );
1326 
1327         list.clear();
1328         list.add( STR_VALUE1 );
1329         list.add( NULL_STRING_VALUE );
1330         list.add( STR_VALUE3 );
1331         attr1.put( list );
1332         assertEquals( 3, attr1.size() );
1333         assertTrue( attr1.contains( STR_VALUE1 ) );
1334         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1335         assertTrue( attr1.contains( STR_VALUE3 ) );
1336         
1337         list.clear();
1338         list.add( STR_VALUE1 );
1339         list.add( NULL_STRING_VALUE );
1340         list.add( BIN_VALUE3 );
1341         attr1.put( list );
1342         assertEquals( 2, attr1.size() );
1343         assertTrue( attr1.contains( STR_VALUE1 ) );
1344         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1345         assertFalse( attr1.contains( STR_VALUE3 ) );
1346         
1347 
1348         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
1349         assertEquals( 0, attr2.size() );
1350         
1351         list.clear();
1352         list.add( NULL_BINARY_VALUE );
1353         attr2.put( list );
1354         assertEquals( 1, attr2.size() );
1355         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
1356         
1357         list.clear();
1358         list.add( BIN_VALUE1 );
1359         list.add( BIN_VALUE2 );
1360         list.add( BIN_VALUE3 );
1361         attr2.put( list );
1362         assertEquals( 3, attr2.size() );
1363         assertTrue( attr2.contains( BIN_VALUE1 ) );
1364         assertTrue( attr2.contains( BIN_VALUE2 ) );
1365         assertTrue( attr2.contains( BIN_VALUE3 ) );
1366         
1367         list.clear();
1368         list.add( BIN_VALUE1 );
1369         list.add( NULL_BINARY_VALUE );
1370         list.add( STR_VALUE3 );
1371         attr2.put( list );
1372         assertEquals( 2, attr2.size() );
1373         assertTrue( attr2.contains( BIN_VALUE1 ) );
1374         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
1375         assertFalse( attr2.contains( BIN_VALUE3 ) );
1376     }
1377 
1378 
1379     /**
1380      * Test method remove( Value... )
1381      */
1382     @Test
1383     public void testRemoveValueArray() throws Exception
1384     {
1385         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1386 
1387         assertFalse( attr1.remove( STR_VALUE1 ) );
1388 
1389         attr1.setHR( true );
1390         assertFalse( attr1.remove( STR_VALUE1 ) );
1391         
1392         attr1.put( "a", "b", "c" );
1393         assertTrue( attr1.remove( STR_VALUE1 ) );
1394         assertEquals( 2, attr1.size() );
1395         
1396         assertTrue( attr1.remove( STR_VALUE2, STR_VALUE3 ) );
1397         assertEquals( 0, attr1.size() );
1398         
1399         assertFalse( attr1.remove( STR_VALUE4 ) );
1400         
1401         attr1.put( "a", "b", "c" );
1402         assertFalse( attr1.remove( STR_VALUE2, STR_VALUE4 ) );
1403         assertEquals( 2, attr1.size() );
1404         
1405         attr1.clear();
1406         attr1.put( "a", (String)null, "b" );
1407         assertTrue( attr1.remove( NULL_STRING_VALUE, STR_VALUE1 ) );
1408         assertEquals( 1, attr1.size() );
1409         
1410         attr1.clear();
1411         attr1.put( "a", (String)null, "b" );
1412         attr1.add( BYTES3 );
1413         assertFalse( attr1.remove( NULL_STRING_VALUE, STR_VALUE1, BIN_VALUE3 ) );
1414         assertEquals( 1, attr1.size() );
1415         
1416         ServerAttribute attr2 = new DefaultServerAttribute( atPwd );
1417 
1418         assertFalse( attr2.remove( BIN_VALUE1 ) );
1419 
1420         attr2.setHR( true );
1421         assertFalse( attr2.remove( BIN_VALUE1 ) );
1422         
1423         attr2.put( BYTES1, BYTES2, BYTES3 );
1424         assertTrue( attr2.remove( BIN_VALUE1 ) );
1425         assertEquals( 2, attr2.size() );
1426         
1427         assertTrue( attr2.remove( BIN_VALUE2, BIN_VALUE3 ) );
1428         assertEquals( 0, attr2.size() );
1429         
1430         assertFalse( attr2.remove( BIN_VALUE4 ) );
1431         
1432         attr2.put( BYTES1, BYTES2, BYTES3 );
1433         assertFalse( attr2.remove( BIN_VALUE2, STR_VALUE4 ) );
1434         assertEquals( 2, attr2.size() );
1435         
1436         attr2.clear();
1437         attr2.put( BYTES1, (byte[])null, BYTES3 );
1438         assertFalse( attr2.remove( NULL_STRING_VALUE, BIN_VALUE1 ) );
1439         assertEquals( 2, attr2.size() );
1440         
1441         attr2.clear();
1442         attr2.put( BYTES1, (byte[])null, BYTES2 );
1443         attr2.add( "c" );
1444         assertFalse( attr2.remove( NULL_STRING_VALUE, BIN_VALUE1, STR_VALUE3 ) );
1445         assertEquals( 2, attr2.size() );
1446     }
1447 
1448 
1449     /**
1450      * Test method remove( byte... )
1451      */
1452     @Test
1453     public void testRemoveByteArray() throws Exception
1454     {
1455         ServerAttribute attr1 = new DefaultServerAttribute( atPwd );
1456 
1457         assertFalse( attr1.remove( BYTES1 ) );
1458 
1459         attr1.put( BYTES1, BYTES2, BYTES3 );
1460         assertTrue( attr1.remove( BYTES1 ) );
1461         assertEquals( 2, attr1.size() );
1462         
1463         assertTrue( attr1.remove( BYTES2, BYTES3 ) );
1464         assertEquals( 0, attr1.size() );
1465         
1466         assertFalse( attr1.remove( BYTES4 ) );
1467         
1468         attr1.put( BYTES1, BYTES2, BYTES3 );
1469         assertFalse( attr1.remove( BYTES3, BYTES4 ) );
1470         assertEquals( 2, attr1.size() );
1471         
1472         attr1.clear();
1473         attr1.put( BYTES1, (byte[])null, BYTES2 ) ;
1474         assertTrue( attr1.remove( (byte[])null, BYTES1 ) );
1475         assertEquals( 1, attr1.size() );
1476     }
1477 
1478 
1479     /**
1480      * Test method remove( String... )
1481      */
1482     @Test
1483     public void testRemoveStringArray() throws Exception
1484     {
1485         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1486 
1487         assertFalse( attr1.remove( "a" ) );
1488 
1489         attr1.setHR( true );
1490         assertFalse( attr1.remove( "a" ) );
1491         
1492         attr1.put( "a", "b", "c" );
1493         assertTrue( attr1.remove( "a" ) );
1494         assertEquals( 2, attr1.size() );
1495         
1496         assertTrue( attr1.remove( "b", "c" ) );
1497         assertEquals( 0, attr1.size() );
1498         
1499         assertFalse( attr1.remove( "d" ) );
1500         
1501         attr1.put( "a", "b", "c" );
1502         assertFalse( attr1.remove( "b", "e" ) );
1503         assertEquals( 2, attr1.size() );
1504         
1505         attr1.clear();
1506         attr1.put( "a", (String)null, "b" );
1507         assertTrue( attr1.remove( (String )null, "a" ) );
1508         assertEquals( 1, attr1.size() );
1509         
1510         EntryAttribute attr2 = new DefaultClientAttribute( "test" );
1511         
1512         attr2.put( BYTES1, BYTES2, BYTES3 );
1513         
1514         assertFalse( attr2.remove( (String)null ) );
1515         assertTrue( attr2.remove( "ab", "c" ) );
1516         assertFalse( attr2.remove( "d" ) );
1517     }
1518 
1519 
1520     /**
1521      * Test method iterator()
1522      */
1523     @Test
1524     public void testIterator()
1525     {
1526         ServerAttribute attr1 = new DefaultServerAttribute( atCN );
1527         attr1.add(  "a", "b", "c" );
1528         
1529         Iterator<Value<?>> iter = attr1.iterator();
1530         
1531         assertTrue( iter.hasNext() );
1532         
1533         String[] values = new String[]{ "a", "b", "c" };
1534         int pos = 0;
1535         
1536         for ( Value<?> val:attr1 )
1537         {
1538             assertTrue( val instanceof ServerStringValue );
1539             assertEquals( values[pos++], val.get() );
1540         }
1541     }
1542 
1543 
1544     /**
1545      * Test method toString
1546      */
1547     @Test
1548     public void testToString()
1549     {
1550         ServerAttribute attr = new DefaultServerAttribute( atCN );
1551         
1552         assertEquals( "    cn: (null)\n", attr.toString() );
1553         
1554         attr.setUpId( "CommonName" );
1555         assertEquals( "    CommonName: (null)\n", attr.toString() );
1556         
1557         attr.add( (String)null );
1558         assertEquals( "    CommonName: ''\n", attr.toString() );
1559 
1560         attr.put( "a", "b" );
1561         assertEquals( "    CommonName: a\n    CommonName: b\n", attr.toString() );
1562     }
1563 
1564 
1565     /**
1566      * Test method instanceOf()
1567      */
1568     @Test
1569     public void testInstanceOf() throws Exception
1570     {
1571         ServerAttribute attr = new DefaultServerAttribute( atCN );
1572         
1573         assertTrue( attr.instanceOf( "CommonName" ) );
1574         assertTrue( attr.instanceOf( "2.5.4.3" ) );
1575         assertTrue( attr.instanceOf( "  Cn  " ) );
1576         assertFalse( attr.instanceOf( "  " ) );
1577         assertFalse( attr.instanceOf( "sn" ) );
1578         assertFalse( attr.instanceOf( "name" ) );
1579     }
1580 
1581 
1582     /**
1583      * Test method setUpId( String, AttributeType )
1584      */
1585     @Test
1586     public void testSetUpIdStringAttributeType() throws Exception
1587     {
1588         ServerAttribute attr = new DefaultServerAttribute( atSN );
1589         
1590         attr.setUpId( null, atCN );
1591         assertEquals( "cn", attr.getId() );
1592         assertEquals( "cn", attr.getUpId() );
1593         assertEquals( atCN, attr.getAttributeType() );
1594 
1595         attr.setUpId( "  ", atCN );
1596         assertEquals( "cn", attr.getId() );
1597         assertEquals( "cn", attr.getUpId() );
1598         assertEquals( atCN, attr.getAttributeType() );
1599 
1600         attr.setUpId( "  CN  ", atCN );
1601         assertEquals( "cn", attr.getId() );
1602         assertEquals( "CN", attr.getUpId() );
1603         assertEquals( atCN, attr.getAttributeType() );
1604 
1605         attr.setUpId( "  CommonName  ", atCN );
1606         assertEquals( "commonname", attr.getId() );
1607         assertEquals( "CommonName", attr.getUpId() );
1608         assertEquals( atCN, attr.getAttributeType() );
1609 
1610         attr.setUpId( "  2.5.4.3  ", atCN );
1611         assertEquals( "2.5.4.3", attr.getId() );
1612         assertEquals( "2.5.4.3", attr.getUpId() );
1613         assertEquals( atCN, attr.getAttributeType() );
1614 
1615         // Check with wrong IDs
1616         try
1617         {
1618             attr.setUpId( "sn", atCN );
1619             fail();
1620         }
1621         catch ( IllegalArgumentException iae )
1622         {
1623             assertTrue( true );
1624         }
1625 
1626         try
1627         {
1628             attr.setUpId( "  2.5.4.4  ", atCN );
1629             fail();
1630         }
1631         catch ( IllegalArgumentException iae )
1632         {
1633             assertTrue( true );
1634         }
1635     }
1636 
1637 
1638     /**
1639      * Test method setUpId( String ) inherited from ClientAttribute
1640      */
1641     @Test
1642     public void testSetUpIdString() throws Exception
1643     {
1644         ServerAttribute attr = new DefaultServerAttribute( atCN );
1645         
1646         attr.setUpId( "cn" );
1647         assertEquals( "cn", attr.getId() );
1648         assertEquals( "cn", attr.getUpId() );
1649         assertEquals( atCN, attr.getAttributeType() );
1650 
1651         attr.setUpId( "  CN  " );
1652         assertEquals( "cn", attr.getId() );
1653         assertEquals( "CN", attr.getUpId() );
1654         assertEquals( atCN, attr.getAttributeType() );
1655 
1656         attr.setUpId( "  CommonName  ");
1657         assertEquals( "commonname", attr.getId() );
1658         assertEquals( "CommonName", attr.getUpId() );
1659         assertEquals( atCN, attr.getAttributeType() );
1660 
1661         attr.setUpId( "  2.5.4.3  " );
1662         assertEquals( "2.5.4.3", attr.getId() );
1663         assertEquals( "2.5.4.3", attr.getUpId() );
1664         assertEquals( atCN, attr.getAttributeType() );
1665         
1666         // Now check wrong IDs
1667         attr = new DefaultServerAttribute( atCN );
1668         attr.setUpId( "sn" );
1669         assertEquals( "cn", attr.getId() );
1670         assertEquals( "cn", attr.getUpId() );
1671         assertEquals( atCN, attr.getAttributeType() );
1672 
1673         attr.setUpId( "  SN  " );
1674         assertEquals( "cn", attr.getId() );
1675         assertEquals( "cn", attr.getUpId() );
1676         assertEquals( atCN, attr.getAttributeType() );
1677 
1678         attr.setUpId( "  surname  " );
1679         assertEquals( "cn", attr.getId() );
1680         assertEquals( "cn", attr.getUpId() );
1681         assertEquals( atCN, attr.getAttributeType() );
1682 
1683         attr.setUpId( "  2.5.4.4  " );
1684         assertEquals( "cn", attr.getId() );
1685         assertEquals( "cn", attr.getUpId() );
1686         assertEquals( atCN, attr.getAttributeType() );
1687     }
1688 
1689 
1690     /**
1691      * Test method setAttributeType( AttributeType )
1692      */
1693     @Test
1694     public void testSetAttributeType() throws Exception
1695     {
1696         ServerAttribute attr = new DefaultServerAttribute( atCN );
1697         
1698         try
1699         {
1700             attr.setAttributeType( null );
1701             fail();
1702         }
1703         catch ( IllegalArgumentException iae )
1704         {
1705             assertTrue( true );
1706         }
1707         
1708         attr.setAttributeType( atSN );
1709         
1710         assertTrue( attr.instanceOf( "Surname" ) );
1711         assertEquals( "sn", attr.getId() );
1712     }
1713 
1714 
1715     /**
1716      * Test method getAttributeType()
1717      */
1718     @Test
1719     public void testGetAttributeType() throws Exception
1720     {
1721         ServerAttribute attr = new DefaultServerAttribute( atSN );
1722         assertEquals( atSN, attr.getAttributeType() );
1723     }
1724 
1725 
1726     /**
1727      * Test constructor DefaultServerAttribute( AttributeType )
1728      */
1729     @Test
1730     public void testDefaultServerAttributeAttributeType()
1731     {
1732         ServerAttribute attr = new DefaultServerAttribute( atCN );
1733         
1734         assertTrue( attr.isHR() );
1735         assertEquals( 0, attr.size() );
1736         assertEquals( "cn", attr.getId() );
1737         assertEquals( "cn", attr.getUpId() );
1738         assertEquals( atCN, attr.getAttributeType() );
1739     }
1740 
1741 
1742     /**
1743      * Test constructor DefaultServerAttribute( String, AttributeType )
1744      */
1745     @Test
1746     public void testDefaultServerAttributeStringAttributeType()
1747     {
1748         ServerAttribute attr1 = new DefaultServerAttribute( "cn", atCN );
1749         
1750         assertTrue( attr1.isHR() );
1751         assertEquals( 0, attr1.size() );
1752         assertEquals( "cn", attr1.getId() );
1753         assertEquals( "cn", attr1.getUpId() );
1754         assertEquals( atCN, attr1.getAttributeType() );
1755 
1756         ServerAttribute attr2 = new DefaultServerAttribute( "  CommonName  ", atCN );
1757         
1758         assertTrue( attr2.isHR() );
1759         assertEquals( 0, attr2.size() );
1760         assertEquals( "commonname", attr2.getId() );
1761         assertEquals( "CommonName", attr2.getUpId() );
1762         assertEquals( atCN, attr2.getAttributeType() );
1763 
1764         ServerAttribute attr3 = new DefaultServerAttribute( "  ", atCN );
1765         
1766         assertTrue( attr3.isHR() );
1767         assertEquals( 0, attr3.size() );
1768         assertEquals( "cn", attr3.getId() );
1769         assertEquals( "cn", attr3.getUpId() );
1770         assertEquals( atCN, attr3.getAttributeType() );
1771     }
1772 
1773 
1774     /**
1775      * Test constructor DefaultServerAttribute( AttributeType, Value... )
1776      */
1777     @Test
1778     public void testDefaultServerAttributeAttributeTypeValueArray() throws Exception
1779     {
1780         ServerAttribute attr1 = new DefaultServerAttribute( atCN, STR_VALUE1, STR_VALUE2, NULL_STRING_VALUE );
1781         
1782         assertTrue( attr1.isHR() );
1783         assertEquals( 3, attr1.size() );
1784         assertEquals( "cn", attr1.getId() );
1785         assertEquals( "cn", attr1.getUpId() );
1786         assertEquals( atCN, attr1.getAttributeType() );
1787         assertTrue( attr1.contains( "a", "b" ) );
1788         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1789 
1790         ServerAttribute attr2 = new DefaultServerAttribute( atCN, STR_VALUE1, BIN_VALUE2, NULL_STRING_VALUE );
1791         
1792         assertTrue( attr2.isHR() );
1793         assertEquals( 2, attr2.size() );
1794         assertEquals( "cn", attr2.getId() );
1795         assertEquals( "cn", attr2.getUpId() );
1796         assertEquals( atCN, attr2.getAttributeType() );
1797         assertTrue( attr2.contains( "a" ) );
1798         assertTrue( attr2.contains( NULL_STRING_VALUE ) );
1799     }
1800 
1801 
1802     /**
1803      * Test constructor DefaultServerAttribute( String, AttributeType, Value... )
1804      */
1805     @Test
1806     public void testDefaultServerAttributeStringAttributeTypeValueArray()
1807     {
1808         ServerAttribute attr1 = new DefaultServerAttribute( "cn", atCN, STR_VALUE1, STR_VALUE2, NULL_STRING_VALUE );
1809         
1810         assertTrue( attr1.isHR() );
1811         assertEquals( 3, attr1.size() );
1812         assertEquals( "cn", attr1.getId() );
1813         assertEquals( "cn", attr1.getUpId() );
1814         assertEquals( atCN, attr1.getAttributeType() );
1815         assertTrue( attr1.contains( "a", "b" ) );
1816         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1817 
1818         ServerAttribute attr2 = new DefaultServerAttribute( atCN, STR_VALUE1, BIN_VALUE2, NULL_STRING_VALUE );
1819         
1820         assertTrue( attr2.isHR() );
1821         assertEquals( 2, attr2.size() );
1822         assertEquals( "cn", attr2.getId() );
1823         assertEquals( "cn", attr2.getUpId() );
1824         assertEquals( atCN, attr2.getAttributeType() );
1825         assertTrue( attr2.contains( "a" ) );
1826         assertTrue( attr2.contains( NULL_STRING_VALUE ) );
1827 
1828         ServerAttribute attr3 = new DefaultServerAttribute( "CommonName", atCN, STR_VALUE1, STR_VALUE2, NULL_STRING_VALUE );
1829         
1830         assertTrue( attr3.isHR() );
1831         assertEquals( 3, attr3.size() );
1832         assertEquals( "commonname", attr3.getId() );
1833         assertEquals( "CommonName", attr3.getUpId() );
1834         assertEquals( atCN, attr3.getAttributeType() );
1835         assertTrue( attr3.contains( "a", "b" ) );
1836         assertTrue( attr3.contains( NULL_STRING_VALUE ) );
1837 
1838         ServerAttribute attr4 = new DefaultServerAttribute( " 2.5.4.3 ", atCN, STR_VALUE1, STR_VALUE2, NULL_STRING_VALUE );
1839         
1840         assertTrue( attr4.isHR() );
1841         assertEquals( 3, attr4.size() );
1842         assertEquals( "2.5.4.3", attr4.getId() );
1843         assertEquals( "2.5.4.3", attr4.getUpId() );
1844         assertEquals( atCN, attr4.getAttributeType() );
1845         assertTrue( attr4.contains( "a", "b" ) );
1846         assertTrue( attr4.contains( NULL_STRING_VALUE ) );
1847     }
1848 
1849 
1850     /**
1851      * Test constructor DefaultServerAttribute( AttributeType, String... ) 
1852      */
1853     @Test
1854     public void testDefaultServerAttributeAttributeTypeStringArray()
1855     {
1856         ServerAttribute attr1 = new DefaultServerAttribute( atCN, "a", "b", (String)null );
1857         
1858         assertTrue( attr1.isHR() );
1859         assertEquals( 3, attr1.size() );
1860         assertEquals( "cn", attr1.getId() );
1861         assertEquals( "cn", attr1.getUpId() );
1862         assertEquals( atCN, attr1.getAttributeType() );
1863         assertTrue( attr1.contains( "a", "b" ) );
1864         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1865 
1866         ServerAttribute attr2 = new DefaultServerAttribute( atCN, STR_VALUE1, BIN_VALUE2, NULL_STRING_VALUE );
1867         
1868         assertTrue( attr2.isHR() );
1869         assertEquals( 2, attr2.size() );
1870         assertEquals( "cn", attr2.getId() );
1871         assertEquals( "cn", attr2.getUpId() );
1872         assertEquals( atCN, attr2.getAttributeType() );
1873         assertTrue( attr2.contains( "a" ) );
1874         assertTrue( attr2.contains( NULL_STRING_VALUE ) );
1875     }
1876 
1877 
1878     /**
1879      * Test constructor DefaultServerAttribute( String, AttributeType, String... )
1880      */
1881     @Test
1882     public void testDefaultServerAttributeStringAttributeTypeStringArray()
1883     {
1884         ServerAttribute attr1 = new DefaultServerAttribute( "cn", atCN, "a", "b", (String)null );
1885         
1886         assertTrue( attr1.isHR() );
1887         assertEquals( 3, attr1.size() );
1888         assertEquals( "cn", attr1.getId() );
1889         assertEquals( "cn", attr1.getUpId() );
1890         assertEquals( atCN, attr1.getAttributeType() );
1891         assertTrue( attr1.contains( "a", "b" ) );
1892         assertTrue( attr1.contains( NULL_STRING_VALUE ) );
1893 
1894         ServerAttribute attr2 = new DefaultServerAttribute( "CommonName", atCN, "a", "b", (String)null );
1895         
1896         assertTrue( attr2.isHR() );
1897         assertEquals( 3, attr2.size() );
1898         assertEquals( "commonname", attr2.getId() );
1899         assertEquals( "CommonName", attr2.getUpId() );
1900         assertEquals( atCN, attr2.getAttributeType() );
1901         assertTrue( attr2.contains( "a", "b" ) );
1902         assertTrue( attr2.contains( NULL_STRING_VALUE ) );
1903 
1904         ServerAttribute attr3 = new DefaultServerAttribute( " 2.5.4.3 ", atCN, "a", "b", (String)null );
1905         
1906         assertTrue( attr3.isHR() );
1907         assertEquals( 3, attr3.size() );
1908         assertEquals( "2.5.4.3", attr3.getId() );
1909         assertEquals( "2.5.4.3", attr3.getUpId() );
1910         assertEquals( atCN, attr3.getAttributeType() );
1911         assertTrue( attr3.contains( "a", "b" ) );
1912         assertTrue( attr3.contains( NULL_STRING_VALUE ) );
1913     }
1914 
1915 
1916     /**
1917      * Test method DefaultServerAttribute( AttributeType, byte[]... ) 
1918      */
1919     @Test
1920     public void testDefaultServerAttributeAttributeTypeByteArray()
1921     {
1922         ServerAttribute attr1 = new DefaultServerAttribute( atPwd, BYTES1, BYTES2, (byte[])null );
1923         
1924         assertFalse( attr1.isHR() );
1925         assertEquals( 3, attr1.size() );
1926         assertEquals( "userpassword", attr1.getId() );
1927         assertEquals( "userPassword", attr1.getUpId() );
1928         assertEquals( atPwd, attr1.getAttributeType() );
1929         assertTrue( attr1.contains( BYTES1, BYTES2 ) );
1930         assertTrue( attr1.contains( NULL_BINARY_VALUE ) );
1931 
1932         ServerAttribute attr2 = new DefaultServerAttribute( atPwd, STR_VALUE1, BIN_VALUE2, NULL_BINARY_VALUE );
1933         
1934         assertFalse( attr2.isHR() );
1935         assertEquals( 2, attr2.size() );
1936         assertEquals( "userpassword", attr2.getId() );
1937         assertEquals( "userPassword", attr2.getUpId() );
1938         assertEquals( atPwd, attr2.getAttributeType() );
1939         assertTrue( attr2.contains( BYTES2 ) );
1940         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
1941     }
1942 
1943 
1944     /**
1945      * Test method DefaultServerAttribute( String, AttributeType, byte[]... )
1946      */
1947     @Test
1948     public void testDefaultServerAttributeStringAttributeTypeByteArray()
1949     {
1950         ServerAttribute attr1 = new DefaultServerAttribute( "userPassword", atPwd, BYTES1, BYTES2, (byte[])null );
1951         
1952         assertFalse( attr1.isHR() );
1953         assertEquals( 3, attr1.size() );
1954         assertEquals( "userpassword", attr1.getId() );
1955         assertEquals( "userPassword", attr1.getUpId() );
1956         assertEquals( atPwd, attr1.getAttributeType() );
1957         assertTrue( attr1.contains( BYTES1, BYTES2 ) );
1958         assertTrue( attr1.contains( NULL_BINARY_VALUE ) );
1959 
1960         ServerAttribute attr2 = new DefaultServerAttribute( "2.5.4.35", atPwd, STR_VALUE1, BIN_VALUE2, NULL_BINARY_VALUE );
1961         
1962         assertFalse( attr2.isHR() );
1963         assertEquals( 2, attr2.size() );
1964         assertEquals( "2.5.4.35", attr2.getId() );
1965         assertEquals( "2.5.4.35", attr2.getUpId() );
1966         assertEquals( atPwd, attr2.getAttributeType() );
1967         assertTrue( attr2.contains( BYTES2 ) );
1968         assertTrue( attr2.contains( NULL_BINARY_VALUE ) );
1969     }
1970 
1971 
1972     /**
1973      * Test method testClone()
1974      */
1975     @Test
1976     public void testClone()
1977     {
1978         ServerAttribute attr = new DefaultServerAttribute( atCN );
1979         
1980         EntryAttribute clone = attr.clone();
1981         
1982         assertEquals( attr, clone );
1983         attr.setUpId( "CommonName" );
1984         assertEquals( "cn", clone.getId() );
1985         
1986         attr.add( "a", (String)null, "b" );
1987         clone = attr.clone();
1988         assertEquals( attr, clone );
1989         
1990         attr.remove( "a" );
1991         assertNotSame( attr, clone );
1992         
1993         clone = attr.clone();
1994         assertEquals( attr, clone );
1995     }
1996     
1997     
1998     /**
1999      * Test the copy constructor of a ServerAttribute
2000      */
2001     @Test 
2002     public void testCopyConstructorServerAttribute() throws InvalidAttributeValueException
2003     {
2004         EntryAttribute attribute = new DefaultServerAttribute( atCN );
2005         
2006         EntryAttribute copy = new DefaultServerAttribute( atCN, attribute );
2007         
2008         assertEquals( copy, attribute );
2009 
2010         EntryAttribute attribute2 = new DefaultServerAttribute( atCN, "test" );
2011         
2012         EntryAttribute copy2 = new DefaultServerAttribute( atCN, attribute2 );
2013         
2014         assertEquals( copy2, attribute2 );
2015         attribute2.add( "test2" );
2016         assertNotSame( copy2, attribute2 );
2017         assertEquals( "test", copy2.getString() );
2018     }
2019     
2020     
2021     /**
2022      * Test the copy constructor of a ClientAttribute
2023      */
2024     @Test 
2025     public void testCopyConstructorClientAttribute() throws InvalidAttributeValueException
2026     {
2027         EntryAttribute attribute = new DefaultClientAttribute( "commonName" );
2028         attribute.put( "test" );
2029         
2030         ServerAttribute copy = new DefaultServerAttribute( atCN, attribute );
2031 
2032         assertEquals( atCN, copy.getAttributeType() );
2033         assertEquals( "test", copy.getString() );
2034         assertTrue( copy.isHR() );
2035         
2036         attribute.add( "test2" );
2037         assertFalse( copy.contains( "test2" ) );
2038     }
2039     
2040     
2041     /**
2042      * Test the conversion method 
2043      */
2044     @Test 
2045     public void testToClientAttribute()
2046     {
2047         ServerAttribute attribute = new DefaultServerAttribute( atCN, "test", "test2" );
2048         
2049         EntryAttribute clientAttribute = attribute.toClientAttribute();
2050         
2051         assertTrue( clientAttribute instanceof ClientAttribute );
2052         assertFalse( clientAttribute instanceof ServerAttribute );
2053         
2054         assertTrue( clientAttribute.contains( "test", "test2" ) );
2055         assertEquals( "cn", clientAttribute.getId() );
2056         
2057         attribute.remove( "test", "test2" );
2058         assertTrue( clientAttribute.contains( "test", "test2" ) );
2059     }
2060     
2061     
2062     /**
2063      * Test the serialization of a complete server attribute
2064      */
2065     @Test
2066     public void testSerializeCompleteAttribute() throws NamingException, IOException, ClassNotFoundException
2067     {
2068         DefaultServerAttribute dsa = new DefaultServerAttribute( atCN );
2069         dsa.setHR( true );
2070         dsa.setUpId( "CommonName" );
2071         dsa.add( "test1", "test2" );
2072 
2073         DefaultServerAttribute dsaSer = deserializeValue( serializeValue( dsa ), atCN );
2074         assertEquals( dsa.toString(), dsaSer.toString() );
2075         assertEquals( "commonname", dsaSer.getId() );
2076         assertEquals( "CommonName", dsaSer.getUpId() );
2077         assertEquals( "test1", dsaSer.getString() );
2078         assertTrue( dsaSer.contains( "test2", "test1" ) );
2079         assertTrue( dsaSer.isHR() );
2080         assertTrue( dsaSer.isValid() );
2081     }
2082     
2083     
2084     /**
2085      * Test the serialization of a server attribute with no value
2086      */
2087     @Test
2088     public void testSerializeAttributeWithNoValue() throws NamingException, IOException, ClassNotFoundException
2089     {
2090         DefaultServerAttribute dsa = new DefaultServerAttribute( atCN );
2091         dsa.setHR( true );
2092         dsa.setId( "cn" );
2093 
2094         DefaultServerAttribute dsaSer = deserializeValue( serializeValue( dsa ), atCN );
2095         assertEquals( dsa.toString(), dsaSer.toString() );
2096         assertEquals( "cn", dsaSer.getId() );
2097         assertEquals( "cn", dsaSer.getUpId() );
2098         assertEquals( 0, dsaSer.size() );
2099         assertTrue( dsaSer.isHR() );
2100         assertTrue( dsaSer.isValid() );
2101     }
2102     
2103     
2104     /**
2105      * Test the serialization of a server attribute with a null value
2106      */
2107     @Test
2108     public void testSerializeAttributeNullValue() throws NamingException, IOException, ClassNotFoundException
2109     {
2110         DefaultServerAttribute dsa = new DefaultServerAttribute( atCN );
2111         dsa.setHR( true );
2112         dsa.setUpId( "CommonName" );
2113         dsa.add( (String)null );
2114 
2115         DefaultServerAttribute dsaSer = deserializeValue( serializeValue( dsa ), atCN );
2116         assertEquals( dsa.toString(), dsaSer.toString() );
2117         assertEquals( "commonname", dsaSer.getId() );
2118         assertEquals( "CommonName", dsaSer.getUpId() );
2119         assertNull( dsaSer.getString() );
2120         assertEquals( 1, dsaSer.size() );
2121         assertTrue( dsaSer.contains( (String)null ) );
2122         assertTrue( dsaSer.isHR() );
2123         assertFalse( dsaSer.isValid() );
2124     }
2125     
2126     
2127     /**
2128      * Test the serialization of a server attribute with a binary value
2129      */
2130     @Test
2131     public void testSerializeAttributeBinaryValue() throws NamingException, IOException, ClassNotFoundException
2132     {
2133         DefaultServerAttribute dsa = new DefaultServerAttribute( atPwd );
2134         dsa.setHR( false );
2135         byte[] password = StringTools.getBytesUtf8( "secret" );
2136         dsa.add( password );
2137 
2138         DefaultServerAttribute dsaSer = deserializeValue( serializeValue( dsa ), atPwd );
2139         assertEquals( dsa.toString(), dsaSer.toString() );
2140         assertEquals( "userpassword", dsaSer.getId() );
2141         assertEquals( "userPassword", dsaSer.getUpId() );
2142         assertTrue( Arrays.equals( dsa.getBytes(), dsaSer.getBytes() ) );
2143         assertEquals( 1, dsaSer.size() );
2144         assertTrue( dsaSer.contains( password ) );
2145         assertFalse( dsaSer.isHR() );
2146         assertTrue( dsaSer.isValid() );
2147     }
2148 }