-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Document how to use CloseableResource
#3840
Merged
marcphilipp
merged 3 commits into
junit-team:main
from
SveinKare:1555-document-closableresource
Jul 9, 2024
Merged
Changes from 1 commit
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 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. | ||
marcphilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* 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[] |
Oops, something went wrong.
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.
This comment was marked as resolved.
Sorry, something went wrong.