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
2 changes: 2 additions & 0 deletions integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
<pluginDescription>Integration Test PmdExtensionPlugin</pluginDescription>
<!-- This is important. It means that this plugin extends the PMD plugin -->
<basePlugin>pmd</basePlugin>
<requirePlugins>java:8.0.0.0,kotlin:2.0.0.0</requirePlugins>
<requiredForLanguages>java,kotlin</requiredForLanguages>
</configuration>
</plugin>
<plugin>
Expand Down
63 changes: 63 additions & 0 deletions integration-test/projects/pmd-kotlin-rules/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.sonarsource.it.projects</groupId>
<artifactId>pmd-kotlin-rules</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.9.0</kotlin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>5.2.0.4988</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>skipSonar</id>
<activation>
<property>
<name>skipTestProjects</name>
<value>true</value>
</property>
</activation>
<properties>
<sonar.skip>true</sonar.skip>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example

class KotlinErrors {
// This function name is too short, which will trigger the FunctionNameTooShort rule
fun fn() {
println("This function name is too short")
}
}

// This class overrides equals but not hashCode, which will trigger the OverrideBothEqualsAndHashcode rule
class EqualsOnly {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
return true
}

// Missing hashCode() override
}
20 changes: 12 additions & 8 deletions integration-test/src/test/java/com/sonar/it/java/suite/PmdIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void testPmdExtensionsWithDifferentJavaVersions(DefinedJavaVersion version) {
.contains("Start MaximumMethodsCountCheck")
.contains("End MaximumMethodsCountCheck");

final List<Issue> issues = retrieveIssues(keyFor(projectName, "pmd/", "Errors"));
final List<Issue> issues = retrieveIssues(keyFor(projectName, "src/main/java", "pmd", "Errors", ".java"));

final List<String> messages = issues
.stream()
Expand Down Expand Up @@ -116,13 +116,15 @@ void testJunitRules() {
ORCHESTRATOR.executeBuild(build);

// then
String testComponentKey = keyFor("pmd-junit-rules", "src/test/java/", "", "ProductionCodeTest" + ".java");
// (component -> com.sonarsource.it.projects:pmd-junit-rules:src/test/java/ProductionCodeTest.java)
String testComponentKey = keyFor("pmd-junit-rules", "src/test/java", "", "ProductionCodeTest", ".java");
final List<Issue> testIssues = retrieveIssues(testComponentKey);
assertThat(testIssues).hasSize(1);
assertThat(testIssues.get(0).message()).isEqualTo("Unit tests should not contain more than 1 assert(s).");
assertThat(testIssues.get(0).ruleKey()).isEqualTo("pmd:UnitTestContainsTooManyAsserts");

final List<Issue> prodIssues = retrieveIssues(keyFor(projectName, "", "ProductionCode"));
// component -> com.sonarsource.it.projects:pmd-junit-rules:src/main/java/ProductionCode.java
final List<Issue> prodIssues = retrieveIssues(keyFor(projectName, "src/main/java", "", "ProductionCode", ".java"));
assertThat(prodIssues).hasSize(1);
assertThat(prodIssues.get(0).message()).contains("Avoid unused private fields such as 'unused'.");
assertThat(prodIssues.get(0).ruleKey()).isEqualTo("pmd:UnusedPrivateField");
Expand Down Expand Up @@ -150,11 +152,11 @@ void testRuleAvoidDuplicateLiterals() {
ORCHESTRATOR.executeBuild(build);

// then
String avoidDuplicateLiteralsKey = keyFor(projectName, "src/main/java", "", "AvoidDuplicateLiterals", ".java");
final List<Issue> issues = ORCHESTRATOR.retrieveIssues(
IssueQuery.create()
.rules("pmd:AvoidDuplicateLiterals")
.components(keyFor(projectName, "", "AvoidDuplicateLiterals")
)
.components(avoidDuplicateLiteralsKey)
);

assertThat(issues)
Expand Down Expand Up @@ -191,9 +193,10 @@ void pmdShouldHaveAccessToExternalLibrariesInItsClasspath() {

// then
// PMD7-MIGRATION: added to force one violation in pmdShouldHaveAccessToExternalLibrariesInItsClasspath: is this testing the correct thing?
final List<Issue> issues = retrieveIssues(keyFor(projectName, "pmd/", "Bar"));
final List<Issue> issues = retrieveIssues(keyFor(projectName, "src/main/java/", "pmd/", "Errors", ".java"));

assertThat(issues)
.hasSize(1);
.hasSize(3);

} catch (HttpException e) {
System.out.println("Failed to associate Project To Quality Profile: " + e.getMessage() + " body: " + e.getBody());
Expand All @@ -219,7 +222,8 @@ void pmdShouldRunWithAllRulesEnabled() {
ORCHESTRATOR.executeBuild(build);

// then
final List<Issue> issues = retrieveIssues(keyFor(projectName, "pmd/", "Bar"));
final List<Issue> issues = retrieveIssues(keyFor(projectName, "src/main/java", "pmd", "Bar", ".java"));

assertThat(issues)
.isNotEmpty();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* SonarQube PMD7 Plugin Integration Test
* Copyright (C) 2013-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
* 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 com.sonar.it.java.suite;

import com.sonar.it.java.suite.orchestrator.PmdTestOrchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.http.HttpException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.sonar.wsclient.issue.Issue;
import org.sonar.wsclient.issue.IssueQuery;

import java.util.List;
import java.util.stream.Collectors;

import static com.sonar.it.java.suite.TestUtils.keyFor;
import static org.assertj.core.api.Assertions.assertThat;

class PmdKotlinIT {

private static PmdTestOrchestrator orchestrator;

@BeforeAll
static void startSonar() {
orchestrator = PmdTestOrchestrator.init();
orchestrator.start();
}

@Test
void testKotlinRules() {
// given
final String projectName = "pmd-kotlin-rules";
final String suffix = ".kt";
final String srcDir = "src/main/kotlin";

final MavenBuild build = MavenBuild
.create(TestUtils.projectPom(projectName))
.setCleanSonarGoals();

try {
orchestrator.associateProjectToQualityProfile("pmd-kotlin-profile", projectName, "kotlin");

// when
final BuildResult buildResult = orchestrator.executeBuild(build);

// then
final String log = buildResult.getLogs();
assertThat(log).contains("Kotlin");

final List<Issue> issues = retrieveIssues(keyFor(projectName, srcDir, "com/example", "KotlinErrors", suffix));

final List<String> messages = issues
.stream()
.map(Issue::message)
.collect(Collectors.toList());

assertThat(issues).hasSize(2);

assertThat(messages)
.contains(
"Function names should have non-cryptic and clear names.",
"Ensure you override both equals() and hashCode()"
);
} catch (HttpException e) {
System.out.println("Failed to associate Project To Quality Profile: " + e.getMessage() + " body: " + e.getBody());
throw e;
} finally {
// Cleanup
orchestrator.resetData(projectName);
}
}

@Test
void pmdKotlinShouldRunWithAllRulesEnabled() {
// given
final String projectName = "pmd-kotlin-rules";
final MavenBuild build = MavenBuild
.create(TestUtils.projectPom(projectName))
.setCleanPackageSonarGoals();
try {
orchestrator.associateProjectToQualityProfile("pmd-kotlin-all-rules", projectName, "kotlin");

// when
final BuildResult buildResult = orchestrator.executeBuild(build);

// then
final String log = buildResult.getLogs();
assertThat(log).contains("Kotlin");

final List<Issue> issues = retrieveIssues(keyFor(projectName, "src/main/kotlin", "com/example", "KotlinErrors", ".kt"));

assertThat(issues).hasSize(2);
Issue functionNameTooShort = issues.stream().filter(i -> i.ruleKey().equals("pmd-kotlin:FunctionNameTooShort")).findFirst().orElseThrow();
assertThat(functionNameTooShort.severity()).isEqualTo("MAJOR");

} catch (HttpException e) {
System.err.println("Failed to associate Project To Quality Profile: " + e.getMessage() + " body: " + e.getBody());
throw e;
} finally {
// Cleanup
orchestrator.resetData(projectName);
}
}

private List<Issue> retrieveIssues(String componentKey) {
final IssueQuery issueQuery = IssueQuery.create();
issueQuery.urlParams().put("componentKeys", componentKey);
return orchestrator.retrieveIssues(issueQuery);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SonarQube PMD7 Plugin Integration Test
*/
package com.sonar.it.java.suite;

import com.sonar.it.java.suite.orchestrator.PmdTestOrchestrator;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.build.BuildResult;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

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

/**
* Sanity check that our integration test suite can start and run against both
* the lowest and highest supported SonarQube versions.
*/
class SanitySonarVersionsIT {

private static final String SONAR_VERSION_KEY = "test.sonar.version";

@ParameterizedTest(name = "sanity on SonarQube {0}")
@ValueSource(strings = {
// Lowest supported SonarQube LTS line
"LATEST_RELEASE[9.9]",
// Highest supported SonarQube current line (see README table)
"LATEST_RELEASE[25.6]"
})
void sanity_runs_on_lowest_and_highest_supported_versions(String sonarqubeVersion) {
final String previous = System.getProperty(SONAR_VERSION_KEY);
System.setProperty(SONAR_VERSION_KEY, sonarqubeVersion);

PmdTestOrchestrator orchestrator = null;
try {
orchestrator = PmdTestOrchestrator.init();
orchestrator.start();

final String projectName = "pmd-extensions";
final MavenBuild build = MavenBuild
.create(TestUtils.projectPom(projectName))
.setCleanSonarGoals()
// keep analysis minimal for sanity run
.setProperty("sonar.java.binaries", ".");

orchestrator.associateProjectToQualityProfile("pmd-extensions-profile", projectName);
final BuildResult result = orchestrator.executeBuild(build); // will throw if analysis fails
assertThat(result.getLogs()).contains("[INFO] Sensor PmdSensor [pmd]");

// Additionally run a minimal Kotlin project analysis to ensure Kotlin support works
final String kotlinProject = "pmd-kotlin-rules";
final MavenBuild kotlinBuild = MavenBuild
.create(TestUtils.projectPom(kotlinProject))
.setCleanSonarGoals();
orchestrator.associateProjectToQualityProfile("pmd-kotlin-profile", kotlinProject, "kotlin");
final BuildResult kotlinResult = orchestrator.executeBuild(kotlinBuild);
assertThat(kotlinResult.getLogs()).contains("[INFO] Sensor PmdSensor [pmd]");
}
finally {
// restore previous property to not affect other tests
if (previous != null) {
System.setProperty(SONAR_VERSION_KEY, previous);
} else {
System.clearProperty(SONAR_VERSION_KEY);
}
if (orchestrator != null) {
try {
orchestrator.stop();
} catch (Throwable ignored) {
// ignore
}
}
}
}
}
Loading