This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit a187b4bddaf65ed4bb7ae58bb355258bf4fd82c8 Author: Tran Tien Duc <[email protected]> AuthorDate: Fri Aug 23 10:57:55 2019 +0700 JAMES-2871 Override PropertiesProvider getStringArray & getList To strip double quotes --- ....java => DelegatedPropertiesConfiguration.java} | 35 +++++++++++++++++++--- .../org/apache/james/utils/PropertiesProvider.java | 3 +- .../PropertiesProviderFromEnvVariablesTest.java | 3 -- .../apache/james/utils/PropertiesProviderTest.java | 14 +++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesWrapperConfiguration.java b/server/container/guice/configuration/src/main/java/org/apache/james/utils/DelegatedPropertiesConfiguration.java similarity index 88% rename from server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesWrapperConfiguration.java rename to server/container/guice/configuration/src/main/java/org/apache/james/utils/DelegatedPropertiesConfiguration.java index 9b95716..3acc215 100644 --- a/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesWrapperConfiguration.java +++ b/server/container/guice/configuration/src/main/java/org/apache/james/utils/DelegatedPropertiesConfiguration.java @@ -25,21 +25,31 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Optional; import java.util.Properties; +import java.util.stream.Stream; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.ConfigurationDecoder; import org.apache.commons.configuration2.ImmutableConfiguration; +import org.apache.commons.configuration2.ex.ConversionException; import org.apache.commons.configuration2.interpol.ConfigurationInterpolator; import org.apache.commons.configuration2.interpol.Lookup; import org.apache.commons.configuration2.sync.LockMode; import org.apache.commons.configuration2.sync.Synchronizer; +import org.apache.commons.lang3.StringUtils; -public class PropertiesWrapperConfiguration implements Configuration { +import com.github.steveash.guavate.Guavate; +import com.google.common.collect.ImmutableList; +public class DelegatedPropertiesConfiguration implements Configuration { + + private final String delimiter; private final Configuration configuration; - PropertiesWrapperConfiguration(Configuration configuration) { + DelegatedPropertiesConfiguration(String delimiter, Configuration configuration) { + this.delimiter = delimiter; this.configuration = configuration; } @@ -265,12 +275,21 @@ public class PropertiesWrapperConfiguration implements Configuration { @Override public String[] getStringArray(String key) { - return configuration.getStringArray(key); + return splitAndStripDoubleQuotes(configuration.getString(key)) + .toArray(String[]::new); } @Override public List<Object> getList(String key) { - return configuration.getList(key); + try { + String rawList = configuration.get(String.class, key); + return splitAndStripDoubleQuotes(rawList) + .collect(Guavate.toImmutableList()); + } catch (ConversionException e) { + return configuration.getList(key); + } catch (NoSuchElementException e) { + return ImmutableList.of(); + } } @Override @@ -350,4 +369,12 @@ public class PropertiesWrapperConfiguration implements Configuration { public void unlock(LockMode mode) { configuration.unlock(mode); } + + private Stream<String> splitAndStripDoubleQuotes(String value) { + return Optional.ofNullable(value) + .map(notNullValue -> Stream + .of(StringUtils.strip(notNullValue, "\"").split(delimiter)) + .map(String::trim)) + .orElseGet(Stream::empty); + } } diff --git a/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java b/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java index 3260945..b174e38 100644 --- a/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java +++ b/server/container/guice/configuration/src/main/java/org/apache/james/utils/PropertiesProvider.java @@ -43,6 +43,7 @@ import com.google.common.base.Strings; public class PropertiesProvider { private static final char COMMA = ','; + private static final String COMMA_STRING = ","; private final FileSystem fileSystem; private final String configurationPrefix; @@ -79,7 +80,7 @@ public class PropertiesProvider { .setListDelimiterHandler(new DefaultListDelimiterHandler(COMMA)) .setFile(propertiesFile)); - return new PropertiesWrapperConfiguration(builder.getConfiguration()); + return new DelegatedPropertiesConfiguration(COMMA_STRING, builder.getConfiguration()); } private Optional<File> getConfigurationFile(String fileName) { diff --git a/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderFromEnvVariablesTest.java b/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderFromEnvVariablesTest.java index 394af7d..3aab2bc 100644 --- a/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderFromEnvVariablesTest.java +++ b/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderFromEnvVariablesTest.java @@ -24,7 +24,6 @@ import static org.assertj.core.api.Assertions.assertThat; import org.apache.james.server.core.configuration.Configuration; import org.apache.james.server.core.filesystem.FileSystemImpl; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.EnvironmentVariables; @@ -46,7 +45,6 @@ public class PropertiesProviderFromEnvVariablesTest { testee = new PropertiesProvider(fileSystem, configuration); } - @Ignore("It returns a string 'value1, value2, value3, value4, value5'") @Test public void getListShouldLoadEnvVariables() throws Exception { environmentVariables.set("PROPERTIES_PROVIDER_ENV_VARIABLES", "value1, value2, value3, value4, value5"); @@ -56,7 +54,6 @@ public class PropertiesProviderFromEnvVariablesTest { .containsExactly("value1", "value2", "value3", "value4", "value5"); } - @Ignore("It returns a string 'value1, value2, value3, value4, value5'") @Test public void getArrayShouldLoadEnvVariables() throws Exception { environmentVariables.set("PROPERTIES_PROVIDER_ENV_VARIABLES", "value1, value2, value3, value4, value5"); diff --git a/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java b/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java index 01a0b02..ab78db3 100644 --- a/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java +++ b/server/container/guice/configuration/src/test/java/org/apache/james/utils/PropertiesProviderTest.java @@ -24,6 +24,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.FileNotFoundException; +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.apache.commons.lang3.StringUtils; import org.apache.james.server.core.configuration.Configuration; import org.apache.james.server.core.filesystem.FileSystemImpl; import org.junit.jupiter.api.BeforeEach; @@ -83,4 +85,16 @@ class PropertiesProviderTest { assertThat(testee.getConfiguration("a").getList(String.class, "keyByList")) .containsExactly("value1", "value2", "value3", "value4", "value5"); } + + @Test + void getStringArrayShouldReturnEmptyArrayWhenKeyNotFound() throws Exception { + assertThat(testee.getConfiguration("a").getStringArray("notExistKey")) + .isEmpty(); + } + + @Test + void getListShouldReturnEmptyListWhenKeyNotFound() throws Exception { + assertThat(testee.getConfiguration("a").getList("notExistKey")) + .isEmpty(); + } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
