use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class AnalyzerMessageTest method testAnalyzerMessageOnFile.
@Test
public void testAnalyzerMessageOnFile() {
JavaCheck javaCheck = mock(JavaCheck.class);
File file = new File("a");
String message = "analyzer message";
int cost = 3;
AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, file, null, message, cost);
assertThat(analyzerMessage.getCheck()).isEqualTo(javaCheck);
assertThat(analyzerMessage.getFile()).isEqualTo(file);
assertThat(analyzerMessage.getLine()).isEqualTo(null);
assertThat(analyzerMessage.getMessage()).isEqualTo(message);
assertThat(analyzerMessage.getCost()).isEqualTo(cost);
assertThat(analyzerMessage.primaryLocation()).isNull();
}
use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class CommonsCollectionsTest method initAndScan.
private void initAndScan() {
File prjDir = new File("target/test-projects/commons-collections-3.2.1");
File srcDir = new File(prjDir, "src");
context = SensorContextTester.create(srcDir);
DefaultFileSystem fs = context.fileSystem();
Collection<File> files = FileUtils.listFiles(srcDir, new String[] { "java" }, true);
for (File file : files) {
fs.add(new TestInputFileBuilder("", file.getPath()).build());
}
Measurer measurer = new Measurer(fs, context, mock(NoSonarFilter.class));
JavaResourceLocator javaResourceLocator = new JavaResourceLocator() {
public Map<String, String> sourceFileCache = Maps.newHashMap();
@Override
public InputFile findResourceByClassName(String className) {
return null;
}
@Override
public String findSourceFileKeyByClassName(String className) {
String name = className.replace('.', '/');
return sourceFileCache.get(name);
}
@Override
public Collection<File> classFilesToAnalyze() {
return Collections.emptyList();
}
@Override
public Collection<File> classpath() {
return null;
}
@Override
public void scanFile(JavaFileScannerContext context) {
JavaFilesCache javaFilesCache = new JavaFilesCache();
javaFilesCache.scanFile(context);
for (String key : javaFilesCache.resourcesCache.keySet()) {
sourceFileCache.put(key, context.getFileKey());
}
}
};
squid = new JavaSquid(new JavaVersionImpl(), null, measurer, javaResourceLocator, null, new JavaCheck[0]);
squid.scan(files, Collections.<File>emptyList());
}
use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class XmlFileSensorTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock(DefaultFileSystem fs, @Nullable File file) throws IOException {
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.checkClasses()).thenReturn(new JavaCheck[] { new PomElementOrderCheck() });
when(sonarComponents.getFileSystem()).thenReturn(fs);
if (file != null) {
when(sonarComponents.fileLines(any(File.class))).thenReturn(Files.readLines(file, StandardCharsets.UTF_8));
}
Checks<JavaCheck> checks = mock(Checks.class);
when(checks.ruleKey(any(JavaCheck.class))).thenReturn(RuleKey.of("squid", RuleAnnotationUtils.getRuleKey(PomElementOrderCheck.class)));
when(sonarComponents.checks()).thenReturn(Lists.<Checks<JavaCheck>>newArrayList(checks));
return sonarComponents;
}
use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class XmlAnalyzerTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock(DefaultFileSystem fs, JavaCheck... visitors) {
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.checkClasses()).thenReturn(visitors);
when(sonarComponents.getFileSystem()).thenReturn(fs);
Checks<JavaCheck> checks = mock(Checks.class);
when(checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
when(sonarComponents.checks()).thenReturn(Lists.newArrayList(checks));
return sonarComponents;
}
use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class FilterVerifier method verify.
public static void verify(String filename, JavaIssueFilter filter, JavaCheck... extraJavaChecks) {
// set the component to the filter
filter.setComponentKey(filename);
IssueCollector issueCollector = new IssueCollector();
ArrayList<JavaCheck> visitors = Lists.<JavaCheck>newArrayList(filter, issueCollector);
// instantiate the rules filtered by the filter
visitors.addAll(instantiateRules(filter.filteredRules()));
for (JavaCheck visitor : extraJavaChecks) {
visitors.add(visitor);
}
Collection<File> classpath = FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar", "zip" }, true);
VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, Lists.newArrayList(classpath), null);
JavaAstScanner.scanSingleFileForTests(new File(filename), visitorsBridge);
VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
Multimap<Integer, String> issuesByLines = HashMultimap.create();
for (AnalyzerMessage analyzerMessage : testJavaFileScannerContext.getIssues()) {
Integer issueLine = analyzerMessage.getLine();
String ruleKey = AnnotationUtils.getAnnotation(analyzerMessage.getCheck().getClass(), Rule.class).key();
FilterableIssue issue = mock(FilterableIssue.class);
when(issue.ruleKey()).thenReturn(RuleKey.of("repo", ruleKey));
when(issue.componentKey()).thenReturn(filename);
when(issue.line()).thenReturn(issueLine);
if (issueCollector.rejectedIssuesLines.contains(issueLine)) {
assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'NoIssue' but issue of rule '" + ruleKey + "' has been accepted!").isFalse();
} else if (issueCollector.acceptedIssuesLines.contains(issueLine)) {
// force check on accepted issues
assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'WithIssue' but no issue have been raised!").isTrue();
} else {
issuesByLines.put(issueLine, ruleKey);
}
}
if (!issuesByLines.isEmpty()) {
List<Integer> lines = Lists.newArrayList(issuesByLines.keySet());
Collections.sort(lines);
StringBuilder builder = new StringBuilder();
for (Integer line : lines) {
builder.append("\n#" + line + ": " + issuesByLines.get(line).toString());
}
Fail.fail("The following lines have not been marked with 'WithIssue' or 'NoIssue' and raised issues:" + builder.toString());
}
}
Aggregations