Skip to content

Commit

Permalink
Add integration tests for manual and common rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Jul 21, 2015
1 parent 853cb39 commit 4027764
Show file tree
Hide file tree
Showing 14 changed files with 386 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
- JOB=POSTGRES
- JOB=MYSQL
- JOB=WEB
- JOB=ITS_ISSUE
- JOB=ITS_QUALITYGATE
- JOB=ITS_UPDATECENTER
- JOB=ITS_TESTING
Expand Down
5 changes: 5 additions & 0 deletions it/it-projects/issue/common-rules/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sonar.projectKey=common-rules-project
sonar.projectName=Sample
sonar.projectVersion=1.0-SNAPSHOT
sonar.sources=src
sonar.tests=test
8 changes: 8 additions & 0 deletions it/it-projects/issue/common-rules/src/Sample.xoo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
there are duplications
in this file

there are duplications
in this file

there are duplications
in this file
10 changes: 10 additions & 0 deletions it/it-projects/issue/common-rules/src/Sample.xoo.measures
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
lines:120
ncloc:100
complexity:7
comment_lines:3
public_api:5
public_undocumented_api:2
lines_to_cover:80
uncovered_lines:70
conditions_to_cover:10
uncovered_conditions:9
1 change: 1 addition & 0 deletions it/it-projects/issue/common-rules/test/SampleTest.xoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
skipped_tests:5
test_errors:2
test_failures:1
tests:10
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ sonar.projectKey=sample
sonar.projectName=Sample
sonar.projectVersion=1.0-SNAPSHOT
sonar.sources=src
sonar.language=xoo
17 changes: 10 additions & 7 deletions it/it-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,47 @@
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<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>org.codehaus.sonar</groupId>
<artifactId>sonar-update-center-common</artifactId>
<version>1.12.1</version>
</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>


Expand Down
77 changes: 77 additions & 0 deletions it/it-tests/src/test/java/issue/suite/CommonRulesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2009-2014 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package issue.suite;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarRunner;
import com.sonar.orchestrator.locator.FileLocation;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.issue.Issue;
import org.sonar.wsclient.issue.IssueQuery;

import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.projectDir;

public class CommonRulesTest {

public static final String FILE_KEY = "common-rules-project:src/Sample.xoo";
public static final String TEST_FILE_KEY = "common-rules-project:test/SampleTest.xoo";

@ClassRule
public static Orchestrator orchestrator = IssueTestSuite.ORCHESTRATOR;

@BeforeClass
public static void setUp() {
orchestrator.resetData();
orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/CommonRulesTest/xoo-common-rules-profile.xml"));
orchestrator.getServer().provisionProject("common-rules-project", "Sample");
orchestrator.getServer().associateProjectToQualityProfile("common-rules-project", "xoo", "xoo-common-rules");
SonarRunner analysis = SonarRunner.create(projectDir("issue/common-rules"),
"sonar.cpd.xoo.minimumTokens", "2",
"sonar.cpd.xoo.minimumLines", "2");
orchestrator.executeBuild(analysis);
}

@Test
public void test_rule_on_duplicated_blocks() {
List<Issue> issues = findIssues(FILE_KEY, "common-xoo:DuplicatedBlocks");
assertThat(issues).hasSize(1);
}

@Test
public void test_rule_on_comments() {
List<Issue> issues = findIssues(FILE_KEY, "common-xoo:InsufficientCommentDensity");
assertThat(issues.size()).isEqualTo(1);
}

@Test
public void test_rule_on_coverage() {
List<Issue> issues = findIssues(FILE_KEY, "common-xoo:InsufficientBranchCoverage");
assertThat(issues.size()).isEqualTo(1);

issues = findIssues(FILE_KEY, "common-xoo:InsufficientLineCoverage");
assertThat(issues.size()).isEqualTo(1);
}

@Test
public void test_rule_on_skipped_tests() {
List<Issue> issues = findIssues(TEST_FILE_KEY, "common-xoo:SkippedUnitTests");
assertThat(issues.size()).isEqualTo(1);
}

@Test
public void test_rule_on_test_errors() {
List<Issue> issues = findIssues(TEST_FILE_KEY, "common-xoo:FailedUnitTests");
assertThat(issues.size()).isEqualTo(1);
}

private List<Issue> findIssues(String componentKey, String ruleKey) {
return orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().components(componentKey).rules(ruleKey)).list();
}
}
24 changes: 24 additions & 0 deletions it/it-tests/src/test/java/issue/suite/IssueTestSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2009-2014 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package issue.suite;

import com.sonar.orchestrator.Orchestrator;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import util.ItUtils;

@RunWith(Suite.class)
@Suite.SuiteClasses({
ManualRulesTest.class, CommonRulesTest.class
})
public class IssueTestSuite {

@ClassRule
public static final Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
.addPlugin(ItUtils.xooPlugin())
.build();
}
52 changes: 52 additions & 0 deletions it/it-tests/src/test/java/issue/suite/ManualRulesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2009-2014 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package issue.suite;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.selenium.Selenese;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

public class ManualRulesTest {

@ClassRule
public static Orchestrator orchestrator = IssueTestSuite.ORCHESTRATOR;

@BeforeClass
public static void setup() throws Exception {
orchestrator.resetData();
deleteManualRules();
}

@AfterClass
public static void purgeManualRules() {
deleteManualRules();
}

@Test
public void testManualRules() {
Selenese selenese = Selenese
.builder()
.setHtmlTestsInClasspath("manual-rules",
"/issue/suite/ManualRulesTest/create_edit_delete_manual_rule.html"
).build();
orchestrator.executeSelenese(selenese);
}

protected static void deleteManualRules(){
try {
Connection connection = orchestrator.getDatabase().openConnection();
connection.prepareStatement("DELETE FROM rules WHERE rules.plugin_name='manual'").execute();
} catch (SQLException e) {
throw new IllegalStateException("Fail to remove manual rules", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?><!-- Generated by Sonar -->
<profile>
<name>xoo-common-rules</name>
<language>xoo</language>
<rules>
<rule>
<repositoryKey>common-xoo</repositoryKey>
<key>DuplicatedBlocks</key>
<priority>CRITICAL</priority>
</rule>
<rule>
<repositoryKey>common-xoo</repositoryKey>
<key>InsufficientBranchCoverage</key>
<priority>CRITICAL</priority>
<parameters>
<parameter>
<key>minimumBranchCoverageRatio</key>
<value>90</value>
</parameter>
</parameters>
</rule>
<rule>
<repositoryKey>common-xoo</repositoryKey>
<key>InsufficientCommentDensity</key>
<priority>CRITICAL</priority>
<parameters>
<parameter>
<key>minimumCommentDensity</key>
<value>90.0</value>
</parameter>
</parameters>
</rule>
<rule>
<repositoryKey>common-xoo</repositoryKey>
<key>InsufficientLineCoverage</key>
<priority>CRITICAL</priority>
<parameters>
<parameter>
<key>minimumLineCoverageRatio</key>
<value>90</value>
</parameter>
</parameters>
</rule>
<rule>
<repositoryKey>common-xoo</repositoryKey>
<key>FailedUnitTests</key>
<priority>CRITICAL</priority>
</rule>
<rule>
<repositoryKey>common-xoo</repositoryKey>
<key>SkippedUnitTests</key>
<priority>CRITICAL</priority>
</rule>
</rules>
</profile>
Loading

0 comments on commit 4027764

Please sign in to comment.