Skip to content

Commit 888e5a6

Browse files
committed
DATAMONGO-1393 - Polishing.
Inline repositories into test. Apply Spring Data formatting rules. Remove final keyword from local variables. Remove unused XML namespace declarations. Original pull request: #350.
1 parent b03a38f commit 888e5a6

File tree

7 files changed

+64
-85
lines changed

7 files changed

+64
-85
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public ValidatingMongoEventListener(Validator validator) {
6060
public void onBeforeSave(Object source, DBObject dbo) {
6161

6262
LOG.debug("Validating object: {}", source);
63-
final Object target = source instanceof LazyLoadingProxy ? ((LazyLoadingProxy) source).getTarget() : source;
64-
final Set violations = validator.validate(target);
63+
Object target = source instanceof LazyLoadingProxy ? ((LazyLoadingProxy) source).getTarget() : source;
64+
Set violations = validator.validate(target);
6565

6666
if (!violations.isEmpty()) {
6767

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/Address.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,31 @@
2929
@Document(collection = "ADDRESS")
3030
public class Address {
3131

32-
@Id
33-
private ObjectId id;
34-
35-
@NotNull @Size(min = 2)
36-
private String name;
32+
@Id private ObjectId id;
33+
34+
@NotNull @Size(min = 2) private String name;
3735

3836
public Address() {
3937
super();
4038
}
39+
4140
public Address(String name) {
4241
this();
4342
this.name = name;
4443
}
44+
4545
public ObjectId getId() {
4646
return id;
4747
}
48+
4849
public void setId(ObjectId id) {
4950
this.id = id;
5051
}
52+
5153
public String getName() {
5254
return name;
5355
}
56+
5457
public void setName(String name) {
5558
this.name = name;
5659
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/User.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,59 +37,69 @@
3737
*/
3838
@Document(collection = "USER")
3939
public class User {
40-
40+
4141
public static class AddressRelation {
4242
public enum Type {
43-
PRIVATE,
44-
WORK
43+
PRIVATE, WORK
4544
}
45+
4646
private Type type = Type.PRIVATE;
47-
@DBRef(lazy = true)
48-
private Address address;
47+
@DBRef(lazy = true) private Address address;
48+
4949
// needed otherwise spring doesn't create the proxy for some reason ...
5050
public AddressRelation() {
5151
super();
5252
}
53+
5354
public AddressRelation(Type type, Address address) {
55+
5456
this();
57+
5558
this.type = type;
5659
this.address = address;
5760
}
61+
5862
public Type getType() {
5963
return type;
6064
}
65+
6166
public void setType(Type type) {
6267
this.type = type;
6368
}
69+
6470
public Address getAddress() {
6571
return address;
6672
}
73+
6774
public void setAddress(Address address) {
6875
this.address = address;
6976
}
7077
}
7178

72-
@Id
73-
private ObjectId id;
79+
@Id private ObjectId id;
80+
81+
@Size(min = 10) private String name;
7482

75-
@Size(min = 10)
76-
private String name;
83+
@Min(18) private Integer age;
7784

78-
@Min(18)
79-
private Integer age;
80-
8185
private List<AddressRelation> addresses = new ArrayList<AddressRelation>();
8286

8387
public User() {
8488
super();
8589
}
90+
8691
public User(String name, Integer age) {
92+
8793
this();
94+
8895
this.name = name;
8996
this.age = age;
9097
}
91-
public User(String name, Integer age, Address...addresses) {
98+
99+
public User(String name, Integer age, Address... addresses) {
100+
92101
this(name, age);
102+
93103
for (Address address : addresses) {
94104
this.addresses.add(new AddressRelation(Type.WORK, address));
95105
}
@@ -110,9 +120,11 @@ public String getName() {
110120
public Integer getAge() {
111121
return age;
112122
}
123+
113124
public List<AddressRelation> getAddresses() {
114125
return addresses;
115126
}
127+
116128
public void setAddresses(List<AddressRelation> addresses) {
117129
this.addresses = addresses;
118130
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListenerTests.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
*/
1616
package org.springframework.data.mongodb.core.mapping.event;
1717

18-
import static org.hamcrest.core.IsEqual.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

2121
import java.util.UUID;
2222

2323
import javax.validation.ConstraintViolationException;
2424

25+
import org.bson.types.ObjectId;
2526
import org.junit.ClassRule;
2627
import org.junit.Test;
2728
import org.junit.runner.RunWith;
2829
import org.springframework.beans.factory.annotation.Autowired;
2930
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
30-
import org.springframework.data.mongodb.core.mapping.event.repo.AddressRepository;
31-
import org.springframework.data.mongodb.core.mapping.event.repo.UserRepository;
31+
import org.springframework.data.mongodb.repository.MongoRepository;
3232
import org.springframework.data.mongodb.test.util.MongoVersionRule;
3333
import org.springframework.data.util.Version;
3434
import org.springframework.test.context.ContextConfiguration;
@@ -69,32 +69,50 @@ public void shouldThrowConstraintViolationException() {
6969
public void shouldNotThrowAnyExceptions() {
7070
userRepo.save(new User("john smith", 18));
7171
}
72-
72+
7373
/**
7474
* @see DATAMONGO-1393
7575
*/
7676
@Test
77-
public void validationOfLazyProxy() {
77+
public void validationOfLazyProxyShouldPass() {
78+
7879
Address address = addressRepo.insert(new Address(UUID.randomUUID().toString()));
7980
User user = userRepo.insert(new User("Foo Bar Name 123456", 18, address));
80-
81+
8182
user = userRepo.findOne(user.getId());
82-
assertTrue("Spring should use now a LazyLoadingProxy.", user.getAddresses().get(0).getAddress() instanceof LazyLoadingProxy);
83+
84+
assertThat(user.getAddresses().get(0).getAddress(), is(instanceOf(LazyLoadingProxy.class)));
85+
8386
address = user.getAddresses().get(0).getAddress();
8487
address.setName("New Fancy Address Name");
88+
8589
addressRepo.save(address);
8690
}
91+
8792
/**
8893
* @see DATAMONGO-1393
8994
*/
9095
@Test(expected = ConstraintViolationException.class)
91-
public void validationErrorTest() {
96+
public void validationOfLazyProxyShouldFail() {
97+
9298
Address address = addressRepo.insert(new Address(UUID.randomUUID().toString()));
9399
User user = userRepo.insert(new User("Foo Bar Name 123456", 18, address));
94-
100+
95101
user = userRepo.findOne(user.getId());
96102
address = user.getAddresses().get(0).getAddress();
103+
97104
address.setName(null);
105+
98106
addressRepo.save(address);
99107
}
108+
109+
/**
110+
* @author Paul Sterl
111+
*/
112+
public interface AddressRepository extends MongoRepository<Address, ObjectId> {}
113+
114+
/**
115+
* @author Paul Sterl
116+
*/
117+
public interface UserRepository extends MongoRepository<User, ObjectId> {}
100118
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/repo/AddressRepository.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/repo/UserRepository.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

spring-data-mongodb/src/test/resources/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListenerTests-context.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5-
xmlns:context="http://www.springframework.org/schema/context"
65
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
7-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
97

108
<mongo:db-factory dbname="validation" />
119

1210
<mongo:mapping-converter base-package="org.springframework.data.mongodb.core" />
13-
<mongo:repositories base-package="org.springframework.data.mongodb.core.mapping.event.repo" />
11+
<mongo:repositories base-package="org.springframework.data.mongodb.core.mapping.event" consider-nested-repositories="true"/>
1412

1513
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
1614
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

0 commit comments

Comments
 (0)