Skip to content

Commit ddc61dc

Browse files
Daniel Muehlbachlerromani
authored andcommitted
Issue #232: update to SQ 7.9
1 parent 3195b48 commit ddc61dc

File tree

10 files changed

+388
-7
lines changed

10 files changed

+388
-7
lines changed

.ci/travis.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ integration-tests)
1515
URL=$URL"sonar-application/$SONAR_APP_VERSION/sonar-application-$SONAR_APP_VERSION.zip"
1616
wget $URL -O ~/.m2/sonar-application-$SONAR_APP_VERSION.zip
1717
fi
18+
mkdir -p target/temp-downloads && cp ~/.m2/sonar-application-$SONAR_APP_VERSION.zip target/temp-downloads/
1819
mvn -e integration-test -DskipITs=false
1920
;;
2021

config/pmd-main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
PMD ruleset for Checkstyle main code (copy from main repo)
99
</description>
1010
<exclude-pattern>.*/src/test/.*</exclude-pattern>
11+
<exclude-pattern>.*/src/it/.*</exclude-pattern>
12+
1113
<rule ref="config/pmd.xml">
1214
<!-- until https://github.com/checkstyle/sonar-checkstyle/issues/161 -->
1315
<exclude name="CommentRequired"/>

pom.xml

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@
9696

