import org.sonar.api.BatchExtension;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.ProjectClasspath;
+import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.SonarException;
import org.sonar.plugins.findbugs.xml.FindBugsFilter;
import org.sonar.plugins.findbugs.xml.Match;
+import javax.annotation.CheckForNull;
+
import java.io.*;
import java.util.ArrayList;
import java.util.List;
*/
public class FindbugsConfiguration implements BatchExtension {
- private Project project;
- private RulesProfile profile;
- private FindbugsProfileExporter exporter;
- private ProjectClasspath projectClasspath;
+ private final Project project;
+ private final Settings settings;
+ private final RulesProfile profile;
+ private final FindbugsProfileExporter exporter;
+ private final ProjectClasspath projectClasspath;
- public FindbugsConfiguration(Project project, RulesProfile profile, FindbugsProfileExporter exporter, ProjectClasspath classpath) {
+ public FindbugsConfiguration(Project project, Settings settings, RulesProfile profile, FindbugsProfileExporter exporter, ProjectClasspath classpath) {
this.project = project;
+ this.settings = settings;
this.profile = profile;
this.exporter = exporter;
this.projectClasspath = classpath;
}
+ @CheckForNull
public File getTargetXMLReport() {
- if (project.getConfiguration().getBoolean(FindbugsConstants.GENERATE_XML_KEY, FindbugsConstants.GENERATE_XML_DEFAULT_VALUE)) {
+ if (settings.getBoolean(FindbugsConstants.GENERATE_XML_KEY)) {
return new File(project.getFileSystem().getSonarWorkingDirectory(), "findbugs-result.xml");
}
return null;
public List<File> getExcludesFilters() {
List<File> result = new ArrayList<File>();
- String[] filters = project.getConfiguration().getStringArray(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY);
+ String[] filters = settings.getStringArray(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY);
for (String excludesFilterPath : filters) {
excludesFilterPath = StringUtils.trim(excludesFilterPath);
if (StringUtils.isNotBlank(excludesFilterPath)) {
}
public String getEffort() {
- return StringUtils.lowerCase(project.getConfiguration().getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY,
- CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE));
+ return StringUtils.lowerCase(settings.getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY));
}
public long getTimeout() {
- return project.getConfiguration().getLong(CoreProperties.FINDBUGS_TIMEOUT_PROPERTY, CoreProperties.FINDBUGS_TIMEOUT_DEFAULT_VALUE);
+ return settings.getLong(CoreProperties.FINDBUGS_TIMEOUT_PROPERTY);
}
private File jsr305Lib;
throw new SonarException(e);
}
}
+
}
*/
package org.sonar.plugins.findbugs;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
-import org.sonar.api.utils.Logs;
import java.io.File;
import java.util.List;
public class FindbugsSensor implements Sensor {
+
+ private static final Logger LOG = LoggerFactory.getLogger(FindbugsSensor.class);
+
private RulesProfile profile;
private RuleFinder ruleFinder;
private FindbugsExecutor executor;
public void analyse(Project project, SensorContext context) {
if (project.getReuseExistingRulesConfig()) {
- Logs.INFO.warn("Reusing existing Findbugs configuration not supported any more.");
+ LOG.warn("Reusing existing Findbugs configuration not supported any more.");
}
File report = getFindbugsReportFile(project);
if (report == null) {
report = executor.execute();
}
FindbugsXmlReportParser reportParser = new FindbugsXmlReportParser(report);
- List<FindbugsXmlReportParser.Violation> fbViolations = reportParser.getViolations();
- for (FindbugsXmlReportParser.Violation fbViolation : fbViolations) {
- Rule rule = ruleFinder.findByKey(FindbugsConstants.REPOSITORY_KEY, fbViolation.getType());
- if (rule != null) { // ignore violations from report, if rule not activated in Sonar
- JavaFile resource = new JavaFile(fbViolation.getSonarJavaFileKey());
- if (context.getResource(resource) != null) {
- Violation violation = Violation.create(rule, resource).setLineId(fbViolation.getStart()).setMessage(fbViolation.getLongMessage());
- context.saveViolation(violation);
- }
- } else {
- Logs.INFO.warn("Findbugs rule '{}' not active in Sonar.", fbViolation.getType());
+ List<FindbugsXmlReportParser.XmlBugInstance> bugInstances = reportParser.getBugInstances();
+
+ for (FindbugsXmlReportParser.XmlBugInstance bugInstance : bugInstances) {
+ FindbugsXmlReportParser.XmlSourceLineAnnotation sourceLine = bugInstance.getPrimarySourceLine();
+ if (sourceLine == null) {
+ LOG.warn("No source line for " + bugInstance.getType());
+ continue;
+ }
+
+ Rule rule = ruleFinder.findByKey(FindbugsConstants.REPOSITORY_KEY, bugInstance.getType());
+ if (rule == null) {
+ // ignore violations from report, if rule not activated in Sonar
+ LOG.warn("Findbugs rule '{}' not active in Sonar.", bugInstance.getType());
+ continue;
+ }
+
+ JavaFile resource = new JavaFile(sourceLine.getSonarJavaFileKey());
+ if (context.getResource(resource) != null) {
+ Violation violation = Violation.create(rule, resource)
+ .setLineId(sourceLine.getStart())
+ .setMessage(bugInstance.getLongMessage());
+ context.saveViolation(violation);
}
}
}
*/
package org.sonar.plugins.findbugs;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamException;
-
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
import org.codehaus.staxmate.SMInputFactory;
import org.codehaus.staxmate.in.SMInputCursor;
import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.XmlParserException;
+import javax.annotation.CheckForNull;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+
+import java.io.File;
+import java.util.List;
+
class FindbugsXmlReportParser {
private final File findbugsXmlReport;
public FindbugsXmlReportParser(File findbugsXmlReport) {
this.findbugsXmlReport = findbugsXmlReport;
findbugsXmlReportPath = findbugsXmlReport.getAbsolutePath();
- if ( !findbugsXmlReport.exists()) {
+ if (!findbugsXmlReport.exists()) {
throw new SonarException("The findbugs XML report can't be found at '" + findbugsXmlReportPath + "'");
}
}
- public List<Violation> getViolations() {
- List<Violation> violations = new ArrayList<Violation>();
+ public List<XmlBugInstance> getBugInstances() {
+ List<XmlBugInstance> result = Lists.newArrayList();
try {
SMInputFactory inf = new SMInputFactory(XMLInputFactory.newInstance());
SMInputCursor cursor = inf.rootElementCursor(findbugsXmlReport).advance();
SMInputCursor bugInstanceCursor = cursor.childElementCursor("BugInstance").advance();
while (bugInstanceCursor.asEvent() != null) {
- String type = bugInstanceCursor.getAttrValue("type");
- String longMessage = "";
+ XmlBugInstance xmlBugInstance = new XmlBugInstance();
+ xmlBugInstance.type = bugInstanceCursor.getAttrValue("type");
+ xmlBugInstance.longMessage = "";
+ result.add(xmlBugInstance);
+
+ ImmutableList.Builder<XmlSourceLineAnnotation> lines = ImmutableList.builder();
SMInputCursor bugInstanceChildCursor = bugInstanceCursor.childElementCursor().advance();
while (bugInstanceChildCursor.asEvent() != null) {
String nodeName = bugInstanceChildCursor.getLocalName();
if ("LongMessage".equals(nodeName)) {
- longMessage = bugInstanceChildCursor.collectDescendantText();
+ xmlBugInstance.longMessage = bugInstanceChildCursor.collectDescendantText();
} else if ("SourceLine".equals(nodeName)) {
- Violation fbViolation = new Violation();
- fbViolation.type = type;
- fbViolation.longMessage = longMessage;
- fbViolation.parseStart(bugInstanceChildCursor.getAttrValue("start"));
- fbViolation.parseEnd(bugInstanceChildCursor.getAttrValue("end"));
- fbViolation.className = bugInstanceChildCursor.getAttrValue("classname");
- fbViolation.sourcePath = bugInstanceChildCursor.getAttrValue("sourcepath");
- violations.add(fbViolation);
+ XmlSourceLineAnnotation xmlSourceLineAnnotation = new XmlSourceLineAnnotation();
+ xmlSourceLineAnnotation.parseStart(bugInstanceChildCursor.getAttrValue("start"));
+ xmlSourceLineAnnotation.parseEnd(bugInstanceChildCursor.getAttrValue("end"));
+ xmlSourceLineAnnotation.parsePrimary(bugInstanceChildCursor.getAttrValue("primary"));
+ xmlSourceLineAnnotation.className = bugInstanceChildCursor.getAttrValue("classname");
+ lines.add(xmlSourceLineAnnotation);
}
bugInstanceChildCursor.advance();
}
+ xmlBugInstance.sourceLines = lines.build();
bugInstanceCursor.advance();
}
cursor.getStreamReader().closeCompletely();
} catch (XMLStreamException e) {
throw new XmlParserException("Unable to parse the Findbugs XML Report '" + findbugsXmlReportPath + "'", e);
}
- return violations;
+ return result;
}
- public static class Violation {
-
+ public static class XmlBugInstance {
private String type;
private String longMessage;
- private Integer start;
- private Integer end;
- protected String className;
- protected String sourcePath;
+ private List<XmlSourceLineAnnotation> sourceLines;
public String getType() {
return type;
}
+ public String getLongMessage() {
+ return longMessage;
+ }
+
+ @CheckForNull
+ public XmlSourceLineAnnotation getPrimarySourceLine() {
+ for (XmlSourceLineAnnotation sourceLine : sourceLines) {
+ if (sourceLine.isPrimary()) {
+ // According to source code of Findbugs 2.0 - should be exactly one primary
+ return sourceLine;
+ }
+ }
+ // As a last resort - return first line
+ return sourceLines.isEmpty() ? null : sourceLines.get(0);
+ }
+
+ }
+
+ public static class XmlSourceLineAnnotation {
+ private boolean primary;
+ private Integer start;
+ private Integer end;
+ @VisibleForTesting
+ protected String className;
+
public void parseStart(String attrValue) {
try {
start = Integer.parseInt(attrValue);
}
}
- public String getLongMessage() {
- return longMessage;
+ public void parsePrimary(String attrValue) {
+ primary = Boolean.parseBoolean(attrValue);
+ }
+
+ public boolean isPrimary() {
+ return primary;
}
public Integer getStart() {
return className;
}
- public String getSourcePath() {
- return sourcePath;
- }
-
public String getSonarJavaFileKey() {
if (className.indexOf('$') > -1) {
return className.substring(0, className.indexOf('$'));
}
return className;
}
+
}
}
*/
package org.sonar.plugins.findbugs;
-import static org.junit.Assert.assertThat;
-
-import org.hamcrest.core.Is;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import static org.fest.assertions.Assertions.assertThat;
+
public class FindbugsAntConverterTest {
@Test
}
private void assertAntPatternEqualsToFindBugsRegExp(String antPattern, String regExp, String example) {
- assertThat(FindbugsAntConverter.antToJavaRegexpConvertor(antPattern), Is.is(regExp));
+ assertThat(FindbugsAntConverter.antToJavaRegexpConvertor(antPattern)).isEqualTo(regExp);
String javaRegexp = regExp.substring(1, regExp.length());
assertJavaRegexpResult(javaRegexp, example, true);
}
private void assertJavaRegexpResult(String javaRegexp, String example, boolean expectedResult) {
Pattern pattern = Pattern.compile(javaRegexp);
Matcher matcher = pattern.matcher(example);
- assertThat(example + " tested with pattern " + javaRegexp, matcher.matches(), Is.is(expectedResult));
+ assertThat(matcher.matches()).as(example + " tested with pattern " + javaRegexp).isEqualTo(expectedResult);
}
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import org.apache.commons.configuration.BaseConfiguration;
-import org.apache.commons.configuration.Configuration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
import org.sonar.api.test.SimpleProjectFileSystem;
import java.io.File;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
public class FindbugsConfigurationTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private Project project;
+ private Settings settings;
private File findbugsTempDir;
@Before
public void setup() {
project = mock(Project.class);
+ settings = new Settings();
findbugsTempDir = tempFolder.newFolder("findbugs");
when(project.getFileSystem()).thenReturn(new SimpleProjectFileSystem(findbugsTempDir));
}
@Test
public void shouldSaveConfigFiles() throws Exception {
- FindbugsConfiguration conf = new FindbugsConfiguration(project, RulesProfile.create(), new FindbugsProfileExporter(), null);
+ FindbugsConfiguration conf = new FindbugsConfiguration(project, settings, RulesProfile.create(), new FindbugsProfileExporter(), null);
conf.saveIncludeConfigXml();
conf.saveExcludeConfigXml();
File findbugsIncludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-include.xml");
File findbugsExcludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-exclude.xml");
- assertThat(findbugsIncludeFile.exists(), is(true));
- assertThat(findbugsExcludeFile.exists(), is(true));
+ assertThat(findbugsIncludeFile.exists()).isTrue();
+ assertThat(findbugsExcludeFile.exists()).isTrue();
}
@Test
public void shouldReturnExcludesFilters() {
- Configuration projectConfiguration = new BaseConfiguration();
- when(project.getConfiguration()).thenReturn(projectConfiguration);
- FindbugsConfiguration conf = new FindbugsConfiguration(project, RulesProfile.create(), new FindbugsProfileExporter(), null);
+ FindbugsConfiguration conf = new FindbugsConfiguration(project, settings, RulesProfile.create(), new FindbugsProfileExporter(), null);
- assertThat(conf.getExcludesFilters().size(), is(0));
- projectConfiguration.setProperty(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, " foo.xml , bar.xml,");
- assertThat(conf.getExcludesFilters().size(), is(2));
+ assertThat(conf.getExcludesFilters()).isEmpty();
+ settings.setProperty(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, " foo.xml , bar.xml,");
+ assertThat(conf.getExcludesFilters()).hasSize(2);
}
+
}
import java.io.File;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.internal.matchers.StringContains.containsString;
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
new FindbugsExecutor(conf).execute();
- assertThat(reportFile.exists(), is(true));
+ assertThat(reportFile.exists()).isTrue();
String report = FileUtils.readFileToString(reportFile);
- assertThat("Report should contain bug instance", report, containsString("<BugInstance"));
- assertThat("Report should be generated with messages", report, containsString("<Message>"));
- assertThat(report, containsString("synthetic=\"true\""));
+ assertThat(report).as("Report should contain bug instance").contains("<BugInstance");
+ assertThat(report).as("Report should be generated with messages").contains("<Message>");
+ assertThat(report).contains("synthetic=\"true\"");
}
@Test(expected = SonarException.class)
Project project = mock(Project.class);
ProjectFileSystem fs = mock(ProjectFileSystem.class);
when(project.getFileSystem()).thenReturn(fs);
- FindbugsConfiguration conf = new FindbugsConfiguration(project, null, null, null);
+ FindbugsConfiguration conf = new FindbugsConfiguration(project, null, null, null, null);
new FindbugsExecutor(conf).execute();
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.apache.commons.configuration.Configuration;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.resources.Project;
+import org.sonar.api.test.MavenTestUtils;
+
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import org.apache.commons.configuration.Configuration;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.resources.Project;
-import org.sonar.api.test.MavenTestUtils;
-
public class FindbugsMavenInitializerTest {
private Project project;
public void shouldGetExcludesFiltersFromPom() {
Project project = MavenTestUtils.loadProjectFromPom(getClass(), "pom.xml");
initializer.execute(project);
- assertThat(project.getConfiguration().getString(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY), is("foo.xml"));
+ assertThat(project.getConfiguration().getString(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)).isEqualTo("foo.xml");
}
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.junit.Assert.assertThat;
-
import org.junit.Test;
+import static org.fest.assertions.Assertions.assertThat;
+
public class FindbugsPluginTest {
@Test
public void testGetExtensions() {
- assertThat(new FindbugsPlugin().getExtensions().size(), greaterThan(1));
+ assertThat(new FindbugsPlugin().getExtensions().size()).isGreaterThan(1);
}
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-import java.io.IOException;
-import java.io.StringWriter;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
import org.junit.Test;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.plugins.findbugs.xml.Match;
import org.xml.sax.SAXException;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
public class FindbugsProfileExporterTest extends FindbugsTests {
private FindbugsProfileExporter exporter = new FindbugsProfileExporter();
@Test
public void shouldBuildOnlyOneModuleWhenNoActiveRules() {
FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Collections.<ActiveRule> emptyList());
- assertThat(filter.getMatchs().size(), is(0));
+ assertThat(filter.getMatchs()).hasSize(0);
}
@Test
FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2));
List<Match> matches = filter.getMatchs();
- assertThat(matches.size(), is(2));
+ assertThat(matches).hasSize(2);
- assertThat(matches.get(0).getBug().getPattern(), is("DLS_DEAD_LOCAL_STORE"));
- assertThat(matches.get(1).getBug().getPattern(), is("SS_SHOULD_BE_STATIC"));
+ assertThat(matches.get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");
+ assertThat(matches.get(1).getBug().getPattern()).isEqualTo("SS_SHOULD_BE_STATIC");
}
@Test
ActiveRule activeRule2 = anActiveRuleFromAnotherPlugin();
FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2));
- assertThat(filter.getMatchs().size(), is(0));
+ assertThat(filter.getMatchs()).hasSize(0);
}
@Test
ActiveRule activeRule = anActiveRule(DLS_DEAD_LOCAL_STORE);
FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule));
- assertThat(filter.getMatchs().size(), is(1));
- assertThat(filter.getMatchs().get(0).getBug().getPattern(), is("DLS_DEAD_LOCAL_STORE"));
+ assertThat(filter.getMatchs()).hasSize(1);
+ assertThat(filter.getMatchs().get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");
}
@Test
import java.io.StringReader;
import java.util.List;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class FindbugsProfileImporterTest {
String findbugsConf = TestUtils.getResourceContent("/org/sonar/plugins/findbugs/shouldImportPatterns.xml");
RulesProfile profile = importer.importProfile(new StringReader(findbugsConf), ValidationMessages.create());
- assertThat(profile.getActiveRules().size(), is(2));
- assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "NP_CLOSING_NULL"), is(notNullValue()));
- assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE"), is(notNullValue()));
+ assertThat(profile.getActiveRules()).hasSize(2);
+ assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "NP_CLOSING_NULL")).isNotNull();
+ assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE")).isNotNull();
}
@Test
RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create());
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(18));
- assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "EC_INCOMPATIBLE_ARRAY_COMPARE"), is(notNullValue()));
- assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY"), is(notNullValue()));
+ assertThat(results).hasSize(18);
+ assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "EC_INCOMPATIBLE_ARRAY_COMPARE")).isNotNull();
+ assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY")).isNotNull();
}
@Test
RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create());
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(182));
- assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE"), is(notNullValue()));
+ assertThat(results).hasSize(182);
+ assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")).isNotNull();
}
@Test
RulesProfile profile = importer.importProfile(new InputStreamReader(input), ValidationMessages.create());
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(11));
- assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE"), is(notNullValue()));
+ assertThat(results).hasSize(11);
+ assertThat(profile.getActiveRule(FindbugsConstants.REPOSITORY_KEY, "RC_REF_COMPARISON_BAD_PRACTICE")).isNotNull();
}
@Test
FindBugsFilter filter = (FindBugsFilter) xStream.fromXML(IOUtils.toString(input));
List<Match> matches = filter.getMatchs();
- assertThat(matches.size(), is(2));
- assertThat(matches.get(0).getBug().getPattern(), is("DLS_DEAD_LOCAL_STORE"));
- assertThat(matches.get(1).getBug().getPattern(), is("URF_UNREAD_FIELD"));
+ assertThat(matches).hasSize(2);
+ assertThat(matches.get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE");
+ assertThat(matches.get(1).getBug().getPattern()).isEqualTo("URF_UNREAD_FIELD");
}
@Test
RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(0));
- assertThat(messages.getErrors().size(), is(1));
+ assertThat(results).hasSize(0);
+ assertThat(messages.getErrors()).hasSize(1);
}
@Test
RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(1));
- assertThat(messages.getWarnings().size(), is(1));
+ assertThat(results).hasSize(1);
+ assertThat(messages.getWarnings()).hasSize(1);
}
@Test
RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(141));
- assertThat(messages.getWarnings().size(), is(1));
+ assertThat(results).hasSize(141);
+ assertThat(messages.getWarnings()).hasSize(1);
}
@Test
RulesProfile profile = importer.importProfile(new StringReader(uncorrectFindbugsXml), messages);
List<ActiveRule> results = profile.getActiveRules();
- assertThat(results.size(), is(9));
- assertThat(messages.getWarnings().size(), is(1));
+ assertThat(results).hasSize(9);
+ assertThat(messages.getWarnings()).hasSize(1);
}
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-
-import java.util.List;
-
import org.junit.Test;
import org.sonar.api.platform.ServerFileSystem;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.XMLRuleParser;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
public class FindbugsRuleRepositoryTest {
@Test
ServerFileSystem fileSystem = mock(ServerFileSystem.class);
FindbugsRuleRepository repository = new FindbugsRuleRepository(fileSystem, new XMLRuleParser());
List<Rule> rules = repository.createRules();
- assertThat(rules.size(), greaterThan(300));
+ assertThat(rules.size()).isGreaterThan(300);
for (Rule rule : rules) {
- assertNotNull(rule.getKey());
- assertNotNull(rule.getConfigKey());
- assertNotNull(rule.getName());
+ assertThat(rule.getKey()).isNotNull();
+ assertThat(rule.getConfigKey()).isNotNull();
+ assertThat(rule.getName()).isNotNull();
}
}
+
}
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class FindbugsSensorTest extends FindbugsTests {
analyser.analyse(project, context);
verify(executor).execute();
- verify(context, times(3)).saveViolation(any(Violation.class));
+ verify(context, times(2)).saveViolation(any(Violation.class));
Violation wanted = Violation.create((Rule) null, new JavaFile("org.sonar.commons.ZipUtils")).setMessage(
"Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)").setLineId(107);
-
verify(context).saveViolation(argThat(new IsViolation(wanted)));
wanted = Violation.create((Rule) null, new JavaFile("org.sonar.commons.resources.MeasuresDao")).setMessage(
"The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class").setLineId(56);
-
verify(context).saveViolation(argThat(new IsViolation(wanted)));
}
analyser.analyse(project, context);
verify(executor, never()).execute();
- verify(context, times(3)).saveViolation(any(Violation.class));
+ verify(context, times(2)).saveViolation(any(Violation.class));
Violation wanted = Violation.create((Rule) null, new JavaFile("org.sonar.commons.ZipUtils")).setMessage(
"Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)").setLineId(107);
-
verify(context).saveViolation(argThat(new IsViolation(wanted)));
wanted = Violation.create((Rule) null, new JavaFile("org.sonar.commons.resources.MeasuresDao")).setMessage(
"The class org.sonar.commons.resources.MeasuresDao$1 could be refactored into a named _static_ inner class").setLineId(56);
-
verify(context).saveViolation(argThat(new IsViolation(wanted)));
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.greaterThan;
-
import org.junit.Test;
+import static org.fest.assertions.Assertions.assertThat;
+
public class FindbugsVersionTest {
@Test
public void getFindbugsVersion() {
- assertThat(FindbugsVersion.getVersion().length(), greaterThan(1));
+ assertThat(FindbugsVersion.getVersion().length()).isGreaterThan(1);
}
}
*/
package org.sonar.plugins.findbugs;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.SonarException;
import java.io.File;
import java.net.URISyntaxException;
import java.util.List;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.utils.SonarException;
+import static org.fest.assertions.Assertions.assertThat;
public class FindbugsXmlReportParserTest {
- private List<FindbugsXmlReportParser.Violation> violations;
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private List<FindbugsXmlReportParser.XmlBugInstance> violations;
@Before
public void init() {
File findbugsXmlReport = getFile("/org/sonar/plugins/findbugs/findbugsReport.xml");
FindbugsXmlReportParser xmlParser = new FindbugsXmlReportParser(findbugsXmlReport);
- violations = xmlParser.getViolations();
+ violations = xmlParser.getBugInstances();
}
- @Test(expected = SonarException.class)
+ @Test
public void createFindbugsXmlReportParserWithUnexistedReportFile() {
File xmlReport = new File("doesntExist.xml");
+ thrown.expect(SonarException.class);
+ thrown.expectMessage("The findbugs XML report can't be found at '" + xmlReport.getAbsolutePath() + "'");
new FindbugsXmlReportParser(xmlReport);
}
@Test
public void testGetViolations() {
- assertThat(violations.size(), is(3));
+ assertThat(violations.size()).isEqualTo(2);
+
+ FindbugsXmlReportParser.XmlBugInstance fbViolation = violations.get(0);
+ assertThat(fbViolation.getType()).isEqualTo("AM_CREATES_EMPTY_ZIP_FILE_ENTRY");
+ assertThat(fbViolation.getLongMessage()).isEqualTo("Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)");
- FindbugsXmlReportParser.Violation fbViolation = violations.get(0);
- assertThat(fbViolation.getType(), is("AM_CREATES_EMPTY_ZIP_FILE_ENTRY"));
- assertThat(fbViolation.getLongMessage(),
- is("Empty zip file entry created in org.sonar.commons.ZipUtils._zip(String, File, ZipOutputStream)"));
- assertThat(fbViolation.getStart(), is(107));
- assertThat(fbViolation.getEnd(), is(107));
- assertThat(fbViolation.getClassName(), is("org.sonar.commons.ZipUtils"));
- assertThat(fbViolation.getSourcePath(), is("org/sonar/commons/ZipUtils.java"));
+ FindbugsXmlReportParser.XmlSourceLineAnnotation sourceLine = fbViolation.getPrimarySourceLine();
+ assertThat(sourceLine.getStart()).isEqualTo(107);
+ assertThat(sourceLine.getEnd()).isEqualTo(107);
+ assertThat(sourceLine.getClassName()).isEqualTo("org.sonar.commons.ZipUtils");
}
@Test
public void testGetSonarJavaFileKey() {
- FindbugsXmlReportParser.Violation violation = new FindbugsXmlReportParser.Violation();
- violation.className = "org.sonar.batch.Sensor";
- assertThat(violation.getSonarJavaFileKey(), is("org.sonar.batch.Sensor"));
- violation.className = "Sensor";
- assertThat(violation.getSonarJavaFileKey(), is("Sensor"));
- violation.className = "org.sonar.batch.Sensor$1";
- assertThat(violation.getSonarJavaFileKey(), is("org.sonar.batch.Sensor"));
+ FindbugsXmlReportParser.XmlSourceLineAnnotation sourceLine = new FindbugsXmlReportParser.XmlSourceLineAnnotation();
+ sourceLine.className = "org.sonar.batch.Sensor";
+ assertThat(sourceLine.getSonarJavaFileKey()).isEqualTo("org.sonar.batch.Sensor");
+ sourceLine.className = "Sensor";
+ assertThat(sourceLine.getSonarJavaFileKey()).isEqualTo("Sensor");
+ sourceLine.className = "org.sonar.batch.Sensor$1";
+ assertThat(sourceLine.getSonarJavaFileKey()).isEqualTo("org.sonar.batch.Sensor");
}
private final File getFile(String filename) {
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.utils.ValidationMessages;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class SonarWayWithFindbugsProfileTest {
SonarWayWithFindbugsProfile sonarWayWithFindbugs = new SonarWayWithFindbugsProfile(importer);
ValidationMessages validation = ValidationMessages.create();
RulesProfile profile = sonarWayWithFindbugs.createProfile(validation);
- assertThat(profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY).size(), is(399));
- assertThat(validation.hasErrors(), is(false));
+ assertThat(profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY)).hasSize(399);
+ assertThat(validation.hasErrors()).isFalse();
}
+
}