Skip to content

Commit

Permalink
Split Maven and Java ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Dec 17, 2020
1 parent 47f5224 commit 09fde2b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 127 deletions.
134 changes: 134 additions & 0 deletions its/src/test/java/com/sonar/maven/it/suite/JavaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* SonarSource :: IT :: SonarQube Maven
* Copyright (C) 2009-2019 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 com.sonar.maven.it.suite;

import com.sonar.maven.it.ItUtils;
import com.sonar.orchestrator.build.MavenBuild;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonarqube.ws.client.settings.SetRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;

public class JavaTest extends AbstractMavenTest {

@Rule
public TemporaryFolder temp = new TemporaryFolder();

@After
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 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);

Properties generatedProps = getProps(outputProps);
String[] moduleIds = generatedProps.getProperty("sonar.modules").split(",");
String strutsCoreModuleId = null;
for (String moduleId : moduleIds) {
if (generatedProps.getProperty(moduleId + ".sonar.moduleKey").equals("org.apache.struts:struts-core")) {
strutsCoreModuleId = moduleId;
break;
}
}
assertThat(strutsCoreModuleId).isNotNull();
assertThat(generatedProps.getProperty(strutsCoreModuleId + ".sonar.java.libraries")).contains("antlr-2.7.2.jar");
assertThat(generatedProps.getProperty(strutsCoreModuleId + ".sonar.libraries")).contains("antlr-2.7.2.jar");
}

@Test
public void read_default_from_plugins_config() throws Exception {
File outputProps = temp.newFile();
// 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);

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.findbugs.excludeFilters", new File(pom.getParentFile(), "findbugs-filter.xml").toString()),
entry("sonar.junit.reportsPath", new File(pom.getParentFile(), "target/surefire-output").toString()),
entry("sonar.junit.reportPaths", new File(pom.getParentFile(), "target/surefire-output").toString()),
entry("sonar.java.source", "1.7"));
}

@Test
public void setJavaVersionCompilerConfiguration() throws FileNotFoundException, IOException {
File outputProps = temp.newFile();

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

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.java.source", "1.7"),
entry("sonar.java.target", "1.8"));
}

Integer mavenVersion = null;

@Test
public void setJavaVersionProperties() throws IOException {
File outputProps = temp.newFile();

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

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.java.source", "1.7"),
entry("sonar.java.target", "1.8"));
}

private Properties getProps(File outputProps)
throws FileNotFoundException, IOException {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(outputProps)) {
props.load(fis);
}
return props;
}

}
127 changes: 1 addition & 126 deletions its/src/test/java/com/sonar/maven/it/suite/MavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,10 @@
import com.sonar.orchestrator.build.BuildRunner;
import com.sonar.orchestrator.build.MavenBuild;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -44,7 +36,6 @@
import org.sonarqube.ws.client.users.CreateRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;

public class MavenTest extends AbstractMavenTest {

Expand Down Expand Up @@ -88,7 +79,7 @@ public void useUserPropertiesGlobalConfig() throws Exception {
public void supportSonarHostURLParam() {
BuildRunner runner = new BuildRunner(orchestrator.getConfiguration());
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-global-properties"))
//global property should take precedence
// global property should take precedence
.setEnvironmentVariable("SONAR_HOST_URL", "http://from-env.org:9000")
.setGoals(cleanSonarGoal());

Expand Down Expand Up @@ -419,30 +410,6 @@ public void fail_if_bad_value_of_sonar_tests_property() {
"java2' does not exist for Maven module com.sonarsource.it.samples:maven-bad-tests-property:jar:1.0-SNAPSHOT. Please check the property sonar.tests");
}

// MSONAR-83
@Test
public void shouldPopulateLibraries() throws IOException {
File outputProps = temp.newFile();
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);

Properties generatedProps = getProps(outputProps);
String[] moduleIds = generatedProps.getProperty("sonar.modules").split(",");
String strutsCoreModuleId = null;
for (String moduleId : moduleIds) {
if (generatedProps.getProperty(moduleId + ".sonar.moduleKey").equals("org.apache.struts:struts-core")) {
strutsCoreModuleId = moduleId;
break;
}
}
assertThat(strutsCoreModuleId).isNotNull();
assertThat(generatedProps.getProperty(strutsCoreModuleId + ".sonar.java.libraries")).contains("antlr-2.7.2.jar");
assertThat(generatedProps.getProperty(strutsCoreModuleId + ".sonar.libraries")).contains("antlr-2.7.2.jar");
}

// MSONAR-91
@Test
public void shouldSkipModules() {
Expand Down Expand Up @@ -472,25 +439,6 @@ public void shouldSkipWithEnvVar() {
assertThat(result.getLogs()).contains("SonarQube Scanner analysis skipped");
}

@Test
public void read_default_from_plugins_config() throws Exception {
File outputProps = temp.newFile();
// 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);

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.findbugs.excludeFilters", new File(pom.getParentFile(), "findbugs-filter.xml").toString()),
entry("sonar.junit.reportsPath", new File(pom.getParentFile(), "target/surefire-output").toString()),
entry("sonar.junit.reportPaths", new File(pom.getParentFile(), "target/surefire-output").toString()),
entry("sonar.java.source", "1.7"));
}

/**
* MSONAR-141
*/
Expand All @@ -513,79 +461,6 @@ public void supportMavenEncryption() throws Exception {
orchestrator.executeBuild(build);
}

@Test
public void setJavaVersionCompilerConfiguration() throws FileNotFoundException, IOException {
Assume.assumeTrue(3 == getMavenMajorVersion());

File outputProps = temp.newFile();

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

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.java.source", "1.7"),
entry("sonar.java.target", "1.8"));
}

Integer mavenVersion = null;

private int getMavenMajorVersion() {
String versionRegex = "Apache Maven\\s(\\d+)\\.\\d+(?:\\.\\d+)?\\s";

if (mavenVersion != null) {
return mavenVersion;
}

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

String logs = result.getLogs();
Pattern p = Pattern.compile(versionRegex);
Matcher matcher = p.matcher(logs);

if (matcher.find()) {
mavenVersion = Integer.parseInt(matcher.group(1));
return mavenVersion;
}
throw new IllegalStateException("Could not find maven version: " + logs);
}

@Test
public void setJavaVersionProperties() throws IOException {
Assume.assumeTrue(3 == getMavenMajorVersion());

File outputProps = temp.newFile();

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

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.java.source", "1.7"),
entry("sonar.java.target", "1.8"));
}

private Properties getProps(File outputProps)
throws FileNotFoundException, IOException {
FileInputStream fis = null;
try {
Properties props = new Properties();
fis = new FileInputStream(outputProps);
props.load(fis);
return props;
} finally {
IOUtils.closeQuietly(fis);
}
}

private boolean hasModules() {
return !orchestrator.getServer().version().isGreaterThanOrEquals(7, 6);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
LinksTest.class, MavenTest.class, ProxyTest.class
LinksTest.class, MavenTest.class, ProxyTest.class, JavaTest.class
})
public class MavenTestSuite {

Expand Down

0 comments on commit 09fde2b

Please sign in to comment.