mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
08.23.2014 4ca2a9a3650344ed74e55c1bdb646dc88cf91965
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2012 ForgeRock AS.
 */
 
package org.forgerock.opendj.ldap;
 
import static org.fest.assertions.Assertions.assertThat;
import static org.forgerock.opendj.ldap.Attributes.emptyAttribute;
import static org.forgerock.opendj.ldap.Attributes.singletonAttribute;
 
import java.util.LinkedList;
import java.util.List;
 
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.SchemaBuilder;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/**
 * Test {@code Entry}.
 */
@SuppressWarnings({ "javadoc", "resource" })
public final class EntryTestCase extends SdkTestCase {
 
    private static interface EntryFactory {
        Entry newEntry(String... ldifLines) throws Exception;
    }
 
    private static final class LinkedHashMapEntryFactory implements EntryFactory {
        @Override
        public Entry newEntry(final String... ldifLines) throws Exception {
            final LDIFEntryReader reader = new LDIFEntryReader(ldifLines).setSchema(SCHEMA);
            final Entry entry = reader.readEntry();
            assertThat(reader.hasNext()).isFalse();
            return new LinkedHashMapEntry(entry);
        }
    }
 
    private static final class TreeMapEntryFactory implements EntryFactory {
        @Override
        public Entry newEntry(final String... ldifLines) throws Exception {
            final LDIFEntryReader reader = new LDIFEntryReader(ldifLines).setSchema(SCHEMA);
            final Entry entry = reader.readEntry();
            assertThat(reader.hasNext()).isFalse();
            return new TreeMapEntry(entry);
        }
    }
 
    private static final AttributeDescription AD_CN;
    private static final AttributeDescription AD_CUSTOM1;
    private static final AttributeDescription AD_CUSTOM2;
    private static final AttributeDescription AD_NAME;
 
    private static final AttributeDescription AD_SN;
 
    private static final Schema SCHEMA;
 
    static {
        final SchemaBuilder builder = new SchemaBuilder(Schema.getCoreSchema());
        builder.addAttributeType("( 9.9.9.1 NAME 'custom1' SUP name )", false);
        builder.addAttributeType("( 9.9.9.2 NAME 'custom2' SUP name )", false);
        SCHEMA = builder.toSchema();
        AD_CUSTOM1 = AttributeDescription.valueOf("custom1", SCHEMA);
        AD_CUSTOM2 = AttributeDescription.valueOf("custom2", SCHEMA);
        AD_CN = AttributeDescription.valueOf("cn");
        AD_SN = AttributeDescription.valueOf("sn");
        AD_NAME = AttributeDescription.valueOf("name");
    }
 
