Skip to content

Commit

Permalink
spotlessJavaCheck passed successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimesseddyq committed Jun 24, 2023
1 parent a3e2eaf commit 89d3b18
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 73 deletions.
11 changes: 5 additions & 6 deletions junit-jupiter-api/junit-jupiter-api.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ tasks {
bundle {
val version = project.version
bnd("""
Require-Capability:\
org.junit.platform.engine;\
filter:='(&(org.junit.platform.engine=junit-jupiter)(version>=${'$'}{version_cleanup;${version}})(!(version>=${'$'}{versionmask;+;${'$'}{version_cleanup;${version}}})))';\
effective:=active
""")
Require-Capability:\
org.junit.platform.engine;\
filter:='(&(org.junit.platform.engine=junit-jupiter)(version>=${'$'}{version_cleanup;${version}})(!(version>=${'$'}{versionmask;+;${'$'}{version_cleanup;${version}}})))';\
effective:=active
""")
}
}
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package org.junit.jupiter.api;
/*
* 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
*/

import org.apiguardian.api.API;
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.
*
Expand All @@ -32,13 +42,13 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@API(status = API.Status.EXPERIMENTAL,since = "5.9")
@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";
/**
* 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";
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
/*
* 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 org.apiguardian.api.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.
*
Expand All @@ -31,49 +43,50 @@
* @see java.io.Closeable
* @see java.lang.reflect.Field
*/
@API(status = API.Status.EXPERIMENTAL,since = "5.9")

@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 AutoCloseUtils() {
// Private constructor to prevent instantiation
}

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;
}
/**
* 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 void close(Closeable closeable) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
/*
* 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;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* The {@code AutoCloseExtension} class is a JUnit 5 extension that automatically closes resources used in tests.
*
Expand All @@ -24,20 +36,20 @@
* @see org.junit.jupiter.api.AutoClose
* @see AutoCloseUtils#closeResources(Object)
*/
@API(status = API.Status.EXPERIMENTAL,since = "5.9")

@API(status = API.Status.EXPERIMENTAL, since = "5.9")
public class AutoCloseExtension implements AfterEachCallback {
/**
* Invoked after each test execution to close the annotated resources within the test instance.
*
* @throws Exception if an exception occurs during resource cleanup
*/
AutoCloseExtension(){
/**
* Invoked after each test execution to close the annotated resources within the test instance.
*
* @throws Exception if an exception occurs during resource cleanup
*/
public AutoCloseExtension() {

}

}
@Override
public void afterEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
AutoCloseUtils.closeResources(testInstance);
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
AutoCloseUtils.closeResources(testInstance);
}
}

0 comments on commit 89d3b18

Please sign in to comment.