Skip to content

Commit 7841a52

Browse files
committed
Issue #118: add Treewalker Filter Group to Sonar UI
1 parent 746090b commit 7841a52

File tree

14 files changed

+89
-31
lines changed

14 files changed

+89
-31
lines changed

checkstyle-sonar-plugin/config/sevntu_suppressions.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
<!-- parsing of xml imply reference attribute names, does not make sense to move all to special variables -->
1717
<suppress checks="MultipleStringLiteralsExtended" files="CheckstyleProfileImporter\.java"/>
18-
18+
<!-- till https://github.com/checkstyle/sonar-checkstyle/issues/135 -->
19+
<suppress checks="MultipleStringLiteralsExtended" files="CheckstylePlugin\.java"/>
20+
1921
<!-- Tone down the checking for test code -->
2022
<suppress checks="MultipleStringLiteralsExtended" files=".*[\\/]src[\\/]test[\\/]"/>
2123
<suppress checks="ForbidCCommentsInMethods" files=".*[\\/]src[\\/]test[\\/]"/>

checkstyle-sonar-plugin/config/suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
<!-- parsing of xml imply reference attribute names, does not make sense to move all to special variables -->
2020
<suppress checks="MultipleStringLiterals" files="CheckstyleProfileImporter\.java"/>
21+
<!-- till https://github.com/checkstyle/sonar-checkstyle/issues/135 -->
22+
<suppress checks="MultipleStringLiterals" files=".*[\\/]CheckstylePlugin\.java"/>
2123
<!-- messing test code with such optimization does not make sense , readability will decrease -->
2224
<suppress checks="MultipleStringLiterals" files=".*[\\/]src[\\/]test[\\/]"/>
2325

checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConstants.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ public final class CheckstyleConstants {
2626
public static final String PLUGIN_NAME = REPOSITORY_NAME;
2727
public static final String REPOSITORY_KEY = PLUGIN_KEY;
2828

29-
public static final String FILTERS_KEY = "sonar.checkstyle.filters";
29+
public static final String CHECKER_FILTERS_KEY = "sonar.checkstyle.filters";
30+
public static final String TREEWALKER_FILTERS_KEY = "sonar.checkstyle.treewalkerfilters";
3031

31-
public static final String FILTERS_DEFAULT_VALUE =
32-
"<module name=\"SuppressionCommentFilter\" />"
33-
+ "<module name=\"SuppressWarningsFilter\" />";
32+
public static final String CHECKER_FILTERS_DEFAULT_VALUE =
33+
"<module name=\"SuppressWarningsFilter\" />";
34+
35+
public static final String TREEWALKER_FILTERS_DEFAULT_VALUE =
36+
"<module name=\"SuppressionCommentFilter\" />";
3437

3538
public static final String JAVA_KEY = "java";
3639

checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,51 @@ public final class CheckstylePlugin extends SonarPlugin {
3232

3333
private static final String CHECKSTYLE_SUB_CATEGORY_NAME = "Checkstyle";
3434

35-
private static final String CHECKER_FILTERS_DESCRIPTION = "Checkstyle supports"
36-
+ " <a href=\"http://checkstyle.sourceforge.net/config_filefilters.html\">file filter"
37-
+ "</a> and several "
38-
+ "<a href=\"http://checkstyle.sourceforge.net/config_filters.html\">"
39-
+ "violation filtering mechanisms</a>: "
40-
+ "This property allows the configuration of those filters with a "
35+
private static final String FILTERS_DESCRIPTION_HEADER = "Checkstyle supports";
36+
private static final String FILTERS_DESCRIPTION_FOOTER = "This property"
37+
+ " allows the configuration of those filters with a "
4138
+ "native XML format. See the "
4239
+ "<a href='http://checkstyle.sourceforge.net/config.html'>"
4340
+ "Checkstyle</a> "
4441
+ "configuration for more information.";
4542

43+
private static final String CHECKER_FILTERS_DESCRIPTION = FILTERS_DESCRIPTION_HEADER
44+
+ " <a href=\"http://checkstyle.sourceforge.net/config_filefilters.html\">file filter"
45+
+ "</a> and several "
46+
+ "<a href=\"http://checkstyle.sourceforge.net/config_filters.html\">"
47+
+ "violation filtering mechanisms</a>: "
48+
+ FILTERS_DESCRIPTION_FOOTER;
49+
50+
private static final String TREEWALKER_FILTERS_DESCRIPTION = FILTERS_DESCRIPTION_HEADER
51+
+ " <a href=\"http://checkstyle.sourceforge.net/config_filters.html"
52+
+ "#SuppressWithNearbyCommentFilter\">"
53+
+ "SuppressWithNearbyCommentFilter"
54+
+ "</a> and "
55+
+ " <a href=\"http://checkstyle.sourceforge.net/config_filters.html"
56+
+ "#SuppressionCommentFilter\">"
57+
+ "SuppressionCommentFilter</a>: "
58+
+ FILTERS_DESCRIPTION_FOOTER;
59+
4660
@SuppressWarnings("rawtypes")
4761
@Override
4862
public List getExtensions() {
4963
return Arrays
50-
.asList(PropertyDefinition
51-
.builder(CheckstyleConstants.FILTERS_KEY)
52-
.defaultValue(CheckstyleConstants.FILTERS_DEFAULT_VALUE)
53-
.category(CoreProperties.CATEGORY_JAVA)
54-
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
55-
.name("Filters")
56-
.description(CHECKER_FILTERS_DESCRIPTION)
57-
.type(PropertyType.TEXT)
58-
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
64+
.asList(PropertyDefinition.builder(CheckstyleConstants.CHECKER_FILTERS_KEY)
65+
.defaultValue(CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE)
66+
.category(CoreProperties.CATEGORY_JAVA)
67+
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
68+
.name("Checker Filters")
69+
.description(CHECKER_FILTERS_DESCRIPTION)
70+
.type(PropertyType.TEXT)
71+
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
72+
PropertyDefinition.builder(CheckstyleConstants.TREEWALKER_FILTERS_KEY)
73+
.defaultValue(CheckstyleConstants.TREEWALKER_FILTERS_DEFAULT_VALUE)
74+
.category(CoreProperties.CATEGORY_JAVA)
75+
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)
76+
.name("Treewalker Filters")
77+
.description(TREEWALKER_FILTERS_DESCRIPTION)
78+
.type(PropertyType.TEXT)
79+
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE).build(),
5980
PropertyDefinition.builder(CheckstyleConfiguration.PROPERTY_GENERATE_XML)
6081
.defaultValue("false").category(CoreProperties.CATEGORY_JAVA)
6182
.subCategory(CHECKSTYLE_SUB_CATEGORY_NAME)

checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static void appendXmlHeader(Writer writer) throws IOException {
8787
}
8888

8989
private void appendCustomFilters(Writer writer) throws IOException {
90-
final String filtersXml = settings.getString(CheckstyleConstants.FILTERS_KEY);
90+
final String filtersXml = settings.getString(CheckstyleConstants.CHECKER_FILTERS_KEY);
9191
if (StringUtils.isNotBlank(filtersXml)) {
9292
writer.append(filtersXml);
9393
}
@@ -123,11 +123,16 @@ private void appendTreeWalker(Writer writer,
123123
}
124124
}
125125
}
126+
// append Treewalker filters
127+
final String filtersXml = settings.getString(CheckstyleConstants.TREEWALKER_FILTERS_KEY);
128+
if (StringUtils.isNotBlank(filtersXml)) {
129+
writer.append(filtersXml);
130+
}
126131
writer.append(CLOSE_MODULE);
127132
}
128133

129134
private boolean isSuppressWarningsEnabled() {
130-
final String filtersXml = settings.getString(CheckstyleConstants.FILTERS_KEY);
135+
final String filtersXml = settings.getString(CheckstyleConstants.CHECKER_FILTERS_KEY);
131136
boolean result = false;
132137
if (filtersXml != null) {
133138
result = filtersXml.contains("<module name=\"SuppressWarningsFilter\" />");

checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public void getCheckstyleConfiguration() throws Exception {
100100
fileSystem.setEncoding(StandardCharsets.UTF_8);
101101
final Settings settings = new Settings(new PropertyDefinitions(
102102
new CheckstylePlugin().getExtensions()));
103-
settings.setProperty(CheckstyleConstants.FILTERS_KEY,
104-
CheckstyleConstants.FILTERS_DEFAULT_VALUE);
103+
settings.setProperty(CheckstyleConstants.CHECKER_FILTERS_KEY,
104+
CheckstyleConstants.CHECKER_FILTERS_DEFAULT_VALUE);
105105

106106
final RulesProfile profile = RulesProfile.create("sonar way", "java");
107107

checkstyle-sonar-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public void exportParameters() {
143143
}
144144

145145
@Test
146-
public void addCustomFilters() {
147-
settings.setProperty(CheckstyleConstants.FILTERS_KEY,
146+
public void addCustomCheckerFilters() {
147+
settings.setProperty(CheckstyleConstants.CHECKER_FILTERS_KEY,
148148
"<module name=\"SuppressionCommentFilter\">"
149149
+ "<property name=\"offCommentFormat\" value=\"BEGIN GENERATED CODE\"/>"
150150
+ "<property name=\"onCommentFormat\" value=\"END GENERATED CODE\"/>"
@@ -163,6 +163,20 @@ public void addCustomFilters() {
163163
+ "addCustomFilters.xml", sanitizeForTests(writer.toString()));
164164
}
165165

166+
@Test
167+
public void addCustomTreewalkerFilters() {
168+
settings.setProperty(CheckstyleConstants.TREEWALKER_FILTERS_KEY,
169+
"<module name=\"SuppressWithNearbyCommentFilter\"/>");
170+
171+
final RulesProfile profile = RulesProfile.create("sonar way", "java");
172+
final StringWriter writer = new StringWriter();
173+
new CheckstyleProfileExporter(settings).exportProfile(profile, writer);
174+
175+
CheckstyleTestUtils.assertSimilarXmlWithResource(
176+
"/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/"
177+
+ "addCustomTreewalkerFilters.xml", sanitizeForTests(writer.toString()));
178+
}
179+
166180
private static String sanitizeForTests(String xml) {
167181
// remove the doctype declaration, else the unit test fails when
168182
// executed offline

checkstyle-sonar-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addCustomFilters.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313

1414
<module name="TreeWalker">
1515
<module name="FileContentsHolder"/>
16+
<module name="SuppressionCommentFilter"/>
1617
</module>
1718
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Generated by Sonar -->
3+
<module name="Checker">
4+
<module name="SuppressWarningsFilter"/>
5+
<module name="TreeWalker">
6+
<module name="FileContentsHolder"/>
7+
<module name="SuppressWarningsHolder"/>
8+
<module name="SuppressWithNearbyCommentFilter"/>
9+
</module>
10+
</module>

checkstyle-sonar-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addTheIdPropertyWhenManyInstancesWithTheSameConfigKey.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!-- Generated by Sonar -->
33
<module name="Checker">
4-
<module name="SuppressionCommentFilter"/>
54
<module name="SuppressWarningsFilter" />
65
<module name="JavadocPackage">
76
<property name="severity" value="warning" />
@@ -13,5 +12,6 @@
1312
<module name="TreeWalker">
1413
<module name="FileContentsHolder"/>
1514
<module name="SuppressWarningsHolder"/>
15+
<module name="SuppressionCommentFilter"/>
1616
</module>
1717
</module>

0 commit comments

Comments
 (0)