-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Document how to use CloseableResource
#3840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcphilipp
merged 3 commits into
junit-team:main
from
SveinKare:1555-document-closableresource
Jul 9, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,58 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URI; | ||
| import java.net.URL; | ||
|
|
||
| import com.sun.net.httpserver.HttpServer; | ||
|
|
||
| import example.extensions.HttpServerExtension; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
|
|
||
| // tag::user_guide[] | ||
| @ExtendWith(HttpServerExtension.class) | ||
| public class HttpServerDemo { | ||
|
|
||
| // end::user_guide[] | ||
| @SuppressWarnings("HttpUrlsUsage") | ||
| // tag::user_guide[] | ||
| @Test | ||
| void httpCall(HttpServer server) throws Exception { | ||
| String hostName = server.getAddress().getHostName(); | ||
| int port = server.getAddress().getPort(); | ||
| String rawUrl = String.format("http://%s:%d/example", hostName, port); | ||
| URL requestUrl = URI.create(rawUrl).toURL(); | ||
|
|
||
| String responseBody = sendRequest(requestUrl); | ||
|
|
||
| assertEquals("This is a test", responseBody); | ||
| } | ||
|
|
||
| private static String sendRequest(URL url) throws IOException { | ||
| HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
| int contentLength = connection.getContentLength(); | ||
| try (InputStream response = url.openStream()) { | ||
| byte[] content = new byte[contentLength]; | ||
| assertEquals(contentLength, response.read(content)); | ||
| return new String(content, UTF_8); | ||
| } | ||
| } | ||
| } | ||
| // end::user_guide[] |
50 changes: 50 additions & 0 deletions
50
documentation/src/test/java/example/extensions/HttpServerExtension.java
This file contains hidden or 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,50 @@ | ||
| /* | ||
| * 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.UncheckedIOException; | ||
|
|
||
| import com.sun.net.httpserver.HttpServer; | ||
|
|
||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
| import org.junit.jupiter.api.extension.ParameterContext; | ||
| import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
|
||
| // tag::user_guide[] | ||
| public class HttpServerExtension implements ParameterResolver { | ||
|
|
||
| @Override | ||
| public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
| return HttpServer.class.equals(parameterContext.getParameter().getType()); | ||
| } | ||
|
|
||
| @Override | ||
| public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
|
|
||
| ExtensionContext rootContext = extensionContext.getRoot(); | ||
| ExtensionContext.Store store = rootContext.getStore(Namespace.GLOBAL); | ||
| String key = HttpServerResource.class.getName(); | ||
| HttpServerResource resource = store.getOrComputeIfAbsent(key, __ -> { | ||
| try { | ||
| HttpServerResource serverResource = new HttpServerResource(0); | ||
| serverResource.start(); | ||
| return serverResource; | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to create HttpServerResource", e); | ||
| } | ||
| }, HttpServerResource.class); | ||
| return resource.getHttpServer(); | ||
| } | ||
| } | ||
| // end::user_guide[] | ||
74 changes: 74 additions & 0 deletions
74
documentation/src/test/java/example/extensions/HttpServerResource.java
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * 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 static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.net.InetAddress; | ||
| 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[] | ||
| 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[] | ||
| HttpServerResource(int port) throws IOException { | ||
| InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); | ||
| this.httpServer = HttpServer.create(new InetSocketAddress(loopbackAddress, port), 0); | ||
| } | ||
|
|
||
| HttpServer getHttpServer() { | ||
| return httpServer; | ||
| } | ||
|
|
||
| // end::user_guide[] | ||
|
|
||
| /** | ||
| * Starts the Http server with an example handler. | ||
| */ | ||
| // tag::user_guide[] | ||
| void start() { | ||
| // Example handler | ||
| httpServer.createContext("/example", exchange -> { | ||
| String body = "This is a test"; | ||
| exchange.sendResponseHeaders(200, body.length()); | ||
| try (OutputStream os = exchange.getResponseBody()) { | ||
| os.write(body.getBytes(UTF_8)); | ||
| } | ||
| }); | ||
| httpServer.setExecutor(null); | ||
| httpServer.start(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| httpServer.stop(0); | ||
| } | ||
| } | ||
| // end::user_guide[] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.