Skip to content

Commit 53ca7f3

Browse files
Daniel Muehlbachlerromani
authored andcommitted
Issue #269: enable checkstyle for test sources
1 parent e2fa022 commit 53ca7f3

File tree

3 files changed

+111
-67
lines changed

3 files changed

+111
-67
lines changed

src/it/java/org/checkstyle/plugins/sonar/CheckJarTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code for adherence to a set of rules.
3+
// Copyright (C) 2001-2020 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 3 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
////////////////////////////////////////////////////////////////////////////////
19+
120
package org.checkstyle.plugins.sonar;
221

322
import static org.junit.Assert.assertTrue;

src/it/java/org/checkstyle/plugins/sonar/RunPluginTest.java

Lines changed: 86 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code for adherence to a set of rules.
3+
// Copyright (C) 2001-2020 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 3 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
////////////////////////////////////////////////////////////////////////////////
19+
120
package org.checkstyle.plugins.sonar;
221

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;
22+
import static org.junit.Assert.fail;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.stream.Collectors;
32+
1433
import org.apache.commons.io.FileUtils;
1534
import org.apache.commons.lang.StringUtils;
1635
import org.assertj.core.api.Assertions;
@@ -23,16 +42,17 @@
2342
import org.slf4j.Logger;
2443
import org.slf4j.LoggerFactory;
2544

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;
45+
import com.google.gson.Gson;
46+
import com.sonar.orchestrator.Orchestrator;
47+
import com.sonar.orchestrator.build.Build;
48+
import com.sonar.orchestrator.build.BuildResult;
49+
import com.sonar.orchestrator.build.MavenBuild;
50+
import com.sonar.orchestrator.container.Edition;
51+
import com.sonar.orchestrator.container.Server;
52+
import com.sonar.orchestrator.http.HttpMethod;
53+
import com.sonar.orchestrator.http.HttpResponse;
54+
import com.sonar.orchestrator.locator.FileLocation;
55+
import com.sonar.orchestrator.locator.MavenLocation;
3656

