Issue #537 refactoring: remove copy-paste of messages from UTests

This commit is contained in:
ychulovskyy 2014-12-27 22:49:55 +01:00 committed by Roman Ivanov
parent df5e519a3d
commit 09a7ca6041
15 changed files with 731 additions and 682 deletions

View File

@ -112,6 +112,18 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*/
public class AnnotationLocationCheck extends Check
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_LOCATION_ALONE = "annotation.location.alone";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_LOCATION = "annotation.location";
/**
* Some javadoc.
*/
@ -191,10 +203,10 @@ public class AnnotationLocationCheck extends Check
if (!isCorrectLocation(annotation, hasParameters)) {
log(annotation.getLineNo(),
"annotation.location.alone", getAnnotationName(annotation));
MSG_KEY_ANNOTATION_LOCATION_ALONE, getAnnotationName(annotation));
}
else if (annotation.getColumnNo() != aCorrectLevel && !hasNodeBefore(annotation)) {
log(annotation.getLineNo(), "annotation.location",
log(annotation.getLineNo(), MSG_KEY_ANNOTATION_LOCATION,
getAnnotationName(annotation), annotation.getColumnNo(), aCorrectLevel);
}
annotation = annotation.getNextSibling();

View File

@ -125,6 +125,41 @@ public final class AnnotationUseStyleCheck extends Check
private static final String ANNOTATION_ELEMENT_SINGLE_NAME =
"value";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_INCORRECT_STYLE =
"annotation.incorrect.style";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_PARENS_MISSING =
"annotation.parens.missing";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_PARENS_PRESENT =
"annotation.parens.present";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING =
"annotation.trailing.comma.missing";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT =
"annotation.trailing.comma.present";
//not extending AbstractOptionCheck because check
//has more than one option type.
@ -260,7 +295,7 @@ public final class AnnotationUseStyleCheck extends Check
if (valuePairCount == 0
&& aAnnotation.branchContains(TokenTypes.EXPR))
{
this.log(aAnnotation.getLineNo(), "annotation.incorrect.style",
this.log(aAnnotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.EXPANDED);
}
}
@ -382,13 +417,13 @@ public final class AnnotationUseStyleCheck extends Check
&& (comma == null || comma.getType() != TokenTypes.COMMA))
{
this.log(rCurly.getLineNo(),
rCurly.getColumnNo(), "annotation.trailing.comma.missing");
rCurly.getColumnNo(), MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING);
}
else if (TrailingArrayComma.NEVER.equals(this.mComma)
&& comma != null && comma.getType() == TokenTypes.COMMA)
{
this.log(comma.getLineNo(),
comma.getColumnNo(), "annotation.trailing.comma.present");
comma.getColumnNo(), MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT);
}
}
@ -412,7 +447,7 @@ public final class AnnotationUseStyleCheck extends Check
if (ClosingParens.ALWAYS.equals(this.mParens)
&& !parenExists)
{
this.log(aAST.getLineNo(), "annotation.parens.missing");
this.log(aAST.getLineNo(), MSG_KEY_ANNOTATION_PARENS_MISSING);
}
else if (ClosingParens.NEVER.equals(this.mParens)
&& !aAST.branchContains(TokenTypes.EXPR)
@ -420,7 +455,7 @@ public final class AnnotationUseStyleCheck extends Check
&& !aAST.branchContains(TokenTypes.ANNOTATION_ARRAY_INIT)
&& parenExists)
{
this.log(aAST.getLineNo(), "annotation.parens.present");
this.log(aAST.getLineNo(), MSG_KEY_ANNOTATION_PARENS_PRESENT);
}
}

View File

