-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
403 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...e-spring-boot-starter/src/main/java/ml/wonwoo/autoconfigure/DefaultAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
mpple-spring-boot-starter/src/main/java/ml/wonwoo/autoconfigure/DozerAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
mpple-spring-boot-starter/src/main/java/ml/wonwoo/autoconfigure/MapperCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
mpple-spring-boot-starter/src/main/java/ml/wonwoo/autoconfigure/MapperType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
mpple-spring-boot-starter/src/main/java/ml/wonwoo/autoconfigure/dozer/DozerCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
32 changes: 32 additions & 0 deletions
32
mpple-spring-boot-starter/src/main/java/ml/wonwoo/autoconfigure/dozer/DozerFactoryBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ng-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
4 changes: 3 additions & 1 deletion
4
mpple-spring-boot-starter/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
35 changes: 35 additions & 0 deletions
35
...ing-boot-starter/src/test/java/ml/wonwoo/autoconfigure/DefaultAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...pring-boot-starter/src/test/java/ml/wonwoo/autoconfigure/DozerAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...boot-starter/src/test/java/ml/wonwoo/autoconfigure/ModelMapperAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
...pring-boot-starter/src/test/java/ml/wonwoo/autoconfigure/MppleAutoConfigurationTests.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.