-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
261ccaa
commit ddea01a
Showing
6 changed files
with
161 additions
and
31 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...dk/core/src/testFixtures/java/io/airbyte/cdk/extensions/LoggingInvocationInterceptor.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,130 @@ | ||
package io.airbyte.cdk.extensions; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Arrays; | ||
import java.util.StringJoiner; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.junit.jupiter.api.extension.DynamicTestInvocationContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.InvocationInterceptor; | ||
import org.junit.jupiter.api.extension.ReflectiveInvocationContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This allows us to have junit loglines in our test logs. | ||
* This is instanciated via Java's ServiceLoader (https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/util/ServiceLoader.html) | ||
* The declaration can be found in resources/META-INF/services/org.junit.jupiter.api.extension.Extension | ||
*/ | ||
public class LoggingInvocationInterceptor implements InvocationInterceptor { | ||
private static final class LoggingInvocationInterceptorHandler implements InvocationHandler { | ||
private static Logger LOGGER = LoggerFactory.getLogger(LoggingInvocationInterceptor.class); | ||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
Invocation<?> invocation= (Invocation<?>)args[0]; | ||
ReflectiveInvocationContext<Method> invocationContext = (ReflectiveInvocationContext<Method>)args[1]; | ||
ExtensionContext extensionContext = (ExtensionContext) args[2]; | ||
String methodName = method.getName(); | ||
String logLineSuffix; | ||
if (methodName.equals("interceptDynamicTest")) { | ||
logLineSuffix = "execution of DynamicTest " + extensionContext.getDisplayName(); | ||
} else if (methodName.equals("interceptTestClassConstructor")) { | ||
logLineSuffix = "instance creation for " + invocationContext.getTargetClass(); | ||
} else { | ||
String interceptedEvent = methodName.substring("intercept".length(), methodName.length() - "Method".length()); | ||
logLineSuffix = "execution of @" + interceptedEvent + " method " + invocationContext.getExecutable().getDeclaringClass().getSimpleName() | ||
+ "." + invocationContext.getExecutable().getName(); | ||
} | ||
LOGGER.info("Junit starting " + logLineSuffix); | ||
try { | ||
long start = System.nanoTime(); | ||
Object retVal = invocation.proceed(); | ||
long elapsedMs = (System.nanoTime() - start)/1_000_000; | ||
LOGGER.info("Junit completed " + logLineSuffix + " in " + elapsedMs + "ms"); | ||
return retVal; | ||
} catch (Throwable t) { | ||
String stackTrace = Arrays.stream(ExceptionUtils.getStackFrames(t)).takeWhile(s->!s.startsWith("\tat org.junit")).collect( | ||
Collectors.joining("\n ")); | ||
LOGGER.warn("Junit exception throw during " + logLineSuffix + ":\n" + stackTrace); | ||
throw t; | ||
} | ||
} | ||
} | ||
|
||
private final InvocationInterceptor proxy = (InvocationInterceptor) Proxy.newProxyInstance( | ||
getClass().getClassLoader(), | ||
new Class[] {InvocationInterceptor.class }, | ||
new LoggingInvocationInterceptorHandler()); | ||
|
||
@Override | ||
public void interceptAfterAllMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptAfterAllMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptAfterEachMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptAfterEachMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptBeforeAllMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptBeforeAllMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptBeforeEachMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptBeforeEachMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptDynamicTest(Invocation<Void> invocation, | ||
DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptDynamicTest(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptTestMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptTestMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public void interceptTestTemplateMethod(Invocation<Void> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
proxy.interceptTestTemplateMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public <T> T interceptTestFactoryMethod(Invocation<T> invocation, | ||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) | ||
throws Throwable { | ||
return proxy.interceptTestFactoryMethod(invocation, invocationContext, extensionContext); | ||
} | ||
|
||
@Override | ||
public <T> T interceptTestClassConstructor(Invocation<T> invocation, | ||
ReflectiveInvocationContext<Constructor<T>> invocationContext, | ||
ExtensionContext extensionContext) throws Throwable { | ||
return proxy.interceptTestClassConstructor(invocation, invocationContext, extensionContext); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...re/src/testFixtures/resources/META-INF/services/org.junit.jupiter.api.extension.Extension
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 @@ | ||
io.airbyte.cdk.extensions.LoggingInvocationInterceptor |
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
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