Skip to content

Commit 6593fef

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 6593fef

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2016 the original author or authors.
2+
* Copyright 2011-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.
@@ -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
@@ -3629,10 +3633,12 @@ static class WithObjectTypeProperty {
36293633
Object value;
36303634
}
36313635

3632-
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3636+
static class PersonWithIdPropertyOfTypeUUIDListener
3637+
extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
36333638

36343639
@Override
36353640
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3641+
36363642
PersonWithIdPropertyOfTypeUUID person = event.getSource();
36373643

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

36423648
person.setId(UUID.randomUUID());
36433649
}
3644-
36453650
}
36463651
}
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)