-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/eluder/coveralls/maven/plugin/source/UniqueSourceCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/org/eluder/coveralls/maven/plugin/source/UniqueSourceCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |