From cfcf2fea5cbd36a64f56525fc986910e73f37d80 Mon Sep 17 00:00:00 2001 From: Tiago da Costa Peixoto Date: Sun, 24 Feb 2019 22:10:27 -0300 Subject: [PATCH] refactor: add issue #233 fix test --- pom.xml | 2 +- .../DynamoDBRepositoryConfigExtension.java | 14 ++- .../config/DynamoDBMapperConfigTest.java | 96 +++++++++++++++++++ 3 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBMapperConfigTest.java diff --git a/pom.xml b/pom.xml index 47f39d06..77a437a9 100755 --- a/pom.xml +++ b/pom.xml @@ -438,7 +438,7 @@ com.github.spotbugs spotbugs-maven-plugin - 3.1.5 + 3.1.11 Max diff --git a/src/main/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBRepositoryConfigExtension.java b/src/main/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBRepositoryConfigExtension.java index dd52c2da..c641ce6b 100644 --- a/src/main/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBRepositoryConfigExtension.java +++ b/src/main/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBRepositoryConfigExtension.java @@ -15,10 +15,6 @@ */ package org.socialsignin.spring.data.dynamodb.repository.config; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.socialsignin.spring.data.dynamodb.core.DynamoDBTemplate; @@ -38,6 +34,11 @@ import org.springframework.util.StringUtils; import org.w3c.dom.Element; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + /** * @author Michael Lavelle * @author Sebastian Just @@ -77,7 +78,7 @@ public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfi /* * (non-Javadoc) - * + * * @see org.springframework.data.repository.config. * RepositoryConfigurationExtensionSupport * #postProcess(org.springframework.beans @@ -100,7 +101,6 @@ public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfiguratio defaultDynamoDBMappingContext = registerDynamoDBMappingContext(registry); } dynamoDBMappingContextRef = defaultDynamoDBMappingContext; - } registerAndSetPostProcessingBeans(builder, registry, dynamoDBMappingContextRef); } @@ -150,7 +150,6 @@ private void postProcess(BeanDefinitionBuilder builder, String repositoryName, S defaultDynamoDBMappingContext = registerDynamoDBMappingContext(registry); } dynamoDBMappingContextRef = defaultDynamoDBMappingContext; - } builder.addPropertyReference("dynamoDBMappingContext", dynamoDBMappingContextRef); @@ -250,5 +249,4 @@ protected String getBeanNameWithModulePrefix(String baseBeanName) { protected String getModulePrefix() { return "dynamoDB"; } - } diff --git a/src/test/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBMapperConfigTest.java b/src/test/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBMapperConfigTest.java new file mode 100644 index 00000000..2d88bf8f --- /dev/null +++ b/src/test/java/org/socialsignin/spring/data/dynamodb/repository/config/DynamoDBMapperConfigTest.java @@ -0,0 +1,96 @@ +/** + * Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.socialsignin.spring.data.dynamodb.repository.config; + +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.UnsatisfiedDependencyException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Test Issue #233 fix + */ +public class DynamoDBMapperConfigTest { + + static class BaseTestConfig { + @Bean + public AmazonDynamoDB amazonDynamoDB() { + return Mockito.mock(AmazonDynamoDB.class); + } + + @Bean + public DynamoDBMapper dynamoDBMapper() { + return Mockito.mock(DynamoDBMapper.class); + } + } + + @Configuration + @EnableDynamoDBRepositories(dynamoDBMapperConfigRef = "customDynamoDBMapperConfig") + static class TestConfigWithDynamoDBMapperConfigRef extends BaseTestConfig { + @Bean("customDynamoDBMapperConfig") + public DynamoDBMapperConfig dynamoDBMapperConfig() { + return DynamoDBMapperConfig.DEFAULT; + } + } + + @Configuration + @EnableDynamoDBRepositories + static class TestConfigWithoutDynamoDBMapperConfigRef extends TestConfigWithDynamoDBMapperConfigRef { + } + + @Configuration + @EnableDynamoDBRepositories + static class TestConfigWithoutDynamoDBMapperConfigBean extends BaseTestConfig { + } + + @Test + public void testConfigWithDynamoDBMapperConfigRef() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + TestConfigWithDynamoDBMapperConfigRef.class); + + boolean containsBean = ctx.containsBean("customDynamoDBMapperConfig"); + assertTrue(containsBean); + } + + @Test(expected = UnsatisfiedDependencyException.class) + public void testConfigWithoutDynamoDBMapperConfigRef() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + TestConfigWithoutDynamoDBMapperConfigRef.class); + + boolean containsBean = ctx.containsBean("customDynamoDBMapperConfig"); + assertTrue(containsBean); + } + + @Test + public void testConfigWithoutDynamoDBMapperConfigBean() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + TestConfigWithoutDynamoDBMapperConfigBean.class); + + boolean containsBean = ctx.containsBean("customDynamoDBMapperConfig"); + assertFalse(containsBean); + + containsBean = ctx.containsBean("dynamoDB-DynamoDBMapperConfig"); + assertTrue(containsBean); + } +}