diff --git a/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessor.java b/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessor.java index 038d1b8..df7a656 100644 --- a/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessor.java +++ b/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessor.java @@ -6,9 +6,6 @@ import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; -import javax.lang.model.element.TypeElement; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.WildcardType; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.tools.Diagnostic; @@ -24,7 +21,6 @@ */ public abstract class TraceeContextLoggerAbstractProcessor extends AbstractProcessor { - private static Map cachedParentTypes = new HashMap(); private static Map traceeProfileProperties = new HashMap(); protected Messager messager; @@ -32,11 +28,6 @@ public abstract class TraceeContextLoggerAbstractProcessor extends AbstractProce protected Elements elementUtils; protected Filer filer; - protected TypeElement COLLECTION; - protected TypeElement MAP; - protected TypeElement VOID; - protected WildcardType WILDCARD_TYPE_NULL; - public static class FileObjectWrapper { private final FileObject fileObject; private final Writer foWriter; @@ -81,7 +72,7 @@ protected void info(Element e, String message, Object... args) { /** * Central method to get cached FileObjectWrapper. Creates new FileObjectWrapper if it can't be found */ - protected static synchronized FileObjectWrapper getOrcreateProfileProperties(final Filer filer, String fileName) throws IOException { + protected static synchronized FileObjectWrapper getOrCreateProfileProperties(final Filer filer, String fileName) throws IOException { FileObjectWrapper fileObject = traceeProfileProperties.get(fileName); @@ -95,5 +86,8 @@ protected static synchronized FileObjectWrapper getOrcreateProfileProperties(fin } + protected static void clearCache() { + traceeProfileProperties.clear(); + } } diff --git a/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessor.java b/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessor.java index dbdca60..25586e2 100644 --- a/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessor.java +++ b/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessor.java @@ -48,7 +48,7 @@ public boolean process(final Set annotations, final Round try { // write entry for fields - ProfileSettings annotation = element.getAnnotation(ProfileSettings.class); + ProfileConfig annotation = element.getAnnotation(ProfileConfig.class); boolean basicProfileSettings = annotation != null && annotation.basic(); boolean enhancedProfileSettings = annotation != null && annotation.enhanced(); @@ -76,7 +76,7 @@ public Set getSupportedAnnotationTypes() { protected void writeToPropertyFile(Profile profile, TypeElement parentElement, Element element, boolean value) throws IOException { - FileObjectWrapper fileObject = getOrcreateProfileProperties(filer, profile.getFilename()); + FileObjectWrapper fileObject = getOrCreateProfileProperties(filer, profile.getFilename()); String fieldName = GetterUtilities.getFieldName(element.getSimpleName().toString()); if (fieldName == null) { diff --git a/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessor.java b/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessor.java index 01522c0..9dc44b4 100644 --- a/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessor.java +++ b/contextprovider/annotationprocessor/src/main/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessor.java @@ -46,7 +46,7 @@ public boolean process(final Set annotations, final Round // Write class profile properties try { - ProfileSettings annotation = element.getAnnotation(ProfileSettings.class); + ProfileConfig annotation = element.getAnnotation(ProfileConfig.class); boolean basicProfileSettings = annotation != null && annotation.basic(); boolean enhancedProfileSettings = annotation != null && annotation.enhanced(); @@ -76,7 +76,7 @@ public Set getSupportedAnnotationTypes() { protected void writeToPropertyFile(Profile profile, TypeElement element, boolean value) throws IOException { - FileObjectWrapper fileObject = getOrcreateProfileProperties(filer, profile.getFilename()); + FileObjectWrapper fileObject = getOrCreateProfileProperties(filer, profile.getFilename()); fileObject.append(element.getQualifiedName() + "=" + value + "\n"); } diff --git a/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessorTest.java b/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessorTest.java new file mode 100644 index 0000000..0e96326 --- /dev/null +++ b/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextLoggerAbstractProcessorTest.java @@ -0,0 +1,85 @@ +package io.tracee.contextlogger.contextprovider.api; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import javax.annotation.processing.Filer; +import javax.lang.model.element.Element; +import javax.tools.FileObject; +import javax.tools.StandardLocation; +import java.io.IOException; +import java.io.Writer; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit test for {@link TraceeContextLoggerAbstractProcessor}. + */ +@RunWith(MockitoJUnitRunner.class) +public class TraceeContextLoggerAbstractProcessorTest { + + static final String GIVEN_FILE_NAME = "XXX"; + + @Mock + Filer filer; + + @Before + public void init() { + TraceeContextLoggerAbstractProcessor.clearCache(); + } + + @Test + public void FileObjectWrapper_instantiationAndAppendTest() throws IOException { + + // prepare test + FileObject fileObject = mock(FileObject.class); + Writer writer = mock(Writer.class); + when(fileObject.openWriter()).thenReturn(writer); + + TraceeContextLoggerAbstractProcessor.FileObjectWrapper fowUnit = new TraceeContextLoggerAbstractProcessor.FileObjectWrapper(fileObject); + + fowUnit.append("TEST"); + + verify(fileObject).openWriter(); + verify(writer).append("TEST"); + verify(writer).flush(); + + } + + @Test + public void getOrcreateProfileProperties_createNewFileObject() throws IOException { + + FileObject fileObject = mock(FileObject.class); + Writer writer = mock(Writer.class); + + when(filer.createResource(eq(StandardLocation.SOURCE_OUTPUT), eq(""), eq(GIVEN_FILE_NAME), isNull(Element.class))).thenReturn(fileObject); + + assertThat(TraceeContextLoggerAbstractProcessor.getOrCreateProfileProperties(filer, GIVEN_FILE_NAME), notNullValue()); + verify(filer, times(1)).createResource(StandardLocation.SOURCE_OUTPUT, "", GIVEN_FILE_NAME, null); + + } + + @Test + public void getOrcreateProfileProperties_getCachedFileObject() throws IOException { + + // fill cache + getOrcreateProfileProperties_createNewFileObject(); + + // get it for the second time + assertThat(TraceeContextLoggerAbstractProcessor.getOrCreateProfileProperties(filer, GIVEN_FILE_NAME), notNullValue()); + verify(filer, times(1)).createResource(StandardLocation.SOURCE_OUTPUT, "", GIVEN_FILE_NAME, null); + + } + + +} diff --git a/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessorTest.java b/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessorTest.java index 6c08a4e..de06f4d 100644 --- a/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessorTest.java +++ b/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderMethodProcessorTest.java @@ -64,11 +64,11 @@ public void process_validAnnotatedMethod() throws IOException { when(element.getEnclosingElement()).thenReturn(parentElement); // prepare profile settings - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); - when(element.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); + when(element.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); // provide elements annotated with Flatten annotation Set set = new HashSet(); @@ -110,8 +110,8 @@ public void process_missingProfileSettingsAnnotationCorrectly() throws IOExcepti when(element.getEnclosingElement()).thenReturn(parentElement); // prepare profile settings - ProfileSettings profileSettings = null; - when(element.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + ProfileConfig profileConfig = null; + when(element.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); // provide elements annotated with Flatten annotation Set set = new HashSet(); @@ -154,11 +154,11 @@ public void process_skipBecauseOfMissingTraceeContextProviderAnnotation() throws when(element.getEnclosingElement()).thenReturn(parentElement); // prepare profile settings - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); - when(element.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); + when(element.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); // provide elements annotated with Flatten annotation Set set = new HashSet(); @@ -200,11 +200,11 @@ public void process_skipBecauseOfIsNoGetter() throws IOException { when(element.getEnclosingElement()).thenReturn(parentElement); // prepare profile settings - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); - when(element.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); + when(element.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); // provide elements annotated with Flatten annotation Set set = new HashSet(); @@ -246,11 +246,11 @@ public void process_skipBecauseOfInvalidMethod() throws IOException { when(element.getEnclosingElement()).thenReturn(parentElement); // prepare profile settings - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); - when(element.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); + when(element.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); // provide elements annotated with Flatten annotation Set set = new HashSet(); diff --git a/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessorTest.java b/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessorTest.java index c1558fa..6d01c5f 100644 --- a/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessorTest.java +++ b/contextprovider/annotationprocessor/src/test/java/io/tracee/contextlogger/contextprovider/api/TraceeContextProviderProcessorTest.java @@ -145,13 +145,13 @@ public void process_validClass() throws IOException { TraceeContextProviderProcessor spy = Mockito.spy(unit); - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); TypeElement typeElement = mock(TypeElement.class); - when(typeElement.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + when(typeElement.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); Set set = new HashSet(); set.add(typeElement); @@ -183,10 +183,10 @@ public void process_validClassWithMissingProfileSettingsAnnotation() throws IOEx TraceeContextProviderProcessor spy = Mockito.spy(unit); - ProfileSettings profileSettings = null; + ProfileConfig profileConfig = null; TypeElement typeElement = mock(TypeElement.class); - when(typeElement.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + when(typeElement.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); Set set = new HashSet(); set.add(typeElement); @@ -219,13 +219,13 @@ public void process_classWithMissingNoargsConstructor() throws IOException { TraceeContextProviderProcessor spy = Mockito.spy(unit); - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); TypeElement typeElement = mock(TypeElement.class); - when(typeElement.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + when(typeElement.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); Set set = new HashSet(); set.add(typeElement); @@ -257,13 +257,13 @@ public void process_classWithMissingNoargsConstructor2() throws IOException { TraceeContextProviderProcessor spy = Mockito.spy(unit); - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); TypeElement typeElement = mock(TypeElement.class); - when(typeElement.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + when(typeElement.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); Set set = new HashSet(); set.add(typeElement); @@ -295,13 +295,13 @@ public void process_invalidClass() throws IOException { TraceeContextProviderProcessor spy = Mockito.spy(unit); - ProfileSettings profileSettings = mock(ProfileSettings.class); - when(profileSettings.basic()).thenReturn(true); - when(profileSettings.enhanced()).thenReturn(true); - when(profileSettings.full()).thenReturn(true); + ProfileConfig profileConfig = mock(ProfileConfig.class); + when(profileConfig.basic()).thenReturn(true); + when(profileConfig.enhanced()).thenReturn(true); + when(profileConfig.full()).thenReturn(true); TypeElement typeElement = mock(TypeElement.class); - when(typeElement.getAnnotation(ProfileSettings.class)).thenReturn(profileSettings); + when(typeElement.getAnnotation(ProfileConfig.class)).thenReturn(profileConfig); Set set = new HashSet(); set.add(typeElement); diff --git a/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContext.java b/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContext.java index 14b3626..73d9bfc 100644 --- a/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContext.java +++ b/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContext.java @@ -1,10 +1,8 @@ package io.tracee.contextlogger.contextprovider.api; /** - * Defines all existing implicit contexts + * Enums can be used to add implicit context providers to string representation output. * Created by Tobias Gindler, holisticon AG on 14.03.14. */ -public enum ImplicitContext { - TRACEE, - COMMON; +public interface ImplicitContext { } diff --git a/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContextData.java b/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContextData.java index 1e3224e..23f47d5 100644 --- a/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContextData.java +++ b/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ImplicitContextData.java @@ -6,9 +6,12 @@ */ public interface ImplicitContextData { - /** - * Gets the implict context type. - */ - ImplicitContext getImplicitContext(); + /** + * Used to map enum values against implicit context providers. + * This allows adding of implicit context providers to string representation builder output just by adding the corresponding enum value. + * + * @return an enum value that implements the ImplicitContext interface + */ + ImplicitContext getImplicitContext(); } diff --git a/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ProfileSettings.java b/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ProfileConfig.java similarity index 73% rename from contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ProfileSettings.java rename to contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ProfileConfig.java index 39e049d..0e2a70e 100644 --- a/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ProfileSettings.java +++ b/contextprovider/api/src/main/java/io/tracee/contextlogger/contextprovider/api/ProfileConfig.java @@ -5,9 +5,12 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * Configures if annotated class or method should be outputted in string representation builder output. + */ @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.TYPE}) -public @interface ProfileSettings { +public @interface ProfileConfig { boolean basic() default false; diff --git a/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/WatchdogAspect.java b/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/WatchdogAspect.java index 28f8070..f86b181 100644 --- a/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/WatchdogAspect.java +++ b/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/WatchdogAspect.java @@ -1,126 +1,122 @@ package io.tracee.contextlogger.contextprovider.aspectj; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import io.tracee.contextlogger.MessagePrefixProvider; import io.tracee.contextlogger.TraceeContextLogger; import io.tracee.contextlogger.api.ErrorMessage; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.api.internal.MessageLogLevel; import io.tracee.contextlogger.contextprovider.aspectj.contextprovider.WatchdogDataWrapper; import io.tracee.contextlogger.contextprovider.aspectj.util.WatchdogUtils; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMessage; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Watchdog Assert class. * This aspect logs method calls of Watchdog annotated classes and methods in case of an exception is thrown during the execution of the method. - *

+ *

* Created by Tobias Gindler, holisticon AG on 16.02.14. */ @Aspect public class WatchdogAspect { - private static final Logger logger = LoggerFactory.getLogger(WatchdogAspect.class); - - private final boolean active; - - public WatchdogAspect() { - this(Boolean.valueOf(System.getProperty(Constants.SYSTEM_PROPERTY_IS_ACTIVE, "true"))); - } - - WatchdogAspect(boolean active) { - this.active = active; - } - - @SuppressWarnings("unused") - @Pointcut("(execution(* *(..)) && @annotation(io.tracee.contextlogger.contextprovider.aspectj.Watchdog))") - void withinWatchdogAnnotatedMethods() { - } - - @SuppressWarnings("unused") - @Pointcut("within(@io.tracee.contextlogger.contextprovider.aspectj.Watchdog *)") - void withinClassWithWatchdogAnnotation() { - - } - - @SuppressWarnings("unused") - @Pointcut("execution(public * *(..))") - void publicMethods() { - - } - - @SuppressWarnings("unused") - @Around("withinWatchdogAnnotatedMethods() || (publicMethods() && withinClassWithWatchdogAnnotation()) ") - public Object guard(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable { - - try { - return proceedingJoinPoint.proceed(); - } - catch (final Throwable e) { - - // check if watchdog processing is flagged as active - if (active) { - - // make sure that original exception will be passed through - try { - - // get watchdog annotation - Watchdog watchdog = WatchdogUtils.getWatchdogAnnotation(proceedingJoinPoint); - - // check if watchdog aspect processing is deactivated by annotation - if (WatchdogUtils.checkProcessWatchdog(watchdog, proceedingJoinPoint, e)) { - - String annotatedId = watchdog.id().isEmpty() ? null : watchdog.id(); - sendErrorReportToConnectors(proceedingJoinPoint, annotatedId, e); - - } - - } - catch (Throwable error) { - // will be ignored - logger.error("error", error); - } - } - // rethrow exception - throw e; - } - - } - - /** - * Sends the error reports to all connectors. - * - * @param proceedingJoinPoint the aspectj calling context - * @param annotatedId the id defined in the watchdog annotation - */ - void sendErrorReportToConnectors(ProceedingJoinPoint proceedingJoinPoint, String annotatedId, Throwable e) { - - // try to get error message annotation - ErrorMessage errorMessage = WatchdogUtils.getErrorMessageAnnotation(proceedingJoinPoint); - - if (errorMessage == null) { - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, Watchdog.class), ImplicitContext.COMMON, - ImplicitContext.TRACEE, WatchdogDataWrapper.wrap(annotatedId, proceedingJoinPoint), e); - } - else { - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, Watchdog.class), - TraceeMessage.wrap(errorMessage.value()), ImplicitContext.COMMON, ImplicitContext.TRACEE, - WatchdogDataWrapper.wrap(annotatedId, proceedingJoinPoint), e); - } - } + private static final Logger logger = LoggerFactory.getLogger(WatchdogAspect.class); + + private final boolean active; + + public WatchdogAspect() { + this(Boolean.valueOf(System.getProperty(Constants.SYSTEM_PROPERTY_IS_ACTIVE, "true"))); + } + + WatchdogAspect(boolean active) { + this.active = active; + } + + @SuppressWarnings("unused") + @Pointcut("(execution(* *(..)) && @annotation(io.tracee.contextlogger.contextprovider.aspectj.Watchdog))") + void withinWatchdogAnnotatedMethods() { + } + + @SuppressWarnings("unused") + @Pointcut("within(@io.tracee.contextlogger.contextprovider.aspectj.Watchdog *)") + void withinClassWithWatchdogAnnotation() { + + } + + @SuppressWarnings("unused") + @Pointcut("execution(public * *(..))") + void publicMethods() { + + } + + @SuppressWarnings("unused") + @Around("withinWatchdogAnnotatedMethods() || (publicMethods() && withinClassWithWatchdogAnnotation()) ") + public Object guard(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + + try { + return proceedingJoinPoint.proceed(); + } catch (final Throwable e) { + + // check if watchdog processing is flagged as active + if (active) { + + // make sure that original exception will be passed through + try { + + // get watchdog annotation + Watchdog watchdog = WatchdogUtils.getWatchdogAnnotation(proceedingJoinPoint); + + // check if watchdog aspect processing is deactivated by annotation + if (WatchdogUtils.checkProcessWatchdog(watchdog, proceedingJoinPoint, e)) { + + String annotatedId = watchdog.id().isEmpty() ? null : watchdog.id(); + sendErrorReportToConnectors(proceedingJoinPoint, annotatedId, e); + + } + + } catch (Throwable error) { + // will be ignored + logger.error("error", error); + } + } + // rethrow exception + throw e; + } + + } + + /** + * Sends the error reports to all connectors. + * + * @param proceedingJoinPoint the aspectj calling context + * @param annotatedId the id defined in the watchdog annotation + */ + void sendErrorReportToConnectors(ProceedingJoinPoint proceedingJoinPoint, String annotatedId, Throwable e) { + + // try to get error message annotation + ErrorMessage errorMessage = WatchdogUtils.getErrorMessageAnnotation(proceedingJoinPoint); + + if (errorMessage == null) { + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, Watchdog.class), CoreImplicitContextProviders.COMMON, + CoreImplicitContextProviders.TRACEE, WatchdogDataWrapper.wrap(annotatedId, proceedingJoinPoint), e); + } else { + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, Watchdog.class), + TraceeMessage.wrap(errorMessage.value()), CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, + WatchdogDataWrapper.wrap(annotatedId, proceedingJoinPoint), e); + } + } } diff --git a/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/AspectjProceedingJoinPointContextProvider.java b/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/AspectjProceedingJoinPointContextProvider.java index efbf2b8..5ca8afa 100644 --- a/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/AspectjProceedingJoinPointContextProvider.java +++ b/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/AspectjProceedingJoinPointContextProvider.java @@ -1,6 +1,6 @@ package io.tracee.contextlogger.contextprovider.aspectj.contextprovider; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -14,7 +14,7 @@ * Created by Tobias Gindler, holisticon AG on 20.03.14. */ @TraceeContextProvider(displayName = "proceedingJoinPoint") -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class AspectjProceedingJoinPointContextProvider implements WrappedContextData { private ProceedingJoinPoint proceedingJoinPoint; @@ -48,7 +48,7 @@ public static AspectjProceedingJoinPointContextProvider wrap(final ProceedingJoi @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "class", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getClazz() { if (proceedingJoinPoint != null && proceedingJoinPoint.getSignature() != null) { return proceedingJoinPoint.getSignature().getDeclaringTypeName(); @@ -58,7 +58,7 @@ public final String getClazz() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "method", order = 30) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getMethod() { if (proceedingJoinPoint != null && proceedingJoinPoint.getSignature() != null) { return proceedingJoinPoint.getSignature().getName(); @@ -68,7 +68,7 @@ public final String getMethod() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "parameters", order = 40) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final List getParameters() { if (proceedingJoinPoint != null && proceedingJoinPoint.getArgs() != null) { @@ -80,7 +80,7 @@ public final List getParameters() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "serialized-target-instance", order = 50) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public final Object getSerializedTargetInstance() { if (proceedingJoinPoint != null) { // output invoked instance diff --git a/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/WatchdogContextProvider.java b/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/WatchdogContextProvider.java index 4378e0d..f6b2ad0 100644 --- a/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/WatchdogContextProvider.java +++ b/contextprovider/aspectj/src/main/java/io/tracee/contextlogger/contextprovider/aspectj/contextprovider/WatchdogContextProvider.java @@ -3,7 +3,7 @@ import io.tracee.contextlogger.contextprovider.api.Order; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; /** @@ -12,7 +12,7 @@ */ @SuppressWarnings("unused") @TraceeContextProvider(displayName = "watchdog", order = Order.WATCHDOG) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class WatchdogContextProvider implements WrappedContextData { private WatchdogDataWrapper watchdogDataWrapper; @@ -42,7 +42,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "id", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getId() { if (watchdogDataWrapper != null) { return watchdogDataWrapper.getAnnotatedId(); @@ -52,7 +52,7 @@ public String getId() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "aspectj.proceedingJoinPoint", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public AspectjProceedingJoinPointContextProvider getProceedingJoinPoint() { if (watchdogDataWrapper != null && watchdogDataWrapper.getProceedingJoinPoint() != null) { return AspectjProceedingJoinPointContextProvider.wrap(watchdogDataWrapper.getProceedingJoinPoint()); diff --git a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/CoreImplicitContextProviders.java b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/CoreImplicitContextProviders.java new file mode 100644 index 0000000..2d6ab07 --- /dev/null +++ b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/CoreImplicitContextProviders.java @@ -0,0 +1,22 @@ +package io.tracee.contextlogger.contextprovider.core; + +import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import io.tracee.contextlogger.contextprovider.core.tracee.CommonDataContextProvider; +import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMdcContextProvider; + +/** + * Enum that can be used to provide implicit core context providers. + */ +public enum CoreImplicitContextProviders implements ImplicitContext { + + TRACEE(TraceeMdcContextProvider.class), + COMMON(CommonDataContextProvider.class); + + private final Class implicitContextProviderClass; + + private CoreImplicitContextProviders(final Class implicitContextProviderClass) { + this.implicitContextProviderClass = implicitContextProviderClass; + + } + +} diff --git a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/java/JavaThrowableContextProvider.java b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/java/JavaThrowableContextProvider.java index 7186cab..3035f66 100644 --- a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/java/JavaThrowableContextProvider.java +++ b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/java/JavaThrowableContextProvider.java @@ -1,7 +1,7 @@ package io.tracee.contextlogger.contextprovider.core.java; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -15,7 +15,7 @@ */ @SuppressWarnings("unused") @TraceeContextProvider(displayName = "throwable", order = Order.EXCEPTION) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class JavaThrowableContextProvider implements WrappedContextData { private Throwable throwable; @@ -46,14 +46,14 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "message", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getMessage() { return throwable != null ? throwable.getMessage() : null; } @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "stacktrace", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getStacktrace() { if (this.throwable != null) { StringWriter sw = new StringWriter(); diff --git a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProvider.java b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProvider.java index f7d0b19..e048726 100644 --- a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProvider.java +++ b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProvider.java @@ -3,9 +3,10 @@ import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.contextprovider.api.ImplicitContextData; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import java.net.InetAddress; import java.net.UnknownHostException; @@ -17,7 +18,7 @@ * Created by Tobias Gindler, holisticon AG on 14.03.14. */ @TraceeContextProvider(displayName = "common", order = Order.COMMON) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class CommonDataContextProvider implements ImplicitContextData { public static final String SYSTEM_PROPERTY_PREFIX = "io.tracee.contextlogger."; @@ -26,12 +27,12 @@ public class CommonDataContextProvider implements ImplicitContextData { @Override public final ImplicitContext getImplicitContext() { - return ImplicitContext.COMMON; + return CoreImplicitContextProviders.COMMON; } @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "timestamp", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final Date getTimestamp() { return Calendar.getInstance().getTime(); @@ -39,14 +40,14 @@ public final Date getTimestamp() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "stage", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getStage() { return getSystemProperty(SYSTEM_PROPERTY_NAME_STAGE); } @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "system-name", order = 30) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getSystemName() { @@ -66,14 +67,14 @@ public final String getSystemName() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "thread-name", order = 40) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getThreadName() { return Thread.currentThread().getName(); } @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "thread-id", order = 50) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final Long getThreadId() { return Thread.currentThread().getId(); } diff --git a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMdcContextProvider.java b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMdcContextProvider.java index 539ac5f..89fac58 100644 --- a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMdcContextProvider.java +++ b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMdcContextProvider.java @@ -4,9 +4,10 @@ import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.contextprovider.api.ImplicitContextData; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import io.tracee.contextlogger.contextprovider.core.utility.NameValuePair; import org.slf4j.MDC; @@ -20,7 +21,7 @@ */ @SuppressWarnings("unused") @TraceeContextProvider(displayName = "tracee", order = Order.TRACEE) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class TraceeMdcContextProvider implements ImplicitContextData { public TraceeMdcContextProvider() { @@ -29,13 +30,13 @@ public TraceeMdcContextProvider() { @Override public ImplicitContext getImplicitContext() { - return ImplicitContext.TRACEE; + return CoreImplicitContextProviders.TRACEE; } @SuppressWarnings("unused") @Flatten @TraceeContextProviderMethod(displayName = "DYNAMIC", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public List> getNameValuePairs() { final List> list = new ArrayList>(); diff --git a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMessageContextProvider.java b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMessageContextProvider.java index 156ed91..5e7f6e4 100644 --- a/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMessageContextProvider.java +++ b/contextprovider/core/src/main/java/io/tracee/contextlogger/contextprovider/core/tracee/TraceeMessageContextProvider.java @@ -3,7 +3,7 @@ import io.tracee.contextlogger.contextprovider.api.Order; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; /** @@ -12,7 +12,7 @@ */ @SuppressWarnings("unused") @TraceeContextProvider(displayName = "message", order = Order.MESSAGE) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class TraceeMessageContextProvider implements WrappedContextData { @@ -51,7 +51,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "value", order = 10, enabledPerDefault = true) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getValue() { if (message != null && message.getMessage() != null) { return message.getMessage().toString(); diff --git a/contextprovider/core/src/test/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataProviderTest.java b/contextprovider/core/src/test/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProviderTest.java similarity index 93% rename from contextprovider/core/src/test/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataProviderTest.java rename to contextprovider/core/src/test/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProviderTest.java index 2b8974c..1946759 100644 --- a/contextprovider/core/src/test/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataProviderTest.java +++ b/contextprovider/core/src/test/java/io/tracee/contextlogger/contextprovider/core/tracee/CommonDataContextProviderTest.java @@ -1,6 +1,7 @@ package io.tracee.contextlogger.contextprovider.core.tracee; import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -18,7 +19,7 @@ * Test class for {@link CommonDataContextProvider}. * Created by Tobias Gindler, holisticon AG on 31.03.14. */ -public class CommonDataProviderTest { +public class CommonDataContextProviderTest { public static final String STAGE = "STAGE"; public static final String SYSTEM = "SYSTEM"; @@ -38,7 +39,7 @@ public void should_return_implicit_context() { ImplicitContext implicitContext = commonDataContextProvider.getImplicitContext(); assertThat(implicitContext, notNullValue()); - assertThat(implicitContext, equalTo(ImplicitContext.COMMON)); + assertThat(implicitContext, equalTo((ImplicitContext) CoreImplicitContextProviders.COMMON)); } diff --git a/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptor.java b/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptor.java index 4b33741..6b6bc7c 100644 --- a/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptor.java +++ b/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptor.java @@ -1,15 +1,15 @@ package io.tracee.contextlogger.contextprovider.javaee; -import javax.interceptor.AroundInvoke; -import javax.interceptor.InvocationContext; - import io.tracee.contextlogger.MessagePrefixProvider; import io.tracee.contextlogger.TraceeContextLogger; import io.tracee.contextlogger.api.ErrorMessage; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMessage; +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + /** * Message listener to detect exceptions that happened during javaee message processing. * In case of an exception contextual information will be written to the log. @@ -17,40 +17,38 @@ */ public class TraceeErrorContextLoggingInterceptor { - @SuppressWarnings("unused") - public TraceeErrorContextLoggingInterceptor() { - } + @SuppressWarnings("unused") + public TraceeErrorContextLoggingInterceptor() { + } - @AroundInvoke - public Object intercept(final InvocationContext ctx) throws Exception { - try { - return ctx.proceed(); - } - catch (Exception e) { + @AroundInvoke + public Object intercept(final InvocationContext ctx) throws Exception { + try { + return ctx.proceed(); + } catch (Exception e) { - // try to get error message annotation - ErrorMessage errorMessage = ctx.getMethod().getAnnotation(ErrorMessage.class); + // try to get error message annotation + ErrorMessage errorMessage = ctx.getMethod().getAnnotation(ErrorMessage.class); - // now log context informations - if (errorMessage == null) { - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage( - MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorContextLoggingInterceptor.class), - ImplicitContext.COMMON, ImplicitContext.TRACEE, ctx, e); - } - else { - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage( - MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorContextLoggingInterceptor.class), - TraceeMessage.wrap(errorMessage.value()), ImplicitContext.COMMON, ImplicitContext.TRACEE, ctx, e); - } - throw e; - } - } + // now log context informations + if (errorMessage == null) { + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage( + MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorContextLoggingInterceptor.class), + CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, ctx, e); + } else { + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage( + MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorContextLoggingInterceptor.class), + TraceeMessage.wrap(errorMessage.value()), CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, ctx, e); + } + throw e; + } + } } diff --git a/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListener.java b/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListener.java index dccd79a..4bed70d 100644 --- a/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListener.java +++ b/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListener.java @@ -1,13 +1,12 @@ package io.tracee.contextlogger.contextprovider.javaee; -import java.lang.reflect.Method; +import io.tracee.contextlogger.TraceeContextLogger; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import javax.interceptor.AroundInvoke; import javax.interceptor.InvocationContext; import javax.jms.Message; - -import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import java.lang.reflect.Method; /** * Message listener to detect exceptions that happened during javaee message processing. @@ -16,32 +15,31 @@ */ public class TraceeJmsErrorMessageListener { - static final String JSON_PREFIXED_MESSAGE = "TRACEE JMS ERROR CONTEXT LOGGING LISTENER : "; + static final String JSON_PREFIXED_MESSAGE = "TRACEE JMS ERROR CONTEXT LOGGING LISTENER : "; - @SuppressWarnings("unused") - public TraceeJmsErrorMessageListener() { + @SuppressWarnings("unused") + public TraceeJmsErrorMessageListener() { - } + } - @AroundInvoke - public Object intercept(InvocationContext ctx) throws Exception { - try { - return ctx.proceed(); - } - catch (Exception e) { - final boolean isMdbInvocation = isMessageListenerOnMessageMethod(ctx.getMethod()); + @AroundInvoke + public Object intercept(InvocationContext ctx) throws Exception { + try { + return ctx.proceed(); + } catch (Exception e) { + final boolean isMdbInvocation = isMessageListenerOnMessageMethod(ctx.getMethod()); - if (isMdbInvocation) { - TraceeContextLogger.create().enforceOrder().apply() - .logWithPrefixedMessage(JSON_PREFIXED_MESSAGE, ImplicitContext.COMMON, ImplicitContext.TRACEE, ctx, e); - } + if (isMdbInvocation) { + TraceeContextLogger.create().enforceOrder().apply() + .logWithPrefixedMessage(JSON_PREFIXED_MESSAGE, CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, ctx, e); + } - throw e; - } - } + throw e; + } + } - boolean isMessageListenerOnMessageMethod(Method method) { - return "onMessage".equals(method.getName()) && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == Message.class; - } + boolean isMessageListenerOnMessageMethod(Method method) { + return "onMessage".equals(method.getName()) && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == Message.class; + } } diff --git a/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/contextprovider/InvocationContextContextProvider.java b/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/contextprovider/InvocationContextContextProvider.java index 75b45d8..ba76e63 100644 --- a/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/contextprovider/InvocationContextContextProvider.java +++ b/contextprovider/javaee/src/main/java/io/tracee/contextlogger/contextprovider/javaee/contextprovider/InvocationContextContextProvider.java @@ -1,7 +1,7 @@ package io.tracee.contextlogger.contextprovider.javaee.contextprovider; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -17,7 +17,7 @@ * Created by Tobias Gindler, holisticon AG on 20.03.14. */ @TraceeContextProvider(displayName = "invocationContext", order = Order.EJB) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class InvocationContextContextProvider implements WrappedContextData { private InvocationContext invocationContext; @@ -45,20 +45,20 @@ public final Class getWrappedType() { } @TraceeContextProviderMethod(displayName = "typeName", order = 0) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getTypeName() { return this.invocationContext != null && this.invocationContext.getTarget() != null && this.invocationContext.getTarget().getClass() != null ? this.invocationContext.getTarget().getClass().getCanonicalName() : null; } @TraceeContextProviderMethod(displayName = "methodName", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getMethodName() { return this.invocationContext != null && this.invocationContext.getMethod() != null ? this.invocationContext.getMethod().getName() : null; } @TraceeContextProviderMethod(displayName = "parameters", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final List getParameters() { List result = new ArrayList(); @@ -75,7 +75,7 @@ public final List getParameters() { } @TraceeContextProviderMethod(displayName = "target-instance", order = 30) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public final Object getTargetInstance() { String result = null; if (this.invocationContext != null) { @@ -87,7 +87,7 @@ public final Object getTargetInstance() { } @TraceeContextProviderMethod(displayName = "invocationContextData", order = 40) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public final List> getInvocationContextData() { List> result = new ArrayList>(); diff --git a/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptorTest.java b/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptorTest.java index 25e5312..d4f8fcd 100644 --- a/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptorTest.java +++ b/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeErrorContextLoggingInterceptorTest.java @@ -1,118 +1,120 @@ package io.tracee.contextlogger.contextprovider.javaee; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.*; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - -import javax.interceptor.InvocationContext; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - import io.tracee.contextlogger.MessagePrefixProvider; import io.tracee.contextlogger.TraceeContextLogger; import io.tracee.contextlogger.api.ConfigBuilder; import io.tracee.contextlogger.api.ContextLogger; import io.tracee.contextlogger.api.ErrorMessage; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMessage; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.interceptor.InvocationContext; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; /** * Test class for {@link TraceeErrorContextLoggingInterceptor}. * Created by Tobias Gindler on 19.06.14. */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ TraceeContextLogger.class, TraceeMessage.class, MessagePrefixProvider.class }) +@PrepareForTest({TraceeContextLogger.class, TraceeMessage.class, MessagePrefixProvider.class}) public class TraceeErrorContextLoggingInterceptorTest { - private final static String LOG_MESSAGE_PREFIX = "XX"; - - public static final String ERROR_MESSAGE = "ABC"; - - private final TraceeErrorContextLoggingInterceptor unit = mock(TraceeErrorContextLoggingInterceptor.class); - - private final ContextLogger contextLogger = mock(ContextLogger.class); - private final ConfigBuilder configBuilder = mock(ConfigBuilder.class); - - private final TraceeMessage traceeMessage = new TraceeMessage("ABC"); - - @Before - public void setupMocks() throws Exception { - - // Let's return true for every Message. The intercept-method must call teh real method. otherwise - // we would test the mocking framework! - // java.lang.reflect.Method is not suitable for mocks. Also Powermock is unable to create a mock for it! - when(unit.intercept(Mockito.any(InvocationContext.class))).thenCallRealMethod(); - mockStatic(TraceeContextLogger.class); - when(TraceeContextLogger.create()).thenReturn(configBuilder); - when(configBuilder.enforceOrder()).thenReturn(configBuilder); - when(configBuilder.apply()).thenReturn(contextLogger); - mockStatic(TraceeMessage.class); - when(TraceeMessage.wrap(ERROR_MESSAGE)).thenReturn(traceeMessage); - - // mock log message prefix creation - mockStatic(MessagePrefixProvider.class); - when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(String.class))).thenReturn(LOG_MESSAGE_PREFIX); - when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(Class.class))).thenReturn(LOG_MESSAGE_PREFIX); - } - - @Test - public void shouldBeInitializable() { - assertThat(new TraceeErrorContextLoggingInterceptor(), is(not(nullValue()))); - } - - @Test - public void noInteractionWhenNoExceptionOccurs() throws Exception { - final InvocationContext invocationContext = mock(InvocationContext.class); - - unit.intercept(invocationContext); - verify(invocationContext).proceed(); - verify(contextLogger, never()).logWithPrefixedMessage(anyString(), any(), any(), any(), any()); - } - - @Test(expected = RuntimeException.class) - public void logJsonWithPrefixedMessageIfAnExceptionOccurs() throws Exception { - final InvocationContext invocationContext = mock(InvocationContext.class); - final RuntimeException exception = new RuntimeException(); - when(invocationContext.proceed()).thenThrow(exception); - when(invocationContext.getMethod()).thenReturn( - TraceeErrorContextLoggingInterceptorTest.class.getMethod("logJsonWithPrefixedMessageIfAnExceptionOccurs", null)); - - try { - unit.intercept(invocationContext); - } - catch (Exception e) { - verify(invocationContext).proceed(); - verify(contextLogger).logWithPrefixedMessage(LOG_MESSAGE_PREFIX, ImplicitContext.COMMON, ImplicitContext.TRACEE, invocationContext, exception); - throw e; - } - } - - @ErrorMessage(ERROR_MESSAGE) - @Test(expected = RuntimeException.class) - public void logJsonWithAnnotatedMessageAndPrefixedMessageIfAnExceptionOccurs() throws Exception { - final InvocationContext invocationContext = mock(InvocationContext.class); - final RuntimeException exception = new RuntimeException(); - when(invocationContext.proceed()).thenThrow(exception); - when(invocationContext.getMethod()).thenReturn( - TraceeErrorContextLoggingInterceptorTest.class.getMethod("logJsonWithAnnotatedMessageAndPrefixedMessageIfAnExceptionOccurs", null)); - - try { - unit.intercept(invocationContext); - } - catch (Exception e) { - verify(invocationContext).proceed(); - verify(contextLogger).logWithPrefixedMessage(LOG_MESSAGE_PREFIX, traceeMessage, ImplicitContext.COMMON, ImplicitContext.TRACEE, - invocationContext, exception); - throw e; - } - } + private final static String LOG_MESSAGE_PREFIX = "XX"; + + public static final String ERROR_MESSAGE = "ABC"; + + private final TraceeErrorContextLoggingInterceptor unit = mock(TraceeErrorContextLoggingInterceptor.class); + + private final ContextLogger contextLogger = mock(ContextLogger.class); + private final ConfigBuilder configBuilder = mock(ConfigBuilder.class); + + private final TraceeMessage traceeMessage = new TraceeMessage("ABC"); + + @Before + public void setupMocks() throws Exception { + + // Let's return true for every Message. The intercept-method must call teh real method. otherwise + // we would test the mocking framework! + // java.lang.reflect.Method is not suitable for mocks. Also Powermock is unable to create a mock for it! + when(unit.intercept(Mockito.any(InvocationContext.class))).thenCallRealMethod(); + mockStatic(TraceeContextLogger.class); + when(TraceeContextLogger.create()).thenReturn(configBuilder); + when(configBuilder.enforceOrder()).thenReturn(configBuilder); + when(configBuilder.apply()).thenReturn(contextLogger); + mockStatic(TraceeMessage.class); + when(TraceeMessage.wrap(ERROR_MESSAGE)).thenReturn(traceeMessage); + + // mock log message prefix creation + mockStatic(MessagePrefixProvider.class); + when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(String.class))).thenReturn(LOG_MESSAGE_PREFIX); + when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(Class.class))).thenReturn(LOG_MESSAGE_PREFIX); + } + + @Test + public void shouldBeInitializable() { + assertThat(new TraceeErrorContextLoggingInterceptor(), is(not(nullValue()))); + } + + @Test + public void noInteractionWhenNoExceptionOccurs() throws Exception { + final InvocationContext invocationContext = mock(InvocationContext.class); + + unit.intercept(invocationContext); + verify(invocationContext).proceed(); + verify(contextLogger, never()).logWithPrefixedMessage(anyString(), any(), any(), any(), any()); + } + + @Test(expected = RuntimeException.class) + public void logJsonWithPrefixedMessageIfAnExceptionOccurs() throws Exception { + final InvocationContext invocationContext = mock(InvocationContext.class); + final RuntimeException exception = new RuntimeException(); + when(invocationContext.proceed()).thenThrow(exception); + when(invocationContext.getMethod()).thenReturn( + TraceeErrorContextLoggingInterceptorTest.class.getMethod("logJsonWithPrefixedMessageIfAnExceptionOccurs", null)); + + try { + unit.intercept(invocationContext); + } catch (Exception e) { + verify(invocationContext).proceed(); + verify(contextLogger).logWithPrefixedMessage(LOG_MESSAGE_PREFIX, CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, invocationContext, exception); + throw e; + } + } + + @ErrorMessage(ERROR_MESSAGE) + @Test(expected = RuntimeException.class) + public void logJsonWithAnnotatedMessageAndPrefixedMessageIfAnExceptionOccurs() throws Exception { + final InvocationContext invocationContext = mock(InvocationContext.class); + final RuntimeException exception = new RuntimeException(); + when(invocationContext.proceed()).thenThrow(exception); + when(invocationContext.getMethod()).thenReturn( + TraceeErrorContextLoggingInterceptorTest.class.getMethod("logJsonWithAnnotatedMessageAndPrefixedMessageIfAnExceptionOccurs", null)); + + try { + unit.intercept(invocationContext); + } catch (Exception e) { + verify(invocationContext).proceed(); + verify(contextLogger).logWithPrefixedMessage(LOG_MESSAGE_PREFIX, traceeMessage, CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, + invocationContext, exception); + throw e; + } + } } diff --git a/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListenerTest.java b/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListenerTest.java index 648e6ad..1447760 100644 --- a/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListenerTest.java +++ b/contextprovider/javaee/src/test/java/io/tracee/contextlogger/contextprovider/javaee/TraceeJmsErrorMessageListenerTest.java @@ -1,16 +1,9 @@ package io.tracee.contextlogger.contextprovider.javaee; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.*; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - -import java.lang.reflect.Method; - -import javax.interceptor.InvocationContext; - +import io.tracee.contextlogger.TraceeContextLogger; +import io.tracee.contextlogger.api.ConfigBuilder; +import io.tracee.contextlogger.api.ContextLogger; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,10 +11,20 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.api.ConfigBuilder; -import io.tracee.contextlogger.api.ContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import javax.interceptor.InvocationContext; +import java.lang.reflect.Method; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @PrepareForTest(TraceeContextLogger.class) @@ -73,8 +76,8 @@ public void logJsonWithPrefixedMessageIfAnExceptionOccurs() throws Exception { } catch (Exception e) { verify(invocationContext).proceed(); - verify(contextLogger).logWithPrefixedMessage(TraceeJmsErrorMessageListener.JSON_PREFIXED_MESSAGE, ImplicitContext.COMMON, - ImplicitContext.TRACEE, invocationContext, exception); + verify(contextLogger).logWithPrefixedMessage(TraceeJmsErrorMessageListener.JSON_PREFIXED_MESSAGE, CoreImplicitContextProviders.COMMON, + CoreImplicitContextProviders.TRACEE, invocationContext, exception); throw e; } } diff --git a/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/AbstractTraceeErrorLoggingHandler.java b/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/AbstractTraceeErrorLoggingHandler.java index bd1a50f..c7efa35 100644 --- a/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/AbstractTraceeErrorLoggingHandler.java +++ b/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/AbstractTraceeErrorLoggingHandler.java @@ -1,131 +1,126 @@ package io.tracee.contextlogger.contextprovider.jaxws; -import java.io.ByteArrayOutputStream; -import java.nio.charset.Charset; -import java.util.Set; +import io.tracee.contextlogger.MessagePrefixProvider; +import io.tracee.contextlogger.TraceeContextLogger; +import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; +import io.tracee.contextlogger.contextprovider.jaxws.contextprovider.JaxWsWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.xml.namespace.QName; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.tracee.contextlogger.MessagePrefixProvider; -import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; -import io.tracee.contextlogger.api.internal.MessageLogLevel; -import io.tracee.contextlogger.contextprovider.jaxws.contextprovider.JaxWsWrapper; +import java.io.ByteArrayOutputStream; +import java.nio.charset.Charset; +import java.util.Set; /** * Abstract base class for detecting JAX-WS related uncaught exceptions and outputting of contextual information. */ public abstract class AbstractTraceeErrorLoggingHandler implements SOAPHandler { - private static final Logger logger = LoggerFactory.getLogger(AbstractTraceeErrorLoggingHandler.class); - - protected static final ThreadLocal THREAD_LOCAL_SOAP_MESSAGE_STR = new ThreadLocal(); - - /** - * The constructor. - */ - AbstractTraceeErrorLoggingHandler() { - } - - @Override - public final boolean handleMessage(final SOAPMessageContext context) { - if (this.isOutgoing(context)) { - this.handleOutgoing(context); - } - else { - this.handleIncoming(context); - } - return true; - } - - @Override - public final boolean handleFault(SOAPMessageContext context) { - - // Must pipe out Soap envelope - SOAPMessage soapMessage = context.getMessage(); - - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, "JAX-WS"), ImplicitContext.COMMON, - ImplicitContext.TRACEE, JaxWsWrapper.wrap(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), convertSoapMessageAsString(soapMessage))); - - return true; - } - - @Override - public void close(MessageContext context) { - // cleanup thread local request soap message - THREAD_LOCAL_SOAP_MESSAGE_STR.remove(); - } - - @Override - public final Set getHeaders() { - return null; - } - - /** - * Converts a SOAPMessage instance to string representation. - */ - String convertSoapMessageAsString(SOAPMessage soapMessage) { - if (soapMessage == null) { - return "null"; - } - try { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - soapMessage.writeTo(os); - return new String(os.toByteArray(), determineMessageEncoding(soapMessage)); - } - catch (Exception e) { - logger.error("Couldn't create string representation of soapMessage: " + soapMessage.toString()); - return "ERROR"; - } - } - - Charset determineMessageEncoding(SOAPMessage soapMessage) { - try { - final Object encProp = soapMessage.getProperty(SOAPMessage.CHARACTER_SET_ENCODING); - if (encProp != null) { - return Charset.forName(String.valueOf(encProp)); - } - return Charset.forName("UTF-8"); - } - catch (Exception e) { - return Charset.forName("UTF-8"); - } - } - - protected void storeMessageInThreadLocal(SOAPMessageContext context) { - - // Save soap request message in thread local storage for error logging - SOAPMessage soapMessage = context.getMessage(); - if (soapMessage != null) { - String soapMessageAsString = convertSoapMessageAsString(soapMessage); - THREAD_LOCAL_SOAP_MESSAGE_STR.set(soapMessageAsString); - } - } - - protected abstract void handleIncoming(SOAPMessageContext context); - - protected abstract void handleOutgoing(SOAPMessageContext context); - - /** - * Checks whether is is an incoming or outgoing call. - * - * @param messageContext the message context - * @return true if is outgoing call, otherwise false - */ - private boolean isOutgoing(MessageContext messageContext) { - Object outboundBoolean = messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); - return outboundBoolean != null && (Boolean)outboundBoolean; - } + private static final Logger logger = LoggerFactory.getLogger(AbstractTraceeErrorLoggingHandler.class); + + protected static final ThreadLocal THREAD_LOCAL_SOAP_MESSAGE_STR = new ThreadLocal(); + + /** + * The constructor. + */ + AbstractTraceeErrorLoggingHandler() { + } + + @Override + public final boolean handleMessage(final SOAPMessageContext context) { + if (this.isOutgoing(context)) { + this.handleOutgoing(context); + } else { + this.handleIncoming(context); + } + return true; + } + + @Override + public final boolean handleFault(SOAPMessageContext context) { + + // Must pipe out Soap envelope + SOAPMessage soapMessage = context.getMessage(); + + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, "JAX-WS"), CoreImplicitContextProviders.COMMON, + CoreImplicitContextProviders.TRACEE, JaxWsWrapper.wrap(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), convertSoapMessageAsString(soapMessage))); + + return true; + } + + @Override + public void close(MessageContext context) { + // cleanup thread local request soap message + THREAD_LOCAL_SOAP_MESSAGE_STR.remove(); + } + + @Override + public final Set getHeaders() { + return null; + } + + /** + * Converts a SOAPMessage instance to string representation. + */ + String convertSoapMessageAsString(SOAPMessage soapMessage) { + if (soapMessage == null) { + return "null"; + } + try { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + soapMessage.writeTo(os); + return new String(os.toByteArray(), determineMessageEncoding(soapMessage)); + } catch (Exception e) { + logger.error("Couldn't create string representation of soapMessage: " + soapMessage.toString()); + return "ERROR"; + } + } + + Charset determineMessageEncoding(SOAPMessage soapMessage) { + try { + final Object encProp = soapMessage.getProperty(SOAPMessage.CHARACTER_SET_ENCODING); + if (encProp != null) { + return Charset.forName(String.valueOf(encProp)); + } + return Charset.forName("UTF-8"); + } catch (Exception e) { + return Charset.forName("UTF-8"); + } + } + + protected void storeMessageInThreadLocal(SOAPMessageContext context) { + + // Save soap request message in thread local storage for error logging + SOAPMessage soapMessage = context.getMessage(); + if (soapMessage != null) { + String soapMessageAsString = convertSoapMessageAsString(soapMessage); + THREAD_LOCAL_SOAP_MESSAGE_STR.set(soapMessageAsString); + } + } + + protected abstract void handleIncoming(SOAPMessageContext context); + + protected abstract void handleOutgoing(SOAPMessageContext context); + + /** + * Checks whether is is an incoming or outgoing call. + * + * @param messageContext the message context + * @return true if is outgoing call, otherwise false + */ + private boolean isOutgoing(MessageContext messageContext) { + Object outboundBoolean = messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); + return outboundBoolean != null && (Boolean) outboundBoolean; + } } diff --git a/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/contextprovider/JaxWsContextProvider.java b/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/contextprovider/JaxWsContextProvider.java index a00d655..3b0c7c5 100644 --- a/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/contextprovider/JaxWsContextProvider.java +++ b/contextprovider/jaxws/src/main/java/io/tracee/contextlogger/contextprovider/jaxws/contextprovider/JaxWsContextProvider.java @@ -1,7 +1,7 @@ package io.tracee.contextlogger.contextprovider.jaxws.contextprovider; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -10,7 +10,7 @@ * JaxWsContextProvider context provider. */ @TraceeContextProvider(displayName = "jaxWs", order = Order.JAXWS) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class JaxWsContextProvider implements WrappedContextData { private JaxWsWrapper jaxWsWrapper; @@ -32,7 +32,7 @@ public JaxWsWrapper getContextData() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "soapRequest", order = 40) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getSoapRequest() { if (jaxWsWrapper != null) { return jaxWsWrapper.getSoapRequest(); @@ -42,7 +42,7 @@ public final String getSoapRequest() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "soapResponse", order = 50) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getSoapResponse() { if (jaxWsWrapper != null) { return jaxWsWrapper.getSoapResponse(); diff --git a/contextprovider/jaxws/src/test/java/io/tracee/contextlogger/contextprovider/jaxws/TraceeServerErrorLoggingHandlerTest.java b/contextprovider/jaxws/src/test/java/io/tracee/contextlogger/contextprovider/jaxws/TraceeServerErrorLoggingHandlerTest.java index 2d0fba8..6212fd8 100644 --- a/contextprovider/jaxws/src/test/java/io/tracee/contextlogger/contextprovider/jaxws/TraceeServerErrorLoggingHandlerTest.java +++ b/contextprovider/jaxws/src/test/java/io/tracee/contextlogger/contextprovider/jaxws/TraceeServerErrorLoggingHandlerTest.java @@ -1,5 +1,29 @@ package io.tracee.contextlogger.contextprovider.jaxws; +import io.tracee.contextlogger.MessagePrefixProvider; +import io.tracee.contextlogger.TraceeContextLogger; +import io.tracee.contextlogger.api.ConfigBuilder; +import io.tracee.contextlogger.api.ContextLogger; +import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; +import io.tracee.contextlogger.contextprovider.jaxws.contextprovider.JaxWsWrapper; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.xml.namespace.QName; +import javax.xml.soap.MessageFactory; +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPMessage; +import javax.xml.ws.handler.soap.SOAPMessageContext; +import java.io.OutputStream; +import java.nio.charset.Charset; + +import static io.tracee.contextlogger.contextprovider.jaxws.TraceeServerErrorLoggingHandler.THREAD_LOCAL_SOAP_MESSAGE_STR; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -16,175 +40,148 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.verifyStatic; -import static io.tracee.contextlogger.contextprovider.jaxws.TraceeServerErrorLoggingHandler.THREAD_LOCAL_SOAP_MESSAGE_STR; - -import java.io.OutputStream; -import java.nio.charset.Charset; - -import javax.xml.namespace.QName; -import javax.xml.soap.MessageFactory; -import javax.xml.soap.SOAPException; -import javax.xml.soap.SOAPMessage; -import javax.xml.ws.handler.soap.SOAPMessageContext; - -import org.hamcrest.Matchers; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import io.tracee.contextlogger.MessagePrefixProvider; -import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.api.ConfigBuilder; -import io.tracee.contextlogger.api.ContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; -import io.tracee.contextlogger.api.internal.MessageLogLevel; -import io.tracee.contextlogger.contextprovider.jaxws.contextprovider.JaxWsWrapper; - /** * Test class for {@link AbstractTraceeErrorLoggingHandler} and {@link TraceeServerErrorLoggingHandler}. */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ TraceeContextLogger.class, MessagePrefixProvider.class }) +@PrepareForTest({TraceeContextLogger.class, MessagePrefixProvider.class}) public class TraceeServerErrorLoggingHandlerTest { - private final static String LOG_MESSAGE_PREFIX = "XX"; - - private TraceeServerErrorLoggingHandler unit; - private TraceeContextLogger contextLogger; - private final ConfigBuilder configBuilder = mock(ConfigBuilder.class); - - @Before - public void setup() { - unit = new TraceeServerErrorLoggingHandler(); - THREAD_LOCAL_SOAP_MESSAGE_STR.remove(); - - // Stuff for TraceeServerErrorLoggingHandler.handleFault() - mockStatic(TraceeContextLogger.class); - contextLogger = mock(TraceeContextLogger.class); - when(TraceeContextLogger.create()).thenReturn(configBuilder); - when(configBuilder.enforceOrder()).thenReturn(configBuilder); - when(configBuilder.apply()).thenReturn(contextLogger); - - // mock log message prefix creation - mockStatic(MessagePrefixProvider.class); - when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(String.class))).thenReturn(LOG_MESSAGE_PREFIX); - when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(Class.class))).thenReturn(LOG_MESSAGE_PREFIX); - } - - @Test - public void defaultConstructorShouldUseTraceeBackend() { - new TraceeServerErrorLoggingHandler(); - // Verification of call Tracee.getBackend: - verifyStatic(); - } - - @Test - public void shouldConvertNullMessageToNullString() { - assertThat(unit.convertSoapMessageAsString(null), equalTo("null")); - } - - @Test - public void shouldConvertSoapMessageToString() throws Exception { - final SOAPMessage message = buildSpiedTestMessage("vÄ"); - - assertThat(unit.convertSoapMessageAsString(message), - Matchers.allOf(containsString("SOAP-ENV:Body A=\"vÄ\"/>"), containsString("SOAP-ENV:Envelope"))); - } - - @Test - public void messageShouldBeWrittenToThreadLocal() throws Exception { - SOAPMessageContext context = mock(SOAPMessageContext.class); - when(context.getMessage()).thenReturn(buildSpiedTestMessage("vÄ")); - unit.handleIncoming(context); - assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), - Matchers.allOf(notNullValue(), containsString("SOAP-ENV:Body A=\"vÄ\"/>"), containsString("SOAP-ENV:Envelope"))); - } - - @Test - public void messageShouldBeWrittenToThreadLocalAndRespectEncoding() throws Exception { - SOAPMessageContext context = mock(SOAPMessageContext.class); - SOAPMessage soapMessage = buildSpiedTestMessage("vř"); - soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "ISO-8859-2"); - when(context.getMessage()).thenReturn(soapMessage); - - unit.handleIncoming(context); - assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), Matchers.allOf(notNullValue(), containsString("SOAP-ENV:Body A=\"vř\"/>"))); - } - - @Test - public void useUtf8IfNoEncodingIsSpecified() throws Exception { - SOAPMessage soapMessage = buildSpiedTestMessage("vř"); - - assertThat(unit.determineMessageEncoding(soapMessage), is(Charset.forName("UTF-8"))); - } - - @Test - public void useGivenEncodingFromSoapMessage() throws Exception { - SOAPMessage soapMessage = buildSpiedTestMessage("vř"); - soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "ISO-8859-2"); - - assertThat(unit.determineMessageEncoding(soapMessage), is(Charset.forName("ISO-8859-2"))); - } - - @Test - public void fallbackToUtf8IfEncodingisNotSupported() throws Exception { - SOAPMessage soapMessage = buildSpiedTestMessage("vř"); - soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "DonaldDuck"); - - assertThat(unit.determineMessageEncoding(soapMessage), is(Charset.forName("UTF-8"))); - } - - @Test - public void failedMessageTranslationShouldTranslateToErrorString() throws Exception { - SOAPMessage soapMessage = buildSpiedTestMessage("v"); - doThrow(new RuntimeException("ohoh")).when(soapMessage).writeTo(Mockito.any(OutputStream.class)); - assertThat(unit.convertSoapMessageAsString(soapMessage), is("ERROR")); - } - - @Test - public void closeShouldDeleteThreadLocalStore() throws Exception { - THREAD_LOCAL_SOAP_MESSAGE_STR.set("My Value"); - unit.close(null); - assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), is(nullValue())); - } - - @Test - public void faultsShouldLogTheSoapMessage() throws Exception { - final SOAPMessageContext messageContext = mock(SOAPMessageContext.class); - when(messageContext.getMessage()).thenReturn(buildSpiedTestMessage("vA")); - unit.handleFault(messageContext); - verify(contextLogger).logWithPrefixedMessage(eq(LOG_MESSAGE_PREFIX), eq(ImplicitContext.COMMON), eq(ImplicitContext.TRACEE), - Mockito.any(JaxWsWrapper.class)); - } - - @Test - public void handleFouldShouldReturnTrueToProcessWithHandlerChain() throws Exception { - final SOAPMessageContext messageContext = mock(SOAPMessageContext.class); - when(messageContext.getMessage()).thenReturn(buildSpiedTestMessage("vA")); - unit.handleFault(messageContext); - - assertThat(unit.handleFault(messageContext), is(true)); - } - - @Test - public void outgoingMessageShouldNotBeProcessed() { - final SOAPMessageContext messageContext = mock(SOAPMessageContext.class); - verifyNoMoreInteractions(messageContext); - unit.handleOutgoing(messageContext); - assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), is(nullValue())); - } - - @Test - public void registerForNoHeaders() { - assertThat(unit.getHeaders(), is(nullValue())); - } - - private SOAPMessage buildSpiedTestMessage(String value) throws SOAPException { - final SOAPMessage message = MessageFactory.newInstance().createMessage(); - message.getSOAPBody().addAttribute(new QName("A"), value); - return spy(message); - } + private final static String LOG_MESSAGE_PREFIX = "XX"; + + private TraceeServerErrorLoggingHandler unit; + private TraceeContextLogger contextLogger; + private final ConfigBuilder configBuilder = mock(ConfigBuilder.class); + + @Before + public void setup() { + unit = new TraceeServerErrorLoggingHandler(); + THREAD_LOCAL_SOAP_MESSAGE_STR.remove(); + + // Stuff for TraceeServerErrorLoggingHandler.handleFault() + mockStatic(TraceeContextLogger.class); + contextLogger = mock(TraceeContextLogger.class); + when(TraceeContextLogger.create()).thenReturn(configBuilder); + when(configBuilder.enforceOrder()).thenReturn(configBuilder); + when(configBuilder.apply()).thenReturn(contextLogger); + + // mock log message prefix creation + mockStatic(MessagePrefixProvider.class); + when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(String.class))).thenReturn(LOG_MESSAGE_PREFIX); + when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(Class.class))).thenReturn(LOG_MESSAGE_PREFIX); + } + + @Test + public void defaultConstructorShouldUseTraceeBackend() { + new TraceeServerErrorLoggingHandler(); + // Verification of call Tracee.getBackend: + verifyStatic(); + } + + @Test + public void shouldConvertNullMessageToNullString() { + assertThat(unit.convertSoapMessageAsString(null), equalTo("null")); + } + + @Test + public void shouldConvertSoapMessageToString() throws Exception { + final SOAPMessage message = buildSpiedTestMessage("vÄ"); + + assertThat(unit.convertSoapMessageAsString(message), + Matchers.allOf(containsString("SOAP-ENV:Body A=\"vÄ\"/>"), containsString("SOAP-ENV:Envelope"))); + } + + @Test + public void messageShouldBeWrittenToThreadLocal() throws Exception { + SOAPMessageContext context = mock(SOAPMessageContext.class); + when(context.getMessage()).thenReturn(buildSpiedTestMessage("vÄ")); + unit.handleIncoming(context); + assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), + Matchers.allOf(notNullValue(), containsString("SOAP-ENV:Body A=\"vÄ\"/>"), containsString("SOAP-ENV:Envelope"))); + } + + @Test + public void messageShouldBeWrittenToThreadLocalAndRespectEncoding() throws Exception { + SOAPMessageContext context = mock(SOAPMessageContext.class); + SOAPMessage soapMessage = buildSpiedTestMessage("vř"); + soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "ISO-8859-2"); + when(context.getMessage()).thenReturn(soapMessage); + + unit.handleIncoming(context); + assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), Matchers.allOf(notNullValue(), containsString("SOAP-ENV:Body A=\"vř\"/>"))); + } + + @Test + public void useUtf8IfNoEncodingIsSpecified() throws Exception { + SOAPMessage soapMessage = buildSpiedTestMessage("vř"); + + assertThat(unit.determineMessageEncoding(soapMessage), is(Charset.forName("UTF-8"))); + } + + @Test + public void useGivenEncodingFromSoapMessage() throws Exception { + SOAPMessage soapMessage = buildSpiedTestMessage("vř"); + soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "ISO-8859-2"); + + assertThat(unit.determineMessageEncoding(soapMessage), is(Charset.forName("ISO-8859-2"))); + } + + @Test + public void fallbackToUtf8IfEncodingisNotSupported() throws Exception { + SOAPMessage soapMessage = buildSpiedTestMessage("vř"); + soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "DonaldDuck"); + + assertThat(unit.determineMessageEncoding(soapMessage), is(Charset.forName("UTF-8"))); + } + + @Test + public void failedMessageTranslationShouldTranslateToErrorString() throws Exception { + SOAPMessage soapMessage = buildSpiedTestMessage("v"); + doThrow(new RuntimeException("ohoh")).when(soapMessage).writeTo(Mockito.any(OutputStream.class)); + assertThat(unit.convertSoapMessageAsString(soapMessage), is("ERROR")); + } + + @Test + public void closeShouldDeleteThreadLocalStore() throws Exception { + THREAD_LOCAL_SOAP_MESSAGE_STR.set("My Value"); + unit.close(null); + assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), is(nullValue())); + } + + @Test + public void faultsShouldLogTheSoapMessage() throws Exception { + final SOAPMessageContext messageContext = mock(SOAPMessageContext.class); + when(messageContext.getMessage()).thenReturn(buildSpiedTestMessage("vA")); + unit.handleFault(messageContext); + verify(contextLogger).logWithPrefixedMessage(eq(LOG_MESSAGE_PREFIX), eq(CoreImplicitContextProviders.COMMON), eq(CoreImplicitContextProviders.TRACEE), + Mockito.any(JaxWsWrapper.class)); + } + + @Test + public void handleFouldShouldReturnTrueToProcessWithHandlerChain() throws Exception { + final SOAPMessageContext messageContext = mock(SOAPMessageContext.class); + when(messageContext.getMessage()).thenReturn(buildSpiedTestMessage("vA")); + unit.handleFault(messageContext); + + assertThat(unit.handleFault(messageContext), is(true)); + } + + @Test + public void outgoingMessageShouldNotBeProcessed() { + final SOAPMessageContext messageContext = mock(SOAPMessageContext.class); + verifyNoMoreInteractions(messageContext); + unit.handleOutgoing(messageContext); + assertThat(THREAD_LOCAL_SOAP_MESSAGE_STR.get(), is(nullValue())); + } + + @Test + public void registerForNoHeaders() { + assertThat(unit.getHeaders(), is(nullValue())); + } + + private SOAPMessage buildSpiedTestMessage(String value) throws SOAPException { + final SOAPMessage message = MessageFactory.newInstance().createMessage(); + message.getSOAPBody().addAttribute(new QName("A"), value); + return spy(message); + } } diff --git a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletCookieContextProvider.java b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletCookieContextProvider.java index ee0b10b..0fbe540 100644 --- a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletCookieContextProvider.java +++ b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletCookieContextProvider.java @@ -1,7 +1,7 @@ package io.tracee.contextlogger.contextprovider.servlet.contextprovider; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -13,7 +13,7 @@ * Created by Tobias Gindler, holisticon AG on 24.01.14. */ @TraceeContextProvider(displayName = "servletCookies", order = Order.SERVLET) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class ServletCookieContextProvider implements WrappedContextData { private Cookie cookie; @@ -39,7 +39,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "name", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getName() { if (cookie != null) { return cookie.getName(); @@ -49,7 +49,7 @@ public String getName() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "value", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getValue() { if (cookie != null) { return cookie.getValue(); @@ -59,7 +59,7 @@ public String getValue() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "domain", order = 30) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getDomain() { if (cookie != null) { return cookie.getDomain(); @@ -69,7 +69,7 @@ public String getDomain() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "path", order = 40) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getPath() { if (cookie != null) { return cookie.getPath(); @@ -79,7 +79,7 @@ public String getPath() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "secure", order = 50) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public Boolean getSecure() { if (cookie != null) { return cookie.getSecure(); @@ -89,7 +89,7 @@ public Boolean getSecure() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "max-age", order = 60) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public Integer getMaxAge() { if (cookie != null) { return cookie.getMaxAge(); diff --git a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletRequestContextProvider.java b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletRequestContextProvider.java index 6034d33..783c8e8 100644 --- a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletRequestContextProvider.java +++ b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletRequestContextProvider.java @@ -1,7 +1,7 @@ package io.tracee.contextlogger.contextprovider.servlet.contextprovider; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -19,7 +19,7 @@ */ @TraceeContextProvider(displayName = "servletRequest", order = Order.SERVLET) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class ServletRequestContextProvider implements WrappedContextData { private HttpServletRequest request; @@ -49,7 +49,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "url", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getUrl() { if (this.request != null && request.getRequestURL() != null) { return request.getRequestURL().toString(); @@ -59,7 +59,7 @@ public String getUrl() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-method", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public String getHttpMethod() { if (this.request != null) { return this.request.getMethod(); @@ -69,7 +69,7 @@ public String getHttpMethod() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-parameters", order = 30) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public List> getHttpParameters() { final List> list = new ArrayList>(); @@ -96,7 +96,7 @@ public List> getHttpParameters() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-request-headers", order = 40) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public List> getHttpRequestHeaders() { final List> list = new ArrayList>(); @@ -117,7 +117,7 @@ public List> getHttpRequestHeaders() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-request-attributes", order = 45) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public List> getHttpRequestAttributes() { final List> list = new ArrayList>(); @@ -138,7 +138,7 @@ public List> getHttpRequestAttributes() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "cookies", order = 50) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public List getCookies() { List wrappedCookies = new ArrayList(); @@ -154,7 +154,7 @@ public List getCookies() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-remote-address", order = 150) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public String getHttpRemoteAddress() { if (this.request != null) { return request.getRemoteAddr(); @@ -164,7 +164,7 @@ public String getHttpRemoteAddress() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-remote-host", order = 160) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public String getHttpRemoteHost() { if (this.request != null) { return this.request.getRemoteHost(); @@ -174,7 +174,7 @@ public String getHttpRemoteHost() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-remote-port", order = 170) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public Integer getHttpRemotePort() { if (this.request != null) { return this.request.getRemotePort(); @@ -184,7 +184,7 @@ public Integer getHttpRemotePort() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "scheme", order = 200) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public String getScheme() { if (this.request != null) { return this.request.getScheme(); @@ -195,7 +195,7 @@ public String getScheme() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "is-secure", order = 210) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public Boolean getSecure() { if (this.request != null) { return this.request.isSecure(); @@ -206,7 +206,7 @@ public Boolean getSecure() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "content-type", order = 220) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public String getContentType() { if (this.request != null) { return this.request.getContentType(); @@ -217,7 +217,7 @@ public String getContentType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "content-length", order = 230) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public Integer getContentLength() { if (this.request != null) { return this.request.getContentLength(); @@ -227,7 +227,7 @@ public Integer getContentLength() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "locale", order = 240) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public String getLocale() { if (this.request != null && this.request.getLocale() != null) { return this.request.getLocale().toString(); diff --git a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletResponseContextProvider.java b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletResponseContextProvider.java index b76037c..e38bc7b 100644 --- a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletResponseContextProvider.java +++ b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletResponseContextProvider.java @@ -1,7 +1,7 @@ package io.tracee.contextlogger.contextprovider.servlet.contextprovider; import io.tracee.contextlogger.contextprovider.api.Order; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -17,7 +17,7 @@ * Created by Tobias Gindler, holisticon AG on 20.03.14. */ @TraceeContextProvider(displayName = "servletResponse", order = Order.SERVLET) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class ServletResponseContextProvider implements WrappedContextData { private HttpServletResponse response; @@ -46,7 +46,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-status-code", order = 10) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public Integer getHttpStatusCode() { if (this.response != null) { return this.response.getStatus(); @@ -56,7 +56,7 @@ public Integer getHttpStatusCode() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "http-header", order = 20) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public List> getHttpResponseHeaders() { final List> list = new ArrayList>(); diff --git a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletSessionContextProvider.java b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletSessionContextProvider.java index 257a8d0..a4478b2 100644 --- a/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletSessionContextProvider.java +++ b/contextprovider/servlet/core/src/main/java/io/tracee/contextlogger/contextprovider/servlet/contextprovider/ServletSessionContextProvider.java @@ -2,7 +2,7 @@ import io.tracee.contextlogger.contextprovider.api.Order; import io.tracee.contextlogger.contextprovider.api.Flatten; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -18,7 +18,7 @@ * Created by Tobias Gindler, holisticon AG on 19.03.14. */ @TraceeContextProvider(displayName = "servletSession", order = Order.SERVLET) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public final class ServletSessionContextProvider implements WrappedContextData { private HttpSession session; @@ -48,7 +48,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @Flatten @TraceeContextProviderMethod(displayName = "DYNAMIC") - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public List> getSessionAttributes() { if (session == null) { diff --git a/contextprovider/servlet/servlet/src/main/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilter.java b/contextprovider/servlet/servlet/src/main/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilter.java index 3df0c6c..e446cbc 100644 --- a/contextprovider/servlet/servlet/src/main/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilter.java +++ b/contextprovider/servlet/servlet/src/main/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilter.java @@ -1,6 +1,9 @@ package io.tracee.contextlogger.contextprovider.servlet; -import java.io.IOException; +import io.tracee.contextlogger.MessagePrefixProvider; +import io.tracee.contextlogger.TraceeContextLogger; +import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -10,11 +13,7 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import io.tracee.contextlogger.MessagePrefixProvider; -import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; -import io.tracee.contextlogger.api.internal.MessageLogLevel; +import java.io.IOException; /** * Servlet filter to detect uncaught exceptions and to provide some contextual error logs. @@ -22,50 +21,49 @@ */ public class TraceeErrorLoggingFilter implements Filter { - @Override - public final void init(FilterConfig filterConfig) throws ServletException { + @Override + public final void init(FilterConfig filterConfig) throws ServletException { - } + } - @Override - public final void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, - ServletException { + @Override + public final void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, + ServletException { - try { - filterChain.doFilter(servletRequest, servletResponse); - } - catch (Throwable e) { + try { + filterChain.doFilter(servletRequest, servletResponse); + } catch (Throwable e) { - if (servletRequest instanceof HttpServletRequest) { - handleHttpServletRequest((HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse, e); - } + if (servletRequest instanceof HttpServletRequest) { + handleHttpServletRequest((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse, e); + } - // now rethrow error - if (e instanceof IOException) { - throw (IOException)e; - } - if (e instanceof ServletException) { - throw (ServletException)e; - } + // now rethrow error + if (e instanceof IOException) { + throw (IOException) e; + } + if (e instanceof ServletException) { + throw (ServletException) e; + } - // wrap all other kinds of exceptions ... - throw new ServletException(e); - } - } + // wrap all other kinds of exceptions ... + throw new ServletException(e); + } + } - private void handleHttpServletRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Throwable e) { + private void handleHttpServletRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Throwable e) { - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorLoggingFilter.class), - ImplicitContext.COMMON, ImplicitContext.TRACEE, servletRequest, servletResponse, servletRequest.getSession(false), e); + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorLoggingFilter.class), + CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, servletRequest, servletResponse, servletRequest.getSession(false), e); - } + } - @Override - public final void destroy() { + @Override + public final void destroy() { - } + } } diff --git a/contextprovider/servlet/servlet/src/test/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilterTest.java b/contextprovider/servlet/servlet/src/test/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilterTest.java index 5dc7fa5..b9b0752 100644 --- a/contextprovider/servlet/servlet/src/test/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilterTest.java +++ b/contextprovider/servlet/servlet/src/test/java/io/tracee/contextlogger/contextprovider/servlet/TraceeErrorLoggingFilterTest.java @@ -1,21 +1,11 @@ package io.tracee.contextlogger.contextprovider.servlet; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.sameInstance; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - -import java.io.IOException; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; - +import io.tracee.contextlogger.MessagePrefixProvider; +import io.tracee.contextlogger.TraceeContextLogger; +import io.tracee.contextlogger.api.ConfigBuilder; +import io.tracee.contextlogger.api.ContextLogger; +import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,98 +13,102 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import io.tracee.contextlogger.MessagePrefixProvider; -import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.api.ConfigBuilder; -import io.tracee.contextlogger.api.ContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; -import io.tracee.contextlogger.api.internal.MessageLogLevel; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.sameInstance; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) -@PrepareForTest({ TraceeContextLogger.class, MessagePrefixProvider.class }) +@PrepareForTest({TraceeContextLogger.class, MessagePrefixProvider.class}) public class TraceeErrorLoggingFilterTest { - private final static String LOG_MESSAGE_PREFIX = "XX"; - - private final TraceeErrorLoggingFilter unit = new TraceeErrorLoggingFilter(); - private final HttpServletRequest request = mock(HttpServletRequest.class); - private final HttpServletResponse response = mock(HttpServletResponse.class); - private final HttpSession session = mock(HttpSession.class); - private final FilterChain filterChain = mock(FilterChain.class); - private TraceeContextLogger traceeContextLogger = mock(TraceeContextLogger.class); - private final ConfigBuilder configBuilder = mock(ConfigBuilder.class); - - @Before - public void setUpMocks() { - // mock log message prefix creation - mockStatic(MessagePrefixProvider.class); - when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(String.class))).thenReturn(LOG_MESSAGE_PREFIX); - when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(Class.class))).thenReturn(LOG_MESSAGE_PREFIX); - - mockStatic(TraceeContextLogger.class); - when(TraceeContextLogger.create()).thenReturn(configBuilder); - when(configBuilder.enforceOrder()).thenReturn(configBuilder); - when(configBuilder.apply()).thenReturn(traceeContextLogger); - when(request.getSession(false)).thenReturn(session); - } - - @Test - public void isTransparentWhenEverythingIsFine() throws Exception { - unit.doFilter(request, response, filterChain); - verify(filterChain).doFilter(request, response); - } - - @Test(expected = ServletException.class) - public void logWholeContextOnException() throws Exception { - final ServletException expectedException = new ServletException("test"); - try { - doThrow(expectedException).when(filterChain).doFilter(request, response); - unit.doFilter(request, response, filterChain); - } - catch (Exception e) { - verify(traceeContextLogger).logWithPrefixedMessage(LOG_MESSAGE_PREFIX, ImplicitContext.COMMON, ImplicitContext.TRACEE, request, response, - session, expectedException); - throw e; - } - } - - @Test(expected = ServletException.class) - public void rethrowRuntimeException() throws Exception { - final RuntimeException expectedException = new RuntimeException(); - try { - doThrow(expectedException).when(filterChain).doFilter(request, response); - unit.doFilter(request, response, filterChain); - } - catch (ServletException e) { - assertThat(e.getRootCause(), sameInstance((Throwable)expectedException)); - throw e; - } - } - - @Test(expected = IOException.class) - public void rethrowIOException() throws Exception { - final IOException expectedException = new IOException(); - try { - doThrow(expectedException).when(filterChain).doFilter(request, response); - unit.doFilter(request, response, filterChain); - } - catch (IOException e) { - assertThat(e, sameInstance(expectedException)); - throw e; - } - } - - @Test(expected = ServletException.class) - public void rethrowServletException() throws Exception { - final ServletException expectedException = new ServletException(); - try { - doThrow(expectedException).when(filterChain).doFilter(request, response); - unit.doFilter(request, response, filterChain); - } - catch (ServletException e) { - assertThat(e, sameInstance(expectedException)); - throw e; - } - } + private final static String LOG_MESSAGE_PREFIX = "XX"; + + private final TraceeErrorLoggingFilter unit = new TraceeErrorLoggingFilter(); + private final HttpServletRequest request = mock(HttpServletRequest.class); + private final HttpServletResponse response = mock(HttpServletResponse.class); + private final HttpSession session = mock(HttpSession.class); + private final FilterChain filterChain = mock(FilterChain.class); + private TraceeContextLogger traceeContextLogger = mock(TraceeContextLogger.class); + private final ConfigBuilder configBuilder = mock(ConfigBuilder.class); + + @Before + public void setUpMocks() { + // mock log message prefix creation + mockStatic(MessagePrefixProvider.class); + when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(String.class))).thenReturn(LOG_MESSAGE_PREFIX); + when(MessagePrefixProvider.provideLogMessagePrefix(Mockito.any(MessageLogLevel.class), Mockito.any(Class.class))).thenReturn(LOG_MESSAGE_PREFIX); + + mockStatic(TraceeContextLogger.class); + when(TraceeContextLogger.create()).thenReturn(configBuilder); + when(configBuilder.enforceOrder()).thenReturn(configBuilder); + when(configBuilder.apply()).thenReturn(traceeContextLogger); + when(request.getSession(false)).thenReturn(session); + } + + @Test + public void isTransparentWhenEverythingIsFine() throws Exception { + unit.doFilter(request, response, filterChain); + verify(filterChain).doFilter(request, response); + } + + @Test(expected = ServletException.class) + public void logWholeContextOnException() throws Exception { + final ServletException expectedException = new ServletException("test"); + try { + doThrow(expectedException).when(filterChain).doFilter(request, response); + unit.doFilter(request, response, filterChain); + } catch (Exception e) { + verify(traceeContextLogger).logWithPrefixedMessage(LOG_MESSAGE_PREFIX, CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, request, response, + session, expectedException); + throw e; + } + } + + @Test(expected = ServletException.class) + public void rethrowRuntimeException() throws Exception { + final RuntimeException expectedException = new RuntimeException(); + try { + doThrow(expectedException).when(filterChain).doFilter(request, response); + unit.doFilter(request, response, filterChain); + } catch (ServletException e) { + assertThat(e.getRootCause(), sameInstance((Throwable) expectedException)); + throw e; + } + } + + @Test(expected = IOException.class) + public void rethrowIOException() throws Exception { + final IOException expectedException = new IOException(); + try { + doThrow(expectedException).when(filterChain).doFilter(request, response); + unit.doFilter(request, response, filterChain); + } catch (IOException e) { + assertThat(e, sameInstance(expectedException)); + throw e; + } + } + + @Test(expected = ServletException.class) + public void rethrowServletException() throws Exception { + final ServletException expectedException = new ServletException(); + try { + doThrow(expectedException).when(filterChain).doFilter(request, response); + unit.doFilter(request, response, filterChain); + } catch (ServletException e) { + assertThat(e, sameInstance(expectedException)); + throw e; + } + } } diff --git a/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/TraceeContextLoggerHandlerInterceptor.java b/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/TraceeContextLoggerHandlerInterceptor.java index 45a88c1..a95a528 100644 --- a/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/TraceeContextLoggerHandlerInterceptor.java +++ b/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/TraceeContextLoggerHandlerInterceptor.java @@ -1,45 +1,44 @@ package io.tracee.contextlogger.contextprovider.springmvc; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.ModelAndView; - import io.tracee.contextlogger.MessagePrefixProvider; import io.tracee.contextlogger.TraceeContextLogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.api.internal.MessageLogLevel; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; public class TraceeContextLoggerHandlerInterceptor implements HandlerInterceptor { - @Override - public boolean preHandle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o) throws Exception { - return true; - } + @Override + public boolean preHandle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o) throws Exception { + return true; + } - @Override - public void postHandle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o, - final ModelAndView modelAndView) throws Exception { + @Override + public void postHandle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o, + final ModelAndView modelAndView) throws Exception { - } + } - @Override - public void afterCompletion(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o, - final Exception e) throws Exception { + @Override + public void afterCompletion(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o, + final Exception e) throws Exception { - // Output context data in case of an exception - if (e != null) { + // Output context data in case of an exception + if (e != null) { - TraceeContextLogger - .create() - .enforceOrder() - .apply() - .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeContextLoggerHandlerInterceptor.class), - ImplicitContext.COMMON, ImplicitContext.TRACEE, o, httpServletRequest, httpServletResponse, - httpServletRequest.getSession(false), e); + TraceeContextLogger + .create() + .enforceOrder() + .apply() + .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeContextLoggerHandlerInterceptor.class), + CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, o, httpServletRequest, httpServletResponse, + httpServletRequest.getSession(false), e); - } + } - } + } } diff --git a/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/HandlerMethodContextProvider.java b/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/HandlerMethodContextProvider.java index 4235b86..a2dd1e6 100644 --- a/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/HandlerMethodContextProvider.java +++ b/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/HandlerMethodContextProvider.java @@ -1,6 +1,6 @@ package io.tracee.contextlogger.contextprovider.springmvc.contextprovider; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -14,7 +14,7 @@ * Context provider for {@link HandlerMethod}. */ @TraceeContextProvider(displayName = "handlerMethod") -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class HandlerMethodContextProvider implements WrappedContextData { private HandlerMethod handlerMethod; @@ -48,7 +48,7 @@ public static HandlerMethodContextProvider wrap(final HandlerMethod handlerMetho @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "type", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getType() { if (handlerMethod != null && handlerMethod.getBeanType() != null) { return handlerMethod.getBeanType().getCanonicalName(); @@ -58,7 +58,7 @@ public final String getType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "method", order = 30) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getMethod() { if (handlerMethod != null && handlerMethod.getMethod() != null) { return handlerMethod.getMethod().getName(); @@ -68,7 +68,7 @@ public final String getMethod() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "parameters", order = 40) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final List getParameters() { if (handlerMethod != null && handlerMethod.getMethodParameters() != null) { @@ -82,7 +82,7 @@ public final List getParameters() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "serialized-target-instance", order = 50) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public final Object getSerializedTargetInstance() { if (handlerMethod != null) { // output invoked instance diff --git a/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/MethodParameterContextProvider.java b/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/MethodParameterContextProvider.java index 85e8e23..92936e7 100644 --- a/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/MethodParameterContextProvider.java +++ b/contextprovider/springmvc/src/main/java/io/tracee/contextlogger/contextprovider/springmvc/contextprovider/MethodParameterContextProvider.java @@ -1,6 +1,6 @@ package io.tracee.contextlogger.contextprovider.springmvc.contextprovider; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -10,7 +10,7 @@ * Context provider dor {@link MethodParameter}. */ @TraceeContextProvider(displayName = "methodParameter") -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class MethodParameterContextProvider implements WrappedContextData { private MethodParameter methodParameter; @@ -44,7 +44,7 @@ public static MethodParameterContextProvider wrap(final MethodParameter methodPa @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "type", order = 20) - @ProfileSettings(basic = true, enhanced = true, full = true) + @ProfileConfig(basic = true, enhanced = true, full = true) public final String getType() { if (methodParameter != null && methodParameter.getParameterType() != null) { return methodParameter.getParameterType().getCanonicalName(); diff --git a/core/src/test/java/io/tracee/contextlogger/TestMain.java b/core/src/test/java/io/tracee/contextlogger/TestMain.java index 3085b05..a4e7945 100644 --- a/core/src/test/java/io/tracee/contextlogger/TestMain.java +++ b/core/src/test/java/io/tracee/contextlogger/TestMain.java @@ -1,17 +1,17 @@ package io.tracee.contextlogger; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; /** * Created by TGI on 30.01.2015. */ public class TestMain { - public static void main(String[] args) { + public static void main(String[] args) { - TraceeContextLogger.createDefault().logWithPrefixedMessage("ABC", "DEF", ImplicitContext.COMMON, ImplicitContext.TRACEE); - TraceeContextLogger.createDefault().log("ABC", "DEF", ImplicitContext.COMMON, ImplicitContext.TRACEE); + TraceeContextLogger.createDefault().logWithPrefixedMessage("ABC", "DEF", CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE); + TraceeContextLogger.createDefault().log("ABC", "DEF", CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE); - } + } } diff --git a/core/src/test/java/io/tracee/contextlogger/impl/ContextLoggerConfigurationTest.java b/core/src/test/java/io/tracee/contextlogger/impl/ContextLoggerConfigurationTest.java index b6c04db..c949249 100644 --- a/core/src/test/java/io/tracee/contextlogger/impl/ContextLoggerConfigurationTest.java +++ b/core/src/test/java/io/tracee/contextlogger/impl/ContextLoggerConfigurationTest.java @@ -1,91 +1,90 @@ package io.tracee.contextlogger.impl; import io.tracee.contextlogger.contextprovider.api.Profile; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; +import io.tracee.contextlogger.contextprovider.core.java.JavaThrowableContextProvider; +import io.tracee.contextlogger.profile.ProfilePropertyNames; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; -import io.tracee.contextlogger.contextprovider.core.java.JavaThrowableContextProvider; -import io.tracee.contextlogger.profile.ProfilePropertyNames; - /** * Test class for {@link ContextLoggerConfiguration}. * Created by Tobias Gindler, holisticon AG on 01.04.14. */ public class ContextLoggerConfigurationTest { - @Before - public void init() { - System.setProperty(ProfilePropertyNames.PROFILE_SET_GLOBALLY_VIA_SYSTEM_PROPERTIES, Profile.BASIC.name()); - } + @Before + public void init() { + System.setProperty(ProfilePropertyNames.PROFILE_SET_GLOBALLY_VIA_SYSTEM_PROPERTIES, Profile.BASIC.name()); + } - @Test - public void should_get_context_logger_configuration() { + @Test + public void should_get_context_logger_configuration() { - ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); + ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); - MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); - } + } - @Test - public void should_get_implicit_wrapper_list_of_context_logger_configuration() { + @Test + public void should_get_implicit_wrapper_list_of_context_logger_configuration() { - ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); + ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); - MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getImplicitContextClassMap(), Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getImplicitContextClassMap().containsKey(ImplicitContext.COMMON), Matchers.equalTo(true)); - MatcherAssert.assertThat(contextLoggerConfiguration.getImplicitContextClassMap().containsKey(ImplicitContext.TRACEE), Matchers.equalTo(true)); + MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getImplicitContextClassMap(), Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getImplicitContextClassMap().containsKey(CoreImplicitContextProviders.COMMON), Matchers.equalTo(true)); + MatcherAssert.assertThat(contextLoggerConfiguration.getImplicitContextClassMap().containsKey(CoreImplicitContextProviders.TRACEE), Matchers.equalTo(true)); - } + } - @Test - public void should_get_wrapper_class_mapping_of_context_logger_configuration() { + @Test + public void should_get_wrapper_class_mapping_of_context_logger_configuration() { - ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); + ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); - MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getClassToWrapperMap(), Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getClassToWrapperMap().containsKey(Throwable.class), Matchers.equalTo(true)); - MatcherAssert.assertThat(contextLoggerConfiguration.getClassToWrapperMap().get(Throwable.class).equals(JavaThrowableContextProvider.class), - Matchers.equalTo(true)); + MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getClassToWrapperMap(), Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getClassToWrapperMap().containsKey(Throwable.class), Matchers.equalTo(true)); + MatcherAssert.assertThat(contextLoggerConfiguration.getClassToWrapperMap().get(Throwable.class).equals(JavaThrowableContextProvider.class), + Matchers.equalTo(true)); - } + } - @Test - public void should_get_profile_of_context_logger_configuration() { + @Test + public void should_get_profile_of_context_logger_configuration() { - ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); + ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); - MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getProfile(), Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getProfile(), Matchers.equalTo(Profile.BASIC)); + MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getProfile(), Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getProfile(), Matchers.equalTo(Profile.BASIC)); - } + } - @Test - public void should_get_wrapper_classes_of_context_logger_configuration() { + @Test + public void should_get_wrapper_classes_of_context_logger_configuration() { - ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); + ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); - MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperClasses(), Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperClasses().contains(JavaThrowableContextProvider.class), Matchers.equalTo(true)); + MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperClasses(), Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperClasses().contains(JavaThrowableContextProvider.class), Matchers.equalTo(true)); - } + } - @Test - public void should_get_wrappers_of_context_logger_configuration() { + @Test + public void should_get_wrappers_of_context_logger_configuration() { - ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); + ContextLoggerConfiguration contextLoggerConfiguration = ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration(); - MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration, Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperList(), Matchers.notNullValue()); - MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperList().size(), Matchers.greaterThan(0)); - } + MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperList(), Matchers.notNullValue()); + MatcherAssert.assertThat(contextLoggerConfiguration.getWrapperList().size(), Matchers.greaterThan(0)); + } } diff --git a/core/src/test/java/io/tracee/contextlogger/outputgenerator/functions/TraceeContextProviderWrapperFunctionTest.java b/core/src/test/java/io/tracee/contextlogger/outputgenerator/functions/TraceeContextProviderWrapperFunctionTest.java index 3253a1a..8d2122f 100644 --- a/core/src/test/java/io/tracee/contextlogger/outputgenerator/functions/TraceeContextProviderWrapperFunctionTest.java +++ b/core/src/test/java/io/tracee/contextlogger/outputgenerator/functions/TraceeContextProviderWrapperFunctionTest.java @@ -1,192 +1,192 @@ package io.tracee.contextlogger.outputgenerator.functions; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.when; +import io.tracee.contextlogger.contextprovider.TypeToWrapper; +import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import io.tracee.contextlogger.contextprovider.api.WrappedContextData; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; +import io.tracee.contextlogger.impl.ContextLoggerConfiguration; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.hamcrest.Matchers; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; -import io.tracee.contextlogger.contextprovider.TypeToWrapper; -import io.tracee.contextlogger.contextprovider.api.WrappedContextData; -import io.tracee.contextlogger.impl.ContextLoggerConfiguration; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.when; /** * Unit test for {@link TraceeContextProviderWrapperFunction}. */ public class TraceeContextProviderWrapperFunctionTest { - private ContextLoggerConfiguration contextLoggerConfiguration = Mockito.mock(ContextLoggerConfiguration.class); + private ContextLoggerConfiguration contextLoggerConfiguration = Mockito.mock(ContextLoggerConfiguration.class); - private TraceeContextProviderWrapperFunction unit = TraceeContextProviderWrapperFunction.getInstance(); + private TraceeContextProviderWrapperFunction unit = TraceeContextProviderWrapperFunction.getInstance(); - public static class ClassWithNoNoargConstructor { + public static class ClassWithNoNoargConstructor { - public ClassWithNoNoargConstructor(String xyz) { + public ClassWithNoNoargConstructor(String xyz) { - } + } - } + } - public static class UnmappedClass { + public static class UnmappedClass { - } + } - public static class MappedClass { + public static class MappedClass { - } + } - public static class MappedClassWrapper implements WrappedContextData { + public static class MappedClassWrapper implements WrappedContextData { - private MappedClass instance; + private MappedClass instance; - @Override - public void setContextData(final Object instance) throws ClassCastException { - this.instance = (MappedClass)instance; - } + @Override + public void setContextData(final Object instance) throws ClassCastException { + this.instance = (MappedClass) instance; + } - @Override - public MappedClass getContextData() { - return this.instance; - } + @Override + public MappedClass getContextData() { + return this.instance; + } - @Override - public Class getWrappedType() { - return MappedClass.class; - } - } + @Override + public Class getWrappedType() { + return MappedClass.class; + } + } - public static class IncorrectMappedClass { + public static class IncorrectMappedClass { - } + } - public static class IncorrectMappedClassWrapper { + public static class IncorrectMappedClassWrapper { - } + } - public static class WrappedTypeListType { + public static class WrappedTypeListType { - } + } - public static class WrappedTypeListTypeWrapper implements WrappedContextData { + public static class WrappedTypeListTypeWrapper implements WrappedContextData { - private WrappedTypeListType instance; + private WrappedTypeListType instance; - @Override - public void setContextData(final Object instance) throws ClassCastException { - this.instance = (WrappedTypeListType)instance; - } + @Override + public void setContextData(final Object instance) throws ClassCastException { + this.instance = (WrappedTypeListType) instance; + } - @Override - public WrappedTypeListType getContextData() { - return this.instance; - } + @Override + public WrappedTypeListType getContextData() { + return this.instance; + } - @Override - public Class getWrappedType() { - return WrappedTypeListType.class; - } - } + @Override + public Class getWrappedType() { + return WrappedTypeListType.class; + } + } - @Before - public void init() { + @Before + public void init() { - Map implicitContextClassMap = new HashMap(); - implicitContextClassMap.put(ImplicitContext.COMMON, TraceeContextProviderWrapperFunctionTest.class); + Map implicitContextClassMap = new HashMap(); + implicitContextClassMap.put(CoreImplicitContextProviders.COMMON, TraceeContextProviderWrapperFunctionTest.class); - when(contextLoggerConfiguration.getImplicitContextClassMap()).thenReturn(implicitContextClassMap); + when(contextLoggerConfiguration.getImplicitContextClassMap()).thenReturn(implicitContextClassMap); - Map classToWrapperMap = new HashMap(); - classToWrapperMap.put(MappedClass.class, MappedClassWrapper.class); - classToWrapperMap.put(IncorrectMappedClass.class, IncorrectMappedClassWrapper.class); + Map classToWrapperMap = new HashMap(); + classToWrapperMap.put(MappedClass.class, MappedClassWrapper.class); + classToWrapperMap.put(IncorrectMappedClass.class, IncorrectMappedClassWrapper.class); - when(contextLoggerConfiguration.getClassToWrapperMap()).thenReturn(classToWrapperMap); + when(contextLoggerConfiguration.getClassToWrapperMap()).thenReturn(classToWrapperMap); - List typeToWrapperList = new ArrayList(); - typeToWrapperList.add(new TypeToWrapper(WrappedTypeListType.class, WrappedTypeListTypeWrapper.class)); + List typeToWrapperList = new ArrayList(); + typeToWrapperList.add(new TypeToWrapper(WrappedTypeListType.class, WrappedTypeListTypeWrapper.class)); - when(contextLoggerConfiguration.getWrapperList()).thenReturn(typeToWrapperList); + when(contextLoggerConfiguration.getWrapperList()).thenReturn(typeToWrapperList); - } + } - @Test - public void apply_should_handle_implicit_context_correctly() { + @Test + public void apply_should_handle_implicit_context_correctly() { - Object result = unit.apply(contextLoggerConfiguration, ImplicitContext.COMMON); - assertThat(result, Matchers.notNullValue()); - assertThat(result.getClass(), Matchers.typeCompatibleWith(this.getClass())); + Object result = unit.apply(contextLoggerConfiguration, CoreImplicitContextProviders.COMMON); + assertThat(result, Matchers.notNullValue()); + assertThat(result.getClass(), Matchers.typeCompatibleWith(this.getClass())); - } + } - @Test - public void apply_should_handle_known_mapped_context_class_correctly() { + @Test + public void apply_should_handle_known_mapped_context_class_correctly() { - Object instance = new MappedClass(); + Object instance = new MappedClass(); - Object result = unit.apply(contextLoggerConfiguration, instance); + Object result = unit.apply(contextLoggerConfiguration, instance); - assertThat(result, Matchers.notNullValue()); - assertThat(result.getClass(), Matchers.typeCompatibleWith(MappedClassWrapper.class)); + assertThat(result, Matchers.notNullValue()); + assertThat(result.getClass(), Matchers.typeCompatibleWith(MappedClassWrapper.class)); - } + } - @Test - public void apply_should_handle_type_to_wrapper_type_correctly() { + @Test + public void apply_should_handle_type_to_wrapper_type_correctly() { - Object instance = new WrappedTypeListType(); + Object instance = new WrappedTypeListType(); - Object result = unit.apply(contextLoggerConfiguration, instance); + Object result = unit.apply(contextLoggerConfiguration, instance); - assertThat(result, Matchers.notNullValue()); - assertThat(result.getClass(), Matchers.typeCompatibleWith(WrappedTypeListTypeWrapper.class)); + assertThat(result, Matchers.notNullValue()); + assertThat(result.getClass(), Matchers.typeCompatibleWith(WrappedTypeListTypeWrapper.class)); - } + } - @Test - public void apply_should_handle_broken_known_mapped_context_class_correctly() { + @Test + public void apply_should_handle_broken_known_mapped_context_class_correctly() { - Object instance = new IncorrectMappedClass(); + Object instance = new IncorrectMappedClass(); - Object result = unit.apply(contextLoggerConfiguration, instance); + Object result = unit.apply(contextLoggerConfiguration, instance); - // should return passed instance if mapping is broken - assertThat(result, Matchers.notNullValue()); - assertThat(result, Matchers.is(instance)); + // should return passed instance if mapping is broken + assertThat(result, Matchers.notNullValue()); + assertThat(result, Matchers.is(instance)); - } + } - @Test - public void apply_should_return_passed_instance_if_no_wrapper_can_be_found() { + @Test + public void apply_should_return_passed_instance_if_no_wrapper_can_be_found() { - Object instance = new UnmappedClass(); + Object instance = new UnmappedClass(); - Object result = unit.apply(contextLoggerConfiguration, instance); - assertThat(result, Matchers.notNullValue()); - assertThat(result, Matchers.is(instance)); + Object result = unit.apply(contextLoggerConfiguration, instance); + assertThat(result, Matchers.notNullValue()); + assertThat(result, Matchers.is(instance)); - } + } - @Test - public void createInstance_should_create_instance_correctly() { - Object result = unit.createInstance(this.getClass()); + @Test + public void createInstance_should_create_instance_correctly() { + Object result = unit.createInstance(this.getClass()); - assertThat(result, Matchers.notNullValue()); - assertThat(result.getClass(), Matchers.typeCompatibleWith(this.getClass())); - } + assertThat(result, Matchers.notNullValue()); + assertThat(result.getClass(), Matchers.typeCompatibleWith(this.getClass())); + } - @Test - public void createInstance_should_create_null_for_type_with_no_noarg_constructor() { + @Test + public void createInstance_should_create_null_for_type_with_no_noarg_constructor() { - Object result = unit.createInstance(ClassWithNoNoargConstructor.class); + Object result = unit.createInstance(ClassWithNoNoargConstructor.class); - assertThat(result, Matchers.nullValue()); + assertThat(result, Matchers.nullValue()); - } + } } diff --git a/core/src/test/java/io/tracee/contextlogger/outputgenerator/predicates/IsImplicitContextEnumValuePredicateTest.java b/core/src/test/java/io/tracee/contextlogger/outputgenerator/predicates/IsImplicitContextEnumValuePredicateTest.java index c2c97ac..aca1f1f 100644 --- a/core/src/test/java/io/tracee/contextlogger/outputgenerator/predicates/IsImplicitContextEnumValuePredicateTest.java +++ b/core/src/test/java/io/tracee/contextlogger/outputgenerator/predicates/IsImplicitContextEnumValuePredicateTest.java @@ -1,30 +1,29 @@ package io.tracee.contextlogger.outputgenerator.predicates; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; - /** * Unit test for {@link io.tracee.contextlogger.outputgenerator.predicates.IsImplicitContextEnumValuePredicate}. */ public class IsImplicitContextEnumValuePredicateTest { - @Test - public void should_detect_implicit_context_enum_value_correctly() { - MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(ImplicitContext.COMMON), Matchers.is(true)); - MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(ImplicitContext.TRACEE), Matchers.is(true)); - } + @Test + public void should_detect_implicit_context_enum_value_correctly() { + MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(CoreImplicitContextProviders.COMMON), Matchers.is(true)); + MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(CoreImplicitContextProviders.TRACEE), Matchers.is(true)); + } - @Test - public void should_detect_non_implicit_context_enum_value_correctly() { - MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(this), Matchers.is(false)); - } + @Test + public void should_detect_non_implicit_context_enum_value_correctly() { + MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(this), Matchers.is(false)); + } - @Test - public void should_handle_null_value_correctly() { - MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(null), Matchers.is(false)); - } + @Test + public void should_handle_null_value_correctly() { + MatcherAssert.assertThat(IsImplicitContextEnumValuePredicate.getInstance().apply(null), Matchers.is(false)); + } } diff --git a/integration-test/general/src/main/java/io/tracee/contextlogger/integrationtest/DemoHttpServer.java b/integration-test/general/src/main/java/io/tracee/contextlogger/integrationtest/DemoHttpServer.java index 1ecddf2..fe020f1 100644 --- a/integration-test/general/src/main/java/io/tracee/contextlogger/integrationtest/DemoHttpServer.java +++ b/integration-test/general/src/main/java/io/tracee/contextlogger/integrationtest/DemoHttpServer.java @@ -10,7 +10,7 @@ import com.sun.net.httpserver.HttpServer; /** - * Created by TGI on 20.03.2015. + * Demo service endpoint for receiving message of HttpConnector. */ public class DemoHttpServer { diff --git a/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/ImplicitContextIntegrationTest.java b/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/ImplicitContextIntegrationTest.java index d5c4718..605c8a2 100644 --- a/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/ImplicitContextIntegrationTest.java +++ b/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/ImplicitContextIntegrationTest.java @@ -3,6 +3,8 @@ import io.tracee.contextlogger.TraceeContextLogger; import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.contextprovider.api.Profile; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; +import io.tracee.contextlogger.integrationtest.testcontextprovider.TestImplicitContextProviders; import io.tracee.contextlogger.outputgenerator.writer.BasicOutputWriterConfiguration; import io.tracee.contextlogger.util.test.RegexMatcher; import org.hamcrest.MatcherAssert; @@ -25,7 +27,7 @@ public void init() { @Test public void shouldOutputImplicitContextCorrectly() { String result = TraceeContextLogger.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE) - .enforceProfile(Profile.BASIC).apply().toString(ImplicitContext.COMMON, ImplicitContext.TRACEE); + .enforceProfile(Profile.BASIC).apply().toString(CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE); MatcherAssert.assertThat(result, RegexMatcher.matches("\\[\"TYPE:Object\\[]\",\\{\"TYPE\":\"common\".*")); } @@ -35,7 +37,7 @@ public void shouldOutputSingleEmptyImplicitContextCorrectly() { String result = TraceeContextLogger.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE) - .enforceProfile(Profile.BASIC).apply().toString(ImplicitContext.TRACEE); + .enforceProfile(Profile.BASIC).apply().toString(CoreImplicitContextProviders.TRACEE); MatcherAssert.assertThat(result, Matchers.is("{\"TYPE\":\"tracee\",\"DYNAMIC\":null}")); } @@ -54,4 +56,13 @@ public void should_write_this_instance_without_tostring_overwrite_correctly() { MatcherAssert.assertThat(result, Matchers.is("\"ImplicitContextIntegrationTest[]\"")); } + @Test + public void should_write_test_and_core_implicit_context_providers() { + + String result = TraceeContextLogger.create().enforceProfile(Profile.FULL).apply().toString(CoreImplicitContextProviders.TRACEE, TestImplicitContextProviders.TEST); + + MatcherAssert.assertThat(result, Matchers.containsString("tracee")); + MatcherAssert.assertThat(result, Matchers.containsString("testImplicitContextData")); + } + } diff --git a/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/MdcContextLoggerTest.java b/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/MdcContextLoggerTest.java index 38697ab..303aea3 100644 --- a/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/MdcContextLoggerTest.java +++ b/integration-test/general/src/test/java/io/tracee/contextlogger/integrationtest/MdcContextLoggerTest.java @@ -1,64 +1,63 @@ package io.tracee.contextlogger.integrationtest; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.junit.Test; -import org.slf4j.MDC; - import io.tracee.contextlogger.TraceeToStringBuilder; -import io.tracee.contextlogger.contextprovider.api.ImplicitContext; +import io.tracee.contextlogger.contextprovider.core.CoreImplicitContextProviders; import io.tracee.contextlogger.contextprovider.core.utility.NameValuePair; import io.tracee.contextlogger.outputgenerator.writer.BasicOutputWriterConfiguration; +import org.junit.Test; +import org.slf4j.MDC; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; /** * */ public class MdcContextLoggerTest { - private MdcContextLoggerTest cycle1 = this; - private MdcContextLoggerTest cycle2 = this; - private String field = "FIELD"; - private List list = new ArrayList(); + private MdcContextLoggerTest cycle1 = this; + private MdcContextLoggerTest cycle2 = this; + private String field = "FIELD"; + private List list = new ArrayList(); - public List getList() { - return list; - } + public List getList() { + return list; + } - public String getField() { - return field; - } + public String getField() { + return field; + } - { - Collections.addAll(list, "ABC", "DEF"); + { + Collections.addAll(list, "ABC", "DEF"); - } + } - public MdcContextLoggerTest getCycle1() { - return cycle1; - } + public MdcContextLoggerTest getCycle1() { + return cycle1; + } - public MdcContextLoggerTest getCycle2() { - return cycle2; - } + public MdcContextLoggerTest getCycle2() { + return cycle2; + } - @Test - public void test_should_use_mdc_logger_correctly() { + @Test + public void test_should_use_mdc_logger_correctly() { - MDC.put("KEY", "VALUE"); + MDC.put("KEY", "VALUE"); - NameValuePair nvPair = new NameValuePair("NVPAIR", "VALUE"); + NameValuePair nvPair = new NameValuePair("NVPAIR", "VALUE"); - TraceeToStringBuilder.createDefault().toString("ABC", "DEF"); - // TraceeToStringBuilder.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INTENDED).enforceProfile(Profile.FULL).disableTypes(TypeX.class).apply().toString("ABC", - // "DEF"); + TraceeToStringBuilder.createDefault().toString("ABC", "DEF"); + // TraceeToStringBuilder.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INTENDED).enforceProfile(Profile.FULL).disableTypes(TypeX.class).apply().toString("ABC", + // "DEF"); - System.out.println(TraceeToStringBuilder.create().enforceOrder().apply() - .toString("ABC", new NullPointerException(), ImplicitContext.TRACEE, this, ImplicitContext.COMMON)); + System.out.println(TraceeToStringBuilder.create().enforceOrder().apply() + .toString("ABC", new NullPointerException(), CoreImplicitContextProviders.TRACEE, this, CoreImplicitContextProviders.COMMON)); - System.out.println(TraceeToStringBuilder.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE) - .disableTypes(MdcContextLoggerTest.class).apply().wrap("ABC", this, ImplicitContext.COMMON, ImplicitContext.TRACEE)); + System.out.println(TraceeToStringBuilder.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE) + .disableTypes(MdcContextLoggerTest.class).apply().wrap("ABC", this, CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE)); - } + } } diff --git a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenContextProviderThatThrowsNullPointerException.java b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenContextProviderThatThrowsNullPointerException.java index 4138085..b79bb91 100644 --- a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenContextProviderThatThrowsNullPointerException.java +++ b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenContextProviderThatThrowsNullPointerException.java @@ -1,6 +1,6 @@ package io.tracee.contextlogger.integrationtest.testcontextprovider; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -9,7 +9,7 @@ * Broken context data wrapper that throws a NullPointerException at deserialization. */ @TraceeContextProvider(displayName = "brokenCustomContextDataWrapper", order = 50) -@ProfileSettings(basic = true, enhanced = true) +@ProfileConfig(basic = true, enhanced = true) public class BrokenContextProviderThatThrowsNullPointerException implements WrappedContextData { public static final String PROPERTY_NAME = "io.tracee.contextlogger.integrationtest.BrokenCustomContextDataWrapper.output"; @@ -41,7 +41,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "testoutput", order = 10) - @ProfileSettings(basic = false, enhanced = true) + @ProfileConfig(basic = false, enhanced = true) public String getOutput() { throw new NullPointerException("Whoops!!!"); } diff --git a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenImplicitContextProviderThatThrowsNullPointerException.java b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenImplicitContextProviderThatThrowsNullPointerException.java index 8af33db..7853059 100644 --- a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenImplicitContextProviderThatThrowsNullPointerException.java +++ b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/BrokenImplicitContextProviderThatThrowsNullPointerException.java @@ -2,7 +2,7 @@ import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.contextprovider.api.ImplicitContextData; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; @@ -10,21 +10,21 @@ * Test provider that provides implicit context information that triggers an exception during deserialization. */ @TraceeContextProvider(displayName = "testBrokenImplicitContextData", order = 200) -@ProfileSettings(basic = true, enhanced = true) +@ProfileConfig(basic = true, enhanced = true) public class BrokenImplicitContextProviderThatThrowsNullPointerException implements ImplicitContextData { public static final String PROPERTY_NAME = "io.tracee.contextlogger.integrationtest.TestBrokenImplicitContextDataProvider.output"; @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "output", order = 10) - @ProfileSettings(basic = true, enhanced = true) + @ProfileConfig(basic = true, enhanced = true) public final String getOutput() { throw new NullPointerException("Whoops!!!"); } @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "workingOutput", order = 20) - @ProfileSettings(basic = true, enhanced = true) + @ProfileConfig(basic = true, enhanced = true) public final String getWorkingOutput() { return "IT_WORKS"; } diff --git a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestContextDataWrapper.java b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestContextDataWrapper.java index b39d40a..14c2113 100644 --- a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestContextDataWrapper.java +++ b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestContextDataWrapper.java @@ -1,6 +1,6 @@ package io.tracee.contextlogger.integrationtest.testcontextprovider; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; import io.tracee.contextlogger.contextprovider.api.WrappedContextData; @@ -9,7 +9,7 @@ * Test wrapper class that wraps type {@link io.tracee.contextlogger.integrationtest.WrappedTestContextData}. */ @TraceeContextProvider(displayName = "testdata", order = 50) -@ProfileSettings(basic = true, enhanced = true, full = true) +@ProfileConfig(basic = true, enhanced = true, full = true) public class TestContextDataWrapper implements WrappedContextData { public static final String PROPERTY_NAME = "io.tracee.contextlogger.integrationtest.testcontextprovider.TestContextDataWrapper.output"; @@ -41,7 +41,7 @@ public Class getWrappedType() { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "testoutput", order = 10) - @ProfileSettings(basic = false, enhanced = true, full = true) + @ProfileConfig(basic = false, enhanced = true, full = true) public String getOutput() { return contextData != null ? contextData.getOutput() : null; } diff --git a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProvider.java b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProvider.java index e54767f..67e4114 100644 --- a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProvider.java +++ b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProvider.java @@ -3,7 +3,7 @@ import io.tracee.contextlogger.contextprovider.api.ImplicitContext; import io.tracee.contextlogger.contextprovider.api.ImplicitContextData; -import io.tracee.contextlogger.contextprovider.api.ProfileSettings; +import io.tracee.contextlogger.contextprovider.api.ProfileConfig; import io.tracee.contextlogger.contextprovider.api.TraceeContextProvider; import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderMethod; @@ -13,7 +13,7 @@ @TraceeContextProvider(displayName = "testImplicitContextData", order = 200) -@ProfileSettings(basic = false, enhanced = true) +@ProfileConfig(basic = false, enhanced = true) public class TestImplicitContextProvider implements ImplicitContextData { @@ -22,14 +22,14 @@ public class TestImplicitContextProvider implements ImplicitContextData { @SuppressWarnings("unused") @TraceeContextProviderMethod(displayName = "output", order = 10) - @ProfileSettings(basic = false, enhanced = true) + @ProfileConfig(basic = false, enhanced = true) public final String getOutput() { return OUTPUT; } @Override public ImplicitContext getImplicitContext() { - return null; + return TestImplicitContextProviders.TEST; } } diff --git a/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProviders.java b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProviders.java new file mode 100644 index 0000000..56548a3 --- /dev/null +++ b/integration-test/testcontextprovider/src/main/java/io/tracee/contextlogger/integrationtest/testcontextprovider/TestImplicitContextProviders.java @@ -0,0 +1,19 @@ +package io.tracee.contextlogger.integrationtest.testcontextprovider; + +import io.tracee.contextlogger.contextprovider.api.ImplicitContext; + +/** + * Enum for adding implicit test contextproviders. + */ +public enum TestImplicitContextProviders implements ImplicitContext { + + BROKEN(BrokenImplicitContextProviderThatThrowsNullPointerException.class), + TEST(TestImplicitContextProvider.class); + + private final Class implicitContextProviderClass; + + private TestImplicitContextProviders(final Class implicitContextProviderClass) { + this.implicitContextProviderClass = implicitContextProviderClass; + } + +}