FilesConfigurationSource implements ConfigurationSource { @Override public Properties getConfiguration(Environment environment) { Properties properties = new Properties(); Path rootPath; if (environment.getName().trim().isEmpty()) { rootPath = Paths.get(System.getProperty("user.home")); } else { rootPath = Paths.get(environment.getName()); } if (!rootPath.toFile().exists()) { throw new MissingEnvironmentException("Directory doesn't exist: " + rootPath); } List<Path> paths = new ArrayList<>(); for (Path path : configFilesProvider.getConfigFiles()) { paths.add(rootPath.resolve(path)); } for (Path path : paths) { try (InputStream input = new FileInputStream(path.toFile())) { PropertiesProvider provider = propertiesProviderSelector.getProvider(path.getFileName().toString()); properties.putAll(provider.getProperties(input)); } catch (IOException e) { throw new IllegalStateException("Unable to load properties from file: " + path, e); } } return properties; } FilesConfigurationSource();  FilesConfigurationSource(ConfigFilesProvider configFilesProvider);  FilesConfigurationSource(ConfigFilesProvider configFilesProvider, PropertiesProviderSelector propertiesProviderSelector); @Override Properties getConfiguration(Environment environment); @Override void init(); @Override String toString();  }
@Test void getConfigurationReadsFromDefaultFile() { assertThat(source.getConfiguration(environment)).containsOnly(MapEntry.entry("some.setting", "masterValue")); }
@Test void getConfigurationReadsFromGivenFiles() { configFilesProvider = () -> Arrays.asList( Paths.get("application.properties"), Paths.get("otherConfig.properties") ); source = new FilesConfigurationSource(configFilesProvider); assertThat(source.getConfiguration(environment)).containsOnlyKeys("some.setting", "otherConfig.setting"); }
@Test void getConfigurationThrowsOnMissingEnvironment() { assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsOnMissingConfigFile() throws Exception { fileRepo.deleteFile(Paths.get("application.properties")); assertThatThrownBy(() -> source.getConfiguration(environment)).isExactlyInstanceOf(IllegalStateException.class); }
@Test void getConfigurationThrowsOnMalformedConfigFile() { configFilesProvider = () -> Collections.singletonList( Paths.get("malformed.properties") ); source = new FilesConfigurationSource(configFilesProvider); assertThatThrownBy(() -> source.getConfiguration(environment)).isExactlyInstanceOf(IllegalStateException.class); }