@ -96,6 +96,26 @@ public final class MissingDeprecatedCheck extends Check
/** Multiline finished at next Javadoc * */
private static final String NEXT_TAG = "@";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_MISSING_DEPRECATED =
"annotation.missing.deprecated";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_JAVADOC_DUPLICATE_TAG =
"javadoc.duplicateTag";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_JAVADOC_MISSING = "javadoc.missing";
/** {@inheritDoc} */
@Override
public int[] getDefaultTokens()
@ -134,7 +154,7 @@ public final class MissingDeprecatedCheck extends Check
final boolean containsJavadocTag = this.containsJavadocTag(javadoc);
if (containsAnnotation ^ containsJavadocTag) {
this.log(aAST.getLineNo(), "annotation.missing.deprecated");
this.log(aAST.getLineNo(), MSG_KEY_ANNOTATION_MISSING_DEPRECATED);
}
}
@ -168,7 +188,7 @@ public final class MissingDeprecatedCheck extends Check
if (javadocNoargMatcher.find()) {
if (found) {
this.log(currentLine, "javadoc.duplicateTag",
this.log(currentLine, MSG_KEY_JAVADOC_DUPLICATE_TAG,
JavadocTagInfo.DEPRECATED.getText());
}
found = true;
@ -193,15 +213,15 @@ public final class MissingDeprecatedCheck extends Check
&& !lFin.equals(MissingDeprecatedCheck.END_JAVADOC))
{
if (found) {
this.log(currentLine, "javadoc.duplicateTag",
this.log(currentLine, MSG_KEY_JAVADOC_DUPLICATE_TAG,
JavadocTagInfo.DEPRECATED.getText());
}
found = true;
}
else {
this.log(currentLine, "javadoc.missing");
this.log(currentLine, MSG_KEY_JAVADOC_MISSING);
if (found) {
this.log(currentLine, "javadoc.duplicateTag",
this.log(currentLine, MSG_KEY_JAVADOC_DUPLICATE_TAG,
JavadocTagInfo.DEPRECATED.getText());
}
found = true;

View File

@ -86,6 +86,19 @@ public final class MissingOverrideCheck extends Check
private static final Pattern MATCH_INHERITDOC =
Utils.createPattern("\\{\\s*@(inheritDoc)\\s*\\}");
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_TAG_NOT_VALID_ON = "tag.not.valid.on";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_MISSING_OVERRIDE =
"annotation.missing.override";
/** @see #setJavaFiveCompatibility(boolean) */
private boolean mJavaFiveCompatibility;
@ -142,7 +155,7 @@ public final class MissingOverrideCheck extends Check
final boolean containsTag = this.containsJavadocTag(javadoc);
if (containsTag && !JavadocTagInfo.INHERIT_DOC.isValidOn(aAST)) {
this.log(aAST.getLineNo(), "tag.not.valid.on",
this.log(aAST.getLineNo(), MSG_KEY_TAG_NOT_VALID_ON,
JavadocTagInfo.INHERIT_DOC.getText());
return;
}
@ -162,7 +175,7 @@ public final class MissingOverrideCheck extends Check
&& (!AnnotationUtility.containsAnnotation(aAST, OVERRIDE)
&& !AnnotationUtility.containsAnnotation(aAST, FQ_OVERRIDE)))
{
this.log(aAST.getLineNo(), "annotation.missing.override");
this.log(aAST.getLineNo(), MSG_KEY_ANNOTATION_MISSING_OVERRIDE);
}
}

View File

@ -96,6 +96,13 @@ public class SuppressWarningsCheck extends AbstractFormatCheck
private static final String FQ_SUPPRESS_WARNINGS =
"java.lang." + SUPPRESS_WARNINGS;
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED =
"suppressed.warning.not.allowed";
/**
* Ctor that specifies the default for the format property
* as specified in the class javadocs.
@ -216,7 +223,7 @@ public class SuppressWarningsCheck extends AbstractFormatCheck
final Matcher matcher = this.getRegexp().matcher(aWarningText);
if (matcher.matches()) {
this.log(aLineNo, aColNum,
"suppressed.warning.not.allowed", aWarningText);
MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED, aWarningText);
}
}

View File

@ -1,5 +1,6 @@
package com.puppycrawl.tools.checkstyle;
import static java.text.MessageFormat.format;
import static org.junit.Assert.assertEquals;
import com.google.common.collect.Lists;
@ -18,38 +19,41 @@ import java.util.List;
import java.util.Locale;
import java.util.Properties;
public abstract class BaseCheckTestSupport
{
/** a brief logger that only display info about errors */
public abstract class BaseCheckTestSupport {
/**
* a brief logger that only display info about errors
*/
protected static class BriefLogger
extends DefaultLogger
{
public BriefLogger(OutputStream out)
{
extends DefaultLogger {
public BriefLogger(OutputStream out) {
super(out, true);
}
@Override
public void auditStarted(AuditEvent evt) {}
public void auditStarted(AuditEvent evt) {
}
@Override
public void fileFinished(AuditEvent evt) {}
public void fileFinished(AuditEvent evt) {
}
@Override
public void fileStarted(AuditEvent evt) {}
public void fileStarted(AuditEvent evt) {
}
}
protected final ByteArrayOutputStream mBAOS = new ByteArrayOutputStream();
protected final PrintStream mStream = new PrintStream(mBAOS);
protected final Properties mProps = new Properties();
public static DefaultConfiguration createCheckConfig(Class<?> aClazz)
{
public static DefaultConfiguration createCheckConfig(Class<?> aClazz) {
final DefaultConfiguration checkConfig =
new DefaultConfiguration(aClazz.getName());
new DefaultConfiguration(aClazz.getName());
return checkConfig;
}
protected Checker createChecker(Configuration aCheckConfig)
throws Exception
{
throws Exception {
final DefaultConfiguration dc = createCheckerConfig(aCheckConfig);
final Checker c = new Checker();
// make sure the tests always run with english error messages
@ -63,10 +67,9 @@ public abstract class BaseCheckTestSupport
return c;
}
protected DefaultConfiguration createCheckerConfig(Configuration aConfig)
{
protected DefaultConfiguration createCheckerConfig(Configuration aConfig) {
final DefaultConfiguration dc =
new DefaultConfiguration("configuration");
new DefaultConfiguration("configuration");
final DefaultConfiguration twConf = createCheckConfig(TreeWalker.class);
// make sure that the tests always run with this charset
dc.addAttribute("charset", "iso-8859-1");
@ -76,26 +79,22 @@ public abstract class BaseCheckTestSupport
}
protected static String getPath(String aFilename)
throws IOException
{
throws IOException {
return new File("src/test/resources/com/puppycrawl/tools/checkstyle/" + aFilename).getCanonicalPath();
}
protected static String getSrcPath(String aFilename) throws IOException
{
protected static String getSrcPath(String aFilename) throws IOException {
return new File("src/test/java/com/puppycrawl/tools/checkstyle/" + aFilename).getCanonicalPath();
}
protected void verify(Configuration aConfig, String aFileName, String[] aExpected)
throws Exception
{
throws Exception {
verify(createChecker(aConfig), aFileName, aFileName, aExpected);
}
protected void verify(Checker aC, String aFileName, String[] aExpected)
throws Exception
{
throws Exception {
verify(aC, aFileName, aFileName, aExpected);
}
@ -103,19 +102,17 @@ public abstract class BaseCheckTestSupport
String aProcessedFilename,
String aMessageFileName,
String[] aExpected)
throws Exception
{
throws Exception {
verify(aC,
new File[] {new File(aProcessedFilename)},
aMessageFileName, aExpected);
new File[]{new File(aProcessedFilename)},
aMessageFileName, aExpected);
}
protected void verify(Checker aC,
File[] aProcessedFiles,
String aMessageFileName,
String[] aExpected)
throws Exception
{
throws Exception {
mStream.flush();
final List<File> theFiles = Lists.newArrayList();
Collections.addAll(theFiles, aProcessedFiles);
@ -123,10 +120,10 @@ public abstract class BaseCheckTestSupport
// process each of the lines
final ByteArrayInputStream bais =
new ByteArrayInputStream(mBAOS.toByteArray());
new ByteArrayInputStream(mBAOS.toByteArray());
final LineNumberReader lnr =
new LineNumberReader(new InputStreamReader(bais));
new LineNumberReader(new InputStreamReader(bais));
for (int i = 0; i < aExpected.length; i++) {
final String expected = aMessageFileName + ":" + aExpected[i];
final String actual = lnr.readLine();
@ -134,7 +131,7 @@ public abstract class BaseCheckTestSupport
}
assertEquals("unexpected output: " + lnr.readLine(),
aExpected.length, errs);
aExpected.length, errs);
aC.destroy();
}
@ -142,19 +139,16 @@ public abstract class BaseCheckTestSupport
* Gets the check message 'as is' from appropriate 'messages.properties'
* file.
*
* @param messageKey
* the key of message in 'messages.properties' file.
* @param messageKey the key of message in 'messages.properties' file.
* @param arguments the arguments of message in 'messages.properties' file.
*/
public String getCheckMessage(String messageKey)
{
public String getCheckMessage(String messageKey, Object... arguments) {
Properties pr = new Properties();
try {
pr.load(getClass().getResourceAsStream("messages.properties"));
}
catch (IOException e) {
} catch (IOException e) {
return null;
}
return pr.getProperty(messageKey);
return format(pr.getProperty(messageKey), arguments);
}
}

View File

@ -22,7 +22,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -36,15 +35,15 @@ import com.puppycrawl.tools.checkstyle.BaseFileSetCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import static com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck.IO_EXCEPTION_KEY;
import static com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck.MSG_KEY;
/**
* JUnit tests for Unique Properties check.
*/
public class UniquePropertiesCheckTest extends BaseFileSetCheckTestSupport
{
private String msg = getCheckMessage(UniquePropertiesCheck.MSG_KEY);
private String ioMsg = getCheckMessage(UniquePropertiesCheck.IO_EXCEPTION_KEY);
private DefaultConfiguration mCheckConfig;
@Before
@ -63,12 +62,12 @@ public class UniquePropertiesCheckTest extends BaseFileSetCheckTestSupport
{
final String[] expected =
{
buildMesssage(3, "general.exception", 2),
buildMesssage(5, "DefaultLogger.auditStarted", 2),
buildMesssage(11, "onlineManual", 3),
buildMesssage(22, "time stamp", 3),
buildMesssage(28, "Support Link ", 2),
buildMesssage(34, "failed", 2),
"3: " + getCheckMessage(MSG_KEY, "general.exception", 2),
"5: " + getCheckMessage(MSG_KEY, "DefaultLogger.auditStarted", 2),
"11: " + getCheckMessage(MSG_KEY, "onlineManual", 3),
"22: " + getCheckMessage(MSG_KEY, "time stamp", 3),
"28: " + getCheckMessage(MSG_KEY, "Support Link ", 2),
"34: " + getCheckMessage(MSG_KEY, "failed", 2),
};
verify(mCheckConfig, getPath("InputUniquePropertiesCheck.properties"),
expected);
@ -118,7 +117,7 @@ public class UniquePropertiesCheckTest extends BaseFileSetCheckTestSupport
"unable.open.cause");
Assert.assertEquals("Message '" + message.getMessage()
+ "' is not valid", message.getMessage(),
buildIOMessage(fileName, getFileNotFoundDetail(file)));
getCheckMessage(IO_EXCEPTION_KEY, fileName, getFileNotFoundDetail(file)));
}
/**
@ -142,17 +141,4 @@ public class UniquePropertiesCheckTest extends BaseFileSetCheckTestSupport
return ex.getLocalizedMessage();
}
}
private String buildMesssage(int lineNumber, String keyName,
int nOccurrences)
{
return lineNumber + ": "
+ MessageFormat.format(msg, keyName, nOccurrences);
}
private String buildIOMessage(String filename, String exceptionDetails)
{
return MessageFormat.format(ioMsg, filename, exceptionDetails);
}
}

View File

@ -23,6 +23,9 @@ import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION;
public class AnnotationLocationCheckTest extends BaseCheckTestSupport
{
@Test
@ -40,25 +43,25 @@ public class AnnotationLocationCheckTest extends BaseCheckTestSupport
{
DefaultConfiguration checkConfig = createCheckConfig(AnnotationLocationCheck.class);
final String[] expected = {
"6: Annotation 'MyAnnotation1' should be alone on line.",
"11: Annotation 'MyAnnotation1' should be alone on line.",
"17: Annotation 'MyAnnotation1' have incorrect indentation level 8, expected level should be 4.",
"25: Annotation 'MyAnnotation1' have incorrect indentation level 8, expected level should be 4.",
"29: Annotation 'MyAnnotation1' should be alone on line.",
"29: Annotation 'MyAnnotation2' should be alone on line.",
"32: Annotation 'MyAnnotation2' have incorrect indentation level 7, expected level should be 4.",
"36: Annotation 'MyAnnotation2' have incorrect indentation level 8, expected level should be 4.",
"37: Annotation 'MyAnnotation3' have incorrect indentation level 6, expected level should be 4.",
"38: Annotation 'MyAnnotation4' have incorrect indentation level 10, expected level should be 4.",
"41: Annotation 'MyAnnotation1' should be alone on line.",
"48: Annotation 'MyAnnotation1' have incorrect indentation level 12, expected level should be 8.",
"61: Annotation 'MyAnnotation2' have incorrect indentation level 12, expected level should be 8.",
"65: Annotation 'MyAnnotation2' have incorrect indentation level 12, expected level should be 8.",
"70: Annotation 'MyAnnotation2' have incorrect indentation level 7, expected level should be 4.",
"73: Annotation 'MyAnnotation1' should be alone on line.",
"85: Annotation 'MyAnnotation2' have incorrect indentation level 11, expected level should be 8.",
"88: Annotation 'MyAnnotation2' have incorrect indentation level 10, expected level should be 8.",
"98: Annotation 'MyAnnotation2' have incorrect indentation level 0, expected level should be 3.",
"6: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation2"),
"32: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
"36: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 8, 4),
"37: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
"38: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"48: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
"61: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"65: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"70: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
"73: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"85: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 11, 8),
"88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
"98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
};
verify(checkConfig, getPath("annotation/InputIncorrectAnnotationLocation.java"), expected);
}

View File

@ -23,6 +23,12 @@ import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_MISSING;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_PRESENT;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT;
public class AnnotationUseStyleTest extends BaseCheckTestSupport
{
/**
@ -37,9 +43,9 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "ignore");
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"3: Annotation must have closing parenthesis.",
"18: Annotation must have closing parenthesis.",
"23: Annotation must have closing parenthesis.",
"3: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
"18: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
"23: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
};
verify(checkConfig, getPath("annotation" + File.separator + "DifferentUseStyles.java"), expected);
@ -57,9 +63,9 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "ignore");
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"13: Annotation cannot have closing parenthesis.",
"30: Annotation cannot have closing parenthesis.",
"33: Annotation cannot have closing parenthesis.",
"13: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
"30: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
"33: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
};
verify(checkConfig, getPath("annotation" + File.separator + "DifferentUseStyles.java"), expected);
@ -73,13 +79,13 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "EXPANDED");
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"5: Annotation style must be 'EXPANDED'.",
"12: Annotation style must be 'EXPANDED'.",
"20: Annotation style must be 'EXPANDED'.",
"26: Annotation style must be 'EXPANDED'.",
"39: Annotation style must be 'EXPANDED'.",
"41: Annotation style must be 'EXPANDED'.",
"58: Annotation style must be 'EXPANDED'.",
"5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
"12: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
"20: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
"26: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
"39: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
"58: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
};
verify(checkConfig, getPath("annotation" + File.separator + "DifferentUseStyles.java"), expected);
@ -93,8 +99,8 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "COMPACT");
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"43: Annotation style must be 'COMPACT'.",
"47: Annotation style must be 'COMPACT'.",
"43: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
"47: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
};
verify(checkConfig, getPath("annotation" + File.separator + "DifferentUseStyles.java"), expected);
@ -108,11 +114,11 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "COMPACT_NO_ARRAY");
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"5: Annotation style must be 'COMPACT_NO_ARRAY'.",
"20: Annotation style must be 'COMPACT_NO_ARRAY'.",
"41: Annotation style must be 'COMPACT_NO_ARRAY'.",
"43: Annotation style must be 'COMPACT_NO_ARRAY'.",
"47: Annotation style must be 'COMPACT_NO_ARRAY'.",
"5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"20: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"43: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"47: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
};
verify(checkConfig, getPath("annotation" + File.separator + "DifferentUseStyles.java"), expected);
@ -126,19 +132,19 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "ignore");
checkConfig.addAttribute("trailingArrayComma", "ALWAYS");
final String[] expected = {
"3:20: Annotation array values must contain trailing comma.",
"6:30: Annotation array values must contain trailing comma.",
"10:40: Annotation array values must contain trailing comma.",
"13:44: Annotation array values must contain trailing comma.",
"16:54: Annotation array values must contain trailing comma.",
"24:37: Annotation array values must contain trailing comma.",
"24:65: Annotation array values must contain trailing comma.",
"26:21: Annotation array values must contain trailing comma.",
"26:30: Annotation array values must contain trailing comma.",
"29:39: Annotation array values must contain trailing comma.",
"29:49: Annotation array values must contain trailing comma.",
"32:21: Annotation array values must contain trailing comma.",
"32:56: Annotation array values must contain trailing comma.",
"3:20: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"6:30: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"10:40: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"13:44: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"16:54: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"24:37: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"24:65: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"26:21: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"26:30: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"29:39: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"29:49: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"32:21: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
"32:56: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
};
verify(checkConfig, getPath("annotation" + File.separator + "AnnotationUseNoTrailingComma.java"), expected);
@ -165,14 +171,14 @@ public class AnnotationUseStyleTest extends BaseCheckTestSupport
checkConfig.addAttribute("elementStyle", "ignore");
checkConfig.addAttribute("trailingArrayComma", "NEVER");
final String[] expected = {
"9:32: Annotation array values cannot contain trailing comma.",
"13:42: Annotation array values cannot contain trailing comma.",
"16:46: Annotation array values cannot contain trailing comma.",
"19:56: Annotation array values cannot contain trailing comma.",
"27:38: Annotation array values cannot contain trailing comma.",
"27:67: Annotation array values cannot contain trailing comma.",
"33:39: Annotation array values cannot contain trailing comma.",
"33:50: Annotation array values cannot contain trailing comma.",
"9:32: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"13:42: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"16:46: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"19:56: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"27:38: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"27:67: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"33:39: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
"33:50: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
};
verify(checkConfig, getPath("annotation" + File.separator + "AnnotationUseWithTrailingComma.java"), expected);

View File

@ -25,6 +25,10 @@ import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.MSG_KEY_ANNOTATION_MISSING_DEPRECATED;
import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.MSG_KEY_JAVADOC_DUPLICATE_TAG;
import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.MSG_KEY_JAVADOC_MISSING;
public class MissingDeprecatedTest extends BaseCheckTestSupport
{
/**
@ -36,15 +40,15 @@ public class MissingDeprecatedTest extends BaseCheckTestSupport
DefaultConfiguration checkConfig = createCheckConfig(MissingDeprecatedCheck.class);
final String[] expected = {
"7: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"12: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"19: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"26: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"31: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"38: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"43: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"51: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"56: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"7: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"12: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"19: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"31: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"38: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"43: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"51: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"56: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
};
verify(checkConfig, getPath("annotation" + File.separator + "BadDeprecatedAnnotation.java"), expected);
@ -60,15 +64,15 @@ public class MissingDeprecatedTest extends BaseCheckTestSupport
DefaultConfiguration checkConfig = createCheckConfig(MissingDeprecatedCheck.class);
final String[] expected = {
"5: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"11: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"16: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"23: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"29: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"38: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"40: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"48: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"55: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"23: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"38: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"40: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"48: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"55: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
};
verify(checkConfig, getPath("annotation" + File.separator + "BadDeprecatedJavadoc.java"), expected);
@ -84,19 +88,19 @@ public class MissingDeprecatedTest extends BaseCheckTestSupport
DefaultConfiguration checkConfig = createCheckConfig(MissingDeprecatedCheck.class);
final String[] expected = {
"5: Duplicate @deprecated tag.",
"12: Duplicate @deprecated tag.",
"14: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"17: Missing a Javadoc comment.",
"19: Must include both @java.lang.Deprecated annotation and @deprecated Javadoc tag with description.",
"24: Missing a Javadoc comment.",
"32: Missing a Javadoc comment.",
"33: Duplicate @deprecated tag.",
"33: Missing a Javadoc comment.",
"42: Duplicate @deprecated tag.",
"42: Missing a Javadoc comment.",
"50: Missing a Javadoc comment.",
"51: Duplicate @deprecated tag.",
"5: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
"12: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
"14: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"17: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
"19: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
"24: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
"32: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
"33: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
"33: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
"42: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
"42: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
"50: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
"51: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
};
verify(checkConfig, getPath("annotation" + File.separator + "SpecialCaseDeprecated.java"), expected);

View File

@ -25,6 +25,9 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE;
import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck.MSG_KEY_TAG_NOT_VALID_ON;
public class MissingOverrideCheckTest extends BaseCheckTestSupport
{
/**
@ -39,10 +42,10 @@ public class MissingOverrideCheckTest extends BaseCheckTestSupport
checkConfig.addAttribute("javaFiveCompatibility", "false");
final String[] expected = {
"8: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"30: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"41: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"50: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"8: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"30: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"50: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};
verify(checkConfig, getPath("annotation" + File.separator + "BadOverrideFromObject.java"), expected);
@ -60,10 +63,10 @@ public class MissingOverrideCheckTest extends BaseCheckTestSupport
checkConfig.addAttribute("javaFiveCompatibility", "true");
final String[] expected = {
"8: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"30: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"41: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"50: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"8: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"30: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"50: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};
verify(checkConfig, getPath("annotation" + File.separator + "BadOverrideFromObject.java"), expected);
@ -79,13 +82,13 @@ public class MissingOverrideCheckTest extends BaseCheckTestSupport
{
DefaultConfiguration checkConfig = createCheckConfig(MissingOverrideCheck.class);
final String[] expected = {
"10: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"26: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"34: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"40: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"47: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"53: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"63: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"10: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"34: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"40: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"47: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"53: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"63: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};
verify(checkConfig, getPath("annotation" + File.separator + "BadOverrideFromOther.java"), expected);
@ -118,10 +121,10 @@ public class MissingOverrideCheckTest extends BaseCheckTestSupport
{
DefaultConfiguration checkConfig = createCheckConfig(MissingOverrideCheck.class);
final String[] expected = {
"10: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"16: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"29: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"35: Must include @java.lang.Override annotation when {@inheritDoc} Javadoc tag exists.",
"10: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
"35: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
};
verify(checkConfig, getPath("annotation" + File.separator + "BadAnnonOverride.java"), expected);
@ -152,10 +155,8 @@ public class MissingOverrideCheckTest extends BaseCheckTestSupport
{
DefaultConfiguration checkConfig = createCheckConfig(MissingOverrideCheck.class);
final String[] expected = {
"8: The Javadoc {@inheritDoc} tag is not valid at this location.",
"15: The Javadoc {@inheritDoc} tag is not valid at this location.",
//this wont be flagged because this check only checks methods.
//"22: The Javadoc comment contains an {@inheritDoc} tag. The tag is not valid at this location.",
"8: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
"15: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
};
verify(checkConfig, getPath("annotation" + File.separator + "NotOverride.java"), expected);

View File

@ -21,8 +21,6 @@ package com.puppycrawl.tools.checkstyle.checks.coding;
import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY;
import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY_EXT;
import static java.text.MessageFormat.format;
import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
@ -204,14 +202,4 @@ public class VariableDeclarationUsageDistanceCheckTest extends
};
verify(checkConfig, getPath("coding/InputVariableDeclarationUsageDistanceCheck.java"), expected);
}
/**
* 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.
*/
public String getCheckMessage(String messageKey, Object ... arguments)
{
return format(getCheckMessage(messageKey), arguments);
}
}

View File

@ -19,7 +19,6 @@
package com.puppycrawl.tools.checkstyle.checks.naming;
import static com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck.MSG_KEY;
import static java.text.MessageFormat.format;
import org.junit.Test;
@ -314,14 +313,4 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport
verify(checkConfig, getPath("naming/AbstractMultisetSetCountTester.java"), expected);
}
/**
* 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.
*/
public String getCheckMessage(String messageKey, Object ... arguments)
{
return format(getCheckMessage(messageKey), arguments);
}
}

View File

@ -20,7 +20,6 @@ package com.puppycrawl.tools.checkstyle.checks.naming;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import org.junit.Test;
@ -28,15 +27,13 @@ import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
import static com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck.DEFAULT_PATTERN;
public class TypeNameCheckTest
extends BaseCheckTestSupport
{
/**
* Localized error message from @link {@link TypeNameCheck}.
*/
private final String msg = getCheckMessage(AbstractNameCheck.MSG_INVALID_PATTERN);
private final String inputFilename;
public TypeNameCheckTest() throws IOException
@ -64,14 +61,14 @@ public class TypeNameCheckTest
final DefaultConfiguration checkConfig =
createCheckConfig(TypeNameCheck.class);
final String[] expected = {
buildMesssage(3, 7, "inputHeaderClass",
TypeNameCheck.DEFAULT_PATTERN),
buildMesssage(5, 22, "inputHeaderInterface",
TypeNameCheck.DEFAULT_PATTERN),
buildMesssage(7, 17, "inputHeaderEnum",
TypeNameCheck.DEFAULT_PATTERN),
buildMesssage(9, 23, "inputHeaderAnnotation",
TypeNameCheck.DEFAULT_PATTERN),
"3:7: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderClass", DEFAULT_PATTERN),
"5:22: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderInterface", DEFAULT_PATTERN),
"7:17: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderEnum", DEFAULT_PATTERN),
"9:23: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderAnnotation", DEFAULT_PATTERN),
};
verify(checkConfig, inputFilename, expected);
}
@ -84,8 +81,8 @@ public class TypeNameCheckTest
createCheckConfig(TypeNameCheck.class);
checkConfig.addAttribute("tokens", TokenTypes.getTokenName(TokenTypes.CLASS_DEF));
final String[] expected = {
buildMesssage(3, 7, "inputHeaderClass",
TypeNameCheck.DEFAULT_PATTERN),
"3:7: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderClass", DEFAULT_PATTERN),
};
verify(checkConfig, inputFilename, expected);
}
@ -98,8 +95,8 @@ public class TypeNameCheckTest
createCheckConfig(TypeNameCheck.class);
checkConfig.addAttribute("tokens", TokenTypes.getTokenName(TokenTypes.INTERFACE_DEF));
final String[] expected = {
buildMesssage(5, 22, "inputHeaderInterface",
TypeNameCheck.DEFAULT_PATTERN),
"5:22: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderInterface", DEFAULT_PATTERN),
};
verify(checkConfig, inputFilename, expected);
}
@ -112,8 +109,8 @@ public class TypeNameCheckTest
createCheckConfig(TypeNameCheck.class);
checkConfig.addAttribute("tokens", TokenTypes.getTokenName(TokenTypes.ENUM_DEF));
final String[] expected = {
buildMesssage(7, 17, "inputHeaderEnum",
TypeNameCheck.DEFAULT_PATTERN),
"7:17: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderEnum", DEFAULT_PATTERN),
};
verify(checkConfig, inputFilename, expected);
}
@ -126,17 +123,9 @@ public class TypeNameCheckTest
createCheckConfig(TypeNameCheck.class);
checkConfig.addAttribute("tokens", TokenTypes.getTokenName(TokenTypes.ANNOTATION_DEF));
final String[] expected = {
buildMesssage(9, 23, "inputHeaderAnnotation",
TypeNameCheck.DEFAULT_PATTERN),
"9:23: " + getCheckMessage(MSG_INVALID_PATTERN,
"inputHeaderAnnotation", DEFAULT_PATTERN),
};
verify(checkConfig, inputFilename, expected);
}
private String buildMesssage(int lineNumber, int colNumber, String name,
String pattern)
{
return lineNumber + ":" + colNumber + ": "
+ MessageFormat.format(msg, name, pattern);
}
}