Skip to content

Commit

Permalink
Accept File Paths Containing Spaces.
Browse files Browse the repository at this point in the history
Updated the uri processing to allow loading files that contain spaces in
their paths.
  • Loading branch information
Cameron Rollhieser committed Feb 13, 2016
1 parent 85fe748 commit 585aadb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
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!

0 comments on commit 585aadb

Please sign in to comment.