Skip to content

Commit 77b59e2

Browse files
committed
Ensure LauncherConfigurationParametersTests runs in all environments
Prior to this commit, the warnsOnMultiplePropertyResources() test in LauncherConfigurationParametersTests only ran within the Gradle build or within IntelliJ IDEA when configured with "build" as the target build directory. However, other environments might not use a "build" directory. For example, Eclipse uses a "bin" directory. This commit therefore removes the hardcoded "build/resources/test" path from the assertion and instead determines the URL for the resource by loading it via the ClassLoader. This commit also refactors the assertion to make it easier to follow.
1 parent 3194419 commit 77b59e2

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.platform.launcher.core;
1212

13+
import static org.assertj.core.api.Assertions.as;
1314
import static org.assertj.core.api.Assertions.assertThat;
1415
import static org.junit.jupiter.api.Assertions.assertEquals;
1516
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -18,12 +19,12 @@
1819
import java.net.URLClassLoader;
1920
import java.nio.file.Files;
2021
import java.nio.file.Path;
21-
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Properties;
2424
import java.util.logging.Level;
2525
import java.util.logging.LogRecord;
2626

27+
import org.assertj.core.api.InstanceOfAssertFactories;
2728
import org.junit.jupiter.api.AfterEach;
2829
import org.junit.jupiter.api.BeforeEach;
2930
import org.junit.jupiter.api.Test;
@@ -204,31 +205,32 @@ void ignoresSystemPropertyAndConfigFileWhenImplicitLookupsAreDisabled() {
204205
@Test
205206
void warnsOnMultiplePropertyResources(@TempDir Path tempDir, @TrackLogRecords LogRecordListener logRecordListener)
206207
throws Exception {
208+
207209
Properties properties = new Properties();
208210
properties.setProperty(KEY, "from second config file");
209211
try (var out = Files.newOutputStream(tempDir.resolve(CONFIG_FILE_NAME))) {
210212
properties.store(out, "");
211213
}
214+
212215
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
216+
URL originalResource = originalClassLoader.getResource(CONFIG_FILE_NAME);
217+
213218
try (var customClassLoader = new URLClassLoader(new URL[] { tempDir.toUri().toURL() }, originalClassLoader)) {
214219
Thread.currentThread().setContextClassLoader(customClassLoader);
215220
ConfigurationParameters configParams = fromMapAndFile(Map.of(), CONFIG_FILE_NAME);
216221

217222
assertThat(configParams.get(KEY)).contains(CONFIG_FILE);
218223

219-
List<String> loggedWarnings = logRecordListener.stream(Level.WARNING) //
220-
.map(LogRecord::getMessage) //
221-
.toList();
222-
assertThat(loggedWarnings) //
223-
.hasSize(1);
224-
assertThat(loggedWarnings.getFirst()) //
225-
.contains("Discovered 2 '" + CONFIG_FILE_NAME
226-
+ "' configuration files on the classpath (see below); only the first (*) will be used.") //
227-
.contains("- "
228-
+ Path.of(
229-
"build/resources/test/test-junit-platform.properties").toAbsolutePath().toUri().toURL()
230-
+ " (*)") //
231-
.contains("- " + tempDir.resolve(CONFIG_FILE_NAME).toUri().toURL());
224+
assertThat(logRecordListener.stream(Level.WARNING).map(LogRecord::getMessage)) //
225+
.hasSize(1) //
226+
.first(as(InstanceOfAssertFactories.STRING)) //
227+
.contains("""
228+
Discovered 2 '%s' configuration files on the classpath (see below); \
229+
only the first (*) will be used.
230+
- %s (*)
231+
- %s"""//
232+
.formatted(CONFIG_FILE_NAME, originalResource,
233+
tempDir.resolve(CONFIG_FILE_NAME).toUri().toURL()));
232234
}
233235
finally {
234236
Thread.currentThread().setContextClassLoader(originalClassLoader);

0 commit comments

Comments
 (0)