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 #29 from tobiasgindler/master
Browse files Browse the repository at this point in the history
[#22] Added unit tests to increase test coverage.
  • Loading branch information
tobiasgindler committed May 18, 2015
2 parents 3b7f2cb + 6e6e64f commit 0e53dec
Show file tree
Hide file tree
Showing 12 changed files with 581 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
public abstract class AbstractToStringBuilder<T extends ToStringBuilder> implements ContextLoggerBuilderAccessable {

protected final ContextLoggerConfiguration contextLoggerConfiguration;
protected TraceeContextStringRepresentationBuilder traceeContextLogBuilder;
private final ContextLoggerConfiguration contextLoggerConfiguration;
private TraceeContextStringRepresentationBuilder traceeContextLogBuilder;

protected Object[] objectsToProcess;
private Object[] objectsToProcess;

protected AbstractToStringBuilder(ContextLoggerConfiguration contextLoggerConfiguration) {
this.contextLoggerConfiguration = contextLoggerConfiguration;
Expand Down Expand Up @@ -49,8 +49,20 @@ public void setStringRepresentationBuilder(TraceeContextStringRepresentationBuil
this.traceeContextLogBuilder = stringRepresentationBuilder;
}

protected TraceeContextStringRepresentationBuilder getStringRepresentationBuilder() {
return this.traceeContextLogBuilder;
}

@Override
public ContextLoggerConfiguration getContextLoggerConfiguration() {
return this.contextLoggerConfiguration;
}

protected Object[] getObjectsToProcess() {
return objectsToProcess;
}

protected void setObjectsToProcess(final Object[] objectsToProcess) {
this.objectsToProcess = objectsToProcess;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public final class TraceeContextLogger extends AbstractToStringBuilder<ContextLo
private ConnectorFactory connectorsWrapper;

private String prefix;
private Class[] excludedTypes;

private TraceeContextLogger(ContextLoggerConfiguration contextLoggerConfiguration) {
super(contextLoggerConfiguration);
Expand All @@ -33,7 +32,6 @@ private TraceeContextLogger(TraceeContextLogger instanceToClone) {
this.connectorsWrapper = instanceToClone.connectorsWrapper;

this.prefix = instanceToClone.prefix;
this.excludedTypes = instanceToClone.excludedTypes;

}

Expand Down Expand Up @@ -64,7 +62,7 @@ public void log(Object... instancesToLog) {
public void logWithPrefixedMessage(String prefixedMessage, Object... instancesToLog) {

this.prefix = prefixedMessage;
this.objectsToProcess = instancesToLog;
this.setObjectsToProcess(instancesToLog);

this.connectorsWrapper.sendErrorReportToConnectors(this);
}
Expand All @@ -73,8 +71,8 @@ public void logWithPrefixedMessage(String prefixedMessage, Object... instancesTo
public ConnectorOutputProvider excludeContextProviders(final Class... contextProvidersToInclude) {

TraceeContextLogger traceeContextLogger = new TraceeContextLogger(this);
traceeContextLogger.excludedTypes = contextProvidersToInclude;
TraceeContextStringRepresentationBuilder traceeContextStringRepresentationBuilder = traceeContextLogBuilder.cloneStringRepresentationBuilder();
TraceeContextStringRepresentationBuilder traceeContextStringRepresentationBuilder = this.getStringRepresentationBuilder()
.cloneStringRepresentationBuilder();

for (Class type : contextProvidersToInclude) {

Expand All @@ -91,28 +89,12 @@ public ConnectorOutputProvider excludeContextProviders(final Class... contextPro
@Override
public String provideOutput() {

return traceeContextLogBuilder.createStringRepresentation(objectsToProcess);
return this.getStringRepresentationBuilder().createStringRepresentation(this.getObjectsToProcess());
}

@Override
public String getPrefix() {
return prefix;
}

boolean isInstanceTypeExcluded(Object contextProviderType) {

if (excludedTypes != null && contextProviderType != null) {

for (Class type : excludedTypes) {

if (type.isAssignableFrom(contextProviderType.getClass())) {
return true;
}

}

}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.tracee.contextlogger.api;

/**
* Class for providing output at runtime vi toString.
* Class for providing output at runtime via toString.
*/
public class WrappedContextLoggerOutput {

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

import io.tracee.Tracee;
import io.tracee.contextlogger.TraceeContextLoggerConstants;
import io.tracee.contextlogger.contextprovider.api.CustomImplicitContextData;
import io.tracee.contextlogger.api.ImplicitContextData;
import io.tracee.contextlogger.contextprovider.api.WrappedContextData;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import io.tracee.Tracee;
import io.tracee.contextlogger.TraceeContextLoggerConstants;
import io.tracee.contextlogger.api.ImplicitContextData;
import io.tracee.contextlogger.contextprovider.api.CustomImplicitContextData;
import io.tracee.contextlogger.contextprovider.api.WrappedContextData;

/**
* Class to store class to wrapper mapppings.
Expand Down Expand Up @@ -104,10 +109,10 @@ public static Set<CustomImplicitContextData> getCustomImplicitDataProviders() {
/**
* Generic function to get a implicit data provider classes from resource files.
*
* @param type the type of implicit context data provider to look for
* @param type the type of implicit context data provider to look for
* @param resourceUrl the resource file url process
* @param <T> The generic type of implicit data provider either {@link io.tracee.contextlogger.contextprovider.api.CustomImplicitContextData} or
* {@link io.tracee.contextlogger.api.ImplicitContextData}
* @param <T> The generic type of implicit data provider either {@link io.tracee.contextlogger.contextprovider.api.CustomImplicitContextData} or
* {@link io.tracee.contextlogger.api.ImplicitContextData}
* @return a set that contains all context provider type the were found
*/
static <T> Set<T> getImplicitWrappers(final Class<T> type, final String resourceUrl) {
Expand All @@ -133,19 +138,21 @@ static <T> Set<T> getImplicitWrappers(final Class<T> type, final String resource
T instance = type.cast(clazz.newInstance());
result.add(instance);
}
} catch (Exception e) {
// to be ignored
} catch (NoClassDefFoundError error) {
}
catch (Throwable e) {
// to be ignored
}
}
} catch (Exception e) {
}
catch (Exception e) {
logError("Context logger - An error occurred while loading implicit type wrappers.", e);
} finally {
}
finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ignored) {
}
catch (IOException ignored) {
// ignore
}
}
Expand Down Expand Up @@ -194,25 +201,27 @@ static List<TypeToWrapper> getAvailableWrappers(final String resourceUrl) {

if (WrappedContextData.class.isAssignableFrom(clazz)) {
// try to create instance to get the wrapped type
final WrappedContextData instance = (WrappedContextData) clazz.newInstance();
final WrappedContextData instance = (WrappedContextData)clazz.newInstance();
result.add(new TypeToWrapper(instance.getWrappedType(), clazz));
}

} catch (Exception e) {
// to be ignored
} catch (NoClassDefFoundError error) {
}
catch (Throwable e) {
// to be ignored
}

}
} catch (Exception e) {
}
catch (Throwable e) {
logError("Context logger - An error occurred while loading explicit type wrappers.", e);

} finally {
}
finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e1) {
}
catch (IOException e1) {
// ignore
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void cloneTo(AbstractContextStringRepresentationBuilder cloneTo) {

cloneTo.setWrapperClasses(new HashSet<Class>(wrapperClasses));
cloneTo.setProfile(profile);
cloneTo.setManualContextOverrides(manualContextOverrides);
cloneTo.setManualContextOverrides(new HashMap<String, Boolean>(manualContextOverrides));
cloneTo.setEnforceOrder(keepOrder);
cloneTo.setOutputWriterConfiguration(outputWriterConfiguration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Object apply(ContextLoggerConfiguration contextLoggerConfiguration, Objec
* @param type the type of the new instance
* @return a new instance of the passed type or null if an exception occurred during the creation of the instance of if the passed type is null.
*/
private Object createInstance(final Class type) {
protected Object createInstance(final Class type) {
if (type != null) {
try {
return type.newInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package io.tracee.contextlogger;

import java.util.Map;
import java.util.Set;

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

import io.tracee.contextlogger.api.TraceeContextStringRepresentationBuilder;
import io.tracee.contextlogger.impl.ContextLoggerConfiguration;
import io.tracee.contextlogger.outputgenerator.writer.OutputWriterConfiguration;
import io.tracee.contextlogger.profile.Profile;

/**
* Unit test for {@link AbstractToStringBuilder}.
*/
public class AbstractToStringBuilderTest {

public static class TestToStringBuilder extends AbstractToStringBuilder<TestToStringBuilder> {

protected TestToStringBuilder() {
super((ContextLoggerConfiguration)null);
}

protected TestToStringBuilder(final AbstractToStringBuilder instanceToClone) {
super(instanceToClone);
}

@Override
public void log(final Object... instancesToLog) {

}

@Override
public void logWithPrefixedMessage(final String prefixedMessage, final Object... instancesToLog) {

}
}

public TraceeContextStringRepresentationBuilder traceeContextLogBuilder = new TraceeContextStringRepresentationBuilder() {

@Override
public Set<Class> getWrapperClasses() {
return null;
}

@Override
public void setWrapperClasses(final Set<Class> wrapperClasses) {

}

@Override
public boolean getEnforceOrder() {
return false;
}

@Override
public void setEnforceOrder(final boolean keepOrder) {

}

@Override
public String createStringRepresentation(final Object... instancesToLog) {
return null;
}

@Override
public void setOutputWriterConfiguration(final OutputWriterConfiguration outputWriterConfiguration) {

}

@Override
public void setManualContextOverrides(final Map<String, Boolean> manualContextOverrides) {

}

@Override
public Map<String, Boolean> getManualContextOverrides() {
return null;
}

@Override
public TraceeContextStringRepresentationBuilder cloneStringRepresentationBuilder() {
return null;
}

@Override
public void setProfile(final Profile profile) {

}

@Override
public Profile getProfile() {
return null;
}
};

@Test
public void should_use_constructor_correctly() {

TestToStringBuilder source = new TestToStringBuilder();
source.setStringRepresentationBuilder(traceeContextLogBuilder);

final Object[] objArray = { "ABC", "DEF" };
source.setObjectsToProcess(objArray);

TestToStringBuilder unit = new TestToStringBuilder(source);

MatcherAssert.assertThat(unit.getStringRepresentationBuilder(), Matchers.equalTo(source.getStringRepresentationBuilder()));
MatcherAssert.assertThat(unit.getObjectsToProcess(), Matchers.equalTo(objArray));
MatcherAssert.assertThat(unit.getContextLoggerConfiguration(), Matchers.nullValue());

}

@Test
public void toString_should_be_forwarded_to_tracee_context_log_builder_correctly() {

TestToStringBuilder unit = new TestToStringBuilder();

TraceeContextStringRepresentationBuilder spy = Mockito.spy(traceeContextLogBuilder);

unit.setStringRepresentationBuilder(spy);

unit.toString("ABC", "DEF");
Mockito.verify(spy).createStringRepresentation("ABC", "DEF");

unit.create("DEF", "GHI");
Mockito.verify(spy).createStringRepresentation("DEF", "GHI");
}
}
Loading

0 comments on commit 0e53dec

Please sign in to comment.