ClasspathConfigurationSource implements ConfigurationSource { @Override public Properties getConfiguration(Environment environment) { Properties properties = new Properties(); Path pathPrefix = Paths.get(environment.getName()); URL url = getClass().getClassLoader().getResource(pathPrefix.toString()); if (url == null && !environment.getName().isEmpty()) { throw new MissingEnvironmentException("Directory doesn't exist: " + environment.getName()); } List<Path> paths = new ArrayList<>(); for (Path path : configFilesProvider.getConfigFiles()) { paths.add(pathPrefix.resolve(path)); } for (Path path : paths) { try (InputStream input = getClass().getClassLoader().getResourceAsStream(path.toString())) { if (input == null) { throw new IllegalStateException("Unable to load properties from classpath: " + path); } PropertiesProvider provider = propertiesProviderSelector.getProvider(path.getFileName().toString()); properties.putAll(provider.getProperties(input)); } catch (IOException e) { throw new IllegalStateException("Unable to load properties from classpath: " + path, e); } } return properties; } ClasspathConfigurationSource();  ClasspathConfigurationSource(ConfigFilesProvider configFilesProvider);  ClasspathConfigurationSource(ConfigFilesProvider configFilesProvider, PropertiesProviderSelector propertiesProviderSelector); @Override Properties getConfiguration(Environment environment); @Override void init(); @Override String toString();  }
@Test void getConfigurationReadsFromGivenPath() { Environment environment = new ImmutableEnvironment("otherApplicationConfigs"); assertThat(source.getConfiguration(environment)).containsOnly(MapEntry.entry("some.setting", "otherAppSetting")); }
@Test void getConfigurationDisallowsLeadingSlashInClasspathLocation() { Environment environment = new ImmutableEnvironment("/otherApplicationConfigs"); assertThatThrownBy(() -> source.getConfiguration(environment)).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationReadsFromGivenFiles() { configFilesProvider = () -> Arrays.asList( Paths.get("application.properties"), Paths.get("otherConfig.properties") ); source = new ClasspathConfigurationSource(configFilesProvider); assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnlyKeys("some.setting", "otherConfig.setting"); }
@Test void getConfigurationThrowsOnMissingEnvironment() { assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsOnMissingConfigFile() { configFilesProvider = () -> Collections.singletonList( Paths.get("nonexistent.properties") ); source = new ClasspathConfigurationSource(configFilesProvider); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).isExactlyInstanceOf(IllegalStateException.class); }
@Test void getConfigurationThrowsOnMalformedConfigFile() { configFilesProvider = () -> Collections.singletonList( Paths.get("malformed.properties") ); source = new ClasspathConfigurationSource(configFilesProvider); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).isExactlyInstanceOf(IllegalStateException.class); }