Skip to content

Commit

Permalink
test: test against sonarqube 10 (#725)
Browse files Browse the repository at this point in the history
- Added SonarQube 10 to the matrix build
- LogTester was moved to a different artifact since the 9.15 version of
the sonar plugin API: use the new LogTester based on slf4j
  • Loading branch information
gtoison authored Apr 11, 2023
1 parent 8e93e5a commit b2fa625
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 104 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ jobs:
SONAR_PLUGIN_API_GROUPID: org.sonarsource.api.plugin
SONAR_JAVA_VERSION: 7.16.0.30901
SONAR_SERVER_JAVA_VERSION: 17
# 10.x
- SONAR_SERVER_VERSION: 10.0.0.68432
SONAR_PLUGIN_API_VERSION: 9.14.0.375
SONAR_PLUGIN_API_GROUPID: org.sonarsource.api.plugin
SONAR_JAVA_VERSION: 7.16.1.31255
SONAR_SERVER_JAVA_VERSION: 17
steps:
- uses: actions/checkout@v3
with:
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@
<version>9.1.0.47736</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api-test-fixtures</artifactId>
<version>9.15.0.435</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Plugin.Context;
import org.sonar.api.PropertyType;
import org.sonar.api.Startable;
import org.sonar.api.Plugin.Context;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
Expand All @@ -55,8 +57,6 @@
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.plugins.findbugs.classpath.ClasspathLocator;
import org.sonar.plugins.findbugs.classpath.DefaultClasspathLocator;
import org.sonar.plugins.findbugs.rules.FbContribRulesDefinition;
Expand All @@ -77,7 +77,7 @@
@ScannerSide
public class FindbugsConfiguration implements Startable {

private static final Logger LOG = Loggers.get(FindbugsConfiguration.class);
private static final Logger LOG = LoggerFactory.getLogger(FindbugsConfiguration.class);
private static final Pattern JSP_FILE_NAME_PATTERN = Pattern.compile(".*_jsp[\\$0-9]*\\.class");
public static final String SONAR_JAVA_BINARIES = "sonar.java.binaries";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.slf4j.event.Level;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.classpath.ClasspathLocator;
import org.sonar.plugins.findbugs.configuration.SimpleConfiguration;
import org.sonar.plugins.findbugs.rule.FakeActiveRules;
import org.sonar.plugins.findbugs.util.JupiterLogTester;
import org.sonar.plugins.java.api.JavaResourceLocator;

import com.google.common.collect.ImmutableList;
Expand All @@ -67,7 +65,7 @@ class FindbugsConfigurationTest {
public File temp;

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

private FilePredicates filePredicates;
private FileSystem fs;
Expand Down Expand Up @@ -263,9 +261,9 @@ void should_warn_of_missing_precompiled_jsp(boolean withSq98Api, boolean analyze
// - Findbugs needs sources to be compiled
// With the SonarQube 9.8+ API we get the Test.class so only one warning
if (withSq98Api) {
assertThat(logTester.getLogs(LoggerLevel.WARN)).hasSize(1);
assertThat(logTester.getLogs(Level.WARN)).hasSize(1);
} else {
assertThat(logTester.getLogs(LoggerLevel.WARN)).hasSize(2);
assertThat(logTester.getLogs(Level.WARN)).hasSize(2);
}
}

Expand Down Expand Up @@ -297,7 +295,7 @@ void should_analyze_precompiled_jsp(boolean withSq98Api, boolean analyzeTests) t
}
}

assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(logTester.getLogs(Level.WARN)).isEmpty();
}

@ParameterizedTest
Expand Down Expand Up @@ -331,7 +329,7 @@ void scala_project(boolean withSq98Api, boolean analyzeTests) throws IOException
}
}

assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(logTester.getLogs(Level.WARN)).isEmpty();
}

