From a8f065738de6c4ff917a2ee57521f6cf8718f2ba Mon Sep 17 00:00:00 2001 From: rnveach Date: Fri, 26 Aug 2022 00:11:39 -0400 Subject: [PATCH] Issue #12101: swap all non-violation messages to localized message --- .../checker-framework-index-suppressions.xml | 18 --- ...llness-optional-interning-suppressions.xml | 118 +----------------- .../pitest-misc-suppressions.xml | 9 -- .../pitest-tree-walker-suppressions.xml | 9 -- config/import-control.xml | 3 + config/pmd.xml | 4 +- .../DetailNodeTreeStringPrinter.java | 12 +- .../com/puppycrawl/tools/checkstyle/Main.java | 17 ++- .../checkstyle/PackageObjectFactory.java | 17 ++- .../tools/checkstyle/api/Violation.java | 21 +--- .../checkstyle/checks/TranslationCheck.java | 9 +- .../DetailNodeTreeStringPrinterTest.java | 37 +++--- .../puppycrawl/tools/checkstyle/MainTest.java | 63 +++++----- .../checkstyle/PackageObjectFactoryTest.java | 32 ++--- .../checks/TranslationCheckTest.java | 25 ++-- 15 files changed, 113 insertions(+), 281 deletions(-) diff --git a/.ci/checker-framework-suppressions/checker-framework-index-suppressions.xml b/.ci/checker-framework-suppressions/checker-framework-index-suppressions.xml index c736518aa0a..2c445879c0d 100644 --- a/.ci/checker-framework-suppressions/checker-framework-index-suppressions.xml +++ b/.ci/checker-framework-suppressions/checker-framework-index-suppressions.xml @@ -662,24 +662,6 @@ - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java - array.initializer - incompatible types in array initializer. - WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); -
- found : @SameLen({"code", "iter#num0.next()"}) String - required: @SameLen({"code", "[error for expression: iter#num0.next(); error: Invalid 'iter#num0.next()' because the expression did not parse. Error message: Lexical error at line 1, column 5. Encountered: "#" (35), after : ""]"}) Object -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java - expression.unparsable - Expression invalid in dependent type annotation: [error for expression: iter#num0.next(); error: Invalid 'iter#num0.next()' because the expression did not parse. Error message: Lexical error at line 1, column 5. Encountered: "#" (35), after : ""] - WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/annotation/MissingDeprecatedCheck.java argument diff --git a/.ci/checker-framework-suppressions/checker-framework-nullness-optional-interning-suppressions.xml b/.ci/checker-framework-suppressions/checker-framework-nullness-optional-interning-suppressions.xml index 6a576816422..794e1211e3e 100644 --- a/.ci/checker-framework-suppressions/checker-framework-nullness-optional-interning-suppressions.xml +++ b/.ci/checker-framework-suppressions/checker-framework-nullness-optional-interning-suppressions.xml @@ -288,17 +288,6 @@ - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java - argument - incompatible argument for parameter customMessage of Violation. - null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/JavaAstVisitor.java argument @@ -489,28 +478,6 @@ - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java - argument - incompatible argument for parameter customMessage of Violation. - new String[] {String.valueOf(errorCounter)}, null, Main.class, null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java - argument - incompatible argument for parameter customMessage of Violation. - new String[] {file.getAbsolutePath()}, null, Main.class, null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java argument @@ -522,28 +489,6 @@ - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java - argument - incompatible argument for parameter moduleId of Violation. - new String[] {String.valueOf(errorCounter)}, null, Main.class, null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java - argument - incompatible argument for parameter moduleId of Violation. - new String[] {file.getAbsolutePath()}, null, Main.class, null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java dereference.of.nullable @@ -653,55 +598,11 @@ checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java argument - incompatible argument for parameter args of Violation. - new String[] {name, attemptedNames}, null, getClass(), null); -
- found : @Initialized @Nullable String @Initialized @NonNull [] - required: @Initialized @NonNull Object @Initialized @NonNull [] -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java - argument - incompatible argument for parameter customMessage of Violation. - new String[] {name, attemptedNames}, null, getClass(), null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java - argument - incompatible argument for parameter customMessage of Violation. - new String[] {name, optionalNames}, null, getClass(), null); + incompatible argument for parameter args of LocalizedMessage. + UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, attemptedNames);
- found : null (NullType) - required: @Initialized @NonNull String -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java - argument - incompatible argument for parameter moduleId of Violation. - new String[] {name, attemptedNames}, null, getClass(), null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- - - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java - argument - incompatible argument for parameter moduleId of Violation. - new String[] {name, optionalNames}, null, getClass(), null); -
- found : null (NullType) - required: @Initialized @NonNull String + found : @Initialized @Nullable String + required: @Initialized @NonNull Object
@@ -1442,17 +1343,6 @@
- - checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java - argument - incompatible argument for parameter customMessage of Violation. - WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); -
- found : null (NullType) - required: @Initialized @NonNull String -
-
- checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java argument diff --git a/.ci/pitest-suppressions/pitest-misc-suppressions.xml b/.ci/pitest-suppressions/pitest-misc-suppressions.xml index 45f5037101b..cdc3e1e5057 100644 --- a/.ci/pitest-suppressions/pitest-misc-suppressions.xml +++ b/.ci/pitest-suppressions/pitest-misc-suppressions.xml @@ -207,15 +207,6 @@ getId(), - - TranslationCheck.java - com.puppycrawl.tools.checkstyle.checks.TranslationCheck - validateUserSpecifiedLanguageCodes - org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator - removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::getId - WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); - - TranslationCheck.java com.puppycrawl.tools.checkstyle.checks.TranslationCheck$ResourceBundle diff --git a/.ci/pitest-suppressions/pitest-tree-walker-suppressions.xml b/.ci/pitest-suppressions/pitest-tree-walker-suppressions.xml index ada17be889e..187867d8dc0 100644 --- a/.ci/pitest-suppressions/pitest-tree-walker-suppressions.xml +++ b/.ci/pitest-suppressions/pitest-tree-walker-suppressions.xml @@ -1,14 +1,5 @@ - - DetailNodeTreeStringPrinter.java - com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinter - getParseErrorMessage - org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator - removed call to com/puppycrawl/tools/checkstyle/JavadocDetailNodeParser$ParseErrorMessage::getLineNumber - parseErrorMessage.getLineNumber(), - - DetailNodeTreeStringPrinter.java com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinter diff --git a/config/import-control.xml b/config/import-control.xml index a4a993bea4c..7ed99188ebd 100644 --- a/config/import-control.xml +++ b/config/import-control.xml @@ -136,6 +136,9 @@ + + + diff --git a/config/pmd.xml b/config/pmd.xml index f0ec15688e2..395cb5df28f 100644 --- a/config/pmd.xml +++ b/config/pmd.xml @@ -336,10 +336,12 @@ + or @SimpleName='Checker' or @SimpleName='CheckstyleAntTask' + or @SimpleName='TranslationCheck']"/> diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java b/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java index e365d0147f1..de390a42cd4 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java @@ -29,7 +29,6 @@ import com.puppycrawl.tools.checkstyle.api.DetailNode; import com.puppycrawl.tools.checkstyle.api.FileText; import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; -import com.puppycrawl.tools.checkstyle.api.Violation; import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; import com.puppycrawl.tools.checkstyle.utils.ParserUtil; @@ -91,15 +90,12 @@ private static DetailNode parseJavadocAsDetailNode(String javadocComment) { * @return error violation */ private static String getParseErrorMessage(ParseErrorMessage parseErrorMessage) { - final Violation lmessage = new Violation( - parseErrorMessage.getLineNumber(), + final LocalizedMessage message = new LocalizedMessage( "com.puppycrawl.tools.checkstyle.checks.javadoc.messages", - parseErrorMessage.getMessageKey(), - parseErrorMessage.getMessageArguments(), - "", DetailNodeTreeStringPrinter.class, - null); - return "[ERROR:" + parseErrorMessage.getLineNumber() + "] " + lmessage.getViolation(); + parseErrorMessage.getMessageKey(), + parseErrorMessage.getMessageArguments()); + return "[ERROR:" + parseErrorMessage.getLineNumber() + "] " + message.getMessage(); } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/Main.java b/src/main/java/com/puppycrawl/tools/checkstyle/Main.java index 03f815c9f22..d8e7a9d02c6 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/Main.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/Main.java @@ -48,7 +48,6 @@ import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; import com.puppycrawl.tools.checkstyle.api.RootModule; -import com.puppycrawl.tools.checkstyle.api.Violation; import com.puppycrawl.tools.checkstyle.utils.ChainedPropertyUtil; import com.puppycrawl.tools.checkstyle.utils.CommonUtil; import com.puppycrawl.tools.checkstyle.utils.XpathUtil; @@ -146,12 +145,12 @@ else if (parseResult.isUsageHelpRequested()) { finally { // return exit code base on validation of Checker if (errorCounter > 0) { - final Violation errorCounterViolation = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, ERROR_COUNTER, - new String[] {String.valueOf(errorCounter)}, null, Main.class, null); + final LocalizedMessage errorCounterViolation = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, Main.class, + ERROR_COUNTER, String.valueOf(errorCounter)); // print error count statistic to error output stream, // output stream might be used by validation report content - System.err.println(errorCounterViolation.getViolation()); + System.err.println(errorCounterViolation.getMessage()); } } Runtime.getRuntime().exit(exitStatus); @@ -439,10 +438,10 @@ private static Properties loadProperties(File file) properties.load(stream); } catch (final IOException ex) { - final Violation loadPropertiesExceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, LOAD_PROPERTIES_EXCEPTION, - new String[] {file.getAbsolutePath()}, null, Main.class, null); - throw new CheckstyleException(loadPropertiesExceptionMessage.getViolation(), ex); + final LocalizedMessage loadPropertiesExceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, Main.class, + LOAD_PROPERTIES_EXCEPTION, file.getAbsolutePath()); + throw new CheckstyleException(loadPropertiesExceptionMessage.getMessage(), ex); } return ChainedPropertyUtil.getResolvedProperties(properties); diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java b/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java index 568abdb44ab..bec9669bf3f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java @@ -31,7 +31,6 @@ import java.util.stream.Stream; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import com.puppycrawl.tools.checkstyle.api.Violation; import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil; /** @@ -209,10 +208,10 @@ public Object createModule(String name) throws CheckstyleException { + STRING_SEPARATOR + nameCheck + STRING_SEPARATOR + joinPackageNamesWithClassName(nameCheck, packages); } - final Violation exceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, - new String[] {name, attemptedNames}, null, getClass(), null); - throw new CheckstyleException(exceptionMessage.getViolation()); + final LocalizedMessage exceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, getClass(), + UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, attemptedNames); + throw new CheckstyleException(exceptionMessage.getMessage()); } return instance; } @@ -285,10 +284,10 @@ private Object createObjectFromFullModuleNames(String name, Set fullModu final String optionalNames = fullModuleNames.stream() .sorted() .collect(Collectors.joining(STRING_SEPARATOR)); - final Violation exceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, - new String[] {name, optionalNames}, null, getClass(), null); - throw new CheckstyleException(exceptionMessage.getViolation()); + final LocalizedMessage exceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, getClass(), + AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, name, optionalNames); + throw new CheckstyleException(exceptionMessage.getMessage()); } return returnValue; } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java index ff813166433..3fb0e8e9c7d 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java @@ -431,26 +431,15 @@ else if (other.moduleId == null) { * @return the translated violation */ public String getViolation() { - String violation = getCustomViolation(); + final String violation; - if (violation == null) { + if (customMessage != null) { + violation = new MessageFormat(customMessage, Locale.ROOT).format(args); + } + else { violation = new LocalizedMessage(bundle, sourceClass, key, args).getMessage(); } - return violation; - } - /** - * Returns the formatted custom violation if one is configured. - * - * @return the formatted custom violation or {@code null} - * if there is no custom violation - */ - private String getCustomViolation() { - String violation = null; - if (customMessage != null) { - final MessageFormat formatter = new MessageFormat(customMessage, Locale.ROOT); - violation = formatter.format(args); - } return violation; } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java index e622925189d..c10c588aa2e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java @@ -45,6 +45,7 @@ import com.puppycrawl.tools.checkstyle.Definitions; import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck; +import com.puppycrawl.tools.checkstyle.LocalizedMessage; import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; import com.puppycrawl.tools.checkstyle.api.FileText; import com.puppycrawl.tools.checkstyle.api.MessageDispatcher; @@ -318,11 +319,9 @@ public void setRequiredTranslations(String... translationCodes) { private void validateUserSpecifiedLanguageCodes(Set languageCodes) { for (String code : languageCodes) { if (!isValidLanguageCode(code)) { - final Violation msg = new Violation(1, TRANSLATION_BUNDLE, - WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); - final String exceptionMessage = String.format(Locale.ROOT, - "%s [%s]", msg.getViolation(), TranslationCheck.class.getSimpleName()); - throw new IllegalArgumentException(exceptionMessage); + final LocalizedMessage msg = new LocalizedMessage(TRANSLATION_BUNDLE, + getClass(), WRONG_LANGUAGE_CODE_KEY, code); + throw new IllegalArgumentException(msg.getMessage()); } } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java index 047f423f588..88b5163b988 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java @@ -30,7 +30,6 @@ import org.junit.jupiter.api.Test; import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage; -import com.puppycrawl.tools.checkstyle.api.Violation; import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil; public class DetailNodeTreeStringPrinterTest extends AbstractTreeTestSupport { @@ -83,15 +82,13 @@ public void testMissedHtmlTagParseErrorMessage() throws Exception { final String actual = TestUtil.invokeStaticMethod(DetailNodeTreeStringPrinter.class, "getParseErrorMessage", new ParseErrorMessage(35, MSG_JAVADOC_MISSED_HTML_CLOSE, 7, "xyz")); - final Violation violation = new Violation( - 35, + final LocalizedMessage violation = new LocalizedMessage( "com.puppycrawl.tools.checkstyle.checks.javadoc.messages", - MSG_JAVADOC_MISSED_HTML_CLOSE, - new Object[] {7, "xyz"}, - "", DetailNodeTreeStringPrinter.class, - null); - final String expected = "[ERROR:35] " + violation.getViolation(); + MSG_JAVADOC_MISSED_HTML_CLOSE, + 7, + "xyz"); + final String expected = "[ERROR:35] " + violation.getMessage(); assertWithMessage("Javadoc parse error violation for missed HTML tag " + "doesn't meet expectations") .that(actual) @@ -104,15 +101,13 @@ public void testParseErrorMessage() throws Exception { "getParseErrorMessage", new ParseErrorMessage(10, MSG_JAVADOC_PARSE_RULE_ERROR, 9, "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT")); - final Violation violation = new Violation( - 10, + final LocalizedMessage violation = new LocalizedMessage( "com.puppycrawl.tools.checkstyle.checks.javadoc.messages", - MSG_JAVADOC_PARSE_RULE_ERROR, - new Object[] {9, "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT"}, - "", DetailNodeTreeStringPrinter.class, - null); - final String expected = "[ERROR:10] " + violation.getViolation(); + MSG_JAVADOC_PARSE_RULE_ERROR, + 9, + "no viable alternative at input ' xyz'", "SOME_JAVADOC_ELEMENT"); + final String expected = "[ERROR:10] " + violation.getMessage(); assertWithMessage("Javadoc parse error violation doesn't meet expectations") .that(actual) .isEqualTo(expected); @@ -124,15 +119,13 @@ public void testWrongSingletonParseErrorMessage() throws Exception { "getParseErrorMessage", new ParseErrorMessage(100, MSG_JAVADOC_WRONG_SINGLETON_TAG, 9, "tag")); - final Violation violation = new Violation( - 100, + final LocalizedMessage violation = new LocalizedMessage( "com.puppycrawl.tools.checkstyle.checks.javadoc.messages", - MSG_JAVADOC_WRONG_SINGLETON_TAG, - new Object[] {9, "tag"}, - "", DetailNodeTreeStringPrinter.class, - null); - final String expected = "[ERROR:100] " + violation.getViolation(); + MSG_JAVADOC_WRONG_SINGLETON_TAG, + 9, + "tag"); + final String expected = "[ERROR:100] " + violation.getMessage(); assertWithMessage("Javadoc parse error violation for void elements with close tag " + "doesn't meet expectations") .that(actual) diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java index ed65b9eacb4..553d14262b0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java @@ -186,16 +186,16 @@ public class MainTest { @TempDir public File temporaryFolder; - private final Violation auditStartMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditStarted", null, null, - getClass(), null); + private final LocalizedMessage auditStartMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, getClass(), + "DefaultLogger.auditStarted"); - private final Violation auditFinishMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, "DefaultLogger.auditFinished", null, null, - getClass(), null); + private final LocalizedMessage auditFinishMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, getClass(), + "DefaultLogger.auditFinished"); - private final String noViolationsOutput = auditStartMessage.getViolation() + EOL - + auditFinishMessage.getViolation() + EOL; + private final String noViolationsOutput = auditStartMessage.getMessage() + EOL + + auditFinishMessage.getMessage() + EOL; private static String getPath(String filename) { return "src/test/resources/com/puppycrawl/tools/checkstyle/main/" + filename; @@ -388,8 +388,8 @@ public void testExistingTargetFile(@SysErr Capturable systemErr, @SysOut Captura getPath("InputMain.java")); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), - auditFinishMessage.getViolation())); + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(""); @@ -470,8 +470,8 @@ public void testExistingTargetFilePlainOutput(@SysErr Capturable systemErr, getPath("InputMain.java")); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), - auditFinishMessage.getViolation())); + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(""); @@ -493,14 +493,14 @@ public void testExistingTargetFileWithViolations(@SysErr Capturable systemErr, final String expectedPath = getFilePath("InputMain.java"); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), "[WARN] " + expectedPath + ":3:14: " + invalidPatternMessageMain.getViolation() + " [TypeName]", "[WARN] " + expectedPath + ":5:7: " + invalidPatternMessageMainInner.getViolation() + " [TypeName]", - auditFinishMessage.getViolation())); + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(""); @@ -555,12 +555,12 @@ public void testExistingTargetFileWithError(@SysErr Capturable systemErr, final String expectedPath = getFilePath("InputMain.java"); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), "[ERROR] " + expectedPath + ":3:14: " + invalidPatternMessageMain.getViolation() + " [TypeName]", "[ERROR] " + expectedPath + ":5:7: " + invalidPatternMessageMainInner.getViolation() + " [TypeName]", - auditFinishMessage.getViolation())); + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(addEndOfLine(errorCounterTwoMessage.getViolation())); @@ -588,10 +588,10 @@ public void testExistingTargetFileWithOneError(@SysErr Capturable systemErr, final String expectedPath = getFilePath("InputMain1.java"); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), "[ERROR] " + expectedPath + ":3:14: " + invalidPatternMessageMain.getViolation() + " [TypeName]", - auditFinishMessage.getViolation())); + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(addEndOfLine(errorCounterTwoMessage.getViolation())); @@ -611,9 +611,9 @@ public void testExistingTargetFileWithOneErrorAgainstSunCheck(@SysErr Capturable final String expectedPath = getFilePath("InputMain1.java"); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), "[ERROR] " + expectedPath + ":1: " + message.getViolation() + " [JavadocPackage]", - auditFinishMessage.getViolation())); + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(addEndOfLine(errorCounterTwoMessage.getViolation())); @@ -670,8 +670,8 @@ public void testExistingTargetFilePlainOutputProperties(@SysErr Capturable syste "-p", getPath("InputMainMycheckstyle.properties"), getPath("InputMain.java")); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), - auditFinishMessage.getViolation())); + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(""); @@ -685,8 +685,8 @@ public void testPropertyFileWithPropertyChaining(@SysErr Capturable systemErr, assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) - .isEqualTo(addEndOfLine(auditStartMessage.getViolation(), - auditFinishMessage.getViolation())); + .isEqualTo(addEndOfLine(auditStartMessage.getMessage(), + auditFinishMessage.getMessage())); assertWithMessage("Unexpected system error log") .that(systemErr.getCapturedData()) .isEqualTo(""); @@ -813,7 +813,7 @@ public void testExistingDirectoryWithViolations(@SysErr Capturable systemErr, getPath("")); final String expectedPath = getFilePath("") + File.separator; final StringBuilder sb = new StringBuilder(28); - sb.append(auditStartMessage.getViolation()) + sb.append(auditStartMessage.getMessage()) .append(EOL); final String format = "[WARN] " + expectedPath + outputValues[0][0] + ".java:" + outputValues[0][1] + ": "; @@ -824,7 +824,7 @@ public void testExistingDirectoryWithViolations(@SysErr Capturable systemErr, final String line = format + violation + " [FileLength]"; sb.append(line).append(EOL); } - sb.append(auditFinishMessage.getViolation()) + sb.append(auditFinishMessage.getMessage()) .append(EOL); assertWithMessage("Unexpected output log") .that(systemOut.getCapturedData()) @@ -1740,19 +1740,20 @@ public void testCustomSimpleRootModule(@SysErr Capturable systemErr) { assertMainReturnCode(-2, "-c", getPath("InputMainConfig-custom-simple-root-module.xml"), getPath("InputMain.java")); final String checkstylePackage = "com.puppycrawl.tools.checkstyle."; - final Violation unableToInstantiateExceptionMessage = new Violation(1, + final LocalizedMessage unableToInstantiateExceptionMessage = new LocalizedMessage( Definitions.CHECKSTYLE_BUNDLE, + getClass(), "PackageObjectFactory.unableToInstantiateExceptionMessage", - new String[] {"TestRootModuleChecker", checkstylePackage + "TestRootModuleChecker", + checkstylePackage + "TestRootModuleChecker, " + "TestRootModuleCheckerCheck, " + checkstylePackage - + "TestRootModuleCheckerCheck"}, - null, getClass(), null); + + "TestRootModuleCheckerCheck"); assertWithMessage( "Unexpected system error log") .that(systemErr.getCapturedData() .startsWith(checkstylePackage + "api.CheckstyleException: " - + unableToInstantiateExceptionMessage.getViolation())) + + unableToInstantiateExceptionMessage.getMessage())) .isTrue(); assertWithMessage("Invalid checker state") .that(TestRootModuleChecker.isProcessed()) diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java index 8679c3a4f7f..8bfe18c23e5 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java @@ -156,12 +156,12 @@ public void testMakeCheckFromName() { assertWithMessage("Exception is expected").fail(); } catch (CheckstyleException ex) { - final Violation exceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, - new String[] {name, null}, null, factory.getClass(), null); + final LocalizedMessage exceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, factory.getClass(), + UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, null); assertWithMessage("Invalid exception message") .that(ex.getMessage()) - .isEqualTo(exceptionMessage.getViolation()); + .isEqualTo(exceptionMessage.getMessage()); } } @@ -177,12 +177,12 @@ public void testCreateModuleWithNonExistName() { final String attemptedNames = BASE_PACKAGE + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + name + CHECK_SUFFIX + STRING_SEPARATOR + BASE_PACKAGE + PACKAGE_SEPARATOR + name + CHECK_SUFFIX; - final Violation exceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, - new String[] {name, attemptedNames}, null, factory.getClass(), null); + final LocalizedMessage exceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, factory.getClass(), + UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, attemptedNames); assertWithMessage("Invalid exception message") .that(ex.getMessage()) - .isEqualTo(exceptionMessage.getViolation()); + .isEqualTo(exceptionMessage.getMessage()); } } } @@ -250,12 +250,12 @@ public void testCreateObjectFromFullModuleNamesWithAmbiguousException() { catch (CheckstyleException ex) { final String optionalNames = barPackage + PACKAGE_SEPARATOR + name + STRING_SEPARATOR + fooPackage + PACKAGE_SEPARATOR + name; - final Violation exceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, - new String[] {name, optionalNames}, null, getClass(), null); + final LocalizedMessage exceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, getClass(), + AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, name, optionalNames); assertWithMessage("Invalid exception message") .that(ex.getMessage()) - .isEqualTo(exceptionMessage.getViolation()); + .isEqualTo(exceptionMessage.getMessage()); } } @@ -278,12 +278,12 @@ public void testCreateObjectFromFullModuleNamesWithCantInstantiateException() { + checkName + STRING_SEPARATOR + package1 + PACKAGE_SEPARATOR + checkName + STRING_SEPARATOR + package2 + PACKAGE_SEPARATOR + checkName; - final Violation exceptionMessage = new Violation(1, - Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, - new String[] {name, attemptedNames}, null, getClass(), null); + final LocalizedMessage exceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, getClass(), + UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, attemptedNames); assertWithMessage("Invalid exception message") .that(ex.getMessage()) - .isEqualTo(exceptionMessage.getViolation()); + .isEqualTo(exceptionMessage.getMessage()); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java index 97de7fd5db9..12ed1d79768 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java @@ -231,12 +231,12 @@ public void testLogIoExceptionFileNotFound() throws Exception { assertWithMessage("expected number of errors to fire") .that(dispatcher.savedErrors) .hasSize(1); - final Violation violation = new Violation(1, + final Violation violation = new Violation(0, Definitions.CHECKSTYLE_BUNDLE, "general.fileNotFound", - null, null, getClass(), null); + null, null, TranslationCheck.class, null); assertWithMessage("Invalid violation") - .that(dispatcher.savedErrors.iterator().next().getViolation()) - .isEqualTo(violation.getViolation()); + .that(dispatcher.savedErrors.iterator().next()) + .isEqualTo(violation); } @Test @@ -256,12 +256,12 @@ public void testLogIoException() throws Exception { assertWithMessage("expected number of errors to fire") .that(dispatcher.savedErrors.size()) .isEqualTo(1); - final Violation violation = new Violation(1, + final Violation violation = new Violation(0, Definitions.CHECKSTYLE_BUNDLE, "general.exception", - new String[] {exception.getMessage()}, null, getClass(), null); + new String[] {exception.getMessage()}, null, TranslationCheck.class, null); assertWithMessage("Invalid violation") - .that(dispatcher.savedErrors.iterator().next().getViolation()) - .isEqualTo(violation.getViolation()); + .that(dispatcher.savedErrors.iterator().next()) + .isEqualTo(violation); } @Test @@ -269,9 +269,9 @@ public void testLogIllegalArgumentException() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class); checkConfig.addProperty("baseName", "^bad.*$"); final String[] expected = { - "0: " + new Violation(1, Definitions.CHECKSTYLE_BUNDLE, "general.exception", - new String[] {"Malformed \\uxxxx encoding." }, null, getClass(), - null).getViolation(), "1: " + getCheckMessage(MSG_KEY, "test"), + "0: " + getCheckMessage(Checker.class, "general.exception", + "Malformed \\uxxxx encoding."), + "1: " + getCheckMessage(MSG_KEY, "test"), }; final File[] propertyFiles = { new File(getPath("bad.properties")), @@ -607,9 +607,6 @@ public void testWrongUserSpecifiedLanguageCodes() { assertWithMessage("Error message is unexpected") .that(exceptionMessage) .contains("11"); - assertWithMessage("Error message is unexpected") - .that(exceptionMessage) - .endsWith("[TranslationCheck]"); } }