Skip to content
Merged
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
1 change: 1 addition & 0 deletions java-checks-test-sources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<execution>
<id>copy</id>
<phase>test-compile</phase>
<inherited>false</inherited>
<goals>
<goal>copy</goal>
</goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public final class TestClasspathUtils {
public static final Module JAVA_17_MODULE = new Module("java-checks-test-sources/java-17");
public static final Module SPRING_32_MODULE = new Module("java-checks-test-sources/spring-3.2");
public static final Module SPRING_WEB_40_MODULE = new Module("java-checks-test-sources/spring-web-4.0");
private static final Path testJarsPath = Path.of("java-checks-test-sources/target/test-jars");

public static List<File> getTestJars(List<String> jars) {
return jars.stream().map(name -> testJarsPath.resolve(name + ".jar").toFile()).toList();
}

public static class Module {
private final String relativePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ static CheckVerifier newInternalVerifier() {
*/
CheckVerifier withClassPath(Collection<File> classpath);

/**
* Defines the libraries to add to the classpath.
* Usually used when the code of the test files requires the knowledge of a particular set
* of libraries or java compiled classes.
* <p>
* <strong>Note:</strong> The JAR files must be pre-downloaded using the {@code maven-dependency-plugin}
* configured in {@code java-checks-test-sources/pom.xml}. The plugin's {@code copy} execution
* (phase: {@code test-compile}) downloads dependencies to {@code target/test-jars} directory.
* Add new dependencies as {@code artifactItem} entries in the plugin configuration.
*
* @param jarsToAdd a collection of jar names (without extensions) to add to the classpath
* @return the verifier configured to use the dependencies provided as arguments to change the classpath
*/
CheckVerifier addJarsToClasspath(String... jarsToAdd);

/**
* Defines the libraries to remove from the classpath.
* Usually used when the code of the test files requires the knowledge of a particular set
* of libraries or java compiled classes.
*
* @param jarsToRemove a collection of jar names (without extensions) to remove from the classpath
* @return the verifier configured to use the dependencies provided as arguments to change the classpath
*/
CheckVerifier removeJarsFromClasspath(String... jarsToRemove);

/**
* Defines the java version syntax to be used for the verification. Usually used when the code of the
* test files explicitly target a given version (e.g. java 7) where a particular syntax/API has been introduced.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ public InternalCheckVerifier withClassPath(Collection<File> classpath) {
return this;
}

@Override
public CheckVerifier addJarsToClasspath(String... jarsToAdd) {
throw new UnsupportedOperationException("Not implemented!");
}

@Override
public CheckVerifier removeJarsFromClasspath(String... jarsToRemove) {
throw new UnsupportedOperationException("Not implemented!");
}

@Beta
public InternalCheckVerifier withCustomIssueVerifier(Consumer<Set<AnalyzerMessage>> customIssueVerifier) {
requiresNull(this.customIssueVerifier, "custom issue verifier");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public static JavaCheckVerifier newInstance() {

private List<JavaFileScanner> checks = null;
private List<File> classpath = null;
private List<String> jarsToAdd = new ArrayList<>();
private List<String> jarsToRemove = new ArrayList<>();
@VisibleForTesting
List<File> actualClasspath = null;
private JavaVersion javaVersion = null;
private boolean inAndroidContext = false;
private List<InputFile> files = null;
Expand All @@ -96,7 +100,6 @@ private MultiFileVerifier createVerifier() {
MultiFileVerifier verifier = MultiFileVerifier.create(Paths.get(files.get(0).uri()), UTF_8);

JavaVersion actualVersion = javaVersion == null ? DEFAULT_JAVA_VERSION : javaVersion;
List<File> actualClasspath = classpath == null ? TestClasspathUtils.DEFAULT_MODULE.getClassPath() : classpath;

List<JavaFileScanner> visitors = new ArrayList<>(checks);
CommentLinesVisitor commentLinesVisitor = new CommentLinesVisitor();
Expand All @@ -107,6 +110,7 @@ private MultiFileVerifier createVerifier() {
.withSonarComponents(sonarComponents)
.withAndroidContext(inAndroidContext);
if (!withoutSemantic) {
setActualClasspath();
visitorsBridgeBuilder.enableSemanticWithProjectClasspath(actualClasspath);
}

Expand Down Expand Up @@ -135,6 +139,13 @@ private MultiFileVerifier createVerifier() {
return verifier;
}

private void setActualClasspath() {
List<File> newClasspath = classpath == null ? new ArrayList<>(TestClasspathUtils.DEFAULT_MODULE.getClassPath()) : classpath;
jarsToRemove.forEach(f -> newClasspath.removeIf(file -> file.getName().contains(f)));
newClasspath.addAll(TestClasspathUtils.getTestJars(jarsToAdd));
actualClasspath = Collections.unmodifiableList(newClasspath);
}

private static void addIssues(JavaFileScannerContextForTests scannerContext, MultiFileVerifier verifier) {
scannerContext.getIssues().forEach(issue -> {
if (!issue.getInputComponent().isFile()) {
Expand Down Expand Up @@ -232,6 +243,18 @@ public CheckVerifier withClassPath(Collection<File> classpath) {
return this;
}

@Override
public CheckVerifier addJarsToClasspath(String... jarsToAdd) {
this.jarsToAdd.addAll(Arrays.asList(jarsToAdd));
return this;
}

@Override
public CheckVerifier removeJarsFromClasspath(String... jarsToRemove) {
this.jarsToRemove.addAll(Arrays.asList(jarsToRemove));
return this;
}

@Override
public CheckVerifier withJavaVersion(int javaVersionAsInt) {
requiresNull(javaVersion, "java version");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,4 +1067,32 @@ void compilationUnitModifier_not_supported() {
.hasMessage("Method not implemented, feel free to implement.");
}

@Test
void addJarsToClasspath_not_supported() {
InternalCheckVerifier checkVerifier = InternalCheckVerifier.newInstance()
.onFile(TEST_FILE);

Throwable e = catchThrowable(() -> {
checkVerifier.addJarsToClasspath("testng-7.5.1");
});

assertThat(e)
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Not implemented!");
}

@Test
void removeJarsFromClasspath_not_supported() {
InternalCheckVerifier checkVerifier = InternalCheckVerifier.newInstance()
.onFile(TEST_FILE);

Throwable e = catchThrowable(() -> {
checkVerifier.removeJarsFromClasspath("testng-7.12.0");
});

assertThat(e)
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Not implemented!");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,40 @@ void succeeding_check_on_both_files_should_make_verifier_succeed() {
.onFiles(CheckVerifierTestUtils.TEST_FILE, CheckVerifierTestUtils.TEST_FILE_2)
.verifyNoIssues());
}

@Test
void addJarsToClasspath_modifies_classpath() {
JavaCheckVerifier dummyVerifier = JavaCheckVerifier.newInstance();
dummyVerifier.withCheck(NO_EFFECT_CHECK)
.onFile(TEST_FILE)
.verifyNoIssues();
int initialSize = dummyVerifier.actualClasspath.size();
assertThat(dummyVerifier.actualClasspath.stream().map(File::getName)).noneMatch(n -> n.equals("testng-7.5.1.jar"));

dummyVerifier = JavaCheckVerifier.newInstance();
dummyVerifier.addJarsToClasspath("testng-7.5.1");
dummyVerifier.withCheck(NO_EFFECT_CHECK)
.onFile(TEST_FILE)
.verifyNoIssues();
assertThat(dummyVerifier.actualClasspath).hasSize(initialSize + 1);
assertThat(dummyVerifier.actualClasspath.stream().map(File::getName)).anyMatch(n -> n.equals("testng-7.5.1.jar"));
}

@Test
void removeJarsFromClasspath_modifies_classpath() {
JavaCheckVerifier dummyVerifier = JavaCheckVerifier.newInstance();
dummyVerifier.withCheck(NO_EFFECT_CHECK)
.onFile(TEST_FILE)
.verifyNoIssues();
int initialSize = dummyVerifier.actualClasspath.size();
assertThat(dummyVerifier.actualClasspath.stream().map(File::getName)).anyMatch(n -> n.equals("testng-7.12.0.jar"));

dummyVerifier = JavaCheckVerifier.newInstance();
dummyVerifier.removeJarsFromClasspath("testng-7.12.0");
dummyVerifier.withCheck(NO_EFFECT_CHECK)
.onFile(TEST_FILE)
.verifyNoIssues();
assertThat(dummyVerifier.actualClasspath).hasSize(initialSize - 1);
assertThat(dummyVerifier.actualClasspath.stream().map(File::getName)).noneMatch(n -> n.equals("testng-7.12.0.jar"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.java.checks.verifier.FilesUtils;
import org.sonar.java.test.classpath.TestClasspathUtils;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -42,14 +40,11 @@ void test_Testng77() {

@Test
void test_Testng75() {
List<File> classPath = new ArrayList<>(TestClasspathUtils.DEFAULT_MODULE.getClassPath());
classPath.removeIf(file -> file.getName().contains("testng"));
List<File> list = FilesUtils.getFilesRecursively(Path.of("..", "java-checks-test-sources", "target/test-jars"), "jar");
classPath.addAll(list);
CheckVerifier.newVerifier()
.onFile(testCodeSourcesPath("checks/tests/AssertionsWithoutMessageCheckSample_Testng75.java"))
.withCheck(new AssertionsWithoutMessageCheck())
.withClassPath(classPath)
.addJarsToClasspath("testng-7.5.1")
.removeJarsFromClasspath("testng-7.12.0")
.verifyNoIssues();
}
}
Loading