diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-06-26 11:35:47 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-06-29 11:03:32 +0200 |
commit | 81abdbffd8c1adcf0020f31606df1e0b90bd0890 (patch) | |
tree | d6fc6df5664853ea21b3308014145acaea5b8369 /it | |
parent | 35960287f74244d80373eaf3af47a14cefc7bcf8 (diff) | |
download | sonarqube-81abdbffd8c1adcf0020f31606df1e0b90bd0890.tar.gz sonarqube-81abdbffd8c1adcf0020f31606df1e0b90bd0890.zip |
First draft of integration tests
Diffstat (limited to 'it')
-rw-r--r-- | it/plugins/pom.xml | 32 | ||||
-rw-r--r-- | it/pom.xml | 37 | ||||
-rw-r--r-- | it/projects/qualitygate/xoo-sample/sonar-project.properties | 5 | ||||
-rw-r--r-- | it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo | 12 | ||||
-rw-r--r-- | it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo.measures | 11 | ||||
-rw-r--r-- | it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo | 12 | ||||
-rw-r--r-- | it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo.measures | 10 | ||||
-rw-r--r-- | it/tests/pom.xml | 94 | ||||
-rw-r--r-- | it/tests/src/test/java/org/sonar/it/ItUtils.java | 63 | ||||
-rw-r--r-- | it/tests/src/test/java/org/sonar/it/qualitygate/QualityGateTest.java | 194 |
10 files changed, 470 insertions, 0 deletions
diff --git a/it/plugins/pom.xml b/it/plugins/pom.xml new file mode 100644 index 00000000000..75d3eeda78c --- /dev/null +++ b/it/plugins/pom.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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> + + <parent> + <groupId>org.sonarsource.it</groupId> + <artifactId>it-parent</artifactId> + <version>5.2-SNAPSHOT</version> + </parent> + <artifactId>plugins</artifactId> + <name>SonarQube Integration Tests :: Plugins</name> + <description>The fake plugins used by integration tests</description> + <packaging>pom</packaging> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + </plugins> + </build> + + <modules> + + </modules> + +</project> diff --git a/it/pom.xml b/it/pom.xml new file mode 100644 index 00000000000..cbb65650c6b --- /dev/null +++ b/it/pom.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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> + + <parent> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar</artifactId> + <version>5.2-SNAPSHOT</version> + </parent> + + <groupId>org.sonarsource.it</groupId> + <artifactId>it-parent</artifactId> + <version>5.2-SNAPSHOT</version> + <name>SonarQube Integration Tests</name> + <packaging>pom</packaging> + + <modules> + <module>plugins</module> + <module>tests</module> + </modules> + + <dependencies> + <dependency> + <groupId>org.codehaus.sonar.plugins</groupId> + <artifactId>sonar-xoo-plugin</artifactId> + <version>${pom.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-application</artifactId> + <version>${pom.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> diff --git a/it/projects/qualitygate/xoo-sample/sonar-project.properties b/it/projects/qualitygate/xoo-sample/sonar-project.properties new file mode 100644 index 00000000000..7fe05081304 --- /dev/null +++ b/it/projects/qualitygate/xoo-sample/sonar-project.properties @@ -0,0 +1,5 @@ +sonar.projectKey=sample +sonar.projectName=Sample +sonar.projectVersion=1.0-SNAPSHOT +sonar.sources=src +sonar.language=xoo diff --git a/it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo b/it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo new file mode 100644 index 00000000000..b1210973dd9 --- /dev/null +++ b/it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo @@ -0,0 +1,12 @@ +package sample; + +public class Sample { + + public Sample(int i) { + int j = i++; + } + + private String myMethod() { + return "hello"; + } +} diff --git a/it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo.measures b/it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo.measures new file mode 100644 index 00000000000..4f638a7a00b --- /dev/null +++ b/it/projects/qualitygate/xoo-sample/src/sample/Sample.xoo.measures @@ -0,0 +1,11 @@ +ncloc:13 +complexity:7 +coverage:89.0 +test_execution_time:630 +sqale_index:4830 +#Used by dashboard/widgets tests +complexity_in_classes:3 +classes:1 +comment_lines:3 +public_api:5 +public_undocumented_api:2 diff --git a/it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo b/it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo new file mode 100644 index 00000000000..6c1b6459c34 --- /dev/null +++ b/it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo @@ -0,0 +1,12 @@ +package sample; + +public class Sample { + + public Sample(int i) { + int j = i++; + } + + private String myMethod2() { + return "hello"; + } +} diff --git a/it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo.measures b/it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo.measures new file mode 100644 index 00000000000..768513ad374 --- /dev/null +++ b/it/projects/qualitygate/xoo-sample/src/sample/Sample2.xoo.measures @@ -0,0 +1,10 @@ +ncloc:13 +complexity:7 +coverage:89.0 +test_execution_time:630 +sqale_index:4830 +complexity_in_classes:3 +classes:1 +comment_lines:3 +public_api:5 +public_undocumented_api:2 diff --git a/it/tests/pom.xml b/it/tests/pom.xml new file mode 100644 index 00000000000..9249fd32ad7 --- /dev/null +++ b/it/tests/pom.xml @@ -0,0 +1,94 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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> + + <parent> + <groupId>org.sonarsource.it</groupId> + <artifactId>it-parent</artifactId> + <version>5.2-SNAPSHOT</version> + </parent> + <artifactId>tests</artifactId> + <name>SonarQube Integration Tests :: Tests</name> + + <properties> + <category>*</category> + <surefire.argLine>-Xmx128m -server</surefire.argLine> + </properties> + + <dependencies> + <dependency> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-ws-client</artifactId> + <version>5.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.sonarsource.orchestrator</groupId> + <artifactId>sonar-orchestrator</artifactId> + <version>3.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-all</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.googlecode.json-simple</groupId> + <artifactId>json-simple</artifactId> + <version>1.1</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.skyscreamer</groupId> + <artifactId>jsonassert</artifactId> + <version>1.2.0</version> + <scope>test</scope> + </dependency> + + + <!-- Email notifications --> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-email</artifactId> + <version>1.2</version> + </dependency> + <dependency> + <groupId>org.subethamail</groupId> + <artifactId>subethasmtp</artifactId> + <version>3.1.6</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <includes> + <include>org/sonar/it/${category}/suite/*TestSuite.java</include> + <!-- not included in suites --> + <include>org/sonar/it/${category}/*Test.java</include> + </includes> + <excludes> + <!-- included into suites --> + <exclude>org/sonar/it/*/suite/*Test.java</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/it/tests/src/test/java/org/sonar/it/ItUtils.java b/it/tests/src/test/java/org/sonar/it/ItUtils.java new file mode 100644 index 00000000000..aba015afea8 --- /dev/null +++ b/it/tests/src/test/java/org/sonar/it/ItUtils.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2009-2014 SonarSource SA + * All rights reserved + * mailto:contact AT sonarsource DOT com + */ +package org.sonar.it; + +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.sonar.orchestrator.locator.FileLocation; +import java.io.File; +import org.apache.commons.io.FileUtils; + +public class ItUtils { + + private ItUtils() { + } + + private static final Supplier<File> HOME_DIR = Suppliers.memoize(new Supplier<File>() { + @Override + public File get() { + File dir = new File("it"); + + // intellij way + if (dir.exists() && dir.isDirectory()) { + return dir.getParentFile(); + } + + // maven way + dir = new File("../it"); + if (dir.exists() && dir.isDirectory()) { + return dir.getParentFile(); + } + + throw new IllegalStateException("Fail to locate home directory"); + } + }); + + public static FileLocation xooPlugin() { + File target = new File(HOME_DIR.get(), "plugins/sonar-xoo-plugin/target"); + if (target.exists()) { + for (File jar : FileUtils.listFiles(target, new String[] {"jar"}, false)) { + if (jar.getName().startsWith("sonar-xoo-plugin-") && !jar.getName().contains("-source")) { + return FileLocation.of(jar); + } + } + } + throw new IllegalStateException("XOO plugin is not built"); + } + + /** + * Locate the directory of sample project + * + * @param relativePath path related to the directory it/projects, for example "qualitygate/xoo-sample" + */ + public static File projectDir(String relativePath) { + File dir = new File(HOME_DIR.get(), "it/projects/" + relativePath); + if (!dir.exists() || !dir.isDirectory()) { + throw new IllegalStateException("Directory does not exist: " + dir.getAbsolutePath()); + } + return dir; + } +} diff --git a/it/tests/src/test/java/org/sonar/it/qualitygate/QualityGateTest.java b/it/tests/src/test/java/org/sonar/it/qualitygate/QualityGateTest.java new file mode 100644 index 00000000000..3fbefcaf26b --- /dev/null +++ b/it/tests/src/test/java/org/sonar/it/qualitygate/QualityGateTest.java @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2009-2014 SonarSource SA + * All rights reserved + * mailto:contact AT sonarsource DOT com + */ +package org.sonar.it.qualitygate; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarRunner; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.it.ItUtils; +import org.sonar.wsclient.project.NewProject; +import org.sonar.wsclient.qualitygate.NewCondition; +import org.sonar.wsclient.qualitygate.QualityGate; +import org.sonar.wsclient.qualitygate.QualityGateClient; +import org.sonar.wsclient.services.Measure; +import org.sonar.wsclient.services.Resource; +import org.sonar.wsclient.services.ResourceQuery; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.it.ItUtils.projectDir; + +public class QualityGateTest { + + private static final String PROJECT_KEY = "sample"; + + private long provisionnedProjectId = -1L; + + @ClassRule + public static Orchestrator orchestrator = Orchestrator.builderEnv() + .addPlugin(ItUtils.xooPlugin()) + .build(); + + @Before + public void cleanUp() throws Exception { + orchestrator.resetData(); + provisionnedProjectId = Long.parseLong(orchestrator.getServer().adminWsClient().projectClient().create(NewProject.create().key(PROJECT_KEY).name("Sample")).id()); + } + + @Test + public void do_not_compute_status_if_no_gate() { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + + assertThat(fetchResourceWithGateStatus()).isNull(); + } + + @Test + public void status_ok_if_empty_gate() { + QualityGate empty = qgClient().create("Empty"); + qgClient().setDefault(empty.id()); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + + assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(empty.id()); + } + } + + @Test + public void test_status_ok() { + QualityGate simple = qgClient().create("SimpleWithHighThreshold"); + qgClient().setDefault(simple.id()); + qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").warningThreshold("40")); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + + assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(simple.id()); + } + } + + @Test + public void test_status_warning() { + QualityGate simple = qgClient().create("SimpleWithLowThreshold"); + qgClient().setDefault(simple.id()); + qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").warningThreshold("10")); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + + assertThat(fetchGateStatus().getData()).isEqualTo("WARN"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(simple.id()); + } + + } + + @Test + public void test_status_error() { + QualityGate simple = qgClient().create("SimpleWithLowThreshold"); + qgClient().setDefault(simple.id()); + qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("10")); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + + assertThat(fetchGateStatus().getData()).isEqualTo("ERROR"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(simple.id()); + } + } + + @Test + public void use_server_settings_instead_of_default_gate() { + QualityGate alert = qgClient().create("AlertWithLowThreshold"); + qgClient().createCondition(NewCondition.create(alert.id()).metricKey("ncloc").operator("GT").warningThreshold("10")); + QualityGate error = qgClient().create("ErrorWithLowThreshold"); + qgClient().createCondition(NewCondition.create(error.id()).metricKey("ncloc").operator("GT").errorThreshold("10")); + + qgClient().setDefault(alert.id()); + qgClient().selectProject(error.id(), provisionnedProjectId); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + + assertThat(fetchGateStatus().getData()).isEqualTo("ERROR"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(alert.id()); + qgClient().destroy(error.id()); + } + } + + @Test + public void conditions_on_multiple_metric_types() { + QualityGate allTypes = qgClient().create("AllMetricTypes"); + qgClient().createCondition(NewCondition.create(allTypes.id()).metricKey("ncloc").operator("GT").warningThreshold("10")); + qgClient().createCondition(NewCondition.create(allTypes.id()).metricKey("duplicated_lines_density").operator("GT").warningThreshold("20")); + qgClient().setDefault(allTypes.id()); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")) + .setProperty("sonar.cpd.xoo.minimumLines", "2") + .setProperty("sonar.cpd.xoo.minimumTokens", "5"); + orchestrator.executeBuild(build); + + Measure alertStatus = fetchGateStatus(); + assertThat(alertStatus.getData()).isEqualTo("WARN"); + assertThat(alertStatus.getAlertText()) + .contains("Lines of code > 10") + .contains("Duplicated lines (%) > 20"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(allTypes.id()); + } + } + + @Test + public void compute_gate_status_on_metric_variation() { + QualityGate simple = qgClient().create("SimpleWithDifferential"); + qgClient().setDefault(simple.id()); + qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").period(1).operator("EQ").warningThreshold("0")); + + try { + SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample")); + orchestrator.executeBuild(build); + assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + + orchestrator.executeBuild(build); + assertThat(fetchGateStatus().getData()).isEqualTo("WARN"); + } finally { + qgClient().unsetDefault(); + qgClient().destroy(simple.id()); + } + } + + private Measure fetchGateStatus() { + return fetchResourceWithGateStatus().getMeasure("alert_status"); + } + + private Resource fetchResourceWithGateStatus() { + return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, "alert_status").setIncludeAlerts(true)); + } + + private static QualityGateClient qgClient() { + return orchestrator.getServer().adminWsClient().qualityGateClient(); + } +} |