-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a unified invocation interceptor treating all the same by default
This allows for cleaner code, as we don't need to override all methods each time we implement an invocation interceptor that has to cover all cases.
- Loading branch information
1 parent
601449f
commit c5e1e78
Showing
3 changed files
with
102 additions
and
112 deletions.
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
95 changes: 95 additions & 0 deletions
95
src/main/java/de/tum/in/test/api/jupiter/UnifiedInvocationInterceptor.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,95 @@ | ||
package de.tum.in.test.api.jupiter; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.InvocationInterceptor; | ||
import org.junit.jupiter.api.extension.ReflectiveInvocationContext; | ||
|
||
/** | ||
* Combines all invocation interceptions into one method called | ||
* {@link #interceptGenericInvocation(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation, ExtensionContext, Optional) | ||
* interceptGenericInvocation}. | ||
* <p> | ||
* For derivations from the generic interception for special cases, override the | ||
* corresponding default method. | ||
* | ||
* @author Christian Femers | ||
* | ||
*/ | ||
public interface UnifiedInvocationInterceptor extends InvocationInterceptor { | ||
|
||
@Override | ||
default <T> T interceptTestClassConstructor(Invocation<T> invocation, | ||
ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
return interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default void interceptBeforeAllMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default void interceptBeforeEachMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, | ||
ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default <T> T interceptTestFactoryMethod(Invocation<T> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
return interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default void interceptTestTemplateMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.empty()); | ||
} | ||
|
||
@Override | ||
default void interceptAfterEachMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
@Override | ||
default void interceptAfterAllMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { | ||
interceptGenericInvocation(invocation, extensionContext, Optional.of(invocationContext)); | ||
} | ||
|
||
/** | ||
* Called for all invocation types unless the default method of this interface | ||
* is explicitly overridden. | ||
* | ||
* @param <T> the result type of the invocation, often just | ||
* {@link Void} | ||
* @param invocation the invocation that is being intercepted; never null | ||
* @param extensionContext the current extension context; never null | ||
* @param invocationContext the context of the invocation that is being | ||
* intercepted; never null; but may be empty for | ||
* {@link DynamicTest}s | ||
* @return the result of the invocation; potentially null | ||
* @author Christian Femers | ||
*/ | ||
<T> T interceptGenericInvocation(Invocation<T> invocation, ExtensionContext extensionContext, | ||
Optional<ReflectiveInvocationContext<?>> invocationContext) throws Throwable; | ||
} |