Skip to content

Commit

Permalink
Improve logging in TestContextManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 26, 2022
1 parent 9ec937c commit 33d3380
Showing 1 changed file with 31 additions and 17 deletions.
Expand Up @@ -162,7 +162,7 @@ public void registerTestExecutionListeners(List<TestExecutionListener> testExecu
public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners) {
for (TestExecutionListener listener : testExecutionListeners) {
if (logger.isTraceEnabled()) {
logger.trace("Registering TestExecutionListener: " + listener);
logger.trace("Registering TestExecutionListener: " + typeName(listener));
}
this.testExecutionListeners.add(listener);
}
Expand Down Expand Up @@ -205,7 +205,7 @@ private List<TestExecutionListener> getReversedTestExecutionListeners() {
public void beforeTestClass() throws Exception {
Class<?> testClass = getTestContext().getTestClass();
if (logger.isTraceEnabled()) {
logger.trace("beforeTestClass(): class [" + testClass.getName() + "]");
logger.trace("beforeTestClass(): class [" + typeName(testClass) + "]");
}
getTestContext().updateState(null, null, null);

Expand Down Expand Up @@ -250,8 +250,10 @@ public void prepareTestInstance(Object testInstance) throws Exception {
}
catch (Throwable ex) {
if (logger.isErrorEnabled()) {
logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to prepare test instance [" + testInstance + "]", ex);
logger.error("""
Caught exception while allowing TestExecutionListener [%s] to \
prepare test instance [%s]"""
.formatted(typeName(testExecutionListener), testInstance), ex);
}
ReflectionUtils.rethrowException(ex);
}
Expand Down Expand Up @@ -481,7 +483,7 @@ public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Th
public void afterTestClass() throws Exception {
Class<?> testClass = getTestContext().getTestClass();
if (logger.isTraceEnabled()) {
logger.trace("afterTestClass(): class [" + testClass.getName() + "]");
logger.trace("afterTestClass(): class [" + typeName(testClass) + "]");
}
getTestContext().updateState(null, null, null);

Expand Down Expand Up @@ -512,7 +514,7 @@ public void afterTestClass() throws Exception {

private void prepareForBeforeCallback(String callbackName, Object testInstance, Method testMethod) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("%s(): instance [%s], method [%s]", callbackName, testInstance, testMethod));
logger.trace("%s(): instance [%s], method [%s]".formatted(callbackName, testInstance, testMethod));
}
getTestContext().updateState(testInstance, testMethod, null);
}
Expand All @@ -521,8 +523,8 @@ private void prepareForAfterCallback(String callbackName, Object testInstance, M
@Nullable Throwable exception) {

if (logger.isTraceEnabled()) {
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]",
callbackName, testInstance, testMethod, exception));
logger.trace("%s(): instance [%s], method [%s], exception [%s]"
.formatted(callbackName, testInstance, testMethod, exception));
}
getTestContext().updateState(testInstance, testMethod, exception);
}
Expand All @@ -538,19 +540,21 @@ private void logException(
Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) {

if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " +
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener,
testClass), ex);
logger.warn("""
Caught exception while invoking '%s' callback on TestExecutionListener [%s] \
for test class [%s]"""
.formatted(callbackName, typeName(testExecutionListener), typeName(testClass)), ex);
}
}

private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Object testInstance, Method testMethod) {

if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " +
"TestExecutionListener [%s] for test method [%s] and test instance [%s]",
callbackName, testExecutionListener, testMethod, testInstance), ex);
logger.warn("""
Caught exception while invoking '%s' callback on TestExecutionListener [%s] for \
test method [%s] and test instance [%s]"""
.formatted(callbackName, typeName(testExecutionListener), testMethod, testInstance), ex);
}
}

Expand All @@ -570,9 +574,9 @@ private static TestContext copyTestContext(TestContext testContext) {
}
catch (Exception ex) {
if (logger.isInfoEnabled()) {
logger.info(String.format("Failed to invoke copy constructor for [%s]; " +
"concurrent test execution is therefore likely not supported.",
testContext), ex);
logger.info("""
Failed to invoke copy constructor for [%s]; concurrent test execution \
is therefore likely not supported.""".formatted(testContext), ex);
}
}
}
Expand All @@ -581,4 +585,14 @@ private static TestContext copyTestContext(TestContext testContext) {
return testContext;
}

private static String typeName(Object obj) {
if (obj == null) {
return "null";
}
if (obj instanceof Class<?> type) {
return type.getName();
}
return obj.getClass().getName();
}

}

0 comments on commit 33d3380

Please sign in to comment.