Skip to content

Commit

Permalink
#52: Only report unique sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
trautonen committed Oct 12, 2014
1 parent 72c26c5 commit 135d1c9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.eluder.coveralls.maven.plugin.service.Travis;
import org.eluder.coveralls.maven.plugin.source.SourceCallback;
import org.eluder.coveralls.maven.plugin.source.SourceLoader;
import org.eluder.coveralls.maven.plugin.source.UniqueSourceCallback;
import org.eluder.coveralls.maven.plugin.util.CoverageParsersFactory;
import org.eluder.coveralls.maven.plugin.util.SourceLoaderFactory;

Expand Down Expand Up @@ -310,6 +311,7 @@ protected SourceCallback createSourceCallbackChain(final JsonWriter writer, fina
chain = coverageTracingReporter;
reporters.add(coverageTracingReporter);
}
chain = new UniqueSourceCallback(chain);
return chain;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.eluder.coveralls.maven.plugin.source;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.eluder.coveralls.maven.plugin.ProcessingException;
import org.eluder.coveralls.maven.plugin.domain.Source;

/**
* Source callback that tracks passed by source files and provides only unique
* source files to the delegate. Note that the implementation is not thread
* safe so the {@link #onSource(org.eluder.coveralls.maven.plugin.domain.Source)}
* can be called only from single thread concurrently.
*/
public class UniqueSourceCallback implements SourceCallback {

private static final String LINES_SEPARATOR = "#";

private final Set<String> cache = new HashSet<String>();
private final SourceCallback delegate;

public UniqueSourceCallback(final SourceCallback delegate) {
this.delegate = delegate;
}

@Override
public void onSource(final Source source) throws ProcessingException, IOException {
String key = getKey(source);
if (!cache.contains(key)) {
cache.add(key);
delegate.onSource(source);
}
}

private String getKey(final Source source) {
return source.getFullName() + LINES_SEPARATOR + getRelevantLines(source);
}

private int getRelevantLines(final Source source) {
int relevant = 0;
for (Integer cov : source.getCoverage()) {
if (cov != null) {
relevant++;
}
}
return relevant;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.eluder.coveralls.maven.plugin.source;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.eluder.coveralls.maven.plugin.domain.Source;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class UniqueSourceCallbackTest {

@Mock
private SourceCallback sourceCallbackMock;

@Test
public void testOnSourceWithUniqueSources() throws Exception {
Source s1 = createSource("Foo.java", "{\n void();\n}\n", 2);
Source s2 = createSource("Bar.java", "{\n bar();\n}\n", 2);

UniqueSourceCallback cb = createUniqueSourceCallback();
cb.onSource(s1);
cb.onSource(s2);
verify(sourceCallbackMock, times(2)).onSource(Mockito.any(Source.class));
}

@Test
public void testOnSourceWithDuplicateSources() throws Exception {
Source s1 = createSource("Foo.java", "{\n void();\n}\n", 2);

UniqueSourceCallback cb = createUniqueSourceCallback();
cb.onSource(s1);
cb.onSource(s1);
verify(sourceCallbackMock, times(1)).onSource(Mockito.any(Source.class));
}

@Test
public void testOnSourceWithUniqueRelevantLines() throws Exception {
Source s1 = createSource("Foo.java", "{\n void();\n}\n", 2);
Source s2 = createSource("Foo.java", "{\n void();\n}\n", 1, 3);

UniqueSourceCallback cb = createUniqueSourceCallback();
cb.onSource(s1);
cb.onSource(s2);
verify(sourceCallbackMock, times(2)).onSource(Mockito.any(Source.class));
}

private UniqueSourceCallback createUniqueSourceCallback() {
return new UniqueSourceCallback(sourceCallbackMock);
}

private Source createSource(final String name, final String source, final int... relevant) {
Source s = new Source(name, source);
for (int i : relevant) {
s.addCoverage(i, 1);
}
return s;
}
}

0 comments on commit 135d1c9

Please sign in to comment.