Skip to content

Commit

Permalink
Added distinction between required and optional configuration files i…
Browse files Browse the repository at this point in the history
…n order to minimize log warnings
  • Loading branch information
slu-it authored and Axel Schüssler committed Oct 23, 2016
1 parent 5924713 commit bfe83ce
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 26 deletions.
Expand Up @@ -12,16 +12,15 @@


/**
* Abstract base implementation of a {@link ConfigurationAdapter} using a
* {@link Properties properties} object as its source.
* Abstract base implementation of a {@link ConfigurationAdapter} using a {@link Properties properties} object as its source.
*
* @see ConfigurationAdapter
* @since 2.0
*/
public abstract class AbstractPropertiesConfigurationAdapter implements ConfigurationAdapter {

protected void loadPropertiesFromResource(URL resource, Properties properties) throws IOException {
try(InputStreamReader isr = new InputStreamReader(resource.openStream(), Charsets.UTF_8)) {
try (InputStreamReader isr = new InputStreamReader(resource.openStream(), Charsets.UTF_8)) {
properties.load(isr);
}
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.function.Consumer;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -11,10 +12,8 @@


/**
* Base implementation of a {@link ConfigurationAdapter configuration adapter}
* using a {@link Properties properties} file from the classpath as its source.
* If the file doesn't exist the adaptation will be canceled and a warning will
* be logged.
* Base implementation of a {@link ConfigurationAdapter configuration adapter} using a {@link Properties properties} file
* from the classpath as its source. If the file doesn't exist the adaptation will be canceled and a warning will be logged.
*
* @see ConfigurationAdapter
* @see AbstractPropertiesConfigurationAdapter
Expand All @@ -23,10 +22,85 @@
@Slf4j
public class ClasspathPropertiesFileConfigurationAdapter extends AbstractPropertiesConfigurationAdapter {

private String propertyFilePath;
private static final String FILE_NOT_ON_CLASSPATH =
"{} properties file {} wasn't found on the classpath - it's properties will not be merged into configuration.";

/**
* Classification how important the existence of a configuration file is.
*
* @see ClasspathPropertiesFileConfigurationAdapter
* @since 2.1
*/
public enum Importance {

/** Property file is optional - if not found an INFO will be logged. */
OPTIONAL(path -> {
log.info(FILE_NOT_ON_CLASSPATH, "Optional", path);
}),

/** Property file is recommended - if not found a WARNING will be logged. */
RECOMMENDED(path -> {
log.warn(FILE_NOT_ON_CLASSPATH, "Recommended", path);
}),

/** Property file is required - if not found an exception will be thrown. */
REQUIRED(path -> {
String message = "Required properties file " + path + " wasn't found on the classpath!";
throw new IllegalStateException(message);
});

private final Consumer<String> handler;

Importance(Consumer<String> handler) {
this.handler = handler;
}

private void handleFileNotFound(String pathToFile) {
handler.accept(pathToFile);
}

}

private final String propertyFilePath;
private final Importance importance;

/**
* Creates a new instance for the given property file path and a default {@link Importance} of
* {@link Importance#OPTIONAL}.
* <p>
* The property file path must be provided in a format relative to the classpath root.
* <p>
* <b>Examples:</b>
* <ul>
* <li>my.properties</li>
* <li>some/folders/within/classpath/my.properties</li>
* </ul>
*
* @param propertyFilePath the path to the property file on the classpath
* @since 2.0
*/
public ClasspathPropertiesFileConfigurationAdapter(String propertyFilePath) {
this(propertyFilePath, Importance.OPTIONAL);
}

/**
* Creates a new instance for the given property file path and a {@link Importance}.
* <p>
* The property file path must be provided in a format relative to the classpath root.
* <p>
* <b>Examples:</b>
* <ul>
* <li>my.properties</li>
* <li>some/folders/within/classpath/my.properties</li>
* </ul>
*
* @param propertyFilePath the path to the property file on the classpath
* @param importance the importance of the property file
* @since 2.0
*/
public ClasspathPropertiesFileConfigurationAdapter(String propertyFilePath, Importance importance) {
this.propertyFilePath = propertyFilePath;
this.importance = importance;
}

@Override
Expand All @@ -36,7 +110,7 @@ public boolean adapt(Configuration configuration) {

URL resource = getClass().getClassLoader().getResource(propertyFilePath);
if (resource == null) {
log.warn("Could not load configuration file! {} file is not on the classpath.", propertyFilePath);
importance.handleFileNotFound(propertyFilePath);
return false;
}

Expand Down
Expand Up @@ -5,10 +5,9 @@


/**
* Loads a file called <code>testit-webtester-default.properties</code> from the
* classpath's root level in order to {@link ConfigurationAdapter adapt}
* {@link Configuration configurations}. If the file doesn't exist the
* adaptation will be canceled and a warning will be logged.
* Loads a file called <code>testit-webtester-default.properties</code> from the classpath's root level in order to {@link
* ConfigurationAdapter adapt} {@link Configuration configurations}. If the file doesn't exist the adaptation will be
* canceled and a warning will be logged.
* <p>
* This file is provided as part of the WebTester framework!
*
Expand All @@ -21,7 +20,7 @@ public class DefaultFileConfigurationAdapter extends ClasspathPropertiesFileConf
private static final String PROPERTIES_FILE = "testit-webtester-default.properties";

public DefaultFileConfigurationAdapter() {
super(PROPERTIES_FILE);
super(PROPERTIES_FILE, Importance.REQUIRED);
}

}
Expand Up @@ -5,13 +5,12 @@


/**
* Loads a file called <code>testit-webtester-global.properties</code> from the
* classpath's root level in order to {@link ConfigurationAdapter adapt}
* {@link Configuration configurations}. If the file doesn't exist the
* adaptation will be canceled and a warning will be logged.
* Loads a file called <code>testit-webtester-global.properties</code> from the classpath's root level in order to {@link
* ConfigurationAdapter adapt} {@link Configuration configurations}. If the file doesn't exist the adaptation will be
* canceled and a warning will be logged.
* <p>
* This file should be used to define common properties in an environment where
* multiple test projects are using a common base.
* This file should be used to define common properties in an environment where multiple test projects are using a common
* base.
*
* @see ConfigurationAdapter
* @see ClasspathPropertiesFileConfigurationAdapter
Expand All @@ -22,7 +21,7 @@ public class GlobalFileConfigurationAdapter extends ClasspathPropertiesFileConfi
private static final String PROPERTIES_FILE = "testit-webtester-global.properties";

public GlobalFileConfigurationAdapter() {
super(PROPERTIES_FILE);
super(PROPERTIES_FILE, Importance.OPTIONAL);
}

}
Expand Up @@ -5,10 +5,9 @@


/**
* Loads a file called <code>testit-webtester.properties</code> from the
* classpath's root level in order to {@link ConfigurationAdapter adapt}
* {@link Configuration configurations}. If the file doesn't exist the
* adaptation will be canceled and a warning will be logged.
* Loads a file called <code>testit-webtester.properties</code> from the classpath's root level in order to {@link
* ConfigurationAdapter adapt} {@link Configuration configurations}. If the file doesn't exist the adaptation will be
* canceled and a warning will be logged.
* <p>
* This file should be used to define properties for the current test project.
*
Expand All @@ -21,7 +20,7 @@ public class LocalFileConfigurationAdapter extends ClasspathPropertiesFileConfig
private static final String PROPERTIES_FILE = "testit-webtester.properties";

public LocalFileConfigurationAdapter() {
super(PROPERTIES_FILE);
super(PROPERTIES_FILE, Importance.RECOMMENDED);
}

}
@@ -1,5 +1,6 @@
package info.novatec.testit.webtester.config.adapters;

import static info.novatec.testit.webtester.config.adapters.ClasspathPropertiesFileConfigurationAdapter.Importance.REQUIRED;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -53,4 +54,10 @@ public void propertiesFromFileAreSetOnConfiguration() {

}

@Test(expected = IllegalStateException.class)
public void unknownRequiredFileThrowsException() {
cut = new ClasspathPropertiesFileConfigurationAdapter(UNKNOWN_PATH, REQUIRED);
cut.adapt(mock(Configuration.class));
}

}

0 comments on commit bfe83ce

Please sign in to comment.