29
29
import java .util .LinkedList ;
30
30
import java .util .List ;
31
31
import java .util .Map ;
32
+ import java .util .Set ;
32
33
33
- import com .mongodb .BasicDBObject ;
34
- import org .bson .BsonDocument ;
35
34
import org .bson .Document ;
36
35
import org .bson .conversions .Bson ;
37
36
import org .bson .types .ObjectId ;
38
37
import org .junit .Before ;
39
38
import org .junit .Test ;
40
39
import org .junit .runner .RunWith ;
41
- import org .mockito .Matchers ;
42
40
import org .mockito .Mock ;
43
41
import org .mockito .Mockito ;
44
42
import org .mockito .junit .MockitoJUnitRunner ;
43
+
45
44
import org .springframework .data .annotation .AccessType ;
46
45
import org .springframework .data .annotation .AccessType .Type ;
47
46
import org .springframework .data .annotation .Id ;
65
64
66
65
/**
67
66
* Unit tests for {@link DbRefMappingMongoConverter}.
68
- *
67
+ *
69
68
* @author Oliver Gierke
70
69
* @author Thomas Darimont
71
70
* @author Christoph Strobl
@@ -485,7 +484,8 @@ public void shouldEagerlyResolveIdPropertyWithFieldAccess() {
485
484
ClassWithLazyDbRefs result = converter .read (ClassWithLazyDbRefs .class , object );
486
485
487
486
PersistentPropertyAccessor accessor = propertyEntity .getPropertyAccessor (result .dbRefToConcreteType );
488
- MongoPersistentProperty idProperty = mappingContext .getRequiredPersistentEntity (LazyDbRefTarget .class ).getIdProperty ().get ();
487
+ MongoPersistentProperty idProperty = mappingContext .getRequiredPersistentEntity (LazyDbRefTarget .class )
488
+ .getIdProperty ().get ();
489
489
490
490
assertThat (accessor .getProperty (idProperty ), is (notNullValue ()));
491
491
assertProxyIsResolved (result .dbRefToConcreteType , false );
@@ -512,7 +512,8 @@ public void shouldNotEagerlyResolveIdPropertyWithPropertyAccess() {
512
512
@ Test // DATAMONGO-1076
513
513
public void shouldNotTriggerResolvingOfLazyLoadedProxyWhenFinalizeMethodIsInvoked () throws Exception {
514
514
515
- MongoPersistentEntity <?> entity = mappingContext .getRequiredPersistentEntity (WithObjectMethodOverrideLazyDbRefs .class );
515
+ MongoPersistentEntity <?> entity = mappingContext
516
+ .getRequiredPersistentEntity (WithObjectMethodOverrideLazyDbRefs .class );
516
517
MongoPersistentProperty property = entity .getRequiredPersistentProperty ("dbRefToPlainObject" );
517
518
518
519
String idValue = new ObjectId ().toString ();
@@ -534,8 +535,9 @@ public void shouldBulkFetchListOfReferences() {
534
535
String value = "val" ;
535
536
536
537
MappingMongoConverter converterSpy = spy (converter );
537
- doReturn (Arrays .asList (new Document ("_id" , id1 ).append ("value" , value ),
538
- new Document ("_id" , id2 ).append ("value" , value ))).when (converterSpy ).bulkReadRefs (anyListOf (DBRef .class ));
538
+ doReturn (
539
+ Arrays .asList (new Document ("_id" , id1 ).append ("value" , value ), new Document ("_id" , id2 ).append ("value" , value )))
540
+ .when (converterSpy ).bulkReadRefs (anyListOf (DBRef .class ));
539
541
540
542
Document document = new Document ();
541
543
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs ();
@@ -553,6 +555,28 @@ public void shouldBulkFetchListOfReferences() {
553
555
verify (converterSpy , never ()).readRef (Mockito .any (DBRef .class ));
554
556
}
555
557
558
+ @ Test // DATAMONGO-1666
559
+ public void shouldBulkFetchSetOfReferencesForConstructorCreation () {
560
+
561
+ String id1 = "1" ;
562
+ String id2 = "2" ;
563
+ String value = "val" ;
564
+
565
+ MappingMongoConverter converterSpy = spy (converter );
566
+ doReturn (
567
+ Arrays .asList (new Document ("_id" , id1 ).append ("value" , value ), new Document ("_id" , id2 ).append ("value" , value )))
568
+ .when (converterSpy ).bulkReadRefs (any ());
569
+
570
+ Document document = new Document ("dbRefToInterface" ,
571
+ Arrays .asList (new DBRef ("lazyDbRefTarget" , "1" ), new DBRef ("lazyDbRefTarget" , "2" )));
572
+
573
+ ClassWithDbRefSetConstructor result = converterSpy .read (ClassWithDbRefSetConstructor .class , document );
574
+
575
+ assertThat (result .dbRefToInterface , is (instanceOf (Set .class )));
576
+
577
+ verify (converterSpy , never ()).readRef (Mockito .any (DBRef .class ));
578
+ }
579
+
556
580
@ Test // DATAMONGO-1194
557
581
public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointToDifferentCollections () {
558
582
@@ -561,9 +585,8 @@ public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointT
561
585
String value = "val" ;
562
586
563
587
MappingMongoConverter converterSpy = spy (converter );
564
- doReturn (new Document ("_id" , id1 ).append ("value" , value ))
565
- .doReturn (new Document ("_id" , id2 ).append ("value" , value )).when (converterSpy )
566
- .readRef (Mockito .any (DBRef .class ));
588
+ doReturn (new Document ("_id" , id1 ).append ("value" , value )).doReturn (new Document ("_id" , id2 ).append ("value" , value ))
589
+ .when (converterSpy ).readRef (Mockito .any (DBRef .class ));
567
590
568
591
Document document = new Document ();
569
592
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs ();
@@ -678,6 +701,15 @@ static class ClassWithLazyDbRefs {
678
701
lazy = true ) LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor ;
679
702
}
680
703
704
+ static class ClassWithDbRefSetConstructor {
705
+
706
+ final @ org .springframework .data .mongodb .core .mapping .DBRef Set <LazyDbRefTarget > dbRefToInterface ;
707
+
708
+ public ClassWithDbRefSetConstructor (Set <LazyDbRefTarget > dbRefToInterface ) {
709
+ this .dbRefToInterface = dbRefToInterface ;
710
+ }
711
+ }
712
+
681
713
static class SerializableClassWithLazyDbRefs implements Serializable {
682
714
683
715
private static final long serialVersionUID = 1L ;
@@ -785,7 +817,7 @@ public ToStringObjectMethodOverrideLazyDbRefTarget(String id, String value) {
785
817
super (id , value );
786
818
}
787
819
788
- /*
820
+ /*
789
821
* (non-Javadoc)
790
822
* @see java.lang.Object#toString()
791
823
*/
0 commit comments