Skip to content

Commit

Permalink
MSONAR-208 Migrate tests to JUnit 5 (#197)
Browse files Browse the repository at this point in the history
* Note: SQMojoTest is still JUnit 4 with a vintage JUnit 5 engine because it requires much rework due to the usage of a MojoRule
  • Loading branch information
leveretka authored Feb 28, 2024
1 parent a55ceac commit f2ab7c9
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 395 deletions.
9 changes: 2 additions & 7 deletions its/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<dependencies>
<dependency>
<groupId>org.sonarsource.orchestrator</groupId>
<artifactId>sonar-orchestrator</artifactId>
<version>3.42.0.312</version>
<artifactId>sonar-orchestrator-junit5</artifactId>
<version>4.8.0.1898</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -94,11 +94,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/MavenTestSuite.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Expand Down
35 changes: 22 additions & 13 deletions its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonValue;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.container.Server;
import com.sonar.orchestrator.http.HttpMethod;
import com.sonar.orchestrator.http.HttpResponse;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.version.Version;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -39,9 +39,9 @@
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonarqube.ws.Components.Component;
import org.sonarqube.ws.Measures;
import org.sonarqube.ws.Measures.Measure;
Expand All @@ -61,8 +61,12 @@ public abstract class AbstractMavenTest {

private static Version mojoVersion;

@ClassRule
public static Orchestrator orchestrator = MavenTestSuite.ORCHESTRATOR;
@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = OrchestratorExtension.builderEnv()
.setSonarVersion(getSonarVersion())
.useDefaultAdminCredentialsForBuilds(true)
.keepBundledPlugins()
.build();

protected HttpConnector wsConnector;
protected WsClient wsClient;
Expand All @@ -87,21 +91,21 @@ protected static String[] cleanVerifySonarGoal() {
return new String[]{"clean verify " + sonarGoal()};
}

@Before
@BeforeEach
public void setUpWsClient() {
wsConnector = HttpConnector.newBuilder()
.url(orchestrator.getServer().getUrl())
.url(ORCHESTRATOR.getServer().getUrl())
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
.build();
wsClient = WsClientFactories.getDefault().newClient(wsConnector);
}

@After
@AfterEach
public void resetData() {
Set<String> projectKeys = getProjectKeysToDelete();

if (!projectKeys.isEmpty()) {
orchestrator.getServer()
ORCHESTRATOR.getServer()
.newHttpCall("/api/projects/bulk_delete")
.setAdminCredentials()
.setMethod(HttpMethod.POST)
Expand All @@ -111,7 +115,7 @@ public void resetData() {
}

private Set<String> getProjectKeysToDelete() {
HttpResponse ps = orchestrator.getServer()
HttpResponse ps = ORCHESTRATOR.getServer()
.newHttpCall("api/projects/search")
.setAdminCredentials()
.setMethod(HttpMethod.GET)
Expand Down Expand Up @@ -182,7 +186,7 @@ static List<Component> getModules(String projectKey) {

static WsClient newWsClient() {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(orchestrator.getServer().getUrl())
.url(ORCHESTRATOR.getServer().getUrl())
.build());
}

Expand All @@ -196,7 +200,7 @@ protected Version getMavenVersion() {

MavenBuild build = MavenBuild.create()
.setGoals("-version");
BuildResult result = orchestrator.executeBuild(build);
BuildResult result = ORCHESTRATOR.executeBuild(build);

String logs = result.getLogs();
Matcher matcher = VERSION_REGEX.matcher(logs);
Expand All @@ -208,4 +212,9 @@ protected Version getMavenVersion() {
throw new IllegalStateException("Could not find maven version: " + logs);
}

private static String getSonarVersion() {
String versionProperty = System.getProperty("sonar.runtimeVersion");
return versionProperty != null ? versionProperty : "DEV";
}

}
63 changes: 37 additions & 26 deletions its/src/test/java/com/sonar/maven/it/suite/JavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,39 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;
import org.junit.After;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.sonarqube.ws.client.settings.SetRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class JavaTest extends AbstractMavenTest {

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

@After
@AfterEach
public void cleanup() {
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false"));
}

// MSONAR-83
@Test
public void shouldPopulateLibraries() throws IOException {
File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File projectPom = ItUtils.locateProjectPom("shared/struts-1.3.9-diet");
MavenBuild build = MavenBuild.create(projectPom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties generatedProps = getProps(outputProps);
String[] moduleIds = generatedProps.getProperty("sonar.modules").split(",");
Expand All @@ -73,14 +76,16 @@ public void shouldPopulateLibraries() throws IOException {

@Test
public void read_default_from_plugins_config() throws Exception {
File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

// Need package to have test execution
// Surefire reports are not in standard directory
File pom = ItUtils.locateProjectPom("project-default-config");
MavenBuild build = MavenBuild.create(pom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(
Expand All @@ -92,13 +97,14 @@ public void read_default_from_plugins_config() throws Exception {

@Test
public void setJavaVersionCompilerConfiguration() throws IOException {
File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File pom = ItUtils.locateProjectPom("version/compilerPluginConfig");
MavenBuild build = MavenBuild.create(pom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(
Expand All @@ -108,13 +114,14 @@ public void setJavaVersionCompilerConfiguration() throws IOException {

@Test
public void setJavaVersionProperties() throws IOException {
File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File pom = ItUtils.locateProjectPom("version/properties");
MavenBuild build = MavenBuild.create(pom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(
Expand All @@ -124,21 +131,23 @@ public void setJavaVersionProperties() throws IOException {

@Test
public void setJdkHomeFromCompilerExecutableConfiguration() throws IOException {
File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File pom = ItUtils.locateProjectPom("jdkHome/compilerPluginConfigExecutable");
MavenBuild build = MavenBuild.create(pom)
.setGoals(sonarGoal())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "path/to/java_executable"));
}

@Test
public void setJdkHomeFromGlobalToolchainsPlugin() throws IOException {
File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File pom = ItUtils.locateProjectPom("jdkHome/globalToolchain");
MavenBuild build = MavenBuild.create(pom)
Expand All @@ -147,7 +156,7 @@ public void setJdkHomeFromGlobalToolchainsPlugin() throws IOException {
.setGoals("toolchains:toolchain " + sonarGoal())
.addArguments("--toolchains", new File(pom.getParent(), "toolchains.xml").getAbsolutePath())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_1.5"));
Expand All @@ -156,17 +165,18 @@ public void setJdkHomeFromGlobalToolchainsPlugin() throws IOException {
@Test
public void setJdkHomeFromCompilerToolchainsConfiguration() throws IOException {
// https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#jdkToolchain requires Maven 3.3.1+
Assume.assumeTrue(getMavenVersion().compareTo(Version.create("3.3.1")) >= 0);
assumeTrue(getMavenVersion().compareTo(Version.create("3.3.1")) >= 0);

File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File pom = ItUtils.locateProjectPom("jdkHome/compilerPluginConfigToolchain");
MavenBuild build = MavenBuild.create(pom)
.setExecutionDir(pom.getParentFile())
.setGoals(sonarGoal())
.addArguments("--toolchains", new File(pom.getParent(), "toolchains.xml").getAbsolutePath())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_1.6"));
Expand All @@ -175,17 +185,18 @@ public void setJdkHomeFromCompilerToolchainsConfiguration() throws IOException {
@Test
public void takeFirstToolchainIfMultipleExecutions() throws IOException {
// https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#jdkToolchain requires Maven 3.3.1+
Assume.assumeTrue(getMavenVersion().compareTo(Version.create("3.3.1")) >= 0);
assumeTrue(getMavenVersion().compareTo(Version.create("3.3.1")) >= 0);

File outputProps = temp.newFile();
File outputProps = temp.resolve("out.properties").toFile();
outputProps.createNewFile();

File pom = ItUtils.locateProjectPom("jdkHome/compilerPluginConfigToolchainMultipleExecutions");
MavenBuild build = MavenBuild.create(pom)
.setExecutionDir(pom.getParentFile())
.setGoals(sonarGoal())
.addArguments("--toolchains", new File(pom.getParent(), "toolchains.xml").getAbsolutePath())
.setProperty("sonar.scanner.dumpToFile", outputProps.getAbsolutePath());
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_9"));
Expand Down
19 changes: 6 additions & 13 deletions its/src/test/java/com/sonar/maven/it/suite/LinksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import com.sonar.maven.it.ItUtils;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.container.Server;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.junit.jupiter.api.Test;
import org.sonarqube.ws.ProjectLinks;
import org.sonarqube.ws.ProjectLinks.SearchWsResponse;
import org.sonarqube.ws.client.HttpConnector;
Expand All @@ -35,29 +34,23 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;

public class LinksTest extends AbstractMavenTest {

@Before
@After
public void cleanProjectLinksTable() {
orchestrator.getDatabase().truncate("project_links");
}
class LinksTest extends AbstractMavenTest {

/**
* SONAR-3676
*/
@Test
public void shouldUseLinkPropertiesOverPomLinksInMaven() {
void shouldUseLinkPropertiesOverPomLinksInMaven() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("batch/links-project"))
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scm.disabled", "true");
orchestrator.executeBuild(build);
ORCHESTRATOR.executeBuild(build);

checkLinks();
}

private void checkLinks() {
Server server = orchestrator.getServer();
Server server = ORCHESTRATOR.getServer();
WsClient client = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(server.getUrl())
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
Expand Down
Loading

0 comments on commit f2ab7c9

Please sign in to comment.