Skip to content

Commit

Permalink
[NO JIRA] Update dependencies after the release
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill authored Oct 22, 2024
1 parent d914d02 commit d0b10f5
Show file tree
Hide file tree
Showing 58 changed files with 428 additions and 353 deletions.
8 changes: 7 additions & 1 deletion docs/java-custom-rules-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>9.9.0.229</version>
<version>10.12.0.2522</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -46,6 +46,12 @@
</dependency>

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

<dependency>
<groupId>org.sonarsource.java</groupId>
<artifactId>java-checks-testkit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package org.sonar.samples.java.checks;

import java.util.Locale;
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.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
Expand All @@ -17,7 +17,7 @@
@Rule(key = "AvoidBrandInMethodNames")
public class AvoidBrandInMethodNamesRule extends BaseTreeVisitor implements JavaFileScanner {

private static final Logger LOGGER = Loggers.get(AvoidBrandInMethodNamesRule.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AvoidBrandInMethodNamesRule.class);

private JavaFileScannerContext context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package org.sonar.samples.java.checks;

import java.util.List;
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.java.api.JavaFileScanner;
Expand All @@ -26,7 +26,7 @@
@Rule(key = "SecurityAnnotationMandatory")
public class SecurityAnnotationMandatoryRule extends BaseTreeVisitor implements JavaFileScanner {

private static final Logger LOGGER = Loggers.get(SecurityAnnotationMandatoryRule.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SecurityAnnotationMandatoryRule.class);

private static final String DEFAULT_VALUE = "MySecurityAnnotation";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.java.checks.verifier.CheckVerifier;

@EnableRuleMigrationSupport
class AvoidBrandInMethodNamesRuleTest {

// Set a LogTester to see the Syntax Tree when running tests and executing the rule
@Rule
public LogTester logTester = new LogTester().setLevel(LoggerLevel.DEBUG);
public LogTester logTester = new LogTester().setLevel(Level.DEBUG);

@Test
void detected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.java.checks.verifier.CheckVerifier;

@EnableRuleMigrationSupport
class SecurityAnnotationMandatoryRuleTest {

// Set a LogTester to see the Syntax Tree when running tests and executing the rule
@Rule
public LogTester logTester = new LogTester().setLevel(LoggerLevel.DEBUG);
public LogTester logTester = new LogTester().setLevel(Level.DEBUG);

@Test
void detected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
Expand All @@ -43,11 +44,20 @@ public class CheckstyleSensor implements Sensor {
public static final String REPORT_PROPERTY_KEY = "sonar.java.checkstyle.reportPaths";
public static final String LINTER_KEY = "checkstyle";

public static final ExternalRuleLoader RULE_LOADER = new ExternalRuleLoader(
CheckstyleSensor.LINTER_KEY,
CheckstyleSensor.LINTER_NAME,
"org/sonar/l10n/java/rules/checkstyle/rules.json",
CheckstyleSensor.LANGUAGE_KEY);
private final ExternalRuleLoader ruleLoader;

public CheckstyleSensor(SonarRuntime sonarRuntime) {
ruleLoader = new ExternalRuleLoader(
CheckstyleSensor.LINTER_KEY,
CheckstyleSensor.LINTER_NAME,
"org/sonar/l10n/java/rules/checkstyle/rules.json",
CheckstyleSensor.LANGUAGE_KEY,
sonarRuntime);
}

public ExternalRuleLoader ruleLoader() {
return ruleLoader;
}

@Override
public void describe(SensorDescriptor descriptor) {
Expand All @@ -60,20 +70,20 @@ public void describe(SensorDescriptor descriptor) {
@Override
public void execute(SensorContext context) {
List<File> reportFiles = ExternalReportProvider.getReportFiles(context, REPORT_PROPERTY_KEY);
reportFiles.forEach(report -> importIfExist(LINTER_NAME, context, report, CheckstyleSensor::importReport));
reportFiles.forEach(report -> importIfExist(LINTER_NAME, context, report, this::importReport));
}

private static void importReport(File reportPath, SensorContext context) {
private void importReport(File reportPath, SensorContext context) {
try (InputStream in = new FileInputStream(reportPath)) {
LOG.info("Importing {}", reportPath);
CheckstyleXmlReportReader.read(context, in, CheckstyleSensor::saveIssue);
CheckstyleXmlReportReader.read(context, in, this::saveIssue);
} catch (Exception e) {
LOG.error("Failed to import external issues report: " + reportPath, e);
LOG.error("Failed to import external issues report: {}", reportPath, e);
}
}

private static void saveIssue(SensorContext context, InputFile inputFile, String key, String line, String message) {
ExternalIssueUtils.saveIssue(context, RULE_LOADER, inputFile, CheckstyleSensor.LINTER_KEY, key, line, message);
private void saveIssue(SensorContext context, InputFile inputFile, String key, String line, String message) {
ExternalIssueUtils.saveIssue(context, ruleLoader, inputFile, CheckstyleSensor.LINTER_KEY, key, line, message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
Expand All @@ -41,11 +42,20 @@ public class PmdSensor implements Sensor {
private static final String LINTER_NAME = "PMD";
private static final String LANGUAGE_KEY = "java";

public static final ExternalRuleLoader RULE_LOADER = new ExternalRuleLoader(
PmdSensor.LINTER_KEY,
PmdSensor.LINTER_NAME,
"org/sonar/l10n/java/rules/pmd/rules.json",
PmdSensor.LANGUAGE_KEY);
private final ExternalRuleLoader ruleLoader;

public PmdSensor(SonarRuntime sonarRuntime) {
ruleLoader = new ExternalRuleLoader(
PmdSensor.LINTER_KEY,
PmdSensor.LINTER_NAME,
"org/sonar/l10n/java/rules/pmd/rules.json",
PmdSensor.LANGUAGE_KEY,
sonarRuntime);
}

public ExternalRuleLoader ruleLoader() {
return ruleLoader;
}

@Override
public void describe(SensorDescriptor descriptor) {
Expand All @@ -58,15 +68,15 @@ public void describe(SensorDescriptor descriptor) {
@Override
public void execute(SensorContext context) {
List<File> reportFiles = ExternalReportProvider.getReportFiles(context, REPORT_PROPERTY_KEY);
reportFiles.forEach(report -> importIfExist(LINTER_NAME, context, report, PmdSensor::importReport));
reportFiles.forEach(report -> importIfExist(LINTER_NAME, context, report, this::importReport));
}

private static void importReport(File reportFile, SensorContext context) {
private void importReport(File reportFile, SensorContext context) {
try {
LOG.info("Importing {}", reportFile);
PmdXmlReportReader.read(context, reportFile, RULE_LOADER);
PmdXmlReportReader.read(context, reportFile, ruleLoader);
} catch (Exception e) {
LOG.error("Failed to import external issues report: " + reportFile.getAbsolutePath(), e);
LOG.error("Failed to import external issues report: {}", reportFile.getAbsolutePath(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
Expand All @@ -49,23 +50,46 @@ public class SpotBugsSensor implements Sensor {
private static final String LANGUAGE_KEY = "java";
public static final String REPORT_PROPERTY_KEY = "sonar.java.spotbugs.reportPaths";

public static final ExternalRuleLoader RULE_LOADER = new ExternalRuleLoader(
SpotBugsSensor.SPOTBUGS_KEY,
SpotBugsSensor.SPOTBUGS_NAME,
"org/sonar/l10n/java/rules/spotbugs/spotbugs-rules.json",
SpotBugsSensor.LANGUAGE_KEY);
private final ExternalRuleLoader ruleLoader;

public static final ExternalRuleLoader FINDSECBUGS_LOADER = new ExternalRuleLoader(
SpotBugsSensor.FINDSECBUGS_KEY,
SpotBugsSensor.FINDSECBUGS_NAME,
"org/sonar/l10n/java/rules/spotbugs/findsecbugs-rules.json",
SpotBugsSensor.LANGUAGE_KEY);
private final ExternalRuleLoader findSecBugsLoader;

public static final ExternalRuleLoader FBCONTRIB_LOADER = new ExternalRuleLoader(
SpotBugsSensor.FBCONTRIB_KEY,
SpotBugsSensor.FBCONTRIB_NAME,
"org/sonar/l10n/java/rules/spotbugs/fbcontrib-rules.json",
SpotBugsSensor.LANGUAGE_KEY);
private final ExternalRuleLoader fbContribLoader;

public SpotBugsSensor(SonarRuntime sonarRuntime) {
ruleLoader = new ExternalRuleLoader(
SpotBugsSensor.SPOTBUGS_KEY,
SpotBugsSensor.SPOTBUGS_NAME,
"org/sonar/l10n/java/rules/spotbugs/spotbugs-rules.json",
SpotBugsSensor.LANGUAGE_KEY,
sonarRuntime);

findSecBugsLoader = new ExternalRuleLoader(
SpotBugsSensor.FINDSECBUGS_KEY,
SpotBugsSensor.FINDSECBUGS_NAME,
"org/sonar/l10n/java/rules/spotbugs/findsecbugs-rules.json",
SpotBugsSensor.LANGUAGE_KEY,
sonarRuntime);

fbContribLoader = new ExternalRuleLoader(
SpotBugsSensor.FBCONTRIB_KEY,
SpotBugsSensor.FBCONTRIB_NAME,
"org/sonar/l10n/java/rules/spotbugs/fbcontrib-rules.json",
SpotBugsSensor.LANGUAGE_KEY,
sonarRuntime);
}

public ExternalRuleLoader ruleLoader() {
return ruleLoader;
}

public ExternalRuleLoader findSecBugsLoader() {
return findSecBugsLoader;
}

public ExternalRuleLoader fbContribLoader() {
return fbContribLoader;
}

@Override
public void describe(SensorDescriptor descriptor) {
Expand All @@ -78,19 +102,19 @@ public void describe(SensorDescriptor descriptor) {
@Override
public void execute(SensorContext context) {
List<File> reportFiles = ExternalReportProvider.getReportFiles(context, REPORT_PROPERTY_KEY);
reportFiles.forEach(report -> importIfExist(SPOTBUGS_NAME, context, report, SpotBugsSensor::importReport));
reportFiles.forEach(report -> importIfExist(SPOTBUGS_NAME, context, report, this::importReport));
}

private static void importReport(File reportPath, SensorContext context) {
private void importReport(File reportPath, SensorContext context) {
try (InputStream in = new FileInputStream(reportPath)) {
LOG.info("Importing {}", reportPath);

Map<String, ExternalRuleLoader> otherLoaders = new HashMap<>();
otherLoaders.put(FINDSECBUGS_KEY, FINDSECBUGS_LOADER);
otherLoaders.put(FBCONTRIB_KEY, FBCONTRIB_LOADER);
SpotBugsXmlReportReader.read(context, in, RULE_LOADER, otherLoaders);
otherLoaders.put(FINDSECBUGS_KEY, findSecBugsLoader);
otherLoaders.put(FBCONTRIB_KEY, fbContribLoader);
SpotBugsXmlReportReader.read(context, in, ruleLoader, otherLoaders);
} catch (Exception e) {
LOG.error("Failed to import external issues report: " + reportPath, e);
LOG.error("Failed to import external issues report: {}", reportPath, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class CheckstyleSensorTest {
private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "checkstyle")
.toAbsolutePath().normalize();

private static CheckstyleSensor checkstyleSensor = new CheckstyleSensor();
private static SensorContextTester sensorContext = SensorContextTester.create(PROJECT_DIR);
private static CheckstyleSensor checkstyleSensor = new CheckstyleSensor(sensorContext.runtime());

@Rule
public final TemporaryFolder tmp = new TemporaryFolder();
Expand All @@ -62,7 +63,7 @@ class CheckstyleSensorTest {
@Test
void checkstyle_rules_definition() {
RulesDefinition.Context context = new RulesDefinition.Context();
new ExternalRulesDefinition(CheckstyleSensor.RULE_LOADER, CheckstyleSensor.LINTER_KEY).define(context);
new ExternalRulesDefinition(checkstyleSensor.ruleLoader(), CheckstyleSensor.LINTER_KEY).define(context);

assertThat(context.repositories()).hasSize(1);
RulesDefinition.Repository repository = context.repository("external_checkstyle");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
*/
package org.sonar.java.externalreport;

import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.sensor.internal.SensorContextTester;

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

class ExternalRulesDefinitionTest {

private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "spotbugs")
.toAbsolutePath().normalize();

@Test
void toString_should_exist_and_contains_linter_name() {
// to string is used by compute engine logs and should return a unique key
assertThat(new ExternalRulesDefinition(SpotBugsSensor.RULE_LOADER, "someLinterKey")).hasToString("someLinterKey-rules-definition");
SensorContextTester sensorContext = SensorContextTester.create(PROJECT_DIR);
var spotBugsSensor = new SpotBugsSensor(sensorContext.runtime());
assertThat(new ExternalRulesDefinition(spotBugsSensor.ruleLoader(), "someLinterKey")).hasToString("someLinterKey-rules-definition");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class PmdSensorTest {
private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "pmd");
private static final String PROJECT_ID = "pmd-test";

private static final PmdSensor sensor = new PmdSensor();
private static final SensorContextTester sensorContext = SensorContextTester.create(PROJECT_DIR);
private static final PmdSensor sensor = new PmdSensor(sensorContext.runtime());

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
Expand All @@ -72,7 +73,7 @@ void test_descriptor() {
@Test
void pmd_rules_definition() {
RulesDefinition.Context context = new RulesDefinition.Context();
new ExternalRulesDefinition(PmdSensor.RULE_LOADER, PmdSensor.LINTER_KEY).define(context);
new ExternalRulesDefinition(sensor.ruleLoader(), PmdSensor.LINTER_KEY).define(context);

assertThat(context.repositories()).hasSize(1);
RulesDefinition.Repository repository = context.repository("external_pmd");
Expand Down Expand Up @@ -210,7 +211,7 @@ private static void addFileToContext(SensorContextTester context, Path projectDi
context.fileSystem().add(TestInputFileBuilder.create(PROJECT_ID, projectDir.toFile(), file.toFile())
.setCharset(UTF_8)
.setLanguage(language(file))
.setContents(new String(Files.readAllBytes(file), UTF_8))
.setContents(Files.readString(file))
.setType(InputFile.Type.MAIN)
.build());
} catch (IOException e) {
Expand Down
Loading

0 comments on commit d0b10f5

Please sign in to comment.