abstract boolean isLanguageSupported(String language);
- abstract void analyse(Project project, SensorContext context);
+ abstract void analyse(Project project, String language, SensorContext context);
protected void logExclusions(String[] exclusions, Logger logger) {
if (exclusions.length > 0) {
continue;
}
LOG.info("{} is used for {}", engine, language);
- engine.analyse(project, context);
+ engine.analyse(project, language, context);
}
}
*/
package org.sonar.plugins.cpd;
-import org.sonar.api.scan.filesystem.InputFile;
-
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.FileQuery;
+import org.sonar.api.scan.filesystem.InputFile;
import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
}
@Override
- public void analyse(Project project, SensorContext context) {
+ public void analyse(Project project, String languageKey, SensorContext context) {
String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
logExclusions(cpdExclusions, LOG);
- Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onSource().onLanguage(project.getLanguageKey())
+ Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onMain().onLanguage(languageKey)
.withExclusions(cpdExclusions));
if (!sourceFiles.iterator().hasNext()) {
return;
}
- CpdMapping mapping = getMapping(project.getLanguage().getKey());
+ CpdMapping mapping = getMapping(languageKey);
// Create index
SonarDuplicationsIndex index = indexFactory.create(project);
- TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fileSystem.sourceCharset().name(), getBlockSize(project));
+ TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fileSystem.sourceCharset().name(), getBlockSize(project, languageKey));
for (InputFile inputFile : sourceFiles) {
LOG.debug("Populating index from {}", inputFile);
String resourceEffectiveKey = inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY);
}
// Detect
- Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(project));
+ Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(project, languageKey));
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
}
@VisibleForTesting
- int getBlockSize(Project project) {
- String languageKey = project.getLanguageKey();
+ int getBlockSize(Project project, String languageKey) {
int blockSize = settings.getInt("sonar.cpd." + languageKey + ".minimumLines");
if (blockSize == 0) {
blockSize = getDefaultBlockSize(languageKey);
}
@VisibleForTesting
- int getMinimumTokens(Project project) {
- int minimumTokens = settings.getInt("sonar.cpd." + project.getLanguageKey() + ".minimumTokens");
+ int getMinimumTokens(Project project, String languageKey) {
+ int minimumTokens = settings.getInt("sonar.cpd." + languageKey + ".minimumTokens");
if (minimumTokens == 0) {
minimumTokens = settings.getInt(CoreProperties.CPD_MINIMUM_TOKENS_PROPERTY);
}
package org.sonar.plugins.cpd;
-import org.sonar.api.scan.filesystem.InputFile;
-
import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.PersistenceMode;
import org.sonar.api.resources.Java;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.FileQuery;
+import org.sonar.api.scan.filesystem.InputFile;
import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
}
@Override
- public void analyse(Project project, SensorContext context) {
+ public void analyse(Project project, String languageKey, SensorContext context) {
String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
logExclusions(cpdExclusions, LOG);
- Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onSource().onLanguage(project.getLanguageKey()).withExclusions(cpdExclusions));
+ Iterable<InputFile> sourceFiles = fileSystem.inputFiles(FileQuery.onMain().onLanguage(languageKey).withExclusions(cpdExclusions));
if (!sourceFiles.iterator().hasNext()) {
return;
}
*/
package org.sonar.plugins.cpd;
-import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.sonar.api.resources.Project;
import static org.fest.assertions.Assertions.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@Test
public void shouldReturnDefaultBlockSize() {
- assertThat(SonarBridgeEngine.getDefaultBlockSize("cobol"), is(30));
- assertThat(SonarBridgeEngine.getDefaultBlockSize("natur"), is(20));
- assertThat(SonarBridgeEngine.getDefaultBlockSize("abap"), is(20));
- assertThat(SonarBridgeEngine.getDefaultBlockSize("other"), is(10));
+ assertThat(SonarBridgeEngine.getDefaultBlockSize("cobol")).isEqualTo(30);
+ assertThat(SonarBridgeEngine.getDefaultBlockSize("natur")).isEqualTo(20);
+ assertThat(SonarBridgeEngine.getDefaultBlockSize("abap")).isEqualTo(20);
+ assertThat(SonarBridgeEngine.getDefaultBlockSize("other")).isEqualTo(10);
}
@Test
public void defaultBlockSize() {
- Project project = newProject("foo", "java");
+ Project project = newProject("foo");
- assertThat(engine.getBlockSize(project)).isEqualTo(10);
+ assertThat(engine.getBlockSize(project, "java")).isEqualTo(10);
}
@Test
public void blockSizeForCobol() {
- Project project = newProject("foo", "cobol");
+ Project project = newProject("foo");
settings.setProperty("sonar.cpd.cobol.minimumLines", "42");
- assertThat(engine.getBlockSize(project)).isEqualTo(42);
+ assertThat(engine.getBlockSize(project, "cobol")).isEqualTo(42);
}
@Test
public void defaultMinimumTokens() {
- Project project = newProject("foo", "java");
+ Project project = newProject("foo");
- assertThat(engine.getMinimumTokens(project), is(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE));
+ assertThat(engine.getMinimumTokens(project, "java")).isEqualTo(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE);
}
@Test
public void generalMinimumTokens() {
- Project project = newProject("foo", "java");
+ Project project = newProject("foo");
settings.setProperty("sonar.cpd.minimumTokens", 33);
- assertThat(engine.getMinimumTokens(project), is(33));
+ assertThat(engine.getMinimumTokens(project, "java")).isEqualTo(33);
}
@Test
public void minimumTokensByLanguage() {
- Project javaProject = newProject("foo", "java");
+ Project javaProject = newProject("foo");
settings.setProperty("sonar.cpd.java.minimumTokens", "42");
settings.setProperty("sonar.cpd.php.minimumTokens", "33");
- assertThat(engine.getMinimumTokens(javaProject), is(42));
+ assertThat(engine.getMinimumTokens(javaProject, "java")).isEqualTo(42);
- Project phpProject = newProject("foo", "php");
+ Project phpProject = newProject("foo");
settings.setProperty("sonar.cpd.java.minimumTokens", "42");
settings.setProperty("sonar.cpd.php.minimumTokens", "33");
- assertThat(engine.getMinimumTokens(phpProject), is(33));
+ assertThat(engine.getMinimumTokens(phpProject, "php")).isEqualTo(33);
}
- private static Project newProject(String key, String language) {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty("sonar.language", language);
- return new Project(key).setConfiguration(conf).setAnalysisType(Project.AnalysisType.DYNAMIC);
+ private static Project newProject(String key) {
+ return new Project(key).setAnalysisType(Project.AnalysisType.DYNAMIC);
}
}
Language getLanguage();
/**
- * @deprecated since 4.2
+ * @deprecated since 4.2 not used anymore
*/
@Deprecated
Resource createResource(File file, List<File> sourceDirs);