From db268c6621c34df3ace3f931bc84498e232b3402 Mon Sep 17 00:00:00 2001 From: gtoison Date: Sat, 15 Oct 2022 16:56:01 +0100 Subject: [PATCH] test: exclude integration tests from Infinitest --- infinitest.filters | 1 + .../plugins/findbugs/it/FBContribIT.java | 145 +++---- .../plugins/findbugs/it/FindSecBugsIT.java | 113 ++--- .../plugins/findbugs/it/FindSecBugsJspIT.java | 201 ++++----- .../sonar/plugins/findbugs/it/FindbugsIT.java | 389 +++++++++--------- .../findbugs/it/FindbugsTestSuite.java | 289 ++++++------- .../plugins/findbugs/it/IntegrationTest.java | 22 + .../plugins/findbugs/it/MultiModuleIT.java | 201 ++++----- .../sonar/plugins/findbugs/it/ScalaIT.java | 135 +++--- 9 files changed, 763 insertions(+), 733 deletions(-) create mode 100644 infinitest.filters create mode 100644 src/test/java/org/sonar/plugins/findbugs/it/IntegrationTest.java diff --git a/infinitest.filters b/infinitest.filters new file mode 100644 index 00000000..4bf087c7 --- /dev/null +++ b/infinitest.filters @@ -0,0 +1 @@ +excludeGroups=IntegrationTest \ No newline at end of file diff --git a/src/test/java/org/sonar/plugins/findbugs/it/FBContribIT.java b/src/test/java/org/sonar/plugins/findbugs/it/FBContribIT.java index 4b910417..ee9cb752 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/FBContribIT.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/FBContribIT.java @@ -1,72 +1,73 @@ -/* - * Findbugs :: IT :: Plugin - * Copyright (C) 2014 SonarSource - * sonarqube@googlegroups.com - * - * This program 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. - * - * This program 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 02 - */ -package org.sonar.plugins.findbugs.it; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.sonarqube.ws.Issues.Issue; -import org.sonarqube.ws.client.issues.IssuesService; - -import java.util.List; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.MavenBuild; - -class FBContribIT { - - private static final String PROJECT_KEY = "org.sonar.tests:fb-contrib"; - public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; - - @BeforeEach - public void setupProfile() { - FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Findbugs Contrib Integration Tests", "IT", "java"); - } - - @AfterEach - public void deleteProject() { - FindbugsTestSuite.deleteProject(PROJECT_KEY); - } - - @Test - void test() throws Exception { - MavenBuild build = MavenBuild.create() - .setPom(FindbugsTestSuite.projectPom("simple")) - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "low") - .setCleanPackageSonarGoals(); - orchestrator.executeBuild(build); - - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().projects(PROJECT_KEY)).getIssuesList(); - assertThat(issues).hasSize(3); - - issues = issueClient.search(IssueQuery.create().rules("fb-contrib:IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY")).getIssuesList(); - // SONARJAVA-216: - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getMessage()).isEqualTo("Method Simple.method() uses Properties.put instead of Properties.setProperty"); - - issues = issueClient.search(IssueQuery.create().rules("findsecbugs:PREDICTABLE_RANDOM")).getIssuesList(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getMessage()).isEqualTo("This random generator (java.util.Random) is predictable"); - } -} +/* + * Findbugs :: IT :: Plugin + * Copyright (C) 2014 SonarSource + * sonarqube@googlegroups.com + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.plugins.findbugs.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.sonarqube.ws.Issues.Issue; +import org.sonarqube.ws.client.issues.IssuesService; + +import java.util.List; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.MavenBuild; + +@IntegrationTest +class FBContribIT { + + private static final String PROJECT_KEY = "org.sonar.tests:fb-contrib"; + public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; + + @BeforeEach + public void setupProfile() { + FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Findbugs Contrib Integration Tests", "IT", "java"); + } + + @AfterEach + public void deleteProject() { + FindbugsTestSuite.deleteProject(PROJECT_KEY); + } + + @Test + void test() throws Exception { + MavenBuild build = MavenBuild.create() + .setPom(FindbugsTestSuite.projectPom("simple")) + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "low") + .setCleanPackageSonarGoals(); + orchestrator.executeBuild(build); + + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().projects(PROJECT_KEY)).getIssuesList(); + assertThat(issues).hasSize(3); + + issues = issueClient.search(IssueQuery.create().rules("fb-contrib:IPU_IMPROPER_PROPERTIES_USE_SETPROPERTY")).getIssuesList(); + // SONARJAVA-216: + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getMessage()).isEqualTo("Method Simple.method() uses Properties.put instead of Properties.setProperty"); + + issues = issueClient.search(IssueQuery.create().rules("findsecbugs:PREDICTABLE_RANDOM")).getIssuesList(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getMessage()).isEqualTo("This random generator (java.util.Random) is predictable"); + } +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsIT.java b/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsIT.java index 795fa534..e0897722 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsIT.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsIT.java @@ -1,56 +1,57 @@ -package org.sonar.plugins.findbugs.it; - -import static org.junit.Assert.assertFalse; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.sonarqube.ws.client.qualityprofiles.AddProjectRequest; - -import java.io.File; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.MavenBuild; - -/** - * @author gtoison - * - */ -class FindSecBugsIT { - - private static final String PROJECT_KEY = "com.sonarsource.it.samples:findbugs"; - public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; - - @BeforeEach - public void setupProfile() { - FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Find Sec Bugs Integration Tests", "IT", "java"); - } - - @AfterEach - public void deleteProject() { - FindbugsTestSuite.deleteProject(PROJECT_KEY); - } - - @Test - void noAnalysisWhenProfileDoesNotHaveSpotBugsRules() throws Exception { - // Set an empty Java quality profile for the project - AddProjectRequest addProjectRequest = new AddProjectRequest(); - addProjectRequest.setLanguage("java"); - addProjectRequest.setProject(PROJECT_KEY); - addProjectRequest.setQualityProfile("empty"); - FindbugsTestSuite.qualityProfileClient().addProject(addProjectRequest); - - MavenBuild build = MavenBuild.create() - .setPom(FindbugsTestSuite.projectPom("findbugs")) - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "low") - .setCleanPackageSonarGoals(); - orchestrator.executeBuild(build); - - File projectDir = FindbugsTestSuite.projectPom("findbugs").getParentFile(); - File resultFile = new File(projectDir, "target/sonar/findbugs-result.xml"); - - String message = "The analysis must not run because the findbugs sample project does not contain JSP files and there are no spotbugs rules in the profile"; - assertFalse(message, resultFile.exists()); - } -} +package org.sonar.plugins.findbugs.it; + +import static org.junit.Assert.assertFalse; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.sonarqube.ws.client.qualityprofiles.AddProjectRequest; + +import java.io.File; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.MavenBuild; + +/** + * @author gtoison + * + */ +@IntegrationTest +class FindSecBugsIT { + + private static final String PROJECT_KEY = "com.sonarsource.it.samples:findbugs"; + public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; + + @BeforeEach + public void setupProfile() { + FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Find Sec Bugs Integration Tests", "IT", "java"); + } + + @AfterEach + public void deleteProject() { + FindbugsTestSuite.deleteProject(PROJECT_KEY); + } + + @Test + void noAnalysisWhenProfileDoesNotHaveSpotBugsRules() throws Exception { + // Set an empty Java quality profile for the project + AddProjectRequest addProjectRequest = new AddProjectRequest(); + addProjectRequest.setLanguage("java"); + addProjectRequest.setProject(PROJECT_KEY); + addProjectRequest.setQualityProfile("empty"); + FindbugsTestSuite.qualityProfileClient().addProject(addProjectRequest); + + MavenBuild build = MavenBuild.create() + .setPom(FindbugsTestSuite.projectPom("findbugs")) + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "low") + .setCleanPackageSonarGoals(); + orchestrator.executeBuild(build); + + File projectDir = FindbugsTestSuite.projectPom("findbugs").getParentFile(); + File resultFile = new File(projectDir, "target/sonar/findbugs-result.xml"); + + String message = "The analysis must not run because the findbugs sample project does not contain JSP files and there are no spotbugs rules in the profile"; + assertFalse(message, resultFile.exists()); + } +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsJspIT.java b/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsJspIT.java index 53a4886f..eeb5b45e 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsJspIT.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/FindSecBugsJspIT.java @@ -1,100 +1,101 @@ -package org.sonar.plugins.findbugs.it; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertTrue; - -import org.junit.jupiter.api.Test; -import org.sonarqube.ws.Issues.Issue; -import org.sonarqube.ws.client.issues.IssuesService; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.MavenBuild; - -/** - * @author gtoison - * - */ -class FindSecBugsJspIT { - - private static final String SLING_PROJECT_KEY = "org.sonar.tests:jspc-sling"; - private static final String JETTY_PROJECT_KEY = "org.sonar.tests:jspc-jetty"; - - public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; - - @Test - void jspSlingAnalysis() throws Exception { - FindbugsTestSuite.setupProjectAndProfile(SLING_PROJECT_KEY, "Find Sec Bugs JSP Sling Integration Tests", "IT", "java"); - MavenBuild build = MavenBuild.create() - .setPom(FindbugsTestSuite.projectPom("jspc-sling")) - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "low") - .setGoals("clean package sonar:sonar"); - orchestrator.executeBuild(build); - - File projectDir = FindbugsTestSuite.projectPom("jspc-sling").getParentFile(); - File resultFile = new File(projectDir, "target/sonar/findbugs-result.xml"); - - assertTrue("JSP Sling project must be analyzed", resultFile.exists()); - - IssuesService issueClient = FindbugsTestSuite.issueClient(); - - String indexKey = FindbugsTestSuite.keyForFile(SLING_PROJECT_KEY, "src/main/scripts/index.jsp"); - - List issues = issueClient.search(IssueQuery.create().components(indexKey)).getIssuesList(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getRule()).isEqualTo("findsecbugs-jsp:XSS_REQUEST_PARAMETER_TO_JSP_WRITER"); - assertThat(issues.get(0).getLine()).isEqualTo(18); - - FindbugsTestSuite.deleteProject(SLING_PROJECT_KEY); - } - - @Test - void jspJettyAnalysis() throws Exception { - FindbugsTestSuite.setupProjectAndProfile(JETTY_PROJECT_KEY, "Find Sec Bugs JSP Jetty Integration Tests", "IT", "java"); - - MavenBuild build = MavenBuild.create() - .setPom(FindbugsTestSuite.projectPom("jspc-jetty")) - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "low") - .setGoals("clean package sonar:sonar"); - orchestrator.executeBuild(build); - - File projectDir = FindbugsTestSuite.projectPom("jspc-jetty").getParentFile(); - File resultFile = new File(projectDir, "target/sonar/findbugs-result.xml"); - - assertTrue("JSP Jetty project must be analyzed", resultFile.exists()); - - IssuesService issueClient = FindbugsTestSuite.issueClient(); - - String indexKey = FindbugsTestSuite.keyForFile(JETTY_PROJECT_KEY, "src/main/webapp/WEB-INF/jsp/pages/page1/page1.jsp"); - - // Check that there's an issue on page1.jsp - List issues = new ArrayList<>(issueClient.search(IssueQuery.create().components(indexKey)).getIssuesList()); - Collections.sort(issues, Comparator.comparing(Issue::getRule)); - assertThat(issues).hasSize(2); - assertThat(issues.get(0).getRule()).isEqualTo("findsecbugs-jsp:XSS_JSP_PRINT"); - assertThat(issues.get(0).getLine()).isEqualTo(11); - assertThat(issues.get(1).getRule()).isEqualTo("findsecbugs-jsp:XSS_REQUEST_PARAMETER_TO_JSP_WRITER"); - assertThat(issues.get(1).getLine()).isEqualTo(11); - - - // Check that there's an issue on page2.jsp - indexKey = FindbugsTestSuite.keyForFile(JETTY_PROJECT_KEY, "src/main/webapp/WEB-INF/jsp/pages/page2/page2.jsp"); - - issues = new ArrayList<>(issueClient.search(IssueQuery.create().components(indexKey)).getIssuesList()); - Collections.sort(issues, Comparator.comparing(Issue::getRule)); - assertThat(issues).hasSize(2); - assertThat(issues.get(0).getRule()).isEqualTo("findsecbugs-jsp:XSS_JSP_PRINT"); - assertThat(issues.get(0).getLine()).isEqualTo(11); - assertThat(issues.get(1).getRule()).isEqualTo("findsecbugs-jsp:XSS_REQUEST_PARAMETER_TO_JSP_WRITER"); - assertThat(issues.get(1).getLine()).isEqualTo(11); - - FindbugsTestSuite.deleteProject(JETTY_PROJECT_KEY); - } -} +package org.sonar.plugins.findbugs.it; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; +import org.sonarqube.ws.Issues.Issue; +import org.sonarqube.ws.client.issues.IssuesService; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.MavenBuild; + +/** + * @author gtoison + * + */ +@IntegrationTest +class FindSecBugsJspIT { + + private static final String SLING_PROJECT_KEY = "org.sonar.tests:jspc-sling"; + private static final String JETTY_PROJECT_KEY = "org.sonar.tests:jspc-jetty"; + + public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; + + @Test + void jspSlingAnalysis() throws Exception { + FindbugsTestSuite.setupProjectAndProfile(SLING_PROJECT_KEY, "Find Sec Bugs JSP Sling Integration Tests", "IT", "java"); + MavenBuild build = MavenBuild.create() + .setPom(FindbugsTestSuite.projectPom("jspc-sling")) + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "low") + .setGoals("clean package sonar:sonar"); + orchestrator.executeBuild(build); + + File projectDir = FindbugsTestSuite.projectPom("jspc-sling").getParentFile(); + File resultFile = new File(projectDir, "target/sonar/findbugs-result.xml"); + + assertTrue("JSP Sling project must be analyzed", resultFile.exists()); + + IssuesService issueClient = FindbugsTestSuite.issueClient(); + + String indexKey = FindbugsTestSuite.keyForFile(SLING_PROJECT_KEY, "src/main/scripts/index.jsp"); + + List issues = issueClient.search(IssueQuery.create().components(indexKey)).getIssuesList(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getRule()).isEqualTo("findsecbugs-jsp:XSS_REQUEST_PARAMETER_TO_JSP_WRITER"); + assertThat(issues.get(0).getLine()).isEqualTo(18); + + FindbugsTestSuite.deleteProject(SLING_PROJECT_KEY); + } + + @Test + void jspJettyAnalysis() throws Exception { + FindbugsTestSuite.setupProjectAndProfile(JETTY_PROJECT_KEY, "Find Sec Bugs JSP Jetty Integration Tests", "IT", "java"); + + MavenBuild build = MavenBuild.create() + .setPom(FindbugsTestSuite.projectPom("jspc-jetty")) + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "low") + .setGoals("clean package sonar:sonar"); + orchestrator.executeBuild(build); + + File projectDir = FindbugsTestSuite.projectPom("jspc-jetty").getParentFile(); + File resultFile = new File(projectDir, "target/sonar/findbugs-result.xml"); + + assertTrue("JSP Jetty project must be analyzed", resultFile.exists()); + + IssuesService issueClient = FindbugsTestSuite.issueClient(); + + String indexKey = FindbugsTestSuite.keyForFile(JETTY_PROJECT_KEY, "src/main/webapp/WEB-INF/jsp/pages/page1/page1.jsp"); + + // Check that there's an issue on page1.jsp + List issues = new ArrayList<>(issueClient.search(IssueQuery.create().components(indexKey)).getIssuesList()); + Collections.sort(issues, Comparator.comparing(Issue::getRule)); + assertThat(issues).hasSize(2); + assertThat(issues.get(0).getRule()).isEqualTo("findsecbugs-jsp:XSS_JSP_PRINT"); + assertThat(issues.get(0).getLine()).isEqualTo(11); + assertThat(issues.get(1).getRule()).isEqualTo("findsecbugs-jsp:XSS_REQUEST_PARAMETER_TO_JSP_WRITER"); + assertThat(issues.get(1).getLine()).isEqualTo(11); + + + // Check that there's an issue on page2.jsp + indexKey = FindbugsTestSuite.keyForFile(JETTY_PROJECT_KEY, "src/main/webapp/WEB-INF/jsp/pages/page2/page2.jsp"); + + issues = new ArrayList<>(issueClient.search(IssueQuery.create().components(indexKey)).getIssuesList()); + Collections.sort(issues, Comparator.comparing(Issue::getRule)); + assertThat(issues).hasSize(2); + assertThat(issues.get(0).getRule()).isEqualTo("findsecbugs-jsp:XSS_JSP_PRINT"); + assertThat(issues.get(0).getLine()).isEqualTo(11); + assertThat(issues.get(1).getRule()).isEqualTo("findsecbugs-jsp:XSS_REQUEST_PARAMETER_TO_JSP_WRITER"); + assertThat(issues.get(1).getLine()).isEqualTo(11); + + FindbugsTestSuite.deleteProject(JETTY_PROJECT_KEY); + } +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java b/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java index 18154093..2d187041 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/FindbugsIT.java @@ -1,194 +1,195 @@ -/* - * Findbugs :: IT :: Plugin - * Copyright (C) 2014 SonarSource - * sonarqube@googlegroups.com - * - * This program 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. - * - * This program 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 02 - */ -package org.sonar.plugins.findbugs.it; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.sonar.plugins.findbugs.FindbugsConstants; -import org.sonarqube.ws.Issues.Issue; -import org.sonarqube.ws.client.issues.IssuesService; - -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.List; - -import com.google.common.io.Files; -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.MavenBuild; -import com.sonar.orchestrator.build.SonarScanner; - -class FindbugsIT { - - private static final String PROJECT_KEY = "com.sonarsource.it.samples:findbugs"; - private static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; - - @BeforeEach - public void setupProfile() { - FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Findbugs Integration Tests", "findbugs-it", "java"); - } - - @AfterEach - public void deleteProject() { - FindbugsTestSuite.deleteProject(PROJECT_KEY); - } - - @Test - void analysis() { - MavenBuild build = MavenBuild.create(FindbugsTestSuite.projectPom("findbugs")) - .setCleanPackageSonarGoals() - .setProperty("sonar.dynamicAnalysis", "false"); - orchestrator.executeBuild(build); - - // Check that issue with confidence level lower than high was reported: - IssuesService issueClient = FindbugsTestSuite.issueClient(); - - List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getRule()).isEqualTo("findbugs:NP_DEREFERENCE_OF_READLINE_VALUE"); - assertThat(issues.get(0).getLine()).isEqualTo(13); - - issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs2.java"))).getIssuesList(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getRule()).isEqualTo("findbugs:ICAST_INT_2_LONG_AS_INSTANT"); - assertThat(issues.get(0).getLine()).isEqualTo(8); - - issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs4.java"))).getIssuesList(); - assertThat(issues).isEmpty(); - } - - /** - * SONAR-2325 - */ - @Test - void confidence_level() { - MavenBuild build = MavenBuild.create(FindbugsTestSuite.projectPom("findbugs")) - .setCleanPackageSonarGoals() - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "high"); - orchestrator.executeBuild(build); - - // Check that issue with confidence level lower than high was NOT reported: - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); - assertThat(issues).isEmpty(); - - // Check that other files were analysed by Findbugs: - issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs2.java"))).getIssuesList(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getRule()).isEqualTo("findbugs:ICAST_INT_2_LONG_AS_INSTANT"); - assertThat(issues.get(0).getLine()).isEqualTo(8); - } - - /** - * SONARJAVA-380 - */ - @Test - void should_always_use_english_locale() throws Exception { - MavenBuild build = MavenBuild.create(FindbugsTestSuite.projectPom("findbugs")) - .setCleanPackageSonarGoals() - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "low") - .setProperty("sonar.violationLocale", "fr") - .setEnvironmentVariable("MAVEN_OPTS", "-Duser.language=fr"); - orchestrator.executeBuild(build); - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs3.java"))).getIssuesList(); - assertThat(issues).hasSize(1); - assertThat(issues.get(0).getMessage()).isEqualTo("Private method Findbugs3.method() is never called"); - } - - /** - * SONARJAVA-385 - */ - @Test - void inclusions_exclusions() throws Exception { - File projectDir = FindbugsTestSuite.projectPom("findbugs").getParentFile(); - // Compile - MavenBuild build = MavenBuild.create(new File(projectDir, "pom.xml")) - .setGoals("clean package"); - orchestrator.executeBuild(build); - // Analyze - SonarScanner sonarScanner = SonarScanner.create() - .setProjectDir(projectDir) - .setProperty("sonar.projectKey", PROJECT_KEY) - .setProperty("sonar.projectName", "findbugs") - .setProperty("sonar.projectVersion", "1.0-SNAPSHOT") - .setProperty("sonar.sources", ".") - .setProperty("sonar.java.binaries", "target/classes") - .setProperty("sonar.exclusions", "src/main/java/Findbugs2.java"); - orchestrator.executeBuild(sonarScanner); - - // Check that class was really excluded from Findbugs analysis: - String findbugsXml = Files.toString(new File(projectDir, ".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8); - - // FIXME Even though a source file is excluded, the corresponding .class file is currently analyzed by the plugin - // assertThat(findbugsXml).doesNotContain("Findbugs2.class"); - - // Check that other files were analysed by Findbugs: - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); - assertThat(issues).hasSize(1); - } - - @Test - void multiple_directories_with_classes() throws Exception { - File projectDir = FindbugsTestSuite.projectPom("multiple-directories-with-classes").getParentFile(); - // Compile - MavenBuild build = MavenBuild.create(new File(projectDir, "pom.xml")) - .setGoals("clean package"); - orchestrator.executeBuild(build); - // Analyze - SonarScanner sonarScanner = SonarScanner.create() - .setProjectDir(projectDir) - .setProperty("sonar.projectKey", PROJECT_KEY) - .setProperty("sonar.projectName", "example") - .setProperty("sonar.projectVersion", "1") - .setProperty("sonar.sources", ".") - .setProperty("sonar.java.binaries", "dir1/target/classes,dir2/target/classes"); - orchestrator.executeBuild(sonarScanner); - - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().projects(PROJECT_KEY)).getIssuesList(); - assertThat(issues).hasSize(2); - } - - @Test - void only_analyze() throws Exception { - File projectDir = FindbugsTestSuite.projectPom("findbugs").getParentFile(); - - MavenBuild build = MavenBuild.create(new File(projectDir, "pom.xml")) - .setProperty(FindbugsConstants.ONLY_ANALYZE_PROPERTY, "Findbugs1") - .setGoals("clean package sonar:sonar"); - orchestrator.executeBuild(build); - - // Check that class was really excluded from Findbugs analysis: - String findbugsXml = Files.toString(new File(projectDir, ".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8); - - assertThat(findbugsXml).doesNotContain("Findbugs2.class"); - - // Check that other files were analysed by Findbugs: - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); - assertThat(issues).hasSize(1); - } -} +/* + * Findbugs :: IT :: Plugin + * Copyright (C) 2014 SonarSource + * sonarqube@googlegroups.com + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.plugins.findbugs.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.sonar.plugins.findbugs.FindbugsConstants; +import org.sonarqube.ws.Issues.Issue; +import org.sonarqube.ws.client.issues.IssuesService; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import com.google.common.io.Files; +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.MavenBuild; +import com.sonar.orchestrator.build.SonarScanner; + +@IntegrationTest +class FindbugsIT { + + private static final String PROJECT_KEY = "com.sonarsource.it.samples:findbugs"; + private static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; + + @BeforeEach + public void setupProfile() { + FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Findbugs Integration Tests", "findbugs-it", "java"); + } + + @AfterEach + public void deleteProject() { + FindbugsTestSuite.deleteProject(PROJECT_KEY); + } + + @Test + void analysis() { + MavenBuild build = MavenBuild.create(FindbugsTestSuite.projectPom("findbugs")) + .setCleanPackageSonarGoals() + .setProperty("sonar.dynamicAnalysis", "false"); + orchestrator.executeBuild(build); + + // Check that issue with confidence level lower than high was reported: + IssuesService issueClient = FindbugsTestSuite.issueClient(); + + List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getRule()).isEqualTo("findbugs:NP_DEREFERENCE_OF_READLINE_VALUE"); + assertThat(issues.get(0).getLine()).isEqualTo(13); + + issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs2.java"))).getIssuesList(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getRule()).isEqualTo("findbugs:ICAST_INT_2_LONG_AS_INSTANT"); + assertThat(issues.get(0).getLine()).isEqualTo(8); + + issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs4.java"))).getIssuesList(); + assertThat(issues).isEmpty(); + } + + /** + * SONAR-2325 + */ + @Test + void confidence_level() { + MavenBuild build = MavenBuild.create(FindbugsTestSuite.projectPom("findbugs")) + .setCleanPackageSonarGoals() + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "high"); + orchestrator.executeBuild(build); + + // Check that issue with confidence level lower than high was NOT reported: + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); + assertThat(issues).isEmpty(); + + // Check that other files were analysed by Findbugs: + issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs2.java"))).getIssuesList(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getRule()).isEqualTo("findbugs:ICAST_INT_2_LONG_AS_INSTANT"); + assertThat(issues.get(0).getLine()).isEqualTo(8); + } + + /** + * SONARJAVA-380 + */ + @Test + void should_always_use_english_locale() throws Exception { + MavenBuild build = MavenBuild.create(FindbugsTestSuite.projectPom("findbugs")) + .setCleanPackageSonarGoals() + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "low") + .setProperty("sonar.violationLocale", "fr") + .setEnvironmentVariable("MAVEN_OPTS", "-Duser.language=fr"); + orchestrator.executeBuild(build); + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs3.java"))).getIssuesList(); + assertThat(issues).hasSize(1); + assertThat(issues.get(0).getMessage()).isEqualTo("Private method Findbugs3.method() is never called"); + } + + /** + * SONARJAVA-385 + */ + @Test + void inclusions_exclusions() throws Exception { + File projectDir = FindbugsTestSuite.projectPom("findbugs").getParentFile(); + // Compile + MavenBuild build = MavenBuild.create(new File(projectDir, "pom.xml")) + .setGoals("clean package"); + orchestrator.executeBuild(build); + // Analyze + SonarScanner sonarScanner = SonarScanner.create() + .setProjectDir(projectDir) + .setProperty("sonar.projectKey", PROJECT_KEY) + .setProperty("sonar.projectName", "findbugs") + .setProperty("sonar.projectVersion", "1.0-SNAPSHOT") + .setProperty("sonar.sources", ".") + .setProperty("sonar.java.binaries", "target/classes") + .setProperty("sonar.exclusions", "src/main/java/Findbugs2.java"); + orchestrator.executeBuild(sonarScanner); + + // Check that class was really excluded from Findbugs analysis: + String findbugsXml = Files.toString(new File(projectDir, ".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8); + + // FIXME Even though a source file is excluded, the corresponding .class file is currently analyzed by the plugin + // assertThat(findbugsXml).doesNotContain("Findbugs2.class"); + + // Check that other files were analysed by Findbugs: + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); + assertThat(issues).hasSize(1); + } + + @Test + void multiple_directories_with_classes() throws Exception { + File projectDir = FindbugsTestSuite.projectPom("multiple-directories-with-classes").getParentFile(); + // Compile + MavenBuild build = MavenBuild.create(new File(projectDir, "pom.xml")) + .setGoals("clean package"); + orchestrator.executeBuild(build); + // Analyze + SonarScanner sonarScanner = SonarScanner.create() + .setProjectDir(projectDir) + .setProperty("sonar.projectKey", PROJECT_KEY) + .setProperty("sonar.projectName", "example") + .setProperty("sonar.projectVersion", "1") + .setProperty("sonar.sources", ".") + .setProperty("sonar.java.binaries", "dir1/target/classes,dir2/target/classes"); + orchestrator.executeBuild(sonarScanner); + + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().projects(PROJECT_KEY)).getIssuesList(); + assertThat(issues).hasSize(2); + } + + @Test + void only_analyze() throws Exception { + File projectDir = FindbugsTestSuite.projectPom("findbugs").getParentFile(); + + MavenBuild build = MavenBuild.create(new File(projectDir, "pom.xml")) + .setProperty(FindbugsConstants.ONLY_ANALYZE_PROPERTY, "Findbugs1") + .setGoals("clean package sonar:sonar"); + orchestrator.executeBuild(build); + + // Check that class was really excluded from Findbugs analysis: + String findbugsXml = Files.toString(new File(projectDir, ".scannerwork/findbugs-result.xml"), StandardCharsets.UTF_8); + + assertThat(findbugsXml).doesNotContain("Findbugs2.class"); + + // Check that other files were analysed by Findbugs: + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().components(FindbugsTestSuite.keyFor(PROJECT_KEY, "", "Findbugs1.java"))).getIssuesList(); + assertThat(issues).hasSize(1); + } +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/FindbugsTestSuite.java b/src/test/java/org/sonar/plugins/findbugs/it/FindbugsTestSuite.java index c4929db3..8e66b01b 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/FindbugsTestSuite.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/FindbugsTestSuite.java @@ -1,144 +1,145 @@ -/* - * Findbugs :: IT :: Plugin - * Copyright (C) 2014 SonarSource - * sonarqube@googlegroups.com - * - * This program 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. - * - * This program 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 02 - */ -package org.sonar.plugins.findbugs.it; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.OrchestratorBuilder; -import com.sonar.orchestrator.locator.FileLocation; -import com.sonar.orchestrator.locator.Location; -import com.sonar.orchestrator.locator.MavenLocation; - -import org.sonarqube.ws.client.HttpConnector; -import org.sonarqube.ws.client.issues.IssuesService; -import org.sonarqube.ws.client.projects.CreateRequest; -import org.sonarqube.ws.client.projects.DeleteRequest; -import org.sonarqube.ws.client.projects.ProjectsService; -import org.sonarqube.ws.client.qualityprofiles.AddProjectRequest; -import org.sonarqube.ws.client.qualityprofiles.QualityprofilesService; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.file.FileSystems; -import java.nio.file.Files; - -public class FindbugsTestSuite { - - public static final Orchestrator ORCHESTRATOR; - - static { - // build, start and stop the orchestrator here, making sure that it happens exactly once whether we run one or multiple tests - String sonarVersion = System.getProperty("sonar.server.version", "8.9"); - - // We will test here the case where an older version of the plugin was already installed and upgrade it - // This should be its own test case but it takes a long time to build a server so we're doing it here - - OrchestratorBuilder orchestratorBuilder = Orchestrator.builderEnv() - // Build the SonarQube server with an older version of the plugin - .addPlugin(MavenLocation.of("com.github.spotbugs", "sonar-findbugs-plugin", "4.0.6")) - .keepBundledPlugins() - .setSonarVersion("LATEST_RELEASE[" + sonarVersion + "]") - .restoreProfileAtStartup(FileLocation.ofClasspath("/it/profiles/empty-backup.xml")) - .restoreProfileAtStartup(FileLocation.ofClasspath("/it/profiles/findbugs-backup.xml")) - .restoreProfileAtStartup(FileLocation.ofClasspath("/it/profiles/fbcontrib-backup.xml")); - ORCHESTRATOR = orchestratorBuilder.build(); - // Start the server with the older version of the plugin so it will save the older version of the rules/profiles - ORCHESTRATOR.start(); - - ORCHESTRATOR.stop(); - // Now install the version of the plugin we have built locally - File pluginTargetFile = new File(ORCHESTRATOR.getServer().getHome(), "extensions/plugins/sonar-findbugs-plugin.jar"); - try (OutputStream out = new FileOutputStream(pluginTargetFile)) { - // Delete the old version of the plugin - Files.delete(ORCHESTRATOR.getServer().getHome().toPath().resolve("extensions/plugins/sonar-findbugs-plugin-4.0.6.jar")); - Files.copy(FileSystems.getDefault().getPath("target", "sonar-findbugs-plugin.jar"), out); - } catch (IOException e) { - throw new RuntimeException(e); - } - - ORCHESTRATOR.start(); - - Thread stopOrchestratorThread = new Thread(() -> ORCHESTRATOR.stop(), sonarVersion); - Runtime.getRuntime().addShutdownHook(stopOrchestratorThread); - } - - public static void setupProjectAndProfile(String projectKey, String projectName, String qualityProfile, String language) { - CreateRequest createRequest = new CreateRequest(); - createRequest.setName(projectName); - createRequest.setProject(projectKey); - - projectClient().create(createRequest); - - setupProfile(projectKey, qualityProfile, language); - } - - public static void setupProfile(String projectKey, String qualityProfile, String language) { - AddProjectRequest addProjectRequest = new AddProjectRequest(); - addProjectRequest.setLanguage(language); - addProjectRequest.setProject(projectKey); - addProjectRequest.setQualityProfile(qualityProfile); - qualityProfileClient().addProject(addProjectRequest); - } - - /** - * Tests are checking the number of issues so we need to delete previous issues after running a another test - */ - public static void deleteProject(String projectKey) { - DeleteRequest deleteRequest = new DeleteRequest(); - deleteRequest.setProject(projectKey); - - projectClient().delete(deleteRequest); - } - - public static String keyFor(String projectKey, String pkgDir, String cls) { - return projectKey + ":src/main/java/" + pkgDir + cls; - } - - public static String keyForFile(String projectKey, String fileName) { - return projectKey + ":" + fileName; - } - - public static File projectPom(String projectName) { - return new File("src/test/resources/projects/" + projectName + "/pom.xml").getAbsoluteFile(); - } - - public static Location projectDirectoryLocation(String projectName) { - return FileLocation.of(new File("src/test/resources/projects/" + projectName).getAbsoluteFile()); - } - - public static HttpConnector connector() { - String baseUrl = ORCHESTRATOR.getServer().getUrl(); - - return HttpConnector.newBuilder().url(baseUrl).credentials("admin", "admin").build(); - } - - public static IssuesService issueClient() { - return new IssuesService(connector()); - } - - public static ProjectsService projectClient() { - return new ProjectsService(connector()); - } - - public static QualityprofilesService qualityProfileClient() { - return new QualityprofilesService(connector()); - } -} +/* + * Findbugs :: IT :: Plugin + * Copyright (C) 2014 SonarSource + * sonarqube@googlegroups.com + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.plugins.findbugs.it; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.OrchestratorBuilder; +import com.sonar.orchestrator.locator.FileLocation; +import com.sonar.orchestrator.locator.Location; +import com.sonar.orchestrator.locator.MavenLocation; + +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.issues.IssuesService; +import org.sonarqube.ws.client.projects.CreateRequest; +import org.sonarqube.ws.client.projects.DeleteRequest; +import org.sonarqube.ws.client.projects.ProjectsService; +import org.sonarqube.ws.client.qualityprofiles.AddProjectRequest; +import org.sonarqube.ws.client.qualityprofiles.QualityprofilesService; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.FileSystems; +import java.nio.file.Files; + +@IntegrationTest +public class FindbugsTestSuite { + + public static final Orchestrator ORCHESTRATOR; + + static { + // build, start and stop the orchestrator here, making sure that it happens exactly once whether we run one or multiple tests + String sonarVersion = System.getProperty("sonar.server.version", "8.9"); + + // We will test here the case where an older version of the plugin was already installed and upgrade it + // This should be its own test case but it takes a long time to build a server so we're doing it here + + OrchestratorBuilder orchestratorBuilder = Orchestrator.builderEnv() + // Build the SonarQube server with an older version of the plugin + .addPlugin(MavenLocation.of("com.github.spotbugs", "sonar-findbugs-plugin", "4.0.6")) + .keepBundledPlugins() + .setSonarVersion("LATEST_RELEASE[" + sonarVersion + "]") + .restoreProfileAtStartup(FileLocation.ofClasspath("/it/profiles/empty-backup.xml")) + .restoreProfileAtStartup(FileLocation.ofClasspath("/it/profiles/findbugs-backup.xml")) + .restoreProfileAtStartup(FileLocation.ofClasspath("/it/profiles/fbcontrib-backup.xml")); + ORCHESTRATOR = orchestratorBuilder.build(); + // Start the server with the older version of the plugin so it will save the older version of the rules/profiles + ORCHESTRATOR.start(); + + ORCHESTRATOR.stop(); + // Now install the version of the plugin we have built locally + File pluginTargetFile = new File(ORCHESTRATOR.getServer().getHome(), "extensions/plugins/sonar-findbugs-plugin.jar"); + try (OutputStream out = new FileOutputStream(pluginTargetFile)) { + // Delete the old version of the plugin + Files.delete(ORCHESTRATOR.getServer().getHome().toPath().resolve("extensions/plugins/sonar-findbugs-plugin-4.0.6.jar")); + Files.copy(FileSystems.getDefault().getPath("target", "sonar-findbugs-plugin.jar"), out); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ORCHESTRATOR.start(); + + Thread stopOrchestratorThread = new Thread(() -> ORCHESTRATOR.stop(), sonarVersion); + Runtime.getRuntime().addShutdownHook(stopOrchestratorThread); + } + + public static void setupProjectAndProfile(String projectKey, String projectName, String qualityProfile, String language) { + CreateRequest createRequest = new CreateRequest(); + createRequest.setName(projectName); + createRequest.setProject(projectKey); + + projectClient().create(createRequest); + + setupProfile(projectKey, qualityProfile, language); + } + + public static void setupProfile(String projectKey, String qualityProfile, String language) { + AddProjectRequest addProjectRequest = new AddProjectRequest(); + addProjectRequest.setLanguage(language); + addProjectRequest.setProject(projectKey); + addProjectRequest.setQualityProfile(qualityProfile); + qualityProfileClient().addProject(addProjectRequest); + } + + /** + * Tests are checking the number of issues so we need to delete previous issues after running a another test + */ + public static void deleteProject(String projectKey) { + DeleteRequest deleteRequest = new DeleteRequest(); + deleteRequest.setProject(projectKey); + + projectClient().delete(deleteRequest); + } + + public static String keyFor(String projectKey, String pkgDir, String cls) { + return projectKey + ":src/main/java/" + pkgDir + cls; + } + + public static String keyForFile(String projectKey, String fileName) { + return projectKey + ":" + fileName; + } + + public static File projectPom(String projectName) { + return new File("src/test/resources/projects/" + projectName + "/pom.xml").getAbsoluteFile(); + } + + public static Location projectDirectoryLocation(String projectName) { + return FileLocation.of(new File("src/test/resources/projects/" + projectName).getAbsoluteFile()); + } + + public static HttpConnector connector() { + String baseUrl = ORCHESTRATOR.getServer().getUrl(); + + return HttpConnector.newBuilder().url(baseUrl).credentials("admin", "admin").build(); + } + + public static IssuesService issueClient() { + return new IssuesService(connector()); + } + + public static ProjectsService projectClient() { + return new ProjectsService(connector()); + } + + public static QualityprofilesService qualityProfileClient() { + return new QualityprofilesService(connector()); + } +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/IntegrationTest.java b/src/test/java/org/sonar/plugins/findbugs/it/IntegrationTest.java new file mode 100644 index 00000000..dc7ae07a --- /dev/null +++ b/src/test/java/org/sonar/plugins/findbugs/it/IntegrationTest.java @@ -0,0 +1,22 @@ +/** + * + */ +package org.sonar.plugins.findbugs.it; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.Test; + +/** + * @author gtoison + * + */ +@Target(value = {ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Test +public @interface IntegrationTest { + +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/MultiModuleIT.java b/src/test/java/org/sonar/plugins/findbugs/it/MultiModuleIT.java index fe61ed54..8ef905c4 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/MultiModuleIT.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/MultiModuleIT.java @@ -1,100 +1,101 @@ -package org.sonar.plugins.findbugs.it; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; -import org.sonarqube.ws.Issues.Issue; -import org.sonarqube.ws.client.issues.IssuesService; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.function.Predicate; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.GradleBuild; -import com.sonar.orchestrator.build.MavenBuild; - -/** - * @author gtoison - * - */ -class MultiModuleIT { - - private static final String MAVEN_PROJECT_KEY = "spotbugs:multi-module"; - private static final String GRADLE_PROJECT_KEY = "org.sonarqube:gradle-multimodule"; - - public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; - - @Test - void multiModuleMavenAnalysis() throws Exception { - FindbugsTestSuite.setupProjectAndProfile("spotbugs:multi-module", "Multi Module Maven Project", "FindBugs + FB-Contrib", "java"); - - File projectDir = FindbugsTestSuite.projectPom("multi-module").getParentFile(); - - MavenBuild build = MavenBuild.create() - .setPom(FindbugsTestSuite.projectPom("multi-module")) - .setGoals("clean package sonar:sonar"); - orchestrator.executeBuild(build); - - Path appModuleReportPath = projectDir.toPath().resolve("multi-module-app/target/sonar/findbugs-result.xml"); - - checkMultiModuleAnalysis(appModuleReportPath); - checkIssues(MAVEN_PROJECT_KEY); - - FindbugsTestSuite.deleteProject(MAVEN_PROJECT_KEY); - } - - @Test - void multiModuleGradleAnalysis() throws IOException { - FindbugsTestSuite.setupProjectAndProfile(GRADLE_PROJECT_KEY, "Multimodule Gradle Project", "FindBugs + FB-Contrib", "java"); - - File projectDir = FindbugsTestSuite.projectPom("multi-module").getParentFile(); - - GradleBuild build = GradleBuild.create() - .setProjectDirectory(FindbugsTestSuite.projectDirectoryLocation("multi-module")) - .setTasks("clean", "build") - .addArgument("--stacktrace") - .addSonarTask(); - orchestrator.executeBuild(build); - - Path appModuleReportPath = projectDir.toPath().resolve("build/sonar/org.sonarqube_gradle-multimodule_multi-module-app/findbugs-result.xml"); - - checkMultiModuleAnalysis(appModuleReportPath); - checkIssues(GRADLE_PROJECT_KEY); - - FindbugsTestSuite.deleteProject(GRADLE_PROJECT_KEY); - } - - public void checkMultiModuleAnalysis(Path appModuleReportPath) throws IOException { - List appModuleFindbugsXml = Files.readAllLines(appModuleReportPath, StandardCharsets.UTF_8); - - // SampleApp should be analyzed - assertThat(appModuleFindbugsXml).filteredOn(line -> line.contains("SampleApp.class")).singleElement(); - - // But not SampleCore or SampleFx - assertThat(appModuleFindbugsXml) - .noneMatch(line -> line.contains("SampleCore.class")) - .noneMatch(line -> line.contains("SampleFx.class")); - } - - private void checkIssues(String projectKey) { - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().projects(projectKey)).getIssuesList(); - - assertThat(issues.stream().filter(component(projectKey, "multi-module-app/src/main/java/multimodule/app/SampleApp.java"))).hasSize(5); - assertThat(issues.stream().filter(component(projectKey, "multi-module-core/src/main/java/multimodule/core/SampleCore.java"))).hasSize(4); - assertThat(issues.stream().filter(component(projectKey, "multi-module-core/src/main/java/multimodule/core/InnerClassSample.java"))).hasSize(9); - assertThat(issues.stream().filter(component(projectKey, "multi-module-fx/src/main/java/multimodule/fx/SampleFx.java"))).hasSize(3); - // There's one native SQ issue for the Hello.scala sample - assertThat(issues.stream().filter(component(projectKey, "multi-module-scala/src/main/scala/Hello.scala"))).hasSize(2); - assertThat(issues.stream().filter(component(projectKey, "multi-module-kotlin/src/main/kotlin/com/bugs/KotlinSample.kt"))).hasSize(2); - } - - private Predicate component(String projectKey, String fileName) { - return i -> i.getComponent().equals(projectKey + ":" + fileName); - } -} +package org.sonar.plugins.findbugs.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.sonarqube.ws.Issues.Issue; +import org.sonarqube.ws.client.issues.IssuesService; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.function.Predicate; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.GradleBuild; +import com.sonar.orchestrator.build.MavenBuild; + +/** + * @author gtoison + * + */ +@IntegrationTest +class MultiModuleIT { + + private static final String MAVEN_PROJECT_KEY = "spotbugs:multi-module"; + private static final String GRADLE_PROJECT_KEY = "org.sonarqube:gradle-multimodule"; + + public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; + + @Test + void multiModuleMavenAnalysis() throws Exception { + FindbugsTestSuite.setupProjectAndProfile("spotbugs:multi-module", "Multi Module Maven Project", "FindBugs + FB-Contrib", "java"); + + File projectDir = FindbugsTestSuite.projectPom("multi-module").getParentFile(); + + MavenBuild build = MavenBuild.create() + .setPom(FindbugsTestSuite.projectPom("multi-module")) + .setGoals("clean package sonar:sonar"); + orchestrator.executeBuild(build); + + Path appModuleReportPath = projectDir.toPath().resolve("multi-module-app/target/sonar/findbugs-result.xml"); + + checkMultiModuleAnalysis(appModuleReportPath); + checkIssues(MAVEN_PROJECT_KEY); + + FindbugsTestSuite.deleteProject(MAVEN_PROJECT_KEY); + } + + @Test + void multiModuleGradleAnalysis() throws IOException { + FindbugsTestSuite.setupProjectAndProfile(GRADLE_PROJECT_KEY, "Multimodule Gradle Project", "FindBugs + FB-Contrib", "java"); + + File projectDir = FindbugsTestSuite.projectPom("multi-module").getParentFile(); + + GradleBuild build = GradleBuild.create() + .setProjectDirectory(FindbugsTestSuite.projectDirectoryLocation("multi-module")) + .setTasks("clean", "build") + .addArgument("--stacktrace") + .addSonarTask(); + orchestrator.executeBuild(build); + + Path appModuleReportPath = projectDir.toPath().resolve("build/sonar/org.sonarqube_gradle-multimodule_multi-module-app/findbugs-result.xml"); + + checkMultiModuleAnalysis(appModuleReportPath); + checkIssues(GRADLE_PROJECT_KEY); + + FindbugsTestSuite.deleteProject(GRADLE_PROJECT_KEY); + } + + public void checkMultiModuleAnalysis(Path appModuleReportPath) throws IOException { + List appModuleFindbugsXml = Files.readAllLines(appModuleReportPath, StandardCharsets.UTF_8); + + // SampleApp should be analyzed + assertThat(appModuleFindbugsXml).filteredOn(line -> line.contains("SampleApp.class")).singleElement(); + + // But not SampleCore or SampleFx + assertThat(appModuleFindbugsXml) + .noneMatch(line -> line.contains("SampleCore.class")) + .noneMatch(line -> line.contains("SampleFx.class")); + } + + private void checkIssues(String projectKey) { + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().projects(projectKey)).getIssuesList(); + + assertThat(issues.stream().filter(component(projectKey, "multi-module-app/src/main/java/multimodule/app/SampleApp.java"))).hasSize(5); + assertThat(issues.stream().filter(component(projectKey, "multi-module-core/src/main/java/multimodule/core/SampleCore.java"))).hasSize(4); + assertThat(issues.stream().filter(component(projectKey, "multi-module-core/src/main/java/multimodule/core/InnerClassSample.java"))).hasSize(9); + assertThat(issues.stream().filter(component(projectKey, "multi-module-fx/src/main/java/multimodule/fx/SampleFx.java"))).hasSize(3); + // There's one native SQ issue for the Hello.scala sample + assertThat(issues.stream().filter(component(projectKey, "multi-module-scala/src/main/scala/Hello.scala"))).hasSize(2); + assertThat(issues.stream().filter(component(projectKey, "multi-module-kotlin/src/main/kotlin/com/bugs/KotlinSample.kt"))).hasSize(2); + } + + private Predicate component(String projectKey, String fileName) { + return i -> i.getComponent().equals(projectKey + ":" + fileName); + } +} diff --git a/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java b/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java index 65b74e85..dafad3c6 100644 --- a/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java +++ b/src/test/java/org/sonar/plugins/findbugs/it/ScalaIT.java @@ -1,67 +1,68 @@ -/* - * SonarQube SpotBugs Plugin - * - * This program 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. - * - * This program 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 02 - */ -package org.sonar.plugins.findbugs.it; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.sonar.plugins.findbugs.profiles.FindbugsSecurityScalaProfile; -import org.sonarqube.ws.Issues.Issue; -import org.sonarqube.ws.client.issues.IssuesService; - -import java.util.List; - -import com.sonar.orchestrator.Orchestrator; -import com.sonar.orchestrator.build.MavenBuild; - -class ScalaIT { - - private static final String PROJECT_KEY = "org.sonar.tests:scala"; - public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; - - @BeforeEach - public void setupProfile() { - FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Scala Integration Tests", "IT", "java"); - FindbugsTestSuite.setupProfile(PROJECT_KEY, FindbugsSecurityScalaProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME, "scala"); - } - - @AfterEach - public void deleteProject() { - FindbugsTestSuite.deleteProject(PROJECT_KEY); - } - - @Test - void test() throws Exception { - MavenBuild build = MavenBuild.create() - .setPom(FindbugsTestSuite.projectPom("scala")) - .setProperty("sonar.dynamicAnalysis", "false") - .setProperty("sonar.findbugs.confidenceLevel", "low") - //.setProperty("sonar.sources", "src/main/scala") - //.setProperty("sonar.java.binaries", "target/classes") - .setGoals("clean package sonar:sonar"); - orchestrator.executeBuild(build); - - IssuesService issueClient = FindbugsTestSuite.issueClient(); - List issues = issueClient.search(IssueQuery.create().projects(PROJECT_KEY)).getIssuesList(); - assertThat(issues).hasSize(2); - assertThat(issues.get(0).getMessage()).isEqualTo("Hello$.main(String[]) invokes toString() method on a String"); - assertThat(issues.get(1).getMessage()).isEqualTo("This Scala random generator (scala.util.Random.nextInt()) is predictable"); - } -} +/* + * SonarQube SpotBugs Plugin + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.plugins.findbugs.it; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.sonar.plugins.findbugs.profiles.FindbugsSecurityScalaProfile; +import org.sonarqube.ws.Issues.Issue; +import org.sonarqube.ws.client.issues.IssuesService; + +import java.util.List; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.MavenBuild; + +@IntegrationTest +class ScalaIT { + + private static final String PROJECT_KEY = "org.sonar.tests:scala"; + public static Orchestrator orchestrator = FindbugsTestSuite.ORCHESTRATOR; + + @BeforeEach + public void setupProfile() { + FindbugsTestSuite.setupProjectAndProfile(PROJECT_KEY, "Scala Integration Tests", "IT", "java"); + FindbugsTestSuite.setupProfile(PROJECT_KEY, FindbugsSecurityScalaProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME, "scala"); + } + + @AfterEach + public void deleteProject() { + FindbugsTestSuite.deleteProject(PROJECT_KEY); + } + + @Test + void test() throws Exception { + MavenBuild build = MavenBuild.create() + .setPom(FindbugsTestSuite.projectPom("scala")) + .setProperty("sonar.dynamicAnalysis", "false") + .setProperty("sonar.findbugs.confidenceLevel", "low") + //.setProperty("sonar.sources", "src/main/scala") + //.setProperty("sonar.java.binaries", "target/classes") + .setGoals("clean package sonar:sonar"); + orchestrator.executeBuild(build); + + IssuesService issueClient = FindbugsTestSuite.issueClient(); + List issues = issueClient.search(IssueQuery.create().projects(PROJECT_KEY)).getIssuesList(); + assertThat(issues).hasSize(2); + assertThat(issues.get(0).getMessage()).isEqualTo("Hello$.main(String[]) invokes toString() method on a String"); + assertThat(issues.get(1).getMessage()).isEqualTo("This Scala random generator (scala.util.Random.nextInt()) is predictable"); + } +}