SystemUtil { public static Configuration getSystemPropertiesConfiguration(final String prefix, final boolean trimPrefix) { final BaseConfiguration apacheConfiguration = new BaseConfiguration(); for (final Map.Entry<Object, Object> entry : System.getProperties().entrySet()) { final String key = entry.getKey().toString(); final Object value = entry.getValue(); if (key.startsWith(prefix + ".")) apacheConfiguration.setProperty(trimPrefix ? key.substring(prefix.length() + 1) : key, value); } return apacheConfiguration; } private  SystemUtil(); static Configuration getSystemPropertiesConfiguration(final String prefix, final boolean trimPrefix);  }
@Test public void shouldLoadSystemProperties() { System.setProperty("blah.aa", "1"); System.setProperty("blah.b", "true"); System.setProperty("blah.c", "three"); System.setProperty("bleep.d", "false"); Configuration configuration = SystemUtil.getSystemPropertiesConfiguration("blah", false); assertEquals(3, IteratorUtils.count(configuration.getKeys())); assertEquals(1, configuration.getInt("blah.aa")); assertTrue(configuration.getBoolean("blah.b")); assertEquals("three", configuration.getProperty("blah.c")); assertFalse(configuration.containsKey("d") || configuration.containsKey("bleep.d")); System.clearProperty("blah.aa"); System.clearProperty("blah.b"); System.clearProperty("blah.c"); System.clearProperty("bleep.d"); }
@Test public void shouldTrimSystemPropertyPrefixes() { System.setProperty("blah.a", "1"); System.setProperty("blah.bbb", "true"); System.setProperty("blah.c", "three"); System.setProperty("bleep.d", "false"); Configuration configuration = SystemUtil.getSystemPropertiesConfiguration("blah", true); assertEquals(3, IteratorUtils.count(configuration.getKeys())); assertEquals(1, configuration.getInt("a")); assertTrue(configuration.getBoolean("bbb")); assertEquals("three", configuration.getProperty("c")); assertFalse(configuration.containsKey("d") || configuration.containsKey("bleep.d")); System.clearProperty("blah.a"); System.clearProperty("blah.bbb"); System.clearProperty("blah.c"); System.clearProperty("bleep.d"); }
@Test public void shouldTrimSystemPropertyPrefixesAndNoMore() { System.setProperty("blah.a.x", "1"); System.setProperty("blah.b.y", "true"); System.setProperty("blah.cc.zzz", "three"); System.setProperty("bleep.d.d", "false"); Configuration configuration = SystemUtil.getSystemPropertiesConfiguration("blah", true); assertEquals(3, IteratorUtils.count(configuration.getKeys())); assertEquals(1, configuration.getInt("a.x")); assertTrue(configuration.getBoolean("b.y")); assertEquals("three", configuration.getProperty("cc.zzz")); assertFalse(configuration.containsKey("d") || configuration.containsKey("bleep.d")); assertFalse(configuration.containsKey("d.d") || configuration.containsKey("bleep.d.d")); System.clearProperty("blah.a.x"); System.clearProperty("blah.b.y"); System.clearProperty("blah.cc.zzz"); System.clearProperty("bleep.d.d"); }