Skip to content

Commit

Permalink
Merge 4231f5c into 0a4b0e6
Browse files Browse the repository at this point in the history
  • Loading branch information
andrioli committed Mar 23, 2017
2 parents 0a4b0e6 + 4231f5c commit 86316ad
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 7 deletions.
65 changes: 65 additions & 0 deletions src/main/java/org/eluder/coveralls/maven/plugin/domain/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.eluder.coveralls.maven.plugin.domain;

/*
* #[license]
* coveralls-maven-plugin
* %%
* Copyright (C) 2013 - 2016 Tapio Rautonen
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* %[license]
*/

public class Branch {

private final int lineNumber;

private final int blockNumber;

private final int branchNumber;

private final int hits;

public Branch(final int lineNumber,
final int blockNumber,
final int branchNumber,
final int hits) {
this.lineNumber = lineNumber;
this.blockNumber = blockNumber;
this.branchNumber = branchNumber;
this.hits = hits;
}

public int getLineNumber() {
return lineNumber;
}

public int getBlockNumber() {
return blockNumber;
}

public int getBranchNumber() {
return branchNumber;
}

public int getHits() {
return hits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -41,6 +45,7 @@ public final class Source implements JsonObject {
private final String name;
private final String digest;
private final Integer[] coverage;
private final List<Branch> branches;
private String classifier;

public Source(final String name, final String source, final String digest) {
Expand All @@ -52,6 +57,7 @@ protected Source(final String name, final int lines, final String digest, final
this.digest = digest;
this.coverage = new Integer[lines];
this.classifier = classifier;
this.branches = new ArrayList<>();
}

@JsonIgnore
Expand All @@ -77,6 +83,22 @@ public Integer[] getCoverage() {
return coverage;
}

@JsonProperty("branches")
public Integer[] getBranches() {
final List<Integer> branchesRaw = new ArrayList<>(branches.size() * 4);
for (final Branch b : branches) {
branchesRaw.add(b.getLineNumber());
branchesRaw.add(b.getBlockNumber());
branchesRaw.add(b.getBranchNumber());
branchesRaw.add(b.getHits());
}
return branchesRaw.toArray(new Integer[branchesRaw.size()]);
}

public List<Branch> getBranchesList() {
return Collections.unmodifiableList(branches);
}

@JsonIgnore
public String getClassifier() {
return classifier;
Expand All @@ -86,24 +108,65 @@ public void setClassifier(final String classifier) {
this.classifier = classifier;
}

public void addCoverage(final int lineNumber, final Integer coverage) {
private void checkLineRange(final int lineNumber) {
int index = lineNumber - 1;
if (index >= this.coverage.length) {
throw new IllegalArgumentException("Line number " + lineNumber + " is greater than the source file " + name + " size");
}
}

public void addCoverage(final int lineNumber, final Integer coverage) {
checkLineRange(lineNumber);
this.coverage[lineNumber - 1] = coverage;
}

public void addBranchCoverage(final int lineNumber,
final int blockNumber,
final int branchNumber,
final int hits) {
addBranchCoverage(false, lineNumber, blockNumber, branchNumber, hits);
}

private void addBranchCoverage(final boolean merge,
final int lineNumber,
final int blockNumber,
final int branchNumber,
final int hits) {
checkLineRange(lineNumber);
int hitSum = hits;
final ListIterator<Branch> it = this.branches.listIterator();
while (it.hasNext()) {
final Branch b = it.next();
if (b.getLineNumber() == lineNumber &&
b.getBlockNumber() == blockNumber &&
b.getBranchNumber() == branchNumber) {
it.remove();
if (merge) {
hitSum += b.getHits();
}
}
}
this.branches.add(new Branch(lineNumber, blockNumber, branchNumber, hitSum));
}

public Source merge(final Source source) {
Source copy = new Source(this.name, this.coverage.length, this.digest, this.classifier);
System.arraycopy(this.coverage, 0, copy.coverage, 0, this.coverage.length);
copy.branches.addAll(this.branches);
if (copy.equals(source)) {
for (int i = 0; i < copy.coverage.length; i++) {
if (source.coverage[i] != null) {
int base = copy.coverage[i] != null ? copy.coverage[i] : 0;
copy.coverage[i] = base + source.coverage[i];
}
}
for (final Branch b : source.branches) {
copy.addBranchCoverage(true,
b.getLineNumber(),
b.getBlockNumber(),
b.getBranchNumber(),
b.getHits());
}
}
return copy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.apache.maven.plugin.logging.Log;
import org.eluder.coveralls.maven.plugin.ProcessingException;
import org.eluder.coveralls.maven.plugin.domain.Branch;
import org.eluder.coveralls.maven.plugin.domain.Source;
import org.eluder.coveralls.maven.plugin.source.ChainingSourceCallback;
import org.eluder.coveralls.maven.plugin.source.SourceCallback;
Expand All @@ -40,6 +41,8 @@ public class CoverageTracingLogger extends ChainingSourceCallback implements Log
private long lines = 0;
private long relevant = 0;
private long covered = 0;
private long branches = 0;
private long coveredBranches = 0;

public CoverageTracingLogger(final SourceCallback chained) {
super(chained);
Expand All @@ -65,6 +68,18 @@ public final long getMissed() {
return relevant - covered;
}

public final long getBranches() {
return branches;
}

public final long getCoveredBranches() {
return coveredBranches;
}

public final long getMissedBranches() {
return branches - coveredBranches;
}

@Override
public Position getPosition() {
return Position.AFTER;
Expand All @@ -76,6 +91,9 @@ public void log(final Log log) {
log.info("- " + getRelevant() + " relevant lines");
log.info("- " + getCovered() + " covered lines");
log.info("- " + getMissed() + " missed lines");
log.info("- " + getBranches() + " branches");
log.info("- " + getCoveredBranches() + " covered branches");
log.info("- " + getMissedBranches() + " missed branches");
}

@Override
Expand All @@ -90,5 +108,12 @@ protected void onSourceInternal(final Source source) throws ProcessingException,
}
}
}

this.branches += source.getBranchesList().size();
for (final Branch b : source.getBranchesList()) {
if (b.getHits() > 0) {
coveredBranches++;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,22 @@ protected void onEvent(final XMLStreamReader xml, final SourceCallback callback)

if (isStartElement(xml, "line") && this.source != null) {
int ci = Integer.parseInt(xml.getAttributeValue(null, "ci"));
this.source.addCoverage(
Integer.parseInt(xml.getAttributeValue(null, "nr")),
(ci == 0 ? 0 : 1) // jacoco does not count hits
);
int cb = Integer.parseInt(xml.getAttributeValue(null, "cb"));
int mb = Integer.parseInt(xml.getAttributeValue(null, "mb"));
int nr = Integer.parseInt(xml.getAttributeValue(null, "nr"));

// jacoco does not count hits. this is why hits is always 0 or 1
this.source.addCoverage(nr, (ci == 0 ? 0 : 1));

// add branches. unfortunately, there is NO block number and
// branch number will NOT be unique between coverage changes.
int branchId = 0;
for (int b = 0; b < cb; b++) {
this.source.addBranchCoverage(nr, 0, branchId++, 1);
}
for (int b = 0; b < mb; b++) {
this.source.addBranchCoverage(nr, 0, branchId++, 0);
}
} else

if (isEndElement(xml, "sourcefile") && this.source != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,43 @@ public void testAddCoverage() {
assertArrayEquals(new Integer[] { 3, null, 3, null }, source.getCoverage());
}

@Test
public void testAddBranchCoverage() {
Source source = new Source("src/main/java/Hello.java", "public class Hello {\n if(true) {\n }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
source.addBranchCoverage(2, 0, 0, 2);
source.addBranchCoverage(2, 0, 1, 3);
assertArrayEquals(new Integer[] { 2, 0, 0, 2, 2, 0, 1, 3 }, source.getBranches());
}

@Test
public void testAddSameBranchReplaceExistingOne() {
Source source = new Source("src/main/java/Hello.java", "public class Hello {\n if(true) {\n }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
source.addBranchCoverage(2, 0, 0, 2);
source.addBranchCoverage(2, 0, 0, 3);
assertArrayEquals(new Integer[] { 2, 0, 0, 3 }, source.getBranches());
}

@Test
public void testAddSameBranchDoNotKeepOrdering() {
Source source = new Source("src/main/java/Hello.java", "public class Hello {\n if(true) {\n }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
source.addBranchCoverage(2, 0, 0, 0);
source.addBranchCoverage(2, 0, 1, 0);
source.addBranchCoverage(2, 0, 0, 1);
assertArrayEquals(new Integer[] { 2, 0, 1, 0, 2, 0, 0, 1 }, source.getBranches());
}

@Test(expected = IllegalArgumentException.class)
public void testAddCoverageForSourceOutOfBounds() {
Source source = new Source("src/main/java/Hello.java", "public class Hello {\n \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
source.addCoverage(5, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testAddBranchCoverageForSourceOutOfBounds() {
Source source = new Source("src/main/java/Hello.java", "public class Hello {\n if(true) {\n }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
source.addBranchCoverage(6, 0, 0, 2);
}

@Test
@Ignore("#45: https://github.com/trautonen/coveralls-maven-plugin/issues/45")
public void testGetNameWithClassifier() throws Exception {
Expand All @@ -58,12 +89,15 @@ public void testGetNameWithClassifier() throws Exception {

@Test
public void testMerge() {
Source source1 = new Source("src/main/java/Hello.java", "public class Hello {\n \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
Source source1 = new Source("src/main/java/Hello.java", "public class Hello {\n if(true) {\n }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
source1.addCoverage(1, 2);
source1.addCoverage(3, 4);
Source source2 = new Source("src/main/java/Hello.java", "public class Hello {\n \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
source1.addBranchCoverage(2, 0, 0, 1);
Source source2 = new Source("src/main/java/Hello.java", "public class Hello {\n if(true) {\n }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
source2.addCoverage(2, 1);
source2.addCoverage(3, 3);
source2.addBranchCoverage(2, 0, 0, 1);
source2.addBranchCoverage(2, 0, 1, 3);

Source merged = source1.merge(source2);
assertFalse(source1 == merged);
Expand All @@ -75,6 +109,14 @@ public void testMerge() {
assertEquals(new Integer(1), merged.getCoverage()[1]);
assertEquals(new Integer(7), merged.getCoverage()[2]);
assertNull(merged.getCoverage()[3]);
assertEquals(new Integer(2), merged.getBranches()[0]);
assertEquals(new Integer(0), merged.getBranches()[1]);
assertEquals(new Integer(0), merged.getBranches()[2]);
assertEquals(new Integer(2), merged.getBranches()[3]);
assertEquals(new Integer(2), merged.getBranches()[4]);
assertEquals(new Integer(0), merged.getBranches()[5]);
assertEquals(new Integer(1), merged.getBranches()[6]);
assertEquals(new Integer(3), merged.getBranches()[7]);
}

@Test
Expand Down

0 comments on commit 86316ad

Please sign in to comment.