Skip to content

Commit 20aeb03

Browse files
committed
SONARPY-132 Setup java-based integration tests
1 parent 35703c1 commit 20aeb03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+85578
-17
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "its/sources"]
2+
path = its/sources
3+
url = https://github.com/SonarCommunity/python-test-sources.git

.travis.yml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
language: java
2+
sudo: false
23

34
jdk:
45
- oraclejdk7
56

6-
install:
7-
- cd /tmp
8-
- wget http://dist.sonar.codehaus.org/sonarqube-4.5.2.zip
9-
- unzip sonarqube-4.5.2.zip
10-
- wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
11-
- unzip sonar-runner-dist-2.4.zip
12-
- cd -
13-
14-
before_script:
15-
- sudo pip install requests
16-
- sudo pip install behave
17-
- sudo pip install colorama
18-
19-
script:
20-
- mvn install -DskipTests=true
21-
- mvn test
22-
- RAILS_ENV=production PATH=$PATH:/tmp/sonar-runner-2.4/bin SONARHOME=/tmp/sonarqube-4.5.2 behave
7+
install: true
8+
script: ./travis.sh
9+
env:
10+
- TEST=ci
11+
12+
- TEST=ruling
13+
SQ_VERSION=LATEST_RELEASE
14+
15+
- TEST=plugin
16+
SQ_VERSION=DEV
17+
18+
- TEST=plugin
19+
SQ_VERSION=LTS
20+
21+
matrix:
22+
fast_finish: true
23+
24+
cache:
25+
directories:
26+
- '$HOME/.m2/repository'
27+
28+
notifications:
29+
email: false

its/plugin/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.sonarsource.parent</groupId>
8+
<artifactId>parent</artifactId>
9+
<version>23</version>
10+
</parent>
11+
12+
<groupId>org.sonarsource.python.it</groupId>
13+
<artifactId>it-python-plugin</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
<name>Python :: ITs :: Plugin</name>
17+
<inceptionYear>2012</inceptionYear>
18+
<organization>
19+
<name>SonarSource and Waleri Enns</name>
20+
</organization>
21+
22+
<properties>
23+
<surefire.argLine>-server</surefire.argLine>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>com.sonarsource.orchestrator</groupId>
29+
<artifactId>sonar-orchestrator</artifactId>
30+
<version>3.3</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.10</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.easytesting</groupId>
39+
<artifactId>fest-assert</artifactId>
40+
<version>1.4</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<pluginManagement>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-surefire-plugin</artifactId>
50+
<configuration>
51+
<includes>
52+
<include>com/sonar/python/it/plugin/Tests.java</include>
53+
</includes>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</pluginManagement>
58+
</build>
59+
60+
</project>

