-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LauncherSessionListener example to user guide
- Loading branch information
1 parent
439084d
commit b6dff0d
Showing
4 changed files
with
161 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
documentation/src/test/java/example/session/GlobalSetupTeardownListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2015-2021 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 v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.session; | ||
|
||
//tag::user_guide[] | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import org.junit.platform.launcher.LauncherSession; | ||
import org.junit.platform.launcher.LauncherSessionListener; | ||
import org.junit.platform.launcher.TestExecutionListener; | ||
import org.junit.platform.launcher.TestPlan; | ||
|
||
public class GlobalSetupTeardownListener implements LauncherSessionListener { | ||
|
||
private Fixture fixture; | ||
|
||
@Override | ||
public void launcherSessionOpened(LauncherSession session) { | ||
// Avoid setup for test discovery by delaying it until tests are about to be executed | ||
session.getLauncher().registerTestExecutionListeners(new TestExecutionListener() { | ||
@Override | ||
public void testPlanExecutionStarted(TestPlan testPlan) { | ||
if (fixture == null) { | ||
fixture = new Fixture(); | ||
fixture.setUp(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void launcherSessionClosed(LauncherSession session) { | ||
if (fixture != null) { | ||
fixture.tearDown(); | ||
fixture = null; | ||
} | ||
} | ||
|
||
static class Fixture { | ||
|
||
private HttpServer server; | ||
private ExecutorService executorService; | ||
|
||
void setUp() { | ||
try { | ||
server = HttpServer.create(new InetSocketAddress(0), 0); | ||
} | ||
catch (IOException e) { | ||
throw new UncheckedIOException("Failed to start HTTP server", e); | ||
} | ||
server.createContext("/test", exchange -> { | ||
exchange.sendResponseHeaders(204, -1); | ||
exchange.close(); | ||
}); | ||
executorService = Executors.newCachedThreadPool(); | ||
server.setExecutor(executorService); | ||
server.start(); // <1> | ||
int port = server.getAddress().getPort(); | ||
System.setProperty("http.server.port", String.valueOf(port)); // <2> | ||
} | ||
|
||
void tearDown() { | ||
server.stop(0); // <3> | ||
executorService.shutdownNow(); | ||
} | ||
} | ||
|
||
} | ||
//end::user_guide[] |
35 changes: 35 additions & 0 deletions
35
documentation/src/test/java/example/session/HttpTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2015-2021 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 v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.session; | ||
|
||
//tag::user_guide[] | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class HttpTests { | ||
|
||
@Test | ||
void respondsWith204() throws Exception { | ||
String port = System.getProperty("http.server.port"); // <1> | ||
URL url = new URL("http://localhost:" + port + "/test"); | ||
|
||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
int responseCode = connection.getResponseCode(); // <2> | ||
|
||
assertEquals(204, responseCode); // <3> | ||
} | ||
} | ||
//end::user_guide[] |
1 change: 1 addition & 0 deletions
1
.../src/test/resources/META-INF/services/org.junit.platform.launcher.LauncherSessionListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
example.session.GlobalSetupTeardownListener |