summaryrefslogtreecommitdiffstats
path: root/plugins/sonar-cpd-plugin
diff options
context:
space:
mode:
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>2013-02-14 12:45:06 +0100
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>2013-02-15 13:52:12 +0100
commitc42500c22f3ed38716e8ef1c240be3d42f9ae08e (patch)
tree604285e01e792ac61049ac4f39766d28901de0d8 /plugins/sonar-cpd-plugin
parentd2a1cded379c7d1955d9343ea3d671329b487665 (diff)
downloadsonarqube-c42500c22f3ed38716e8ef1c240be3d42f9ae08e.tar.gz
sonarqube-c42500c22f3ed38716e8ef1c240be3d42f9ae08e.zip
SONAR-3108 Replace deprecated calls
Diffstat (limited to 'plugins/sonar-cpd-plugin')
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java61
-rw-r--r--plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java2
-rw-r--r--plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java78
3 files changed, 93 insertions, 48 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 7187746e5ad..a00c906e514 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
@@ -29,7 +29,13 @@ 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.ModuleFileSystem;
+import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.SonarException;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.BlockChunker;
@@ -47,6 +53,7 @@ import org.sonar.plugins.cpd.index.SonarDuplicationsIndex;
import javax.annotation.Nullable;
+import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
@@ -55,7 +62,12 @@ 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 {
@@ -70,8 +82,14 @@ public class SonarEngine extends CpdEngine {
private final IndexFactory indexFactory;
- public SonarEngine(IndexFactory indexFactory) {
+ private final ModuleFileSystem fileSystem;
+
+ private final PathResolver pathResolver;
+
+ public SonarEngine(IndexFactory indexFactory, ModuleFileSystem moduleFileSystem, PathResolver pathResolver) {
this.indexFactory = indexFactory;
+ this.fileSystem = moduleFileSystem;
+ this.pathResolver = pathResolver;
}
@Override
@@ -79,7 +97,7 @@ public class SonarEngine extends CpdEngine {
return Java.INSTANCE.equals(language);
}
- static String getFullKey(Project project, Resource resource) {
+ static String getFullKey(Project project, Resource<?> resource) {
return new StringBuilder(ResourceModel.KEY_SIZE)
.append(project.getKey())
.append(':')
@@ -89,31 +107,31 @@ public class SonarEngine extends CpdEngine {
@Override
public void analyse(Project project, SensorContext context) {
- List<InputFile> inputFiles = project.getFileSystem().mainFiles(project.getLanguageKey());
- if (inputFiles.isEmpty()) {
+ List<File> sourceFiles = fileSystem.sourceFilesOfLang(project.getLanguageKey());
+ if (sourceFiles.isEmpty()) {
return;
}
- SonarDuplicationsIndex index = createIndex(project, inputFiles);
- detect(index, context, project, inputFiles);
+ SonarDuplicationsIndex index = createIndex(project, sourceFiles);
+ detect(index, context, project, sourceFiles);
}
- private SonarDuplicationsIndex createIndex(Project project, List<InputFile> inputFiles) {
+ private SonarDuplicationsIndex createIndex(Project project, List<File> sourceFiles) {
final SonarDuplicationsIndex index = indexFactory.create(project);
TokenChunker tokenChunker = JavaTokenProducer.build();
StatementChunker statementChunker = JavaStatementBuilder.build();
BlockChunker blockChunker = new BlockChunker(BLOCK_SIZE);
- for (InputFile inputFile : inputFiles) {
- LOG.debug("Populating index from {}", inputFile.getFile());
- Resource resource = getResource(inputFile);
+ for (File file : sourceFiles) {
+ LOG.debug("Populating index from {}", file);
+ Resource<?> resource = getResource(file);
String resourceKey = getFullKey(project, resource);
List<Statement> statements;
Reader reader = null;
try {
- reader = new InputStreamReader(new FileInputStream(inputFile.getFile()), project.getFileSystem().getSourceCharset());
+ reader = new InputStreamReader(new FileInputStream(file), fileSystem.sourceCharset());
statements = statementChunker.chunk(tokenChunker.chunk(reader));
} catch (FileNotFoundException e) {
throw new SonarException(e);
@@ -128,12 +146,12 @@ public class SonarEngine extends CpdEngine {
return index;
}
- private void detect(SonarDuplicationsIndex index, SensorContext context, Project project, List<InputFile> inputFiles) {
+ private void detect(SonarDuplicationsIndex index, SensorContext context, Project project, List<File> sourceFiles) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
- for (InputFile inputFile : inputFiles) {
- LOG.debug("Detection of duplications for {}", inputFile.getFile());
- Resource resource = getResource(inputFile);
+ for (File file : sourceFiles) {
+ LOG.debug("Detection of duplications for {}", file);
+ Resource<?> resource = getResource(file);
String resourceKey = getFullKey(project, resource);
Collection<Block> fileBlocks = index.getByResource(resource, resourceKey);
@@ -143,7 +161,7 @@ public class SonarEngine extends CpdEngine {
clones = executorService.submit(new Task(index, fileBlocks)).get(TIMEOUT, TimeUnit.SECONDS);
} catch (TimeoutException e) {
clones = null;
- LOG.warn("Timeout during detection of duplications for " + inputFile.getFile(), e);
+ LOG.warn("Timeout during detection of duplications for " + file, e);
} catch (InterruptedException e) {
throw new SonarException(e);
} catch (ExecutionException e) {
@@ -171,11 +189,12 @@ public class SonarEngine extends CpdEngine {
}
}
- private Resource getResource(InputFile inputFile) {
- return JavaFile.fromRelativePath(inputFile.getRelativePath(), false);
+ protected Resource<?> getResource(File file) {
+ String relativePath = pathResolver.relativePath(fileSystem.sourceDirs(), file).path();
+ return JavaFile.fromRelativePath(relativePath, false);
}
- static void save(SensorContext context, Resource resource, @Nullable Iterable<CloneGroup> duplications) {
+ static void save(SensorContext context, Resource<?> resource, @Nullable Iterable<CloneGroup> duplications) {
if (duplications == null || Iterables.isEmpty(duplications)) {
return;
}
diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java
index 4d17611c5f0..4ea1a94db45 100644
--- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java
+++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java
@@ -42,7 +42,7 @@ public class CpdSensorTest {
@Before
public void setUp() {
IndexFactory indexFactory = mock(IndexFactory.class);
- sonarEngine = new SonarEngine(indexFactory);
+ sonarEngine = new SonarEngine(indexFactory, null, null);
sonarBridgeEngine = new SonarBridgeEngine(indexFactory);
settings = new Settings(new PropertyDefinitions(CpdPlugin.class));
sensor = new CpdSensor(sonarEngine, sonarBridgeEngine, settings);
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 c5511962b37..e3a054324a4 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
@@ -26,6 +26,9 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.File;
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;
@@ -34,14 +37,20 @@ import java.util.Arrays;
import java.util.Collections;
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.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
public class SonarEngineTest {
private SensorContext context;
- private Resource resource;
+ private Resource<?> resource;
@Before
public void setUp() {
@@ -49,6 +58,23 @@ 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 = mock(RelativePath.class);
+ when(pathResolver.relativePath(anyCollection(), any(java.io.File.class))).thenReturn(relativePath);
+ when(relativePath.path()).thenReturn("com/foo/Bar.java");
+
+ SonarEngine engine = new SonarEngine(null, fileSystem, pathResolver);
+ Resource<?> resource = engine.getResource(new java.io.File(""));
+
+ assertThat(resource.getKey()).isEqualTo("com.foo.Bar");
+ assertThat(resource).isInstanceOf(JavaFile.class);
+ }
+
+ @SuppressWarnings("unchecked")
@Test
public void testNothingToSave() {
SonarEngine.save(context, resource, null);
@@ -68,9 +94,9 @@ public class SonarEngineTest {
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>")));
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
+ + "</g></duplications>")));
}
@Test
@@ -84,9 +110,9 @@ public class SonarEngineTest {
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>")));
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"215\" l=\"200\" r=\"key1\"/>"
+ + "</g></duplications>")));
}
@Test
@@ -100,10 +126,10 @@ public class SonarEngineTest {
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>")));
+ + "<b s=\"5\" l=\"200\" r=\"key1\"/>"
+ + "<b s=\"15\" l=\"200\" r=\"key2\"/>"
+ + "<b s=\"25\" l=\"200\" r=\"key3\"/>"
+ + "</g></duplications>")));
}
@Test
@@ -119,31 +145,31 @@ public class SonarEngineTest {
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>")));
+ + "<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>")));
+ + "<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) {