private void setupSampleProject(boolean withPrecompiledJsp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.event.Level;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import org.sonar.plugins.findbugs.util.JupiterLogTester;
import org.sonar.plugins.findbugs.xml.FindBugsFilter;
import org.sonar.plugins.findbugs.xml.Match;
import org.sonar.plugins.java.Java;
Expand All @@ -54,7 +53,7 @@ class FindbugsProfileImporterTest {
private static final String TEST_PROFILE = "TEST_PROFILE";

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

private Context context = new Context();
private final FindbugsProfileImporter importer = new FindbugsProfileImporter(FakeRuleFinder.createWithOnlyFindbugsRules());
Expand Down Expand Up @@ -177,7 +176,7 @@ void testImportingUncorrectXmlFile() {
Collection<BuiltInActiveRule> results = profile.rules();

assertThat(results).isEmpty();
assertThat(logTester.getLogs(LoggerLevel.ERROR)).hasSize(1);
assertThat(logTester.getLogs(Level.ERROR)).hasSize(1);
}

@ParameterizedTest
Expand All @@ -196,8 +195,8 @@ void profileImport(String profilePath, int expectedSize) {
Collection<BuiltInActiveRule> results = profile.rules();

assertThat(results).hasSize(expectedSize);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(LoggerLevel.WARN)).hasSize(1);
assertThat(logTester.getLogs(Level.ERROR)).hasSize(0);
assertThat(logTester.getLogs(Level.WARN)).hasSize(1);
}

private BuiltInActiveRule findActiveRule(BuiltInQualityProfile profile, String repositoryKey, String ruleKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package org.sonar.plugins.findbugs.profiles;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FbContribRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import org.sonar.plugins.findbugs.util.JupiterLogTester;
import org.sonar.plugins.java.Java;

import static org.assertj.core.api.Assertions.assertThat;

class FindbugsContribProfileTest {

class FindbugsContribProfileTest {

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

@Test
void shouldCreateProfile() {
Expand All @@ -30,7 +29,7 @@ void shouldCreateProfile() {
BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsContribProfile.FB_CONTRIB_PROFILE_NAME);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FindbugsRulesDefinition.RULE_COUNT);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FbContribRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FbContribRulesDefinition.RULE_COUNT);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(Level.ERROR)).isEmpty();

FindbugsProfileTest.assertHasOnlyRulesForLanguage(profile.rules(), Java.KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.language.Jsp;
import org.sonar.plugins.findbugs.language.scala.Scala;
Expand All @@ -35,7 +35,6 @@
import org.sonar.plugins.findbugs.rules.FindSecurityBugsRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsScalaRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import org.sonar.plugins.findbugs.util.JupiterLogTester;
import org.sonar.plugins.java.Java;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -45,7 +44,7 @@
class FindbugsProfileTest {

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

@Test
void shouldCreateProfile() {
Expand All @@ -57,7 +56,7 @@ void shouldCreateProfile() {
BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsProfile.FINDBUGS_PROFILE_NAME);
assertThat(profile.rules()).hasSize(FindbugsRulesDefinition.RULE_COUNT);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FindbugsRulesDefinition.RULE_COUNT);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(Level.ERROR)).isEmpty();

FindbugsProfileTest.assertHasOnlyRulesForLanguage(profile.rules(), Java.KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.language.scala.Scala;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsScalaRulesDefinition;
import org.sonar.plugins.findbugs.util.JupiterLogTester;

class FindbugsScalaProfileTest {

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

@Test
void shouldCreateProfile() {
Expand All @@ -46,7 +45,7 @@ void shouldCreateProfile() {
BuiltInQualityProfile profile = context.profile(Scala.KEY, FindbugsSecurityScalaProfile.FINDBUGS_SECURITY_SCALA_PROFILE_NAME);
assertThat(profile.rules()).hasSize(FindSecurityBugsScalaRulesDefinition.RULE_COUNT);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsScalaRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(FindSecurityBugsScalaRulesDefinition.RULE_COUNT);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(Level.ERROR)).isEmpty();

FindbugsProfileTest.assertHasOnlyRulesForLanguage(profile.rules(), Scala.KEY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import org.sonar.plugins.findbugs.util.JupiterLogTester;
import org.sonar.plugins.java.Java;

import static org.assertj.core.api.Assertions.assertThat;

class FindbugsSecurityAuditProfileTest {

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

@Test
void shouldCreateProfile() {
Expand All @@ -48,9 +47,9 @@ void shouldCreateProfile() {

// The standard FindBugs include only 9. Fb-Contrib and FindSecurityBugs include other rules
BuiltInQualityProfile profile = context.profile(Java.KEY, FindbugsSecurityAuditProfile.FINDBUGS_SECURITY_AUDIT_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(Level.ERROR)).isEmpty();
// FSB rules must be added to FsbClassifier.groovy otherwise new rules metadata are not added in rules-findsecbugs.xml
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(logTester.getLogs(Level.WARN)).isEmpty();
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(8);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsRulesDefinition.REPOSITORY_KEY)).count())
.isEqualTo(FindSecurityBugsRulesDefinition.RULE_COUNT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.plugins.findbugs.FindbugsProfileImporter;
import org.sonar.plugins.findbugs.language.Jsp;
import org.sonar.plugins.findbugs.rule.FakeRuleFinder;
import org.sonar.plugins.findbugs.rules.FindSecurityBugsJspRulesDefinition;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import org.sonar.plugins.findbugs.util.JupiterLogTester;

import static org.assertj.core.api.Assertions.assertThat;

class FindbugsSecurityJspProfileTest {

@RegisterExtension
public LogTester logTester = new JupiterLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5();

@Test
void shouldCreateProfile() {
Expand All @@ -49,8 +48,8 @@ void shouldCreateProfile() {

//There are 6 rules that are JSP specific (the other findbugs rules can also be found in JSP files)
BuiltInQualityProfile profile = context.profile(Jsp.KEY, FindbugsSecurityJspProfile.FINDBUGS_SECURITY_JSP_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(logTester.getLogs(Level.ERROR)).isEmpty();
assertThat(logTester.getLogs(Level.WARN)).isEmpty();
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(6);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isZero();

Expand All @@ -72,8 +71,8 @@ void disabledRuleMustNotBeActivated() {

//There should be 5 rules left since we removed one
BuiltInQualityProfile profile = context.profile(Jsp.KEY, FindbugsSecurityJspProfile.FINDBUGS_SECURITY_JSP_PROFILE_NAME);
assertThat(logTester.getLogs(LoggerLevel.ERROR)).isNull();
assertThat(logTester.getLogs(LoggerLevel.WARN)).isNull();
assertThat(logTester.getLogs(Level.ERROR)).isEmpty();
assertThat(logTester.getLogs(Level.WARN)).isEmpty();
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY)).count()).isEqualTo(5);
assertThat(profile.rules().stream().filter(r -> r.repoKey().equals(FindbugsRulesDefinition.REPOSITORY_KEY)).count()).isZero();
}
Expand Down
Loading

0 comments on commit b2fa625

Please sign in to comment.