Skip to content

Commit 8cfb002

Browse files
Update cyclic test cases
1 parent cd25a4f commit 8cfb002

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDocumentReferenceTests.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import lombok.Data;
2424
import lombok.Getter;
2525
import lombok.Setter;
26+
import lombok.ToString;
2627

2728
import java.util.Arrays;
2829
import java.util.Collections;
2930
import java.util.LinkedHashMap;
3031
import java.util.List;
3132
import java.util.Map;
3233

33-
import lombok.ToString;
3434
import org.bson.Document;
3535
import org.junit.jupiter.api.BeforeEach;
3636
import org.junit.jupiter.api.Test;
@@ -39,7 +39,6 @@
3939
import org.springframework.data.annotation.Id;
4040
import org.springframework.data.convert.WritingConverter;
4141
import org.springframework.data.mongodb.core.convert.LazyLoadingTestUtils;
42-
import org.springframework.data.mongodb.core.mapping.DBRef;
4342
import org.springframework.data.mongodb.core.mapping.DocumentPointer;
4443
import org.springframework.data.mongodb.core.mapping.DocumentReference;
4544
import org.springframework.data.mongodb.core.mapping.Field;
@@ -52,14 +51,14 @@
5251
import com.mongodb.client.model.Filters;
5352

5453
/**
55-
* {@link DBRef} related integration tests for {@link MongoTemplate}.
54+
* {@link DocumentReference} related integration tests for {@link MongoTemplate}.
5655
*
5756
* @author Christoph Strobl
5857
*/
5958
@ExtendWith(MongoClientExtension.class)
6059
public class MongoTemplateDocumentReferenceTests {
6160

62-
public static final String DB_NAME = "manual-reference-tests";
61+
public static final String DB_NAME = "document-reference-tests";
6362

6463
static @Client MongoClient client;
6564

@@ -492,16 +491,43 @@ void testLazyCyclic() {
492491

493492
WithRefB b = new WithRefB();
494493
b.id = "b";
495-
b.toA = a;
496494

497495
a.toB = b;
496+
b.lazyToA = a;
498497

499498
template.save(a);
500499
template.save(b);
501500

502501
WithRefA loadedA = template.query(WithRefA.class).matching(where("id").is(a.id)).firstValue();
503502
assertThat(loadedA).isNotNull();
504503
assertThat(loadedA.getToB()).isNotNull();
504+
LazyLoadingTestUtils.assertProxy(loadedA.getToB().lazyToA, (proxy) -> {
505+
506+
assertThat(proxy.isResolved()).isFalse();
507+
assertThat(proxy.currentValue()).isNull();
508+
});
509+
}
510+
511+
@Test
512+
void testEagerCyclic() {
513+
514+
WithRefA a = new WithRefA();
515+
a.id = "a";
516+
517+
WithRefB b = new WithRefB();
518+
b.id = "b";
519+
520+
a.toB = b;
521+
b.eagerToA = a;
522+
523+
template.save(a);
524+
template.save(b);
525+
526+
WithRefA loadedA = template.query(WithRefA.class).matching(where("id").is(a.id)).firstValue();
527+
528+
assertThat(loadedA).isNotNull();
529+
assertThat(loadedA.getToB()).isNotNull();
530+
assertThat(loadedA.getToB().eagerToA).isSameAs(loadedA);
505531
}
506532

507533
@Data
@@ -573,7 +599,6 @@ static class SimpleObjectRef {
573599

574600
@Id String id;
575601
String value;
576-
577602
}
578603

579604
@Getter
@@ -683,8 +708,9 @@ public Object toReference() {
683708
static class WithRefB/* to A */ implements ReferenceAble {
684709

685710
@Id String id;
686-
@DocumentReference //(lazy = true)
687-
WithRefA toA;
711+
@DocumentReference(lazy = true) WithRefA lazyToA;
712+
713+
@DocumentReference WithRefA eagerToA;
688714

689715
@Override
690716
public Object toReference() {

0 commit comments

Comments
 (0)