Skip to content

Commit

Permalink
[NO JIRA] Migrate ITs to Junit 5 (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka committed Dec 21, 2023
1 parent 8962b85 commit 86f2713
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 83 deletions.
8 changes: 3 additions & 5 deletions its/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
testImplementation(testLibs.sonarlint.core)
testImplementation(testLibs.sonar.orchestrator.junit4)
testImplementation(testLibs.sonar.orchestrator.junit5)
testImplementation(testLibs.junit.engine)
testImplementation(testLibs.assertj.core)
testImplementation(testLibs.sonar.ws)
testImplementation(libs.sonar.analyzer.commons)
Expand All @@ -9,13 +10,10 @@ dependencies {
sonarqube.isSkipProject = true

tasks.test {
useJUnitPlatform()
onlyIf {
project.hasProperty("plugin") || project.hasProperty("its")
}
filter {
includeTestsMatching("org.sonarsource.slang.Tests")
includeTestsMatching("org.sonarsource.slang.SonarLintTest")
}
systemProperty("java.awt.headless", "true")
outputs.upToDateWhen { false }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package org.sonarsource.slang;

import org.junit.Test;
import org.junit.jupiter.api.Test;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
package org.sonarsource.slang;

import com.sonar.orchestrator.build.SonarScanner;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.sonarqube.ws.Issues.Issue;
import org.sonarqube.ws.client.issues.SearchRequest;

Expand All @@ -35,9 +33,6 @@ public class ExternalReportTest extends TestBase {

private static final String BASE_DIRECTORY = "projects/externalreport/";

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();

@Test
public void detekt() {
final String projectKey = "detekt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package org.sonarsource.slang;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.sonarqube.ws.Issues;

import java.util.List;
Expand Down
52 changes: 26 additions & 26 deletions its/plugin/src/test/java/org/sonarsource/slang/SonarLintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@
*/
package org.sonarsource.slang;

import com.sonar.orchestrator.junit4.OrchestratorRule;
import com.sonar.orchestrator.junit4.OrchestratorRuleBuilder;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.junit5.OrchestratorExtensionBuilder;
import com.sonar.orchestrator.locator.Locators;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl;
import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile;
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine;
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
import org.sonarsource.sonarlint.core.commons.Language;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,40 +45,27 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl;
import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile;
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine;
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
import org.sonarsource.sonarlint.core.commons.Language;

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

public class SonarLintTest {

@ClassRule
public static TemporaryFolder temp = new TemporaryFolder();
@TempDir
public static File temp;

private static StandaloneSonarLintEngine sonarlintEngine;

private static File baseDir;

@BeforeClass
@BeforeAll
public static void prepare() throws Exception {
// Orchestrator is used only to retrieve plugin artifacts from filesystem or maven
OrchestratorRuleBuilder orchestratorBuilder = OrchestratorRule.builderEnv();
Tests.addLanguagePlugins(orchestratorBuilder);
OrchestratorRule orchestrator = orchestratorBuilder
OrchestratorExtensionBuilder orchestratorBuilder = OrchestratorExtension.builderEnv();
TestsHelper.addLanguagePlugins(orchestratorBuilder);
OrchestratorExtension orchestrator = orchestratorBuilder
.useDefaultAdminCredentialsForBuilds(true)
.setSonarVersion(System.getProperty(Tests.SQ_VERSION_PROPERTY, Tests.DEFAULT_SQ_VERSION))
.setSonarVersion(System.getProperty(TestsHelper.SQ_VERSION_PROPERTY, TestsHelper.DEFAULT_SQ_VERSION))
.build();

Locators locators = orchestrator.getConfiguration().locators();
Expand All @@ -76,22 +76,22 @@ public static void prepare() throws Exception {

sonarLintConfigBuilder
.addEnabledLanguage(Language.KOTLIN)
.setSonarLintUserHome(temp.newFolder().toPath())
.setSonarLintUserHome(temp.toPath())
.setLogOutput((formattedMessage, level) -> {
/* Don't pollute logs */
});
StandaloneGlobalConfiguration configuration = sonarLintConfigBuilder.build();
sonarlintEngine = new StandaloneSonarLintEngineImpl(configuration);
baseDir = temp.newFolder();
baseDir = temp;
}

@AfterClass
@AfterAll
public static void stop() {
sonarlintEngine.stop();
}

@Test
public void test_kotlin() throws Exception {
void test_kotlin() throws Exception {
ClientInputFile inputFile = prepareInputFile("foo.kt",
"fun foo_bar() {\n" +
" if (true) { \n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
package org.sonarsource.slang;

import org.junit.Test;

import org.junit.jupiter.api.Test;

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

Expand Down
11 changes: 6 additions & 5 deletions its/plugin/src/test/java/org/sonarsource/slang/SurefireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
package org.sonarsource.slang;

import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import org.junit.ClassRule;
import org.junit.Test;

import com.sonar.orchestrator.junit5.OrchestratorExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonarqube.ws.Measures;

import static java.lang.Double.parseDouble;
Expand All @@ -35,8 +36,8 @@

public class SurefireTest extends TestBase {

@ClassRule
public static final OrchestratorRule ORCHESTRATOR = Tests.ORCHESTRATOR;
@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsHelper.ORCHESTRATOR;
private static final Path BASE_DIRECTORY = Paths.get("projects");


Expand Down
9 changes: 5 additions & 4 deletions its/plugin/src/test/java/org/sonarsource/slang/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonarsource.slang;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -29,7 +28,9 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.ClassRule;

import com.sonar.orchestrator.junit5.OrchestratorExtension;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonarqube.ws.Issues;
import org.sonarqube.ws.Measures.ComponentWsResponse;
import org.sonarqube.ws.Measures.Measure;
Expand All @@ -43,8 +44,8 @@

public abstract class TestBase {

@ClassRule
public static final OrchestratorRule ORCHESTRATOR = Tests.ORCHESTRATOR;
@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsHelper.ORCHESTRATOR;

protected SonarScanner getSonarScanner(String projectKey, String directoryToScan, String languageKey) {
return getSonarScanner(projectKey, directoryToScan, languageKey, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,37 @@
package org.sonarsource.slang;

import com.sonar.orchestrator.OrchestratorBuilder;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import com.sonar.orchestrator.junit4.OrchestratorRuleBuilder;

import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.junit5.OrchestratorExtensionBuilder;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.MavenLocation;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
DuplicationsTest.class,
ExternalReportTest.class,
MeasuresTest.class,
SuppressWarningsTest.class,
SurefireTest.class
})
public class Tests {

public class TestsHelper {

static final String SQ_VERSION_PROPERTY = "sonar.runtimeVersion";
static final String DEFAULT_SQ_VERSION = "LATEST_RELEASE";

private static final Set<String> LANGUAGES = new HashSet<>(Collections.singletonList("kotlin"));

@ClassRule
public static final OrchestratorRule ORCHESTRATOR;
public static final OrchestratorExtension ORCHESTRATOR;

static {
OrchestratorRuleBuilder orchestratorBuilder = OrchestratorRule.builderEnv();
OrchestratorExtensionBuilder orchestratorBuilder = OrchestratorExtension.builderEnv();
addLanguagePlugins(orchestratorBuilder);
ORCHESTRATOR = orchestratorBuilder
.useDefaultAdminCredentialsForBuilds(true)
.setSonarVersion(System.getProperty(SQ_VERSION_PROPERTY, DEFAULT_SQ_VERSION))
.restoreProfileAtStartup(FileLocation.of("src/test/resources/suppress-warnings-kotlin.xml"))
.restoreProfileAtStartup(FileLocation.of("src/test/resources/norule.xml"))
.build();
.useDefaultAdminCredentialsForBuilds(true)
.setSonarVersion(System.getProperty(SQ_VERSION_PROPERTY, DEFAULT_SQ_VERSION))
.restoreProfileAtStartup(FileLocation.of("src/test/resources/suppress-warnings-kotlin.xml"))
.restoreProfileAtStartup(FileLocation.of("src/test/resources/norule.xml"))
.build();
}

static void addLanguagePlugins(OrchestratorBuilder builder) {
Expand Down
4 changes: 3 additions & 1 deletion its/ruling/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ plugins {
}

dependencies {
testImplementation(testLibs.sonar.orchestrator.junit4)
testImplementation(testLibs.sonar.orchestrator.junit5)
testImplementation(testLibs.assertj.core)
testImplementation(testLibs.junit.engine)
testImplementation(libs.sonar.analyzer.commons)
}

sonarqube.isSkipProject = true

tasks.test {
useJUnitPlatform()
onlyIf {
project.hasProperty("its") || project.hasProperty("ruling")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import com.sonar.orchestrator.build.Build;
import com.sonar.orchestrator.build.GradleBuild;
import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import com.sonar.orchestrator.junit4.OrchestratorRuleBuilder;

import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.junit5.OrchestratorExtensionBuilder;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.MavenLocation;
Expand All @@ -41,9 +42,11 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonarsource.analyzer.commons.ProfileGenerator;
Expand All @@ -57,15 +60,15 @@ public class SlangRulingTest {
private static final String SQ_VERSION_PROPERTY = "sonar.runtimeVersion";
private static final String DEFAULT_SQ_VERSION = "LATEST_RELEASE";

private static OrchestratorRule orchestrator;
private static OrchestratorExtension orchestrator;
private static boolean keepSonarqubeRunning = "true".equals(System.getProperty("keepSonarqubeRunning"));
private static final boolean IGNORE_EXPECTED_ISSUES_AND_REPORT_ALL = "true".equals(System.getProperty("reportAll"));
private static final boolean CLEAN_PROJECT_BINARIES = "true".equals(System.getProperty("cleanProjects"));
private static final Set<String> LANGUAGES = new HashSet<>(Collections.singletonList("kotlin"));

@BeforeClass
@BeforeAll
public static void setUp() {
OrchestratorRuleBuilder builder = OrchestratorRule.builderEnv()
OrchestratorExtensionBuilder builder = OrchestratorExtension.builderEnv()
.useDefaultAdminCredentialsForBuilds(true)
.setSonarVersion(System.getProperty(SQ_VERSION_PROPERTY, DEFAULT_SQ_VERSION))
.addPlugin(MavenLocation.of("org.sonarsource.sonar-lits-plugin", "sonar-lits-plugin", "0.11.0.2659"));
Expand Down Expand Up @@ -334,7 +337,7 @@ private void executeBuildAndAssertDifferences(String project, Build<?> build) th
assertThat(litsDifference).isEmpty();
}

@AfterClass
@AfterAll
public static void after() {
if (keepSonarqubeRunning) {
try {
Expand Down
3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ dependencyResolutionManagement {
val analyzerCommons = version("analyzerCommons", analyzerCommonsVersionStr)
val gson = version("gson", "2.10.1")
val staxmate = version("staxmate", "2.4.1")
val gradleToolingApi = version("gradle-tooling-api", "7.5.1")

library("gson", "com.google.code.gson", "gson").versionRef(gson)
library("kotlin-compiler-embeddable", "org.jetbrains.kotlin", "kotlin-compiler-embeddable").version(kotlinVersion)
Expand Down Expand Up @@ -96,7 +95,7 @@ dependencyResolutionManagement {
library("mockk", "io.mockk", "mockk").versionRef(mockk)
library("sonar-analyzer-test-commons", "org.sonarsource.analyzer-commons", "sonar-analyzer-test-commons")
.versionRef(analyzerCommons)
library("sonar-orchestrator-junit4", "org.sonarsource.orchestrator", "sonar-orchestrator-junit4").versionRef(orchestrator)
library("sonar-orchestrator-junit5", "org.sonarsource.orchestrator", "sonar-orchestrator-junit5").versionRef(orchestrator)
library("sonar-plugin-api-impl", "org.sonarsource.sonarqube", "sonar-plugin-api-impl").versionRef(sonarqube)
library("sonar-plugin-api-test-fixtures", "org.sonarsource.api.plugin", "sonar-plugin-api-test-fixtures")
.version(sonarPluginApi)
Expand Down

0 comments on commit 86f2713

Please sign in to comment.