Skip to content

Commit

Permalink
Use a unified invocation interceptor treating all the same by default
Browse files Browse the repository at this point in the history
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
MaisiKoleni committed Sep 10, 2020
1 parent 601449f commit c5e1e78
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import static de.tum.in.test.api.internal.TimeoutUtils.performTimeoutExecution;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Optional;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;

import de.tum.in.test.api.StrictTimeout;
Expand All @@ -21,59 +19,11 @@
* @author Christian Femers
*
*/
public class JupiterStrictTimeoutExtension implements InvocationInterceptor {
public class JupiterStrictTimeoutExtension implements UnifiedInvocationInterceptor {

@Override
public <T> T interceptTestClassConstructor(Invocation<T> invocation,
ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext)
throws Throwable {
public <T> T interceptGenericInvocation(Invocation<T> invocation, ExtensionContext extensionContext,
Optional<ReflectiveInvocationContext<?>> invocationContext) throws Throwable {
return performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptBeforeAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public <T> T interceptTestFactoryMethod(Invocation<T> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
return performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}

@Override
public void interceptAfterAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
performTimeoutExecution(invocation::proceed, JupiterContext.of(extensionContext));
}
}
61 changes: 3 additions & 58 deletions src/main/java/de/tum/in/test/api/jupiter/JupiterTestGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static de.tum.in.test.api.localization.Messages.formatLocalized;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.time.ZonedDateTime;
import java.util.Optional;
Expand All @@ -14,7 +13,6 @@
import org.apiguardian.api.API.Status;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;

import de.tum.in.test.api.Deadline;
Expand All @@ -27,66 +25,13 @@
* @author Christian Femers
*/
@API(status = Status.INTERNAL)
public final class JupiterTestGuard implements InvocationInterceptor, DisplayNameGenerator {
public final class JupiterTestGuard implements UnifiedInvocationInterceptor, DisplayNameGenerator {

private final DisplayNameGenerator defaultNameGen = new DisplayNameGenerator.ReplaceUnderscores();

@Override
public void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

@Override
public <T> T interceptTestFactoryMethod(Invocation<T> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
return proceedWithInvocation(invocation, extensionContext);
}

@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

@Override
public <T> T interceptTestClassConstructor(Invocation<T> invocation,
ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext)
throws Throwable {
return proceedWithInvocation(invocation, extensionContext);
}

@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

@Override
public void interceptBeforeAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

@Override
public void interceptAfterAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
proceedWithInvocation(invocation, extensionContext);
}

private static <T> T proceedWithInvocation(Invocation<T> invocation, ExtensionContext extensionContext)
throws Throwable {
public <T> T interceptGenericInvocation(Invocation<T> invocation, ExtensionContext extensionContext,
Optional<ReflectiveInvocationContext<?>> invocationContext) throws Throwable {
JupiterContext jupiterContext = JupiterContext.of(extensionContext);
checkForHidden(jupiterContext);
return doProceedAndPostProcess(invocation, jupiterContext);
Expand Down
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;
}

0 comments on commit c5e1e78

Please sign in to comment.