Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept File Paths Containing Spaces. #157

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions owner/src/main/java/org/aeonbits/owner/ConfigURIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import java.net.URL;

import static org.aeonbits.owner.Util.fixBackslashesToSlashes;
import static org.aeonbits.owner.Util.fixSpacesToPercentTwenty;

/**
* @author Luigi R. Viggiano
*/
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;

Expand All @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions owner/src/main/java/org/aeonbits/owner/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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!