diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-09-07 16:25:05 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-09-07 17:52:49 +0400 |
commit | 092ad0bbc8f0cd0ffc200b825cc6c1525150595c (patch) | |
tree | cb5e4b7590be78ff94bab7d19d730ac24bf52891 /plugins/sonar-cpd-plugin/src | |
parent | 13d5c77e69b68e74206ea9ad5fbd4920e058e9c0 (diff) | |
download | sonarqube-092ad0bbc8f0cd0ffc200b825cc6c1525150595c.tar.gz sonarqube-092ad0bbc8f0cd0ffc200b825cc6c1525150595c.zip |
SONAR-1091 CPD over different projects
* Fix SQL for Oracle and Derby.
* Rename table clone_blocks to duplications_index.
* The use of "sonar.branch" should deactivate detection of cross-project
duplications.
* Show info about used engine.
* Set size of block - 10.
Diffstat (limited to 'plugins/sonar-cpd-plugin/src')
10 files changed, 52 insertions, 36 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java index 2f8a5883f29..fd1be7bf22e 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdEngine.java @@ -24,10 +24,15 @@ import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; -public interface CpdEngine extends BatchExtension { +public abstract class CpdEngine implements BatchExtension { - boolean isLanguageSupported(Language language); + abstract boolean isLanguageSupported(Language language); - void analyse(Project project, SensorContext context); + abstract void analyse(Project project, SensorContext context); + + @Override + public String toString() { + return getClass().getSimpleName(); + } } diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java index 522d6db766c..a4aacb6c53c 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdSensor.java @@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; +import org.sonar.api.utils.Logs; public class CpdSensor implements Sensor { @@ -75,7 +76,9 @@ public class CpdSensor implements Sensor { } public void analyse(Project project, SensorContext context) { - getEngine(project).analyse(project, context); + CpdEngine engine = getEngine(project); + Logs.INFO.info("{} would be used", engine); + engine.analyse(project, context); } @Override diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java index 8bad74bebe4..48584a83d65 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/PmdEngine.java @@ -33,7 +33,7 @@ import org.sonar.api.resources.Language; import org.sonar.api.resources.Project; import org.sonar.duplications.cpd.CPD; -public class PmdEngine implements CpdEngine { +public class PmdEngine extends CpdEngine { private CpdMapping[] mappings; @@ -41,6 +41,7 @@ public class PmdEngine implements CpdEngine { this.mappings = mappings; } + @Override public boolean isLanguageSupported(Language language) { return getMapping(language) != null; } @@ -54,6 +55,7 @@ public class PmdEngine implements CpdEngine { return null; } + @Override public void analyse(Project project, SensorContext context) { CpdMapping mapping = getMapping(project.getLanguage()); CPD cpd = executeCPD(project, mapping, project.getFileSystem().getSourceCharset()); 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 46d876a6dc5..a81c754ba97 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 @@ -28,6 +28,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.SensorContext; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; @@ -52,12 +54,12 @@ import org.sonar.duplications.statement.Statement; import org.sonar.duplications.statement.StatementChunker; import org.sonar.duplications.token.TokenChunker; import org.sonar.duplications.token.TokenQueue; -import org.sonar.plugins.cpd.index.DbCloneIndex; -import org.sonar.plugins.cpd.index.SonarCloneIndex; +import org.sonar.plugins.cpd.index.DbDuplicationsIndex; +import org.sonar.plugins.cpd.index.SonarDuplicationsIndex; -public class SonarEngine implements CpdEngine { +public class SonarEngine extends CpdEngine { - private static final int BLOCK_SIZE = 13; + private static final int BLOCK_SIZE = 10; private final ResourcePersister resourcePersister; private final DatabaseSession dbSession; @@ -74,6 +76,7 @@ public class SonarEngine implements CpdEngine { this.dbSession = dbSession; } + @Override public boolean isLanguageSupported(Language language) { return Java.INSTANCE.equals(language); } @@ -83,7 +86,8 @@ public class SonarEngine implements CpdEngine { */ private boolean isCrossProject(Project project) { return project.getConfiguration().getBoolean("sonar.cpd.cross_project", false) - && resourcePersister != null && dbSession != null; + && resourcePersister != null && dbSession != null + && StringUtils.isBlank(project.getConfiguration().getString(CoreProperties.PROJECT_BRANCH_PROPERTY)); } private static String getFullKey(Project project, Resource resource) { @@ -94,6 +98,7 @@ public class SonarEngine implements CpdEngine { .toString(); } + @Override public void analyse(Project project, SensorContext context) { List<InputFile> inputFiles = project.getFileSystem().mainFiles(project.getLanguageKey()); if (inputFiles.isEmpty()) { @@ -101,13 +106,13 @@ public class SonarEngine implements CpdEngine { } // Create index - final SonarCloneIndex index; + final SonarDuplicationsIndex index; if (isCrossProject(project)) { Logs.INFO.info("Cross-project analysis enabled"); - index = new SonarCloneIndex(new DbCloneIndex(dbSession, resourcePersister, project)); + index = new SonarDuplicationsIndex(new DbDuplicationsIndex(dbSession, resourcePersister, project)); } else { Logs.INFO.info("Cross-project analysis disabled"); - index = new SonarCloneIndex(); + index = new SonarDuplicationsIndex(); } TokenChunker tokenChunker = JavaTokenProducer.build(); diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/DbCloneIndex.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/DbDuplicationsIndex.java index 8cbaff39900..a0a5529483b 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/DbCloneIndex.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/DbDuplicationsIndex.java @@ -35,12 +35,12 @@ import org.sonar.api.resources.Resource; import org.sonar.batch.index.ResourcePersister; import org.sonar.duplications.block.Block; import org.sonar.duplications.block.ByteArray; -import org.sonar.jpa.entity.CloneBlock; +import org.sonar.jpa.entity.DuplicationBlock; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -public class DbCloneIndex { +public class DbDuplicationsIndex { private final Map<ByteArray, Collection<Block>> cache = Maps.newHashMap(); @@ -49,7 +49,7 @@ public class DbCloneIndex { private final int currentProjectSnapshotId; private final Integer lastSnapshotId; - public DbCloneIndex(DatabaseSession session, ResourcePersister resourcePersister, Project currentProject) { + public DbDuplicationsIndex(DatabaseSession session, ResourcePersister resourcePersister, Project currentProject) { this.session = session; this.resourcePersister = resourcePersister; Snapshot currentSnapshot = resourcePersister.getSnapshotOrFail(currentProject); @@ -61,7 +61,7 @@ public class DbCloneIndex { /** * For tests. */ - DbCloneIndex(DatabaseSession session, ResourcePersister resourcePersister, Integer currentProjectSnapshotId, Integer prevSnapshotId) { + DbDuplicationsIndex(DatabaseSession session, ResourcePersister resourcePersister, Integer currentProjectSnapshotId, Integer prevSnapshotId) { this.session = session; this.resourcePersister = resourcePersister; this.currentProjectSnapshotId = currentProjectSnapshotId; @@ -77,18 +77,19 @@ public class DbCloneIndex { // Order of columns is important - see code below! String sql = "SELECT to_blocks.hash, resource.kee, to_blocks.index_in_file, to_blocks.start_line, to_blocks.end_line" + - " FROM clone_blocks AS to_blocks, clone_blocks AS from_blocks, snapshots AS snapshot, projects AS resource" + + " FROM duplications_index to_blocks, duplications_index from_blocks, snapshots snapshot, projects resource" + " WHERE from_blocks.snapshot_id = :resource_snapshot_id" + " AND to_blocks.hash = from_blocks.hash" + " AND to_blocks.snapshot_id = snapshot.id" + - " AND snapshot.islast = true" + + " AND snapshot.islast = :is_last" + " AND snapshot.project_id = resource.id"; if (lastSnapshotId != null) { // Filter for blocks from previous snapshot of current project sql += " AND to_blocks.project_snapshot_id != :last_project_snapshot_id"; } Query query = session.getEntityManager().createNativeQuery(sql) - .setParameter("resource_snapshot_id", resourceSnapshotId); + .setParameter("resource_snapshot_id", resourceSnapshotId) + .setParameter("is_last", Boolean.TRUE); if (lastSnapshotId != null) { query.setParameter("last_project_snapshot_id", lastSnapshotId); } @@ -128,7 +129,7 @@ public class DbCloneIndex { public void insert(Resource resource, Collection<Block> blocks) { int resourceSnapshotId = getSnapshotIdFor(resource); for (Block block : blocks) { - CloneBlock dbBlock = new CloneBlock( + DuplicationBlock dbBlock = new DuplicationBlock( currentProjectSnapshotId, resourceSnapshotId, block.getBlockHash().toString(), diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/SonarCloneIndex.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/SonarDuplicationsIndex.java index f5bd39542c1..f5ddf5bd579 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/SonarCloneIndex.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/index/SonarDuplicationsIndex.java @@ -31,16 +31,16 @@ import org.sonar.duplications.index.PackedMemoryCloneIndex; import com.google.common.collect.Lists; -public class SonarCloneIndex extends AbstractCloneIndex { +public class SonarDuplicationsIndex extends AbstractCloneIndex { private final CloneIndex mem = new PackedMemoryCloneIndex(); - private final DbCloneIndex db; + private final DbDuplicationsIndex db; - public SonarCloneIndex() { + public SonarDuplicationsIndex() { this(null); } - public SonarCloneIndex(DbCloneIndex db) { + public SonarDuplicationsIndex(DbDuplicationsIndex db) { this.db = db; } diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/DbCloneIndexTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest.java index 5823c9f6044..fedf032033c 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/DbCloneIndexTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest.java @@ -35,14 +35,14 @@ import org.sonar.duplications.block.Block; import org.sonar.duplications.block.ByteArray; import org.sonar.jpa.test.AbstractDbUnitTestCase; -public class DbCloneIndexTest extends AbstractDbUnitTestCase { +public class DbDuplicationsIndexTest extends AbstractDbUnitTestCase { - private DbCloneIndex index; + private DbDuplicationsIndex index; @Test public void shouldGetByHash() { Resource resource = new JavaFile("foo"); - index = spy(new DbCloneIndex(getSession(), null, 9, 7)); + index = spy(new DbDuplicationsIndex(getSession(), null, 9, 7)); doReturn(10).when(index).getSnapshotIdFor(resource); setupData("shouldGetByHash"); @@ -63,13 +63,13 @@ public class DbCloneIndexTest extends AbstractDbUnitTestCase { @Test public void shouldInsert() { Resource resource = new JavaFile("foo"); - index = spy(new DbCloneIndex(getSession(), null, 1, null)); + index = spy(new DbDuplicationsIndex(getSession(), null, 1, null)); doReturn(2).when(index).getSnapshotIdFor(resource); setupData("shouldInsert"); index.insert(resource, Arrays.asList(new Block("foo", new ByteArray("bb"), 0, 1, 2))); - checkTables("shouldInsert", "clone_blocks"); + checkTables("shouldInsert", "duplications_index"); } } diff --git a/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbCloneIndexTest/shouldGetByHash.xml b/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest/shouldGetByHash.xml index 1dab2d464cf..ecef7dd2739 100644 --- a/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbCloneIndexTest/shouldGetByHash.xml +++ b/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest/shouldGetByHash.xml @@ -22,22 +22,22 @@ <!-- Old snapshot of another project --> <!-- bar-old --> - <clone_blocks id="1" project_snapshot_id="1" snapshot_id="2" hash="bb" index_in_file="0" start_line="0" end_line="0" /> + <duplications_index id="1" project_snapshot_id="1" snapshot_id="2" hash="bb" index_in_file="0" start_line="0" end_line="0" /> <!-- Last snapshot of another project --> <!-- bar-last --> - <clone_blocks id="2" project_snapshot_id="3" snapshot_id="4" hash="aa" index_in_file="0" start_line="1" end_line="2" /> + <duplications_index id="2" project_snapshot_id="3" snapshot_id="4" hash="aa" index_in_file="0" start_line="1" end_line="2" /> <!-- Old snapshot of current project --> <!-- foo-old --> - <clone_blocks id="3" project_snapshot_id="5" snapshot_id="6" hash="bb" index_in_file="0" start_line="0" end_line="0" /> + <duplications_index id="3" project_snapshot_id="5" snapshot_id="6" hash="bb" index_in_file="0" start_line="0" end_line="0" /> <!-- Last snapshot of current project --> <!-- foo-last --> - <clone_blocks id="4" project_snapshot_id="7" snapshot_id="8" hash="bb" index_in_file="0" start_line="0" end_line="0" /> + <duplications_index id="4" project_snapshot_id="7" snapshot_id="8" hash="bb" index_in_file="0" start_line="0" end_line="0" /> <!-- New snapshot of current project --> <!-- foo --> - <clone_blocks id="5" project_snapshot_id="9" snapshot_id="10" hash="aa" index_in_file="0" start_line="0" end_line="0" /> + <duplications_index id="5" project_snapshot_id="9" snapshot_id="10" hash="aa" index_in_file="0" start_line="0" end_line="0" /> </dataset> diff --git a/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbCloneIndexTest/shouldInsert-result.xml b/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest/shouldInsert-result.xml index e3e709ffc45..5848ecb5723 100644 --- a/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbCloneIndexTest/shouldInsert-result.xml +++ b/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest/shouldInsert-result.xml @@ -4,6 +4,6 @@ <snapshots id="2" status="U" islast="false" project_id="1" /> <projects id="1" kee="foo" enabled="true" scope="FIL" qualifier="CLA" /> - <clone_blocks id="1" project_snapshot_id="1" snapshot_id="2" hash="bb" index_in_file="0" start_line="1" end_line="2" /> + <duplications_index id="1" project_snapshot_id="1" snapshot_id="2" hash="bb" index_in_file="0" start_line="1" end_line="2" /> </dataset> diff --git a/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbCloneIndexTest/shouldInsert.xml b/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest/shouldInsert.xml index 940281a0599..940281a0599 100644 --- a/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbCloneIndexTest/shouldInsert.xml +++ b/plugins/sonar-cpd-plugin/src/test/resources/org/sonar/plugins/cpd/index/DbDuplicationsIndexTest/shouldInsert.xml |