Skip to content

Commit

Permalink
Read ConfigurationParameters from properties file in classpath
Browse files Browse the repository at this point in the history
WIP

Issue: #1003
  • Loading branch information
sbrannen committed Aug 13, 2017
1 parent d30eb02 commit 1e06766
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@API(Experimental)
public interface ConfigurationParameters {

public static final String CONFIG_FILE_NAME = "junit-platform.properties";

/**
* Get the configuration property stored under the specified {@code key}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

package org.junit.platform.launcher.core;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Logger;

import org.junit.platform.commons.util.ClassLoaderUtils;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ToStringBuilder;
import org.junit.platform.engine.ConfigurationParameters;
Expand All @@ -22,11 +27,39 @@
*/
class LauncherConfigurationParameters implements ConfigurationParameters {

private final Map<String, String> configurationParameters;
private static final Logger LOG = Logger.getLogger(LauncherConfigurationParameters.class.getName());

LauncherConfigurationParameters(Map<String, String> configurationParameters) {
Preconditions.notNull(configurationParameters, "configuration parameters must not be null");
this.configurationParameters = configurationParameters;
private final Map<String, String> explicitConfigParams;
private final Map<String, String> configParamsFromFile;

LauncherConfigurationParameters(Map<String, String> configParams) {
this(configParams, ConfigurationParameters.CONFIG_FILE_NAME);
}

LauncherConfigurationParameters(Map<String, String> configParams, String configFileName) {
Preconditions.notNull(configParams, "configuration parameters must not be null");
Preconditions.notBlank(configFileName, "configFileName must not be null or blank");
this.explicitConfigParams = configParams;
this.configParamsFromFile = buildMap(configFileName);
}

private static Map<String, String> buildMap(String configFileName) {
Map<String, String> map = new HashMap<>();

Properties props = new Properties();
try {
InputStream inputStream = ClassLoaderUtils.getDefaultClassLoader().getResourceAsStream(configFileName);
if (inputStream != null) {
// TODO Log info message stating that the config file was found.
props.load(inputStream);
props.forEach((key, value) -> map.put(String.valueOf(key), String.valueOf(value)));
}
}
catch (Exception ex) {
// TODO Log warning.
}

return map;
}

@Override
Expand All @@ -45,27 +78,42 @@ public Optional<Boolean> getBoolean(String key) {

@Override
public int size() {
return this.configurationParameters.size();
return this.explicitConfigParams.size();
}

private String getProperty(String key) {
Preconditions.notBlank(key, "key must not be null or blank");
String value = this.configurationParameters.get(key);

// 1) Check explicit config param.
String value = this.explicitConfigParams.get(key);

// 2) Check system property.
if (value == null) {
try {
value = System.getProperty(key);
}
catch (Exception ex) {
/* ignore */
}

// 3) Check config file.
if (value == null) {
value = this.configParamsFromFile.get(key);
}
}

return value;
}

@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this);
this.configurationParameters.forEach(builder::append);
this.explicitConfigParams.forEach(builder::append);
this.configParamsFromFile.forEach((key, value) -> {
if (!this.explicitConfigParams.containsKey(key)) {
builder.append(key, value);
}
});
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.platform.launcher.core;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.PreconditionViolationException;
import org.junit.platform.engine.ConfigurationParameters;

/**
* Unit tests for {@link LauncherConfigurationParameters}.
*
* @since 1.0
*/
class LauncherConfigurationParametersTests {

private static final String CONFIG_FILE_NAME = "test-junit-platform.properties";
private static final String KEY = LauncherConfigurationParametersTests.class.getName();
private static final String CONFIG_PARAM = "explicit config param";
private static final String CONFIG_FILE = "from config file";
private static final String SYSTEM_PROPERTY = "system property";

@BeforeEach
@AfterEach
void reset() {
System.clearProperty(KEY);
}

@Test
void constructorPreconditions() {
assertThrows(PreconditionViolationException.class, () -> fromMap(null));
assertThrows(PreconditionViolationException.class, () -> fromMap(emptyMap(), null));
assertThrows(PreconditionViolationException.class, () -> fromMap(emptyMap(), ""));
assertThrows(PreconditionViolationException.class, () -> fromMap(emptyMap(), " "));
}

@Test
void getPreconditions() {
ConfigurationParameters configParams = fromMap(emptyMap());
assertThrows(PreconditionViolationException.class, () -> configParams.get(null));
assertThrows(PreconditionViolationException.class, () -> configParams.get(""));
assertThrows(PreconditionViolationException.class, () -> configParams.get(" "));
}

@Test
void noConfigParams() {
ConfigurationParameters configParams = fromMap(emptyMap());
assertThat(configParams.size()).isEqualTo(0);
assertThat(configParams.get(KEY)).isEmpty();
assertThat(configParams.toString()).doesNotContain(KEY);
}

@Test
void explicitConfigParam() {
ConfigurationParameters configParams = fromMap(singletonMap(KEY, CONFIG_PARAM));
assertThat(configParams.get(KEY)).contains(CONFIG_PARAM);
assertThat(configParams.toString()).contains(CONFIG_PARAM);
}

@Test
void systemProperty() {
System.setProperty(KEY, SYSTEM_PROPERTY);
ConfigurationParameters configParams = fromMap(emptyMap());
assertThat(configParams.get(KEY)).contains(SYSTEM_PROPERTY);
assertThat(configParams.toString()).doesNotContain(KEY);
}

@Test
void configFile() {
ConfigurationParameters configParams = fromMap(emptyMap(), CONFIG_FILE_NAME);
assertThat(configParams.get(KEY)).contains(CONFIG_FILE);
assertThat(configParams.toString()).contains(CONFIG_FILE);
}

@Test
void explicitConfigParamOverridesSystemProperty() {
System.setProperty(KEY, SYSTEM_PROPERTY);
ConfigurationParameters configParams = fromMap(singletonMap(KEY, CONFIG_PARAM));
assertThat(configParams.get(KEY)).contains(CONFIG_PARAM);
assertThat(configParams.toString()).contains(CONFIG_PARAM);
}

@Test
void explicitConfigParamOverridesConfigFile() {
ConfigurationParameters configParams = fromMap(singletonMap(KEY, CONFIG_PARAM), CONFIG_FILE_NAME);
assertThat(configParams.get(KEY)).contains(CONFIG_PARAM);
assertThat(configParams.toString()).contains(CONFIG_PARAM);
assertThat(configParams.toString()).doesNotContain(CONFIG_FILE);
}

@Test
void systemPropertyOverridesConfigFile() {
System.setProperty(KEY, SYSTEM_PROPERTY);
ConfigurationParameters configParams = fromMap(emptyMap(), CONFIG_FILE_NAME);
assertThat(configParams.get(KEY)).contains(SYSTEM_PROPERTY);
assertThat(configParams.toString()).contains(CONFIG_FILE);
}

private static LauncherConfigurationParameters fromMap(Map<String, String> map) {
return new LauncherConfigurationParameters(map);
}

private static LauncherConfigurationParameters fromMap(Map<String, String> map, String configFileName) {
return new LauncherConfigurationParameters(map, configFileName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.junit.platform.launcher.core.LauncherConfigurationParametersTests = from config file

0 comments on commit 1e06766

Please sign in to comment.