Skip to content

Commit a562904

Browse files
committed
DATAMONGO-1617 - Polishing.
Some cleanups in MongoTemplateTests. Removed manual ID assignment in general id handling test to make sure we use the id generation. Removed unneccessary code from domain type in favor of Lombok. Original pull request: #443.
1 parent 5de3cb9 commit a562904

File tree

2 files changed

+236
-43
lines changed

2 files changed

+236
-43
lines changed

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

Lines changed: 232 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ public class MongoTemplateTests {
138138

139139
@Autowired
140140
public void setApplicationContext(ConfigurableApplicationContext context) {
141-
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
141+
142142
this.context = context;
143+
144+
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
143145
}
144146

145147
@Autowired
@@ -149,12 +151,12 @@ public void setMongo(Mongo mongo) throws Exception {
149151
Arrays.asList(DateToDateTimeConverter.INSTANCE, DateTimeToDateConverter.INSTANCE));
150152

151153
MongoMappingContext mappingContext = new MongoMappingContext();
152-
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class,
153-
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
154-
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
155-
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
156-
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
157-
PersonWithIdPropertyOfTypeUUID.class)));
154+
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(
155+
Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class, PersonWith_idPropertyOfTypeString.class,
156+
PersonWithIdPropertyOfTypeObjectId.class, PersonWithIdPropertyOfTypeString.class,
157+
PersonWithIdPropertyOfTypeInteger.class, PersonWithIdPropertyOfTypeBigInteger.class,
158+
PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class,
159+
PersonWithIdPropertyOfPrimitiveLong.class, PersonWithIdPropertyOfTypeUUID.class)));
158160
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
159161
mappingContext.initialize();
160162

