Skip to content

Commit

Permalink
minor: restructures message testing to clarify messages come from
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed Sep 25, 2021
1 parent 90d13b9 commit 0b7cfd0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,14 +528,17 @@ public void testAllCheckstyleModulesInCheckstyleConfig() throws Exception {
public void testAllCheckstyleChecksHaveMessage() throws Exception {
for (Class<?> module : CheckUtil.getCheckstyleChecks()) {
final String name = module.getSimpleName();
final Set<Field> messages = CheckUtil.getCheckMessages(module, false);

// No messages in just module
if ("SuppressWarningsHolder".equals(name)) {
continue;
if ("SuppressWarningsHolder".equals(name) || "JavadocMetadataScraper".equals(name)) {
assertTrue(messages.isEmpty(),
name + " should not have any 'MSG_*' fields for error messages");
}
else {
assertFalse(messages.isEmpty(),
name + " should have at least one 'MSG_*' field for error messages");
}

assertFalse(CheckUtil.getCheckMessages(module).isEmpty(),
name + " should have at least one 'MSG_*' field for error messages");
}
}

Expand All @@ -545,7 +548,7 @@ public void testAllCheckstyleMessages() throws Exception {

// test validity of messages from modules
for (Class<?> module : CheckUtil.getCheckstyleModules()) {
for (Field message : CheckUtil.getCheckMessages(module)) {
for (Field message : CheckUtil.getCheckMessages(module, true)) {
assertEquals(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL,
message.getModifiers(),
module.getSimpleName() + "." + message.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ private static void validateViolationSection(String fileName, String sectionName
Node subSection,
Object instance) throws Exception {
final Class<?> clss = instance.getClass();
final Set<Field> fields = CheckUtil.getCheckMessages(clss);
final Set<Field> fields = CheckUtil.getCheckMessages(clss, true);
final Set<String> list = new TreeSet<>();

for (Field field : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@

import com.google.common.reflect.ClassPath;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.checks.coding.AbstractSuperCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.AbstractAccessControlNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck;
import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck;
import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck;
import com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
Expand Down Expand Up @@ -198,10 +202,13 @@ private static boolean isFromAllowedPackages(Class<?> cls) {
* Get's the check's messages.
*
* @param module class to examine.
* @param deepScan scan subclasses.
* @return a set of checkstyle's module message fields.
* @throws ClassNotFoundException if the attempt to read a protected class fails.
* @noinspection BooleanParameter Test code for easy usage.
*/
public static Set<Field> getCheckMessages(Class<?> module) throws ClassNotFoundException {
public static Set<Field> getCheckMessages(Class<?> module, boolean deepScan)
throws ClassNotFoundException {
final Set<Field> checkstyleMessages = new HashSet<>();

// get all fields from current class
Expand All @@ -216,24 +223,39 @@ public static Set<Field> getCheckMessages(Class<?> module) throws ClassNotFoundE
// deep scan class through hierarchy
final Class<?> superModule = module.getSuperclass();

if (superModule != null) {
checkstyleMessages.addAll(getCheckMessages(superModule));
if (superModule != null && (deepScan || shouldScanDeepClassForMessages(superModule))) {
checkstyleMessages.addAll(getCheckMessages(superModule, deepScan));
}

// special cases that require additional classes
if (module == RegexpMultilineCheck.class) {
checkstyleMessages.addAll(getCheckMessages(Class
.forName("com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector")));
.forName("com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector"),
deepScan));
}
else if (module == RegexpSinglelineCheck.class
|| module == RegexpSinglelineJavaCheck.class) {
checkstyleMessages.addAll(getCheckMessages(Class
.forName("com.puppycrawl.tools.checkstyle.checks.regexp.SinglelineDetector")));
.forName("com.puppycrawl.tools.checkstyle.checks.regexp.SinglelineDetector"),
deepScan));
}

return checkstyleMessages;
}

/**
* Should the class be deep scanned for messages.
*
* @param superModule The class to examine.
* @return {@code true} if the class should be deep scanned.
*/
private static boolean shouldScanDeepClassForMessages(Class<?> superModule) {
return superModule == AbstractNameCheck.class
|| superModule == AbstractAccessControlNameCheck.class
|| superModule == AbstractParenPadCheck.class
|| superModule == AbstractSuperCheck.class;
}

/**
* Gets the check message 'as is' from appropriate 'messages.properties'
* file.
Expand Down

0 comments on commit 0b7cfd0

Please sign in to comment.