From 6593fef803d7e423866bfaf2852463aa0af90586 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 28 Feb 2017 18:03:28 +0100 Subject: [PATCH] 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. --- .../data/mongodb/core/MongoTemplateTests.java | 27 ++++++++------ .../core/PersonWithIdPropertyOfTypeUUID.java | 37 ++----------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 2a351d1ce..fe1d068c8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2016 the original author or authors. + * Copyright 2011-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -138,8 +138,10 @@ public class MongoTemplateTests { @Autowired public void setApplicationContext(ConfigurableApplicationContext context) { - context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener()); + this.context = context; + + context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener()); } @Autowired @@ -149,12 +151,12 @@ public class MongoTemplateTests { Arrays.asList(DateToDateTimeConverter.INSTANCE, DateTimeToDateConverter.INSTANCE)); MongoMappingContext mappingContext = new MongoMappingContext(); - mappingContext.setInitialEntitySet(new HashSet>(Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class, - PersonWith_idPropertyOfTypeString.class, PersonWithIdPropertyOfTypeObjectId.class, - PersonWithIdPropertyOfTypeString.class, PersonWithIdPropertyOfTypeInteger.class, - PersonWithIdPropertyOfTypeBigInteger.class, PersonWithIdPropertyOfPrimitiveInt.class, - PersonWithIdPropertyOfTypeLong.class, PersonWithIdPropertyOfPrimitiveLong.class, - PersonWithIdPropertyOfTypeUUID.class))); + mappingContext.setInitialEntitySet(new HashSet>( + Arrays.asList(PersonWith_idPropertyOfTypeObjectId.class, PersonWith_idPropertyOfTypeString.class, + PersonWithIdPropertyOfTypeObjectId.class, PersonWithIdPropertyOfTypeString.class, + PersonWithIdPropertyOfTypeInteger.class, PersonWithIdPropertyOfTypeBigInteger.class, + PersonWithIdPropertyOfPrimitiveInt.class, PersonWithIdPropertyOfTypeLong.class, + PersonWithIdPropertyOfPrimitiveLong.class, PersonWithIdPropertyOfTypeUUID.class))); mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder()); mappingContext.initialize(); @@ -168,8 +170,11 @@ public class MongoTemplateTests { @Before public void setUp() { + cleanDb(); queryMongoVersionIfNecessary(); + + this.mappingTemplate.setApplicationContext(context); } @After @@ -659,7 +664,6 @@ public class MongoTemplateTests { PersonWithIdPropertyOfTypeUUID p13 = new PersonWithIdPropertyOfTypeUUID(); p13.setFirstName("Sven_10"); p13.setAge(22); - p13.setId(UUID.randomUUID()); // insert mongoTemplate.insert(p13); // also try save @@ -3629,10 +3633,12 @@ public class MongoTemplateTests { Object value; } - static class PersonWithIdPropertyOfTypeUUIDListener extends AbstractMongoEventListener { + static class PersonWithIdPropertyOfTypeUUIDListener + extends AbstractMongoEventListener { @Override public void onBeforeConvert(BeforeConvertEvent event) { + PersonWithIdPropertyOfTypeUUID person = event.getSource(); if (person.getId() != null) { @@ -3641,6 +3647,5 @@ public class MongoTemplateTests { person.setId(UUID.randomUUID()); } - } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithIdPropertyOfTypeUUID.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithIdPropertyOfTypeUUID.java index 46a5a6f87..e50fd3f28 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithIdPropertyOfTypeUUID.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithIdPropertyOfTypeUUID.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2017 the original author or authors. + * Copyright 2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,43 +15,14 @@ */ package org.springframework.data.mongodb.core; +import lombok.Data; + import java.util.UUID; +@Data public class PersonWithIdPropertyOfTypeUUID { private UUID id; - private String firstName; - private int age; - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - @Override - public String toString() { - return "PersonWithIdPropertyOfTypeUUID [id=" + id + ", firstName=" + firstName + ", age=" + age + "]"; - } - }