From f36407c1cf2accd4aabe6e74cb0f195f73357f1d Mon Sep 17 00:00:00 2001 From: Marcus Talbot <112508874+marcus-talbot42@users.noreply.github.com> Date: Wed, 9 Nov 2022 15:03:56 +0100 Subject: [PATCH] Prepared for BeanMapper(-Spring) update (#18) --- CHANGELOG.md | 6 +++ pom.xml | 5 +- .../autoconfigure/ApplicationScanner.java | 15 ++---- .../autoconfigure/BeanMapperAutoConfig.java | 49 +++++++++-------- .../autoconfigure/BeanMapperProperties.java | 12 ++--- .../BeanMapperAutoConfigTest.java | 53 +++++++++---------- 6 files changed, 70 insertions(+), 70 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60dba0e..6147807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed - BeanMapper v4.0.1 adds a new converter, as such, a few tests had to be updated to use the appropriate expected values. +## [4.0.0] - 2022-09-15 +### Upgrades +- Upgraded beanmapper(4.0.1), beanmapper-spring(4.0.1). +### Fixed +- BeanMapper v4.0.1 adds a new converter, as such, a few tests had to be updated to use the appropriate expected values. + ## [4.0.0] - 2022-09-15 ### Upgrades - Upgraded beanmapper(4.0.0), beanmapper-spring(4.0.0) and spring-boot(2.7.3) dependencies. diff --git a/pom.xml b/pom.xml index f1721b7..470b8e5 100644 --- a/pom.xml +++ b/pom.xml @@ -60,9 +60,8 @@ 17 2.7.3 - 4.0.1 - 4.0.1 - + 4.1.0 + 4.1.0 2.5.3 1.13.0 diff --git a/src/main/java/io/beanmapper/autoconfigure/ApplicationScanner.java b/src/main/java/io/beanmapper/autoconfigure/ApplicationScanner.java index 86f11eb..246eba7 100644 --- a/src/main/java/io/beanmapper/autoconfigure/ApplicationScanner.java +++ b/src/main/java/io/beanmapper/autoconfigure/ApplicationScanner.java @@ -2,7 +2,6 @@ import static io.beanmapper.utils.Classes.forName; -import java.io.IOException; import java.lang.annotation.Annotation; import java.util.Collections; import java.util.HashSet; @@ -21,8 +20,6 @@ import org.springframework.boot.autoconfigure.domain.EntityScanner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; -import org.springframework.core.type.classreading.MetadataReader; -import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.TypeFilter; /** @@ -89,14 +86,10 @@ private Set> findClasses(String basePackage, Class loo } private TypeFilter createTypeFilterForClass(Class clazz) { - return new TypeFilter() { - @Override - public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { - String className = metadataReader.getClassMetadata().getClassName(); - Class currentClass = Classes.forName(className); - return clazz.isAssignableFrom(currentClass); - } + return (metadataReader, metadataReaderFactory) -> { + String className = metadataReader.getClassMetadata().getClassName(); + Class currentClass = Classes.forName(className); + return clazz.isAssignableFrom(currentClass); }; } - } diff --git a/src/main/java/io/beanmapper/autoconfigure/BeanMapperAutoConfig.java b/src/main/java/io/beanmapper/autoconfigure/BeanMapperAutoConfig.java index 2cf8941..c4f9e10 100644 --- a/src/main/java/io/beanmapper/autoconfigure/BeanMapperAutoConfig.java +++ b/src/main/java/io/beanmapper/autoconfigure/BeanMapperAutoConfig.java @@ -1,11 +1,11 @@ package io.beanmapper.autoconfigure; import static java.util.Collections.singletonList; -import static org.springframework.beans.BeanUtils.instantiate; import static org.springframework.beans.BeanUtils.instantiateClass; import java.util.List; +import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.persistence.EntityManager; @@ -35,18 +35,16 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.ClassUtils; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * In no BeanMapper bean is found, it will be created with sensible webapplication/spring-data-jpa mapping defaults. - * It's possible to customize the BeanMapperBuilder by adding a bean of type {@link BeanMapperBuilderCustomizer} + * It's possible to customize the BeanMapperBuilder by adding a bean of type {@link BeanMapperBuilderCustomizer} * to your configuration. - * When a {@link MappingJackson2HttpMessageConverter} bean is found, a {@link MergedFormMethodArgumentResolver} + * When a {@link MappingJackson2HttpMessageConverter} bean is found, a {@link MergedFormMethodArgumentResolver} * will be added to the Spring MVC context. */ @Configuration @@ -55,16 +53,19 @@ public class BeanMapperAutoConfig { private final Logger log = LoggerFactory.getLogger(BeanMapperAutoConfig.class); - @Autowired - private BeanMapperProperties props; - @Autowired - private ApplicationContext applicationContext; - @Autowired(required = false) - private BeanMapperBuilderCustomizer builderCustomizer; + private final BeanMapperProperties props; + private final ApplicationContext applicationContext; + private final BeanMapperBuilderCustomizer builderCustomizer; private ApplicationScanner collectionHandlerAppScanner; private ApplicationScanner beanConverterAppScanner; + public BeanMapperAutoConfig(final BeanMapperProperties props, final ApplicationContext applicationContext, @Autowired(required = false) final BeanMapperBuilderCustomizer builderCustomizer) { + this.props = props; + this.applicationContext = applicationContext; + this.builderCustomizer = builderCustomizer; + } + @PostConstruct private void initApplicationScanner() { collectionHandlerAppScanner = new ApplicationScanner(applicationContext); @@ -162,7 +163,7 @@ private void addCustomBeanPairs(BeanMapperBuilder builder) { private void addLogicSecuredChecks(BeanMapperBuilder builder, String basePackage) { collectionHandlerAppScanner.findLogicSecuredCheckClasses(basePackage).forEach(cls -> { - LogicSecuredCheck logicSecuredCheck = instantiateClassAppContextOptional(cls, "logic secured check"); + LogicSecuredCheck logicSecuredCheck = instantiateClassAppContextOptional(cls, "logic secured check"); if (logicSecuredCheck != null) { builder.addLogicSecuredCheck(logicSecuredCheck); } @@ -171,7 +172,7 @@ private void addLogicSecuredChecks(BeanMapperBuilder builder, String basePackage private void addCollectionHandlers(BeanMapperBuilder builder, String basePackage) { collectionHandlerAppScanner.findCollectionHandlerClasses(basePackage).forEach(cls -> { - CollectionHandler collectionHandler = instantiateClassAppContextOptional(cls, "collection handler"); + CollectionHandler collectionHandler = instantiateClassAppContextOptional(cls, "collection handler"); if (collectionHandler != null) { builder.addCollectionHandler(collectionHandler); } @@ -234,20 +235,24 @@ private void customize(BeanMapperBuilder builder) { static class MergedFormConfig implements WebMvcConfigurer { private final Logger log = LoggerFactory.getLogger(MergedFormConfig.class); - @Autowired(required = false) - private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter; - @Autowired - private BeanMapper beanMapper; - @Autowired - private ApplicationContext applicationContext; - @Autowired(required = false) - private EntityManager entityManager; + private final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter; + private final BeanMapper beanMapper; + private final ApplicationContext applicationContext; + private final EntityManager entityManager; + + public MergedFormConfig(@Autowired(required = false) final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter, + final BeanMapper beanMapper, final ApplicationContext applicationContext, @Autowired(required = false) final EntityManager entityManager) { + this.mappingJackson2HttpMessageConverter = mappingJackson2HttpMessageConverter; + this.beanMapper = beanMapper; + this.applicationContext = applicationContext; + this.entityManager = entityManager; + } /** * If a {@link MappingJackson2HttpMessageConverter} bean is found, adds a {@link MergedFormMethodArgumentResolver} to the Spring MVC context. */ @Override - public void addArgumentResolvers(List argumentResolvers) { + public void addArgumentResolvers(@Nonnull List argumentResolvers) { if (mappingJackson2HttpMessageConverter != null) { log.info("Adding MergedFormArgumentResolver to MVC application."); argumentResolvers.add(new MergedFormMethodArgumentResolver( diff --git a/src/main/java/io/beanmapper/autoconfigure/BeanMapperProperties.java b/src/main/java/io/beanmapper/autoconfigure/BeanMapperProperties.java index 3764fe1..24265b8 100644 --- a/src/main/java/io/beanmapper/autoconfigure/BeanMapperProperties.java +++ b/src/main/java/io/beanmapper/autoconfigure/BeanMapperProperties.java @@ -21,9 +21,9 @@ public class BeanMapperProperties { */ private boolean useHibernateUnproxy = true; - private Boolean applyStrictMappingConvention = true; + private boolean applyStrictMappingConvention = true; - private Boolean applySecuredProperties = true; + private boolean applySecuredProperties = true; private String strictSourceSuffix = "Form"; @@ -45,19 +45,19 @@ public void setPackagePrefix(String basePackageName) { this.packagePrefix = basePackageName; } - public Boolean getApplyStrictMappingConvention() { + public boolean getApplyStrictMappingConvention() { return applyStrictMappingConvention; } - public void setApplyStrictMappingConvention(Boolean applyStrictMappingConvention) { + public void setApplyStrictMappingConvention(boolean applyStrictMappingConvention) { this.applyStrictMappingConvention = applyStrictMappingConvention; } - public Boolean getApplySecuredProperties() { + public boolean getApplySecuredProperties() { return applySecuredProperties; } - public void setApplySecuredProperties(Boolean applySecuredProperties) { + public void setApplySecuredProperties(boolean applySecuredProperties) { this.applySecuredProperties = applySecuredProperties; } diff --git a/src/test/java/io/beanmapper/autoconfigure/BeanMapperAutoConfigTest.java b/src/test/java/io/beanmapper/autoconfigure/BeanMapperAutoConfigTest.java index ab03f12..c58d41d 100644 --- a/src/test/java/io/beanmapper/autoconfigure/BeanMapperAutoConfigTest.java +++ b/src/test/java/io/beanmapper/autoconfigure/BeanMapperAutoConfigTest.java @@ -1,6 +1,5 @@ package io.beanmapper.autoconfigure; -import static java.util.stream.Collectors.toList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -53,28 +52,28 @@ public void tearDown() { @Test public void autoconfig_shouldCreateBeanMapper_ifNotExists() { loadApplicationContext(); - assertBeanMapper(1, 17); + assertBeanMapper(1, 20); assertMergedFormArgResolver(); } @Test public void autoconfig_shouldCreateCustomizedBeanMapper_ifNotExists() { loadApplicationContext(ConfigWithBeanMapperBuilderCustomizer.class); - assertBeanMapper(1, 18); + assertBeanMapper(1, 21); assertMergedFormArgResolver(); } @Test public void autoconfig_shouldNotCreateBeanMapper_ifAlreadyExists() { loadApplicationContext(ConfigWithBeanMapper.class); - assertBeanMapper(0, 12, false); + assertBeanMapper(0, 15, false); assertMergedFormArgResolver(); } @Test public void autoconfig_shouldCreateBeanMapper_withDefaultUnproxy_whenEnvIsSet() { loadApplicationContext(BEANMAPPER_USE_HIBERNATE_UNPROXY_PROP); - assertBeanMapper(1, 17, false); + assertBeanMapper(1, 20, false); assertMergedFormArgResolver(); } @@ -82,14 +81,14 @@ public void autoconfig_shouldCreateBeanMapper_withDefaultUnproxy_whenEnvIsSet() public void autoconfig_shouldRegisterCollectionHandler_ifScanned() { loadApplicationContext(BEANMAPPER_USE_HIBERNATE_UNPROXY_PROP); BeanMapper mapper = context.getBean(BeanMapper.class); - List customCollectionHandlers = mapper.configuration().getCollectionHandlers() + List customCollectionHandlers = mapper.getConfiguration().getCollectionHandlers() .stream() .filter(handler -> handler.getType().equals(TestEntity.class) || handler.getType().equals(TestEntity2.class)) - .collect(toList()); + .toList(); assertEquals(2, customCollectionHandlers.size()); - TestCollectionHandlerWithAppCtx collectionHandler = (TestCollectionHandlerWithAppCtx)mapper.configuration().getCollectionHandlerFor(TestEntity2.class); + TestCollectionHandlerWithAppCtx collectionHandler = (TestCollectionHandlerWithAppCtx)mapper.getConfiguration().getCollectionHandlerFor(TestEntity2.class); assertNotNull(collectionHandler.getApplicationContext()); } @@ -98,7 +97,7 @@ public void autoconfig_shouldSetSecurityChecks() { loadApplicationContext(ConfigWithSecurity.class); BeanMapper mapper = context.getBean(BeanMapper.class); - assertTrue(mapper.configuration().getRoleSecuredCheck() instanceof SpringRoleSecuredCheck); + assertTrue(mapper.getConfiguration().getRoleSecuredCheck() instanceof SpringRoleSecuredCheck); } @@ -108,17 +107,17 @@ public void autoconfig_shouldNotSetSecurityChecks_ifSpringSecurityIsMissingFromC loadApplicationContext(ConfigWithSecurity.class, new NoSpringSecurityClassLoader()); BeanMapper mapper = context.getBean(BeanMapper.class); - assertFalse(mapper.configuration().getRoleSecuredCheck() instanceof SpringRoleSecuredCheck); + assertFalse(mapper.getConfiguration().getRoleSecuredCheck() instanceof SpringRoleSecuredCheck); } @Test public void autoconfig_shouldLoadIdToEntityBeanConverterAndHibernateUnproxy_withSpringDataOnClassPath() { loadApplicationContext(ConfigWithSpringData.class); BeanMapper mapper = context.getBean(BeanMapper.class); - List beanConverters = mapper.configuration().getBeanConverters(); + List beanConverters = mapper.getConfiguration().getBeanConverters(); assertTrue(beanConverters.stream().anyMatch(c -> c instanceof IdToEntityBeanConverter)); - BeanUnproxy unproxyDelegate = (BeanUnproxy) getField(mapper.configuration().getBeanUnproxy(), "delegate"); + BeanUnproxy unproxyDelegate = (BeanUnproxy) getField(mapper.getConfiguration().getBeanUnproxy(), "delegate"); assertTrue(unproxyDelegate instanceof HibernateAwareBeanUnproxy); } @@ -126,10 +125,10 @@ public void autoconfig_shouldLoadIdToEntityBeanConverterAndHibernateUnproxy_with public void autoconfig_shouldNotLoadIdToEntityBeanConverterAndHibernateUnproxy_withoutSpringDataOnClassPath() { loadApplicationContext(ConfigWithSpringData.class, new NoSpringDataClassLoader()); BeanMapper mapper = context.getBean(BeanMapper.class); - List beanConverters = mapper.configuration().getBeanConverters(); + List beanConverters = mapper.getConfiguration().getBeanConverters(); assertFalse(beanConverters.stream().anyMatch(c -> c instanceof IdToEntityBeanConverter)); - BeanUnproxy unproxyDelegate = (BeanUnproxy) getField(mapper.configuration().getBeanUnproxy(), "delegate"); + BeanUnproxy unproxyDelegate = (BeanUnproxy) getField(mapper.getConfiguration().getBeanUnproxy(), "delegate"); assertFalse(unproxyDelegate instanceof HibernateAwareBeanUnproxy); assertTrue(unproxyDelegate instanceof DefaultBeanUnproxy); } @@ -169,21 +168,19 @@ static class ConfigWithBeanMapperBuilderCustomizer { @Bean public BeanMapperBuilderCustomizer beanMapperCustomizer() { - return builder -> { - builder.addConverter(new BeanConverter() { + return builder -> builder.addConverter(new BeanConverter() { - @Override - public Object convert(BeanMapper beanMapper, Object source, Class targetClass, BeanPropertyMatch beanFieldMatch) { - return null; - } + @Override + public T convert(BeanMapper beanMapper, S source, Class targetClass, BeanPropertyMatch beanFieldMatch) { + return null; + } - @Override - public boolean match(Class sourceClass, Class targetClass) { - return false; - } + @Override + public boolean match(Class sourceClass, Class targetClass) { + return false; + } - }); - }; + }); } } @@ -194,7 +191,7 @@ private void assertBeanMapper(int expectedNumberOfPackagePrefixes, int expectedN private void assertBeanMapper(int expectedNumberOfPackagePrefixes, int expectedNumberOfConverters, boolean hibernateUnproxy) { BeanMapper mapper = context.getBean(BeanMapper.class); - io.beanmapper.config.Configuration config = mapper.configuration(); + io.beanmapper.config.Configuration config = mapper.getConfiguration(); assertEquals(expectedNumberOfPackagePrefixes, config.getPackagePrefixes().size()); if (expectedNumberOfPackagePrefixes == 1) { assertEquals("io.beanmapper.autoconfigure", config.getPackagePrefixes().get(0)); @@ -213,7 +210,7 @@ private void assertMergedFormArgResolver() { List argResolvers = requestHandler.getArgumentResolvers() .stream() .filter(argResolver -> MergedFormMethodArgumentResolver.class == argResolver.getClass()) - .collect(toList()); + .toList(); assertEquals(1, argResolvers.size()); }