*/
package org.sonar.xoo.coverage;
+import java.io.File;
+import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
-import java.io.File;
-import java.io.IOException;
-
import static org.assertj.core.api.Assertions.assertThat;
public class ItCoverageSensorTest {
sensor.execute(context);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(3);
+ assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3);
}
@Test
sensor.execute(context);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(3);
- assertThat(context.conditions("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(4);
- assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(2);
+ assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3);
+ assertThat(context.conditions("foo:src/foo.xoo", 1)).isEqualTo(4);
+ assertThat(context.coveredConditions("foo:src/foo.xoo", 1)).isEqualTo(2);
}
}
*/
package org.sonar.xoo.coverage;
+import java.io.File;
+import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
-import java.io.File;
-import java.io.IOException;
-
import static org.assertj.core.api.Assertions.assertThat;
public class OverallCoverageSensorTest {
sensor.execute(context);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(3);
+ assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3);
}
@Test
sensor.execute(context);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(3);
- assertThat(context.conditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(4);
- assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(2);
+ assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3);
+ assertThat(context.conditions("foo:src/foo.xoo", 1)).isEqualTo(4);
+ assertThat(context.coveredConditions("foo:src/foo.xoo", 1)).isEqualTo(2);
}
}
*/
package org.sonar.xoo.coverage;
+import java.io.File;
+import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
-import java.io.File;
-import java.io.IOException;
-
import static org.assertj.core.api.Assertions.assertThat;
public class UtCoverageSensorTest {
sensor.execute(context);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(3);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.IT, 1)).isNull();
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isNull();
+ assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3);
}
@Test
sensor.execute(context);
- assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(3);
- assertThat(context.conditions("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(4);
- assertThat(context.conditions("foo:src/foo.xoo", CoverageType.IT, 1)).isNull();
- assertThat(context.conditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isNull();
- assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(2);
- assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.IT, 1)).isNull();
- assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isNull();
+ assertThat(context.lineHits("foo:src/foo.xoo", 1)).isEqualTo(3);
+ assertThat(context.conditions("foo:src/foo.xoo", 1)).isEqualTo(4);
+ assertThat(context.coveredConditions("foo:src/foo.xoo", 1)).isEqualTo(2);
}
}
*/
package org.sonar.api.batch.sensor.internal;
+import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.Multimap;
import com.google.common.collect.Table;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
-import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage;
import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens;
import org.sonar.api.batch.sensor.error.AnalysisError;
Map<String, DefaultHighlighting> highlightingByComponent = new HashMap<>();
Map<String, DefaultCpdTokens> cpdTokensByComponent = new HashMap<>();
- Table<String, CoverageType, DefaultCoverage> coverageByComponentAndType = HashBasedTable.create();
+ Multimap<String, DefaultCoverage> coverageByComponent = ArrayListMultimap.create();
Map<String, DefaultSymbolTable> symbolsPerComponent = new HashMap<>();
Map<String, String> contextProperties = new HashMap<>();
@Override
public void store(DefaultCoverage defaultCoverage) {
String fileKey = defaultCoverage.inputFile().key();
- // Emulate duplicate storage check
- if (coverageByComponentAndType.contains(fileKey, defaultCoverage.type())) {
- throw new UnsupportedOperationException("Trying to save coverage twice for the same file is not supported: " + defaultCoverage.inputFile().relativePath());
- }
- coverageByComponentAndType.row(fileKey).put(defaultCoverage.type(), defaultCoverage);
+ coverageByComponent.put(fileKey, defaultCoverage);
}
@Override
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
+import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
-import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage;
import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
-import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
+import org.sonar.api.config.Settings;
import org.sonar.api.internal.ApiVersion;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.measures.Metric;
}
@CheckForNull
- public Integer lineHits(String fileKey, CoverageType type, int line) {
- DefaultCoverage defaultCoverage = sensorStorage.coverageByComponentAndType.get(fileKey, type);
- if (defaultCoverage == null) {
- return null;
- }
- return defaultCoverage.hitsByLine().get(line);
+ public Integer lineHits(String fileKey, int line) {
+ return sensorStorage.coverageByComponent.get(fileKey).stream()
+ .map(c -> c.hitsByLine().get(line))
+ .flatMap(Stream::of)
+ .filter(Objects::nonNull)
+ .reduce(null, SensorContextTester::sumOrNull);
}
@CheckForNull
- public Integer conditions(String fileKey, CoverageType type, int line) {
- DefaultCoverage defaultCoverage = sensorStorage.coverageByComponentAndType.get(fileKey, type);
- if (defaultCoverage == null) {
- return null;
- }
- return defaultCoverage.conditionsByLine().get(line);
+ public static Integer sumOrNull(Integer o1, Integer o2) {
+ return o1 == null ? o2 : (o1 + o2);
}
@CheckForNull
- public Integer coveredConditions(String fileKey, CoverageType type, int line) {
- DefaultCoverage defaultCoverage = sensorStorage.coverageByComponentAndType.get(fileKey, type);
- if (defaultCoverage == null) {
- return null;
- }
- return defaultCoverage.coveredConditionsByLine().get(line);
+ public Integer conditions(String fileKey, int line) {
+ return sensorStorage.coverageByComponent.get(fileKey).stream()
+ .map(c -> c.conditionsByLine().get(line))
+ .flatMap(Stream::of)
+ .filter(Objects::nonNull)
+ .reduce(null, SensorContextTester::maxOrNull);
+ }
+
+ @CheckForNull
+ public Integer coveredConditions(String fileKey, int line) {
+ return sensorStorage.coverageByComponent.get(fileKey).stream()
+ .map(c -> c.coveredConditionsByLine().get(line))
+ .flatMap(Stream::of)
+ .filter(Objects::nonNull)
+ .reduce(null, SensorContextTester::maxOrNull);
+ }
+
+ @CheckForNull
+ public static Integer maxOrNull(Integer o1, Integer o2) {
+ return o1 == null ? o2 : Math.max(o1, o2);
}
@CheckForNull
import org.sonar.api.batch.fs.internal.FileMetadata;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
-import org.sonar.api.batch.sensor.coverage.CoverageType;
-import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.batch.sensor.error.AnalysisError;
import org.sonar.api.batch.sensor.error.NewAnalysisError;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
-import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.SonarException;
@Test
public void testCoverageAtLineZero() {
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 1)).isNull();
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 4)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
exception.expect(IllegalStateException.class);
- NewCoverage coverage = tester.newCoverage()
+ tester.newCoverage()
.onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
- .ofType(CoverageType.UNIT)
.lineHits(0, 3);
}
@Test
public void testCoverageAtLineOutOfRange() {
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 1)).isNull();
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 4)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
exception.expect(IllegalStateException.class);
- NewCoverage coverage = tester.newCoverage()
+ tester.newCoverage()
.onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar"))))
- .ofType(CoverageType.UNIT)
.lineHits(4, 3);
}
@Test
public void testLineHits() {
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 1)).isNull();
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 4)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
tester.newCoverage()
.onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasdas"))))
- .ofType(CoverageType.UNIT)
.lineHits(1, 2)
.lineHits(2, 3)
.save();
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 1)).isEqualTo(2);
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.IT, 1)).isNull();
- assertThat(tester.lineHits("foo:src/Foo.java", CoverageType.UNIT, 2)).isEqualTo(3);
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isEqualTo(2);
+ assertThat(tester.lineHits("foo:src/Foo.java", 2)).isEqualTo(3);
}
- @Test(expected = UnsupportedOperationException.class)
- public void duplicateCoverage() {
+ public void multipleCoverage() {
tester.newCoverage()
.onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasdas"))))
- .ofType(CoverageType.UNIT)
.lineHits(1, 2)
+ .conditions(3, 4, 2)
.save();
tester.newCoverage()
.onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasdas"))))
- .ofType(CoverageType.UNIT)
.lineHits(1, 2)
+ .conditions(3, 4, 3)
.save();
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isEqualTo(4);
+ assertThat(tester.conditions("foo:src/Foo.java", 3)).isEqualTo(4);
+ assertThat(tester.coveredConditions("foo:src/Foo.java", 3)).isEqualTo(3);
}
@Test
public void testConditions() {
- assertThat(tester.conditions("foo:src/Foo.java", CoverageType.UNIT, 1)).isNull();
- assertThat(tester.coveredConditions("foo:src/Foo.java", CoverageType.UNIT, 1)).isNull();
+ assertThat(tester.conditions("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.coveredConditions("foo:src/Foo.java", 1)).isNull();
tester.newCoverage()
.onFile(new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("annot dsf fds foo bar\nasd\nasdas\nasdfas"))))
- .ofType(CoverageType.UNIT)
.conditions(1, 4, 2)
.save();
- assertThat(tester.conditions("foo:src/Foo.java", CoverageType.UNIT, 1)).isEqualTo(4);
- assertThat(tester.coveredConditions("foo:src/Foo.java", CoverageType.UNIT, 1)).isEqualTo(2);
-
- assertThat(tester.conditions("foo:src/Foo.java", CoverageType.IT, 1)).isNull();
- assertThat(tester.coveredConditions("foo:src/Foo.java", CoverageType.IT, 1)).isNull();
+ assertThat(tester.conditions("foo:src/Foo.java", 1)).isEqualTo(4);
+ assertThat(tester.coveredConditions("foo:src/Foo.java", 1)).isEqualTo(2);
}
@Test