Skip to content

Commit

Permalink
Update analyzer commons and fix loggers usage (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource authored Aug 15, 2023
1 parent bec7fa7 commit 34b16ba
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 39 deletions.
16 changes: 12 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@
<sonar.version>10.0.0.68432</sonar.version>
<sonar.minVersion>9.9</sonar.minVersion>

<sonar.analyzerCommons.version>2.5.0.1358</sonar.analyzerCommons.version>
<sonar.analyzerCommons.version>2.7.0.1482</sonar.analyzerCommons.version>
<sonar.orchestrator.version>3.42.0.312</sonar.orchestrator.version>
<sonar.sonarlint-core.version>8.15.0.65216</sonar.sonarlint-core.version>
<sonar.pluginApi.version>9.14.0.375</sonar.pluginApi.version>

<sonar.pluginApi.version>10.1.0.809</sonar.pluginApi.version>
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>

<!-- Release: enable publication to Bintray -->
Expand All @@ -85,7 +84,16 @@
<artifactId>sonar-analyzer-commons</artifactId>
<version>${sonar.analyzerCommons.version}</version>
</dependency>

<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api-test-fixtures</artifactId>
<version>${sonar.pluginApi.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-xml-parsing</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions sonar-xml-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api-test-fixtures</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.measures.Metric;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonarsource.analyzer.commons.xml.XmlFile;
import org.sonarsource.analyzer.commons.xml.XmlTextRange;
import org.w3c.dom.Document;
Expand All @@ -40,7 +40,7 @@

public final class LineCounter {

private static final Logger LOG = Loggers.get(LineCounter.class);
private static final Logger LOG = LoggerFactory.getLogger(LineCounter.class);

private LineCounter() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.FilePredicate;
Expand All @@ -40,8 +42,6 @@
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.plugins.xml.checks.CheckList;
import org.sonar.plugins.xml.checks.ParsingErrorCheck;
import org.sonarsource.analyzer.commons.ProgressReport;
Expand All @@ -51,7 +51,7 @@

public class XmlSensor implements Sensor {

private static final Logger LOG = Loggers.get(XmlSensor.class);
private static final Logger LOG = LoggerFactory.getLogger(XmlSensor.class);

private static final RuleKey PARSING_ERROR_RULE_KEY = RuleKey.of(Xml.REPOSITORY_KEY, ParsingErrorCheck.RULE_KEY);

Expand Down Expand Up @@ -142,7 +142,7 @@ void runCheck(SensorContext context, SonarXmlCheck check, RuleKey ruleKey, XmlFi
}

private static void logFailingRule(RuleKey rule, URI fileLocation, Exception e) {
LOG.error(String.format("Unable to execute rule %s on %s", rule, fileLocation), e);
LOG.error("Unable to execute rule {} on {}", rule, fileLocation, e);
}

@Override
Expand Down Expand Up @@ -175,7 +175,7 @@ private void processesFilesIndependently(SensorDescriptor descriptor) {
private void processParseException(Exception e, SensorContext context, InputFile inputFile) {
reportAnalysisError(e, context, inputFile);

LOG.warn(String.format("Unable to analyse file %s;", inputFile.uri()));
LOG.warn("Unable to analyse file {};", inputFile.uri());
LOG.debug("Cause: {}", e.getMessage());

if (parsingErrorCheckEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.WildcardPattern;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonarsource.analyzer.commons.xml.XmlFile;
Expand All @@ -46,7 +46,7 @@
@Rule(key = XPathCheck.RULE_KEY)
public class XPathCheck extends SonarXmlCheck {

private static final Logger LOG = Loggers.get(XPathCheck.class);
private static final Logger LOG = LoggerFactory.getLogger(XPathCheck.class);

public static final String RULE_KEY = "XPathCheck";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import javax.xml.xpath.XPathExpression;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.xml.checks.maven.helpers.MavenDependencyMatcher;
Expand All @@ -40,7 +40,7 @@
@DeprecatedRuleKey(repositoryKey = "java", ruleKey = DisallowedDependenciesCheck.KEY)
public class DisallowedDependenciesCheck extends SimpleXPathBasedCheck {

private static final Logger LOG = Loggers.get(DisallowedDependenciesCheck.class);
private static final Logger LOG = LoggerFactory.getLogger(DisallowedDependenciesCheck.class);

public static final String KEY = "S3417";

Expand Down Expand Up @@ -115,7 +115,7 @@ private MavenDependencyMatcher getMatcher() {
try {
dependencyMatcher = new MavenDependencyMatcher(dependencyName, version);
} catch (RuntimeException e) {
LOG.error("The rule xml:" + KEY + " is configured with some invalid parameters. " + e.getMessage());
LOG.error("The rule xml:{} is configured with some invalid parameters. {}", KEY, e.getMessage());
}
}
return dependencyMatcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void test() {
rulesDefinition.define(context);
RulesDefinition.Repository repository = context.repository("xml");

assertThat(repository.name()).isEqualTo("SonarQube");
assertThat(repository.name()).isEqualTo("Sonar");
assertThat(repository.language()).isEqualTo("xml");
assertThat(repository.rules()).hasSize(CheckList.getCheckClasses().size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import org.apache.commons.lang.NotImplementedException;
import org.assertj.core.api.Condition;
import org.junit.Rule;
Expand All @@ -41,6 +40,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
Expand All @@ -66,10 +66,8 @@
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.LogAndArguments;
import org.sonar.api.utils.log.LogTesterJUnit5;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.plugins.xml.checks.TabCharacterCheck;
import org.sonarsource.analyzer.commons.xml.XmlFile;
import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheck;
Expand All @@ -87,7 +85,7 @@ class XmlSensorTest {
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

private DefaultFileSystem fs;
private XmlSensor sensor;
Expand Down Expand Up @@ -133,8 +131,8 @@ void test_not_xml_web_config() throws Exception {
sensor.execute(context);

assertThat(context.allIssues()).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(logTester.logs(Level.WARN)).isEmpty();
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}

@Test
Expand Down Expand Up @@ -281,9 +279,9 @@ public void scanFile(XmlFile file) {
assertThat(context.allIssues()).isEmpty();
assertThat(logTester.getLogs()).isNotEmpty();

List<LogAndArguments> errors = logTester.getLogs(LoggerLevel.ERROR);
List<String> errors = logTester.logs(Level.ERROR);
assertThat(errors).hasSize(1);
assertThat(errors.get(0).getRawMsg()).startsWith("Unable to execute rule xml:S666");
assertThat(errors.get(0)).startsWith("Unable to execute rule xml:S666");
}

/**
Expand Down Expand Up @@ -348,8 +346,8 @@ void should_log_a_warning_if_file_does_not_exist() throws Exception {
sensor.execute(context);

assertThat(context.allIssues()).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Unable to analyse file " + invalidFile.uri() + ";");
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(logTester.logs(Level.WARN)).contains("Unable to analyse file " + invalidFile.uri() + ";");
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}

private void init() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.sonar.api.utils.log.LogTesterJUnit5;
import org.sonar.api.utils.log.LoggerLevel;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheckVerifier;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -109,12 +109,12 @@ void test_failure_without_log() {
check.setExpression("//comment()");

logTester.clear();
logTester.setLevel(LoggerLevel.INFO);
logTester.setLevel(Level.INFO);
SonarXmlCheckVerifier.verifyNoIssue("../xPathFailure.xml", check);
assertThat(logTester.getLogs()).isEmpty();

logTester.clear();
logTester.setLevel(LoggerLevel.DEBUG);
logTester.setLevel(Level.DEBUG);
SonarXmlCheckVerifier.verifyNoIssue("../xPathFailure.xml", check);
assertThat(logTester.getLogs()).isNotEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.sonar.api.utils.log.LogTesterJUnit5;
import org.sonar.api.utils.log.LoggerLevel;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheckVerifier;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -65,7 +65,7 @@ void should_log_error_when_invalid_dependency_name_is_provided() {
check.dependencyName = "org.sonar";
check.version = "";
SonarXmlCheckVerifier.verifyNoIssue("noVersion/pom.xml", check);
assertThat(logTester.logs(LoggerLevel.ERROR))
assertThat(logTester.logs(Level.ERROR))
.containsExactly("The rule xml:S3417 is configured with some invalid parameters." +
" Invalid DependencyName pattern 'org.sonar'." +
" Should match '[groupId]:[artifactId]', you can use '*' as wildcard or a regular expression." +
Expand All @@ -77,7 +77,7 @@ void should_log_error_when_invalid_dependency_version_is_provided() {
check.dependencyName = "org.sonar.*:*";
check.version = "version-0";
SonarXmlCheckVerifier.verifyNoIssue("noVersion/pom.xml", check);
assertThat(logTester.logs(LoggerLevel.ERROR))
assertThat(logTester.logs(Level.ERROR))
.containsExactly("The rule xml:S3417 is configured with some invalid parameters." +
" Invalid Version pattern 'version-0'." +
" Leave blank for all versions. You can use '*' as wildcard and '-' as range like '1.0-3.1' or '*-3.1'." +
Expand Down

0 comments on commit 34b16ba

Please sign in to comment.