-// POLOP_IS_GREAT
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2013 SonarSource
import java.util.List;
-public class IgnoreIssuesConfiguration {
+public final class IgnoreIssuesConfiguration {
+ private IgnoreIssuesConfiguration() {}
static final int LARGE_SIZE = 20;
static final int SMALL_SIZE = 10;
}
public boolean accept(Issue issue) {
- Pattern extraPattern = patternsInitializer.getExtraPattern(issue.componentKey());
- LOG.debug("Extra pattern for resource {}: {}", issue.componentKey(), extraPattern);
- if (extraPattern != null && extraPattern.match(issue)) {
- logExclusion(issue, extraPattern);
- return false;
- }
-
- List<Pattern> patterns = patternsInitializer.getMulticriteriaPatterns();
+ List<Pattern> patterns = patternsInitializer.getPatternsForComponent(issue.componentKey());
for (Pattern pattern : patterns) {
if (pattern.match(issue)) {
logExclusion(issue, pattern);
public final class IgnoreIssuesPlugin {
+ private IgnoreIssuesPlugin() {}
+
public static List<?> getExtensions() {
ImmutableList.Builder<Object> extensions = ImmutableList.builder();
}
public boolean match(Issue violation) {
- boolean match = matchResource(violation.componentKey()) && matchRule(violation.ruleKey());
+ boolean match = violation.componentKey() != null
+ && matchRule(violation.ruleKey());
if (checkLines && violation.line() != null) {
match = match && matchLine(violation.line());
}
}
boolean matchRule(RuleKey rule) {
- System.out.printf("Matching rule {} against pattern {}", rule, rulePattern);
if (rule == null) {
return false;
}
}
boolean matchResource(String resource) {
- System.out.printf("Matching resource {} against pattern {}", resource, resourcePattern);
return resource != null && resourcePattern.match(resource);
}
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.SonarException;
-import java.io.File;
-import java.io.IOException;
import java.util.List;
public class PatternDecoder {
return patterns;
}
- public List<Pattern> decode(File file) {
- try {
- List<String> lines = FileUtils.readLines(file);
- List<Pattern> patterns = Lists.newLinkedList();
- for (String line : lines) {
- Pattern pattern = decodeLine(line);
- if (pattern != null) {
- patterns.add(pattern);
- }
- }
- return patterns;
-
- } catch (IOException e) {
- throw new SonarException("Fail to load the file: " + file.getAbsolutePath(), e);
- }
- }
-
/**
* Main method that decodes a line which defines a pattern
*/
@VisibleForTesting
boolean isBlankOrComment(String line) {
- return StringUtils.isBlank(line) || StringUtils.startsWith(line, "#");
+ return StringUtils.isBlank(line) ^ StringUtils.startsWith(line, "#");
}
@VisibleForTesting
package org.sonar.plugins.core.issue.ignore.pattern;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
private List<Pattern> multicriteriaPatterns;
private List<Pattern> blockPatterns;
private List<Pattern> allFilePatterns;
- private Map<String, Pattern> extraPatternByResource = Maps.newHashMap();
+ private Map<String, List<Pattern>> patternByComponent = Maps.newHashMap();
public PatternsInitializer(Settings settings) {
this.settings = settings;
return allFilePatterns;
}
- public Pattern getExtraPattern(String resource) {
- return extraPatternByResource.get(resource.substring(resource.lastIndexOf(":") + 1));
+ public boolean hasFileContentPattern() {
+ return ! (blockPatterns.isEmpty() && allFilePatterns.isEmpty());
+ }
+
+ public boolean hasMulticriteriaPatterns() {
+ return ! multicriteriaPatterns.isEmpty();
+ }
+
+ public boolean hasConfiguredPatterns() {
+ return hasFileContentPattern() || hasMulticriteriaPatterns();
}
@VisibleForTesting
}
public void addPatternToExcludeResource(String resource) {
- extraPatternByResource.put(resource, new Pattern(resource, "*").setCheckLines(false));
+ addPatternForComponent(resource, new Pattern(resource, "*").setCheckLines(false));
}
public void addPatternToExcludeLines(String resource, Set<LineRange> lineRanges) {
- extraPatternByResource.put(resource, new Pattern(resource, "*", lineRanges));
+ addPatternForComponent(resource, new Pattern(resource, "*", lineRanges));
+ }
+
+ public void configurePatternsForComponent(String componentKey, String path) {
+ for (Pattern pattern: multicriteriaPatterns) {
+ if (pattern.matchResource(path)) {
+ addPatternForComponent(componentKey, pattern);
+ }
+ }
+ }
+
+ public List<Pattern> getPatternsForComponent(String componentKey) {
+ if (patternByComponent.containsKey(componentKey)) {
+ return patternByComponent.get(componentKey);
+ } else {
+ return ImmutableList.of();
+ }
}
+ private void addPatternForComponent(String component, Pattern pattern) {
+ if (!patternByComponent.containsKey(component)) {
+ List<Pattern> newList = Lists.newArrayList();
+ patternByComponent.put(component, newList);
+ }
+ patternByComponent.get(component).add(pattern);
+ }
}
import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaFile;
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;
import org.sonar.api.utils.SonarException;
+import org.sonar.core.component.ComponentKeys;
import org.sonar.plugins.core.issue.ignore.pattern.PatternsInitializer;
import java.io.File;
private final RegexpScanner regexpScanner;
private final PatternsInitializer patternsInitializer;
private final ModuleFileSystem fileSystem;
+ private final PathResolver pathResolver;
public SourceScanner(RegexpScanner regexpScanner, PatternsInitializer patternsInitializer, ModuleFileSystem fileSystem) {
this.regexpScanner = regexpScanner;
this.patternsInitializer = patternsInitializer;
this.fileSystem = fileSystem;
+ this.pathResolver = new PathResolver();
}
public boolean shouldExecuteOnProject(Project project) {
- return patternsInitializer.getAllFilePatterns().size() > 0 || patternsInitializer.getBlockPatterns().size() > 0;
+ return patternsInitializer.hasConfiguredPatterns();
}
/**
Charset sourcesEncoding = fileSystem.sourceCharset();
List<File> files;
+ List<File> dirs;
if (isTest) {
files = fileSystem.files(FileQuery.onTest().onLanguage(project.getLanguageKey()));
+ dirs = fileSystem.testDirs();
} else {
files = fileSystem.files(FileQuery.onSource().onLanguage(project.getLanguageKey()));
+ dirs = fileSystem.sourceDirs();
}
for (File inputFile : files) {
try {
- String resource = defineResource(inputFile, fileSystem, project, isTest);
- if (resource != null) {
- regexpScanner.scan(resource, inputFile, sourcesEncoding);
+ String componentKey = resolveComponent(inputFile, fileSystem, project, isTest);
+ if (componentKey != null) {
+ String relativePath = pathResolver.relativePath(dirs, inputFile).path();
+ patternsInitializer.configurePatternsForComponent(componentKey, relativePath);
+ if (patternsInitializer.hasFileContentPattern()) {
+ regexpScanner.scan(componentKey, inputFile, sourcesEncoding);
+ }
}
} catch (Exception e) {
throw new SonarException("Unable to read the source file : '" + inputFile.getAbsolutePath() + "' with the charset : '"
/*
* This method is necessary because Java resources are not treated as every other resource...
*/
- private String defineResource(File inputFile, ModuleFileSystem fileSystem, Project project, boolean isTest) {
+ private String resolveComponent(File inputFile, ModuleFileSystem fileSystem, Project project, boolean isTest) {
+ Resource<?> resource = null;
if (Java.KEY.equals(project.getLanguageKey()) && Java.isJavaFile(inputFile)) {
List<File> sourceDirs = null;
if (isTest) {
} else {
sourceDirs = fileSystem.sourceDirs();
}
- JavaFile file = JavaFile.fromIOFile(inputFile, sourceDirs, isTest);
- if (file == null) {
- return null;
- } else {
- return file.getKey();
- }
+ resource = JavaFile.fromIOFile(inputFile, sourceDirs, isTest);
+ } else {
+ resource = new org.sonar.api.resources.File(inputFile.getPath());
+ }
+
+ if (resource == null) {
+ return null;
+ } else {
+ return ComponentKeys.createKey(project, resource);
}
- return inputFile.getPath();
}
@Override
public String toString() {
- return "Ignore Issues - Source Scanner";
+ return "Issues Exclusions - Source Scanner";
}
}
@Test
public void shouldBeIgnoredWithStandardPatterns() throws IOException {
- when(patternsInitializer.getMulticriteriaPatterns()).thenReturn(createPatterns("org.foo.Bar;*;*\norg.foo.Hello;checkstyle:MagicNumber;[15-200]"));
+ when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;checkstyle:MagicNumber;[15-200]"));
assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 150))).isFalse();
}
@Test
public void shouldNotBeIgnoredWithStandardPatterns() throws IOException {
- when(patternsInitializer.getMulticriteriaPatterns()).thenReturn(createPatterns("org.foo.Bar;*;*\norg.foo.Hello;checkstyle:MagicNumber;[15-200]"));
+ when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;checkstyle:MagicNumber;[15-200]"));
assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isTrue();
}
@Test
public void shouldBeIgnoredWithExtraPattern() throws IOException {
- when(patternsInitializer.getExtraPattern(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]").get(0));
+ when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]"));
assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 150))).isFalse();
}
@Test
public void shouldNotBeIgnoredWithExtraPattern() throws IOException {
- when(patternsInitializer.getExtraPattern(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]").get(0));
+ when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]"));
assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isTrue();
}
public class IgnoreIssuesPluginTest {
@Test
public void justForCoverage() {
- assertThat(new IgnoreIssuesPlugin().getExtensions()).hasSize(3 /* properties */ + 4 /* extensions */);
+ assertThat(IgnoreIssuesPlugin.getExtensions()).hasSize(3 /* properties */ + 4 /* extensions */);
}
}
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.SonarException;
-import java.io.File;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
assertThat(pattern.getAllFileRegexp()).isEqualTo("SONAR-ALL-OFF");
}
- @Test
- public void shouldFailIfUnexistingFile() {
- thrown.expect(SonarException.class);
- thrown.expectMessage("Fail to load the file");
-
- decoder.decode(new File("foo"));
- }
-
@Test
public void shouldFailToReadUncorrectLine1() {
thrown.expect(SonarException.class);
package org.sonar.plugins.core.issue.ignore.pattern;
+import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.plugins.core.issue.ignore.Constants;
-import org.sonar.plugins.core.issue.ignore.IgnoreIssuesPlugin;
+import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration;
import java.util.Set;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class PatternsInitializerTest {
@Before
public void init() {
- settings = new Settings(new PropertyDefinitions(new IgnoreIssuesPlugin()));
+ settings = new Settings(new PropertyDefinitions(IgnoreIssuesConfiguration.getPropertyDefinitions()));
patternsInitializer = new PatternsInitializer(settings);
}
@Test
public void testNoConfiguration() {
patternsInitializer.initPatterns();
+ assertThat(patternsInitializer.hasConfiguredPatterns()).isFalse();
assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
}
String file = "foo";
patternsInitializer.addPatternToExcludeResource(file);
- Pattern extraPattern = patternsInitializer.getExtraPattern(file);
+ Pattern extraPattern = patternsInitializer.getPatternsForComponent(file).get(0);
assertThat(extraPattern.matchResource(file)).isTrue();
assertThat(extraPattern.isCheckLines()).isFalse();
}
lineRanges.add(new LineRange(25, 28));
patternsInitializer.addPatternToExcludeLines(file, lineRanges);
- Pattern extraPattern = patternsInitializer.getExtraPattern(file);
+ Pattern extraPattern = patternsInitializer.getPatternsForComponent(file).get(0);
assertThat(extraPattern.matchResource(file)).isTrue();
assertThat(extraPattern.getAllLines()).isEqualTo(Sets.newHashSet(25, 26, 27, 28));
}
@Test
public void shouldReturnMulticriteriaPattern() {
settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1,2");
- settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org.foo.Bar");
+ settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org/foo/Bar.java");
settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "*");
settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "*");
- settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "org.foo.Hello");
+ settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "org/foo/Hello.java");
settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.RULE_KEY, "checkstyle:MagicNumber");
settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "[15-200]");
patternsInitializer.initPatterns();
+ assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
+ assertThat(patternsInitializer.hasFileContentPattern()).isFalse();
+ assertThat(patternsInitializer.hasMulticriteriaPatterns()).isTrue();
assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(2);
assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".2." + Constants.END_BLOCK_REGEXP, "// FOO-ON");
patternsInitializer.initPatterns();
+ assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
+ assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
+ assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(2);
assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0);
settings.setProperty(Constants.PATTERNS_ALLFILE_KEY + ".2." + Constants.FILE_REGEXP, "//FOO-IGNORE-ALL");
patternsInitializer.initPatterns();
+ assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue();
+ assertThat(patternsInitializer.hasFileContentPattern()).isTrue();
+ assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse();
assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0);
assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0);
assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2);
}
+
+ @Test
+ public void shouldConfigurePatternsForComponents() {
+ String componentKey = "groupId:artifactId:org.foo.Bar";
+ String path = "org/foo/Bar.java";
+
+ Pattern matching1, matching2, notMatching;
+ matching1 = mock(Pattern.class);
+ when(matching1.matchResource(path)).thenReturn(true);
+ matching2 = mock(Pattern.class);
+ when(matching2.matchResource(path)).thenReturn(true);
+ notMatching = mock(Pattern.class);
+ when(notMatching.matchResource(path)).thenReturn(false);
+
+ patternsInitializer.initPatterns();
+ patternsInitializer.getMulticriteriaPatterns().addAll(Lists.newArrayList(matching1, matching2, notMatching));
+ patternsInitializer.configurePatternsForComponent(componentKey, path);
+
+ assertThat(patternsInitializer.getPatternsForComponent(componentKey).size()).isEqualTo(2);
+ assertThat(patternsInitializer.getPatternsForComponent("other").size()).isEqualTo(0);
+ }
}
package org.sonar.plugins.core.issue.ignore.scanner;
+import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-
import static com.google.common.base.Charsets.UTF_8;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
public class SourceScannerTest {
@Mock
private PatternsInitializer patternsInitializer;
@Mock
- private Project project;
- @Mock
private ModuleFileSystem fileSystem;
+ private Project project;
+
@Rule
public ExpectedException thrown = ExpectedException.none();
public void init() {
MockitoAnnotations.initMocks(this);
+ Project realProject = new Project("polop");
+ project = Mockito.spy(realProject);
+ Mockito.doReturn("java").when(project).getLanguageKey();
when(fileSystem.sourceCharset()).thenReturn(UTF_8);
scanner = new SourceScanner(regexpScanner, patternsInitializer, fileSystem);
@Test
public void testToString() throws Exception {
- assertThat(scanner.toString()).isEqualTo("Ignore Issues - Source Scanner");
+ assertThat(scanner.toString()).isEqualTo("Issues Exclusions - Source Scanner");
}
@Test
public void shouldExecute() throws IOException {
- when(patternsInitializer.getAllFilePatterns()).thenReturn(Arrays.asList(new Pattern(), new Pattern()));
+ when(patternsInitializer.hasConfiguredPatterns()).thenReturn(true);
assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
- when(patternsInitializer.getAllFilePatterns()).thenReturn(Collections.<Pattern>emptyList());
- when(patternsInitializer.getBlockPatterns()).thenReturn(Arrays.asList(new Pattern(), new Pattern()));
- assertThat(scanner.shouldExecuteOnProject(null)).isTrue();
-
- when(patternsInitializer.getAllFilePatterns()).thenReturn(Collections.<Pattern>emptyList());
- when(patternsInitializer.getBlockPatterns()).thenReturn(Collections.<Pattern>emptyList());
+ when(patternsInitializer.hasConfiguredPatterns()).thenReturn(false);
assertThat(scanner.shouldExecuteOnProject(null)).isFalse();
}
.thenReturn(Arrays.asList(testFile));
when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java")));
when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java")));
+ when(patternsInitializer.hasFileContentPattern()).thenReturn(true);
scanner.analyse(project, null);
- verify(regexpScanner).scan("[default].Foo", sourceFile, UTF_8);
- verify(regexpScanner).scan("[default].FooTest", testFile, UTF_8);
+ verify(patternsInitializer).configurePatternsForComponent("polop:[default].Foo", "Foo.java");
+ verify(patternsInitializer).configurePatternsForComponent("polop:[default].FooTest", "FooTest.java");
+ verify(regexpScanner).scan("polop:[default].Foo", sourceFile, UTF_8);
+ verify(regexpScanner).scan("polop:[default].FooTest", testFile, UTF_8);
+ }
+
+ @Test
+ public void shouldAnalyseFilesOnlyWhenRegexConfigured() throws IOException {
+ File sourceFile = new File("src/main/java/Foo.java");
+ File testFile = new File("src/test/java/FooTest.java");
+
+ when(project.getLanguageKey()).thenReturn("java");
+ when(fileSystem.files(Mockito.isA(FileQuery.class)))
+ .thenReturn(Arrays.asList(sourceFile))
+ .thenReturn(Arrays.asList(testFile));
+ when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java")));
+ when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java")));
+ when(patternsInitializer.hasFileContentPattern()).thenReturn(false);
+
+ scanner.analyse(project, null);
+
+ verify(patternsInitializer).configurePatternsForComponent("polop:[default].Foo", "Foo.java");
+ verify(patternsInitializer).configurePatternsForComponent("polop:[default].FooTest", "FooTest.java");
+ verifyNoMoreInteractions(regexpScanner);
}
@Test
when(fileSystem.files(Mockito.isA(FileQuery.class)))
.thenReturn(Arrays.asList(sourceFile))
.thenReturn(Arrays.asList(testFile));
+ when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File("")));
+ when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File("")));
+ when(patternsInitializer.hasFileContentPattern()).thenReturn(true);
scanner.analyse(project, null);
- verify(regexpScanner).scan("Foo.php", sourceFile, UTF_8);
- verify(regexpScanner).scan("FooTest.php", testFile, UTF_8);
+ verify(patternsInitializer).configurePatternsForComponent("polop:Foo.php", "Foo.php");
+ verify(patternsInitializer).configurePatternsForComponent("polop:FooTest.php", "FooTest.php");
+ verify(regexpScanner).scan("polop:Foo.php", sourceFile, UTF_8);
+ verify(regexpScanner).scan("polop:FooTest.php", testFile, UTF_8);
}
@Test
when(fileSystem.files(Mockito.isA(FileQuery.class)))
.thenReturn(Arrays.asList(sourceFile, otherFile))
.thenReturn(empty);
- when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java")));
- when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java")));
+ when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File("src/main/java"), new File("")));
+ when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File("src/test/java")));
+ when(patternsInitializer.hasFileContentPattern()).thenReturn(true);
scanner.analyse(project, null);
.thenReturn(empty);
when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java")));
when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java")));
+ when(patternsInitializer.hasFileContentPattern()).thenReturn(true);
scanner.analyse(project, null);
when(project.getLanguageKey()).thenReturn("php");
when(fileSystem.files(Mockito.isA(FileQuery.class))).thenReturn(Arrays.asList(sourceFile));
- doThrow(new IOException("BUG")).when(regexpScanner).scan("Foo.php", sourceFile, UTF_8);
+ List<Pattern> empty = ImmutableList.of();
+ when(patternsInitializer.getPatternsForComponent("Foo.php")).thenReturn(empty);
+ doThrow(new IOException("BUG")).when(regexpScanner).scan("polop:Foo.php", sourceFile, UTF_8);
thrown.expect(SonarException.class);
thrown.expectMessage("Unable to read the source file");
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Event;
import org.sonar.api.batch.SonarIndex;
-import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.design.Dependency;
-import org.sonar.api.measures.*;
-import org.sonar.api.resources.*;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.MeasuresFilter;
+import org.sonar.api.measures.MeasuresFilters;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.MetricFinder;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectLink;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.ResourceUtils;
+import org.sonar.api.resources.Scopes;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ResourceFilters;
import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.ScanIssues;
+import org.sonar.core.component.ComponentKeys;
import org.sonar.core.component.ScanGraph;
-import java.util.*;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
public class DefaultIndex extends SonarIndex {
return null;
}
- static String createUID(Project project, Resource resource) {
- String uid = resource.getKey();
- if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
- // not a project nor a library
- uid = new StringBuilder(ResourceModel.KEY_SIZE)
- .append(project.getKey())
- .append(':')
- .append(resource.getKey())
- .toString();
- }
- return uid;
- }
-
private boolean checkExclusion(Resource resource, Bucket parent) {
boolean excluded = (parent != null && parent.isExcluded()) || (resourceFilters != null && resourceFilters.isExcluded(resource));
resource.setExcluded(excluded);
return null;
}
- resource.setEffectiveKey(createUID(currentProject, resource));
+ resource.setEffectiveKey(ComponentKeys.createKey(currentProject, resource));
bucket = new Bucket(resource).setParent(parentBucket);
buckets.put(resource, bucket);
index.doStart(project);
}
- @Test
- public void shouldCreateUID() {
- Project project = new Project("my_project");
- assertThat(DefaultIndex.createUID(project, project)).isEqualTo("my_project");
-
- JavaPackage javaPackage = new JavaPackage("org.foo");
- assertThat(DefaultIndex.createUID(project, javaPackage)).isEqualTo("my_project:org.foo");
-
- Library library = new Library("junit:junit", "4.7");
- assertThat(DefaultIndex.createUID(project, library)).isEqualTo("junit:junit");
- }
-
@Test
public void shouldIndexParentOfDeprecatedFiles() {
File file = new File("org/foo/Bar.java");
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.component;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.database.model.ResourceModel;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.Scopes;
+
+public final class ComponentKeys {
+
+ private ComponentKeys() {}
+
+ public static String createKey(Project project, Resource<?> resource) {
+ String key = resource.getKey();
+ if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
+ // not a project nor a library
+ key = new StringBuilder(ResourceModel.KEY_SIZE)
+ .append(project.getKey())
+ .append(':')
+ .append(resource.getKey())
+ .toString();
+ }
+ return key;
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.component;
+
+import org.junit.Test;
+import org.sonar.api.resources.JavaPackage;
+import org.sonar.api.resources.Library;
+import org.sonar.api.resources.Project;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ComponentKeysTest {
+
+ @Test
+ public void shouldCreateUID() {
+ Project project = new Project("my_project");
+ assertThat(ComponentKeys.createKey(project, project)).isEqualTo("my_project");
+
+ JavaPackage javaPackage = new JavaPackage("org.foo");
+ assertThat(ComponentKeys.createKey(project, javaPackage)).isEqualTo("my_project:org.foo");
+
+ Library library = new Library("junit:junit", "4.7");
+ assertThat(ComponentKeys.createKey(project, library)).isEqualTo("junit:junit");
+ }
+
+}