ServerConfigurationProvider implements ConfigurationProvider { @Override public Configuration getConfiguration() { return ensureLoadedConfiguration(); }  @Override Configuration getConfiguration();  }
@Test @DisplayName("Survives unreadable properties file") void testFileConfigUnreadable() throws Exception { Assumptions.assumeTrue(SUPPORTS_POSIX, "File system does not support Posix"); Path file = Paths.get("business-parameters.properties"); try { Files.write(file, Collections.singleton("property=testFileConfigUnreadable")); Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("---------")); Configuration config = new ServerConfigurationProvider().getConfiguration(); assertThat(config.getStringParameter("property")).isNotPresent(); } finally { Files.deleteIfExists(file); } }
@Test @DisplayName("Provides configuration from properties file in config folder") void testFileInConfigConfig() throws Exception { Path configFolder = Paths.get("config"); Path file = configFolder.resolve("business-parameters.properties"); try { Files.createDirectories(configFolder); Files.write(file, Collections.singleton("property=testFileInConfigConfig")); Configuration config = new ServerConfigurationProvider().getConfiguration(); assertThat(config.getStringParameter("property")).hasValue("testFileInConfigConfig"); } finally { Files.deleteIfExists(file); Files.deleteIfExists(configFolder); } }
@Test @DisplayName("Provides configuration from properties file on classpath") void testClasspathConfig() { String pathPrefix = this.getClass().getName().replace('.', '/') + "/testClasspathConfig/"; ClassLoader original = Thread.currentThread().getContextClassLoader(); try { UnaryOperator<String> rewrite = resource -> pathPrefix + resource; Thread.currentThread().setContextClassLoader(rewriteResourceLoader(original, rewrite)); Configuration config = new ServerConfigurationProvider().getConfiguration(); assertThat(config.getStringParameter("property")).hasValue("testClasspathConfig"); } finally { Thread.currentThread().setContextClassLoader(original); } }
@Test @DisplayName("Survives unreadable properties file on classpath") void testClasspathConfigUnreadable() throws Exception { Assumptions.assumeTrue(SUPPORTS_POSIX, "File system does not support Posix"); String pathPrefix = this.getClass().getName().replace('.', '/') + "/testClasspathConfigUnreadable/"; ClassLoader original = Thread.currentThread().getContextClassLoader(); Path path = Paths.get(original.getResource(pathPrefix + "business-parameters.properties").toURI()); Set<PosixFilePermission> originalPermissions = Files.getPosixFilePermissions(path); try { Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("---------")); UnaryOperator<String> rewrite = resource -> pathPrefix + resource; Thread.currentThread().setContextClassLoader(rewriteResourceLoader(original, rewrite)); Configuration config = new ServerConfigurationProvider().getConfiguration(); assertThat(config.getStringParameter("property")).isNotPresent(); } finally { Thread.currentThread().setContextClassLoader(original); Files.setPosixFilePermissions(path, originalPermissions); } }
@Test @DisplayName("Provides an empty configuration") void testEmptyConfig() { Configuration config = new ServerConfigurationProvider().getConfiguration(); assertThat(config.getStringParameter("property")).isNotPresent(); }
@Test @DisplayName("Provides configuration from properties file") void testFileConfig() throws Exception { Path file = Paths.get("business-parameters.properties"); try { Files.write(file, Collections.singleton("property=testFileConfig")); Configuration config = new ServerConfigurationProvider().getConfiguration(); assertThat(config.getStringParameter("property")).hasValue("testFileConfig"); } finally { Files.deleteIfExists(file); } }