Skip to content

make Config non-final, add class SyspropOverridingConfig that will prefe... #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/ch/bind/philib/conf/Config.java
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@
/**
* @author Philipp Meinen
*/
public final class Config {
public class Config {

private final CowSet<ConfigListener> listeners = new CowSet<>(ConfigListener.class);

56 changes: 56 additions & 0 deletions src/main/java/ch/bind/philib/conf/SyspropOverridingConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2013 Philipp Meinen <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package ch.bind.philib.conf;

import java.net.URL;
import java.util.Collection;
import java.util.Map;

/**
* same behaviour as Config with the exception that, if a system property with the requested key exists, it takes precedence over the value from the config or
* the default value.
*/
public class SyspropOverridingConfig extends Config {

public SyspropOverridingConfig(Collection<URL> urls) {
super(urls);
}

public SyspropOverridingConfig(Map<String, String> config) {
super(config);
}

public SyspropOverridingConfig(URL url) {
super(url);
}

public SyspropOverridingConfig(URL[] urls) {
super(urls);
}

@Override
public String get(String key) {
String v = System.getProperty(key);
return v == null ? super.get(key) : v;
}
}
21 changes: 11 additions & 10 deletions src/test/java/ch/bind/philib/conf/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* Copyright (c) 2013 Philipp Meinen <[email protected]>
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -22,8 +22,11 @@

package ch.bind.philib.conf;

import ch.bind.philib.net.URLs;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.IOException;
import java.net.URL;
@@ -34,11 +37,9 @@
import java.util.Map;
import java.util.Set;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import org.testng.annotations.Test;

import ch.bind.philib.net.URLs;

public class ConfigTest {

85 changes: 85 additions & 0 deletions src/test/java/ch/bind/philib/conf/SyspropOverridingConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2013 Philipp Meinen <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package ch.bind.philib.conf;

import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.testng.annotations.Test;

import ch.bind.philib.net.URLs;

public class SyspropOverridingConfigTest {

@Test
public void loadMap() throws IOException {
Map<String, String> map = new HashMap<String, String>();
map.put("aa", "11");
SyspropOverridingConfig c = new SyspropOverridingConfig(map);
assertEquals(c.getInt("aa").intValue(), 11);
c.load();
assertEquals(c.getInt("aa").intValue(), 11);
}

@Test
public void load() throws IOException {
URL url = URLs.forClasspathResource("/words_en");
SyspropOverridingConfig c = new SyspropOverridingConfig(url);
c.load();
}

@Test
public void loadIgnoreNull() throws IOException {
URL url = URLs.forClasspathResource("/words_en");
SyspropOverridingConfig c = new SyspropOverridingConfig(Arrays.asList((URL) null, url));
c.load();
}

@Test
public void loadMultiple() throws IOException {
URL[] urls = {URLs.forClasspathResource("/ch/bind/philib/config/ConfigTest.a"), //
URLs.forClasspathResource("/ch/bind/philib/config/ConfigTest.b")};
SyspropOverridingConfig c = new SyspropOverridingConfig(urls);
c.load();
}

@Test
public void shouldOverride() throws IOException {
URL url = URLs.forClasspathResource("/ch/bind/philib/config/ConfigTest.a");
SyspropOverridingConfig c = new SyspropOverridingConfig(url);
c.load();

// value from the config
assertEquals(c.getInt("a").intValue(), 1);

// override
System.setProperty("a", "99");
assertEquals(c.getInt("a").intValue(), 99);
assertEquals(c.getInt("a", 0), 99);
}
}