|
| 1 | +package io.airbyte.cdk.extensions; |
| 2 | + |
| 3 | +import java.io.PrintWriter; |
| 4 | +import java.io.StringWriter; |
| 5 | +import java.io.Writer; |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.lang.reflect.InvocationHandler; |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.lang.reflect.Proxy; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.StringJoiner; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import org.apache.commons.lang3.StringUtils; |
| 14 | +import org.apache.commons.lang3.builder.ToStringBuilder; |
| 15 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 16 | +import org.junit.jupiter.api.extension.DynamicTestInvocationContext; |
| 17 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 18 | +import org.junit.jupiter.api.extension.InvocationInterceptor; |
| 19 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +/** |
| 24 | + * This allows us to have junit loglines in our test logs. |
| 25 | + * This is instanciated via Java's ServiceLoader (https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/util/ServiceLoader.html) |
| 26 | + * The declaration can be found in resources/META-INF/services/org.junit.jupiter.api.extension.Extension |
| 27 | + */ |
| 28 | +public class LoggingInvocationInterceptor implements InvocationInterceptor { |
| 29 | + private static final class LoggingInvocationInterceptorHandler implements InvocationHandler { |
| 30 | + private static Logger LOGGER = LoggerFactory.getLogger(LoggingInvocationInterceptor.class); |
| 31 | + @Override |
| 32 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 33 | + if (LoggingInvocationInterceptor.class.getDeclaredMethod(method.getName(), Invocation.class, ReflectiveInvocationContext.class, ExtensionContext.class) == null) { |
| 34 | + LOGGER.error("Junit LoggingInvocationInterceptor executing unkown interception point " + method.getName()); |
| 35 | + return method.invoke(proxy, args); |
| 36 | + } |
| 37 | + Invocation<?> invocation= (Invocation<?>)args[0]; |
| 38 | + ReflectiveInvocationContext<Method> invocationContext = (ReflectiveInvocationContext<Method>)args[1]; |
| 39 | + ExtensionContext extensionContext = (ExtensionContext) args[2]; |
| 40 | + String methodName = method.getName(); |
| 41 | + String logLineSuffix; |
| 42 | + if (methodName.equals("interceptDynamicTest")) { |
| 43 | + logLineSuffix = "execution of DynamicTest " + extensionContext.getDisplayName(); |
| 44 | + } else if (methodName.equals("interceptTestClassConstructor")) { |
| 45 | + logLineSuffix = "instance creation for " + invocationContext.getTargetClass(); |
| 46 | + } else if (methodName.startsWith("intercept") && methodName.endsWith("Method")) { |
| 47 | + String interceptedEvent = methodName.substring("intercept".length(), methodName.length() - "Method".length()); |
| 48 | + logLineSuffix = "execution of @" + interceptedEvent + " method " + invocationContext.getExecutable().getDeclaringClass().getSimpleName() |
| 49 | + + "." + invocationContext.getExecutable().getName(); |
| 50 | + } else { |
| 51 | + logLineSuffix = "execution of unknown intercepted call " + methodName; |
| 52 | + } |
| 53 | + LOGGER.info("Junit starting " + logLineSuffix); |
| 54 | + try { |
| 55 | + long start = System.nanoTime(); |
| 56 | + Object retVal = invocation.proceed(); |
| 57 | + long elapsedMs = (System.nanoTime() - start)/1_000_000; |
| 58 | + LOGGER.info("Junit completed " + logLineSuffix + " in " + elapsedMs + "ms"); |
| 59 | + return retVal; |
| 60 | + } catch (Throwable t) { |
| 61 | + String stackTrace = Arrays.stream(ExceptionUtils.getStackFrames(t)).takeWhile(s->!s.startsWith("\tat org.junit")).collect( |
| 62 | + Collectors.joining("\n ")); |
| 63 | + LOGGER.warn("Junit exception throw during " + logLineSuffix + ":\n" + stackTrace); |
| 64 | + throw t; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private final InvocationInterceptor proxy = (InvocationInterceptor) Proxy.newProxyInstance( |
| 70 | + getClass().getClassLoader(), |
| 71 | + new Class[] {InvocationInterceptor.class }, |
| 72 | + new LoggingInvocationInterceptorHandler()); |
| 73 | + |
| 74 | + @Override |
| 75 | + public void interceptAfterAllMethod(Invocation<Void> invocation, |
| 76 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 77 | + throws Throwable { |
| 78 | + proxy.interceptAfterAllMethod(invocation, invocationContext, extensionContext); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void interceptAfterEachMethod(Invocation<Void> invocation, |
| 83 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 84 | + throws Throwable { |
| 85 | + proxy.interceptAfterEachMethod(invocation, invocationContext, extensionContext); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void interceptBeforeAllMethod(Invocation<Void> invocation, |
| 90 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 91 | + throws Throwable { |
| 92 | + proxy.interceptBeforeAllMethod(invocation, invocationContext, extensionContext); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void interceptBeforeEachMethod(Invocation<Void> invocation, |
| 97 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 98 | + throws Throwable { |
| 99 | + proxy.interceptBeforeEachMethod(invocation, invocationContext, extensionContext); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void interceptDynamicTest(Invocation<Void> invocation, |
| 104 | + DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) |
| 105 | + throws Throwable { |
| 106 | + proxy.interceptDynamicTest(invocation, invocationContext, extensionContext); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void interceptTestMethod(Invocation<Void> invocation, |
| 111 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 112 | + throws Throwable { |
| 113 | + proxy.interceptTestMethod(invocation, invocationContext, extensionContext); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void interceptTestTemplateMethod(Invocation<Void> invocation, |
| 118 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 119 | + throws Throwable { |
| 120 | + proxy.interceptTestTemplateMethod(invocation, invocationContext, extensionContext); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public <T> T interceptTestFactoryMethod(Invocation<T> invocation, |
| 125 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) |
| 126 | + throws Throwable { |
| 127 | + return proxy.interceptTestFactoryMethod(invocation, invocationContext, extensionContext); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public <T> T interceptTestClassConstructor(Invocation<T> invocation, |
| 132 | + ReflectiveInvocationContext<Constructor<T>> invocationContext, |
| 133 | + ExtensionContext extensionContext) throws Throwable { |
| 134 | + return proxy.interceptTestClassConstructor(invocation, invocationContext, extensionContext); |
| 135 | + } |
| 136 | +} |
0 commit comments