@@ -168,8 +170,11 @@ public void setMongo(Mongo mongo) throws Exception {
168170

169171
@Before
170172
public void setUp() {
173+
171174
cleanDb();
172175
queryMongoVersionIfNecessary();
176+
177+
this.mappingTemplate.setApplicationContext(context);
173178
}
174179

175180
@After
@@ -659,7 +664,6 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
659664
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
660665
p13.setFirstName("Sven_10");
661666
p13.setAge(22);
662-
p13.setId(UUID.randomUUID());
663667
// insert
664668
mongoTemplate.insert(p13);
665669
// also try save
@@ -3216,6 +3220,223 @@ public void updateShouldWorkForTypesContainingGeoJsonTypes() {
32163220
assertThat(template.findOne(query(where("id").is(wgj.id)), WithGeoJson.class).point, is(equalTo(wgj.point)));
32173221
}
32183222

3223+
@Test // DATAMONGO-1404
3224+
public void updatesDateValueCorrectlyWhenUsingMinOperator() {
3225+
3226+
Calendar cal = Calendar.getInstance(Locale.US);
3227+
cal.set(2013, 10, 13, 0, 0, 0);
3228+
3229+
TypeWithDate twd = new TypeWithDate();
3230+
twd.date = new Date();
3231+
template.save(twd);
3232+
template.updateFirst(query(where("id").is(twd.id)), new Update().min("date", cal.getTime()), TypeWithDate.class);
3233+
3234+
TypeWithDate loaded = template.find(query(where("id").is(twd.id)), TypeWithDate.class).get(0);
3235+
assertThat(loaded.date, equalTo(cal.getTime()));
3236+
}
3237+
3238+
@Test // DATAMONGO-1404
3239+
public void updatesNumericValueCorrectlyWhenUsingMinOperator() {
3240+
3241+
TypeWithNumbers twn = new TypeWithNumbers();
3242+
twn.byteVal = 100;
3243+
twn.doubleVal = 200D;
3244+
twn.floatVal = 300F;
3245+
twn.intVal = 400;
3246+
twn.longVal = 500L;
3247+
3248+
// Note that $min operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3249+
twn.bigIntegerVal = new BigInteger("600");
3250+
twn.bigDeciamVal = new BigDecimal("700.0");
3251+
3252+
template.save(twn);
3253+
3254+
byte byteVal = 90;
3255+
Update update = new Update()//
3256+
.min("byteVal", byteVal) //
3257+
.min("doubleVal", 190D) //
3258+
.min("floatVal", 290F) //
3259+
.min("intVal", 390) //
3260+
.min("longVal", 490) //
3261+
.min("bigIntegerVal", new BigInteger("590")) //
3262+
.min("bigDeciamVal", new BigDecimal("690")) //
3263+
;
3264+
3265+
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
3266+
3267+
TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
3268+
assertThat(loaded.byteVal, equalTo(byteVal));
3269+
assertThat(loaded.doubleVal, equalTo(190D));
3270+
assertThat(loaded.floatVal, equalTo(290F));
3271+
assertThat(loaded.intVal, equalTo(390));
3272+
assertThat(loaded.longVal, equalTo(490L));
3273+
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("590")));
3274+
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("690")));
3275+
}
3276+
3277+
@Test // DATAMONGO-1404
3278+
public void updatesDateValueCorrectlyWhenUsingMaxOperator() {
3279+
3280+
Calendar cal = Calendar.getInstance(Locale.US);
3281+
cal.set(2013, 10, 13, 0, 0, 0);
3282+
3283+
TypeWithDate twd = new TypeWithDate();
3284+
twd.date = cal.getTime();
3285+
template.save(twd);
3286+
3287+
cal.set(2019, 10, 13, 0, 0, 0);
3288+
template.updateFirst(query(where("id").is(twd.id)), new Update().max("date", cal.getTime()), TypeWithDate.class);
3289+
3290+
TypeWithDate loaded = template.find(query(where("id").is(twd.id)), TypeWithDate.class).get(0);
3291+
assertThat(loaded.date, equalTo(cal.getTime()));
3292+
}
3293+
3294+
@Test // DATAMONGO-1404
3295+
public void updatesNumericValueCorrectlyWhenUsingMaxOperator() {
3296+
3297+
TypeWithNumbers twn = new TypeWithNumbers();
3298+
twn.byteVal = 100;
3299+
twn.doubleVal = 200D;
3300+
twn.floatVal = 300F;
3301+
twn.intVal = 400;
3302+
twn.longVal = 500L;
3303+
3304+
// Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3305+
twn.bigIntegerVal = new BigInteger("600");
3306+
twn.bigDeciamVal = new BigDecimal("700.0");
3307+
3308+
template.save(twn);
3309+
3310+
byte byteVal = 101;
3311+
Update update = new Update()//
3312+
.max("byteVal", byteVal) //
3313+
.max("doubleVal", 290D) //
3314+
.max("floatVal", 390F) //
3315+
.max("intVal", 490) //
3316+
.max("longVal", 590) //
3317+
.max("bigIntegerVal", new BigInteger("690")) //
3318+
.max("bigDeciamVal", new BigDecimal("790")) //
3319+
;
3320+
3321+
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
3322+
3323+
TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
3324+
assertThat(loaded.byteVal, equalTo(byteVal));
3325+
assertThat(loaded.doubleVal, equalTo(290D));
3326+
assertThat(loaded.floatVal, equalTo(390F));
3327+
assertThat(loaded.intVal, equalTo(490));
3328+
assertThat(loaded.longVal, equalTo(590L));
3329+
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("690")));
3330+
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("790")));
3331+
}
3332+
3333+
@Test // DATAMONGO-1404
3334+
public void updatesBigNumberValueUsingStringComparisonWhenUsingMaxOperator() {
3335+
3336+
TypeWithNumbers twn = new TypeWithNumbers();
3337+
3338+
// Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3339+
// Therefore "80" is considered greater than "700"
3340+
twn.bigIntegerVal = new BigInteger("600");
3341+
twn.bigDeciamVal = new BigDecimal("700.0");
3342+
3343+
template.save(twn);
3344+
3345+
Update update = new Update()//
3346+
.max("bigIntegerVal", new BigInteger("70")) //
3347+
.max("bigDeciamVal", new BigDecimal("80")) //
3348+
;
3349+
3350+
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
3351+
3352+
TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
3353+
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("70")));
3354+
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("80")));
3355+
}
3356+
3357+
@Test // DATAMONGO-1404
3358+
public void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator() {
3359+
3360+
TypeWithNumbers twn = new TypeWithNumbers();
3361+
3362+
// Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
3363+
// Therefore "80" is considered greater than "700"
3364+
twn.bigIntegerVal = new BigInteger("80");
3365+
twn.bigDeciamVal = new BigDecimal("90.0");
3366+
3367+
template.save(twn);
3368+
3369+
Update update = new Update()//
3370+
.min("bigIntegerVal", new BigInteger("700")) //
3371+
.min("bigDeciamVal", new BigDecimal("800")) //
3372+
;
3373+
3374+
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
3375+
3376+
TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
3377+
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("700")));
3378+
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("800")));
3379+
}
3380+
3381+
@Test // DATAMONGO-1431
3382+
public void streamExecutionUsesExplicitCollectionName() {
3383+
3384+
template.remove(new Query(), "some_special_collection");
3385+
template.remove(new Query(), Document.class);
3386+
3387+
Document document = new Document();
3388+
3389+
template.insert(document, "some_special_collection");
3390+
3391+
CloseableIterator<Document> stream = template.stream(new Query(), Document.class);
3392+
3393+
assertThat(stream.hasNext(), is(false));
3394+
3395+
stream = template.stream(new Query(), Document.class, "some_special_collection");
3396+
3397+
assertThat(stream.hasNext(), is(true));
3398+
assertThat(stream.next().id, is(document.id));
3399+
assertThat(stream.hasNext(), is(false));
3400+
}
3401+
3402+
@Test // DATAMONGO-1194
3403+
public void shouldFetchListOfReferencesCorrectly() {
3404+
3405+
Sample one = new Sample("1", "jon snow");
3406+
Sample two = new Sample("2", "tyrion lannister");
3407+
3408+
template.save(one);
3409+
template.save(two);
3410+
3411+
DocumentWithDBRefCollection source = new DocumentWithDBRefCollection();
3412+
source.dbRefAnnotatedList = Arrays.asList(two, one);
3413+
3414+
template.save(source);
3415+
3416+
assertThat(template.findOne(query(where("id").is(source.id)), DocumentWithDBRefCollection.class), is(source));
3417+
}
3418+
3419+
@Test // DATAMONGO-1194
3420+
public void shouldFetchListOfLazyReferencesCorrectly() {
3421+
3422+
Sample one = new Sample("1", "jon snow");
3423+
Sample two = new Sample("2", "tyrion lannister");
3424+
3425+
template.save(one);
3426+
template.save(two);
3427+
3428+
DocumentWithDBRefCollection source = new DocumentWithDBRefCollection();
3429+
source.lazyDbRefAnnotatedList = Arrays.asList(two, one);
3430+
3431+
template.save(source);
3432+
3433+
DocumentWithDBRefCollection target = template.findOne(query(where("id").is(source.id)),
3434+
DocumentWithDBRefCollection.class);
3435+
3436+
assertThat(target.lazyDbRefAnnotatedList, instanceOf(LazyLoadingProxy.class));
3437+
assertThat(target.getLazyDbRefAnnotatedList(), contains(two, one));
3438+
}
3439+
32193440
/**
32203441
* @see DATAMONGO-1513
32213442
*/
@@ -3629,10 +3850,12 @@ static class WithObjectTypeProperty {
36293850
Object value;
36303851
}
36313852

