From 827b6204a9268d2d845bae0f8840f68ada76aec3 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 | 25 ++++++++----- .../core/PersonWithIdPropertyOfTypeUUID.java | 37 ++----------------- 2 files changed, 19 insertions(+), 43 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 5ebb9f1eb..24b8fd843 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 @@ -143,8 +143,10 @@ public class MongoTemplateTests { @Autowired public void setApplicationContext(ConfigurableApplicationContext context) { - context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener()); + this.context = context; + + context.addApplicationListener(new PersonWithIdPropertyOfTypeUUIDListener()); } @Autowired @@ -154,12 +156,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(); @@ -173,8 +175,11 @@ public class MongoTemplateTests { @Before public void setUp() { + cleanDb(); queryMongoVersionIfNecessary(); + + this.mappingTemplate.setApplicationContext(context); } @After @@ -643,7 +648,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 @@ -3609,10 +3613,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) { @@ -3621,6 +3627,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 + "]"; - } - }