1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.entry;
21
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashSet;
32 import java.util.List;
33
34 import javax.naming.NamingException;
35 import javax.naming.directory.InvalidAttributeValueException;
36
37 import org.apache.directory.shared.ldap.entry.Value;
38 import org.apache.directory.shared.ldap.schema.AttributeType;
39 import org.apache.directory.shared.ldap.schema.DeepTrimToLowerNormalizer;
40 import org.apache.directory.shared.ldap.schema.NoOpNormalizer;
41 import org.apache.directory.shared.ldap.schema.syntax.AcceptAllSyntaxChecker;
42 import org.apache.directory.shared.ldap.schema.syntax.SyntaxChecker;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertNotSame;
47 import static org.junit.Assert.assertNull;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50
51 import org.junit.Before;
52 import org.junit.Test;
53
54 import jdbm.helper.StringComparator;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class ServerStringValueTest
71 {
72 private TestServerEntryUtils.S s;
73 private TestServerEntryUtils.AT at;
74 private TestServerEntryUtils.MR mr;
75
76
77
78
79
80
81 @Before public void initAT()
82 {
83 s = new TestServerEntryUtils.S( "1.1.1.1", false );
84 s.setSyntaxChecker( new AcceptAllSyntaxChecker( "1.1.1.1" ) );
85 mr = new TestServerEntryUtils.MR( "1.1.2.1" );
86 mr.syntax = s;
87 mr.comparator = new StringComparator();
88 mr.normalizer = new DeepTrimToLowerNormalizer();
89 at = new TestServerEntryUtils.AT( "1.1.3.1" );
90 at.setEquality( mr );
91 at.setOrdering( mr );
92 at.setSubstr( mr );
93 at.setSyntax( s );
94 }
95
96
97
98
99
100 private ByteArrayOutputStream serializeValue( ServerStringValue value ) throws IOException
101 {
102 ObjectOutputStream oOut = null;
103 ByteArrayOutputStream out = new ByteArrayOutputStream();
104
105 try
106 {
107 oOut = new ObjectOutputStream( out );
108 value.serialize( oOut );
109 }
110 catch ( IOException ioe )
111 {
112 throw ioe;
113 }
114 finally
115 {
116 try
117 {
118 if ( oOut != null )
119 {
120 oOut.flush();
121 oOut.close();
122 }
123 }
124 catch ( IOException ioe )
125 {
126 throw ioe;
127 }
128 }
129
130 return out;
131 }
132
133
134
135
136
137 private ServerStringValue deserializeValue( ByteArrayOutputStream out, AttributeType at ) throws IOException, ClassNotFoundException
138 {
139 ObjectInputStream oIn = null;
140 ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
141
142 try
143 {
144 oIn = new ObjectInputStream( in );
145
146 ServerStringValue value = new ServerStringValue( at );
147 value.deserialize( oIn );
148
149 return value;
150 }
151 catch ( IOException ioe )
152 {
153 throw ioe;
154 }
155 finally
156 {
157 try
158 {
159 if ( oIn != null )
160 {
161 oIn.close();
162 }
163 }
164 catch ( IOException ioe )
165 {
166 throw ioe;
167 }
168 }
169 }
170
171
172
173
174
175 @Test
176 public void testServerStringValueNullValue()
177 {
178 AttributeType attribute = TestServerEntryUtils.getIA5StringAttributeType();
179
180 ServerStringValue value = new ServerStringValue( attribute, null );
181
182 assertNull( value.get() );
183 assertTrue( value.isNull() );
184 }
185
186
187
188
189
190 @Test public void testGetNormalizedValue()
191 {
192 AttributeType attribute = TestServerEntryUtils.getIA5StringAttributeType();
193
194 ServerStringValue value = new ServerStringValue( attribute, null );
195
196 assertFalse( value.isNormalized() );
197 assertNull( value.getNormalizedValue() );
198 assertTrue( value.isNormalized() );
199
200 value.set( "" );
201 assertFalse( value.isNormalized() );
202 assertEquals( "", value.getNormalizedValue() );
203 assertTrue( value.isNormalized() );
204
205 value.set( "TEST" );
206 assertFalse( value.isNormalized() );
207 assertEquals( "test", value.getNormalizedValue() );
208 assertTrue( value.isNormalized() );
209 }
210
211
212
213
214
215
216
217 @Test public void testIsValid()
218 {
219 AttributeType attribute = TestServerEntryUtils.getIA5StringAttributeType();
220
221 ServerStringValue value = new ServerStringValue( attribute, null );
222 assertTrue( value.isValid() );
223
224 value.set( "" );
225 assertTrue( value.isValid() );
226
227 value.set( "TEST" );
228 assertTrue( value.isValid() );
229
230 value.set( "testlong" );
231 assertFalse( value.isValid() );
232 }
233
234
235
236
237
238 @Test
239 public void testNormalize() throws NamingException
240 {
241 AttributeType attribute = TestServerEntryUtils.getIA5StringAttributeType();
242 ServerStringValue ssv = new ServerStringValue( attribute );
243
244 ssv.normalize();
245 assertEquals( null, ssv.getNormalizedValue() );
246
247 ssv.set( "" );
248 ssv.normalize();
249 assertEquals( "", ssv.getNormalizedValue() );
250
251 ssv.set( " This is a TEST " );
252 ssv.normalize();
253 assertEquals( "this is a test", ssv.getNormalizedValue() );
254 }
255
256
257
258
259
260 @Test
261 public void testInstanceOf() throws NamingException
262 {
263 AttributeType attribute = TestServerEntryUtils.getIA5StringAttributeType();
264 ServerStringValue ssv = new ServerStringValue( attribute );
265
266 assertTrue( ssv.instanceOf( attribute ) );
267
268 attribute = TestServerEntryUtils.getBytesAttributeType();
269
270 assertFalse( ssv.instanceOf( attribute ) );
271 }
272
273
274
275
276
277 @Test
278 public void testgetAttributeType()
279 {
280 AttributeType attribute = TestServerEntryUtils.getIA5StringAttributeType();
281 ServerStringValue ssv = new ServerStringValue( attribute );
282
283 assertEquals( attribute, ssv.getAttributeType() );
284 }
285
286
287
288
289
290 @Test public void testEquals()
291 {
292 AttributeType at1 = TestServerEntryUtils.getIA5StringAttributeType();
293 AttributeType at2 = TestServerEntryUtils.getBytesAttributeType();
294
295 ServerStringValue value1 = new ServerStringValue( at1, "test" );
296 ServerStringValue value2 = new ServerStringValue( at1, "test" );
297 ServerStringValue value3 = new ServerStringValue( at1, "TEST" );
298 ServerStringValue value4 = new ServerStringValue( at1, "tes" );
299 ServerStringValue value5 = new ServerStringValue( at1, null );
300 ServerBinaryValue valueBytes = new ServerBinaryValue( at2, new byte[]{0x01} );
301 ServerStringValue valueString = new ServerStringValue( at, "test" );
302
303 assertTrue( value1.equals( value1 ) );
304 assertTrue( value1.equals( value2 ) );
305 assertTrue( value1.equals( value3 ) );
306 assertFalse( value1.equals( value4 ) );
307 assertFalse( value1.equals( value5 ) );
308 assertFalse( value1.equals( "test" ) );
309 assertFalse( value1.equals( null ) );
310
311 assertFalse( value1.equals( valueString ) );
312 assertFalse( value1.equals( valueBytes ) );
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327 @Test public void testBadConstructor()
328 {
329 try
330 {
331 new ServerStringValue( null );
332 fail();
333 }
334 catch ( IllegalArgumentException iae )
335 {
336
337 }
338
339
340 AttributeType attribute = new TestServerEntryUtils.AT( "1.1.3.1" );
341
342 try
343 {
344 new ServerStringValue( attribute );
345 fail();
346 }
347 catch ( IllegalArgumentException iae )
348 {
349
350 }
351 }
352
353
354
355
356
357
358 @Test public void testHashCode()
359 {
360 AttributeType at1 = TestServerEntryUtils.getCaseIgnoringAttributeNoNumbersType();
361 ServerStringValue v0 = new ServerStringValue( at1, "Alex" );
362 ServerStringValue v1 = new ServerStringValue( at1, "ALEX" );
363 ServerStringValue v2 = new ServerStringValue( at1, "alex" );
364
365 assertEquals( v0.hashCode(), v1.hashCode() );
366 assertEquals( v0.hashCode(), v2.hashCode() );
367 assertEquals( v1.hashCode(), v2.hashCode() );
368
369 assertEquals( v0, v1 );
370 assertEquals( v0, v2 );
371 assertEquals( v1, v2 );
372
373 assertTrue( v0.isValid() );
374 assertTrue( v1.isValid() );
375 assertTrue( v2.isValid() );
376
377 ServerStringValue v3 = new ServerStringValue( at1, "Timber" );
378
379 assertTrue( v3.isValid() );
380 assertNotSame( v0.hashCode(), v3.hashCode() );
381
382 ServerStringValue v4 = new ServerStringValue( at, "Alex" );
383
384 assertNotSame( v0.hashCode(), v4.hashCode() );
385 }
386
387
388
389
390
391 @Test
392 public void testCompareTo()
393 {
394 AttributeType at1 = TestServerEntryUtils.getCaseIgnoringAttributeNoNumbersType();
395 ServerStringValue v0 = new ServerStringValue( at1, "Alex" );
396 ServerStringValue v1 = new ServerStringValue( at1, "ALEX" );
397
398 assertEquals( 0, v0.compareTo( v1 ) );
399 assertEquals( 0, v1.compareTo( v0 ) );
400
401 ServerStringValue v2 = new ServerStringValue( at1, null );
402
403 assertEquals( 1, v0.compareTo( v2 ) );
404 assertEquals( -1, v2.compareTo( v0 ) );
405 }
406
407
408
409
410
411 @Test
412 public void testClone() throws NamingException
413 {
414 AttributeType at1 = TestServerEntryUtils.getCaseIgnoringAttributeNoNumbersType();
415 ServerStringValue ssv = new ServerStringValue( at1, "Test" );
416
417 ServerStringValue ssv1 = ssv.clone();
418
419 assertEquals( ssv, ssv1 );
420
421 ssv.set( "" );
422
423 assertNotSame( ssv, ssv1 );
424 assertEquals( "", ssv.get() );
425
426 ssv.set( " This is a TEST " );
427 ssv1 = ssv.clone();
428
429 assertEquals( ssv, ssv1 );
430
431 ssv.normalize();
432
433 assertEquals( ssv, ssv1 );
434 }
435
436
437
438
439
440
441
442
443
444 @Test public void testConstrainedString()
445 {
446 s.setSyntaxChecker( new SyntaxChecker() {
447 public String getSyntaxOid() { return "1.1.1.1"; }
448 public boolean isValidSyntax( Object value )
449 {
450 if ( value instanceof String )
451 {
452 String strval = ( String ) value;
453 return strval.equals( "HIGH" ) || strval.equals( "LOW" ) || strval.equals( "MEDIUM" );
454 }
455 return false;
456 }
457 public void assertSyntax( Object value ) throws NamingException
458 { if ( ! isValidSyntax( value ) ) {throw new InvalidAttributeValueException(); }}
459 });
460
461 mr.syntax = s;
462 mr.comparator = new Comparator<String>()
463 {
464 public int compare( String o1, String o2 )
465 {
466 if ( o1 == null )
467 {
468 if ( o2 == null )
469 {
470 return 0;
471 }
472 else
473 {
474 return -1;
475 }
476 }
477 else if ( o2 == null )
478 {
479 return 1;
480 }
481
482 int i1 = getValue( o1 );
483 int i2 = getValue( o2 );
484
485 if ( i1 == i2 ) { return 0; }
486 if ( i1 > i2 ) { return 1; }
487 if ( i1 < i2 ) { return -1; }
488
489 throw new IllegalStateException( "should not get here at all" );
490 }
491
492 public int getValue( String val )
493 {
494 if ( val.equals( "LOW" ) ) { return 0; }
495 if ( val.equals( "MEDIUM" ) ) { return 1; }
496 if ( val.equals( "HIGH" ) ) { return 2; }
497 throw new IllegalArgumentException( "Not a valid value" );
498 }
499 };
500 mr.normalizer = new NoOpNormalizer();
501 at.setEquality( mr );
502 at.setSyntax( s );
503
504
505 ServerStringValue value = new ServerStringValue( at, "HIGH" );
506 assertEquals( value.get(), value.get() );
507 assertTrue( value.isValid() );
508 value = new ServerStringValue( at, "high" );
509 assertFalse( value.isValid() );
510
511
512 ServerStringValue v0 = new ServerStringValue( at, "LOW" );
513 assertTrue( v0.isValid() );
514 ServerStringValue v1 = new ServerStringValue( at, "LOW" );
515 assertTrue( v1.isValid() );
516 ServerStringValue v2 = new ServerStringValue( at, "MEDIUM" );
517 assertTrue( v2.isValid() );
518 ServerStringValue v3 = new ServerStringValue( at, "HIGH" );
519 assertTrue( v3.isValid() );
520 ServerStringValue v4 = new ServerStringValue( at );
521 assertFalse( v4.isValid() );
522 ServerStringValue v5 = new ServerStringValue( at );
523 assertFalse( v5.isValid() );
524
525
526 assertTrue( v0.equals( v1 ) );
527 assertTrue( v1.equals( v0 ) );
528 assertEquals( 0, v0.compareTo( v1 ) );
529
530 assertTrue( v4.equals( v5 ) );
531 assertTrue( v5.equals( v4 ) );
532 assertEquals( 0, v4.compareTo( v5 ) );
533
534 assertFalse( v2.equals( v3 ) );
535 assertFalse( v3.equals( v2 ) );
536 assertTrue( v2.compareTo( v3 ) < 0 );
537 assertTrue( v3.compareTo( v2 ) > 0 );
538
539
540 HashSet<ServerStringValue> set = new HashSet<ServerStringValue>();
541 set.add( v0 );
542 set.add( v2 );
543 set.add( v3 );
544 set.add( v4 );
545
546
547 assertTrue( "since v1.equals( v0 ) and v0 was added then this should be true", set.contains( v1 ) );
548 assertTrue( "since v4.equals( v5 ) and v4 was added then this should be true", set.contains( v5 ) );
549
550
551 List<Value<String>> list = new ArrayList<Value<String>>();
552 list.add( v1 );
553 list.add( v3 );
554 list.add( v5 );
555 list.add( v0 );
556 list.add( v2 );
557 list.add( v4 );
558
559 Collections.sort( list );
560
561
562 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v4 ) );
563 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v5 ) );
564 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v4 ) );
565 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v5 ) );
566
567
568 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v0 ) );
569 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v1 ) );
570 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v0 ) );
571 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v1 ) );
572
573
574 assertTrue( "since v2 \"MEDIUM\" should be at index 4", list.get( 4 ).equals( v2 ) );
575 assertTrue( "since v3 \"HIGH\" should be at index 5", list.get( 5 ).equals( v3 ) );
576
577 assertEquals( 6, list.size() );
578 }
579
580
581
582
583
584
585
586
587
588
589 @Test public void testAcceptAllNoNormalization()
590 {
591
592 ServerStringValue value = new ServerStringValue( at, "hello" );
593 assertEquals( value.get(), value.get() );
594 assertTrue( value.isValid() );
595
596
597 ServerStringValue v0 = new ServerStringValue( at, "hello" );
598 ServerStringValue v1 = new ServerStringValue( at, "hello" );
599 ServerStringValue v2 = new ServerStringValue( at, "next0" );
600 ServerStringValue v3 = new ServerStringValue( at, "next1" );
601 ServerStringValue v4 = new ServerStringValue( at );
602 ServerStringValue v5 = new ServerStringValue( at );
603
604
605 assertTrue( v0.equals( v1 ) );
606 assertTrue( v1.equals( v0 ) );
607 assertTrue( v4.equals( v5 ) );
608 assertTrue( v5.equals( v4 ) );
609 assertFalse( v2.equals( v3 ) );
610 assertFalse( v3.equals( v2 ) );
611
612
613 HashSet<ServerStringValue> set = new HashSet<ServerStringValue>();
614 set.add( v0 );
615 set.add( v2 );
616 set.add( v3 );
617 set.add( v4 );
618
619
620 assertTrue( "since v1.equals( v0 ) and v0 was added then this should be true", set.contains( v1 ) );
621 assertTrue( "since v4.equals( v5 ) and v4 was added then this should be true", set.contains( v5 ) );
622
623
624 ArrayList<ServerStringValue> list = new ArrayList<ServerStringValue>();
625 list.add( v1 );
626 list.add( v3 );
627 list.add( v5 );
628 list.add( v0 );
629 list.add( v2 );
630 list.add( v4 );
631
632 Comparator<ServerStringValue> c = new Comparator<ServerStringValue>()
633 {
634 public int compare( ServerStringValue o1, ServerStringValue o2 )
635 {
636 String n1 = null;
637 String n2 = null;
638
639 if ( o1 != null )
640 {
641 n1 = o1.get();
642 }
643
644 if ( o2 != null )
645 {
646 n2 = o2.get();
647 }
648
649 if ( n1 == null )
650 {
651 return ( n2 == null ) ? 0 : -1;
652 }
653 else if ( n2 == null )
654 {
655 return 1;
656 }
657
658 try
659 {
660 return mr.getComparator().compare( n1, n2 );
661 }
662 catch ( NamingException ne )
663 {
664 throw new IllegalStateException( "Normalization and comparison should succeed!", ne );
665 }
666 }
667 };
668
669 Collections.sort( list, c );
670
671 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v4 ) );
672 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v5 ) );
673 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v4 ) );
674 assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v5 ) );
675
676 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v0 ) );
677 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v1 ) );
678 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v0 ) );
679 assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v1 ) );
680
681 assertTrue( "since v2 \"next0\" should be at index 4", list.get( 4 ).equals( v2 ) );
682 assertTrue( "since v3 \"next1\" should be at index 5", list.get( 5 ).equals( v3 ) );
683
684 assertEquals( 6, list.size() );
685 }
686
687
688
689
690
691 @Test public void testNormalizedStringValueSerialization() throws NamingException, IOException, ClassNotFoundException
692 {
693
694 ServerStringValue ssv = new ServerStringValue( at, " Test Test " );
695
696 ssv.normalize();
697 String normalized = ssv.getNormalizedValue();
698
699 assertEquals( "test test", normalized );
700 assertEquals( " Test Test ", ssv.get() );
701
702 ServerStringValue ssvSer = deserializeValue( serializeValue( ssv ), at );
703
704 assertEquals( ssv, ssvSer );
705 }
706
707
708
709
710
711 @Test public void testNoNormalizedStringValueSerialization() throws NamingException, IOException, ClassNotFoundException
712 {
713
714 ServerStringValue ssv = new ServerStringValue( at, "test" );
715
716 ssv.normalize();
717 String normalized = ssv.getNormalizedValue();
718
719 assertEquals( "test", normalized );
720 assertEquals( "test", ssv.get() );
721
722 ServerStringValue ssvSer = deserializeValue( serializeValue( ssv ), at );
723
724 assertEquals( ssv, ssvSer );
725 }
726
727
728
729
730
731 @Test public void testNullStringValueSerialization() throws NamingException, IOException, ClassNotFoundException
732 {
733
734 ServerStringValue ssv = new ServerStringValue( at );
735
736 ssv.normalize();
737 String normalized = ssv.getNormalizedValue();
738
739 assertEquals( null, normalized );
740 assertEquals( null, ssv.get() );
741
742 ServerStringValue ssvSer = deserializeValue( serializeValue( ssv ), at );
743
744 assertEquals( ssv, ssvSer );
745 }
746
747
748
749
750
751 @Test public void testEmptyStringValueSerialization() throws NamingException, IOException, ClassNotFoundException
752 {
753
754 ServerStringValue ssv = new ServerStringValue( at, "" );
755
756 ssv.normalize();
757 String normalized = ssv.getNormalizedValue();
758
759 assertEquals( "", normalized );
760 assertEquals( "", ssv.get() );
761
762 ServerStringValue ssvSer = deserializeValue( serializeValue( ssv ), at );
763
764 assertEquals( ssv, ssvSer );
765 }
766
767
768
769
770
771 @Test public void testStringValueEmptyNormalizedSerialization() throws NamingException, IOException, ClassNotFoundException
772 {
773
774 ServerStringValue ssv = new ServerStringValue( at, " " );
775
776
777 assertEquals( " ", ssv.get() );
778
779 ServerStringValue ssvSer = deserializeValue( serializeValue( ssv ), at );
780
781 assertEquals( ssv, ssvSer );
782 }
783 }