aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-cpd-plugin
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-01-13 20:49:01 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2014-01-13 20:49:54 +0100
commit808e4f107fdd1659a41bb616660090fc04a9d7c0 (patch)
treee3b1c3fa12d35d90909dbe77f1c6722796b15885 /plugins/sonar-cpd-plugin
parent256c992dd006ded2b7e2a84e0a5a7dfb8be16fe7 (diff)
downloadsonarqube-808e4f107fdd1659a41bb616660090fc04a9d7c0.tar.gz
sonarqube-808e4f107fdd1659a41bb616660090fc04a9d7c0.zip
SONAR-3024 Introduce deprecated key on resources to ease transition
Diffstat (limited to 'plugins/sonar-cpd-plugin')
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java44
-rw-r--r--plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java80
2 files changed, 66 insertions, 58 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
index 48ff0147243..d92157f3e11 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
@@ -32,7 +32,11 @@ import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.PersistenceMode;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
import org.sonar.api.scan.filesystem.FileQuery;
import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.api.scan.filesystem.PathResolver;
@@ -54,12 +58,20 @@ import org.sonar.plugins.cpd.index.SonarDuplicationsIndex;
import javax.annotation.Nullable;
import java.io.File;
-import java.io.*;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import java.util.concurrent.*;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
public class SonarEngine extends CpdEngine {
@@ -91,10 +103,10 @@ public class SonarEngine extends CpdEngine {
static String getFullKey(Project project, Resource resource) {
return new StringBuilder(ResourceModel.KEY_SIZE)
- .append(project.getKey())
- .append(':')
- .append(resource.getKey())
- .toString();
+ .append(project.getKey())
+ .append(':')
+ .append(resource.getKey())
+ .toString();
}
@Override
@@ -128,7 +140,7 @@ public class SonarEngine extends CpdEngine {
reader = new InputStreamReader(new FileInputStream(file), fileSystem.sourceCharset());
statements = statementChunker.chunk(tokenChunker.chunk(reader));
} catch (FileNotFoundException e) {
- throw new SonarException("Cannot find file "+ file, e);
+ throw new SonarException("Cannot find file " + file, e);
} finally {
IOUtils.closeQuietly(reader);
}
@@ -157,9 +169,9 @@ public class SonarEngine extends CpdEngine {
clones = null;
LOG.warn("Timeout during detection of duplications for " + file, e);
} catch (InterruptedException e) {
- throw new SonarException("Fail during detection of duplication for "+ file, e);
+ throw new SonarException("Fail during detection of duplication for " + file, e);
} catch (ExecutionException e) {
- throw new SonarException("Fail during detection of duplication for "+ file, e);
+ throw new SonarException("Fail during detection of duplication for " + file, e);
}
save(context, resource, clones);
@@ -184,8 +196,8 @@ public class SonarEngine extends CpdEngine {
}
protected Resource getResource(File file) {
- String relativePath = pathResolver.relativePath(fileSystem.sourceDirs(), file).path();
- return JavaFile.fromRelativePath(relativePath, false);
+ String relativePathFromBaseDir = pathResolver.relativePath(fileSystem.baseDir(), file);
+ return JavaFile.create(relativePathFromBaseDir, "unused", false);
}
static void save(SensorContext context, Resource resource, @Nullable Iterable<CloneGroup> duplications) {
@@ -212,7 +224,7 @@ public class SonarEngine extends CpdEngine {
context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, duplicatedBlocks);
Measure data = new Measure(CoreMetrics.DUPLICATIONS_DATA, toXml(duplications))
- .setPersistenceMode(PersistenceMode.DATABASE);
+ .setPersistenceMode(PersistenceMode.DATABASE);
context.saveMeasure(resource, data);
}
@@ -223,9 +235,9 @@ public class SonarEngine extends CpdEngine {
xml.append("<g>");
for (ClonePart part : duplication.getCloneParts()) {
xml.append("<b s=\"").append(part.getStartLine())
- .append("\" l=\"").append(part.getLines())
- .append("\" r=\"").append(StringEscapeUtils.escapeXml(part.getResourceId()))
- .append("\"/>");
+ .append("\" l=\"").append(part.getLines())
+ .append("\" r=\"").append(StringEscapeUtils.escapeXml(part.getResourceId()))
+ .append("\"/>");
}
xml.append("</g>");
}
diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java
index 3b4b2a30572..c2346fe86fb 100644
--- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java
+++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java
@@ -28,7 +28,6 @@ import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Resource;
import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.scan.filesystem.PathResolver.RelativePath;
import org.sonar.api.test.IsMeasure;
import org.sonar.duplications.index.CloneGroup;
import org.sonar.duplications.index.ClonePart;
@@ -39,7 +38,6 @@ import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyCollection;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@@ -58,18 +56,16 @@ public class SonarEngineTest {
resource = new JavaFile("key1");
}
- @SuppressWarnings("unchecked")
@Test
public void testGetResource() {
PathResolver pathResolver = mock(PathResolver.class);
ModuleFileSystem fileSystem = mock(ModuleFileSystem.class);
- RelativePath relativePath = new RelativePath(null, "com/foo/Bar.java");
- when(pathResolver.relativePath(anyCollection(), any(java.io.File.class))).thenReturn(relativePath);
+ when(pathResolver.relativePath(any(java.io.File.class), any(java.io.File.class))).thenReturn("src/main/java/com/foo/Bar.java");
SonarEngine engine = new SonarEngine(null, fileSystem, pathResolver, null);
Resource resource = engine.getResource(new java.io.File(""));
- assertThat(resource.getKey()).isEqualTo("com.foo.Bar");
+ assertThat(resource.getKey()).isEqualTo("/src/main/java/com/foo/Bar.java");
assertThat(resource).isInstanceOf(JavaFile.class);
}
@@ -91,11 +87,11 @@ public class SonarEngineTest {
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, 1d);
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(
- eq(resource),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
- + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
- + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
- + "</g></duplications>")));
+ eq(resource),
+ argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
+ + "</g></duplications>")));
}
@Test
@@ -107,11 +103,11 @@ public class SonarEngineTest {
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, 400d);
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, 2d);
verify(context).saveMeasure(
- eq(resource),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
- + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
- + "<b s=\"215\" l=\"200\" r=\"key1\"/>"
- + "</g></duplications>")));
+ eq(resource),
+ argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"215\" l=\"200\" r=\"key1\"/>"
+ + "</g></duplications>")));
}
@Test
@@ -123,52 +119,52 @@ public class SonarEngineTest {
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, 1d);
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, 200d);
verify(context).saveMeasure(
- eq(resource),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
- + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
- + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
- + "<b s=\"25\" l=\"200\" r=\"key3\"/>"
- + "</g></duplications>")));
+ eq(resource),
+ argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
+ + "<b s=\"25\" l=\"200\" r=\"key3\"/>"
+ + "</g></duplications>")));
}
@Test
public void testTwoDuplicatedGroupsInvolvingThreeFiles() throws Exception {
List<CloneGroup> groups = Arrays.asList(
- newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key2", 0, 15, 214)),
- newCloneGroup(new ClonePart("key1", 0, 15, 214), new ClonePart("key3", 0, 15, 214)));
+ newCloneGroup(new ClonePart("key1", 0, 5, 204), new ClonePart("key2", 0, 15, 214)),
+ newCloneGroup(new ClonePart("key1", 0, 15, 214), new ClonePart("key3", 0, 15, 214)));
SonarEngine.save(context, resource, groups);
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_FILES, 1d);
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, 2d);
verify(context).saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, 210d);
verify(context).saveMeasure(
- eq(resource),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
- + "<g>"
- + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
- + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
- + "</g>"
- + "<g>"
- + "<b s=\"15\" l=\"200\" r=\"key1\"/>"
- + "<b s=\"15\" l=\"200\" r=\"key3\"/>"
- + "</g>"
- + "</duplications>")));
+ eq(resource),
+ argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications>"
+ + "<g>"
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
+ + "</g>"
+ + "<g>"
+ + "<b s=\"15\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"key3\"/>"
+ + "</g>"
+ + "</duplications>")));
}
@Test
public void shouldEscapeXmlEntities() {
File csharpFile = new File("Loads/File Loads/Subs & Reds/SubsRedsDelivery.cs");
List<CloneGroup> groups = Arrays.asList(newCloneGroup(
- new ClonePart("Loads/File Loads/Subs & Reds/SubsRedsDelivery.cs", 0, 5, 204),
- new ClonePart("Loads/File Loads/Subs & Reds/SubsRedsDelivery2.cs", 0, 15, 214)));
+ new ClonePart("Loads/File Loads/Subs & Reds/SubsRedsDelivery.cs", 0, 5, 204),
+ new ClonePart("Loads/File Loads/Subs & Reds/SubsRedsDelivery2.cs", 0, 15, 214)));
SonarEngine.save(context, csharpFile, groups);
verify(context).saveMeasure(
- eq(csharpFile),
- argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
- + "<b s=\"5\" l=\"200\" r=\"Loads/File Loads/Subs &amp; Reds/SubsRedsDelivery.cs\"/>"
- + "<b s=\"15\" l=\"200\" r=\"Loads/File Loads/Subs &amp; Reds/SubsRedsDelivery2.cs\"/>"
- + "</g></duplications>")));
+ eq(csharpFile),
+ argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
+ + "<b s=\"5\" l=\"200\" r=\"Loads/File Loads/Subs &amp; Reds/SubsRedsDelivery.cs\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"Loads/File Loads/Subs &amp; Reds/SubsRedsDelivery2.cs\"/>"
+ + "</g></duplications>")));
}
private CloneGroup newCloneGroup(ClonePart... parts) {