-
-
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
Issues/3367 introduce autoclose #3369
Closed
ibrahimesseddyq
wants to merge
15
commits into
junit-team:main
from
ibrahimesseddyq:issues/3367-introduce-autoclose
Closed
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a92a1f6
Implement @AutoClose Annotation
ibrahimesseddyq 9335b31
add @API to annotation classes
ibrahimesseddyq f2c3149
remove test module (added by accident)
ibrahimesseddyq a3e2eaf
checkStyle passed successfully
ibrahimesseddyq 89d3b18
spotlessJavaCheck passed successfully
ibrahimesseddyq 5ebc85b
adapt to aggregateJavaDocs
ibrahimesseddyq 053849e
aggregateJavaDocs passed successfully
ibrahimesseddyq 6bea51c
aggregateJavaDocs, checkstyleMain and SpotlessJavaCheck passed succes…
ibrahimesseddyq c35b124
add user guide -> annotation -> @autoclose
ibrahimesseddyq f7773ff
Merge branch 'main' into issues/3367-introduce-autoclose
7917385
[iesseddyq][move autoClose to the engine and register it]
328bbcd
adjust autoclose
b0a53da
Revert "Refer to "arguments providers" in Javadoc"
19301cc
Merge branch 'main' into issues/3367-introduce-autoclose
54a2ecd
[ibrahimesseddyq][fix autoclose, refactoring code to functionnal styl…
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
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
54 changes: 54 additions & 0 deletions
54
junit-jupiter-api/src/main/java/org/junit/jupiter/api/AutoClose.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,54 @@ | ||
/* | ||
* Copyright 2015-2023 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 org.junit.jupiter.api; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* The {@code AutoClose} annotation is used to automatically close resources used in JUnit 5 tests. | ||
* | ||
* <p> | ||
* This annotation should be applied to fields within JUnit 5 test classes. It indicates that the annotated | ||
* resource should be automatically closed after the test execution. The annotation targets | ||
* {@link java.lang.annotation.ElementType#FIELD} elements, allowing it to be applied to instance variables. | ||
* </p> | ||
* | ||
* <p> | ||
* By default, the {@code AutoClose} annotation expects the annotated resource to provide a {@code close()} method | ||
* that will be invoked for closing the resource. However, developers can customize the closing behavior by providing | ||
* a different method name through the {@code value} attribute. For example, setting {@code value = "destroy"} will | ||
* look for a method named {@code destroy()} to close the resource. | ||
* </p> | ||
* | ||
* <p> | ||
* The {@code AutoClose} annotation is retained at runtime, allowing it to be accessed and processed during test execution. | ||
* </p> | ||
* | ||
* @see java.lang.annotation.Retention | ||
* @see java.lang.annotation.Target | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
@API(status = API.Status.EXPERIMENTAL, since = "5.9") | ||
public @interface AutoClose { | ||
/** | ||
* Specifies the name of the method to invoke for closing the resource. | ||
* The default value is "close". | ||
* | ||
* @return the method name for closing the resource | ||
*/ | ||
String value() default "close"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
92 changes: 92 additions & 0 deletions
92
junit-jupiter-api/src/main/java/org/junit/jupiter/api/AutoCloseUtils.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,92 @@ | ||
/* | ||
* Copyright 2015-2023 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 org.junit.jupiter.api; | ||
|
||
import java.io.Closeable; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* The {@code AutoCloseUtils} class provides utility methods for automatically closing resources used in JUnit 5 tests. | ||
* | ||
* <p> | ||
* The class includes a static method {@code closeResources} that accepts a test instance as a parameter and closes | ||
* all the fields annotated with {@link org.junit.jupiter.api.AutoClose}. This allows for automatic resource cleanup | ||
* after test execution. | ||
* </p> | ||
* | ||
* <p> | ||
* The {@code closeResources} method utilizes reflection to find the fields annotated with {@link org.junit.jupiter.api.AutoClose} | ||
* within the given test instance. It collects all the closeable fields and invokes the {@code close} method on each one. | ||
* </p> | ||
* | ||
* <p> | ||
* To be eligible for automatic closing, the fields must implement the {@link java.io.Closeable} interface. | ||
* </p> | ||
* | ||
* <p> | ||
* The {@code AutoCloseUtils} class is not intended to be instantiated, as all its methods are static. | ||
* </p> | ||
* | ||
* @see org.junit.jupiter.api.AutoClose | ||
* @see java.io.Closeable | ||
* @see java.lang.reflect.Field | ||
*/ | ||
@API(status = API.Status.EXPERIMENTAL, since = "5.9") | ||
public class AutoCloseUtils { | ||
private AutoCloseUtils() { | ||
// Private constructor to prevent instantiation | ||
} | ||
|
||
/** | ||
* Closes all the resources annotated with {@link org.junit.jupiter.api.AutoClose} within the given test instance. | ||
* | ||
* @param testInstance the test instance containing the resources to be closed | ||
*/ | ||
public static void closeResources(Object testInstance) { | ||
List<Closeable> closeables = findCloseableFields(testInstance); | ||
for (Closeable closeable : closeables) { | ||
close(closeable); | ||
} | ||
} | ||
|
||
private static List<Closeable> findCloseableFields(Object testInstance) { | ||
List<Closeable> closeables = new ArrayList<>(); | ||
Field[] fields = testInstance.getClass().getDeclaredFields(); | ||
for (Field field : fields) { | ||
if (field.isAnnotationPresent(AutoClose.class)) { | ||
field.setAccessible(true); | ||
try { | ||
Object fieldValue = field.get(testInstance); | ||
if (fieldValue instanceof Closeable) { | ||
closeables.add((Closeable) fieldValue); | ||
} | ||
} | ||
catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return closeables; | ||
} | ||
|
||
private static void close(Closeable closeable) { | ||
try { | ||
closeable.close(); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/AutoCloseExtension.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-2023 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 org.junit.jupiter.api.extension; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.jupiter.api.AutoCloseUtils; | ||
|
||
/** | ||
* The {@code AutoCloseExtension} class is a JUnit 5 extension that automatically closes resources used in tests. | ||
* | ||
* <p> | ||
* This extension implements the {@link org.junit.jupiter.api.extension.AfterEachCallback} interface, | ||
* allowing it to perform resource cleanup after each test execution. It invokes the | ||
* {@link AutoCloseUtils#closeResources(Object)} method to close the resources annotated with | ||
* {@link org.junit.jupiter.api.AutoClose}. | ||
* </p> | ||
* | ||
* <p> | ||
* To use this extension, annotate your test class or test method with {@link org.junit.jupiter.api.extension.ExtendWith} | ||
* and provide an instance of {@code AutoCloseExtension}. | ||
* </p> | ||
* | ||
* @see org.junit.jupiter.api.extension.AfterEachCallback | ||
* @see org.junit.jupiter.api.extension.Extension | ||
* @see org.junit.jupiter.api.extension.ExtensionContext | ||
* @see org.junit.jupiter.api.AutoClose | ||
* @see AutoCloseUtils#closeResources(Object) | ||
*/ | ||
@API(status = API.Status.EXPERIMENTAL, since = "5.9") | ||
public class AutoCloseExtension implements AfterEachCallback { | ||
|
||
/** | ||
* Creates a new instance of AutoCloseExtension. | ||
*/ | ||
public AutoCloseExtension() { | ||
|
||
} | ||
|
||
/** | ||
* Invoked after each test execution to close the annotated resources within the test instance. | ||
* | ||
* @param context the extension context for the current test execution | ||
* @throws Exception if an exception occurs during resource cleanup | ||
*/ | ||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
Object testInstance = context.getRequiredTestInstance(); | ||
try { | ||
AutoCloseUtils.closeResources(testInstance); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
throw e; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should work out of the box like
@TempDir
without specifying@ExtendWith(AutoCloseExtension.class)
explicitly.As
@AutoClose
is an opt-in feature it should not interfere with other extension if this extension isn't used but registered.