9797
<properties>
9898
<checkstyle.version>8.27</checkstyle.version>
99-
<sonar.version>6.7</sonar.version>
100-
<sonar-java.version>5.12.0.17701</sonar-java.version>
99+
<sonar.version>7.9</sonar.version>
100+
<sonar-java.version>6.0.0.20538</sonar-java.version>
101101
<maven.checkstyle.plugin.version>3.1.0</maven.checkstyle.plugin.version>
102102
<maven.sevntu.checkstyle.plugin.version>1.37.0</maven.sevntu.checkstyle.plugin.version>
103103
<!-- it should be a version of checkstyle that is compatible/compiled with sevntu -->
@@ -176,6 +176,11 @@
176176
<scope>test</scope>
177177
<version>1.6</version>
178178
</dependency>
179+
<dependency>
180+
<groupId>org.slf4j</groupId>
181+
<artifactId>slf4j-api</artifactId>
182+
<version>1.7.30</version>
183+
</dependency>
179184
<!--
180185
We need following dependency, otherwise we will receive
181186
java.lang.NoClassDefFoundError: org/apache/maven/project/MavenProject
@@ -224,23 +229,41 @@
224229
<version>${sonar.version}</version>
225230
</dependency>
226231
<dependency>
227-
<groupId>org.easytesting</groupId>
228-
<artifactId>fest-assert</artifactId>
232+
<groupId>org.sonarsource.orchestrator</groupId>
233+
<artifactId>sonar-orchestrator</artifactId>
234+
<version>3.27.0.2172</version>
229235
<scope>test</scope>
230-
<version>1.4</version>
231236
</dependency>
232237
<dependency>
233238
<groupId>org.slf4j</groupId>
234-
<artifactId>slf4j-nop</artifactId>
235-
<version>1.5.6</version>
239+
<artifactId>jcl-over-slf4j</artifactId>
240+
<version>1.7.30</version>
236241
<scope>test</scope>
237242
</dependency>
243+
<dependency>
244+
<groupId>org.slf4j</groupId>
245+
<artifactId>log4j-over-slf4j</artifactId>
246+
<version>1.7.30</version>
247+
<scope>test</scope>
248+
</dependency>
249+
<dependency>
250+
<groupId>org.easytesting</groupId>
251+
<artifactId>fest-assert</artifactId>
252+
<scope>test</scope>
253+
<version>1.4</version>
254+
</dependency>
238255
<dependency>
239256
<groupId>org.apache.ant</groupId>
240257
<artifactId>ant</artifactId>
241258
<version>1.9.7</version>
242259
<scope>test</scope>
243260
</dependency>
261+
<dependency>
262+
<groupId>org.sonarsource.sonarqube</groupId>
263+
<artifactId>sonar-ws</artifactId>
264+
<version>${sonar.version}</version>
265+
<scope>test</scope>
266+
</dependency>
244267
<dependency>
245268
<groupId>org.jacoco</groupId>
246269
<artifactId>org.jacoco.agent</artifactId>
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
package org.checkstyle.plugins.sonar;
2+
3+
import com.google.gson.Gson;
4+
import com.sonar.orchestrator.Orchestrator;
5+
import com.sonar.orchestrator.build.Build;
6+
import com.sonar.orchestrator.build.BuildResult;
7+
import com.sonar.orchestrator.build.MavenBuild;
8+
import com.sonar.orchestrator.container.Edition;
9+
import com.sonar.orchestrator.container.Server;
10+
import com.sonar.orchestrator.http.HttpMethod;
11+
import com.sonar.orchestrator.http.HttpResponse;
12+
import com.sonar.orchestrator.locator.FileLocation;
13+
import com.sonar.orchestrator.locator.MavenLocation;
14+
import org.apache.commons.io.FileUtils;
15+
import org.apache.commons.lang.StringUtils;
16+
import org.assertj.core.api.Assertions;
17+
import org.fest.util.Collections;
18+
import org.junit.AfterClass;
19+
import org.junit.BeforeClass;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.rules.TemporaryFolder;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.stream.Collectors;
34+
35+
import static org.junit.Assert.fail;
36+
37+
/**
38+
* Integration testing of plugin jar inside of sonar.
39+
*/
40+
public class RunPluginTest {
41+
private static final Logger LOG = LoggerFactory.getLogger(RunPluginTest.class);
42+
private static final String SONAR_APP_VERSION = "7.9.2";
43+
private static final int LOGS_NUMBER_LINES = 200;
44+
private static final String TRUE = "true";
45+
private static final String PROJECT_KEY = "com.puppycrows.tools:checkstyle";
46+
private static final String PROJECT_NAME = "integration-test-project";
47+
private static final List<String> DEACTIVATED_RULES = Collections.list(
48+
"com.puppycrawl.tools.checkstyle.checks.coding.MissingCtorCheck",
49+
"com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck",
50+
"com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck",
51+
"com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck",
52+
"com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck",
53+
"com.puppycrawl.tools.checkstyle.checks.UncommentedMainCheck");
54+
55+
private static Orchestrator orchestrator;
56+
57+
@Rule
58+
public final TemporaryFolder temp = new TemporaryFolder();
59+
60+
@BeforeClass
61+
public static void beforeAll() {
62+
orchestrator = Orchestrator.builderEnv()
63+
.setZipFile(new File("target/temp-downloads/sonar-application-"
64+
+ SONAR_APP_VERSION
65+
+ ".zip"))
66+
//.setSonarVersion(System.getProperty("sonar.runtimeVersion", SONAR_APP_VERSION))
67+
.setEdition(Edition.COMMUNITY)
68+
.addPlugin(FileLocation.byWildcardMavenFilename(new File("target"),
69+
"checkstyle-sonar-plugin-*.jar"))
70+
.addPlugin(MavenLocation.of("org.sonarsource.sonar-lits-plugin",
71+
"sonar-lits-plugin",
72+
"0.8.0.1209"))
73+
.addPlugin(MavenLocation.of("org.sonarsource.java",
74+
"sonar-java-plugin",
75+
"6.0.0.20538"))
76+
.setServerProperty("sonar.web.javaOpts", "-Xmx1G")
77+
.build();
78+
79+
orchestrator.start();
80+
}
81+
82+
@AfterClass
83+
public static void afterAll() {
84+
orchestrator.stop();
85+
}
86+
87+
@Test
88+
public void testSonarExecution() {
89+
try {
90+
MavenBuild build = testProjectBuild();
91+
executeBuildWithCommonProperties(build, true);
92+
} catch(IOException exception) {
93+
LOG.error("Build execution error.", exception);
94+
fail("Failed to execute build.");
95+
}
96+
}
97+
98+
private static void executeBuildWithCommonProperties(Build<?> build, boolean buildQuietly)
99+
throws IOException {
100+
final String dumpOldLocation = FileLocation.of("target/old/" + PROJECT_NAME)
101+
.getFile()
102+
.getAbsolutePath();
103+
final String dumpNewLocation = FileLocation.of("target/actual/" + PROJECT_NAME)
104+
.getFile()
105+
.getAbsolutePath();
106+
build
107+
.setProperty("sonar.login", "admin")
108+
.setProperty("sonar.password", "admin")
109+
.setProperty("sonar.cpd.exclusions", "**/*")
110+
.setProperty("sonar.import_unknown_files", TRUE)
111+
.setProperty("sonar.skipPackageDesign", TRUE)
112+
.setProperty("dump.old", dumpOldLocation)
113+
.setProperty("dump.new", dumpNewLocation)
114+
.setProperty("lits.differences", litsDifferencesPath())
115+
.setProperty("sonar.java.xfile", TRUE)
116+
.setProperty("sonar.java.failOnException", TRUE);
117+
118+
BuildResult buildResult;
119+
// if build fail, job is not violently interrupted, allowing time to dump SQ logs
120+
if(buildQuietly) {
121+
buildResult = orchestrator.executeBuildQuietly(build);
122+
} else {
123+
buildResult = orchestrator.executeBuild(build);
124+
}
125+
126+
if(buildResult.isSuccess()) {
127+
assertNoDifferences();
128+
} else {
129+
dumpServerLogs();
130+
fail("Build failure for project: " + PROJECT_NAME);
131+
}
132+
}
133+
134+
private static void assertNoDifferences() {
135+
try {
136+
String differences = Files.readString(Paths.get(litsDifferencesPath()));
137+
Assertions.assertThat(differences)
138+
.isEmpty();
139+
} catch(IOException exception) {
140+
LOG.error("Failed to read LITS differences.", exception);
141+
fail("LITS differences not computed.");
142+
}
143+
}
144+
145+
private static String litsDifferencesPath() {
146+
return FileLocation.of("target/" + PROJECT_NAME + "_differences")
147+
.getFile()
148+
.getAbsolutePath();
149+
}
150+
151+
private static void dumpServerLogs() throws IOException {
152+
Server server = orchestrator.getServer();
153+
LOG.error(":::::::::::::::: DUMPING SERVER LOGS ::::::::::::::::");
154+
dumpServerLogLastLines(server.getAppLogs());
155+
dumpServerLogLastLines(server.getCeLogs());
156+
dumpServerLogLastLines(server.getEsLogs());
157+
dumpServerLogLastLines(server.getWebLogs());
158+
}
159+
160+
private static void dumpServerLogLastLines(File logFile) throws IOException {
161+
if(!logFile.exists()) {
162+
return;
163+
}
164+
165+
List<String> logs = Files.readAllLines(logFile.toPath());
166+
int nbLines = logs.size();
167+
if(nbLines > LOGS_NUMBER_LINES) {
168+
logs = logs.subList(nbLines - LOGS_NUMBER_LINES, nbLines);
169+
}
170+
String collectedLogs = logs.stream()
171+
.collect(Collectors.joining(System.lineSeparator()));
172+
173+
LOG.error("============= START {} =============", logFile.getName());
174+
LOG.error("{} {}", System.lineSeparator(), collectedLogs);
175+
LOG.error("============= END {} =============", logFile.getName());
176+
}
177+
178+
private MavenBuild testProjectBuild() throws IOException {
179+
File targetDir = prepareProject();
180+
181+
String pomLocation = targetDir.getCanonicalPath() + "/pom.xml";
182+
File pomFile = FileLocation.of(pomLocation)
183+
.getFile()
184+
.getCanonicalFile();
185+
MavenBuild mavenBuild = MavenBuild.create()
186+
.setPom(pomFile)
187+
.setCleanPackageSonarGoals()
188+
.addArgument("-Dmaven.test.skip=true")
189+
.addArgument("-DskipTests")
190+
.addArgument("-DskipITs");
191+
mavenBuild.setProperty("sonar.projectKey", PROJECT_KEY);
192+
return mavenBuild;
193+
}
194+
195+
@SuppressWarnings("unchecked")
196+
private File prepareProject() throws IOException {
197+
// set severities of all active rules to INFO
198+
String profilesResponse = orchestrator.getServer()
199+
.newHttpCall("api/qualityprofiles/create")
200+
.setAdminCredentials()
201+
.setMethod(HttpMethod.POST)
202+
.setParam("language", "java")
203+
.setParam("name", "checkstyle")
204+
.execute()
205+
.getBodyAsString();
206+
Map<String, Object> map = new Gson().fromJson(profilesResponse, Map.class);
207+
String profileKey = ((Map<String, String>) map.get("profile")).get("key");
208+
if(StringUtils.isEmpty(profileKey)) {
209+
fail("Could not retrieve profile key: setting up quality profile failed.");
210+
} else {
211+
HttpResponse activateRulesResponse = orchestrator.getServer()
212+
.newHttpCall("api/qualityprofiles/activate_rules")
213+
.setAdminCredentials()
214+
.setMethod(HttpMethod.POST)
215+
.setParam("activation_severity", "INFO")
216+
.setParam("languages", "java")
217+
.setParam("profile_key", profileKey)
218+
.setParam("repositories", "checkstyle")
219+
.executeUnsafely();
220+
if(!activateRulesResponse.isSuccessful()) {
221+
fail(String.format("Failed to activate all rules. %s",
222+
activateRulesResponse.getBodyAsString()));
223+
}
224+
// deactivate some rules for test project
225+
for(String ruleKey : DEACTIVATED_RULES) {
226+
HttpResponse deactivateRulesResponse = orchestrator.getServer()
227+
.newHttpCall("api/qualityprofiles/deactivate_rule")
228+
.setAdminCredentials()
229+
.setMethod(HttpMethod.POST)
230+
.setParam("rule_key", "checkstyle:" + ruleKey)
231+
.setParam("profile_key", profileKey)
232+
.executeUnsafely();
233+
if(!deactivateRulesResponse.isSuccessful()) {
234+
fail(String.format("Failed to deactivate rule %s. %s",
235+
ruleKey,
236+
deactivateRulesResponse.getBodyAsString()));
237+
}
238+
}
239+
}
240+
241+
// associate CS profile
242+
orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
243+
HttpResponse assignQPResponse = orchestrator.getServer()
244+
.newHttpCall("api/qualityprofiles/add_project")
245+
.setAdminCredentials()
246+
.setMethod(HttpMethod.POST)
247+
.setParam("language", "java")
248+
.setParam("profileName", "checkstyle")
249+
.setParam("projectKey", PROJECT_KEY)
250+
.executeUnsafely();
251+
if(!assignQPResponse.isSuccessful()) {
252+
fail(String.format("Failed to add project to quality profile. %s",
253+
assignQPResponse.getBodyAsString()));
254+
}
255+
256+
// copy project to analysis space
257+
Path projectRoot = Paths.get("src/it/resources/" + PROJECT_NAME);
258+
File targetDir = temp.newFolder(PROJECT_NAME);
259+
FileUtils.copyDirectory(projectRoot.toFile(), targetDir);
260+
return targetDir;
261+
}
262+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
taken from:
2+
https://github.com/SonarSource/sonar-scanning-examples
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.puppycrows.tools</groupId>
7+
<artifactId>checkstyle</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<name>integration-test-project</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.12</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<pluginManagement>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>3.8.1</version>
33+
</plugin>
34+
</plugins>
35+
</pluginManagement>
36+
</build>
37+
</project>

0 commit comments

Comments
 (0)