Skip to content

Commit 6ab41ac

Browse files
committed
Introduce utility method for finding files in tests
1 parent d8a375f commit 6ab41ac

File tree

5 files changed

+37
-45
lines changed

5 files changed

+37
-45
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2015-2024 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.reporting.testutil;
12+
13+
import java.io.IOException;
14+
import java.io.UncheckedIOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
18+
public class FileUtils {
19+
20+
public static Path findPath(Path rootDir, String syntaxAndPattern) {
21+
var matcher = rootDir.getFileSystem().getPathMatcher(syntaxAndPattern);
22+
try (var files = Files.walk(rootDir)) {
23+
return files.filter(matcher::matches).findFirst() //
24+
.orElseThrow(() -> new AssertionError(
25+
"Failed to find file matching '%s' in %s".formatted(syntaxAndPattern, rootDir)));
26+
}
27+
catch (IOException e) {
28+
throw new UncheckedIOException(e);
29+
}
30+
}
31+
}

platform-tests/src/test/java/org/junit/platform/jfr/FlightRecordingExecutionListenerIntegrationTests.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
1414
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;
1515
import static org.junit.platform.launcher.core.OutputDirectoryProviders.hierarchicalOutputDirectoryProvider;
16+
import static org.junit.platform.reporting.testutil.FileUtils.findPath;
1617
import static org.moditect.jfrunit.ExpectedEvent.event;
1718
import static org.moditect.jfrunit.JfrEventsAssert.assertThat;
1819

19-
import java.io.IOException;
20-
import java.io.UncheckedIOException;
2120
import java.nio.file.Files;
2221
import java.nio.file.Path;
2322

@@ -77,16 +76,6 @@ void reportsEvents(@TempDir Path tempDir) {
7776
.with("reason", "for demonstration purposes"));
7877
}
7978

80-
private static Path findPath(Path rootDir, @SuppressWarnings("SameParameterValue") String syntaxAndPattern) {
81-
var matcher = rootDir.getFileSystem().getPathMatcher(syntaxAndPattern);
82-
try (var files = Files.walk(rootDir)) {
83-
return files.filter(matcher::matches).findFirst().orElseThrow();
84-
}
85-
catch (IOException e) {
86-
throw new UncheckedIOException(e);
87-
}
88-
}
89-
9079
@SuppressWarnings("JUnitMalformedDeclaration")
9180
static class TestCase {
9281
@Test

platform-tests/src/test/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListenerTests.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
import static org.assertj.core.api.Assertions.assertThat;
1515
import static org.junit.jupiter.api.Assertions.fail;
1616
import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS;
17-
import static org.junit.platform.commons.util.CollectionUtils.getOnlyElement;
1817
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
1918
import static org.junit.platform.launcher.LauncherConstants.OUTPUT_DIR_PROPERTY_NAME;
2019
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;
2120
import static org.junit.platform.launcher.core.LauncherFactoryForTestingPurposesOnly.createLauncher;
2221
import static org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener.ENABLED_PROPERTY_NAME;
22+
import static org.junit.platform.reporting.testutil.FileUtils.findPath;
2323

24-
import java.io.IOException;
2524
import java.net.URISyntaxException;
26-
import java.nio.file.Files;
2725
import java.nio.file.Path;
2826

2927
import org.junit.jupiter.api.Test;
@@ -58,7 +56,7 @@ void writesValidXmlReport() throws Exception {
5856

5957
executeTests(engine);
6058

61-
var xmlFile = findXmlReport();
59+
var xmlFile = findPath(tempDirectory, "glob:**/open-test-report.xml");
6260
assertThat(tempDirectory.relativize(xmlFile).toString()) //
6361
.matches("junit-\\d+[/\\\\]open-test-report.xml");
6462
assertThat(validate(xmlFile)).isEmpty();
@@ -138,13 +136,4 @@ private void executeTests(TestEngine engine) {
138136
createLauncher(engine).execute(build, new OpenTestReportGeneratingListener());
139137
}
140138

141-
private Path findXmlReport() throws IOException {
142-
try ( //
143-
var topLevel = Files.list(tempDirectory); //
144-
var nested = Files.list(getOnlyElement(topLevel.toList())) //
145-
) {
146-
return getOnlyElement(nested.toList());
147-
}
148-
}
149-
150139
}

platform-tooling-support-tests/platform-tooling-support-tests.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ dependencies {
7171
}
7272
testImplementation(libs.bundles.xmlunit)
7373
testImplementation(testFixtures(projects.junitJupiterApi))
74+
testImplementation(testFixtures(projects.junitPlatformReporting))
7475

7576
thirdPartyJars(libs.junit4)
7677
thirdPartyJars(libs.assertj)

platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/XmlAssertions.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010

1111
package platform.tooling.support.tests;
1212

13-
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.junit.platform.reporting.testutil.FileUtils.findPath;
1414

15-
import java.io.IOException;
16-
import java.io.UncheckedIOException;
17-
import java.nio.file.Files;
1815
import java.nio.file.Path;
1916

2017
import org.xmlunit.assertj3.XmlAssert;
@@ -23,25 +20,10 @@
2320
class XmlAssertions {
2421

2522
static void verifyContainsExpectedStartedOpenTestReport(Path testResultsDir) {
26-
var xmlFile = findOpenTestReport(testResultsDir);
23+
var xmlFile = findPath(testResultsDir, "glob:**/open-test-report.xml");
2724
verifyContent(xmlFile);
2825
}
2926

30-
private static Path findOpenTestReport(Path testResultsDir) {
31-
try (var files = Files.list(testResultsDir)) {
32-
var outputDir = files.filter(
33-
it -> Files.isDirectory(it) && it.getFileName().toString().startsWith("junit-")) //
34-
.findAny() //
35-
.orElseThrow(() -> new AssertionError("Missing JUnit output dir in " + testResultsDir));
36-
var xmlFile = outputDir.resolve("open-test-report.xml");
37-
assertThat(xmlFile).exists();
38-
return xmlFile;
39-
}
40-
catch (IOException e) {
41-
throw new UncheckedIOException(e);
42-
}
43-
}
44-
4527
private static void verifyContent(Path xmlFile) {
4628
var expected = """
4729
<e:events xmlns="https://schemas.opentest4j.org/reporting/core/0.2.0"

0 commit comments

Comments
 (0)