3632-
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3853+
static class PersonWithIdPropertyOfTypeUUIDListener
3854+
extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
36333855

36343856
@Override
36353857
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3858+
36363859
PersonWithIdPropertyOfTypeUUID person = event.getSource();
36373860

36383861
if (person.getId() != null) {
@@ -3641,6 +3864,5 @@ public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> e
36413864

36423865
person.setId(UUID.randomUUID());
36433866
}
3644-
36453867
}
36463868
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,43 +15,14 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18+
import lombok.Data;
19+
1820
import java.util.UUID;
1921

22+
@Data
2023
public class PersonWithIdPropertyOfTypeUUID {
2124

2225
private UUID id;
23-
2426
private String firstName;
25-
2627
private int age;
27-
28-
public UUID getId() {
29-
return id;
30-
}
31-
32-
public void setId(UUID id) {
33-
this.id = id;
34-
}
35-
36-
public String getFirstName() {
37-
return firstName;
38-
}
39-
40-
public void setFirstName(String firstName) {
41-
this.firstName = firstName;
42-
}
43-
44-
public int getAge() {
45-
return age;
46-
}
47-
48-
public void setAge(int age) {
49-
this.age = age;
50-
}
51-
52-
@Override
53-
public String toString() {
54-
return "PersonWithIdPropertyOfTypeUUID [id=" + id + ", firstName=" + firstName + ", age=" + age + "]";
55-
}
56-
5728
}

0 commit comments

Comments
 (0)