PropertySources { public static PropertySourceChain fromFile(String path) { return fromFile(path, null); }  static PropertySourceChain from(PropertySource source); static PropertySourceChain from(PropertySource source, Func1<String, String> transformer); static PropertySourceChain fromConstantsClass(Class<T> constantsClass); static PropertySourceChain fromConstantsClass(Class<T> constantsClass, Func1<String, String> transformer); static PropertySourceChain fromComputed(Func1<String, String> computation); static PropertySourceChain fromComputed(Func1<String, String> computation, Func1<String, String> transformer); static PropertySourceChain fromEnvironment(); static PropertySourceChain fromEnvironment(Func1<String, String> transformer); static PropertySourceChain fromFile(String path); static PropertySourceChain fromFile(String path, Func1<String, String> transformer); static PropertySourceChain fromImmediate(Action1<ImmediatePropertySource> initializer); static PropertySourceChain fromImmediate(
            Action1<ImmediatePropertySource> initializer,
            Func1<String, String> transformer); static PropertySourceChain fromSystem(); static PropertySourceChain fromSystem(Func1<String, String> transformer);  }
@Test public void files() throws IOException { assertFalse(PropertySources.fromFile("nonexistent.properties").get("test2").isPresent()); File externalProperties = File.createTempFile("test", ".properties"); try { Files.write("test4=value4\n", externalProperties, Charsets.UTF_8); assertEquals("value", PropertySources.fromFile("test.properties").get("test").get()); assertFalse(PropertySources.fromFile("test.properties").get("test2").isPresent()); assertEquals("value3", PropertySources.fromFile("test.properties").orFile("test2.properties").get("test3").get()); assertFalse(PropertySources.fromFile("test.properties").orFile("test2.properties").get("test4").isPresent()); assertEquals("value", PropertySources.fromFile("test.properties").getRequired("test").asString()); assertEquals("value3", PropertySources.fromFile("test.properties").orFile("test2.properties").getRequired("test3").asString()); assertEquals("value4", PropertySources.fromFile(externalProperties.getPath()).orFile("test2.properties").getRequired("test4").asString()); } finally { externalProperties.delete(); } }
@Test public void chains() { environmentVariables.set("test4", "value4"); System.setProperty("test5", "value5"); PropertySource chain = PropertySources .fromFile("test.properties") .orFile("nonexistent.proeprties") .orFile("test2.properties").orSystem().orEnvironment() .orImmediate(p -> p.put("test6", "value6")); assertEquals("value", chain.get("test").get()); assertEquals("value3", chain.get("test3").get()); assertEquals("value4", chain.get("test4").get()); assertEquals("value5", chain.get("test5").get()); assertEquals("value6", chain.get("test6").get()); assertEquals("value", chain.getRequired("test").asString()); assertEquals("value3", chain.getRequired("test3").asString()); assertEquals("value4", chain.getRequired("test4").asString()); assertEquals("value5", chain.getRequired("test5").asString()); assertEquals("value6", chain.getRequired("test6").asString()); }