|
24 | 24 | import static org.junit.platform.launcher.core.LauncherFactoryForTestingPurposesOnly.createLauncher; |
25 | 25 | import static org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener.ENABLED_PROPERTY_NAME; |
26 | 26 | import static org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener.GIT_ENABLED_PROPERTY_NAME; |
| 27 | +import static org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener.SOCKET_PROPERTY_NAME; |
27 | 28 | import static org.junit.platform.reporting.testutil.FileUtils.findPath; |
28 | 29 |
|
| 30 | +import java.io.BufferedReader; |
| 31 | +import java.io.InputStreamReader; |
29 | 32 | import java.io.PrintStream; |
| 33 | +import java.net.InetAddress; |
| 34 | +import java.net.ServerSocket; |
| 35 | +import java.net.Socket; |
30 | 36 | import java.net.URISyntaxException; |
| 37 | +import java.nio.charset.StandardCharsets; |
31 | 38 | import java.nio.file.Files; |
32 | 39 | import java.nio.file.Path; |
| 40 | +import java.time.Duration; |
33 | 41 | import java.util.Map; |
34 | 42 |
|
35 | 43 | import org.junit.jupiter.api.AfterEach; |
@@ -265,6 +273,90 @@ void stripsCredentialsFromOriginUrl(String configuredUrl, String reportedUrl, @T |
265 | 273 | .isEqualTo(reportedUrl); |
266 | 274 | } |
267 | 275 |
|
| 276 | + @Test |
| 277 | + void writesXmlReportToSocket(@TempDir Path tempDirectory) throws Exception { |
| 278 | + var engine = new DemoHierarchicalTestEngine("dummy"); |
| 279 | + engine.addTest("test1", "Test 1", (context, descriptor) -> { |
| 280 | + // Simple test |
| 281 | + }); |
| 282 | + |
| 283 | + // Start a server socket to receive the XML |
| 284 | + var builder = new StringBuilder(); |
| 285 | + |
| 286 | + try (var serverSocket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) { // Use any available port |
| 287 | + int port = serverSocket.getLocalPort(); |
| 288 | + |
| 289 | + // Start a daemon thread to accept the connection and read the XML |
| 290 | + Thread serverThread = new Thread(() -> { |
| 291 | + try (Socket clientSocket = serverSocket.accept(); |
| 292 | + var reader = new BufferedReader( |
| 293 | + new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8))) { |
| 294 | + String line; |
| 295 | + while ((line = reader.readLine()) != null) { |
| 296 | + builder.append(line).append("\n"); |
| 297 | + } |
| 298 | + } |
| 299 | + catch (Exception e) { |
| 300 | + fail(e); |
| 301 | + } |
| 302 | + }); |
| 303 | + serverThread.setDaemon(true); |
| 304 | + serverThread.start(); |
| 305 | + |
| 306 | + // Execute tests with socket configuration |
| 307 | + executeTests(tempDirectory, engine, tempDirectory.resolve("junit-reports"), |
| 308 | + Map.of(SOCKET_PROPERTY_NAME, String.valueOf(port))); |
| 309 | + |
| 310 | + // Wait for the server to receive the data |
| 311 | + assertThat(serverThread.join(Duration.ofSeconds(10))).isTrue(); |
| 312 | + |
| 313 | + // Verify XML was received |
| 314 | + var expected = """ |
| 315 | + <e:events xmlns="https://schemas.opentest4j.org/reporting/core/0.2.0" |
| 316 | + xmlns:e="https://schemas.opentest4j.org/reporting/events/0.2.0" |
| 317 | + xmlns:git="https://schemas.opentest4j.org/reporting/git/0.2.0" |
| 318 | + xmlns:java="https://schemas.opentest4j.org/reporting/java/0.2.0" |
| 319 | + xmlns:junit="https://schemas.junit.org/open-test-reporting" |
| 320 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 321 | + xsi:schemaLocation="https://schemas.junit.org/open-test-reporting https://schemas.junit.org/open-test-reporting/junit-1.9.xsd"> |
| 322 | + <infrastructure> |
| 323 | + <hostName>${xmlunit.ignore}</hostName> |
| 324 | + <userName>${xmlunit.ignore}</userName> |
| 325 | + <operatingSystem>${xmlunit.ignore}</operatingSystem> |
| 326 | + <cpuCores>${xmlunit.ignore}</cpuCores> |
| 327 | + <java:javaVersion>${xmlunit.ignore}</java:javaVersion> |
| 328 | + <java:fileEncoding>${xmlunit.ignore}</java:fileEncoding> |
| 329 | + <java:heapSize max="${xmlunit.isNumber}"/> |
| 330 | + </infrastructure> |
| 331 | + <e:started id="1" name="dummy" time="${xmlunit.isDateTime}"> |
| 332 | + <metadata> |
| 333 | + <junit:uniqueId>[engine:dummy]</junit:uniqueId> |
| 334 | + <junit:legacyReportingName>dummy</junit:legacyReportingName> |
| 335 | + <junit:type>CONTAINER</junit:type> |
| 336 | + </metadata> |
| 337 | + </e:started> |
| 338 | + <e:started id="2" name="Test 1" parentId="1" time="${xmlunit.isDateTime}"> |
| 339 | + <metadata> |
| 340 | + <junit:uniqueId>[engine:dummy]/[test:test1]</junit:uniqueId> |
| 341 | + <junit:legacyReportingName>Test 1</junit:legacyReportingName> |
| 342 | + <junit:type>TEST</junit:type> |
| 343 | + </metadata> |
| 344 | + </e:started> |
| 345 | + <e:finished id="2" time="${xmlunit.isDateTime}"> |
| 346 | + <result status="SUCCESSFUL"/> |
| 347 | + </e:finished> |
| 348 | + <e:finished id="1" time="${xmlunit.isDateTime}"> |
| 349 | + <result status="SUCCESSFUL"/> |
| 350 | + </e:finished> |
| 351 | + </e:events> |
| 352 | + """; |
| 353 | + XmlAssert.assertThat(builder.toString()).and(expected) // |
| 354 | + .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()) // |
| 355 | + .ignoreWhitespace() // |
| 356 | + .areIdentical(); |
| 357 | + } |
| 358 | + } |
| 359 | + |
268 | 360 | private static XmlAssert assertThatXml(Path xmlFile) { |
269 | 361 | return XmlAssert.assertThat(xmlFile) // |
270 | 362 | .withNamespaceContext(NAMESPACE_CONTEXT); |
|
0 commit comments