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.operations.modifydn;
21
22
23 import javax.naming.NameNotFoundException;
24 import javax.naming.NamingEnumeration;
25 import javax.naming.NamingException;
26 import javax.naming.NoPermissionException;
27 import javax.naming.directory.Attribute;
28 import javax.naming.directory.Attributes;
29 import javax.naming.directory.BasicAttribute;
30 import javax.naming.directory.BasicAttributes;
31 import javax.naming.directory.DirContext;
32 import javax.naming.directory.SchemaViolationException;
33 import javax.naming.directory.SearchControls;
34 import javax.naming.directory.SearchResult;
35
36 import org.apache.directory.server.core.integ.Level;
37 import org.apache.directory.server.core.integ.annotations.CleanupLevel;
38 import org.apache.directory.server.integ.SiRunner;
39 import org.apache.directory.server.ldap.LdapService;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43
44 import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContext;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48 import static org.junit.Assert.assertNull;
49 import static org.junit.Assert.fail;
50
51
52
53
54
55
56
57
58
59 @RunWith ( SiRunner.class )
60 @CleanupLevel ( Level.SUITE )
61 public class ModifyRdnIT
62 {
63 private static final String BASE = "ou=system";
64
65 public static LdapService ldapService;
66
67
68
69
70
71 private Attributes getPersonAttributes( String sn, String cn )
72 {
73 Attributes attributes = new BasicAttributes( true );
74 Attribute attribute = new BasicAttribute( "objectClass" );
75 attribute.add( "top" );
76 attribute.add( "person" );
77 attributes.put( attribute );
78 attributes.put( "cn", cn );
79 attributes.put( "sn", sn );
80 attributes.put( "description", cn + " is a person." );
81
82 return attributes;
83 }
84
85
86
87
88
89 private Attributes getOrganizationalUnitAttributes( String ou )
90 {
91 Attributes attributes = new BasicAttributes( true );
92 Attribute attribute = new BasicAttribute( "objectClass" );
93 attribute.add( "top" );
94 attribute.add( "organizationalUnit" );
95 attributes.put( attribute );
96 attributes.put( "ou", ou );
97 attributes.put( "description", ou + " is an organizational unit." );
98
99 return attributes;
100 }
101
102
103
104
105
106 @Test
107 public void testModifyRdnAndDeleteOld() throws Exception
108 {
109 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
110
111
112 String oldCn = "Myra Ellen Amos";
113 String oldRdn = "cn=" + oldCn;
114 Attributes attributes = this.getPersonAttributes( "Amos", oldCn );
115 ctx.createSubcontext( oldRdn, attributes );
116
117
118 String newCn = "Tori Amos";
119 String newRdn = "cn=" + newCn;
120 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
121 ctx.rename( oldRdn, newRdn );
122
123
124 try
125 {
126 ctx.lookup( oldRdn );
127 fail( "Entry must not exist" );
128 }
129 catch ( NameNotFoundException ignored )
130 {
131
132 assertTrue( true );
133 }
134
135
136 DirContext tori = ( DirContext ) ctx.lookup( newRdn );
137 assertNotNull( tori );
138
139
140 Attribute cn = tori.getAttributes( "" ).get( "cn" );
141 assertTrue( cn.contains( newCn ) );
142 assertTrue( !cn.contains( oldCn ) );
143 assertEquals( 1, cn.size() );
144
145
146 ctx.unbind( newRdn );
147 }
148
149
150
151
152
153
154
155 @Test
156 public void testModifyRdnAndDontDeleteOldFalse() throws Exception
157 {
158 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
159
160
161 String oldCn = "Myra Ellen Amos";
162 String oldRdn = "cn=" + oldCn;
163 Attributes attributes = this.getPersonAttributes( "Amos", oldCn );
164 ctx.createSubcontext( oldRdn, attributes );
165
166
167 String newCn = "Tori Amos";
168 String newRdn = "cn=" + newCn;
169 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "False" );
170 ctx.rename( oldRdn, newRdn );
171
172
173 try
174 {
175 ctx.lookup( oldRdn );
176 fail( "Entry must not exist" );
177 }
178 catch ( NameNotFoundException ignored )
179 {
180
181 assertTrue( true );
182 }
183
184
185 DirContext tori = ( DirContext ) ctx.lookup( newRdn );
186 assertNotNull( tori );
187
188
189 Attribute cn = tori.getAttributes( "" ).get( "cn" );
190 assertTrue( cn.contains( newCn ) );
191 assertTrue( cn.contains( oldCn ) );
192 assertEquals( 2, cn.size() );
193
194
195 ctx.unbind( newRdn );
196 }
197
198
199
200
201
202 @Test
203 public void testModifyRdnAndKeepOld() throws Exception
204 {
205 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
206
207
208 String oldCn = "Myra Ellen Amos";
209 String oldRdn = "cn=" + oldCn;
210 Attributes attributes = this.getPersonAttributes( "Amos", oldCn );
211 ctx.createSubcontext( oldRdn, attributes );
212
213
214 String newCn = "Tori Amos";
215 String newRdn = "cn=" + newCn;
216 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
217 ctx.rename( oldRdn, newRdn );
218
219
220 try
221 {
222 ctx.lookup( oldRdn );
223 fail( "Entry must not exist" );
224 }
225 catch ( NameNotFoundException ignored )
226 {
227
228 assertTrue( true );
229 }
230
231
232 DirContext tori = ( DirContext ) ctx.lookup( newRdn );
233 assertNotNull( tori );
234
235
236 Attribute cn = tori.getAttributes( "" ).get( "cn" );
237 assertTrue( cn.contains( newCn ) );
238 assertTrue( cn.contains( oldCn ) );
239 assertEquals( 2, cn.size() );
240
241
242 ctx.unbind( newRdn );
243 }
244
245
246
247
248
249
250 @Test
251 public void testModifyRdnAndDeleteOldVariant() throws Exception
252 {
253 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
254
255
256 String oldCn = "Myra Ellen Amos";
257 String oldRdn = "cn=" + oldCn;
258 Attributes attributes = this.getPersonAttributes( "Amos", oldCn );
259
260
261 String alternateCn = "Myra E. Amos";
262 Attribute cn = attributes.get( "cn" );
263 cn.add( alternateCn );
264 assertEquals( 2, cn.size() );
265
266 ctx.createSubcontext( oldRdn, attributes );
267
268
269 String newCn = "Tori Amos";
270 String newRdn = "cn=" + newCn;
271 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
272 ctx.rename( oldRdn, newRdn );
273
274
275 try
276 {
277 ctx.lookup( oldRdn );
278 fail( "Entry must not exist" );
279 }
280 catch ( NameNotFoundException ignored )
281 {
282
283 assertTrue( true );
284 }
285
286
287 DirContext tori = ( DirContext ) ctx.lookup( newRdn );
288 assertNotNull( tori );
289
290
291 cn = tori.getAttributes( "" ).get( "cn" );
292 assertTrue( cn.contains( newCn ) );
293 assertTrue( !cn.contains( oldCn ) );
294 assertTrue( cn.contains( alternateCn ) );
295 assertEquals( 2, cn.size() );
296
297
298 ctx.unbind( newRdn );
299 }
300
301
302
303
304
305 @Test
306 public void testModifyRdnDifferentAttribute() throws Exception
307 {
308 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
309
310
311 String cnVal = "Tori Amos";
312 String snVal = "Amos";
313 String oldRdn = "cn=" + cnVal;
314 Attributes attributes = this.getPersonAttributes( snVal, cnVal );
315 ctx.createSubcontext( oldRdn, attributes );
316
317
318 String newRdn = "sn=" + snVal;
319 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
320 ctx.rename( oldRdn, newRdn );
321
322
323 try
324 {
325 ctx.lookup( oldRdn );
326 fail( "Entry must not exist" );
327 }
328 catch ( NameNotFoundException ignored )
329 {
330
331 }
332
333
334 DirContext tori = ( DirContext ) ctx.lookup( newRdn );
335 assertNotNull( tori );
336
337
338
339 Attribute cn = tori.getAttributes( "" ).get( "cn" );
340 assertTrue( cn.contains( cnVal ) );
341 assertEquals( "Number of cn occurences", 1, cn.size() );
342 Attribute sn = tori.getAttributes( "" ).get( "sn" );
343 assertTrue( sn.contains( snVal ) );
344 assertEquals( "Number of sn occurences", 1, sn.size() );
345
346
347 ctx.unbind( newRdn );
348 }
349
350
351
352
353
354
355 @Test
356 public void testModifyRdnDifferentAttributeDeleteOldFails() throws Exception
357 {
358 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
359
360
361 String cnVal = "Tori Amos";
362 String snVal = "Amos";
363 String oldRdn = "cn=" + cnVal;
364 Attributes attributes = this.getPersonAttributes( snVal, cnVal );
365 ctx.createSubcontext( oldRdn, attributes );
366
367
368 String newRdn = "sn=" + snVal;
369 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
370 try
371 {
372 ctx.rename( oldRdn, newRdn );
373 fail( "Rename must fail, mandatory attirbute cn can not be deleted." );
374 }
375 catch ( SchemaViolationException ignored )
376 {
377
378 }
379
380
381 ctx.unbind( oldRdn );
382 }
383
384
385
386
387
388
389
390 @Test
391 public void testModifyRdnAndDeleteOldWithChild() throws Exception
392 {
393 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
394
395
396 String oldOu = "Writers";
397 String oldRdn = "ou=" + oldOu;
398 Attributes attributes = this.getOrganizationalUnitAttributes( oldOu );
399 DirContext createdCtx = ctx.createSubcontext( oldRdn, attributes );
400
401
402 String childCn = "Tori Amos";
403 String childRdn = "cn=" + childCn;
404 Attributes childAttributes = this.getPersonAttributes( "Amos", childCn );
405 createdCtx.createSubcontext( childRdn, childAttributes );
406
407
408 String newOu = "Singers";
409 String newRdn = "ou=" + newOu;
410 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
411 ctx.rename( oldRdn, newRdn );
412
413
414 try
415 {
416 ctx.lookup( oldRdn );
417 fail( "Entry must not exist" );
418 }
419 catch ( NameNotFoundException ignored )
420 {
421
422 assertTrue( true );
423 }
424
425
426 DirContext org = ( DirContext ) ctx.lookup( newRdn );
427 assertNotNull( org );
428
429
430 Attribute ou = org.getAttributes( "" ).get( "ou" );
431 assertTrue( ou.contains( newOu ) );
432 assertTrue( !ou.contains( oldOu ) );
433 assertEquals( 1, ou.size() );
434
435
436 SearchControls searchControls = new SearchControls();
437 searchControls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
438 searchControls.setReturningAttributes( new String[]
439 { "objectClass" } );
440 NamingEnumeration<SearchResult> results = org.search( "", "(objectClass=*)", searchControls );
441 assertTrue( results.hasMore() );
442 results.next();
443 assertTrue( !results.hasMore() );
444
445
446 DirContext tori = ( DirContext ) org.lookup( childRdn );
447 assertNotNull( tori );
448
449
450 ctx.unbind( childRdn + "," + newRdn );
451 ctx.unbind( newRdn );
452 }
453
454
455
456
457
458
459
460 @Test
461 public void testModifyRdnWithEncodedNewRdn() throws Exception
462 {
463 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
464
465
466 String cnVal = "Tori Amos";
467 String snVal = "Amos";
468 String oldRdn = "cn=" + cnVal;
469 Attributes attributes = this.getPersonAttributes( snVal, cnVal );
470 ctx.createSubcontext( oldRdn, attributes );
471
472
473 String newCnEscapedVal = new String( new byte[]
474 { ( byte ) 0xC3, ( byte ) 0xA4, '\\', '+' }, "UTF-8" );
475 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
476 String newRdn = "cn=" + newCnEscapedVal;
477 ctx.rename( oldRdn, newRdn );
478
479
480 try
481 {
482 ctx.lookup( oldRdn );
483 fail( "Entry must not exist" );
484 }
485 catch ( NameNotFoundException ignored )
486 {
487
488 }
489
490
491 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
492 assertNotNull( newCtx );
493
494
495 assertEquals( "cn=" + newCnEscapedVal + "," + ctx.getNameInNamespace(), newCtx.getNameInNamespace() );
496
497
498 Attribute cn = newCtx.getAttributes( "" ).get( "cn" );
499 assertEquals( "Number of cn occurences", 1, cn.size() );
500 String expectedCn = new String( new byte[] { ( byte ) 0xC3, ( byte ) 0xA4, '+' }, "UTF-8" );
501 assertTrue( cn.contains( expectedCn ) );
502
503
504 ctx.unbind( newRdn );
505 }
506
507
508
509
510
511
512
513 @Test
514 public void testModifyRdnWithEscapedPoundNewRdn() throws Exception
515 {
516 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
517
518
519 String cnVal = "Tori Amos";
520 String snVal = "Amos";
521 String oldRdn = "cn=" + cnVal;
522 Attributes attributes = this.getPersonAttributes( snVal, cnVal );
523 ctx.createSubcontext( oldRdn, attributes );
524
525
526 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
527 String newRdn = "cn=\\23test";
528 ctx.rename( oldRdn, newRdn );
529
530
531 try
532 {
533 ctx.lookup( oldRdn );
534 fail( "Entry must not exist" );
535 }
536 catch ( NameNotFoundException ignored )
537 {
538
539 }
540
541
542 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
543 assertNotNull( newCtx );
544
545
546 assertEquals( "cn=\\23test," + ctx.getNameInNamespace(), newCtx.getNameInNamespace() );
547
548
549 Attribute cn = newCtx.getAttributes( "" ).get( "cn" );
550 assertEquals( "Number of cn occurences", 1, cn.size() );
551 assertTrue( cn.contains( "#test" ) );
552
553
554 ctx.unbind( newRdn );
555 }
556
557
558
559
560
561
562
563
564
565
566
567 @Test
568 public void testModifyMultiValuedRdnVariant1() throws Exception
569 {
570 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
571
572 Attributes attributes = createPerson( "cn" );
573 String oldRdn = getRdn( attributes, "cn" );
574 String newRdn = getRdn( attributes, "cn", "sn" );
575
576 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
577 ctx.rename( oldRdn, newRdn );
578
579
580 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
581 assertNotNull( newCtx );
582
583
584 Attribute cnAttr = newCtx.getAttributes( "" ).get( "cn" );
585 assertEquals( 1, cnAttr.size() );
586 assertTrue( cnAttr.contains( "Tori Amos" ) );
587 Attribute snAttr = newCtx.getAttributes( "" ).get( "sn" );
588 assertEquals( 1, snAttr.size() );
589 assertTrue( snAttr.contains( "Amos" ) );
590 Attribute descriptionAttr = newCtx.getAttributes( "" ).get( "description" );
591 assertEquals( 1, descriptionAttr.size() );
592
593
594 ctx.unbind( newRdn );
595 }
596
597
598
599
600
601
602
603
604
605
606
607 @Test
608 public void testModifyMultiValuedRdnVariant2() throws Exception
609 {
610 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
611
612 Attributes attributes = createPerson( "cn" );
613 String oldRdn = getRdn( attributes, "cn" );
614 String newRdn = getRdn( attributes, "cn", "sn" );
615
616 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
617 ctx.rename( oldRdn, newRdn );
618
619
620 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
621 assertNotNull( newCtx );
622
623
624 Attribute cnAttr = newCtx.getAttributes( "" ).get( "cn" );
625 assertEquals( 1, cnAttr.size() );
626 assertTrue( cnAttr.contains( "Tori Amos" ) );
627 Attribute snAttr = newCtx.getAttributes( "" ).get( "sn" );
628 assertEquals( 1, snAttr.size() );
629 assertTrue( snAttr.contains( "Amos" ) );
630 Attribute descriptionAttr = newCtx.getAttributes( "" ).get( "description" );
631 assertEquals( 1, descriptionAttr.size() );
632
633
634 ctx.unbind( newRdn );
635 }
636
637
638
639
640
641
642
643
644
645
646
647 @Test
648 public void testModifyMultiValuedRdnVariant3() throws Exception
649 {
650 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
651
652 Attributes attributes = createPerson( "description" );
653 String oldRdn = getRdn( attributes, "description" );
654 String newRdn = getRdn( attributes, "cn", "sn" );
655
656 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
657 ctx.rename( oldRdn, newRdn );
658
659
660 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
661 assertNotNull( newCtx );
662
663
664 Attribute cnAttr = newCtx.getAttributes( "" ).get( "cn" );
665 assertEquals( 1, cnAttr.size() );
666 assertTrue( cnAttr.contains( "Tori Amos" ) );
667 Attribute snAttr = newCtx.getAttributes( "" ).get( "sn" );
668 assertEquals( 1, snAttr.size() );
669 assertTrue( snAttr.contains( "Amos" ) );
670 Attribute descriptionAttr = newCtx.getAttributes( "" ).get( "description" );
671 assertEquals( 1, descriptionAttr.size() );
672
673
674 ctx.unbind( newRdn );
675 }
676
677
678
679
680
681
682
683
684
685
686
687 @Test
688 public void testModifyMultiValuedRdnVariant4() throws Exception
689 {
690 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
691
692 Attributes attributes = createPerson( "description" );
693 String oldRdn = getRdn( attributes, "description" );
694 String newRdn = getRdn( attributes, "cn", "sn" );
695
696 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
697 ctx.rename( oldRdn, newRdn );
698
699
700 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
701 assertNotNull( newCtx );
702
703
704 Attribute cnAttr = newCtx.getAttributes( "" ).get( "cn" );
705 assertEquals( 1, cnAttr.size() );
706 assertTrue( cnAttr.contains( "Tori Amos" ) );
707 Attribute snAttr = newCtx.getAttributes( "" ).get( "sn" );
708 assertEquals( 1, snAttr.size() );
709 assertTrue( snAttr.contains( "Amos" ) );
710 Attribute descriptionAttr = newCtx.getAttributes( "" ).get( "description" );
711 assertNull( descriptionAttr );
712
713
714 ctx.unbind( newRdn );
715 }
716
717
718
719
720
721
722
723
724
725
726
727
728
729 @Test
730 @Ignore ( "Until this is fixed: https://issues.apache.org/jira/browse/DIRSERVER-1231" )
731 public void testModifyMultiValuedRdnVariant5() throws Exception
732 {
733 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
734
735 Attributes attributes = createPerson( "cn" );
736 attributes.put( "telephoneNumber", "12345" );
737 String oldRdn = getRdn( attributes, "cn" );
738 String newRdn = getRdn( attributes, "sn", "telephoneNumber" );
739
740 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
741 ctx.rename( oldRdn, newRdn );
742
743
744 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
745 assertNotNull( newCtx );
746
747
748 Attribute cnAttr = newCtx.getAttributes( "" ).get( "cn" );
749 assertEquals( 1, cnAttr.size() );
750 assertTrue( cnAttr.contains( "Tori Amos" ) );
751 Attribute snAttr = newCtx.getAttributes( "" ).get( "sn" );
752 assertEquals( 1, snAttr.size() );
753 assertTrue( snAttr.contains( "Amos" ) );
754 Attribute descriptionAttr = newCtx.getAttributes( "" ).get( "description" );
755 assertEquals( 1, descriptionAttr.size() );
756 Attribute telephoneNumberAttr = newCtx.getAttributes( "" ).get( "telephoneNumber" );
757 assertEquals( 1, telephoneNumberAttr.size() );
758 assertTrue( telephoneNumberAttr.contains( "12345" ) );
759
760
761 ctx.unbind( newRdn );
762 }
763
764
765
766
767
768
769
770
771
772
773
774
775
776 @Test
777 @Ignore ( "Until this is fixed: https://issues.apache.org/jira/browse/DIRSERVER-1231" )
778 public void testModifyMultiValuedRdnVariant6() throws Exception
779 {
780 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
781
782 Attributes attributes = createPerson( "cn" );
783 attributes.put( "telephoneNumber", "12345" );
784 String oldRdn = getRdn( attributes, "cn" );
785 String newRdn = getRdn( attributes, "sn", "telephoneNumber" );
786
787 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
788 try
789 {
790 ctx.rename( oldRdn, newRdn );
791 fail( "Rename must fail, cn can not be deleted from a person." );
792 }
793 catch ( SchemaViolationException ignored )
794 {
795
796 }
797
798
799 try
800 {
801 ctx.lookup( newRdn );
802 fail( "Previous rename failed as expected, entry must not exist" );
803 }
804 catch ( NameNotFoundException ignored )
805 {
806
807 }
808
809
810 DirContext oldCtx = ( DirContext ) ctx.lookup( oldRdn );
811 assertNotNull( oldCtx );
812 Attribute cnAttr = oldCtx.getAttributes( "" ).get( "cn" );
813 assertEquals( 1, cnAttr.size() );
814 assertTrue( cnAttr.contains( "Tori Amos" ) );
815 Attribute snAttr = oldCtx.getAttributes( "" ).get( "sn" );
816 assertEquals( 1, snAttr.size() );
817 assertTrue( snAttr.contains( "Amos" ) );
818 Attribute descriptionAttr = oldCtx.getAttributes( "" ).get( "description" );
819 assertEquals( 1, descriptionAttr.size() );
820
821
822 ctx.unbind( oldRdn );
823 }
824
825
826
827
828
829
830
831
832
833
834
835
836
837 @Test
838 public void testModifyMultiValuedRdnVariant7() throws Exception
839 {
840 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
841
842 Attributes attributes = createPerson( "cn", "sn" );
843 String oldRdn = getRdn( attributes, "cn", "sn" );
844 String newRdn = getRdn( attributes, "cn" );
845
846 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
847 ctx.rename( oldRdn, newRdn );
848
849
850 DirContext newCtx = ( DirContext ) ctx.lookup( newRdn );
851 assertNotNull( newCtx );
852
853
854 Attribute cnAttr = newCtx.getAttributes( "" ).get( "cn" );
855 assertEquals( 1, cnAttr.size() );
856 assertTrue( cnAttr.contains( "Tori Amos" ) );
857 Attribute snAttr = newCtx.getAttributes( "" ).get( "sn" );
858 assertEquals( 1, snAttr.size() );
859 assertTrue( snAttr.contains( "Amos" ) );
860 Attribute descriptionAttr = newCtx.getAttributes( "" ).get( "description" );
861 assertEquals( 1, descriptionAttr.size() );
862
863
864 ctx.unbind( newRdn );
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878
879 @Test
880 public void testModifyMultiValuedRdnVariant8() throws Exception
881 {
882 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
883
884 Attributes attributes = createPerson( "cn", "sn" );
885 String oldRdn = getRdn( attributes, "cn", "sn" );
886 String newRdn = getRdn( attributes, "cn" );
887
888 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
889 try
890 {
891 ctx.rename( oldRdn, newRdn );
892 fail( "Rename must fail, cn can not be deleted from a person." );
893 }
894 catch ( SchemaViolationException ignored )
895 {
896
897 }
898
899
900 try
901 {
902 ctx.lookup( newRdn );
903 fail( "Previous rename failed as expected, entry must not exist" );
904 }
905 catch ( NameNotFoundException ignored )
906 {
907
908 }
909
910
911 DirContext oldCtx = ( DirContext ) ctx.lookup( oldRdn );
912 assertNotNull( oldCtx );
913 Attribute cnAttr = oldCtx.getAttributes( "" ).get( "cn" );
914 assertEquals( 1, cnAttr.size() );
915 assertTrue( cnAttr.contains( "Tori Amos" ) );
916 Attribute snAttr = oldCtx.getAttributes( "" ).get( "sn" );
917 assertEquals( 1, snAttr.size() );
918 assertTrue( snAttr.contains( "Amos" ) );
919 Attribute descriptionAttr = oldCtx.getAttributes( "" ).get( "description" );
920 assertEquals( 1, descriptionAttr.size() );
921
922
923 ctx.unbind( oldRdn );
924 }
925
926
927
928
929
930
931
932
933
934
935
936 @Test
937 public void testModifyRdnOperationalAttribute() throws Exception
938 {
939 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
940
941
942 Attributes attributes = createPerson( "cn" );
943 String oldRdn = getRdn( attributes, "cn" );
944
945
946 String createTimestamp = ( String ) ctx.getAttributes( oldRdn, new String[]
947 { "createTimestamp" } ).get( "createTimestamp" ).get();
948
949
950 String newRdn = "createTimestamp=" + createTimestamp;
951 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
952 ctx.rename( oldRdn, newRdn );
953
954
955
956 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
957 try
958 {
959 ctx.rename( newRdn, oldRdn );
960 fail( "Rename must fail, operational attribute createTimestamp can not be deleted." );
961 }
962 catch ( NoPermissionException ignored )
963 {
964
965 }
966
967
968 ctx.unbind( newRdn );
969 }
970
971
972
973
974
975
976
977
978
979
980
981 @Test
982 public void testModifyRdnObjectClassAttribute() throws Exception
983 {
984 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
985
986
987 Attributes attributes = createPerson( "cn" );
988 String oldRdn = getRdn( attributes, "cn" );
989
990
991 String newRdn = "objectClass=person";
992 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "false" );
993 ctx.rename( oldRdn, newRdn );
994
995
996
997 ctx.addToEnvironment( "java.naming.ldap.deleteRDN", "true" );
998 try
999 {
1000 ctx.rename( newRdn, oldRdn );
1001 fail( "Rename must fail, structural objectClass person can not be deleted." );
1002 }
1003 catch ( SchemaViolationException ignored )
1004 {
1005
1006 }
1007
1008
1009 ctx.unbind( newRdn );
1010 }
1011
1012
1013 private String getRdn( Attributes attributes, String... rdnTypes ) throws Exception
1014 {
1015 String rdn = "";
1016
1017 for ( String type : rdnTypes )
1018 {
1019 rdn += type + "=" + attributes.get( type ).get() + "+";
1020 }
1021
1022 rdn = rdn.substring( 0, rdn.length() - 1 );
1023 return rdn;
1024 }
1025
1026
1027 private Attributes createPerson( String... rdnTypes ) throws Exception
1028 {
1029 DirContext ctx = ( DirContext ) getWiredContext( ldapService ).lookup( BASE );
1030
1031 Attributes attributes = new BasicAttributes( true );
1032 Attribute attribute = new BasicAttribute( "objectClass" );
1033 attribute.add( "top" );
1034 attribute.add( "person" );
1035 attributes.put( attribute );
1036 attributes.put( "cn", "Tori Amos" );
1037 attributes.put( "sn", "Amos" );
1038 attributes.put( "description", "Tori Amos is a person." );
1039
1040 String rdn = getRdn( attributes, rdnTypes );
1041
1042 ctx.createSubcontext( rdn, attributes );
1043
1044 return attributes;
1045 }
1046 }