Configuration { public static String get(final String key) { try { lock.readLock().lock(); frozen = true; return (String) properties.get(key); } finally { lock.readLock().unlock(); } } private  Configuration(); static Locale getLocale(); static boolean isEscapingEnabled(); static String get(final String key); static Map<String, String> getSiblings(final String prefix); static Map<String, String> getChildren(final String key); static void set(final String key, final String value); static void replace(final Map<String, String> configuration); static String resolve(final String value, final Resolver resolver); static void mergeSystemProperties(final Properties properties); static void resolveProperties(final Properties properties, final Resolver... resolvers); static boolean isFrozen();  }
@Test public void propertiesFileFromClassPath() throws Exception { loadProperties(FileSystem.createTemporaryResource("level = info")); assertThat(Configuration.get("level")).isEqualToIgnoringCase("info"); }
@Test public void propertiesFileFromFileSystem() throws Exception { loadProperties(FileSystem.createTemporaryFile("level = warn")); assertThat(Configuration.get("level")).isEqualToIgnoringCase("warn"); }
@Test public void nonExistingFile() throws Exception { String path = FileSystem.createTemporaryFile(); Files.delete(Paths.get(path)); loadProperties(path); assertThat(Configuration.get("level")).isNull(); assertThat(systemStream.consumeErrorOutput()).contains("ERROR").containsOnlyOnce(path); }
@Test public void overridingProperty() throws Exception { System.setProperty(PROPERTIES_PREFIX + "level", "debug"); loadProperties(FileSystem.createTemporaryFile("level = info")); assertThat(Configuration.get("level")).isEqualToIgnoringCase("debug"); }
@Test public void resolveSystemProperty() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = #{os.name}")); assertThat(Configuration.get("test")).isEqualTo(System.getProperty("os.name")); }
@Test public void resolveMultipleProperties() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = JRE #{java.version} from #{java.vendor}")); assertThat(Configuration.get("test")) .isEqualTo("JRE " + System.getProperty("java.version") + " from " + System.getProperty("java.vendor")); }
@Test public void resolveEnvironmentVariable() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${PATH}")); assertThat(Configuration.get("test")).isEqualTo(System.getenv("PATH")); }
@Test public void resolveMixedVariablesAndProperties() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${PATH} read by JRE #{java.version}")); assertThat(Configuration.get("test")).isEqualTo(System.getenv("PATH") + " read by JRE " + System.getProperty("java.version")); }
@Test public void emptyVariable() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${}")); assertThat(Configuration.get("test")).isEqualTo("${}"); assertThat(systemStream.consumeErrorOutput()).contains("WARN").containsOnlyOnce("${}"); }
@Test public void incompleteVariable() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${os.name")); assertThat(Configuration.get("test")).isEqualTo("${os.name"); assertThat(systemStream.consumeErrorOutput()).contains("WARN").containsOnlyOnce("${os.name"); }
@Test public void nonExistentVariable() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${my.invalid.variable}")); assertThat(Configuration.get("test")).isEqualTo("${my.invalid.variable}"); assertThat(systemStream.consumeErrorOutput()).contains("WARN").containsOnlyOnce("my.invalid.variable"); }
@Test public void existingVariableWithUnusedDefaultValue() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${PATH:/a/default/path}")); assertThat(Configuration.get("test")).isEqualTo(System.getenv("PATH")); }
@Test public void nonExistingVariableWithEmptyDefaultValue() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${my.invalid.variable:}")); assertThat(Configuration.get("test")).isEqualTo(""); }
@Test public void nonExistingVariableWithDefaultValue() throws Exception { loadProperties(FileSystem.createTemporaryFile("test = ${my.invalid.variable:a_default_value}")); assertThat(Configuration.get("test")).isEqualTo("a_default_value"); }
@Test public void checkUsingOtherConfigurationLoader() throws Exception { FileSystem.createServiceFile(ConfigurationLoader.class, FirstDummyConfigurationLoader.class.getName()); loadProperties(null); assertThat(Configuration.get("DummyConfigurationLoader")).isEqualTo("123"); FileSystem.deleteServiceFile(ConfigurationLoader.class); }
@Test public void checkSelectingConfigurationLoader() throws Exception { FileSystem.createServiceFile(ConfigurationLoader.class, FirstDummyConfigurationLoader.class.getName()); System.setProperty("tinylog.configurationloader", PropertiesConfigurationLoader.class.getName()); System.setProperty("tinylog.StandardConfigurationLoader", "456"); loadProperties(null); assertThat(Configuration.get("StandardConfigurationLoader")).isEqualTo("456"); System.setProperty("tinylog.configurationloader", FirstDummyConfigurationLoader.class.getName()); loadProperties(null); assertThat(Configuration.get("DummyConfigurationLoader")).isEqualTo("123"); System.setProperty("tinylog.configurationloader", "properties"); System.setProperty("tinylog.StandardConfigurationLoader", "456"); loadProperties(null); assertThat(Configuration.get("StandardConfigurationLoader")).isEqualTo("456"); System.setProperty("tinylog.configurationloader", "first dummy"); loadProperties(null); assertThat(Configuration.get("DummyConfigurationLoader")).isEqualTo("123"); System.clearProperty("tinylog.configurationloader"); FileSystem.deleteServiceFile(ConfigurationLoader.class); }
@Test public void checkConfigurationLoaderErrorTwoLoader() throws Exception { FileSystem.createServiceFile(ConfigurationLoader.class, FirstDummyConfigurationLoader.class.getName(), SecondDummyConfigurationLoader.class.getName()); loadProperties(null); assertThat(Configuration.get("DummyConfigurationLoader")).isEqualTo("123"); assertThat(systemStream.consumeErrorOutput()).contains("ERROR") .containsOnlyOnce("Ignoring more than one configuration loader"); System.clearProperty("tinylog.configurationloader"); FileSystem.deleteServiceFile(ConfigurationLoader.class); }
@Test public void productionPropertiesFile() throws Exception { FileSystem.createResource("tinylog.properties", "level = off"); try { loadProperties(null); assertThat(Configuration.get("level")).isEqualToIgnoringCase("off"); } finally { FileSystem.deleteResource("tinylog.properties"); } }
@Test public void testPropertiesFile() throws Exception { FileSystem.createResource("tinylog-test.properties", "level = off"); try { loadProperties(null); assertThat(Configuration.get("level")).isEqualToIgnoringCase("off"); } finally { FileSystem.deleteResource("tinylog-test.properties"); } }
@Test public void testPropertiesFileBeforeRelease() throws Exception { FileSystem.createResource("tinylog-test.properties", "level = off"); FileSystem.createResource("tinylog.properties", "level = trace"); try { loadProperties(null); assertThat(Configuration.get("level")).isEqualToIgnoringCase("off"); } finally { FileSystem.deleteResource("tinylog-dev.properties"); FileSystem.deleteResource("tinylog-test.properties"); FileSystem.deleteResource("tinylog.properties"); } }
@Test public void developmentPropertiesFile() throws Exception { FileSystem.createResource("tinylog-dev.properties", "level = off"); try { loadProperties(null); assertThat(Configuration.get("level")).isEqualToIgnoringCase("off"); } finally { FileSystem.deleteResource("tinylog-dev.properties"); } }
@Test public void developmentPropertiesFileBeforeOthers() throws Exception { FileSystem.createResource("tinylog-dev.properties", "level = off"); FileSystem.createResource("tinylog-test.properties", "level = trace"); FileSystem.createResource("tinylog.properties", "level = trace"); try { loadProperties(null); assertThat(Configuration.get("level")).isEqualToIgnoringCase("off"); } finally { FileSystem.deleteResource("tinylog-dev.properties"); FileSystem.deleteResource("tinylog-test.properties"); FileSystem.deleteResource("tinylog.properties"); } }
@Test public void propertiesFileFromUrl() throws Exception { String path = FileSystem.createTemporaryFile("level = debug"); String url = new File(path).getAbsoluteFile().toURI().toURL().toString(); loadProperties(url); assertThat(Configuration.get("level")).isEqualToIgnoringCase("debug"); }