Skip to content

Commit

Permalink
#29 : API: Loaders system:properties and system:env
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed Apr 24, 2021
1 parent d549c88 commit 9e9051e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class ClasspathPropertiesLoader implements Loader {

private static final Logger LOG = Logger.getLogger(ClasspathPropertiesLoader.class.getName());

private static final String CLASSPATH_SCHEME = "classpath";
private static final String SCHEME = "classpath";
private static final String EXTENTION = ".properties";

@Override public boolean accept(URI uri) {
return uri.isOpaque() && CLASSPATH_SCHEME.equals(uri.getScheme()) && uri.getSchemeSpecificPart().endsWith(EXTENTION);
return uri.isOpaque() && SCHEME.equals(uri.getScheme()) && uri.getSchemeSpecificPart().endsWith(EXTENTION);
}

@Override public Map<String, String> load(URI uri, ClassLoader classLoader) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.cactusthorn.config.core.loader;

import java.net.URI;
import java.util.Map;

public class SystemEnvLoader implements Loader {

private static final String SCHEME = "system";
private static final String PART = "env";

@Override public boolean accept(URI uri) {
return uri.isOpaque() && SCHEME.equals(uri.getScheme()) && uri.getSchemeSpecificPart().equals(PART);
}

public Map<String, String> load(URI uri, ClassLoader classLoader) {
return System.getenv();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.cactusthorn.config.core.loader;

import java.net.URI;
import java.util.Map;

public class SystemPropertiesLoader implements Loader {

private static final String SCHEME = "system";
private static final String PART = "properties";

@Override public boolean accept(URI uri) {
return uri.isOpaque() && SCHEME.equals(uri.getScheme()) && uri.getSchemeSpecificPart().equals(PART);
}

@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Map<String, String> load(URI uri, ClassLoader classLoader) {
return (Map) System.getProperties();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class ClasspathPropertiesLoaderTest {

private static final ClasspathPropertiesLoader CPP = new ClasspathPropertiesLoader();
private static final Loader CPP = new ClasspathPropertiesLoader();
private static final ClassLoader CL = ClasspathPropertiesLoaderTest.class.getClassLoader();

@BeforeAll static void setUpLogger() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.cactusthorn.config.core.loader;

import static org.junit.jupiter.api.Assertions.*;

import java.net.URI;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class SystemEnvLoaderTest {

private static final Loader CPP = new SystemEnvLoader();
private static final ClassLoader CL = SystemEnvLoaderTest.class.getClassLoader();
private static final URI SEURI = URI.create("system:env");

@Test public void acceptSimple() {
assertTrue(CPP.accept(SEURI));
}

@Test public void notAcceptNotOpaque() {
assertFalse(CPP.accept(URI.create("classpath://a.properties#ISO-8859-1")));
}

@Test public void notAcceptNotSystem() {
assertFalse(CPP.accept(URI.create("classpath:a.properties")));
}

@Test public void notAcceptNotProperties() {
assertFalse(CPP.accept(URI.create("system:properties")));
}

@Test public void load() {
Map<String, String> values = CPP.load(SEURI, CL);
assertFalse(values.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.cactusthorn.config.core.loader;

import static org.junit.jupiter.api.Assertions.*;

import java.net.URI;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class SystemPropertiesLoaderTest {

private static final Loader CPP = new SystemPropertiesLoader();
private static final ClassLoader CL = SystemPropertiesLoaderTest.class.getClassLoader();
private static final URI SPURI = URI.create("system:properties");

@Test public void acceptSimple() {
assertTrue(CPP.accept(SPURI));
}

@Test public void notAcceptNotOpaque() {
assertFalse(CPP.accept(URI.create("classpath://a.properties#ISO-8859-1")));
}

@Test public void notAcceptNotSystem() {
assertFalse(CPP.accept(URI.create("classpath:a.properties")));
}

@Test public void notAcceptNotProperties() {
assertFalse(CPP.accept(URI.create("system:abc")));
}

@Test public void load() {
System.setProperty("TEST", "TESTVALUE");
Map<String, String> values = CPP.load(SPURI, CL);
assertEquals("TESTVALUE", values.get("TEST"));
}
}

0 comments on commit 9e9051e

Please sign in to comment.