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

Commit

Permalink
Changed type output in string representation.
Browse files Browse the repository at this point in the history
"<TYPE>" is used instead of "TYPE"

Fixed tests to use constant to me more flexible for future changes.
  • Loading branch information
tobiasstamann committed Mar 10, 2017
1 parent 0346725 commit 993328e
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.tracee.contextlogger.contextprovider.core;

import io.tracee.contextlogger.contextprovider.api.TraceeContextProviderServiceProvider;
import io.tracee.contextlogger.contextprovider.core.java.CharArrayContextProvider;
import io.tracee.contextlogger.contextprovider.core.java.JavaThrowableContextProvider;
import io.tracee.contextlogger.contextprovider.core.tracee.CommonDataContextProvider;
import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMdcContextProvider;
Expand All @@ -12,19 +13,19 @@
*/
public class CoreContextProviderServiceProvider implements TraceeContextProviderServiceProvider {

public static final Class[] IMPLICIT_CONTEXT_PROVIDER = {CommonDataContextProvider.class, TraceeMdcContextProvider.class};
public static final Class[] CONTEXT_PROVIDER = {JavaThrowableContextProvider.class, NameValuePair.class,
TraceeMessageContextProvider.class};
public static final Class[] IMPLICIT_CONTEXT_PROVIDER = {CommonDataContextProvider.class, TraceeMdcContextProvider.class};
public static final Class[] CONTEXT_PROVIDER = {JavaThrowableContextProvider.class, NameValuePair.class,
TraceeMessageContextProvider.class, CharArrayContextProvider.class};

@Override
public Class[] getImplicitContextProvider() {
return IMPLICIT_CONTEXT_PROVIDER;
}
@Override
public Class[] getImplicitContextProvider() {
return IMPLICIT_CONTEXT_PROVIDER;
}

@Override
public Class[] getContextProvider() {
return CONTEXT_PROVIDER;
}
@Override
public Class[] getContextProvider() {
return CONTEXT_PROVIDER;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ else if (state.maxDepthReached()) {
if (IsCollectionTypePredicate.getInstance().apply(instanceToDeserialize)) {
// handle arrays and collections

if (instanceToDeserialize.getClass().isArray()) {
if (instanceToDeserialize.getClass().isArray() ) {
outputElement = ArrayToOutputElementTransformerFunction.getInstance().apply(this, state.next(), (Object[])instanceToDeserialize);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*/
public class TypeProviderFunction {

private static final String OUTPUT_STRING_TYPE = "TYPE";
public static final String OUTPUT_STRING_TYPE = "<TYPE>";

private static final TypeProviderFunction INSTANCE = new TypeProviderFunction();

/**
* Write the type string to the Stringbuilder
*
* @param stringBuilder the stringbuilder to write to
* @param outputStyle the output style to use
* @param outputStyle the output style to use
* @param outputElement the output element to write the type string for.
* @return true, if any output was written to the stringbuilder, otherwise false.
*/
Expand All @@ -31,13 +31,11 @@ public boolean apply(final StringBuilder stringBuilder, final OutputStyle output

if (OutputElementType.COLLECTION.equals(outputElement.getOutputElementType())) {
result = handleCollectionType(stringBuilder, outputStyle, outputElement);
}
else if (OutputElementType.COMPLEX.equals(outputElement.getOutputElementType())) {
} else if (OutputElementType.COMPLEX.equals(outputElement.getOutputElementType())) {

if (TraceeContextLogAnnotationUtilities.getAnnotationFromType(outputElement.getEncapsulatedInstance()) != null) {
result = handleTraceeContextprovider(stringBuilder, outputStyle, outputElement);
}
else {
} else {
result = handleComplexType(stringBuilder, outputStyle, outputElement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.tracee.contextlogger.contextprovider.core.java.JavaThrowableContextProvider;
import io.tracee.contextlogger.outputgenerator.writer.BasicOutputWriterConfiguration;
import io.tracee.contextlogger.outputgenerator.writer.function.TypeProviderFunction;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand All @@ -11,6 +12,8 @@
*/
public class TraceeContextLoggerTest {

private final static String TYPE_STRING = TypeProviderFunction.OUTPUT_STRING_TYPE;

@Test
public void should_detect_suppress_passed_instance_to_be_excluded() {

Expand All @@ -19,7 +22,7 @@ public void should_detect_suppress_passed_instance_to_be_excluded() {

String result = unit.toString("ABC", 1L);

MatcherAssert.assertThat(result, Matchers.equalTo("[\"TYPE:Object[]\",\"Long<1>\"]"));
MatcherAssert.assertThat(result, Matchers.equalTo("[\"" + TYPE_STRING + ":Object[]\",\"Long<1>\"]"));

}

Expand All @@ -30,12 +33,12 @@ public void should_create_string_representation() {
.enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE).apply();

String result = unit.toString("ABC", 1L);
MatcherAssert.assertThat(result, Matchers.equalTo("[\"TYPE:Object[]\",\"String<'ABC'>\",\"Long<1>\"]"));
MatcherAssert.assertThat(result, Matchers.equalTo("[\"" + TYPE_STRING + ":Object[]\",\"String<'ABC'>\",\"Long<1>\"]"));

Object[] objectsToBeProcessed = {"DEF", 2L};
unit.setObjectsToProcess(objectsToBeProcessed);
result = unit.provideOutput();
MatcherAssert.assertThat(result, Matchers.equalTo("[\"TYPE:Object[]\",\"String<'DEF'>\",\"Long<2>\"]"));
MatcherAssert.assertThat(result, Matchers.equalTo("[\"" + TYPE_STRING + ":Object[]\",\"String<'DEF'>\",\"Long<2>\"]"));

}

Expand All @@ -52,7 +55,7 @@ public void excludeContextProviders_should_exclude_contextProviders_correctly()

result = unit.toString(new NullPointerException(), "ABC");

MatcherAssert.assertThat(result, Matchers.equalTo("[\"TYPE:Object[]\",\"String<'ABC'>\"]"));
MatcherAssert.assertThat(result, Matchers.equalTo("[\"" + TYPE_STRING + ":Object[]\",\"String<'ABC'>\"]"));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.tracee.contextlogger.contextprovider;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMessage;
import org.junit.Test;

import java.util.List;
import java.util.Set;

import org.junit.Test;

import io.tracee.contextlogger.contextprovider.core.tracee.TraceeMessage;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

/**
* Test class for {@link TypeToWrapper}.
Expand All @@ -34,7 +33,8 @@ public void getAllWrappedClasses_should_get_all_wrapped_classes() {

assertThat(wrappedClassesSet, notNullValue());
assertThat(wrappedClassesSet.size(), greaterThan(0));
assertThat(wrappedClassesSet, containsInAnyOrder((Class)TraceeMessage.class, (Class)Throwable.class));
assertThat(wrappedClassesSet, containsInAnyOrder((Class) TraceeMessage.class, (Class) Throwable.class, (Class) char[].class));

}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.tracee.contextlogger.outputgenerator.writer.collection;

import java.util.List;

import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;

import io.tracee.contextlogger.outputgenerator.outputelements.AtomicOutputElement;
import io.tracee.contextlogger.outputgenerator.outputelements.CollectionOutputElement;
import io.tracee.contextlogger.outputgenerator.writer.TestOutputElementWriter;
import io.tracee.contextlogger.outputgenerator.writer.function.TypeProviderFunction;
import io.tracee.contextlogger.outputgenerator.writer.styles.json.SimpleJsonOutputStyle;
import io.tracee.contextlogger.util.test.RegexMatcher;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
* Unit test for {@link io.tracee.contextlogger.outputgenerator.writer.collection.SimpleCollectionOutputElementWriter}.
Expand Down Expand Up @@ -40,7 +39,7 @@ public void should_process_collection_correctly() {

unit.produceOutput(outputWriter, stringBuilder, new SimpleJsonOutputStyle(), givenCollectionOutputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\[\"TYPE:ArrayList\",ABC]"));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\[\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + ":ArrayList\",ABC]"));

}

Expand All @@ -56,7 +55,7 @@ public void should_process_collection_with_id_correctly() {

unit.produceOutput(outputWriter, stringBuilder, new SimpleJsonOutputStyle(), givenCollectionOutputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\[\"TYPE:ArrayList@\\d+\",ABC]"));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\[\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + ":ArrayList@\\d+\",ABC]"));

}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.tracee.contextlogger.outputgenerator.writer.complex;

import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;

import io.tracee.contextlogger.contextprovider.core.java.JavaThrowableContextProvider;
import io.tracee.contextlogger.contextprovider.core.utility.NameValuePair;
import io.tracee.contextlogger.outputgenerator.outputelements.AtomicOutputElement;
import io.tracee.contextlogger.outputgenerator.outputelements.ComplexOutputElement;
import io.tracee.contextlogger.outputgenerator.outputelements.TraceeContextProviderOutputElement;
import io.tracee.contextlogger.outputgenerator.writer.TestOutputElementWriter;
import io.tracee.contextlogger.outputgenerator.writer.function.TypeProviderFunction;
import io.tracee.contextlogger.outputgenerator.writer.styles.json.SimpleJsonOutputStyle;
import io.tracee.contextlogger.util.test.RegexMatcher;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;

/**
* Unit test for {@link io.tracee.contextlogger.outputgenerator.writer.complex.SimpleComplexOutputElementWriter}
Expand Down Expand Up @@ -49,7 +49,7 @@ public void should_create_output_for_bean_correctly() {
unit.produceOutput(outputWriter, stringBuilder, new SimpleJsonOutputStyle(), givenComplexOutputElement);

MatcherAssert.assertThat(stringBuilder.toString(),
RegexMatcher.matches("\\{\"TYPE\":\"SimplexComplexOutputElementWriterTest\",\"field1\":(ABC)\\}"));
RegexMatcher.matches("\\{\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"SimplexComplexOutputElementWriterTest\",\"field1\":(ABC)\\}"));

}

Expand All @@ -65,7 +65,7 @@ public void should_create_output_for_bean_with_id_correctly() {
unit.produceOutput(outputWriter, stringBuilder, new SimpleJsonOutputStyle(), givenComplexOutputElement);

MatcherAssert.assertThat(stringBuilder.toString(),
RegexMatcher.matches("\\{\"TYPE\":\"SimplexComplexOutputElementWriterTest@\\d+\",\"field1\":(ABC)\\}"));
RegexMatcher.matches("\\{\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"SimplexComplexOutputElementWriterTest@\\d+\",\"field1\":(ABC)\\}"));

}

Expand All @@ -80,7 +80,7 @@ public void should_create_output_for_tracee_context_provider_correctly() {

unit.produceOutput(outputWriter, stringBuilder, new SimpleJsonOutputStyle(), givenComplexOutputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\{\"TYPE\":\"throwable\",\"field1\":(ABC)\\}"));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\{\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"throwable\",\"field1\":(ABC)\\}"));

}

Expand All @@ -96,7 +96,7 @@ public void should_create_output_for_tracee_context_provider_with_id_correctly()

unit.produceOutput(outputWriter, stringBuilder, new SimpleJsonOutputStyle(), givenComplexOutputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\{\"TYPE\":\"throwable@\\d+\",\"field1\":(ABC)\\}"));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\\{\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"throwable@\\d+\",\"field1\":(ABC)\\}"));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package io.tracee.contextlogger.outputgenerator.writer.functions;

import java.util.ArrayList;
import java.util.HashMap;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

import io.tracee.contextlogger.contextprovider.core.java.JavaThrowableContextProvider;
import io.tracee.contextlogger.contextprovider.core.utility.NameValuePair;
import io.tracee.contextlogger.outputgenerator.outputelements.CollectionOutputElement;
Expand All @@ -15,6 +8,12 @@
import io.tracee.contextlogger.outputgenerator.writer.function.TypeProviderFunction;
import io.tracee.contextlogger.outputgenerator.writer.styles.json.SimpleJsonOutputStyle;
import io.tracee.contextlogger.util.test.RegexMatcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Unit test for {@link io.tracee.contextlogger.outputgenerator.writer.function.TypeProviderFunction}.
Expand All @@ -30,7 +29,7 @@ public void should_output_collection_without_id() {

boolean result = TypeProviderFunction.getInstance().apply(stringBuilder, new SimpleJsonOutputStyle(), outputElement);

MatcherAssert.assertThat(stringBuilder.toString(), Matchers.is("\"TYPE:ArrayList\""));
MatcherAssert.assertThat(stringBuilder.toString(), Matchers.is("\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + ":ArrayList\""));
MatcherAssert.assertThat(result, Matchers.is(true));
}

Expand All @@ -44,7 +43,7 @@ public void should_output_collection_with_id() {

boolean result = TypeProviderFunction.getInstance().apply(stringBuilder, new SimpleJsonOutputStyle(), outputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\"TYPE:ArrayList@\\d+\""));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + ":ArrayList@\\d+\""));
MatcherAssert.assertThat(result, Matchers.is(true));
}

Expand All @@ -57,7 +56,7 @@ public void should_output_complex_without_id() {

boolean result = TypeProviderFunction.getInstance().apply(stringBuilder, new SimpleJsonOutputStyle(), outputElement);

MatcherAssert.assertThat(stringBuilder.toString(), Matchers.is("\"TYPE\":\"HashMap\""));
MatcherAssert.assertThat(stringBuilder.toString(), Matchers.is("\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"HashMap\""));
MatcherAssert.assertThat(result, Matchers.is(true));
}

Expand All @@ -71,7 +70,7 @@ public void should_output_complex_with_id() {

boolean result = TypeProviderFunction.getInstance().apply(stringBuilder, new SimpleJsonOutputStyle(), outputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\"TYPE\":\"HashMap@\\d+\""));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"HashMap@\\d+\""));
MatcherAssert.assertThat(result, Matchers.is(true));
}

Expand All @@ -85,7 +84,7 @@ public void should_output_tracee_without_id() {

boolean result = TypeProviderFunction.getInstance().apply(stringBuilder, new SimpleJsonOutputStyle(), outputElement);

MatcherAssert.assertThat(stringBuilder.toString(), Matchers.is("\"TYPE\":\"throwable\""));
MatcherAssert.assertThat(stringBuilder.toString(), Matchers.is("\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"throwable\""));
MatcherAssert.assertThat(result, Matchers.is(true));
}

Expand All @@ -101,7 +100,7 @@ public void should_output_tracee_with_id() {

boolean result = TypeProviderFunction.getInstance().apply(stringBuilder, new SimpleJsonOutputStyle(), outputElement);

MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\"TYPE\":\"throwable@\\d+\""));
MatcherAssert.assertThat(stringBuilder.toString(), RegexMatcher.matches("\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"throwable@\\d+\""));
MatcherAssert.assertThat(result, Matchers.is(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
import io.tracee.contextlogger.integrationtest.testcontextprovider.BrokenImplicitContextProviderThatThrowsNullPointerException;
import io.tracee.contextlogger.integrationtest.testcontextprovider.WrappedBrokenTestContextData;
import io.tracee.contextlogger.outputgenerator.writer.BasicOutputWriterConfiguration;
import io.tracee.contextlogger.outputgenerator.writer.function.TypeProviderFunction;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

public class BrokenExternalWrapperIntegrationTest {

@Test
public void should_ignore_properties_for_wrapper_that_throw_an_exception() {
@Test
public void should_ignore_properties_for_wrapper_that_throw_an_exception() {

// should not break because of the thrown NPE - Exception is handled internally ==> getter with exception should be ignored
// instances with no valid output field will be ignored, so this test has no instances to render and returns "null"
String result = TraceeContextLogger.create().enforceProfile(Profile.ENHANCED).apply().toString(new WrappedBrokenTestContextData());
MatcherAssert.assertThat(result, Matchers.is("null"));
// should not break because of the thrown NPE - Exception is handled internally ==> getter with exception should be ignored
// instances with no valid output field will be ignored, so this test has no instances to render and returns "null"
String result = TraceeContextLogger.create().enforceProfile(Profile.ENHANCED).apply().toString(new WrappedBrokenTestContextData());
MatcherAssert.assertThat(result, Matchers.is("null"));

}
}

@Test
public void should_ignore_properties_for_custom_implicit_context_data_provider_that_throw_an_exception() {
@Test
public void should_ignore_properties_for_custom_implicit_context_data_provider_that_throw_an_exception() {

// should not break because of the thrown NPE, Exception is handled internally ==> getter with exception should be ignored
String result = TraceeContextLogger.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE)
.enforceProfile(Profile.ENHANCED).apply().toString(new BrokenImplicitContextProviderThatThrowsNullPointerException());
MatcherAssert.assertThat(result, Matchers.is("{\"TYPE\":\"testBrokenImplicitContextData\",\"workingOutput\":\"String<'IT_WORKS'>\"}"));
// should not break because of the thrown NPE, Exception is handled internally ==> getter with exception should be ignored
String result = TraceeContextLogger.create().enforceOutputWriterConfiguration(BasicOutputWriterConfiguration.JSON_INLINE)
.enforceProfile(Profile.ENHANCED).apply().toString(new BrokenImplicitContextProviderThatThrowsNullPointerException());
MatcherAssert.assertThat(result, Matchers.is("{\"" + TypeProviderFunction.OUTPUT_STRING_TYPE + "\":\"testBrokenImplicitContextData\",\"workingOutput\":\"String<'IT_WORKS'>\"}"));

}
}

}
Loading

0 comments on commit 993328e

Please sign in to comment.