Search in sources :

Example 1 with CloseableResource

use of org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource in project junit5 by junit-team.

the class ExtensionValuesStore method closeAllStoredCloseableValues.

/**
 * Close all values that implement {@link CloseableResource}.
 *
 * @implNote Only close values stored in this instance. This implementation
 * does not close values in parent stores.
 */
public void closeAllStoredCloseableValues() {
    ThrowableCollector throwableCollector = createThrowableCollector();
    // 
    storedValues.values().stream().filter(// 
    storedValue -> storedValue.evaluateSafely() instanceof CloseableResource).sorted(// 
    REVERSE_INSERT_ORDER).map(// 
    storedValue -> (CloseableResource) storedValue.evaluate()).forEach(resource -> throwableCollector.execute(resource::close));
    throwableCollector.assertEmpty();
}
Also used : ReflectionUtils.getWrapperType(org.junit.platform.commons.util.ReflectionUtils.getWrapperType) ReflectionUtils.isAssignableTo(org.junit.platform.commons.util.ReflectionUtils.isAssignableTo) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ThrowableCollector(org.junit.platform.engine.support.hierarchical.ThrowableCollector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) API(org.apiguardian.api.API) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) Lock(java.util.concurrent.locks.Lock) ExtensionContextException(org.junit.jupiter.api.extension.ExtensionContextException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JupiterThrowableCollectorFactory.createThrowableCollector(org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector) Optional(java.util.Optional) Comparator(java.util.Comparator) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) ThrowableCollector(org.junit.platform.engine.support.hierarchical.ThrowableCollector) JupiterThrowableCollectorFactory.createThrowableCollector(org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector)

Aggregations

Comparator (java.util.Comparator)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 API (org.apiguardian.api.API)1 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 Namespace (org.junit.jupiter.api.extension.ExtensionContext.Namespace)1 CloseableResource (org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource)1 ExtensionContextException (org.junit.jupiter.api.extension.ExtensionContextException)1 JupiterThrowableCollectorFactory.createThrowableCollector (org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector)1 ReflectionUtils.getWrapperType (org.junit.platform.commons.util.ReflectionUtils.getWrapperType)1 ReflectionUtils.isAssignableTo (org.junit.platform.commons.util.ReflectionUtils.isAssignableTo)1 ThrowableCollector (org.junit.platform.engine.support.hierarchical.ThrowableCollector)1