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 12, 2017
1 parent d30eb02 commit 5853dfe
Show file tree
Hide file tree
Showing 3 changed files with 64 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.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

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,41 @@
*/
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> configParams;

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

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

private static Map<String, String> buildMap(String configFileName, Map<String, String> configParams) {
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 (IOException ex) {
// TODO Log warning.
}

// Override properties loaded from config file with values supplied via
// the Launcher API.
configParams.forEach(map::put);

return map;
}

@Override
Expand All @@ -45,12 +80,12 @@ public Optional<Boolean> getBoolean(String key) {

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

private String getProperty(String key) {
Preconditions.notBlank(key, "key must not be null or blank");
String value = this.configurationParameters.get(key);
String value = this.configParams.get(key);
if (value == null) {
try {
value = System.getProperty(key);
Expand All @@ -65,7 +100,7 @@ private String getProperty(String key) {
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this);
this.configurationParameters.forEach(builder::append);
this.configParams.forEach(builder::append);
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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;

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

}

0 comments on commit 5853dfe

Please sign in to comment.