Issue #3065: expanded message test to all locales
This commit is contained in:
parent
dd5d6bce90
commit
3d8efeeccd
|
|
@ -38,6 +38,18 @@ import com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck;
|
|||
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
|
||||
|
||||
public class AllChecksTest extends BaseCheckTestSupport {
|
||||
private static final Locale[] ALL_LOCALES = {
|
||||
Locale.GERMAN,
|
||||
new Locale("es"),
|
||||
new Locale("fi"),
|
||||
Locale.FRENCH,
|
||||
Locale.JAPANESE,
|
||||
new Locale("pt"),
|
||||
new Locale("tr"),
|
||||
Locale.CHINESE,
|
||||
Locale.ENGLISH,
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testAllChecksWithDefaultConfiguration() throws Exception {
|
||||
final String inputFilePath = getPath("InputDefaultConfig.java");
|
||||
|
|
@ -190,17 +202,33 @@ public class AllChecksTest extends BaseCheckTestSupport {
|
|||
message.setAccessible(true);
|
||||
}
|
||||
|
||||
final String result = CheckUtil.getCheckMessage(module, message.get(null)
|
||||
.toString());
|
||||
for (Locale locale : ALL_LOCALES) {
|
||||
final String messageString = message.get(null).toString();
|
||||
String result = null;
|
||||
|
||||
Assert.assertNotNull(module.getSimpleName() + " should have text for the message '"
|
||||
+ message.getName() + "'", result);
|
||||
Assert.assertFalse(
|
||||
module.getSimpleName() + " should have non-empty text for the message '"
|
||||
+ message.getName() + "'", result.trim().isEmpty());
|
||||
Assert.assertFalse(module.getSimpleName()
|
||||
+ " should have non-TODO text for the message '" + message.getName() + "'",
|
||||
result.trim().startsWith("TODO"));
|
||||
try {
|
||||
result = CheckUtil.getCheckMessage(module, locale, messageString);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.fail(module.getSimpleName() + " with the message '" + messageString
|
||||
+ "' in locale '" + locale.getLanguage() + "' failed with: "
|
||||
+ ex.getClass().getSimpleName() + " - " + ex.getMessage());
|
||||
}
|
||||
|
||||
Assert.assertNotNull(
|
||||
module.getSimpleName() + " should have text for the message '"
|
||||
+ messageString + "' in locale " + locale.getLanguage() + "'",
|
||||
result);
|
||||
Assert.assertFalse(
|
||||
module.getSimpleName() + " should have non-empty text for the message '"
|
||||
+ messageString + "' in locale '" + locale.getLanguage() + "'",
|
||||
result.trim().isEmpty());
|
||||
Assert.assertFalse(
|
||||
module.getSimpleName() + " should have non-TODO text for the message '"
|
||||
+ messageString + "' in locale " + locale.getLanguage() + "'",
|
||||
!"todo.match".equals(messageString)
|
||||
&& result.trim().startsWith("TODO"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -253,19 +253,27 @@ public final class CheckUtil {
|
|||
* Gets the check message 'as is' from appropriate 'messages.properties'
|
||||
* file.
|
||||
*
|
||||
* @param messageKey the key of message in 'messages.properties' file.
|
||||
* @param arguments the arguments of message in 'messages.properties' file.
|
||||
* @param locale the locale to get the message for.
|
||||
* @param messageKey the key of message in 'messages*.properties' file.
|
||||
* @param arguments the arguments of message in 'messages*.properties' file.
|
||||
* @return the check's formatted message.
|
||||
*/
|
||||
public static String getCheckMessage(Class<?> module, String messageKey, Object... arguments) {
|
||||
public static String getCheckMessage(Class<?> module, Locale locale, String messageKey,
|
||||
Object... arguments) {
|
||||
final Properties pr = new Properties();
|
||||
try {
|
||||
pr.load(module.getResourceAsStream("messages.properties"));
|
||||
if (locale == Locale.ENGLISH) {
|
||||
pr.load(module.getResourceAsStream("messages.properties"));
|
||||
}
|
||||
else {
|
||||
pr.load(module
|
||||
.getResourceAsStream("messages_" + locale.getLanguage() + ".properties"));
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return null;
|
||||
}
|
||||
final MessageFormat formatter = new MessageFormat(pr.getProperty(messageKey), Locale.ROOT);
|
||||
final MessageFormat formatter = new MessageFormat(pr.getProperty(messageKey), locale);
|
||||
return formatter.format(arguments);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue