Skip to content

Commit

Permalink
Merge 3057bc0 into 5f4d6fa
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jul 7, 2018
2 parents 5f4d6fa + 3057bc0 commit e81969e
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 92 deletions.
20 changes: 14 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.tngtech.java</groupId>
<artifactId>config-builder</artifactId>
<version>1.5.2-SNAPSHOT</version>
<version>1.6.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Config-Builder</name>
<description>The Config Builder creates fully configured instances of config classes, using values from various sources like properties files, command line arguments etc.</description>
Expand Down Expand Up @@ -67,8 +67,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -100,7 +100,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -113,7 +113,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<version>0.8.1</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand All @@ -127,6 +127,14 @@
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<dependencies>
<dependency>
<!-- this fixes https://github.com/trautonen/coveralls-maven-plugin/issues/112 -->
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down Expand Up @@ -164,7 +172,7 @@
<dependency>
<groupId>com.tngtech.java</groupId>
<artifactId>property-loader</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/tngtech/configbuilder/ConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,16 @@ public ConfigBuilder<T> withPropertyLocations(Object ... propertyLocations) {
* @param propertyFilters property filters which should be applied after loading properties
* @return the instance of ConfigBuilder
*/
public ConfigBuilder<T> withPropertyFilters(Class<? extends PropertyLoaderFilter> ... propertyFilters) {
@SafeVarargs
public final ConfigBuilder<T> withPropertyFilters(Class<? extends PropertyLoaderFilter>... propertyFilters) {
final DefaultPropertyFilterContainer filterContainer = propertyLoader.getFilters();
final List<PropertyLoaderFilter> filters = filterContainer.getFilters();
filters.clear();

for (Class<? extends PropertyLoaderFilter> propertyFilter : propertyFilters) {
try {
filters.add(propertyFilter.newInstance());
} catch (InstantiationException e) {
LOGGER.error("could not create filter '{}'", propertyFilter.getSimpleName(), e);
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
LOGGER.error("could not create filter '{}'", propertyFilter.getSimpleName(), e);
}
}
Expand Down Expand Up @@ -295,6 +294,6 @@ private void initializeErrorMessageSetup(PropertyLoader propertyLoader) {
* @return ConfigBuilder instance for config class
*/
public static <T> ConfigBuilder<T> on(Class<T> clazz) {
return new ConfigBuilder<T>(clazz);
return new ConfigBuilder<>(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,15 @@ private Map<Class<? extends PropertyLoaderFilter>, Action> createFilterMap( fina
filterContainer ) {
Map<Class<? extends PropertyLoaderFilter>, Action> actionMap = Maps.newHashMap();

actionMap.put( VariableResolvingFilter.class, new Action() {
@Override
public void execute() {
filterContainer.withVariableResolvingFilter();
}
} );
actionMap.put( VariableResolvingFilter.class, () -> filterContainer.withVariableResolvingFilter());

actionMap.put( DecryptingFilter.class, new Action() {
@Override
public void execute() {
filterContainer.withDecryptingFilter();
}
} );
actionMap.put( DecryptingFilter.class, () -> filterContainer.withDecryptingFilter());

actionMap.put( EnvironmentResolvingFilter.class, new Action() {
@Override
public void execute() {
filterContainer.withEnvironmentResolvingFilter();
}
} );
actionMap.put( EnvironmentResolvingFilter.class, () -> filterContainer.withEnvironmentResolvingFilter());

actionMap.put( WarnOnSurroundingWhitespace.class, new Action() {
@Override
public void execute() {
filterContainer.withWarnOnSurroundingWhitespace();
}
} );
actionMap.put( WarnOnSurroundingWhitespace.class, () -> filterContainer.withWarnOnSurroundingWhitespace());

actionMap.put( ThrowIfPropertyHasToBeDefined.class, new Action() {
@Override
public void execute() {
filterContainer.withWarnIfPropertyHasToBeDefined();
}
} );
actionMap.put( ThrowIfPropertyHasToBeDefined.class, () -> filterContainer.withWarnIfPropertyHasToBeDefined());

return actionMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import java.util.ArrayList;
import java.util.Collection;

public class CollectionToArrayListTransformer extends TypeTransformer<Collection,ArrayList> {
public class CollectionToArrayListTransformer extends TypeTransformer<Collection, ArrayList> {

@Override
public ArrayList transform(Collection argument) {
ArrayList result = Lists.newArrayList();
for(Object value : argument) {
for (Object value : argument) {
result.add(fieldValueTransformer.performNecessaryTransformations(value, ((ParameterizedType) targetType).getActualTypeArguments()[0]));
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.util.Collection;
import java.util.HashSet;

public class CollectionToHashSetTransformer extends TypeTransformer<Collection,HashSet> {
public class CollectionToHashSetTransformer extends TypeTransformer<Collection, HashSet> {

@Override
public HashSet transform(Collection argument) {
HashSet result = Sets.newHashSet();
for(Object value : argument) {
for (Object value : argument) {
result.add(fieldValueTransformer.performNecessaryTransformations(value, ((ParameterizedType) targetType).getActualTypeArguments()[0]));
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.util.Collection;

public class StringCollectionToCommaSeparatedStringTransformer extends TypeTransformer<Collection<String>, String> {

@Override
public String transform(Collection<String> argument) {
Joiner joiner = Joiner.on((String)additionalOptions[0]);
Joiner joiner = Joiner.on((String) additionalOptions[0]);
return joiner.join(argument);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private Type[] determineTypeArguments() {
}

private Map<TypeVariable, Type> buildTypeNameMap(Type[] typeArguments, TypeVariable[] typeParameters) {
Map<TypeVariable, Type> typeVariableMap = new HashMap<TypeVariable, Type>();
Map<TypeVariable, Type> typeVariableMap = new HashMap<>();
for (int i = 0; i < typeParameters.length; i++) {
typeVariableMap.put(typeParameters[i], typeArguments[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public Object getValue(Annotation annotation, ConfigBuilderFactory configBuilder
Field field = importedConfiguration.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
result = field.get(importedConfiguration);
} catch (NoSuchFieldException e) {
throw createException(configBuilderFactory, fieldName);
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException | IllegalAccessException e) {
throw createException(configBuilderFactory, fieldName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ public class ValidatorException extends RuntimeException {

public <T> ValidatorException(String message, Set<ConstraintViolation<T>> constraintViolations) {
super(message);
this.constraintViolations = Sets.newHashSet(Iterables.transform(constraintViolations, new Function<ConstraintViolation<T>, ConstraintViolation>() {
@Override
public ConstraintViolation apply(ConstraintViolation<T> constraintViolation) {
return constraintViolation;
}
}));
this.constraintViolations = Sets.newHashSet(Iterables.transform(constraintViolations, (Function<ConstraintViolation<T>, ConstraintViolation>) constraintViolation -> constraintViolation));
}

public ValidatorException(String message, Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ public Options getOptions(Class configClass) {
private Option getOption(Field field) {
CommandLineValue commandLineValue = field.getAnnotation(CommandLineValue.class);
log.debug("adding command line option {} for field {}", commandLineValue.shortOpt(), field.getName());
return OptionBuilder.withLongOpt(commandLineValue.longOpt())
return Option.builder(commandLineValue.shortOpt())
.longOpt(commandLineValue.longOpt())
.hasArg()
.isRequired(commandLineValue.required())
.withDescription(commandLineValue.description())
.required(commandLineValue.required())
.desc(commandLineValue.description())
.hasArg(commandLineValue.hasArg())
.create(commandLineValue.shortOpt());
.build();
}

private CommandLine parseCommandLine(String[] args, Options options) {
CommandLine commandLine;
try {
commandLine = configBuilderFactory.createInstance(GnuParser.class).parse(options, args);
commandLine = configBuilderFactory.createInstance(DefaultParser.class).parse(options, args);
} catch (ParseException e) {
throw new ConfigBuilderException(errorMessageSetup.getErrorMessage(e.getClass().getSuperclass()), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public T getInstance(Class<T> configClass, Object... objects) {
log.debug("found constructor - instantiating {}", configClass.getName());
tConstructor.setAccessible(true);
return tConstructor.newInstance(objects);
} catch (InstantiationException e) {
throw createConfigBuilderException(e);
} catch (IllegalAccessException e) {
throw createConfigBuilderException(e);
} catch (InvocationTargetException e) {
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
throw createConfigBuilderException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class FieldValueTransformer {
private Object[] additionalOptions;

//Order is important: Prefer List over Set if both apply!
private final ArrayList<Class<? extends TypeTransformer>> defaultTransformers = Lists.<Class<? extends TypeTransformer>>newArrayList(
private final ArrayList<Class<? extends TypeTransformer>> defaultTransformers = Lists.newArrayList(
StringOrPrimitiveToPrimitiveTransformer.class,
CharacterSeparatedStringToStringListTransformer.class,
CharacterSeparatedStringToStringSetTransformer.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class ConfigBuilderExtensionIntegrationTest {

private ExtendedTestConfig config = new ConfigBuilder<ExtendedTestConfig>(ExtendedTestConfig.class).build();
private ExtendedTestConfig config = new ConfigBuilder<>(ExtendedTestConfig.class).build();

@Test
public void testGetValuePresentInSuperClassAndCurrentClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void setUp() {
when(filterContainer.getFilters()).thenReturn(filters);
when(commandLineHelper.getOptions(TestConfig.class)).thenReturn(commandLineOptions);

configBuilder = new ConfigBuilder<TestConfig>(TestConfig.class, configBuilderFactory);
configBuilder = new ConfigBuilder<>(TestConfig.class, configBuilderFactory);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ConfigBuilderWithoutAnnotationsIntegrationTest {
@Rule
public SystemOutRule systemOut = new SystemOutRule();

private ConfigBuilder<TestConfigWithoutAnnotations> configBuilder = new ConfigBuilder<TestConfigWithoutAnnotations>(TestConfigWithoutAnnotations.class);
private ConfigBuilder<TestConfigWithoutAnnotations> configBuilder = new ConfigBuilder<>(TestConfigWithoutAnnotations.class);

@Test
public void testConfigBuilderWithParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.tngtech.configbuilder.annotation.valueextractor.CommandLineValue;
import com.tngtech.configbuilder.configuration.ErrorMessageSetup;
import com.tngtech.configbuilder.exception.ConfigBuilderException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -41,7 +38,7 @@ private static class TestConfig {
@Mock
private Options options;
@Mock
private GnuParser parser;
private DefaultParser parser;
@Mock
private CommandLine commandLine;
@Mock
Expand All @@ -65,7 +62,7 @@ public void setUp() throws Exception {

@Test
public void testGetCommandLine() throws Exception {
when(configBuilderFactory.createInstance(GnuParser.class)).thenReturn(parser);
when(configBuilderFactory.createInstance(DefaultParser.class)).thenReturn(parser);
when(configBuilderFactory.createInstance(Options.class)).thenReturn(options);
ArgumentCaptor<Option> captor = ArgumentCaptor.forClass(Option.class);
assertThat(commandLineHelper.getCommandLine(TestConfig.class, args)).isSameAs(commandLine);
Expand All @@ -75,12 +72,7 @@ public void testGetCommandLine() throws Exception {

assertThat(options).hasSize(2);

final ImmutableList<Option> sortedOptions = FluentIterable.from(options).toSortedList(new Comparator<Option>() {
@Override
public int compare(Option o1, Option o2) {
return o1.getLongOpt().compareTo(o2.getLongOpt());
}
});
final ImmutableList<Option> sortedOptions = FluentIterable.from(options).toSortedList((o1, o2) -> o1.getLongOpt().compareTo(o2.getLongOpt()));

assertThat(sortedOptions.get(0).getLongOpt()).isEqualTo("user");
assertThat(sortedOptions.get(0).getOpt()).isEqualTo("u");
Expand All @@ -93,7 +85,7 @@ public int compare(Option o1, Option o2) {

@Test(expected = ConfigBuilderException.class)
public void testGetCommandLineThrowsException() {
when(configBuilderFactory.createInstance(GnuParser.class)).thenReturn(new GnuParser());
when(configBuilderFactory.createInstance(DefaultParser.class)).thenReturn(new DefaultParser());
when(configBuilderFactory.createInstance(Options.class)).thenReturn(new Options());
args = new String[]{"nd", "notDefined"};
commandLineHelper.getCommandLine(TestConfig.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

Expand All @@ -16,7 +17,7 @@
import java.util.Set;

import static com.google.common.collect.Sets.newHashSet;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -56,7 +57,7 @@ public void setUp() {
when(configBuilderFactory.getInstance(ValidatorFactory.class)).thenReturn(validatorFactory);
when(validatorFactory.getValidator()).thenReturn(validator);

configValidator = new ConfigValidator<TestConfig>(configBuilderFactory);
configValidator = new ConfigValidator<>(configBuilderFactory);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setUp() {

@Test
public void testGetInstance() {
ConstructionHelper<TestConfig> constructionHelper = new ConstructionHelper<TestConfig>(configBuilderFactory);
ConstructionHelper<TestConfig> constructionHelper = new ConstructionHelper<>(configBuilderFactory);
TestConfig testConfig = constructionHelper.getInstance(TestConfig.class, "string", 3);
assertThat(testConfig.getString()).isEqualTo("string");
assertThat(testConfig.getInteger()).isEqualTo(3);
Expand All @@ -65,7 +65,7 @@ public void testGetInstance() {
public void testGetInstanceThrowsException() {
expectedException.expect(NoConstructorFoundException.class);
expectedException.expectMessage("NoConstructorFoundException");
ConstructionHelper<TestConfigForException> constructionHelper = new ConstructionHelper<TestConfigForException>(configBuilderFactory);
ConstructionHelper<TestConfigForException> constructionHelper = new ConstructionHelper<>(configBuilderFactory);
constructionHelper.getInstance(TestConfigForException.class, "string", 3);
}
}
Loading

0 comments on commit e81969e

Please sign in to comment.