Skip to content

Commit c3565b1

Browse files
Zopsssromani
authored andcommittedAug 6, 2024·
Issue checkstyle#15214: migrated section 5.1 to whole config testing and updated it on coverage table, removed unused verifyWithConfigParser method
1 parent 93d7140 commit c3565b1

File tree

4 files changed

+127
-203
lines changed

4 files changed

+127
-203
lines changed
 

‎src/it/java/com/google/checkstyle/test/base/AbstractGoogleModuleTestSupport.java

-120
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@
2020
package com.google.checkstyle.test.base;
2121

2222
import java.io.IOException;
23-
import java.util.ArrayList;
24-
import java.util.Arrays;
25-
import java.util.HashSet;
26-
import java.util.List;
2723
import java.util.Properties;
2824
import java.util.Set;
2925

3026
import org.checkstyle.base.AbstractItModuleTestSupport;
3127

3228
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
33-
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
3429
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
35-
import com.puppycrawl.tools.checkstyle.TreeWalker;
3630
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
3731
import com.puppycrawl.tools.checkstyle.api.Configuration;
3832
import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
@@ -46,26 +40,6 @@ public abstract class AbstractGoogleModuleTestSupport extends AbstractItModuleTe
4640

4741
private static final Set<Class<?>> CHECKSTYLE_MODULES;
4842

