]> source.dussan.org Git - sonarqube.git/commitdiff
Improve coverage of FindbugsConfiguration
authorEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 9 Jul 2012 08:07:06 +0000 (14:07 +0600)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 9 Jul 2012 08:07:06 +0000 (14:07 +0600)
plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java

index a71516c92f648a8af35ff8bfb53116719c15606f..10de177c4d908783b80501e2449567a618a54ddf 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.findbugs;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
@@ -83,13 +84,15 @@ public class FindbugsConfiguration implements BatchExtension {
     return findbugsProject;
   }
 
-  public File saveIncludeConfigXml() throws IOException {
+  @VisibleForTesting
+  File saveIncludeConfigXml() throws IOException {
     StringWriter conf = new StringWriter();
     exporter.exportProfile(profile, conf);
     return project.getFileSystem().writeToWorkingDirectory(conf.toString(), "findbugs-include.xml");
   }
 
-  public File saveExcludeConfigXml() throws IOException {
+  @VisibleForTesting
+  File saveExcludeConfigXml() throws IOException {
     FindBugsFilter findBugsFilter = new FindBugsFilter();
     if (project.getExclusionPatterns() != null) {
       for (String exclusion : project.getExclusionPatterns()) {
@@ -100,7 +103,8 @@ public class FindbugsConfiguration implements BatchExtension {
     return project.getFileSystem().writeToWorkingDirectory(findBugsFilter.toXml(), "findbugs-exclude.xml");
   }
 
-  public List<File> getExcludesFilters() {
+  @VisibleForTesting
+  List<File> getExcludesFilters() {
     List<File> result = new ArrayList<File>();
     String[] filters = settings.getStringArray(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY);
     for (String excludesFilterPath : filters) {
index 0465c58174d16c67f086d1aadd4a9bd7057aa8b3..10073c87003f050f93f17c05e53ed511b18d2e91 100644 (file)
  */
 package org.sonar.plugins.findbugs;
 
+import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
 import org.sonar.api.profiles.RulesProfile;
 import org.sonar.api.resources.Project;
@@ -42,32 +45,64 @@ public class FindbugsConfigurationTest {
   private Project project;
   private Settings settings;
   private File findbugsTempDir;
+  private FindbugsConfiguration conf;
 
   @Before
-  public void setup() {
+  public void setUp() {
     project = mock(Project.class);
-    settings = new Settings();
+    settings = new Settings(new PropertyDefinitions().addComponent(FindbugsPlugin.class));
     findbugsTempDir = tempFolder.newFolder("findbugs");
     when(project.getFileSystem()).thenReturn(new SimpleProjectFileSystem(findbugsTempDir));
+    conf = new FindbugsConfiguration(project, settings, RulesProfile.create(), new FindbugsProfileExporter(), null);
   }
 
   @Test
-  public void shouldSaveConfigFiles() throws Exception {
-    FindbugsConfiguration conf = new FindbugsConfiguration(project, settings, RulesProfile.create(), new FindbugsProfileExporter(), null);
+  public void should_return_report_file() throws Exception {
+    assertThat(conf.getTargetXMLReport()).isEqualTo(new File(findbugsTempDir, "target/sonar/findbugs-result.xml"));
+  }
 
+  @Test
+  public void should_save_include_config() throws Exception {
     conf.saveIncludeConfigXml();
+    File findbugsIncludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-include.xml");
+    assertThat(findbugsIncludeFile.exists()).isTrue();
+  }
+
+  @Test
+  public void should_save_exclude_config() throws Exception {
+    when(project.getExclusionPatterns()).thenReturn(new String[] {"dir/**/*.java"});
     conf.saveExcludeConfigXml();
+    File findbugsExcludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-exclude.xml");
+    assertThat(findbugsExcludeFile.exists()).isTrue();
+    String findbugsExclude = FileUtils.readFileToString(findbugsExcludeFile);
+    assertThat(findbugsExclude).contains("Match");
+  }
 
-    File findbugsIncludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-include.xml");
+  @Test
+  public void should_save_empty_exclude_config() throws Exception {
+    conf.saveExcludeConfigXml();
     File findbugsExcludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-exclude.xml");
-    assertThat(findbugsIncludeFile.exists()).isTrue();
     assertThat(findbugsExcludeFile.exists()).isTrue();
+    String findbugsExclude = FileUtils.readFileToString(findbugsExcludeFile);
+    assertThat(findbugsExclude).doesNotContain("Match");
   }
 
   @Test
-  public void shouldReturnExcludesFilters() {
-    FindbugsConfiguration conf = new FindbugsConfiguration(project, settings, RulesProfile.create(), new FindbugsProfileExporter(), null);
+  public void should_return_effort() {
+    assertThat(conf.getEffort()).as("default effort").isEqualTo("default");
+    settings.setProperty(CoreProperties.FINDBUGS_EFFORT_PROPERTY, "Max");
+    assertThat(conf.getEffort()).isEqualTo("max");
+  }
 
+  @Test
+  public void should_return_timeout() {
+    assertThat(conf.getTimeout()).as("default timeout").isEqualTo(600000);
+    settings.setProperty(CoreProperties.FINDBUGS_TIMEOUT_PROPERTY, 1);
+    assertThat(conf.getTimeout()).isEqualTo(1);
+  }
+
+  @Test
+  public void should_return_excludes_filters() {
     assertThat(conf.getExcludesFilters()).isEmpty();
     settings.setProperty(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY, " foo.xml , bar.xml,");
     assertThat(conf.getExcludesFilters()).hasSize(2);