Skip to content

Commit

Permalink
SONARJAVA-3821 cleanup 'java-checks-testkit' to not rely on 'sonar-pl…
Browse files Browse the repository at this point in the history
…ugin-api-impl'
  • Loading branch information
Wohops authored and quentin-jaquier-sonarsource committed Apr 29, 2021
1 parent 7d0a709 commit 1b1e967
Show file tree
Hide file tree
Showing 27 changed files with 1,225 additions and 92 deletions.
32 changes: 14 additions & 18 deletions java-checks-testkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,11 @@
<artifactId>java-frontend</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand All @@ -43,15 +32,22 @@
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>

<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api-impl</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.sonar.sslr.api.RecognitionException;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -40,25 +39,22 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.utils.Version;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.config.Configuration;
import org.sonar.java.AnalyzerMessage;
import org.sonar.java.SonarComponents;
import org.sonar.java.annotations.Beta;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.checks.verifier.internal.InternalSensorContext;
import org.sonar.java.classpath.ClasspathForMain;
import org.sonar.java.classpath.ClasspathForTest;
import org.sonar.java.model.JavaVersionImpl;
import org.sonar.java.model.VisitorsBridgeForTests;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaVersion;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.sonar.java.checks.verifier.Expectations.IssueAttribute.EFFORT_TO_FIX;
import static org.sonar.java.checks.verifier.Expectations.IssueAttribute.END_COLUMN;
import static org.sonar.java.checks.verifier.Expectations.IssueAttribute.END_LINE;
Expand Down Expand Up @@ -148,7 +144,10 @@ public InternalCheckVerifier onFiles(String... filenames) {
public InternalCheckVerifier onFiles(Collection<String> filenames) {
requiresNull(files, FILE_OR_FILES);
requiresNonEmpty(filenames, "file");
files = filenames.stream().map(File::new).map(InternalCheckVerifier::inputFile).collect(Collectors.toList());
files = filenames.stream()
.map(File::new)
.map(TestUtils::inputFile)
.collect(Collectors.toList());
return this;
}

Expand Down Expand Up @@ -536,28 +535,13 @@ private static void requiresNonEmpty(Collection<?> objects, String fieldName) {
}
}

private static InputFile inputFile(File file) {
try {
return new TestInputFileBuilder("", file.getPath())
.setContents(new String(Files.readAllBytes(file.toPath()), UTF_8))
.setCharset(UTF_8)
.setLanguage("java")
.build();
} catch (Exception e) {
throw new IllegalStateException(String.format("Unable to read file '%s", file.getAbsolutePath()));
}
}

private static SonarComponents sonarComponents() {
SensorContextTester context = SensorContextTester.create(new File(""))
.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
MapSettings settings = new MapSettings();
DefaultFileSystem fileSystem = context.fileSystem();

context.setSettings(settings.setProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, true));
SensorContext context = new InternalSensorContext();
FileSystem fileSystem = context.fileSystem();
Configuration config = context.config();

ClasspathForMain classpathForMain = new ClasspathForMain(context.config(), fileSystem);
ClasspathForTest classpathForTest = new ClasspathForTest(context.config(), fileSystem);
ClasspathForMain classpathForMain = new ClasspathForMain(config, fileSystem);
ClasspathForTest classpathForTest = new ClasspathForTest(config, fileSystem);

SonarComponents sonarComponents = new SonarComponents(null, fileSystem, classpathForMain, classpathForTest, null) {
@Override
Expand All @@ -568,5 +552,4 @@ public boolean reportAnalysisError(RecognitionException re, InputFile inputFile)
sonarComponents.setSensorContext(context);
return sonarComponents;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;

import static java.nio.charset.StandardCharsets.UTF_8;
import org.sonar.java.checks.verifier.internal.InternalInputFile;

public class TestUtils {
private TestUtils() {
Expand Down Expand Up @@ -56,34 +53,22 @@ private static String getFileFrom(String path, String relocated) {
}

public static InputFile emptyInputFile(String filename) {
return emptyInputFile(filename, InputFile.Type.MAIN);
return InternalInputFile.emptyInputFile(filename, InputFile.Type.MAIN);
}

public static InputFile emptyInputFile(String filename, InputFile.Type type) {
return new TestInputFileBuilder("", filename)
.setCharset(UTF_8)
.setLanguage("java")
.setType(type)
.build();
return InternalInputFile.emptyInputFile(filename, type);
}

public static InputFile inputFile(String filepath) {
return inputFile("", new File(filepath));
return InternalInputFile.inputFile("", new File(filepath));
}

public static InputFile inputFile(File file) {
return inputFile("", file);
return InternalInputFile.inputFile("", file);
}

public static InputFile inputFile(String moduleKey, File file) {
try {
return new TestInputFileBuilder(moduleKey, file.getPath())
.setContents(new String(Files.readAllBytes(file.toPath()), UTF_8))
.setCharset(UTF_8)
.setLanguage("java")
.build();
} catch (Exception e) {
throw new IllegalStateException(String.format("Unable to read file '%s", file.getAbsolutePath()));
}
return InternalInputFile.inputFile(moduleKey, file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SonarQube Java
* Copyright (C) 2012-2021 SonarSource SA
* mailto:info AT sonarsource DOT 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 02110-1301, USA.
*/
package org.sonar.java.checks.verifier.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.sonar.api.config.Configuration;
import org.sonar.java.SonarComponents;

final class InternalConfiguration extends InternalMockedSonarAPI implements Configuration {
private final Map<String, String> properties = new HashMap<>();

InternalConfiguration() {
properties.put(SonarComponents.FAIL_ON_EXCEPTION_KEY, Boolean.toString(true));
}

@Override
public boolean hasKey(String arg0) {
return properties.containsKey(arg0);
}

@Override
public Optional<String> get(String arg0) {
return Optional.ofNullable(properties.get(arg0));
}

@Override
public String[] getStringArray(String arg0) {
throw notSupportedException("getStringArray(String)");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* SonarQube Java
* Copyright (C) 2012-2021 SonarSource SA
* mailto:info AT sonarsource DOT 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 02110-1301, USA.
*/
package org.sonar.java.checks.verifier.internal;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
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.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;

final class InternalFileSystem extends InternalMockedSonarAPI implements FileSystem {
private static final TreeSet<String> LANGUAGES = new TreeSet<>(Collections.singleton("java"));
private static final File BASE_DIR = new File(".");

@Override
public File baseDir() {
return BASE_DIR;
}

@Override
public Charset encoding() {
return StandardCharsets.UTF_8;
}

@Override
public Iterable<File> files(FilePredicate arg0) {
return Collections.emptyList();
}

@Override
public boolean hasFiles(FilePredicate arg0) {
return false;
}

@Override
public SortedSet<String> languages() {
return LANGUAGES;
}

@Override
public InputDir inputDir(File arg0) {
throw notSupportedException("inputDir(File)");
}

@Override
public InputFile inputFile(FilePredicate arg0) {
throw notSupportedException("inputFile(FilePredicate)");
}

@Override
public Iterable<InputFile> inputFiles(FilePredicate arg0) {
throw notSupportedException("inputFiles(FilePredicate)");
}

@Override
public FilePredicates predicates() {
throw notSupportedException("predicates()");
}

@Override
public File resolvePath(String arg0) {
throw notSupportedException("resolvePath(String)");
}

@Override
public File workDir() {
throw notSupportedException("workDir()");
}
}
Loading

0 comments on commit 1b1e967

Please sign in to comment.