Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@
*/
package org.sonar.samples.java.checks;

import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
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(Level.DEBUG);

@Test
void detected() {
// Verifies that the check will raise the adequate issues with the expected message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@
*/
package org.sonar.samples.java.checks;

import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
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(Level.DEBUG);

@Test
void detected() {
// Use an instance of the check under test to raise the issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
Expand All @@ -42,7 +40,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.java.externalreport.ExternalReportTestUtils.onlyOneLogElement;

@EnableRuleMigrationSupport
class CheckstyleSensorTest {

private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "checkstyle")
Expand All @@ -51,8 +48,8 @@ class CheckstyleSensorTest {
private static SensorContextTester sensorContext = SensorContextTester.create(PROJECT_DIR);
private static CheckstyleSensor checkstyleSensor = new CheckstyleSensor(sensorContext.runtime());

@Rule
public final TemporaryFolder tmp = new TemporaryFolder();
@TempDir
Path tmp;

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
Expand Down Expand Up @@ -50,14 +49,14 @@ public static String onlyOneLogElement(List<String> elements) {
return elements.get(0);
}

public static File generateReport(Path projectDir, TemporaryFolder tmp, String fileName) throws IOException {
public static File generateReport(Path projectDir, Path tmp, String fileName) throws IOException {
Path filePath = projectDir.resolve(fileName);
if (!filePath.toFile().exists()) {
return filePath.toFile();
}
String reportData = new String(Files.readAllBytes(filePath), UTF_8);
reportData = reportData.replace("${PROJECT_DIR}", projectDir.toRealPath() + File.separator);
File reportFile = tmp.newFile(fileName).getCanonicalFile();
File reportFile = tmp.resolve(fileName).toFile().getCanonicalFile();
Files.write(reportFile.toPath(), reportData.getBytes(UTF_8));
return reportFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
Expand All @@ -43,7 +41,6 @@
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.java.externalreport.ExternalReportTestUtils.onlyOneLogElement;

@EnableRuleMigrationSupport
class SpotBugsSensorTest {

private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "spotbugs")
Expand All @@ -52,8 +49,8 @@ class SpotBugsSensorTest {
private static SensorContextTester sensorContext = SensorContextTester.create(PROJECT_DIR);
private static SpotBugsSensor spotBugsSensor = new SpotBugsSensor(sensorContext.runtime());

@Rule
public final TemporaryFolder tmp = new TemporaryFolder();
@TempDir
Path tmp;

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.io.TempDir;

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

@EnableRuleMigrationSupport
class FilesUtilsTest {
@org.junit.Rule
public TemporaryFolder temp = new TemporaryFolder();
@TempDir
Path temp;

@Test
void verify_get_classpath_files() throws IOException {
Path tmp = temp.newFolder().toPath();
Path tmp = Files.createTempDirectory(temp, "");
Path jar = tmp.resolve("test.jar");
Path zip = tmp.resolve("test.zip");
Path invalid = tmp.resolve("test.txt");
Expand All @@ -43,7 +41,7 @@ void verify_get_classpath_files() throws IOException {
Files.createFile(zip);
Files.createFile(invalid);

List<File> list = FilesUtils.getFilesRecursively(temp.getRoot().toPath(), new String[] {"zip", "jar"});
List<File> list = FilesUtils.getFilesRecursively(temp, new String[] {"zip", "jar"});
assertThat(list).containsOnly(jar.toFile(), zip.toFile());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,23 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.io.TempDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.internal.MapSettings;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.java.InputFileUtils.addFile;

@EnableRuleMigrationSupport
class BatchGeneratorTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@TempDir
Path temp;

@Test
void batch_generator_returns_an_empty_list_when_no_input_files() {
Expand All @@ -48,7 +46,7 @@ void batch_generator_returns_an_empty_list_when_no_input_files() {

@Test
void batch_generator_returns_at_most_one_item_per_batch_when_size_is_zero() throws IOException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContext = SensorContextTester.create(baseDir);
sensorContext.setSettings(new MapSettings());
List<InputFile> inputFiles = new ArrayList<>();
Expand All @@ -69,7 +67,7 @@ void batch_generator_returns_at_most_one_item_per_batch_when_size_is_zero() thro

@Test
void batch_generator_returns_batches_with_multiple_files_that_are_smaller_than_batch_size() throws IOException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContext = SensorContextTester.create(baseDir);
sensorContext.setSettings(new MapSettings());
InputFile fileA = addFile(temp, "class A { public void doSomething() {} }", sensorContext);
Expand Down Expand Up @@ -107,7 +105,7 @@ void batch_generator_returns_batches_with_multiple_files_that_are_smaller_than_b

@Test
void batch_generator_includes_file_excluded_from_previous_batch_into_next_batch() throws IOException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContext = SensorContextTester.create(baseDir);
sensorContext.setSettings(new MapSettings());
InputFile fileA = addFile(temp, "class A { public void doSomething() {} }", sensorContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.internal.SensorContextTester;

class InputFileUtils {
public static InputFile addFile(TemporaryFolder temp, String code, SensorContextTester context) throws IOException {
public static InputFile addFile(Path temp, String code, SensorContextTester context) throws IOException {
Matcher matcher = Pattern.compile("(?:^|\\s)(?:class|interface|enum|record)\\s++(\\w++)").matcher(code);
if (matcher.find()) {
String className = matcher.group(1);
InputFile.Type type = className.endsWith("Test") ? InputFile.Type.TEST : InputFile.Type.MAIN;
File file = temp.newFile(className + ".java").getAbsoluteFile();
File file = temp.resolve(className + ".java").toFile().getAbsoluteFile();
return generateInputFile(code, context, file, type);
} else {
File file = temp.newFile("Unnamed.java").getAbsoluteFile();
File file = temp.resolve("Unnamed.java").toFile().getAbsoluteFile();
return generateInputFile(code, context, file, InputFile.Type.MAIN);
}
}
Expand Down
28 changes: 13 additions & 15 deletions java-frontend/src/test/java/org/sonar/java/JavaFrontendTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import org.eclipse.core.runtime.OperationCanceledException;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.rules.TemporaryFolder;
import org.slf4j.event.Level;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
Expand Down Expand Up @@ -83,7 +82,6 @@
import static org.sonar.java.InputFileUtils.addFile;
import static org.sonar.java.TestUtils.mockSonarComponents;

@EnableRuleMigrationSupport
class JavaFrontendTest {


Expand All @@ -95,8 +93,8 @@ class JavaFrontendTest {
private static final Version LATESTS_SONAR_API_VERSION = Version.create(8, 13);
public static final SonarRuntime SONARQUBE_RUNTIME = SonarRuntimeImpl.forSonarQube(LATESTS_SONAR_API_VERSION, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);

@Rule
public TemporaryFolder temp = new TemporaryFolder();
@TempDir
Path temp;

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
Expand Down Expand Up @@ -211,7 +209,7 @@ public void scanFile(JavaFileScannerContext context) {

var settings = new MapSettings();
if (sensorContext == null) {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
sensorContext = SensorContextTester.create(baseDir);
sensorContext.setSettings(settings);
}
Expand Down Expand Up @@ -239,7 +237,7 @@ void scanning_empty_project_should_be_logged_in_autoscan() {

@Test
void test_scan_logs_when_caching_is_enabled_and_can_skip_unchanged_files() throws ApiMismatchException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContextTester = SensorContextTester.create(baseDir);
sensorContextTester.setSettings(new MapSettings());

Expand Down Expand Up @@ -273,7 +271,7 @@ void test_scan_logs_when_caching_is_enabled_and_can_skip_unchanged_files() throw

@Test
void test_scan_logs_when_caching_is_enabled_and_cannot_skip_unchanged_files() throws ApiMismatchException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContextTester = SensorContextTester.create(baseDir);
sensorContextTester.setSettings(new MapSettings());

Expand Down Expand Up @@ -307,7 +305,7 @@ void test_scan_logs_when_caching_is_enabled_and_cannot_skip_unchanged_files() th

@Test
void test_scan_logs_when_caching_is_enabled_and_cannot_determine_if_unchanged_files_can_be_skipped() throws ApiMismatchException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContextTester = SensorContextTester.create(baseDir);
sensorContextTester.setSettings(new MapSettings());

Expand Down Expand Up @@ -341,7 +339,7 @@ void test_scan_logs_when_caching_is_enabled_and_cannot_determine_if_unchanged_fi

@Test
void test_scan_logs_when_caching_is_disabled_and_can_skip_unchanged_files() throws ApiMismatchException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContextTester = SensorContextTester.create(baseDir);
sensorContextTester.setSettings(new MapSettings());

Expand Down Expand Up @@ -374,7 +372,7 @@ void test_scan_logs_when_caching_is_disabled_and_can_skip_unchanged_files() thro

@Test
void test_scan_logs_when_caching_is_disabled_and_cannot_skip_unchanged_files() throws ApiMismatchException {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContextTester = SensorContextTester.create(baseDir);
sensorContextTester.setSettings(new MapSettings());

Expand Down Expand Up @@ -407,7 +405,7 @@ void test_scan_logs_when_caching_is_disabled_and_cannot_skip_unchanged_files() t

@Test
void test_scan_logs_when_caching_is_disabled_when_sonar_components_is_null() {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
SensorContextTester sensorContextTester = SensorContextTester.create(baseDir);
sensorContextTester.setSettings(new MapSettings());

Expand Down Expand Up @@ -751,7 +749,7 @@ private List<InputFile> scan(SonarRuntime sonarRuntime, String... codeList) thro

private List<InputFile> scan(MapSettings settings, SonarRuntime sonarRuntime, String... codeList) throws IOException {
if (sensorContext == null) {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
sensorContext = SensorContextTester.create(baseDir);
sensorContext.setSettings(settings);
}
Expand All @@ -768,7 +766,7 @@ private List<InputFile> scan(MapSettings settings, SonarRuntime sonarRuntime, Li

private List<InputFile> scan(MapSettings settings, SonarRuntime sonarRuntime, List<InputFile> inputFiles, @Nullable CheckRegistrar[] checkRegistrars) {
if (sensorContext == null) {
File baseDir = temp.getRoot().getAbsoluteFile();
File baseDir = temp.toFile().getAbsoluteFile();
sensorContext = SensorContextTester.create(baseDir);
sensorContext.setSettings(settings);
}
Expand Down
Loading
Loading