Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #47 from tobiasgindler/master
Browse files Browse the repository at this point in the history
[#46, #22] Refactorings
  • Loading branch information
tobiasgindler committed Jul 8, 2015
2 parents 0c2442e + f864e64 commit 0d45baa
Show file tree
Hide file tree
Showing 47 changed files with 1,246 additions and 1,131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,19 +21,13 @@
*/
public abstract class TraceeContextLoggerAbstractProcessor extends AbstractProcessor {

private static Map<String, DeclaredType> cachedParentTypes = new HashMap<String, DeclaredType>();
private static Map<String, FileObjectWrapper> traceeProfileProperties = new HashMap<String, FileObjectWrapper>();

protected Messager messager;
protected Types typeUtils;
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;
Expand Down Expand Up @@ -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);

Expand All @@ -95,5 +86,8 @@ protected static synchronized FileObjectWrapper getOrcreateProfileProperties(fin

}

protected static void clearCache() {
traceeProfileProperties.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean process(final Set<? extends TypeElement> 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();
Expand Down Expand Up @@ -76,7 +76,7 @@ public Set<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public boolean process(final Set<? extends TypeElement> 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();
Expand Down Expand Up @@ -76,7 +76,7 @@ public Set<String> 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");

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element> set = new HashSet<Element>();
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element> set = new HashSet<Element>();
set.add(typeElement);
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
set.add(typeElement);
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
set.add(typeElement);
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
set.add(typeElement);
Expand Down Expand Up @@ -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<Element> set = new HashSet<Element>();
set.add(typeElement);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 0d45baa

Please sign in to comment.