    @DataProvider(name = "EntryFactory")
    Object[][] entryFactory() {
        // Value, type, options, containsOptions("foo")
        return new Object[][] { { new TreeMapEntryFactory() }, { new LinkedHashMapEntryFactory() } };
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeAttribute(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute(new LinkedAttribute("sn", "sn"))).isTrue();
        assertThat(entry.getAttribute(AD_SN)).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeAttributeCollection(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> duplicateValues = new LinkedList<ByteString>();
        assertThat(entry.addAttribute(new LinkedAttribute("sn", "sn"), duplicateValues)).isTrue();
        assertThat(entry.getAttribute(AD_SN)).hasSize(1);
        assertThat(duplicateValues).hasSize(0);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeAttributeCollectionValueMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> duplicateValues = new LinkedList<ByteString>();
        assertThat(entry.addAttribute(new LinkedAttribute("cn", "newcn"), duplicateValues))
                .isTrue();
        assertThat(entry.getAttribute(AD_CN)).hasSize(2);
        assertThat(duplicateValues).hasSize(0);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeAttributeCollectionValuePresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> duplicateValues = new LinkedList<ByteString>();
        assertThat(entry.addAttribute(new LinkedAttribute("cn", "test"), duplicateValues))
                .isFalse();
        assertThat(entry.getAttribute(AD_CN)).hasSize(1);
        assertThat(duplicateValues).hasSize(1);
        assertThat(duplicateValues).contains(ByteString.valueOf("test"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeAttributeValueMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute(new LinkedAttribute("cn", "newcn"))).isTrue();
        assertThat(entry.getAttribute(AD_CN)).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeAttributeValuePresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute(new LinkedAttribute("cn", "test"))).isFalse();
        assertThat(entry.getAttribute(AD_CN)).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeString(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute("sn", "sn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_SN)).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeStringCustom(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute("custom2", "custom2")).isSameAs(entry);
        // This is expected to be null since the type was decoded using the
        // default schema and a temporary oid was allocated.
        assertThat(entry.getAttribute(AD_CUSTOM2)).isNull();
        assertThat(entry.getAttribute("custom2")).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeStringCustomValueMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute("custom1", "xxxx")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CUSTOM1)).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeStringCustomValuePresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute("custom1", "custom1")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CUSTOM1)).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeStringValueMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute("cn", "newcn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testAddAttributeStringValuePresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.addAttribute("cn", "test")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testClearAttributes(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.clearAttributes()).isSameAs(entry);
        assertThat(entry.getAttributeCount()).isEqualTo(0);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeCustomMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(emptyAttribute(AD_CUSTOM2), missingValues)).isFalse();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeCustomPresent1(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(emptyAttribute(AD_CUSTOM1), missingValues)).isTrue();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeCustomPresent2(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(emptyAttribute("custom1"), missingValues)).isTrue();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeCustomValueMissing1(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(
                entry.containsAttribute(singletonAttribute(AD_CUSTOM2, "missing"), missingValues))
                .isFalse();
        assertThat(missingValues).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(emptyAttribute(AD_SN), missingValues)).isFalse();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributePresent1(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(emptyAttribute(AD_CN), missingValues)).isTrue();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributePresent2(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(emptyAttribute("cn"), missingValues)).isTrue();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeValueCustomMissing2(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(
                entry.containsAttribute(singletonAttribute(AD_CUSTOM1, "missing"), missingValues))
                .isFalse();
        assertThat(missingValues).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeValueCustomPresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(
                entry.containsAttribute(singletonAttribute(AD_CUSTOM1, "custom1"), missingValues))
                .isTrue();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeValueMissing1(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(singletonAttribute(AD_SN, "missing"), missingValues))
                .isFalse();
        assertThat(missingValues).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeValueMissing2(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(singletonAttribute(AD_CN, "missing"), missingValues))
                .isFalse();
        assertThat(missingValues).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeAttributeValuePresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.containsAttribute(singletonAttribute(AD_CN, "test"), missingValues))
                .isTrue();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringCustomMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("custom2")).isFalse();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringCustomPresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("custom1")).isTrue();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("sn")).isFalse();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringPresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("cn")).isTrue();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringValueCustom(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("custom1", "custom1")).isTrue();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringValueMissing1(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("cn", "missing")).isFalse();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringValueMissing2(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("sn", "missing")).isFalse();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testContainsAttributeStringValuePresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.containsAttribute("cn", "test")).isTrue();
    }
 
    @Test
    public void testEqualsHashCodeDifferentContentDifferentTypes1() throws Exception {
        final Entry e1 = createTestEntry(new TreeMapEntryFactory());
        // Extra attributes.
        final Entry e2 = createTestEntry(new LinkedHashMapEntryFactory()).addAttribute("sn", "sn");
        assertThat(e1).isNotEqualTo(e2);
        assertThat(e2).isNotEqualTo(e1);
        assertThat(e1.hashCode()).isNotEqualTo(e2.hashCode());
    }
 
    @Test
    public void testEqualsHashCodeDifferentContentDifferentTypes2() throws Exception {
        final Entry e1 = createTestEntry(new TreeMapEntryFactory());
        // Same attributes, extra values.
        final Entry e2 =
                createTestEntry(new LinkedHashMapEntryFactory()).addAttribute("cn", "newcn");
        assertThat(e1).isNotEqualTo(e2);
        assertThat(e2).isNotEqualTo(e1);
        assertThat(e1.hashCode()).isNotEqualTo(e2.hashCode());
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testEqualsHashCodeDifferentContentSameTypes1(final EntryFactory factory)
            throws Exception {
        final Entry e1 = createTestEntry(factory);
        // Extra attributes.
        final Entry e2 = createTestEntry(factory).addAttribute("sn", "sn");
        assertThat(e1).isNotEqualTo(e2);
        assertThat(e2).isNotEqualTo(e1);
        assertThat(e1.hashCode()).isNotEqualTo(e2.hashCode());
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testEqualsHashCodeDifferentContentSameTypes2(final EntryFactory factory)
            throws Exception {
        final Entry e1 = createTestEntry(factory);
        // Same attributes, extra values.
        final Entry e2 = createTestEntry(factory).addAttribute("cn", "newcn");
        assertThat(e1).isNotEqualTo(e2);
        assertThat(e2).isNotEqualTo(e1);
        assertThat(e1.hashCode()).isNotEqualTo(e2.hashCode());
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testEqualsHashCodeDifferentDN(final EntryFactory factory) throws Exception {
        final Entry e1 = createTestEntry(factory);
        final Entry e2 = createTestEntry(factory).setName("cn=foobar");
        assertThat(e1).isNotEqualTo(e2);
        assertThat(e1.hashCode()).isNotEqualTo(e2.hashCode());
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testEqualsHashCodeMutates(final EntryFactory factory) throws Exception {
        final Entry e = createTestEntry(factory);
        final int hc1 = e.hashCode();
        e.addAttribute("sn", "sn");
        final int hc2 = e.hashCode();
        assertThat(hc1).isNotEqualTo(hc2);
    }
 
    @Test
    public void testEqualsHashCodeSameContentDifferentTypes() throws Exception {
        final Entry e1 = createTestEntry(new TreeMapEntryFactory());
        final Entry e2 = createTestEntry(new LinkedHashMapEntryFactory());
        assertThat(e1).isEqualTo(e2);
        assertThat(e2).isEqualTo(e1);
        assertThat(e1.hashCode()).isEqualTo(e2.hashCode());
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testEqualsHashCodeSameContentSameTypes(final EntryFactory factory) throws Exception {
        final Entry e1 = createTestEntry(factory);
        final Entry e2 = createTestEntry(factory);
        assertThat(e1).isEqualTo(e1);
        assertThat(e1).isEqualTo(e2);
        assertThat(e1.hashCode()).isEqualTo(e2.hashCode());
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributes(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes().iterator()).hasSize(3);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesAttributeDescriptionMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes(AD_SN)).hasSize(0);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesAttributeDescriptionPresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes(AD_CN)).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesAttributeDescriptionPresentOptions(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        entry.addAttribute(singletonAttribute(AD_CN.withOption("lang-fr"), "xxxx"));
        assertThat(entry.getAllAttributes(AD_CN)).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesAttributeDescriptionSupertype(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes(AD_NAME)).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesStringCustom(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        entry.addAttribute(singletonAttribute(AD_CUSTOM1.withOption("lang-fr"), "xxxx"));
        assertThat(entry.getAllAttributes("custom1")).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesStringCustomOptions(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        entry.addAttribute("custom2", "value1");
        entry.addAttribute("custom2;lang-fr", "value2");
        assertThat(entry.getAllAttributes("custom2")).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesStringMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes("sn")).hasSize(0);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesStringPresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes("cn")).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAllAttributesStringSupertype(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAllAttributes("name")).hasSize(2);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAttributeAttributeDescriptionMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAttribute(AD_SN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAttributeAttributeDescriptionPresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAttribute(AD_CN)).isNotNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAttributeCount(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAttributeCount()).isEqualTo(3);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAttributeStringCustom(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAttribute("custom1")).isNotNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAttributeStringMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAttribute("sn")).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetAttributeStringPresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.getAttribute("cn")).isNotNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testGetName(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat((Object) entry.getName()).isEqualTo(DN.valueOf("cn=test"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testParseAttributeAttributeDescriptionCustom(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.parseAttribute(AD_CUSTOM1).asString()).isEqualTo("custom1");
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testParseAttributeAttributeDescriptionMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.parseAttribute(AD_SN).asString()).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testParseAttributeAttributeDescriptionPresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.parseAttribute(AD_CN).asString()).isEqualTo("test");
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testParseAttributeStringCustom(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.parseAttribute("custom1").asString()).isEqualTo("custom1");
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testParseAttributeStringMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.parseAttribute("sn").asString()).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testParseAttributeStringPresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.parseAttribute("cn").asString()).isEqualTo("test");
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributeDescriptionMissing(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute(AD_SN)).isFalse();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributeDescriptionPresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute(AD_CN)).isTrue();
        assertThat(entry.getAttribute(AD_CN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributeMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.removeAttribute(emptyAttribute(AD_SN), missingValues)).isFalse();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributePresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.removeAttribute(emptyAttribute(AD_CN), missingValues)).isTrue();
        assertThat(entry.getAttribute(AD_CN)).isNull();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributeValueMissing1(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.removeAttribute(singletonAttribute(AD_CN, "missing"), missingValues))
                .isFalse();
        assertThat(entry.getAttribute(AD_CN)).isNotNull();
        assertThat(missingValues).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributeValueMissing2(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.removeAttribute(singletonAttribute(AD_SN, "missing"), missingValues))
                .isFalse();
        assertThat(missingValues).hasSize(1);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeAttributeValuePresent(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        final List<ByteString> missingValues = new LinkedList<ByteString>();
        assertThat(entry.removeAttribute(singletonAttribute(AD_CN, "test"), missingValues))
                .isTrue();
        assertThat(entry.getAttribute(AD_CN)).isNull();
        assertThat(missingValues).isEmpty();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeStringCustom(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute("custom1")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CUSTOM1)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeStringMissing(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute("sn")).isSameAs(entry);
        assertThat(entry.getAttributeCount()).isEqualTo(3);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeStringPresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute("cn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeStringValueMissing1(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute("cn", "missing")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).isNotNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeStringValueMissing2(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute("sn", "missing")).isSameAs(entry);
        assertThat(entry.getAttributeCount()).isEqualTo(3);
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testRemoveAttributeStringValuePresent(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.removeAttribute("cn", "test")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeAttributeMissingEmpty(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute(emptyAttribute(AD_SN))).isFalse();
        assertThat(entry.getAttribute(AD_SN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeAttributeMissingValue(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute(singletonAttribute(AD_SN, "sn"))).isTrue();
        assertThat(entry.getAttribute(AD_SN)).isEqualTo(singletonAttribute(AD_SN, "sn"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeAttributePresentEmpty(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute(emptyAttribute(AD_CN))).isTrue();
        assertThat(entry.getAttribute(AD_CN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeAttributePresentValue(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute(singletonAttribute(AD_CN, "newcn"))).isTrue();
        assertThat(entry.getAttribute(AD_CN)).isEqualTo(singletonAttribute(AD_CN, "newcn"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringCustomEmpty(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("custom1")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CUSTOM1)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringCustomMissingValue(final EntryFactory factory)
            throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("custom2", "xxxx")).isSameAs(entry);
        // This is expected to be null since the type was decoded using the
        // default schema and a temporary oid was allocated.
        assertThat(entry.getAttribute(AD_CUSTOM2)).isNull();
        assertThat(entry.getAttribute("custom2")).isEqualTo(singletonAttribute("custom2", "xxxx"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringCustomValue(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("custom1", "xxxx")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CUSTOM1))
                .isEqualTo(singletonAttribute(AD_CUSTOM1, "xxxx"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringMissingEmpty(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("sn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_SN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringMissingValue(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("sn", "sn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_SN)).isEqualTo(singletonAttribute(AD_SN, "sn"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringPresentEmpty(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("cn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).isNull();
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testReplaceAttributeStringPresentValue(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.replaceAttribute("cn", "newcn")).isSameAs(entry);
        assertThat(entry.getAttribute(AD_CN)).isEqualTo(singletonAttribute(AD_CN, "newcn"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testSetNameDN(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.setName(DN.valueOf("cn=foobar"))).isSameAs(entry);
        assertThat((Object) entry.getName()).isEqualTo(DN.valueOf("cn=foobar"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testSetNameString(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        assertThat(entry.setName("cn=foobar")).isSameAs(entry);
        assertThat((Object) entry.getName()).isEqualTo(DN.valueOf("cn=foobar"));
    }
 
    @Test(dataProvider = "EntryFactory")
    public void testToString(final EntryFactory factory) throws Exception {
        final Entry entry = createTestEntry(factory);
        // The String representation is unspecified but we should at least
        // expect the DN to be present.
        assertThat(entry.toString()).contains("cn=test");
    }
 
    private Entry createTestEntry(final EntryFactory factory) throws Exception {
        return factory.newEntry("dn: cn=test", "objectClass: top", "objectClass: extensibleObject",
                "cn: test", "custom1: custom1");
    }
}