Skip to content

Commit fd45908

Browse files
committed
Add support for configuration parameters resources
This commit adds support for specifying a properties file, for use with configuration parameters. - Added a new field configurationParametersResources of type List<String> to store the configuration parameters resources. - Added new methods configurationParametersResource(String propertiesFile) and configurationParametersResources(List<String> propertiesFiles) to add configuration parameters resources to the request builder. - Updated the buildLauncherConfigurationParameters() method to include the configurationParametersResources in the Builder instance. Related to issue: #3340
1 parent 49e74f7 commit fd45908

File tree

13 files changed

+256
-3
lines changed

13 files changed

+256
-3
lines changed

documentation/src/docs/asciidoc/user-guide/running-tests.adoc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,13 +895,16 @@ engines running on the JUnit Platform via one of the following mechanisms.
895895
`systemProperty` or `systemProperties` DSL.
896896
* <<running-tests-build-maven-config-params,Maven Surefire provider>>: use the
897897
`configurationParameters` property.
898-
2. JVM system properties.
899-
3. The JUnit Platform configuration file: a file named `junit-platform.properties` in the
898+
2. The `configurationParametersResource()` and `configurationParametersResources()`
899+
methods in the `LauncherDiscoveryRequestBuilder` which are used to select a `.properties`
900+
file on the classpath with configuration parameters.
901+
3. JVM system properties.
902+
4. The JUnit Platform configuration file: a file named `junit-platform.properties` in the
900903
root of the class path that follows the syntax rules for a Java `Properties` file.
901904

902905
NOTE: Configuration parameters are looked up in the exact order defined above.
903906
Consequently, configuration parameters supplied directly to the `Launcher` take
904-
precedence over those supplied via system properties and the configuration file.
907+
precedence over those supplied via system properties, the specified properties files, and the platform configuration file.
905908
Similarly, configuration parameters supplied via system properties take precedence over
906909
those supplied via the configuration file.
907910