3757
/**
3858
* Integration testing of plugin jar inside of sonar.
@@ -87,9 +107,10 @@ public static void afterAll() {
87107
@Test
88108
public void testSonarExecution() {
89109
try {
90-
MavenBuild build = testProjectBuild();
110+
final MavenBuild build = testProjectBuild();
91111
executeBuildWithCommonProperties(build, true);
92-
} catch(IOException exception) {
112+
}
113+
catch (IOException exception) {
93114
LOG.error("Build execution error.", exception);
94115
fail("Failed to execute build.");
95116
}
@@ -115,28 +136,31 @@ private static void executeBuildWithCommonProperties(Build<?> build, boolean bui
115136
.setProperty("sonar.java.xfile", TRUE)
116137
.setProperty("sonar.java.failOnException", TRUE);
117138

118-
BuildResult buildResult;
139+
final BuildResult buildResult;
119140
// if build fail, job is not violently interrupted, allowing time to dump SQ logs
120-
if(buildQuietly) {
141+
if (buildQuietly) {
121142
buildResult = orchestrator.executeBuildQuietly(build);
122-
} else {
143+
}
144+
else {
123145
buildResult = orchestrator.executeBuild(build);
124146
}
125147

126-
if(buildResult.isSuccess()) {
148+
if (buildResult.isSuccess()) {
127149
assertNoDifferences();
128-
} else {
150+
}
151+
else {
129152
dumpServerLogs();
130153
fail("Build failure for project: " + PROJECT_NAME);
131154
}
132155
}
133156

134157
private static void assertNoDifferences() {
135158
try {
136-
String differences = Files.readString(Paths.get(litsDifferencesPath()));
159+
final String differences = Files.readString(Paths.get(litsDifferencesPath()));
137160
Assertions.assertThat(differences)
138161
.isEmpty();
139-
} catch(IOException exception) {
162+
}
163+
catch (IOException exception) {
140164
LOG.error("Failed to read LITS differences.", exception);
141165
fail("LITS differences not computed.");
142166
}
@@ -149,7 +173,7 @@ private static String litsDifferencesPath() {
149173
}
150174

151175
private static void dumpServerLogs() throws IOException {
152-
Server server = orchestrator.getServer();
176+
final Server server = orchestrator.getServer();
153177
LOG.error(":::::::::::::::: DUMPING SERVER LOGS ::::::::::::::::");
154178
dumpServerLogLastLines(server.getAppLogs());
155179
dumpServerLogLastLines(server.getCeLogs());
@@ -158,31 +182,29 @@ private static void dumpServerLogs() throws IOException {
158182
}
159183

160184
private static void dumpServerLogLastLines(File logFile) throws IOException {
161-
if(!logFile.exists()) {
162-
return;
163-
}
185+
if (logFile.exists()) {
186+
List<String> logs = Files.readAllLines(logFile.toPath());
187+
final int nbLines = logs.size();
188+
if (nbLines > LOGS_NUMBER_LINES) {
189+
logs = logs.subList(nbLines - LOGS_NUMBER_LINES, nbLines);
190+
}
191+
final String collectedLogs = logs.stream()
192+
.collect(Collectors.joining(System.lineSeparator()));
164193

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);
194+
LOG.error("============= START {} =============", logFile.getName());
195+
LOG.error("{} {}", System.lineSeparator(), collectedLogs);
196+
LOG.error("============= END {} =============", logFile.getName());
169197
}
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());
176198
}
177199

178200
private MavenBuild testProjectBuild() throws IOException {
179-
File targetDir = prepareProject();
201+
final File targetDir = prepareProject();
180202

181-
String pomLocation = targetDir.getCanonicalPath() + "/pom.xml";
182-
File pomFile = FileLocation.of(pomLocation)
203+
final String pomLocation = targetDir.getCanonicalPath() + "/pom.xml";
204+
final File pomFile = FileLocation.of(pomLocation)
183205
.getFile()
184206
.getCanonicalFile();
185-
MavenBuild mavenBuild = MavenBuild.create()
207+
final MavenBuild mavenBuild = MavenBuild.create()
186208
.setPom(pomFile)
187209
.setCleanPackageSonarGoals()
188210
.addArgument("-Dmaven.test.skip=true")
@@ -195,20 +217,21 @@ private MavenBuild testProjectBuild() throws IOException {
195217
@SuppressWarnings("unchecked")
196218
private File prepareProject() throws IOException {
197219
// set severities of all active rules to INFO
198-
String profilesResponse = orchestrator.getServer()
220+
final String profilesResponse = orchestrator.getServer()
199221
.newHttpCall("api/qualityprofiles/create")
200222
.setAdminCredentials()
201223
.setMethod(HttpMethod.POST)
202224
.setParam("language", "java")
203225
.setParam("name", "checkstyle")
204226
.execute()
205227
.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)) {
228+
final Map<String, Object> map = new Gson().fromJson(profilesResponse, Map.class);
229+
final String profileKey = ((Map<String, String>) map.get("profile")).get("key");
230+
if (StringUtils.isEmpty(profileKey)) {
209231
fail("Could not retrieve profile key: setting up quality profile failed.");
210-
} else {
211-
HttpResponse activateRulesResponse = orchestrator.getServer()
232+
}
233+
else {
234+
final HttpResponse activateRulesResponse = orchestrator.getServer()
212235
.newHttpCall("api/qualityprofiles/activate_rules")
213236
.setAdminCredentials()
214237
.setMethod(HttpMethod.POST)
@@ -217,20 +240,20 @@ private File prepareProject() throws IOException {
217240
.setParam("profile_key", profileKey)
218241
.setParam("repositories", "checkstyle")
219242
.executeUnsafely();
220-
if(!activateRulesResponse.isSuccessful()) {
243+
if (!activateRulesResponse.isSuccessful()) {
221244
fail(String.format("Failed to activate all rules. %s",
222245
activateRulesResponse.getBodyAsString()));
223246
}
224247
// deactivate some rules for test project
225-
for(String ruleKey : DEACTIVATED_RULES) {
226-
HttpResponse deactivateRulesResponse = orchestrator.getServer()
248+
for (String ruleKey : DEACTIVATED_RULES) {
249+
final HttpResponse deactivateRulesResponse = orchestrator.getServer()
227250
.newHttpCall("api/qualityprofiles/deactivate_rule")
228251
.setAdminCredentials()
229252
.setMethod(HttpMethod.POST)
230253
.setParam("rule_key", "checkstyle:" + ruleKey)
231254
.setParam("profile_key", profileKey)
232255
.executeUnsafely();
233-
if(!deactivateRulesResponse.isSuccessful()) {
256+
if (!deactivateRulesResponse.isSuccessful()) {
234257
fail(String.format("Failed to deactivate rule %s. %s",
235258
ruleKey,
236259
deactivateRulesResponse.getBodyAsString()));
@@ -240,22 +263,22 @@ private File prepareProject() throws IOException {
240263

241264
// associate CS profile
242265
orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
243-
HttpResponse assignQPResponse = orchestrator.getServer()
266+
final HttpResponse assignQpResponse = orchestrator.getServer()
244267
.newHttpCall("api/qualityprofiles/add_project")
245268
.setAdminCredentials()
246269
.setMethod(HttpMethod.POST)
247270
.setParam("language", "java")
248271
.setParam("profileName", "checkstyle")
249272
.setParam("projectKey", PROJECT_KEY)
250273
.executeUnsafely();
251-
if(!assignQPResponse.isSuccessful()) {
274+
if (!assignQpResponse.isSuccessful()) {
252275
fail(String.format("Failed to add project to quality profile. %s",
253-
assignQPResponse.getBodyAsString()));
276+
assignQpResponse.getBodyAsString()));
254277
}
255278

256279
// copy project to analysis space
257-
Path projectRoot = Paths.get("src/it/resources/" + PROJECT_NAME);
258-
File targetDir = temp.newFolder(PROJECT_NAME);
280+
final Path projectRoot = Paths.get("src/it/resources/" + PROJECT_NAME);
281+
final File targetDir = temp.newFolder(PROJECT_NAME);
259282
FileUtils.copyDirectory(projectRoot.toFile(), targetDir);
260283
return targetDir;
261284
}

src/test/java/org/sonar/plugins/checkstyle/internal/ChecksTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ private static void validateSonarRules(Set<Class<?>> modules)
101101
final String input = Files.readString(rulesFile.toPath());
102102
final Document document = XmlUtil.getRawXml(rulesFile.getAbsolutePath(), input, input);
103103
validateSonarRules(document, modules);
104-
} catch(IOException ignored) {
104+
}
105+
catch (IOException ignored) {
105106
Assert.fail("Failed to read rulesFile.");
106107
}
107108
}
@@ -396,8 +397,8 @@ else if (AbstractFileSetCheck.class.isAssignableFrom(clss)) {
396397
// remove undocumented properties
397398
new HashSet<>(properties)
398399
.stream()
399-
.filter(property -> UNDOCUMENTED_PROPERTIES.contains(clss
400-
.getSimpleName() + "." + property))
400+
.filter(property -> UNDOCUMENTED_PROPERTIES
401+
.contains(clss.getSimpleName() + "." + property))
401402
.forEach(properties::remove);
402403

403404
if (AbstractCheck.class.isAssignableFrom(clss)) {
@@ -452,7 +453,8 @@ private static Set<String> getProperties(Class<?> clss) {
452453
final PropertyDescriptor[] map = PropertyUtils.getPropertyDescriptors(clss);
453454

454455
for (PropertyDescriptor p : map) {
455-
if (p.getWriteMethod() != null && !p.getWriteMethod().isAnnotationPresent(Deprecated.class)) {
456+
if (p.getWriteMethod() != null
457+
&& !p.getWriteMethod().isAnnotationPresent(Deprecated.class)) {
456458
result.add(p.getName());
457459
}
458460
}

0 commit comments

Comments
 (0)