diff --git a/owner/src/main/java/org/aeonbits/owner/ConfigURIFactory.java b/owner/src/main/java/org/aeonbits/owner/ConfigURIFactory.java index 11e44934..87735f12 100644 --- a/owner/src/main/java/org/aeonbits/owner/ConfigURIFactory.java +++ b/owner/src/main/java/org/aeonbits/owner/ConfigURIFactory.java @@ -13,6 +13,7 @@ import java.net.URL; import static org.aeonbits.owner.Util.fixBackslashesToSlashes; +import static org.aeonbits.owner.Util.fixSpacesToPercentTwenty; /** * @author Luigi R. Viggiano @@ -20,6 +21,7 @@ class ConfigURIFactory { private static final String CLASSPATH_PROTOCOL = "classpath:"; + private static final String FILE_PROTOCOL = "file:"; private final transient ClassLoader classLoader; private final VariablesExpander expander; @@ -37,6 +39,9 @@ URI newURI(String spec) throws URISyntaxException { if (url == null) return null; return url.toURI(); + } else if (fixed.startsWith(FILE_PROTOCOL)) { + String path = fixSpacesToPercentTwenty(fixed); + return new URI(path); } else { return new URI(fixed); } diff --git a/owner/src/main/java/org/aeonbits/owner/Util.java b/owner/src/main/java/org/aeonbits/owner/Util.java index d4cd0391..1e12ff18 100644 --- a/owner/src/main/java/org/aeonbits/owner/Util.java +++ b/owner/src/main/java/org/aeonbits/owner/Util.java @@ -108,6 +108,10 @@ public static String fixBackslashesToSlashes(String path) { return path.replace('\\', '/'); } + public static String fixSpacesToPercentTwenty(String path) { + return path.replace(" ", "%20"); + } + static T ignore() { // the ignore method does absolutely nothing, but it helps to shut up warnings by pmd and other reporting tools // complaining about empty catch methods. diff --git a/owner/src/test/java/org/aeonbits/owner/loadstrategies/LoadPathsWithSpacesTest.java b/owner/src/test/java/org/aeonbits/owner/loadstrategies/LoadPathsWithSpacesTest.java new file mode 100644 index 00000000..66fb63ca --- /dev/null +++ b/owner/src/test/java/org/aeonbits/owner/loadstrategies/LoadPathsWithSpacesTest.java @@ -0,0 +1,52 @@ +package org.aeonbits.owner.loadstrategies; + +import static org.junit.Assert.assertEquals; + +import org.aeonbits.owner.Config; +import org.aeonbits.owner.ConfigFactory; +import org.aeonbits.owner.LoadersManagerForTest; +import org.aeonbits.owner.VariablesExpanderForTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.Properties; +import java.util.concurrent.ScheduledExecutorService; + +@RunWith(MockitoJUnitRunner.class) +public class LoadPathsWithSpacesTest extends LoadStrategyTestBase { + + @Mock + private ScheduledExecutorService scheduler; + @Spy + private final LoadersManagerForTest loaders = new LoadersManagerForTest(); + private final VariablesExpanderForTest expander = new VariablesExpanderForTest(new Properties()); + + + @Config.Sources({ + "file:${user.dir}/src/test/resources/org/aeonbits/owner/directory with spaces/simple.properties"}) + public static interface FileConfigWithSource extends Config { + String helloWorld(); + } + + @Config.Sources({ + "classpath:org/aeonbits/owner/directory with spaces/simple.properties"}) + public static interface ClasspathConfigWithSource extends Config { + String helloWorld(); + } + + @Test + public void shouldLoadFromFile() throws Exception { + FileConfigWithSource sample = ConfigFactory.create(FileConfigWithSource.class); + assertEquals("Hello World!", sample.helloWorld()); + } + + @Test + public void shouldLoadFromClasspath() throws Exception { + ClasspathConfigWithSource sample = ConfigFactory.create(ClasspathConfigWithSource.class); + assertEquals("Hello World!", sample.helloWorld()); + } + +} diff --git a/owner/src/test/resources/org/aeonbits/owner/directory with spaces/simple.properties b/owner/src/test/resources/org/aeonbits/owner/directory with spaces/simple.properties new file mode 100644 index 00000000..0046c2b1 --- /dev/null +++ b/owner/src/test/resources/org/aeonbits/owner/directory with spaces/simple.properties @@ -0,0 +1,9 @@ +# +# Copyright (c) 2012-2015, Luigi R. Viggiano +# All rights reserved. +# +# This software is distributable under the BSD license. +# See the terms of the BSD license in the documentation provided with this software. +# + +helloWorld = Hello World!