You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FindbugsMavenPluginHandlerTest.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.findbugs;
  21. import org.apache.commons.configuration.Configuration;
  22. import org.apache.maven.project.MavenProject;
  23. import static org.hamcrest.CoreMatchers.is;
  24. import org.hamcrest.core.Is;
  25. import static org.hamcrest.text.StringEndsWith.endsWith;
  26. import static org.junit.Assert.assertThat;
  27. import org.junit.Before;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import static org.mockito.Matchers.anyObject;
  31. import static org.mockito.Matchers.anyString;
  32. import static org.mockito.Matchers.argThat;
  33. import static org.mockito.Matchers.eq;
  34. import static org.mockito.Mockito.*;
  35. import org.junit.rules.TemporaryFolder;
  36. import org.sonar.api.CoreProperties;
  37. import org.sonar.api.batch.maven.MavenPlugin;
  38. import org.sonar.api.profiles.RulesProfile;
  39. import org.sonar.api.resources.Project;
  40. import org.sonar.api.resources.ProjectFileSystem;
  41. import org.sonar.api.test.SimpleProjectFileSystem;
  42. import org.sonar.api.utils.SonarException;
  43. import java.io.File;
  44. import java.io.IOException;
  45. import java.net.URISyntaxException;
  46. import java.util.regex.Matcher;
  47. import java.util.regex.Pattern;
  48. public class FindbugsMavenPluginHandlerTest {
  49. @Rule
  50. public TemporaryFolder tempFolder = new TemporaryFolder();
  51. private Project project;
  52. private ProjectFileSystem fs;
  53. private File fakeSonarConfig;
  54. private MavenPlugin plugin;
  55. private FindbugsMavenPluginHandler handler;
  56. private File findbugsTempDir;
  57. @Before
  58. public void setup() {
  59. project = mock(Project.class);
  60. fs = mock(ProjectFileSystem.class);
  61. fakeSonarConfig = mock(File.class);
  62. plugin = mock(MavenPlugin.class);
  63. handler = createMavenPluginHandler();
  64. findbugsTempDir = tempFolder.newFolder("findbugs");
  65. }
  66. @Test
  67. public void doOverrideConfig() throws Exception {
  68. setupConfig();
  69. handler.configureFilters(project, plugin);
  70. verify(plugin).setParameter("includeFilterFile", "fakeSonarConfig.xml");
  71. }
  72. @Test
  73. public void doReuseExistingRulesConfig() throws Exception {
  74. setupConfig();
  75. // See sonar 583
  76. when(project.getReuseExistingRulesConfig()).thenReturn(true);
  77. when(plugin.getParameter("excludeFilterFile")).thenReturn("existingConfig.xml");
  78. handler.configureFilters(project, plugin);
  79. verify(plugin, never()).setParameter(eq("includeFilterFile"), anyString());
  80. setupConfig();
  81. when(project.getReuseExistingRulesConfig()).thenReturn(true);
  82. when(plugin.getParameter("includeFilterFile")).thenReturn("existingConfig.xml");
  83. handler.configureFilters(project, plugin);
  84. verify(plugin, never()).setParameter(eq("includeFilterFile"), anyString());
  85. }
  86. private void setupConfig() throws IOException {
  87. when(fakeSonarConfig.getCanonicalPath()).thenReturn("fakeSonarConfig.xml");
  88. when(project.getFileSystem()).thenReturn(fs);
  89. when(fs.writeToWorkingDirectory(anyString(), anyString())).thenReturn(fakeSonarConfig);
  90. }
  91. @Test
  92. public void shoulConfigurePlugin() throws URISyntaxException, IOException {
  93. mockProject(CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE);
  94. handler.configure(project, plugin);
  95. verify(plugin).setParameter("skip", "false");
  96. verify(plugin).setParameter("xmlOutput", "true");
  97. verify(plugin).setParameter("threshold", "Low");
  98. verify(plugin).setParameter("effort", CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE, false);
  99. verify(plugin).setParameter(eq("classFilesDirectory"), anyString());
  100. verify(plugin).setParameter(eq("includeFilterFile"), argThat(endsWith("findbugs-include.xml")));
  101. assertFindbugsIncludeFileIsSaved();
  102. }
  103. @Test(expected = SonarException.class)
  104. public void shoulFailIfNoCompiledClasses() throws URISyntaxException, IOException {
  105. when(project.getFileSystem()).thenReturn(fs);
  106. handler.configure(project, plugin);
  107. }
  108. @Test
  109. public void shouldConfigureEffort() throws URISyntaxException, IOException {
  110. FindbugsMavenPluginHandler handler = createMavenPluginHandler();
  111. mockProject("EffortSetInPom");
  112. MavenPlugin plugin = mock(MavenPlugin.class);
  113. handler.configure(project, plugin);
  114. verify(plugin).setParameter("effort", "EffortSetInPom", false);
  115. }
  116. @Test
  117. public void shouldConvertAntToJavaRegexp() {
  118. // see SONAR-853
  119. assertAntPatternEqualsToFindBugsRegExp("?", "~.", "g");
  120. assertAntPatternEqualsToFindBugsRegExp("*/myClass.JaVa", "~([^\\\\^\\s]*\\.)?myClass", "foo.bar.test.myClass");
  121. assertAntPatternEqualsToFindBugsRegExp("*/myClass.java", "~([^\\\\^\\s]*\\.)?myClass", "foo.bar.test.myClass");
  122. assertAntPatternEqualsToFindBugsRegExp("*/myClass2.jav", "~([^\\\\^\\s]*\\.)?myClass2", "foo.bar.test.myClass2");
  123. assertAntPatternEqualsToFindBugsRegExp("*/myOtherClass", "~([^\\\\^\\s]*\\.)?myOtherClass", "foo.bar.test.myOtherClass");
  124. assertAntPatternEqualsToFindBugsRegExp("*", "~[^\\\\^\\s]*", "ga.%#123_(*");
  125. assertAntPatternEqualsToFindBugsRegExp("**", "~.*", "gd.3reqg.3151];9#@!");
  126. assertAntPatternEqualsToFindBugsRegExp("**/generated/**", "~(.*\\.)?generated\\..*", "!@$Rq/32T$).generated.##TR.e#@!$");
  127. assertAntPatternEqualsToFindBugsRegExp("**/cl*nt/*", "~(.*\\.)?cl[^\\\\^\\s]*nt\\.[^\\\\^\\s]*", "!#$_.clr31r#!$(nt.!#$QRW)(.");
  128. assertAntPatternEqualsToFindBugsRegExp("**/org/apache/commons/**", "~(.*\\.)?org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
  129. assertAntPatternEqualsToFindBugsRegExp("*/org/apache/commons/**", "~([^\\\\^\\s]*\\.)?org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
  130. assertAntPatternEqualsToFindBugsRegExp("org/apache/commons/**", "~org\\.apache\\.commons\\..*", "org.apache.commons.httpclient.contrib.ssl");
  131. }
  132. @Test
  133. public void shouldntMatchThoseClassPattern() {
  134. // see SONAR-853
  135. assertJavaRegexpResult("[^\\\\^\\s]", "fad f.ate 12#)", false);
  136. }
  137. private void assertAntPatternEqualsToFindBugsRegExp(String antPattern, String regExp, String example) {
  138. assertThat(FindbugsAntConverter.antToJavaRegexpConvertor(antPattern), Is.is(regExp));
  139. String javaRegexp = regExp.substring(1, regExp.length());
  140. assertJavaRegexpResult(javaRegexp, example, true);
  141. }
  142. private void assertJavaRegexpResult(String javaRegexp, String example, boolean expectedResult) {
  143. Pattern pattern = Pattern.compile(javaRegexp);
  144. Matcher matcher = pattern.matcher(example);
  145. assertThat(example + " tested with pattern " + javaRegexp, matcher.matches(), Is.is(expectedResult));
  146. }
  147. private void assertFindbugsIncludeFileIsSaved() {
  148. File findbugsIncludeFile = new File(findbugsTempDir + "/target/sonar/findbugs-include.xml");
  149. assertThat(findbugsIncludeFile.exists(), is(true));
  150. }
  151. private FindbugsMavenPluginHandler createMavenPluginHandler() {
  152. return new FindbugsMavenPluginHandler(RulesProfile.create(), new FindbugsProfileExporter());
  153. }
  154. private void mockProject(String effort) throws URISyntaxException, IOException {
  155. when(project.getPom()).thenReturn(new MavenProject());
  156. when(project.getFileSystem()).thenReturn(new SimpleProjectFileSystem(findbugsTempDir));
  157. Configuration conf = mock(Configuration.class);
  158. when(project.getConfiguration()).thenReturn(conf);
  159. when(conf.getString(eq(CoreProperties.FINDBUGS_EFFORT_PROPERTY), anyString())).thenReturn(effort);
  160. }
  161. }