forked from junit-team/junit5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documented ClosableResource in user guide, with an example use-case f…
…or an HttpServer resource. Issue: junit-team#1555
- Loading branch information
Showing
3 changed files
with
121 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
documentation/src/test/java/example/extensions/HttpServerExtension.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,37 @@ | ||
/* | ||
* Copyright 2015-2024 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.extensions; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
|
||
// tag::user_guide[] | ||
public class HttpServerExtension implements BeforeAllCallback { | ||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
HttpServerResource resource = context.getRoot().getStore(Namespace.GLOBAL).getOrComputeIfAbsent( | ||
HttpServerResource.class.getName(), key -> { | ||
try { | ||
HttpServerResource serverResource = new HttpServerResource(8080); | ||
serverResource.start(); | ||
return serverResource; | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException("Failed to create HttpServerResource", e); | ||
} | ||
}, HttpServerResource.class); | ||
// Now you can use the resource within your tests | ||
} | ||
} | ||
// end::user_guide[] |
64 changes: 64 additions & 0 deletions
64
documentation/src/test/java/example/extensions/HttpServerResource.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,64 @@ | ||
/* | ||
* Copyright 2015-2024 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.extensions; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource; | ||
|
||
/** | ||
* Demonstrates an implementation of {@link CloseableResource} using an {@link HttpServer}. | ||
*/ | ||
// tag::user_guide[] | ||
public class HttpServerResource implements CloseableResource { | ||
private final HttpServer httpServer; | ||
|
||
// end::user_guide[] | ||
/** | ||
* Initializes the Http server resource, using the given port. | ||
* | ||
* @param port (int) The port number for the server, must be in the range 0-65535. | ||
* @throws IOException if an IOException occurs during initialization. | ||
*/ | ||
// tag::user_guide[] | ||
public HttpServerResource(int port) throws IOException { | ||
this.httpServer = HttpServer.create(new InetSocketAddress(port), 0); | ||
} | ||
|
||
// end::user_guide[] | ||
|
||
/** | ||
* Starts the Http server with an example handler. | ||
*/ | ||
// tag::user_guide[] | ||
public void start() { | ||
//Example handler | ||
httpServer.createContext("/example", exchange -> { | ||
String test = "This is a test."; | ||
exchange.sendResponseHeaders(200, test.length()); | ||
try (OutputStream os = exchange.getResponseBody()) { | ||
os.write(test.getBytes()); | ||
} | ||
}); | ||
httpServer.setExecutor(null); | ||
httpServer.start(); | ||
} | ||
|
||
@Override | ||
public void close() throws Throwable { | ||
httpServer.stop(0); | ||
} | ||
} | ||
// end::user_guide[] |