summaryrefslogtreecommitdiffstats
path: root/plugins/sonar-cpd-plugin/src
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-02-18 06:05:06 +0400
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-02-18 07:38:30 +0400
commit7dc3bc3d572799e245710a9be7cfaed07f76addf (patch)
tree9cfee808b10df5e420d57705f845494014c2f272 /plugins/sonar-cpd-plugin/src
parentc357c2734c89a11f5bcb100c9952c5ad13ff6088 (diff)
downloadsonarqube-7dc3bc3d572799e245710a9be7cfaed07f76addf.tar.gz
sonarqube-7dc3bc3d572799e245710a9be7cfaed07f76addf.zip
SONAR-3238 Deprecate old format for DUPLICATIONS_DATA
Diffstat (limited to 'plugins/sonar-cpd-plugin/src')
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java21
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java57
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/decorators/package-info.java25
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/package-info.java25
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/package-info.java25
-rw-r--r--plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java247
6 files changed, 267 insertions, 133 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java
index 1d3b74d4dc3..2b67db65921 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdAnalyser.java
@@ -19,13 +19,7 @@
*/
package org.sonar.plugins.cpd;
-import java.io.File;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
import net.sourceforge.pmd.cpd.TokenEntry;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.CpdMapping;
@@ -34,6 +28,11 @@ import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.duplications.cpd.Match;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
public class CpdAnalyser {
private static final Logger LOG = LoggerFactory.getLogger(CpdAnalyser.class);
@@ -78,20 +77,22 @@ public class CpdAnalyser {
continue;
}
- firstFileData.cumulate(secondFile, secondLine, firstLine, match.getLineCount());
+ String resourceKey = SonarEngine.getFullKey(project, secondFile);
+ firstFileData.cumulate(resourceKey, secondLine, firstLine, match.getLineCount());
}
}
}
- for (DuplicationsData data : duplicationsData.values()) {
- data.save();
+ for (Map.Entry<Resource, DuplicationsData> entry : duplicationsData.entrySet()) {
+ entry.getValue().save(context, entry.getKey());
}
}
private DuplicationsData getDuplicationsData(Map<Resource, DuplicationsData> fileContainer, Resource file) {
DuplicationsData data = fileContainer.get(file);
if (data == null) {
- data = new DuplicationsData(file, context);
+ String resourceKey = SonarEngine.getFullKey(project, file);
+ data = new DuplicationsData(resourceKey, context);
fileContainer.put(file, data);
}
return data;
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java
index 32247cee88e..754b9678568 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/DuplicationsData.java
@@ -19,48 +19,42 @@
*/
package org.sonar.plugins.cpd;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Resource;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+
public class DuplicationsData {
- private Resource resource;
- private Set<Integer> duplicatedLines = new HashSet<Integer>();
- private double duplicatedBlocks;
- private List<XmlEntry> duplicationXMLEntries = new ArrayList<XmlEntry>();
+ private final String resourceKey;
+ private final Set<Integer> duplicatedLines = Sets.newHashSet();
+ private final List<XmlEntry> duplicationXMLEntries = Lists.newArrayList();
- private SensorContext context;
+ private double duplicatedBlocks;
- public DuplicationsData(Resource resource, SensorContext context) {
- this.resource = resource;
- this.context = context;
+ public DuplicationsData(String resourceKey, SensorContext context) {
+ this.resourceKey = resourceKey;
}
- public void cumulate(String targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) {
- duplicationXMLEntries.add(new XmlEntry(targetResource, targetDuplicationStartLine, duplicationStartLine, duplicatedLines));
+ public void cumulate(String targetResourceKey, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) {
+ duplicationXMLEntries.add(new XmlEntry(targetResourceKey, targetDuplicationStartLine, duplicationStartLine, duplicatedLines));
for (int duplicatedLine = duplicationStartLine; duplicatedLine < duplicationStartLine + duplicatedLines; duplicatedLine++) {
this.duplicatedLines.add(duplicatedLine);
}
}
- public void cumulate(Resource targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) {
- cumulate(context.saveResource(targetResource), targetDuplicationStartLine, duplicationStartLine, duplicatedLines);
- }
-
public void incrementDuplicatedBlock() {
duplicatedBlocks++;
}
- public void save() {
+ public void save(SensorContext context, Resource resource) {
context.saveMeasure(resource, CoreMetrics.DUPLICATED_FILES, 1d);
context.saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, (double) duplicatedLines.size());
context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, duplicatedBlocks);
@@ -86,11 +80,11 @@ public class DuplicationsData {
}
};
- private static final class XmlEntry {
- private String target;
- private int targetStartLine;
- private int startLine;
- private int lines;
+ private final class XmlEntry {
+ private final String target;
+ private final int targetStartLine;
+ private final int startLine;
+ private final int lines;
private XmlEntry(String target, int targetStartLine, int startLine, int lines) {
this.target = target;
@@ -101,10 +95,11 @@ public class DuplicationsData {
@Override
public String toString() {
- return new StringBuilder().append("<duplication lines=\"").append(lines)
- .append("\" start=\"").append(startLine)
- .append("\" target-start=\"").append(targetStartLine)
- .append("\" target-resource=\"").append(target).append("\"/>")
+ return new StringBuilder()
+ .append("<g>")
+ .append("<b s=\"").append(startLine).append("\" l=\"").append(lines).append("\" r=\"").append(resourceKey).append("\" />")
+ .append("<b s=\"").append(targetStartLine).append("\" l=\"").append(lines).append("\" r=\"").append(target).append("\" />")
+ .append("</g>")
.toString();
}
}
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/decorators/package-info.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/decorators/package-info.java
new file mode 100644
index 00000000000..cf8081c7f81
--- /dev/null
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/decorators/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.plugins.cpd.decorators;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/package-info.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/package-info.java
new file mode 100644
index 00000000000..d5b116c13a4
--- /dev/null
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.plugins.cpd.index;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/package-info.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/package-info.java
new file mode 100644
index 00000000000..7e779ad6353
--- /dev/null
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.plugins.cpd;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java
index c17b7797565..ada79ec7400 100644
--- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java
+++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdAnalyserTest.java
@@ -19,34 +19,30 @@
*/
package org.sonar.plugins.cpd;
-import static org.mockito.Matchers.anyList;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.atLeastOnce;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.LinkedHashSet;
-import java.util.Set;
-
import net.sourceforge.pmd.cpd.TokenEntry;
-
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.Resource;
-import org.sonar.api.test.IsMeasure;
import org.sonar.duplications.cpd.Match;
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
public class CpdAnalyserTest {
@Test
@@ -65,8 +61,6 @@ public class CpdAnalyserTest {
Resource resource2 = new JavaFile("foo.Bar");
when(cpdMapping.createResource((File) anyObject(), anyList())).thenReturn(resource1).thenReturn(resource2).thenReturn(resource2)
.thenReturn(resource1);
- when(context.saveResource(resource1)).thenReturn("key1");
- when(context.saveResource(resource2)).thenReturn("key2");
Match match1 = new Match(5, new TokenEntry(null, file1.getAbsolutePath(), 5), new TokenEntry(null, file2.getAbsolutePath(), 15));
match1.setLineCount(200);
@@ -74,24 +68,28 @@ public class CpdAnalyserTest {
CpdAnalyser cpdAnalyser = new CpdAnalyser(project, context, cpdMapping);
cpdAnalyser.analyse(Arrays.asList(match1).iterator());
+ ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class);
+
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_BLOCKS, 1d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_LINES, 200d);
- verify(context).saveMeasure(
- eq(resource1),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"200\" start=\"5\" target-start=\"15\" target-resource=\"key2\"/>" + "</duplications>")));
+ verify(context).saveMeasure(eq(resource1), measureCaptor.capture());
+ Measure measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications><g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "</g></duplications>"));
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_BLOCKS, 1d);
- verify(context).saveMeasure(
- eq(resource2),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA,
- "<duplications><duplication lines=\"200\" start=\"15\" target-start=\"5\" target-resource=\"key1\"/></duplications>")));
-
- verify(context, atLeastOnce()).saveResource(resource1);
- verify(context, atLeastOnce()).saveResource(resource2);
+ verify(context).saveMeasure(eq(resource2), measureCaptor.capture());
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications><g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "</g></duplications>"));
}
@Test
@@ -112,9 +110,6 @@ public class CpdAnalyserTest {
Resource resource3 = new JavaFile("foo.Hotel");
when(cpdMapping.createResource((File) anyObject(), anyList())).thenReturn(resource1).thenReturn(resource2).thenReturn(resource2)
.thenReturn(resource1).thenReturn(resource1).thenReturn(resource3).thenReturn(resource3).thenReturn(resource1);
- when(context.saveResource(resource1)).thenReturn("key1");
- when(context.saveResource(resource2)).thenReturn("key2");
- when(context.saveResource(resource3)).thenReturn("key3");
Match match1 = new Match(5, new TokenEntry(null, file1.getAbsolutePath(), 5), new TokenEntry(null, file2.getAbsolutePath(), 15));
match1.setLineCount(200);
@@ -124,35 +119,53 @@ public class CpdAnalyserTest {
CpdAnalyser cpdAnalyser = new CpdAnalyser(project, context, cpdMapping);
cpdAnalyser.analyse(Arrays.asList(match1, match2).iterator());
+ ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class);
+
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_BLOCKS, 2d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_LINES, 200d);
- verify(context).saveMeasure(
- eq(resource1),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"100\" start=\"5\" target-start=\"15\" target-resource=\"key3\"/>"
- + "<duplication lines=\"200\" start=\"5\" target-start=\"15\" target-resource=\"key2\"/>"
- + "</duplications>")));
+ verify(context).saveMeasure(eq(resource1), measureCaptor.capture());
+
+ Measure measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"5\" l=\"100\" r=\"key:foo.Foo\" />"
+ + "<b s=\"15\" l=\"100\" r=\"key:foo.Hotel\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "</g>"
+ + "</duplications>"));
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_BLOCKS, 1d);
- verify(context).saveMeasure(
- eq(resource2),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA,
- "<duplications><duplication lines=\"200\" start=\"15\" target-start=\"5\" target-resource=\"key1\"/></duplications>")));
+ verify(context).saveMeasure(eq(resource2), measureCaptor.capture());
+
+ measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "</duplications>"));
verify(context).saveMeasure(resource3, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource3, CoreMetrics.DUPLICATED_LINES, 100d);
verify(context).saveMeasure(resource3, CoreMetrics.DUPLICATED_BLOCKS, 1d);
- verify(context).saveMeasure(
- eq(resource3),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA,
- "<duplications><duplication lines=\"100\" start=\"15\" target-start=\"5\" target-resource=\"key1\"/></duplications>")));
-
- verify(context, atLeastOnce()).saveResource(resource1);
- verify(context, atLeastOnce()).saveResource(resource2);
- verify(context, atLeastOnce()).saveResource(resource3);
+ verify(context).saveMeasure(eq(resource3), measureCaptor.capture());
+
+ measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"15\" l=\"100\" r=\"key:foo.Hotel\" />"
+ + "<b s=\"5\" l=\"100\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "</duplications>"));
}
@Test
@@ -177,10 +190,6 @@ public class CpdAnalyserTest {
.thenReturn(resource4).thenReturn(resource2).thenReturn(resource1).thenReturn(resource3).thenReturn(resource4)
.thenReturn(resource3).thenReturn(resource1).thenReturn(resource2).thenReturn(resource4).thenReturn(resource4)
.thenReturn(resource1).thenReturn(resource2).thenReturn(resource3);
- when(context.saveResource(resource1)).thenReturn("key1");
- when(context.saveResource(resource2)).thenReturn("key2");
- when(context.saveResource(resource3)).thenReturn("key3");
- when(context.saveResource(resource4)).thenReturn("key4");
Match match = new Match(5, createTokenEntry(file1.getAbsolutePath(), 5), createTokenEntry(file2.getAbsolutePath(), 15));
match.setLineCount(200);
@@ -194,50 +203,95 @@ public class CpdAnalyserTest {
CpdAnalyser cpdAnalyser = new CpdAnalyser(project, context, cpdMapping);
cpdAnalyser.analyse(Arrays.asList(match).iterator());
+ ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class);
+
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_BLOCKS, 1d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_LINES, 200d);
- verify(context).saveMeasure(
- eq(resource1),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"200\" start=\"5\" target-start=\"15\" target-resource=\"key2\"/>"
- + "<duplication lines=\"200\" start=\"5\" target-start=\"7\" target-resource=\"key3\"/>"
- + "<duplication lines=\"200\" start=\"5\" target-start=\"10\" target-resource=\"key4\"/>" + "</duplications>")));
+ verify(context).saveMeasure(eq(resource1), measureCaptor.capture());
+
+ Measure measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"7\" l=\"200\" r=\"key:foo.Hotel\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"10\" l=\"200\" r=\"key:foo.Coffee\" />"
+ + "</g>"
+ + "</duplications>"));
verify(context).saveMeasure(resource3, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource3, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(resource3, CoreMetrics.DUPLICATED_BLOCKS, 1d);
- verify(context).saveMeasure(
- eq(resource2),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"200\" start=\"15\" target-start=\"5\" target-resource=\"key1\"/>"
- + "<duplication lines=\"200\" start=\"15\" target-start=\"7\" target-resource=\"key3\"/>"
- + "<duplication lines=\"200\" start=\"15\" target-start=\"10\" target-resource=\"key4\"/>" + "</duplications>")));
+ verify(context).saveMeasure(eq(resource3), measureCaptor.capture());
+
+ measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"7\" l=\"200\" r=\"key:foo.Hotel\" />"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"7\" l=\"200\" r=\"key:foo.Hotel\" />"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"7\" l=\"200\" r=\"key:foo.Hotel\" />"
+ + "<b s=\"10\" l=\"200\" r=\"key:foo.Coffee\" />"
+ + "</g>"
+ + "</duplications>"));
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(resource2, CoreMetrics.DUPLICATED_BLOCKS, 1d);
- verify(context).saveMeasure(
- eq(resource3),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"200\" start=\"7\" target-start=\"5\" target-resource=\"key1\"/>"
- + "<duplication lines=\"200\" start=\"7\" target-start=\"15\" target-resource=\"key2\"/>"
- + "<duplication lines=\"200\" start=\"7\" target-start=\"10\" target-resource=\"key4\"/>" + "</duplications>")));
+ verify(context).saveMeasure(eq(resource2), measureCaptor.capture());
+
+ measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "<b s=\"7\" l=\"200\" r=\"key:foo.Hotel\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "<b s=\"10\" l=\"200\" r=\"key:foo.Coffee\" />"
+ + "</g>"
+ + "</duplications>"));
verify(context).saveMeasure(resource4, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(resource4, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource4, CoreMetrics.DUPLICATED_BLOCKS, 1d);
- verify(context).saveMeasure(
- eq(resource4),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"200\" start=\"10\" target-start=\"5\" target-resource=\"key1\"/>"
- + "<duplication lines=\"200\" start=\"10\" target-start=\"15\" target-resource=\"key2\"/>"
- + "<duplication lines=\"200\" start=\"10\" target-start=\"7\" target-resource=\"key3\"/>" + "</duplications>")));
-
- verify(context, atLeastOnce()).saveResource(resource1);
- verify(context, atLeastOnce()).saveResource(resource2);
- verify(context, atLeastOnce()).saveResource(resource3);
- verify(context, atLeastOnce()).saveResource(resource4);
+ verify(context).saveMeasure(eq(resource4), measureCaptor.capture());
+
+ measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"10\" l=\"200\" r=\"key:foo.Coffee\" />"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"10\" l=\"200\" r=\"key:foo.Coffee\" />"
+ + "<b s=\"15\" l=\"200\" r=\"key:foo.Bar\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"10\" l=\"200\" r=\"key:foo.Coffee\" />"
+ + "<b s=\"7\" l=\"200\" r=\"key:foo.Hotel\" />"
+ + "</g>"
+ + "</duplications>"));
}
@Test
@@ -253,7 +307,6 @@ public class CpdAnalyserTest {
CpdMapping cpdMapping = mock(CpdMapping.class);
Resource resource1 = new JavaFile("foo.Foo");
when(cpdMapping.createResource((File) anyObject(), anyList())).thenReturn(resource1).thenReturn(resource1);
- when(context.saveResource(resource1)).thenReturn("key1");
Match match1 = new Match(304, new TokenEntry(null, file1.getAbsolutePath(), 5), new TokenEntry(null, file1.getAbsolutePath(), 215));
match1.setLineCount(200);
@@ -261,16 +314,26 @@ public class CpdAnalyserTest {
CpdAnalyser cpdAnalyser = new CpdAnalyser(project, context, cpdMapping);
cpdAnalyser.analyse(Arrays.asList(match1).iterator());
+ ArgumentCaptor<Measure> measureCaptor = ArgumentCaptor.forClass(Measure.class);
+
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_LINES, 400d);
verify(context).saveMeasure(resource1, CoreMetrics.DUPLICATED_BLOCKS, 2d);
- verify(context).saveMeasure(
- eq(resource1),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<duplication lines=\"200\" start=\"5\" target-start=\"215\" target-resource=\"key1\"/>"
- + "<duplication lines=\"200\" start=\"215\" target-start=\"5\" target-resource=\"key1\"/>" + "</duplications>")));
-
- verify(context, atLeastOnce()).saveResource(resource1);
+ verify(context).saveMeasure(eq(resource1), measureCaptor.capture());
+
+ Measure measure = measureCaptor.getValue();
+ assertThat(measure.getMetric(), is(CoreMetrics.DUPLICATIONS_DATA));
+ // FIXME in fact should be only one group - see SONAR-3131
+ assertThat(measure.getData(), is("<duplications>"
+ + "<g>"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"215\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"215\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "<b s=\"5\" l=\"200\" r=\"key:foo.Foo\" />"
+ + "</g>"
+ + "</duplications>"));
}
private static TokenEntry createTokenEntry(String sourceId, int line) {