Skip to content

Commit

Permalink
#5 properties 선택 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
wonwoo committed Oct 21, 2018
1 parent b840cf3 commit c28183e
Show file tree
Hide file tree
Showing 19 changed files with 403 additions and 32 deletions.
17 changes: 17 additions & 0 deletions mpple-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@
<dependency>
<groupId>ml.wonwoo</groupId>
<artifactId>mpple-modelmapper</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>ml.wonwoo</groupId>
<artifactId>mpple-dozer</artifactId>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ml.wonwoo.autoconfigure;

import ml.wonwoo.mapped.DefaultMapped;
import ml.wonwoo.mapped.Mapped;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
@Conditional(MapperCondition.class)
public class DefaultAutoConfiguration {

@Bean
@ConditionalOnMissingBean(Mapped.class)
public Mapped mapped() {
return new DefaultMapped();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ml.wonwoo.autoconfigure;

import java.util.List;
import ml.wonwoo.autoconfigure.dozer.DozerCustomizer;
import ml.wonwoo.autoconfigure.dozer.DozerFactoryBean;
import ml.wonwoo.dozer.mapped.DozerMapped;
import ml.wonwoo.mapped.Mapped;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(DozerMapped.class)
@Conditional(MapperCondition.class)
public class DozerAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public FactoryBean<DozerBeanMapper> mapperFactoryBean(ObjectProvider<List<DozerCustomizer>> provider) {
DozerFactoryBean dozerFactoryBean = new DozerFactoryBean();
dozerFactoryBean.setDozerCustomizers(provider.getIfAvailable());
return dozerFactoryBean;
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(Mapper.class)
public Mapped mapped(Mapper mapper) {
DozerMapped dozerMapped = new DozerMapped();
dozerMapped.setMapper(mapper);
return dozerMapped;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ml.wonwoo.autoconfigure;

import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;

class MapperCondition extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String sourceClass = "";
if (metadata instanceof ClassMetadata) {
sourceClass = ((ClassMetadata) metadata).getClassName();
}
ConditionMessage.Builder message = ConditionMessage.forCondition("Mapper", sourceClass);
MapperType specified = Binder.get(context.getEnvironment())
.bind("mpple.mapper.type", MapperType.class)
.orElseGet(() -> MapperType.NONE);
MapperType storeType = MapperType.getType(((AnnotationMetadata) metadata).getClassName());
if (specified == storeType) {
return ConditionOutcome.match(message.because(specified + " mapper type"));
}
return ConditionOutcome.noMatch(message.because("unknown mapper type"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ml.wonwoo.autoconfigure;

public enum MapperType {

NONE(DefaultAutoConfiguration.class),
MODELMAPPER(ModelMapperAutoConfiguration.class),
DOZER(DozerAutoConfiguration.class);

private final Class<?> configClass;

MapperType(Class<?> configClass) {
this.configClass = configClass;
}

public static MapperType getType(String configClass) {
for (MapperType mapperType : values()) {
if (mapperType.configClass != null) {
if (mapperType.configClass.getName().equals(configClass)) {
return mapperType;
}
}
}
throw new IllegalArgumentException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
import ml.wonwoo.autoconfigure.modelmapper.ModelMapperCustomizer;
import ml.wonwoo.autoconfigure.modelmapper.ModelMapperFactoryBean;
import ml.wonwoo.autoconfigure.modelmapper.PropertyMapCustomizer;
import ml.wonwoo.mapped.Mapped;
import ml.wonwoo.modelmapper.mapped.ModelMapperMapped;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@ConditionalOnClass(ModelMapperMapped.class)
@Conditional(MapperCondition.class)
@Configuration
@ConditionalOnClass(ModelMapper.class)
public class MppleAutoConfiguration {
public class ModelMapperAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public ModelMapperMapped modelMapperMapped(ModelMapper modelMapper) {
public Mapped mapped(ModelMapper modelMapper) {
ModelMapperMapped modelMapperMapped = new ModelMapperMapped();
modelMapperMapped.setModelMapper(modelMapper);
return modelMapperMapped;
}

@Bean
public ModelMapperFactoryBean modelMapperFactoryBean(ObjectProvider<List<ModelMapperCustomizer>> provider) {
@ConditionalOnMissingBean
public FactoryBean<ModelMapper> mapperFactoryBean(ObjectProvider<List<ModelMapperCustomizer>> provider) {
ModelMapperFactoryBean modelMapperFactoryBean = new ModelMapperFactoryBean();
modelMapperFactoryBean.setMapperCustomizers(provider.getIfAvailable());
return modelMapperFactoryBean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ml.wonwoo.autoconfigure.dozer;

import org.dozer.DozerBeanMapper;

@FunctionalInterface
public interface DozerCustomizer {

void customize(DozerBeanMapper dozerBeanMapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ml.wonwoo.autoconfigure.dozer;

import java.util.List;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.FactoryBean;

public class DozerFactoryBean implements FactoryBean<DozerBeanMapper> {

private List<DozerCustomizer> dozerCustomizers;

@Override
public DozerBeanMapper getObject() {
DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
configure(dozerBeanMapper);
return dozerBeanMapper;
}

@Override
public Class<?> getObjectType() {
return DozerBeanMapper.class;
}

private void configure(DozerBeanMapper dozerBeanMapper) {
if (dozerCustomizers != null) {
dozerCustomizers.forEach(dozerCustomizer -> dozerCustomizer.customize(dozerBeanMapper));
}
}

public void setDozerCustomizers(List<DozerCustomizer> dozerCustomizers) {
this.dozerCustomizers = dozerCustomizers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void registerBeanDefinitionMppled(AnnotationMetadata metadata, BeanDef
BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(MppleFactoryBean.class);
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
definition.addPropertyValue("type", className);
definition.addPropertyValue("beanName", "modelMapperMapped");
definition.addPropertyValue("beanName", "mapped");
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, beanDefinitionRegistry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"properties": [
{
"name": "mpple.mapper.type",
"type": "ml.wonwoo.autoconfigure.MapperType",
"description": "mapper.type",
"defaultValue": "none"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
ml.wonwoo.autoconfigure.MppleAutoConfiguration,\
ml.wonwoo.autoconfigure.ModelMapperAutoConfiguration,\
ml.wonwoo.autoconfigure.DozerAutoConfiguration,\
ml.wonwoo.autoconfigure.DefaultAutoConfiguration,\
ml.wonwoo.autoconfigure.MppledsRegistrarAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ml.wonwoo.autoconfigure;

import static org.assertj.core.api.Assertions.assertThat;

import ml.wonwoo.mapped.DefaultMapped;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class DefaultAutoConfigurationTests {

private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(DefaultAutoConfiguration.class));

@Test
public void registersAutomatically() {
runner
.run(context -> assertThat(context).hasSingleBean(DefaultMapped.class));
}

@Test
public void registersAutomaticallyNone() {
runner
.withPropertyValues("mpple.mapper.type=none")
.run(context -> assertThat(context).hasSingleBean(DefaultMapped.class));
}

@Test
public void registersAutomaticallyMiss() {
runner
.withPropertyValues("mpple.mapper.type=modelmapper")
.run(context -> assertThat(context).doesNotHaveBean(DefaultMapped.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ml.wonwoo.autoconfigure;

import static org.assertj.core.api.Assertions.assertThat;

import ml.wonwoo.dozer.mapped.DozerMapped;
import org.dozer.DozerBeanMapper;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class DozerAutoConfigurationTests {

private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(DozerAutoConfiguration.class));

@Test
public void registersAutomatically() {
runner
.withPropertyValues("mpple.mapper.type=dozer")
.run(context -> {
assertThat(context).hasSingleBean(DozerBeanMapper.class);
assertThat(context).hasSingleBean(DozerMapped.class);
});
}

@Test
public void registersAutomaticallyMiss() {
runner
.withPropertyValues("mpple.mapper.type=modelmapper")
.run(context -> assertThat(context).doesNotHaveBean(DozerBeanMapper.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ml.wonwoo.autoconfigure;

import static org.assertj.core.api.Assertions.assertThat;

import ml.wonwoo.modelmapper.mapped.ModelMapperMapped;
import org.junit.Test;
import org.modelmapper.ModelMapper;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class ModelMapperAutoConfigurationTests {

private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(ModelMapperAutoConfiguration.class));

@Test
public void registersAutomatically() {
runner
.withPropertyValues("mpple.mapper.type=modelmapper")
.run(context -> {
assertThat(context).hasSingleBean(ModelMapper.class);
assertThat(context).hasSingleBean(ModelMapperMapped.class);
});
}

@Test
public void registersAutomaticallyMiss() {
runner
.withPropertyValues("mpple.mapper.type=dozer")
.run(context -> assertThat(context).doesNotHaveBean(ModelMapper.class));
}
}

This file was deleted.

Loading

0 comments on commit c28183e

Please sign in to comment.