Skip to content

Commit 827b620

Browse files
odrotbohmchristophstrobl
authored andcommitted
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 781717f commit 827b620

File tree

2 files changed

+19
-43
lines changed

2 files changed

+19
-43
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ public class MongoTemplateTests {
143143

144144
@Autowired
145145
public void setApplicationContext(ConfigurableApplicationContext context) {
146-
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
146+
147147
this.context = context;
148+
149+
context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener());
148150
}
149151

150152
@Autowired
@@ -154,12 +156,12 @@ public void setMongo(Mongo mongo) throws Exception {
154156
Arrays.asList(DateToDateTimeConverter.INSTANCE, DateTimeToDateConverter.INSTANCE));
155157

156158
MongoMappingContext mappingContext = new MongoMappingContext();
157-
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class,
158-
PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class,
159-
PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class,
160-
PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class,
161-
PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class,
162-
PersonWithIdPropertyOfTypeUUID.class)));
159+
mappingContext.setInitialEntitySet(new HashSet<Class<?>>(
160+
Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class, PersonWith_idPropertyOfTypeString.class,
161+
PersonWithIdPropertyOfTypeObjectId.class, PersonWithIdPropertyOfTypeString.class,
162+
PersonWithIdPropertyOfTypeInteger.class, PersonWithIdPropertyOfTypeBigInteger.class,
163+
PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class,
164+
PersonWithIdPropertyOfPrimitiveLong.class, PersonWithIdPropertyOfTypeUUID.class)));
163165
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
164166
mappingContext.initialize();
165167

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

174176
@Before
175177
public void setUp() {
178+
176179
cleanDb();
177180
queryMongoVersionIfNecessary();
181+
182+
this.mappingTemplate.setApplicationContext(context);
178183
}
179184

180185
@After
@@ -643,7 +648,6 @@ private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) t
643648
PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID();
644649
p13.setFirstName("Sven_10");
645650
p13.setAge(22);
646-
p13.setId(UUID.randomUUID());
647651
// insert
648652
mongoTemplate.insert(p13);
649653
// also try save
@@ -3609,10 +3613,12 @@ static class WithObjectTypeProperty {
36093613
Object value;
36103614
}
36113615

3612-
static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
3616+
static class PersonWithIdPropertyOfTypeUUIDListener
3617+
extends AbstractMongoEventListener<PersonWithIdPropertyOfTypeUUID> {
36133618

36143619
@Override
36153620
public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> event) {
3621+
36163622
PersonWithIdPropertyOfTypeUUID person = event.getSource();
36173623

36183624
if (person.getId() != null) {
@@ -3621,6 +3627,5 @@ public void onBeforeConvert(BeforeConvertEvent<PersonWithIdPropertyOfTypeUUID> e
36213627

36223628
person.setId(UUID.randomUUID());
36233629
}
3624-
36253630
}
36263631
}
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)