From 0b98c0389e2cf745cb125a89dece57538fc672b7 Mon Sep 17 00:00:00 2001 From: alexkravin Date: Fri, 20 Feb 2015 17:00:32 +0400 Subject: [PATCH] Refactored UTs, modifier package, issue #537 --- .../checks/modifier/ModifierOrderCheck.java | 17 +++++++++-- .../modifier/RedundantModifierCheck.java | 13 ++++++-- .../modifier/ModifierOrderCheckTest.java | 17 +++++++---- .../modifier/RedundantModifierTest.java | 30 ++++++++++--------- 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheck.java index b69f910cc..ccf31ca92 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheck.java @@ -65,6 +65,19 @@ import java.util.List; public class ModifierOrderCheck extends Check { + + /** + * A key is pointing to the warning message text in "messages.properties" + * file. + */ + public static final String MSG_ANNOTATION_ORDER = "annotation.order"; + + /** + * A key is pointing to the warning message text in "messages.properties" + * file. + */ + public static final String MSG_MODIFIER_ORDER = "mod.order"; + /** * The order of modifiers as suggested in sections 8.1.1, * 8.3.1 and 8.4.3 of the JLS. @@ -102,14 +115,14 @@ public class ModifierOrderCheck if (error != null) { if (error.getType() == TokenTypes.ANNOTATION) { log(error.getLineNo(), error.getColumnNo(), - "annotation.order", + MSG_ANNOTATION_ORDER, error.getFirstChild().getText() + error.getFirstChild().getNextSibling() .getText()); } else { log(error.getLineNo(), error.getColumnNo(), - "mod.order", error.getText()); + MSG_MODIFIER_ORDER, error.getText()); } } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java index ba961cf3e..4990ef36b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java @@ -34,6 +34,13 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; public class RedundantModifierCheck extends Check { + + /** + * A key is pointing to the warning message text in "messages.properties" + * file. + */ + public static final String MSG_KEY = "redundantModifier"; + @Override public int[] getDefaultTokens() { @@ -77,7 +84,7 @@ public class RedundantModifierCheck modifiers.findFirstToken(tokenType); if (null != modifier) { log(modifier.getLineNo(), modifier.getColumnNo(), - "redundantModifier", modifier.getText()); + MSG_KEY, modifier.getText()); } } } @@ -99,7 +106,7 @@ public class RedundantModifierCheck || (type == TokenTypes.FINAL)) { log(modifier.getLineNo(), modifier.getColumnNo(), - "redundantModifier", modifier.getText()); + MSG_KEY, modifier.getText()); break; } @@ -130,7 +137,7 @@ public class RedundantModifierCheck final int type = modifier.getType(); if (type == TokenTypes.FINAL) { log(modifier.getLineNo(), modifier.getColumnNo(), - "redundantModifier", modifier.getText()); + MSG_KEY, modifier.getText()); break; } modifier = modifier.getNextSibling(); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheckTest.java index ecf47ecfc..599a3d454 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/ModifierOrderCheckTest.java @@ -25,6 +25,11 @@ import org.junit.Test; import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import static com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck +.MSG_ANNOTATION_ORDER; +import static com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck +.MSG_MODIFIER_ORDER; + public class ModifierOrderCheckTest extends BaseCheckTestSupport { @@ -34,12 +39,12 @@ public class ModifierOrderCheckTest final DefaultConfiguration checkConfig = createCheckConfig(ModifierOrderCheck.class); final String[] expected = { - "14:10: 'final' modifier out of order with the JLS suggestions.", - "18:12: 'private' modifier out of order with the JLS suggestions.", - "24:14: 'private' modifier out of order with the JLS suggestions.", - "34:13: '@MyAnnotation2' annotation modifier does not preceed non-annotation modifiers.", - "39:13: '@MyAnnotation2' annotation modifier does not preceed non-annotation modifiers.", - "49:35: '@MyAnnotation4' annotation modifier does not preceed non-annotation modifiers.", + "14:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "final"), + "18:12: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"), + "24:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"), + "34:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"), + "39:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"), + "49:35: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation4"), }; verify(checkConfig, getPath("InputModifier.java"), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java index 6dd72ff26..56d4e3dbf 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierTest.java @@ -25,6 +25,8 @@ import org.junit.Test; import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import static com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck.MSG_KEY; + public class RedundantModifierTest extends BaseCheckTestSupport { @@ -34,20 +36,20 @@ public class RedundantModifierTest final DefaultConfiguration checkConfig = createCheckConfig(RedundantModifierCheck.class); final String[] expected = { - "54:12: Redundant 'static' modifier.", - "57:9: Redundant 'public' modifier.", - "63:9: Redundant 'abstract' modifier.", - "66:9: Redundant 'public' modifier.", + "54:12: " + getCheckMessage(MSG_KEY, "static"), + "57:9: " + getCheckMessage(MSG_KEY, "public"), + "63:9: " + getCheckMessage(MSG_KEY, "abstract"), + "66:9: " + getCheckMessage(MSG_KEY, "public"), //"69:9: Redundant 'abstract' modifier.", - "72:9: Redundant 'final' modifier.", - "79:13: Redundant 'final' modifier.", - "88:12: Redundant 'final' modifier.", - "99:1: Redundant 'abstract' modifier.", - "116:5: Redundant 'public' modifier.", - "117:5: Redundant 'final' modifier.", - "118:5: Redundant 'static' modifier.", - "120:5: Redundant 'public' modifier.", - "121:5: Redundant 'abstract' modifier.", + "72:9: " + getCheckMessage(MSG_KEY, "final"), + "79:13: " + getCheckMessage(MSG_KEY, "final"), + "88:12: " + getCheckMessage(MSG_KEY, "final"), + "99:1: " + getCheckMessage(MSG_KEY, "abstract"), + "116:5: " + getCheckMessage(MSG_KEY, "public"), + "117:5: " + getCheckMessage(MSG_KEY, "final"), + "118:5: " + getCheckMessage(MSG_KEY, "static"), + "120:5: " + getCheckMessage(MSG_KEY, "public"), + "121:5: " + getCheckMessage(MSG_KEY, "abstract"), }; verify(checkConfig, getPath("InputModifier.java"), expected); } @@ -73,7 +75,7 @@ public class RedundantModifierTest final DefaultConfiguration checkConfig = createCheckConfig(RedundantModifierCheck.class); final String[] expected = { - "3:9: Redundant 'final' modifier.", + "3:9: " + getCheckMessage(MSG_KEY, "final"), }; verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/"