TSK-1717: The JointPoint for our LoggingAspect now only looks for classes within pro.taskana package

This commit is contained in:
Mustapha Zorgati 2021-08-25 09:12:51 +02:00
parent e9540d04e4
commit c628426d0a
4 changed files with 70 additions and 21 deletions

View File

@ -21,7 +21,8 @@ public class LoggingAspect {
@Pointcut( @Pointcut(
"!@annotation(pro.taskana.common.internal.logging.NoLogging)" "!@annotation(pro.taskana.common.internal.logging.NoLogging)"
+ " && execution(* *(..))" + " && !within(@pro.taskana.common.internal.logging.NoLogging *)"
+ " && execution(* pro.taskana..*(..))"
+ " && !execution(* lambda*(..))" + " && !execution(* lambda*(..))"
+ " && !execution(String *.toString())" + " && !execution(String *.toString())"
+ " && !execution(int *.hashCode())" + " && !execution(int *.hashCode())"

View File

@ -0,0 +1,7 @@
package outside.of.pro.taskana;
public class OutsideOfProTaskanaPackageLoggingTestClass {
@SuppressWarnings("unused")
public void doStuff() {}
}

View File

@ -0,0 +1,7 @@
package pro.taskana;
public class AtProTaskanaRootPackageLoggingTestClass {
@SuppressWarnings("unused")
public void doStuff() {}
}

View File

@ -5,14 +5,15 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Condition; import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import outside.of.pro.taskana.OutsideOfProTaskanaPackageLoggingTestClass;
import uk.org.lidalia.slf4jext.Level; import uk.org.lidalia.slf4jext.Level;
import uk.org.lidalia.slf4jtest.LoggingEvent; import uk.org.lidalia.slf4jtest.LoggingEvent;
import uk.org.lidalia.slf4jtest.TestLogger; import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory; import uk.org.lidalia.slf4jtest.TestLoggerFactory;
class LoggingAspectTest { import pro.taskana.AtProTaskanaRootPackageLoggingTestClass;
TestLogger logger = TestLoggerFactory.getTestLogger(LoggingTestClass.class); class LoggingAspectTest {
@BeforeEach @BeforeEach
public void clearLoggers() { public void clearLoggers() {
@ -20,63 +21,92 @@ class LoggingAspectTest {
} }
@Test @Test
void should_Log_For_InternalMethod() { void should_NotLogMethodCalls_When_ClassDoesNotResideWithinTaskanaPackage() {
OutsideOfProTaskanaPackageLoggingTestClass loggingTestClass =
new OutsideOfProTaskanaPackageLoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.doStuff();
assertThat(logger.getLoggingEvents()).isEmpty();
}
@Test
void should_LogMethod_When_ClassResidesAtTaskanaRootPackage() {
AtProTaskanaRootPackageLoggingTestClass loggingTestClass =
new AtProTaskanaRootPackageLoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.doStuff();
verifyLoggingStatement(logger, "doStuff", "", null);
}
@Test
void should_LogInternalMethod_When_ClassResidesAtTaskanaSubPackage() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.logInternalMethod(); loggingTestClass.logInternalMethod();
verifyLoggingStatement("logInternalMethod", "", null); verifyLoggingStatement(logger, "logInternalMethod", "", null);
} }
@Test @Test
void should_Log_For_InternalMethodWithReturnValue() { void should_LogInternalMethodWithReturnValue_When_ClassResidesAtTaskanaSubPackage() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.logInternalMethodWithReturnValue(); loggingTestClass.logInternalMethodWithReturnValue();
verifyLoggingStatement("logInternalMethodWithReturnValue", "", "test string"); verifyLoggingStatement(logger, "logInternalMethodWithReturnValue", "", "test string");
} }
@Test @Test
void should_Log_For_InternalMethodWithReturnValueNull() { void should_LogInternalMethodWithReturnValueNull_When_ClassResidesAtTaskanaSubPackage() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.logInternalMethodWithReturnValueNull(); loggingTestClass.logInternalMethodWithReturnValueNull();
verifyLoggingStatement("logInternalMethodWithReturnValueNull", "", "null"); verifyLoggingStatement(logger, "logInternalMethodWithReturnValueNull", "", "null");
} }
@Test @Test
void should_Log_For_InternalMethodWithArguments() { void should_LogInternalMethodWithArguments_When_ClassResidesAtTaskanaSubPackage() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.logInternalMethodWithArguments("message"); loggingTestClass.logInternalMethodWithArguments("message");
verifyLoggingStatement("logInternalMethodWithArguments", "param = message", null); verifyLoggingStatement(logger, "logInternalMethodWithArguments", "param = message", null);
} }
@Test @Test
void should_Log_For_InternalMethodWithReturnValueAndArguments() { void should_LogInternalMethodWithReturnValueAndArguments_When_ClassResidesAtTaskanaSubPackage() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.logInternalMethodWithReturnValueAndArguments("message"); loggingTestClass.logInternalMethodWithReturnValueAndArguments("message");
verifyLoggingStatement( verifyLoggingStatement(
"logInternalMethodWithReturnValueAndArguments", "param = message", "return value"); logger, "logInternalMethodWithReturnValueAndArguments", "param = message", "return value");
} }
@Test @Test
void should_NotLog_For_ExternalMethod() { void should_NotLogExternalMethod_When_AMethodCallsAMethodOutsideOfTaskanaPackage() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.callsExternalMethod(); loggingTestClass.callsExternalMethod();
verifyLoggingStatement("callsExternalMethod", "", null); verifyLoggingStatement(logger, "callsExternalMethod", "", null);
} }
@Test @Test
void should_LogMultipleMethods_When_SubMethodIsCalled() { void should_LogMultipleMethods_When_SubMethodIsCalled() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.logInternalMethodWrapper(); loggingTestClass.logInternalMethodWrapper();
assertThat(logger.getLoggingEvents()) assertThat(logger.getLoggingEvents())
@ -88,6 +118,7 @@ class LoggingAspectTest {
@Test @Test
void should_NotLogInternalMethod_When_MethodIsAnnotatedWithNoLogging() { void should_NotLogInternalMethod_When_MethodIsAnnotatedWithNoLogging() {
LoggingTestClass loggingTestClass = new LoggingTestClass(); LoggingTestClass loggingTestClass = new LoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(loggingTestClass.getClass());
loggingTestClass.doNotLogInternalMethod(); loggingTestClass.doNotLogInternalMethod();
@ -97,6 +128,7 @@ class LoggingAspectTest {
@Test @Test
void should_NotLogInternalMethod_When_ClassIsAnnotatedWithNoLogging() { void should_NotLogInternalMethod_When_ClassIsAnnotatedWithNoLogging() {
NoLoggingTestClass noLoggingTestClass = new NoLoggingTestClass(); NoLoggingTestClass noLoggingTestClass = new NoLoggingTestClass();
TestLogger logger = TestLoggerFactory.getTestLogger(noLoggingTestClass.getClass());
noLoggingTestClass.doNotLogInternalMethod(); noLoggingTestClass.doNotLogInternalMethod();
@ -106,13 +138,15 @@ class LoggingAspectTest {
@Test @Test
void should_NotLogInternalMethod_When_SuperClassIsAnnotatedWithNoLogging() { void should_NotLogInternalMethod_When_SuperClassIsAnnotatedWithNoLogging() {
NoLoggingTestSubClass noLoggingTestSubClass = new NoLoggingTestSubClass(); NoLoggingTestSubClass noLoggingTestSubClass = new NoLoggingTestSubClass();
TestLogger logger = TestLoggerFactory.getTestLogger(noLoggingTestSubClass.getClass());
noLoggingTestSubClass.doNotLogInternalMethod(); noLoggingTestSubClass.doNotLogInternalMethod();
assertThat(logger.getLoggingEvents()).isEmpty(); assertThat(logger.getLoggingEvents()).isEmpty();
} }
private void verifyLoggingStatement(String methodName, String arguments, Object returnValue) { private void verifyLoggingStatement(
TestLogger logger, String methodName, String arguments, Object returnValue) {
assertThat(logger.getLoggingEvents()).hasSize(2); assertThat(logger.getLoggingEvents()).hasSize(2);
LoggingEvent entryLoggingEvent = logger.getLoggingEvents().get(0); LoggingEvent entryLoggingEvent = logger.getLoggingEvents().get(0);
assertThat(entryLoggingEvent.getLevel()).isEqualTo(Level.TRACE); assertThat(entryLoggingEvent.getLevel()).isEqualTo(Level.TRACE);
@ -135,6 +169,11 @@ class LoggingAspectTest {
static class LoggingTestClass { static class LoggingTestClass {
public void logInternalMethod() {} public void logInternalMethod() {}
@SuppressWarnings("UnusedReturnValue")
public String logInternalMethodWithReturnValue() {
return "test string";
}
@SuppressWarnings("UnusedReturnValue") @SuppressWarnings("UnusedReturnValue")
public String logInternalMethodWithReturnValueNull() { public String logInternalMethodWithReturnValueNull() {
return null; return null;
@ -160,11 +199,6 @@ class LoggingAspectTest {
@NoLogging @NoLogging
public void doNotLogInternalMethod() {} public void doNotLogInternalMethod() {}
@SuppressWarnings("UnusedReturnValue")
String logInternalMethodWithReturnValue() {
return "test string";
}
private void logInternalMethodPrivate() {} private void logInternalMethodPrivate() {}
} }