diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java index 5792a62f579..db2d1663440 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java @@ -361,7 +361,7 @@ public String getSourceName() { * @param locale the locale to use for localization */ public static void setLocale(Locale locale) { - BUNDLE_CACHE.clear(); + clearCache(); if (Locale.ENGLISH.getLanguage().equals(locale.getLanguage())) { sLocale = Locale.ROOT; } @@ -392,12 +392,12 @@ public int compareTo(LocalizedMessage other) { /** *

* Custom ResourceBundle.Control implementation which allows explicitly read - * the properties files as UTF-8 + * the properties files as UTF-8. *

* * @author Aleksey Nesterenko */ - protected static class Utf8Control extends Control { + public static class Utf8Control extends Control { @Override public ResourceBundle newBundle(String aBaseName, Locale aLocale, String aFormat, ClassLoader aLoader, boolean aReload) throws IOException { diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/indentation/messages_zh.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/indentation/messages_zh.properties index 8bc1d707984..4782934e69f 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/indentation/messages_zh.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/indentation/messages_zh.properties @@ -1,5 +1,5 @@ indentation.error.multi=''{0}'' 缩进了{1}个缩进符,应为以下缩进之一:{2}。 -indentation.child.error.multi=''{0}'' 子元素进了{1}个缩进符,应为以下缩进之一:{2}。 +indentation.child.error.multi=''{0}'' 子元素缩进了{1}个缩进符,应为以下缩进之一:{2}。 indentation.error=''{0}'' 缩进了{1}个缩进符,应为{2}个。 indentation.child.error=''{0}'' 子元素缩进了{1}个缩进符,应为{2}个。 comments.indentation.single=注释应与第{0}行代码同样缩进{2}个缩进符,而不是{1}个。 diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/BaseCheckTestSupport.java b/src/test/java/com/puppycrawl/tools/checkstyle/BaseCheckTestSupport.java index 19c95f801c0..80dbb2317d0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/BaseCheckTestSupport.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/BaseCheckTestSupport.java @@ -39,13 +39,14 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Properties; +import java.util.ResourceBundle; import java.util.stream.Collectors; import com.google.common.collect.MapDifference; import com.google.common.collect.MapDifference.ValueDifference; import com.google.common.collect.Maps; import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; public class BaseCheckTestSupport { protected final ByteArrayOutputStream stream = new ByteArrayOutputStream(); @@ -64,11 +65,6 @@ public Checker createChecker(Configuration checkConfig) throws Exception { final DefaultConfiguration dc = createCheckerConfig(checkConfig); final Checker checker = new Checker(); - // make sure the tests always run with default error messages (language-invariant) - // so the tests don't fail in supported locales like German - final Locale locale = Locale.ROOT; - checker.setLocaleCountry(locale.getCountry()); - checker.setLocaleLanguage(locale.getLanguage()); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(dc); checker.addListener(new BriefUtLogger(stream)); @@ -362,15 +358,54 @@ private Map> getActualViolations(int errorCount) throws IOE * @param arguments the arguments of message in 'messages.properties' file. */ protected String getCheckMessage(String messageKey, Object... arguments) { - final Properties pr = new Properties(); - try { - pr.load(getClass().getResourceAsStream("messages.properties")); - } - catch (IOException ex) { - return null; - } - final MessageFormat formatter = new MessageFormat(pr.getProperty(messageKey), - Locale.ROOT); + return internalGetCheckMessage(getMessageBundle(), messageKey, arguments); + } + + /** + * Gets the check message 'as is' from appropriate 'messages.properties' + * file. + * + * @param clazz the related check class. + * @param messageKey the key of message in 'messages.properties' file. + * @param arguments the arguments of message in 'messages.properties' file. + */ + protected String getCheckMessage( + Class clazz, String messageKey, Object... arguments) { + return internalGetCheckMessage(getMessageBundle(clazz.getName()), messageKey, arguments); + } + + /** + * Gets the check message 'as is' from appropriate 'messages.properties' + * file. + * + * @param messageBundle the bundle name. + * @param messageKey the key of message in 'messages.properties' file. + * @param arguments the arguments of message in 'messages.properties' file. + */ + protected String internalGetCheckMessage( + String messageBundle, String messageKey, Object... arguments) { + final ResourceBundle resourceBundle = ResourceBundle.getBundle( + messageBundle, + Locale.getDefault(), + Thread.currentThread().getContextClassLoader(), + new LocalizedMessage.Utf8Control()); + final String pattern = resourceBundle.getString(messageKey); + final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT); return formatter.format(arguments); } + + private String getMessageBundle() { + final String className = getClass().getName(); + return getMessageBundle(className); + } + + private static String getMessageBundle(String className) { + final int endIndex = className.lastIndexOf('.'); + final String messages = "messages"; + if (endIndex < 0) { + return messages; + } + final String packageName = className.substring(0, endIndex); + return packageName + "." + messages; + } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java index 2c091331a27..87c1bef7a91 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -767,9 +767,6 @@ public void testHaltOnExceptionOff() throws Exception { checkerConfig.addAttribute("haltOnException", "false"); final Checker checker = new Checker(); - final Locale locale = Locale.ROOT; - checker.setLocaleCountry(locale.getCountry()); - checker.setLocaleLanguage(locale.getLanguage()); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(new BriefUtLogger(stream)); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java index 712f8259cfb..9490d1c7f29 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java @@ -19,6 +19,7 @@ package com.puppycrawl.tools.checkstyle; +import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -62,7 +63,8 @@ public void testProperFileExtension() throws Exception { writer.write(content); } final String[] expected1 = { - "1:45: Name 'k' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.", + "1:45: " + getCheckMessage(ConstantNameCheck.class, + MSG_INVALID_PATTERN, "k", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"), }; verify(checkConfig, file.getPath(), expected1); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java index 95f1f6ff4f4..48aa653c351 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks; import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_NO_NEWLINE_EOF; +import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_UNABLE_OPEN; import static java.util.Locale.ENGLISH; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -188,7 +189,7 @@ public void testWrongFile() throws Exception { final Set messages = check.process(impossibleFile, lines); assertEquals(1, messages.size()); final Iterator iterator = messages.iterator(); - assertEquals("Unable to open ''.", iterator.next().getMessage()); + assertEquals(getCheckMessage(MSG_KEY_UNABLE_OPEN, ""), iterator.next().getMessage()); } @Test 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 82a03c71f4a..fd2c3392d13 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks; import static com.puppycrawl.tools.checkstyle.checks.TranslationCheck.MSG_KEY; +import static com.puppycrawl.tools.checkstyle.checks.TranslationCheck.MSG_KEY_MISSING_TRANSLATION_FILE; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.endsWith; import static org.junit.Assert.assertThat; @@ -133,7 +134,8 @@ public void testDefaultTranslationFileIsMissing() throws Exception { }; final String[] expected = { - "0: Properties file 'messages_translation.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "messages_translation.properties"), }; verify( createChecker(checkConfig), @@ -153,7 +155,8 @@ public void testTranslationFilesAreMissing() throws Exception { }; final String[] expected = { - "0: Properties file 'messages_translation_de.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "messages_translation_de.properties"), }; verify( createChecker(checkConfig), @@ -172,7 +175,8 @@ public void testBaseNameWithSeparatorDefaultTranslationIsMissing() throws Except }; final String[] expected = { - "0: Properties file 'messages-translation.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "messages-translation.properties"), }; verify( createChecker(checkConfig), @@ -192,7 +196,8 @@ public void testBaseNameWithSeparatorTranslationsAreMissing() throws Exception { }; final String[] expected = { - "0: Properties file 'messages-translation_tr.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "messages-translation_tr.properties"), }; verify( createChecker(checkConfig), @@ -231,7 +236,8 @@ public void testTranslationFileWithLanguageCountryVariantIsMissing() throws Exce }; final String[] expected = { - "0: Properties file 'messages_home_de.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "messages_home_de.properties"), }; verify( createChecker(checkConfig), @@ -276,7 +282,8 @@ public void testBaseNameOption() throws Exception { }; final String[] expected = { - "0: Properties file 'ButtonLabels_ja.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "ButtonLabels_ja.properties"), }; verify( createChecker(checkConfig), @@ -305,7 +312,8 @@ public void testFileExtensions() throws Exception { }; final String[] expected = { - "0: Properties file 'ButtonLabels_ja.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "ButtonLabels_ja.properties"), }; verify( @@ -335,7 +343,8 @@ public void testEqualBaseNamesButDifferentExtensions() throws Exception { }; final String[] expected = { - "0: Properties file 'ButtonLabels_ja.properties' is missing.", + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, + "ButtonLabels_ja.properties"), }; verify( @@ -359,9 +368,9 @@ public void testRegexpToMatchPartOfBaseName() throws Exception { }; final String[] expected = { - "0: Properties file 'MyLabelsI18_fr.properties' is missing.", - "0: Properties file 'MyLabelsI18_ja.properties' is missing.", - }; + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, "MyLabelsI18_fr.properties"), + "0: " + getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, "MyLabelsI18_ja.properties"), + }; verify( createChecker(checkConfig), diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java index 30529456bdc..4ab4979bf3e 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java @@ -30,7 +30,6 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Locale; import org.junit.Rule; import org.junit.Test; @@ -255,9 +254,6 @@ public void testCacheHeaderFile() throws Exception { checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath()); final Checker checker = new Checker(); - final Locale locale = Locale.ROOT; - checker.setLocaleCountry(locale.getCountry()); - checker.setLocaleLanguage(locale.getLanguage()); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(new BriefUtLogger(stream)); @@ -282,9 +278,6 @@ public void testCacheHeaderWithoutFile() throws Exception { checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath()); final Checker checker = new Checker(); - final Locale locale = Locale.ROOT; - checker.setLocaleCountry(locale.getCountry()); - checker.setLocaleLanguage(locale.getLanguage()); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(new BriefUtLogger(stream)); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index cb638e89946..34838f0b071 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -34,10 +34,12 @@ import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; +import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -49,6 +51,7 @@ import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; import com.puppycrawl.tools.checkstyle.utils.CommonUtils; /** @@ -160,6 +163,9 @@ private void verify(Configuration config, String filePath, String[] expected, final IndentComment... linesWithWarn) throws Exception { final Checker checker = createChecker(config); checker.addListener(new IndentAudit(linesWithWarn)); + checker.setLocaleCountry(Locale.ROOT.getCountry()); + checker.setLocaleLanguage(Locale.ROOT.getLanguage()); + checker.finishLocalSetup(); verify(checker, new File[] {new File(filePath)}, filePath, expected); } @@ -785,8 +791,12 @@ public void testInvalidArrayInitWithChecker() "112: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"), }; + final Checker checker = createChecker(checkConfig); + checker.setLocaleCountry(Locale.ROOT.getCountry()); + checker.setLocaleLanguage(Locale.ROOT.getLanguage()); + checker.finishLocalSetup(); //Test input for this test case is not checked due to issue #693. - verify(checkConfig, fileName, expected); + verify(checker, fileName, expected); } @Test @@ -1737,6 +1747,19 @@ public void testInputAnnotationScopeIndentationCheck() throws Exception { verifyWarns(checkConfig, fileName, expected); } + @Override + protected String internalGetCheckMessage( + String messageBundle, String messageKey, Object... arguments) { + final ResourceBundle resourceBundle = ResourceBundle.getBundle( + messageBundle, + Locale.ROOT, + Thread.currentThread().getContextClassLoader(), + new LocalizedMessage.Utf8Control()); + final String pattern = resourceBundle.getString(messageKey); + final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT); + return formatter.format(arguments); + } + private static final class IndentAudit implements AuditListener { private final IndentComment[] comments; private int position; diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheckTest.java index 2b2a90c847a..f989b96e00c 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheckTest.java @@ -20,8 +20,8 @@ package com.puppycrawl.tools.checkstyle.checks.javadoc; import static com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.MSG_JAVADOC_MISSED_HTML_CLOSE; +import static com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.MSG_JAVADOC_PARSE_RULE_ERROR; import static com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.MSG_JAVADOC_WRONG_SINGLETON_TAG; -import static com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.MSG_KEY_PARSE_ERROR; import static com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck.MSG_KEY_UNRECOGNIZED_ANTLR_ERROR; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -62,9 +62,9 @@ protected String getPath(String filename) throws IOException { public void testNumberFormatException() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(TempCheck.class); final String[] expected = { - "3: " + getCheckMessage(MSG_KEY_PARSE_ERROR, 52, "no viable " - + "alternative at input '
  • a' {@link EntityEntry} (by way of {@link #;' " - + "while parsing HTML_TAG"), + "3: " + getCheckMessage(MSG_JAVADOC_PARSE_RULE_ERROR, 52, "no viable " + + "alternative at input '
    • a' {@link EntityEntry} (by way of {@link #;'", + "HTML_TAG"), }; verify(checkConfig, getPath("InputTestNumberFormatException.java"), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractTypeAwareCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractTypeAwareCheckTest.java index 07c95efe3b6..84d08619293 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractTypeAwareCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractTypeAwareCheckTest.java @@ -167,8 +167,7 @@ public void testWithoutLogErrors() throws Exception { } catch (CheckstyleException ex) { final IllegalStateException cause = (IllegalStateException) ex.getCause(); - assertEquals("Unable to get" - + " class information for @throws tag 'InvalidExceptionName'.", + assertEquals(getCheckMessage(MSG_CLASS_INFO, "@throws", "InvalidExceptionName"), cause.getMessage()); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocPackageCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocPackageCheckTest.java index 3775249fd56..a4d263c1bad 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocPackageCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocPackageCheckTest.java @@ -105,7 +105,7 @@ public void testBoth() throws Exception { public void testHtmlDisallowed() throws Exception { final Configuration checkConfig = createCheckConfig(JavadocPackageCheck.class); final String[] expected = { - "0: Missing package-info.java file.", + "0: " + getCheckMessage(MSG_PACKAGE_INFO), }; verify(createChecker(checkConfig), getPath("pkghtml" + File.separator + "InputIgnored.java"), diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/grammars/GeneratedJava14LexerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/grammars/GeneratedJava14LexerTest.java index f0948c3da93..8ddbb23a0c8 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/grammars/GeneratedJava14LexerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/grammars/GeneratedJava14LexerTest.java @@ -19,6 +19,8 @@ package com.puppycrawl.tools.checkstyle.grammars; +import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN; + import java.io.File; import java.io.IOException; @@ -63,8 +65,8 @@ public void testUnexpectedChar() throws Exception { createCheckConfig(MemberNameCheck.class); // input is 'ÃЯ' final String[] expected = { - "7:9: Name '" + (char) 0xC3 + (char) 0x042F - + "' must match pattern '^[a-z][a-zA-Z0-9]*$'.", + "7:9: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN, + "" + (char) 0xC3 + (char) 0x042F, "^[a-z][a-zA-Z0-9]*$"), }; verify(checkConfig, getPath("InputGrammar.java"), expected); }