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 1ba575662490c693521587b9cba46a7033c2950a Author: Benoit Tellier <[email protected]> AuthorDate: Fri May 28 15:38:04 2021 +0700 JAMES-2886 webadmin.properties extensions.routes was badly split on coma --- .../james/modules/server/WebAdminServerModule.java | 16 ++++- .../modules/server/WebAdminServerModuleTest.java | 70 ++++++++++++++++++++++ .../src/test/resources/webadmin-empty.properties | 1 + .../src/test/resources/webadmin-none.properties | 0 .../src/test/resources/webadmin-one.properties | 1 + .../src/test/resources/webadmin-two.properties | 1 + 6 files changed, 86 insertions(+), 3 deletions(-) diff --git a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java index fb7a7d0..ce68f95 100644 --- a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java +++ b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java @@ -58,6 +58,8 @@ import org.slf4j.LoggerFactory; import com.github.fge.lambdas.Throwing; import com.github.steveash.guavate.Guavate; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.inject.AbstractModule; import com.google.inject.Provider; @@ -79,6 +81,9 @@ public class WebAdminServerModule extends AbstractModule { private static final String DEFAULT_NO_PASSWORD = null; private static final String DEFAULT_NO_TRUST_KEYSTORE = null; private static final String DEFAULT_NO_TRUST_PASSWORD = null; + private static final Splitter SPLITTER = Splitter.on(',') + .trimResults() + .omitEmptyStrings(); @Override protected void configure() { @@ -117,9 +122,7 @@ public class WebAdminServerModule extends AbstractModule { try { Configuration configurationFile = propertiesProvider.getConfiguration("webadmin"); - List<String> additionalRoutes = Optional.ofNullable(configurationFile.getStringArray("extensions.routes")) - .map(ImmutableList::copyOf) - .orElse(ImmutableList.of()); + List<String> additionalRoutes = additionalRoutes(configurationFile); return WebAdminConfiguration.builder() .enable(configurationFile.getBoolean("enabled", DEFAULT_DISABLED)) @@ -138,6 +141,13 @@ public class WebAdminServerModule extends AbstractModule { } } + @VisibleForTesting + ImmutableList<String> additionalRoutes(Configuration configurationFile) { + String rawString = configurationFile.getString("extensions.routes", ""); + + return ImmutableList.copyOf(SPLITTER.splitToList(rawString)); + } + private Optional<String> loadPublicKey(FileSystem fileSystem, Optional<String> jwtPublickeyPemUrl) { return jwtPublickeyPemUrl.map(Throwing.function(url -> FileUtils.readFileToString(fileSystem.getFile(url), StandardCharsets.US_ASCII))); } diff --git a/server/container/guice/protocols/webadmin/src/test/java/org/apache/james/modules/server/WebAdminServerModuleTest.java b/server/container/guice/protocols/webadmin/src/test/java/org/apache/james/modules/server/WebAdminServerModuleTest.java new file mode 100644 index 0000000..45044f4 --- /dev/null +++ b/server/container/guice/protocols/webadmin/src/test/java/org/apache/james/modules/server/WebAdminServerModuleTest.java @@ -0,0 +1,70 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.modules.server; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.configuration2.FileBasedConfiguration; +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; +import org.apache.commons.configuration2.builder.fluent.Parameters; +import org.junit.jupiter.api.Test; + +class WebAdminServerModuleTest { + @Test + void shouldReturnEmptyWhenNoField() throws Exception { + FileBasedConfiguration configuration = getConfiguration("webadmin-none.properties"); + + assertThat(new WebAdminServerModule().additionalRoutes(configuration)) + .isEmpty(); + } + + @Test + void shouldReturnEmptyWhenEmptyField() throws Exception { + FileBasedConfiguration configuration = getConfiguration("webadmin-empty.properties"); + + assertThat(new WebAdminServerModule().additionalRoutes(configuration)) + .isEmpty(); + } + + @Test + void shouldReturnOneRoutes() throws Exception { + FileBasedConfiguration configuration = getConfiguration("webadmin-one.properties"); + + assertThat(new WebAdminServerModule().additionalRoutes(configuration)) + .containsOnly("org.apache.custom.webadmin.CustomRoute"); + } + + @Test + void shouldReturnSeveralRoutes() throws Exception { + FileBasedConfiguration configuration = getConfiguration("webadmin-two.properties"); + + assertThat(new WebAdminServerModule().additionalRoutes(configuration)) + .containsOnly("org.apache.custom.webadmin.CustomRoute", "org.apache.custom.webadmin.AnotherCustomRoute"); + } + + private FileBasedConfiguration getConfiguration(String name) throws org.apache.commons.configuration2.ex.ConfigurationException { + FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class) + .configure(new Parameters() + .fileBased() + .setURL(ClassLoader.getSystemResource(name))); + return builder.getConfiguration(); + } +} \ No newline at end of file diff --git a/server/container/guice/protocols/webadmin/src/test/resources/webadmin-empty.properties b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-empty.properties new file mode 100644 index 0000000..0c9a520 --- /dev/null +++ b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-empty.properties @@ -0,0 +1 @@ +extensions.routes= \ No newline at end of file diff --git a/server/container/guice/protocols/webadmin/src/test/resources/webadmin-none.properties b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-none.properties new file mode 100644 index 0000000..e69de29 diff --git a/server/container/guice/protocols/webadmin/src/test/resources/webadmin-one.properties b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-one.properties new file mode 100644 index 0000000..84ccb4a --- /dev/null +++ b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-one.properties @@ -0,0 +1 @@ +extensions.routes=org.apache.custom.webadmin.CustomRoute \ No newline at end of file diff --git a/server/container/guice/protocols/webadmin/src/test/resources/webadmin-two.properties b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-two.properties new file mode 100644 index 0000000..97e048e --- /dev/null +++ b/server/container/guice/protocols/webadmin/src/test/resources/webadmin-two.properties @@ -0,0 +1 @@ +extensions.routes=org.apache.custom.webadmin.CustomRoute,org.apache.custom.webadmin.AnotherCustomRoute \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
