-
-
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.
Document how to use
CloseableResource
(#3840)
Added an explanation in the user guide for how CloseableResource can be used, with an example using an HttpServer resource. Resolves #1555. Co-authored-by: Marc Philipp <[email protected]>
- Loading branch information
1 parent
0f9e2ad
commit b09aa8b
Showing
4 changed files
with
214 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
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,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 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 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[] |