Skip to content

Commit 506b770

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 541f990 commit 506b770

File tree

2 files changed

+30
-55
lines changed

2 files changed

+30
-55
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,9 @@ public class MongoTemplateTests {
130130
.parse("2.4");
131131
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
132132
.parse("2.8");
133-
private static final org.springframework.data.util.Version THREE_DOT_FOUR= org.springframework.data.util.Version
133+
private static final org.springframework.data.util.Version THREE_DOT_FOUR = org.springframework.data.util.Version
134134
.parse("3.4");
135135

136-
137136
@Autowired MongoTemplate template;
138137
@Autowired MongoDbFactory factory;
139138

@@ -145,23 +144,25 @@ public class MongoTemplateTests {
145144

146145
@Autowired
147146
public void setApplicationContext(ConfigurableApplicationContext context) {
148-
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
147+
149148
this.context = context;
149+
150+
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
150151
}
151152

152153
@Autowired
153154
public void setMongo(Mongo mongo) throws Exception {
154155

155-
CustomConversions conversions = new CustomConversions(Arrays.asList(DateToDateTimeConverter.INSTANCE,
156-
DateTimeToDateConverter.INSTANCE));
156+
CustomConversions conversions = new CustomConversions(
157+
Arrays.asList(DateToDateTimeConverter.INSTANCE, DateTimeToDateConverter.INSTANCE));
157158

158159
MongoMappingContext mappingContext = new MongoMappingContext();
159-
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class,
160-
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
161-
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
162-
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
163-
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
164-
PersonWithIdPropertyOfTypeUUID.class)));
160+
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(
161+
Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class, PersonWith_idPropertyOfTypeString.class,
162+
PersonWithIdPropertyOfTypeObjectId.class, PersonWithIdPropertyOfTypeString.class,
163+
PersonWithIdPropertyOfTypeInteger.class, PersonWithIdPropertyOfTypeBigInteger.class,
164+
PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class,
165+
PersonWithIdPropertyOfPrimitiveLong.class, PersonWithIdPropertyOfTypeUUID.class)));
165166
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
166167
mappingContext.initialize();
167168

@@ -175,8 +176,11 @@ public void setMongo(Mongo mongo) throws Exception {
175176

176177
@Before
177178
public void setUp() {
179+
178180
cleanDb();
179181
queryMongoVersionIfNecessary();
182+
183+
this.mappingTemplate.setApplicationContext(context);
180184
}
181185

182186
@After
@@ -648,7 +652,6 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
648652
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
649653
p13.setFirstName("Sven_10");
650654
p13.setAge(22);
651-
p13.setId(UUID.randomUUID());
652655
// insert
653656
mongoTemplate.insert(p13);
654657
// also try save
@@ -2406,7 +2409,7 @@ public void updateWithPullShouldRemoveNestedItemFromDbRefAnnotatedCollection() {
24062409
doc.dbRefAnnotatedList = Arrays.asList( //
24072410
sample1, //
24082411
sample2 //
2409-
);
2412+
);
24102413
template.save(doc);
24112414

24122415
Update update = new Update().pull("dbRefAnnotatedList", doc.dbRefAnnotatedList.get(1));
@@ -2435,7 +2438,7 @@ public void updateWithPullShouldRemoveNestedItemFromDbRefAnnotatedCollectionWhen
24352438
doc.dbRefAnnotatedList = Arrays.asList( //
24362439
sample1, //
24372440
sample2 //
2438-
);
2441+
);
24392442
template.save(doc);
24402443

24412444
Update update = new Update().pull("dbRefAnnotatedList.id", "2");
@@ -2500,8 +2503,8 @@ public void shouldBeAbleToUpdateDbRefPropertyWithDomainObject() {
25002503
@Test // DATAMONGO-862
25012504
public void testUpdateShouldWorkForPathsOnInterfaceMethods() {
25022505

2503-
DocumentWithCollection document = new DocumentWithCollection(Arrays.<Model> asList(new ModelA("spring"),
2504-
new ModelA("data")));
2506+
DocumentWithCollection document = new DocumentWithCollection(
2507+
Arrays.<Model> asList(new ModelA("spring"), new ModelA("data")));
25052508

25062509
template.save(document);
25072510

@@ -2967,7 +2970,7 @@ public void updatesNumericValueCorrectlyWhenUsingMinOperator() {
29672970
.min("longVal", 490) //
29682971
.min("bigIntegerVal", new BigInteger("590")) //
29692972
.min("bigDeciamVal", new BigDecimal("690")) //
2970-
;
2973+
;
29712974

29722975
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
29732976

@@ -3023,7 +3026,7 @@ public void updatesNumericValueCorrectlyWhenUsingMaxOperator() {
30233026
.max("longVal", 590) //
30243027
.max("bigIntegerVal", new BigInteger("690")) //
30253028
.max("bigDeciamVal", new BigDecimal("790")) //
3026-
;
3029+
;
30273030

30283031
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
30293032

@@ -3052,7 +3055,7 @@ public void updatesBigNumberValueUsingStringComparisonWhenUsingMaxOperator() {
30523055
Update update = new Update()//
30533056
.max("bigIntegerVal", new BigInteger("70")) //
30543057
.max("bigDeciamVal", new BigDecimal("80")) //
3055-
;
3058+
;
30563059

30573060
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
30583061

@@ -3076,7 +3079,7 @@ public void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator() {
30763079
Update update = new Update()//
30773080
.min("bigIntegerVal", new BigInteger("700")) //
30783081
.min("bigDeciamVal", new BigDecimal("800")) //
3079-
;
3082+
;
30803083

30813084
template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);
30823085

@@ -3584,10 +3587,12 @@ static class WithObjectTypeProperty {
35843587
Object value;
35853588
}
35863589

3587-
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3590+
static class PersonWithIdPropertyOfTypeUUIDListener
3591+
extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
35883592

35893593
@Override
35903594
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3595+
35913596
PersonWithIdPropertyOfTypeUUID person = event.getSource();
35923597

35933598
if (person.getId() != null) {
@@ -3596,6 +3601,5 @@ public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> e
35963601

35973602
person.setId(UUID.randomUUID());
35983603
}
3599-
36003604
}
36013605
}
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)