Skip to content

Commit

Permalink
Move tests into CustomConfigurationProviderTest
Browse files Browse the repository at this point in the history
  • Loading branch information
derTobsch committed Jan 13, 2025
1 parent cb84183 commit 4bf0e3c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/
public class ConfigurationProviderManager {

private ConfigurationProvider urlConfigurationProvider = new URLConfigurationProvider();
private ConfigurationProvider classpathConfigurationProvider = new ClasspathConfigurationProvider();
private ConfigurationProvider customConfigurationProvider = new CustomConfigurationProvider();
private final ConfigurationProvider urlConfigurationProvider = new URLConfigurationProvider();
private final ConfigurationProvider classpathConfigurationProvider = new ClasspathConfigurationProvider();
private final ConfigurationProvider customConfigurationProvider = new CustomConfigurationProvider();

/**
* Reads the jollyday configuration from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import java.util.Properties;

import static java.util.Arrays.stream;
import static java.util.function.Predicate.not;

class CustomConfigurationProvider implements ConfigurationProvider {

/**
Expand All @@ -30,23 +33,20 @@ public Properties getProperties() {

final Properties properties = new Properties();

final String providersStrList = System.getProperty(CONFIG_PROVIDERS_PROPERTY);
if (providersStrList != null) {
final String[] providersClassNames = providersStrList.split(",");
for (String providerClassName : providersClassNames) {

if (providerClassName == null || providerClassName.isEmpty()) {
continue;
}

try {
final Class<?> providerClass = Class.forName(providerClassName.trim(), true, ClassLoadingUtil.getClassloader());
final ConfigurationProvider configurationProvider = (ConfigurationProvider) providerClass.getDeclaredConstructor().newInstance();
properties.putAll(configurationProvider.getProperties());
} catch (Exception e) {
LOG.warn("Cannot load configuration from provider class '{}'. {} ({}).", providerClassName, e.getClass().getSimpleName(), e.getMessage());
}
}
final String providerClassNames = System.getProperty(CONFIG_PROVIDERS_PROPERTY);
if(providerClassNames != null) {
stream(providerClassNames.split(","))
.filter(not(String::isEmpty))
.map(String::trim)
.forEach(providerClassName -> {
try {
final Class<?> providerClass = Class.forName(providerClassName, true, ClassLoadingUtil.getClassloader());
final ConfigurationProvider configurationProvider = (ConfigurationProvider) providerClass.getDeclaredConstructor().newInstance();
properties.putAll(configurationProvider.getProperties());
} catch (Exception e) {
LOG.warn("Cannot load configuration from provider class '{}'. {} ({}).", providerClassName, e.getClass().getSimpleName(), e.getMessage());
}
});
}

return properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,29 @@

import de.focus_shift.jollyday.core.ManagerParameter;
import de.focus_shift.jollyday.core.ManagerParameters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class ConfigurationProviderManagerTest {

@Mock
private ClasspathConfigurationProvider classpathConfigurationProvider;
@Mock
private URLConfigurationProvider urlConfigurationProvider;
private ConfigurationProviderManager sut;

@InjectMocks
private final ConfigurationProviderManager sut = new ConfigurationProviderManager();

private final ManagerParameter managerParameter = ManagerParameters.create((String) null);

@AfterEach
void teardown() {
System.clearProperty(CustomConfigurationProvider.CONFIG_PROVIDERS_PROPERTY);
}

@Test
void testGetPropertiesWithEmptyProvidersList() {
System.setProperty(CustomConfigurationProvider.CONFIG_PROVIDERS_PROPERTY, "");
sut.mergeConfigurationProperties(managerParameter);
assertResult(managerParameter);
}

@Test
void testGetPropertiesWithWrongClass() {
System.setProperty(CustomConfigurationProvider.CONFIG_PROVIDERS_PROPERTY, "java.lang.String");
sut.mergeConfigurationProperties(managerParameter);
assertResult(managerParameter);
}

@Test
void testGetPropertiesWithCorrectClass() {
System.setProperty(CustomConfigurationProvider.CONFIG_PROVIDERS_PROPERTY, "de.focus_shift.jollyday.core.configuration.TestProvider");
sut.mergeConfigurationProperties(managerParameter);
assertResult(managerParameter);
assertThat(managerParameter.getProperty("key")).isEqualTo("value");
}

@Test
void testGetPropertiesWithWrongAndCorrectClass() {
System.setProperty(CustomConfigurationProvider.CONFIG_PROVIDERS_PROPERTY, "de.focus_shift.jollyday.core.configuration.TestProvider, java.lang.String");
sut.mergeConfigurationProperties(managerParameter);
assertResult(managerParameter);
assertThat(managerParameter.getProperty("key")).isEqualTo("value");
@BeforeEach
void setUp() {
sut = new ConfigurationProviderManager();
}

@Test
void testGetPropertiesWithManualOverride() {
final ManagerParameter managerParameter = ManagerParameters.create((String) null);
managerParameter.setProperty("MANUAL_KEY", "MANUAL_VALUE");
managerParameter.setProperty("manager.impl", "NewImpl");

sut.mergeConfigurationProperties(managerParameter);

assertResult(managerParameter);
assertThat(managerParameter.getProperty("MANUAL_KEY")).isEqualTo("MANUAL_VALUE");
assertThat(managerParameter.getProperty("manager.impl")).isEqualTo("NewImpl");
}

private void assertResult(ManagerParameter parameter) {
assertThat(parameter).isNotNull();
verify(classpathConfigurationProvider).getProperties();
verify(urlConfigurationProvider).getProperties();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.focus_shift.jollyday.core.configuration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.Properties;

import static de.focus_shift.jollyday.core.configuration.CustomConfigurationProvider.CONFIG_PROVIDERS_PROPERTY;
import static org.assertj.core.api.Assertions.assertThat;

class CustomConfigurationProviderTest {

private CustomConfigurationProvider sut;

@BeforeEach
void setUp() {
sut = new CustomConfigurationProvider();
}

@ParameterizedTest
@CsvSource(value = {
"de.focus_shift.jollyday.core.configuration.CustomConfigurationProviderTest$TestProvider:value",
"de.focus_shift.jollyday.core.configuration.CustomConfigurationProviderTest$TestProvider,de.focus_shift.jollyday.core.configuration.CustomConfigurationProviderTest$SecondTestProvider:second-value",
"de.focus_shift.jollyday.core.configuration.CustomConfigurationProviderTest$TestProvider, java.lang.String: value",
}, delimiter = ':')
void ensuresToOverridePropertiesAndIgnoreExceptions(final String classList, final String property) {
System.setProperty(CONFIG_PROVIDERS_PROPERTY, classList);

final Properties properties = sut.getProperties();
assertThat(properties.getProperty("key")).isEqualTo(property);

System.clearProperty(CONFIG_PROVIDERS_PROPERTY);
}

@Test
void ensuresToDoNothingIfConfigProvidersPropertyIsNotSet() {
System.clearProperty(CONFIG_PROVIDERS_PROPERTY);

final Properties properties = sut.getProperties();
assertThat(properties).isEmpty();
}

@Test
void ensuresCorrectConfigProvidersPropertyKey() {
assertThat(CONFIG_PROVIDERS_PROPERTY).isEqualTo("de.focus_shift.jollyday.config.providers");
}

static class TestProvider implements ConfigurationProvider {

private final Properties properties = new Properties();

TestProvider() {
properties.setProperty("key", "value");
}

@Override
public Properties getProperties() {
return properties;
}
}

static class SecondTestProvider implements ConfigurationProvider {

private final Properties properties = new Properties();

SecondTestProvider() {
properties.setProperty("key", "second-value");
}

@Override
public Properties getProperties() {
return properties;
}
}
}

This file was deleted.

0 comments on commit 4bf0e3c

Please sign in to comment.