its/plugin/profiles/no_rule.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?><!-- Generated by Sonar -->
2+
<profile>
3+
<name>no_rule</name>
4+
<language>py</language>
5+
<rules>
6+
</rules>
7+
</profile>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.example</groupId>
6+
<artifactId>metrics</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<name>Metrics</name>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
</properties>
14+
15+
<build>
16+
<sourceDirectory>src</sourceDirectory>
17+
</build>
18+
19+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#
2+
# Header
3+
#
4+
5+
print "Hello, World!"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" ?>
2+
<!DOCTYPE coverage
3+
SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'>
4+
<coverage branch-rate="0.0" line-rate="0.6" timestamp="1335175420"
5+
version="xxx">
6+
<sources>
7+
<source>
8+
/xxx/SampleProject
9+
</source>
10+
</sources>
11+
<packages>
12+
<package branch-rate="0.0" complexity="0.0" line-rate="0.0"
13+
name="sources.utils">
14+
<classes>
15+
<class branch-rate="0.0" complexity="0.0" filename="src/dir/sample.py"
16+
line-rate="0.0" name="sample_py">
17+
<lines>
18+
<line branch="false" hits="1" number="2" />
19+
<line branch="false" hits="1" number="3" />
20+
<line branch="false" hits="0" number="4" />
21+
<line branch="false" hits="0" number="5" />
22+
<line branch="false" hits="1" number="7" />
23+
</lines>
24+
</class>
25+
</classes>
26+
</package>
27+
</packages>
28+
</coverage>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
x = 1
3+
if x > 3:
4+
x = 0
5+
print "a"
6+
7+
print "b"
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Python :: ITs :: Plugin
3+
* Copyright (C) 2012 SonarSource and Waleri Enns
4+
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package com.sonar.python.it.plugin;
21+
22+
import com.sonar.orchestrator.Orchestrator;
23+
import com.sonar.orchestrator.build.SonarRunner;
24+
import org.junit.BeforeClass;
25+
import org.junit.ClassRule;
26+
import org.junit.Test;
27+
import org.sonar.wsclient.Sonar;
28+
import org.sonar.wsclient.services.Measure;
29+
import org.sonar.wsclient.services.Resource;
30+
import org.sonar.wsclient.services.ResourceQuery;
31+
32+
import java.io.File;
33+
34+
import static org.fest.assertions.Assertions.assertThat;
35+
36+
public class MetricsTest {
37+
38+
private static final String PROJECT_KEY = "metrics";
39+
40+
@ClassRule
41+
public static Orchestrator ORCHESTRATOR = Tests.ORCHESTRATOR;
42+
43+
private static Sonar wsClient;
44+
45+
@BeforeClass
46+
public static void startServer() {
47+
SonarRunner build = SonarRunner.create()
48+
.setProjectDir(new File("projects/metrics"))
49+
.setProjectKey(PROJECT_KEY)
50+
.setProjectName(PROJECT_KEY)
51+
.setProjectVersion("1.0-SNAPSHOT")
52+
.setProfile("no_rule")
53+
.setSourceDirs("src");
54+
ORCHESTRATOR.executeBuild(build);
55+
56+
wsClient = ORCHESTRATOR.getServer().getWsClient();
57+
}
58+
59+
@Test
60+
public void project_level() {
61+
// Size
62+
assertThat(getProjectMeasure("ncloc").getIntValue()).isEqualTo(1);
63+
assertThat(getProjectMeasure("lines").getIntValue()).isEqualTo(6);
64+
assertThat(getProjectMeasure("files").getIntValue()).isEqualTo(1);
65+
assertThat(getProjectMeasure("statements").getIntValue()).isEqualTo(1);
66+
assertThat(getProjectMeasure("directories").getIntValue()).isEqualTo(1);
67+
assertThat(getProjectMeasure("functions").getIntValue()).isEqualTo(0);
68+
assertThat(getProjectMeasure("classes").getIntValue()).isEqualTo(0);
69+
// Documentation
70+
assertThat(getProjectMeasure("comment_lines").getIntValue()).isEqualTo(1);
71+
assertThat(getProjectMeasure("comment_lines_density").getValue()).isEqualTo(50.0);
72+
// Complexity
73+
assertThat(getProjectMeasure("complexity").getValue()).isEqualTo(0.0);
74+
assertThat(getProjectMeasure("function_complexity")).isNull();
75+
assertThat(getProjectMeasure("function_complexity_distribution").getData()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0;20=0;30=0");
76+
assertThat(getProjectMeasure("file_complexity").getValue()).isEqualTo(0.0);
77+
assertThat(getProjectMeasure("file_complexity_distribution").getData()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");
78+
// Duplication
79+
assertThat(getProjectMeasure("duplicated_lines").getValue()).isEqualTo(0.0);
80+
assertThat(getProjectMeasure("duplicated_blocks").getValue()).isEqualTo(0.0);
81+
assertThat(getProjectMeasure("duplicated_files").getValue()).isEqualTo(0.0);
82+
assertThat(getProjectMeasure("duplicated_lines_density").getValue()).isEqualTo(0.0);
83+
// Rules
84+
assertThat(getProjectMeasure("violations").getValue()).isEqualTo(0.0);
85+
86+
assertThat(getProjectMeasure("tests")).isNull();
87+
assertThat(getProjectMeasure("coverage")).isNull();
88+
}
89+
90+
@Test
91+
public void directory_level() {
92+
// Size
93+
assertThat(getDirectoryMeasure("ncloc").getIntValue()).isEqualTo(1);
94+
assertThat(getDirectoryMeasure("lines").getIntValue()).isEqualTo(6);
95+
assertThat(getDirectoryMeasure("files").getIntValue()).isEqualTo(1);
96+
assertThat(getDirectoryMeasure("directories").getIntValue()).isEqualTo(1);
97+
assertThat(getDirectoryMeasure("statements").getIntValue()).isEqualTo(1);
98+
assertThat(getDirectoryMeasure("functions").getIntValue()).isEqualTo(0);
99+
assertThat(getDirectoryMeasure("classes").getIntValue()).isEqualTo(0);
100+
// Documentation
101+
assertThat(getDirectoryMeasure("comment_lines").getIntValue()).isEqualTo(1);
102+
assertThat(getDirectoryMeasure("comment_lines_density").getValue()).isEqualTo(50.0);
103+
// Complexity
104+
assertThat(getDirectoryMeasure("complexity").getValue()).isEqualTo(0.0);
105+
assertThat(getDirectoryMeasure("function_complexity")).isNull();
106+
assertThat(getProjectMeasure("function_complexity_distribution").getData()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0;20=0;30=0");
107+
assertThat(getDirectoryMeasure("file_complexity").getValue()).isEqualTo(0.0);
108+
assertThat(getDirectoryMeasure("file_complexity_distribution").getData()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");
109+
// Duplication
110+
assertThat(getDirectoryMeasure("duplicated_lines").getValue()).isEqualTo(0.0);
111+
assertThat(getDirectoryMeasure("duplicated_blocks").getValue()).isEqualTo(0.0);
112+
assertThat(getDirectoryMeasure("duplicated_files").getValue()).isEqualTo(0.0);
113+
assertThat(getDirectoryMeasure("duplicated_lines_density").getValue()).isEqualTo(0.0);
114+
// Rules
115+
assertThat(getDirectoryMeasure("violations").getValue()).isEqualTo(0.0);
116+
}
117+
118+
@Test
119+
public void file_level() {
120+
// Size
121+
assertThat(getFileMeasure("ncloc").getIntValue()).isEqualTo(1);
122+
assertThat(getFileMeasure("lines").getIntValue()).isEqualTo(6);
123+
assertThat(getFileMeasure("files").getIntValue()).isEqualTo(1);
124+
assertThat(getFileMeasure("statements").getIntValue()).isEqualTo(1);
125+
assertThat(getFileMeasure("functions").getIntValue()).isEqualTo(0);
126+
assertThat(getFileMeasure("classes").getIntValue()).isEqualTo(0);
127+
// Documentation
128+
assertThat(getFileMeasure("comment_lines").getIntValue()).isEqualTo(1);
129+
assertThat(getFileMeasure("comment_lines_density").getValue()).isEqualTo(50.0);
130+
// Complexity
131+
assertThat(getFileMeasure("complexity").getValue()).isEqualTo(0.0);
132+
assertThat(getFileMeasure("function_complexity")).isNull();
133+
assertThat(getFileMeasure("function_complexity_distribution")).isNull();
134+
assertThat(getFileMeasure("file_complexity").getValue()).isEqualTo(0.0);
135+
assertThat(getFileMeasure("file_complexity_distribution")).isNull();
136+
// Duplication
137+
assertThat(getFileMeasure("duplicated_lines")).isNull();
138+
assertThat(getFileMeasure("duplicated_blocks")).isNull();
139+
assertThat(getFileMeasure("duplicated_files")).isNull();
140+
assertThat(getFileMeasure("duplicated_lines_density")).isNull();
141+
// Rules
142+
assertThat(getFileMeasure("violations")).isNull();
143+
}
144+
145+
/**
146+
* SONARPLUGINS-2184
147+
*/
148+
@Test
149+
public void should_be_compatible_with_DevCockpit() {
150+
// TODO probably bug in Sonar: order might depend on JVM
151+
assertThat(getFileMeasure("ncloc_data").getData())
152+
.contains("1=0")
153+
.contains("5=1");
154+
assertThat(getFileMeasure("comment_lines_data").getData())
155+
.contains("1=1")
156+
.contains("4=0");
157+
}
158+
159+
/* Helper methods */
160+
161+
private Measure getProjectMeasure(String metricKey) {
162+
Resource resource = wsClient.find(ResourceQuery.createForMetrics(PROJECT_KEY, metricKey));
163+
return resource == null ? null : resource.getMeasure(metricKey);
164+
}
165+
166+
private Measure getDirectoryMeasure(String metricKey) {
167+
Resource resource = wsClient.find(ResourceQuery.createForMetrics(keyFor("dir"), metricKey));
168+
return resource == null ? null : resource.getMeasure(metricKey);
169+
}
170+
171+
private Measure getFileMeasure(String metricKey) {
172+
Resource resource = wsClient.find(ResourceQuery.createForMetrics(keyFor("dir/HelloWorld.py"), metricKey));
173+
return resource == null ? null : resource.getMeasure(metricKey);
174+
}
175+
176+
private static String keyFor(String s) {
177+
return PROJECT_KEY + ":src/" + s;
178+
}
179+
180+
}

0 commit comments

Comments
 (0)