junit-platform-console/src/main/java/org/junit/platform/console/options/TestDiscoveryOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class TestDiscoveryOptions {
6666
private List<String> includedTagExpressions = emptyList();
6767
private List<String> excludedTagExpressions = emptyList();
6868

69+
private List<String> configurationParametersResources = emptyList();
6970
private Map<String, String> configurationParameters = emptyMap();
7071

7172
public boolean isScanModulepath() {
@@ -262,4 +263,12 @@ public void setConfigurationParameters(Map<String, String> configurationParamete
262263
this.configurationParameters = configurationParameters;
263264
}
264265

266+
public List<String> getConfigurationParametersResources() {
267+
return configurationParametersResources;
268+
}
269+
270+
public TestDiscoveryOptions setConfigurationParametersResources(List<String> configurationParametersResources) {
271+
this.configurationParametersResources = configurationParametersResources;
272+
return this;
273+
}
265274
}

junit-platform-console/src/main/java/org/junit/platform/console/tasks/DiscoveryRequestCreator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ LauncherDiscoveryRequest toDiscoveryRequest(TestDiscoveryOptions options) {
5454
requestBuilder.selectors(selectors);
5555
addFilters(requestBuilder, options, selectors);
5656
requestBuilder.configurationParameters(options.getConfigurationParameters());
57+
requestBuilder.configurationParametersResources(options.getConfigurationParametersResources());
5758
return requestBuilder.build();
5859
}
5960

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/LauncherConfigurationParameters.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.platform.commons.logging.Logger;
3030
import org.junit.platform.commons.logging.LoggerFactory;
3131
import org.junit.platform.commons.util.ClassLoaderUtils;
32+
import org.junit.platform.commons.util.CollectionUtils;
3233
import org.junit.platform.commons.util.Preconditions;
3334
import org.junit.platform.commons.util.ToStringBuilder;
3435
import org.junit.platform.engine.ConfigurationParameters;
@@ -93,6 +94,7 @@ public String toString() {
9394
static final class Builder {
9495

9596
private final Map<String, String> explicitParameters = new HashMap<>();
97+
private final List<String> configResources = new ArrayList<>();
9698
private boolean implicitProvidersEnabled = true;
9799
private String configFileName = ConfigurationParameters.CONFIG_FILE_NAME;
98100
private ConfigurationParameters parentConfigurationParameters;
@@ -106,6 +108,12 @@ Builder explicitParameters(Map<String, String> parameters) {
106108
return this;
107109
}
108110

111+
Builder configurationResources(List<String> configResources) {
112+
Preconditions.notNull(configResources, "configResources must not be null");
113+
this.configResources.addAll(configResources);
114+
return this;
115+
}
116+
109117
Builder enableImplicitProviders(boolean enabled) {
110118
this.implicitProvidersEnabled = enabled;
111119
return this;
@@ -129,6 +137,9 @@ LauncherConfigurationParameters build() {
129137
parameterProviders.add(ParameterProvider.explicit(explicitParameters));
130138
}
131139

140+
CollectionUtils.forEachInReverseOrder(configResources,
141+
configResource -> parameterProviders.add(ParameterProvider.propertiesFile(configResource)));
142+
132143
if (parentConfigurationParameters != null) {
133144
parameterProviders.add(ParameterProvider.inherited(parentConfigurationParameters));
134145
}

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilder.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public final class LauncherDiscoveryRequestBuilder {
102102
private final List<DiscoveryFilter<?>> discoveryFilters = new ArrayList<>();
103103
private final List<PostDiscoveryFilter> postDiscoveryFilters = new ArrayList<>();
104104
private final Map<String, String> configurationParameters = new HashMap<>();
105+
private final List<String> configurationParametersResources = new ArrayList<>();
105106
private final List<LauncherDiscoveryListener> discoveryListeners = new ArrayList<>();
106107
private boolean implicitConfigurationParametersEnabled = true;
107108
private ConfigurationParameters parentConfigurationParameters;
@@ -198,6 +199,30 @@ public LauncherDiscoveryRequestBuilder configurationParameters(Map<String, Strin
198199
return this;
199200
}
200201

202+
/**
203+
* Add the supplied configuration parameters resource file to the request.
204+
* @param propertiesFile the classpath location of the properties file
205+
* never {@code null}
206+
* @return this builder for method chaining
207+
*/
208+
public LauncherDiscoveryRequestBuilder configurationParametersResource(String propertiesFile) {
209+
Preconditions.notNull(propertiesFile, "properties file must not be null");
210+
configurationParametersResources.add(propertiesFile);
211+
return this;
212+
}
213+
214+
/**
215+
* Add all of the supplied configuration parameters resource files to the request.
216+
* @param propertiesFiles the classpath locations of the properties files
217+
* never {@code null}
218+
* @return this builder for method chaining
219+
*/
220+
public LauncherDiscoveryRequestBuilder configurationParametersResources(List<String> propertiesFiles) {
221+
Preconditions.notNull(propertiesFiles, "properties files must not be null");
222+
propertiesFiles.forEach(this::configurationParametersResource);
223+
return this;
224+
}
225+
201226
/**
202227
* Set the parent configuration parameters to use for the request.
203228
*
@@ -296,6 +321,7 @@ public LauncherDiscoveryRequest build() {
296321
private LauncherConfigurationParameters buildLauncherConfigurationParameters() {
297322
Builder builder = LauncherConfigurationParameters.builder() //
298323
.explicitParameters(this.configurationParameters) //
324+
.configurationResources(this.configurationParametersResources) //
299325
.enableImplicitProviders(this.implicitConfigurationParametersEnabled);
300326

301327
if (parentConfigurationParameters != null) {

junit-platform-runner/src/main/java/org/junit/platform/runner/JUnitPlatform.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.junit.platform.launcher.TestPlan;
3131
import org.junit.platform.launcher.core.LauncherFactory;
3232
import org.junit.platform.suite.api.ConfigurationParameter;
33+
import org.junit.platform.suite.api.ConfigurationParametersResource;
3334
import org.junit.platform.suite.api.ExcludeClassNamePatterns;
3435
import org.junit.platform.suite.api.ExcludeEngines;
3536
import org.junit.platform.suite.api.ExcludePackages;
@@ -105,6 +106,7 @@
105106
* @see SuiteDisplayName
106107
* @see org.junit.platform.suite.api.UseTechnicalNames UseTechnicalNames
107108
* @see ConfigurationParameter
109+
* @see ConfigurationParametersResource ConfigurationParametersResource
108110
* @deprecated since 1.8, in favor of the {@link Suite @Suite} support provided by
109111
* the {@code junit-platform-suite-engine} module; to be removed in JUnit Platform 2.0
110112
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2015-2023 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.suite.api;
12+
13+
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
14+
15+
import java.lang.annotation.Documented;
16+
import java.lang.annotation.ElementType;
17+
import java.lang.annotation.Inherited;
18+
import java.lang.annotation.Repeatable;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.apiguardian.api.API;
24+
25+
/**
26+
* {@code @ConfigurationParametersResource} is an annotation that specifies
27+
* a configuration file in property format to be added to the discovery request when running
28+
* a test suite on the JUnit Platform.
29+
*
30+
* @since 1.11
31+
* @see DisableParentConfigurationParameters
32+
* @see Suite
33+
* @see org.junit.platform.runner.JUnitPlatform
34+
* @see org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder#configurationParameter(String, String)
35+
*/
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@Target(ElementType.TYPE)
38+
@Inherited
39+
@Documented
40+
// TODO: Before PR merge, change to STABLE/MAINTAINED
41+
// TODO: Is version 1.11 correct?
42+
@API(status = EXPERIMENTAL, since = "1.11")
43+
@Repeatable(ConfigurationParametersResources.class)
44+
public @interface ConfigurationParametersResource {
45+
46+
/**
47+
* The classpath location for the desired properties file; never {@code null} or blank.
48+
*/
49+
String resource();
50+
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2015-2023 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.suite.api;
12+
13+
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
14+
15+
import java.lang.annotation.Documented;
16+
import java.lang.annotation.ElementType;
17+
import java.lang.annotation.Inherited;
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
20+
import java.lang.annotation.Target;
21+
22+
import org.apiguardian.api.API;
23+
24+
/**
25+
* {@code @ConfigurationParametersResources} is a container for one or more
26+
* {@link ConfigurationParametersResource @ConfigurationParametersResource} declarations.
27+
*
28+
* <p>Note, however, that use of the {@code @ConfigurationParametersResources} container
29+
* is completely optional since {@code @ConfigurationParametersResource} is a
30+
* {@linkplain java.lang.annotation.Repeatable repeatable} annotation.
31+
*
32+
* @since 1.8
33+
* @see ConfigurationParametersResource
34+
*/
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Target(ElementType.TYPE)
37+
@Inherited
38+
@Documented
39+
// TODO: Before PR merge, change to STABLE/MAINTAINED
40+
// TODO: Is version 1.11 correct?
41+
@API(status = EXPERIMENTAL, since = "1.11")
42+
public @interface ConfigurationParametersResources {
43+
44+
/**
45+
* An array of one or more {@link ConfigurationParametersResource @ConfigurationParameterResource}
46+
* declarations.
47+
*/
48+
ConfigurationParametersResource[] value();
49+
50+
}

junit-platform-suite-commons/src/main/java/org/junit/platform/suite/commons/SuiteLauncherDiscoveryRequestBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.junit.platform.launcher.TagFilter;
4646
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
4747
import org.junit.platform.suite.api.ConfigurationParameter;
48+
import org.junit.platform.suite.api.ConfigurationParametersResource;
4849
import org.junit.platform.suite.api.DisableParentConfigurationParameters;
4950
import org.junit.platform.suite.api.ExcludeClassNamePatterns;
5051
import org.junit.platform.suite.api.ExcludeEngines;
@@ -115,6 +116,11 @@ public SuiteLauncherDiscoveryRequestBuilder configurationParameters(Map<String,
115116
return this;
116117
}
117118

119+
public SuiteLauncherDiscoveryRequestBuilder configurationParametersResource(String resourceFile) {
120+
delegate.configurationParametersResource(resourceFile);
121+
return this;
122+
}
123+
118124
public SuiteLauncherDiscoveryRequestBuilder parentConfigurationParameters(
119125
ConfigurationParameters parentConfigurationParameters) {
120126
this.parentConfigurationParameters = parentConfigurationParameters;
@@ -133,6 +139,8 @@ public SuiteLauncherDiscoveryRequestBuilder suite(Class<?> suiteClass) {
133139
// @formatter:off
134140
findRepeatableAnnotations(suiteClass, ConfigurationParameter.class)
135141
.forEach(configuration -> configurationParameter(configuration.key(), configuration.value()));
142+
findRepeatableAnnotations(suiteClass, ConfigurationParametersResource.class)
143+
.forEach(configResource -> configurationParametersResource(configResource.resource()));
136144
findAnnotation(suiteClass, DisableParentConfigurationParameters.class)
137145
.ifPresent(__ -> enableParentConfigurationParameters = false);
138146
findAnnotationValues(suiteClass, ExcludeClassNamePatterns.class, ExcludeClassNamePatterns::value)

platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilderTests.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,48 @@ void multipleConfigurationParametersAddedByMap_areStoredInDiscoveryRequest() {
313313
assertThat(configParams.get("key1")).contains("value1");
314314
assertThat(configParams.get("key2")).contains("value2");
315315
}
316+
317+
@Test
318+
void configurationParametersResource_areStoredInDiscoveryRequest() {
319+
// @formatter:off
320+
var discoveryRequest = request()
321+
.configurationParametersResource("config-test.properties")
322+
.build();
323+
// @formatter:on
324+
325+
var configParams = discoveryRequest.getConfigurationParameters();
326+
assertThat(configParams.get("com.example.prop.first")).contains("first value");
327+
assertThat(configParams.get("com.example.prop.second")).contains("second value");
328+
assertThat(configParams.get("com.example.prop.third")).contains("third value");
329+
}
330+
331+
@Test
332+
void configurationParametersResource_explicitConfigParametersOverrideResource() {
333+
// @formatter:off
334+
var discoveryRequest = request()
335+
.configurationParametersResource("config-test.properties")
336+
.configurationParameter("com.example.prop.first", "first value override")
337+
.build();
338+
// @formatter:on
339+
340+
var configParams = discoveryRequest.getConfigurationParameters();
341+
assertThat(configParams.get("com.example.prop.first")).contains("first value override");
342+
assertThat(configParams.get("com.example.prop.second")).contains("second value");
343+
}
344+
345+
@Test
346+
void configurationParametersResource_lastDeclaredResourceFileWins() {
347+
// @formatter:off
348+
var discoveryRequest = request()
349+
.configurationParametersResource("config-test.properties")
350+
.configurationParametersResource("config-test-override.properties")
351+
.build();
352+
// @formatter:on
353+
354+
var configParams = discoveryRequest.getConfigurationParameters();
355+
assertThat(configParams.get("com.example.prop.first")).contains("first value from override file");
356+
assertThat(configParams.get("com.example.prop.second")).contains("second value");
357+
}
316358
}
317359

318360
@Nested

0 commit comments

Comments
 (0)