49-
private static final Set<String> CHECKER_CHILDREN = new HashSet<>(Arrays.asList(
50-
"BeforeExecutionExclusionFileFilter",
51-
"SeverityMatchFilter",
52-
"SuppressionFilter",
53-
"SuppressionSingleFilter",
54-
"SuppressWarningsFilter",
55-
"SuppressWithNearbyTextFilter",
56-
"SuppressWithPlainTextCommentFilter",
57-
"JavadocPackage",
58-
"NewlineAtEndOfFile",
59-
"UniqueProperties",
60-
"OrderedProperties",
61-
"RegexpMultiline",
62-
"RegexpSingleline",
63-
"RegexpOnFilename",
64-
"FileLength",
65-
"LineLength",
66-
"FileTabCharacter"
67-
));
68-
6943
static {
7044
try {
7145
final Properties properties = new Properties();
@@ -103,42 +77,6 @@ protected ModuleCreationOption findModuleCreationOption(String moduleName) {
10377
return moduleCreationOption;
10478
}
10579

106-
/**
107-
* Returns {@link Configuration} instance for the given module name.
108-
* This implementation uses {@link #getModuleConfig(String, String)} method inside.
109-
*
110-
* @param moduleName module name.
111-
* @return {@link Configuration} instance for the given module name.
112-
*/
113-
protected static Configuration getModuleConfig(String moduleName) {
114-
return getModuleConfig(moduleName, null);
115-
}
116-
117-
/**
118-
* Returns {@link Configuration} instance for the given module name.
119-
* This implementation uses {@link #getModuleConfig(String)} method inside.
120-
*
121-
* @param moduleName module name.
122-
* @param moduleId module id.
123-
* @return {@link Configuration} instance for the given module name.
124-
* @throws IllegalStateException if there is a problem retrieving the module or config.
125-
*/
126-
protected static Configuration getModuleConfig(String moduleName, String moduleId) {
127-
return getModuleConfig(CONFIGURATION, moduleName, moduleId);
128-
}
129-
130-
/**
131-
* Returns a list of all {@link Configuration} instances for the given module IDs.
132-
*
133-
* @param moduleIds module IDs.
134-
* @return List of {@link Configuration} instances.
135-
* @throws CheckstyleException if there is an error with the config.
136-
*/
137-
protected static List<Configuration> getModuleConfigsByIds(String... moduleIds)
138-
throws CheckstyleException {
139-
return getModuleConfigsByIds(CONFIGURATION, moduleIds);
140-
}
141-
14280
/**
14381
* Performs verification of the file with the given file path against the whole config.
14482
*
@@ -148,62 +86,4 @@ protected static List<Configuration> getModuleConfigsByIds(String... moduleIds)
14886
protected void verifyWithWholeConfig(String filePath) throws Exception {
14987
verifyWithItConfig(CONFIGURATION, filePath);
15088
}
151-
152-
// until https://github.com/checkstyle/checkstyle/issues/15214
153-
/**
154-
* Performs verification of the file with the given file path against config.
155-
* It uses the specified list of modules to load them from config for validation.
156-
*
157-
* @param listOfModules list of modules to load from config.
158-
* @param filePath file path to verify.
159-
* @throws Exception if exception occurs during verification process.
160-
*/
161-
protected final void verifyWithConfigParser(String[] listOfModules,
162-
String filePath) throws Exception {
163-
final DefaultConfiguration rootConfig = new DefaultConfiguration(ROOT_MODULE_NAME);
164-
final DefaultConfiguration treeWalkerConfig =
165-
new DefaultConfiguration(TreeWalker.class.getSimpleName());
166-
167-
for (String module : listOfModules) {
168-
final List<Configuration> children =
169-
getConfigChildren(module);
170-
if (CHECKER_CHILDREN.contains(module)) {
171-
for (Configuration child : children) {
172-
rootConfig.addChild(child);
173-
}
174-
}
175-
else {
176-
for (Configuration child : children) {
177-
treeWalkerConfig.addChild(child);
178-
}
179-
}
180-
}
181-
182-
if (treeWalkerConfig.getChildren().length > 0) {
183-
rootConfig.addChild(treeWalkerConfig);
184-
}
185-
186-
verifyWithItConfig(rootConfig, filePath);
187-
}
188-
189-
/**
190-
* Gets the specified children module(s) from the config.
191-
*
192-
* @param module the children module to get. It can be either a module name or module id.
193-
* @return list of children modules.
194-
* @throws CheckstyleException if there is a problem for getting the module.
195-
*/
196-
private static List<Configuration> getConfigChildren(String module)
197-
throws CheckstyleException {
198-
final List<Configuration> children = new ArrayList<>();
199-
200-
try {
201-
children.add(getModuleConfig(module));
202-
}
203-
catch (IllegalStateException ex) {
204-
children.addAll(getModuleConfigsByIds(module));
205-
}
206-
207-
return children;
208-
}
20989
}

‎src/it/java/com/google/checkstyle/test/chapter5naming/rule51identifiernames/CatchParameterNameTest.java renamed to ‎src/it/java/com/google/checkstyle/test/chapter5naming/rule51identifiernames/RulesCommonToAllIdentifiersTest.java

+2-22
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919

2020
package com.google.checkstyle.test.chapter5naming.rule51identifiernames;
2121

22-
import java.util.Map;
23-
2422
import org.junit.jupiter.api.Test;
2523

2624
import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
27-
import com.puppycrawl.tools.checkstyle.api.Configuration;
2825

29-
public class CatchParameterNameTest extends AbstractGoogleModuleTestSupport {
26+
public class RulesCommonToAllIdentifiersTest extends AbstractGoogleModuleTestSupport {
3027

3128
@Override
3229
protected String getPackageLocation() {
@@ -35,24 +32,7 @@ protected String getPackageLocation() {
3532

3633
@Test
3734
public void testCatchParameterName() throws Exception {
38-
final String msgKey = "name.invalidPattern";
39-
final Configuration checkConfig = getModuleConfig("CatchParameterName");
40-
final String format = checkConfig.getProperty("format");
41-
final Map<String, String> messages = checkConfig.getMessages();
42-
43-
final String[] expected = {
44-
"47:28: " + getCheckMessage(messages, msgKey, "iException", format),
45-
"50:28: " + getCheckMessage(messages, msgKey, "ex_1", format),
46-
"53:28: " + getCheckMessage(messages, msgKey, "eX", format),
47-
"56:28: " + getCheckMessage(messages, msgKey, "eXX", format),
48-
"59:28: " + getCheckMessage(messages, msgKey, "x_y_z", format),
49-
"62:28: " + getCheckMessage(messages, msgKey, "Ex", format),
50-
};
51-
52-
final String filePath = getPath("InputCatchParameterName.java");
53-
54-
final Integer[] warnList = getLinesWithWarn(filePath);
55-
verify(checkConfig, filePath, expected, warnList);
35+
verifyWithWholeConfig(getPath("InputCatchParameterName.java"));
5636
}
5737

5838
}
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,115 @@
11
package com.google.checkstyle.test.chapter5naming.rule51identifiernames;
22

3+
/** some javadoc. */
34
public class InputCatchParameterName {
4-
{
5-
try {
6-
} catch (Exception e) {
7-
}
8-
try {
9-
} catch (Exception ex) {
10-
}
11-
try {
12-
} catch (Error | Exception err) {
13-
}
14-
try {
15-
} catch (Exception exception) {
16-
}
17-
try {
18-
} catch (Exception exception1) {
19-
}
20-
try {
21-
} catch (Exception noWorries) {
22-
}
23-
try {
24-
} catch (Throwable t) {
25-
}
26-
try {
27-
throw new InterruptedException("interruptedException");
28-
} catch (InterruptedException ie) {
29-
}
30-
try {
31-
} catch (Exception ok) {
32-
// appropriate to take no action here
33-
}
34-
try {
35-
} catch (Exception e1) {
36-
try {
37-
} catch (Exception e2) {
38-
}
39-
}
40-
try {
41-
} catch (Throwable t1) {
42-
try {
43-
} catch (Throwable t2) {
44-
}
45-
}
46-
try {
47-
} catch (Exception iException) { // warn
48-
}
49-
try {
50-
} catch (Exception ex_1) { // warn
51-
}
52-
try {
53-
} catch (Exception eX) { // warn
54-
}
55-
try {
56-
} catch (Exception eXX) { // warn
57-
}
58-
try {
59-
} catch (Exception x_y_z) { // warn
60-
}
61-
try {
62-
} catch (Exception Ex) { // warn
63-
}
5+
{
6+
try {
7+
/* foo */
8+
} catch (Exception e) {
9+
/* foo */
6410
}
11+
try {
12+
/* foo */
13+
} catch (Exception ex) {
14+
/* foo */
15+
}
16+
try {
17+
/* foo */
18+
} catch (Error | Exception err) {
19+
/* foo */
20+
}
21+
try {
22+
/* foo */
23+
} catch (Exception exception) {
24+
/* foo */
25+
}
26+
try {
27+
/* foo */
28+
} catch (Exception exception1) {
29+
/* foo */
30+
}
31+
try {
32+
/* foo */
33+
} catch (Exception noWorries) {
34+
/* foo */
35+
}
36+
try {
37+
/* foo */
38+
} catch (Throwable t) {
39+
/* foo */
40+
}
41+
try {
42+
throw new InterruptedException("interruptedException");
43+
} catch (InterruptedException ie) {
44+
/* foo */
45+
}
46+
try {
47+
/* foo */
48+
} catch (Exception ok) {
49+
// appropriate to take no action here
50+
}
51+
try {
52+
/* foo */
53+
} catch (Exception e1) {
54+
try {
55+
/* foo */
56+
} catch (Exception e2) {
57+
/* foo */
58+
}
59+
}
60+
try {
61+
/* foo */
62+
} catch (Throwable t1) {
63+
try {
64+
/* foo */
65+
} catch (Throwable t2) {
66+
/* foo */
67+
}
68+
}
69+
try {
70+
/* foo */
71+
} catch (Exception iException) {
72+
// violation above 'Catch parameter name 'iException' must match pattern'
73+
}
74+
try {
75+
/* foo */
76+
} catch (Exception ex_1) { // violation 'Catch parameter name 'ex_1' must match pattern'
77+
}
78+
try {
79+
/* foo */
80+
} catch (Exception eX) { // violation 'Catch parameter name 'eX' must match pattern'
81+
}
82+
try {
83+
/* foo */
84+
} catch (Exception eXX) {
85+
// 2 violations above:
86+
// 'Abbreviation in name 'eXX' must contain no more than '1' consecutive capital letters.'
87+
// 'Catch parameter name 'eXX' must match pattern'
88+
/* foo */
89+
}
90+
try {
91+
/* foo */
92+
} catch (Exception x_y_z) { // violation 'Catch parameter name 'x_y_z' must match pattern'
93+
}
94+
try {
95+
/* foo */
96+
} catch (Exception Ex) { // violation 'Catch parameter name 'Ex' must match pattern'
97+
}
98+
try {
99+
/* foo */
100+
} catch (Exception name_) { // violation 'Catch parameter name 'name_' must match pattern'
101+
}
102+
try {
103+
/* foo */
104+
} catch (Exception mName) { // violation 'Catch parameter name 'mName' must match pattern'
105+
}
106+
try {
107+
/* foo */
108+
} catch (Exception s_name) { // violation 'Catch parameter name 's_name' must match pattern'
109+
}
110+
try {
111+
/* foo */
112+
} catch (Exception kName) { // violation 'Catch parameter name 'kName' must match pattern'
113+
}
114+
}
65115
}

‎src/xdocs/google_style.xml

+15-1
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,21 @@
16081608
<a href="styleguides/google-java-style-20220203/javaguide.html#s5.1-identifier-names">
16091609
5.1 Rules common to all identifiers</a>
16101610
</td>
1611-
<td>"5.2 Rules of identifier type" already includes this rule.
1611+
<td>
1612+
<span class="wrapper inline">
1613+
<img src="images/ok_green.png" alt="" />
1614+
</span>
1615+
<a href="checks/naming/catchparametername.html#CatchParameterName">CatchParameterName</a>
1616+
<br/>
1617+
<br/>
1618+
All other types of identifiers are covered in special sections below
1619+
</td>
1620+
<td>
1621+
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%20path%3A**%2Fgoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+CatchParameterName">
1622+
config</a>
1623+
<br />
1624+
<a href="https://github.com/checkstyle/checkstyle/blob/master/src/it/java/com/google/checkstyle/test/chapter5naming/rule51identifiernames/RulesCommonToAllIdentifiersTest.java">
1625+
test</a>
16121626
</td>
16131627
<td/>
16141628
</tr>

0 commit comments

Comments
 (0)
Please sign in to comment.