Skip to content

Commit

Permalink
Issue #4516: fix second bunch of pmd violations for JUnitAssertionsSh…
Browse files Browse the repository at this point in the history
…ouldIncludeMessage
  • Loading branch information
Nimfadora committed Jun 24, 2017
1 parent 825cd5e commit 8dec45a
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 58 deletions.
26 changes: 17 additions & 9 deletions src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java
Expand Up @@ -147,7 +147,8 @@ public void testWithCheckNotHavingTreeWalkerAsParent() throws Exception {
fail("CheckstyleException is expected");
}
catch (CheckstyleException exception) {
assertTrue(exception.getMessage().contains("TreeWalker is not allowed as a parent of"));
assertTrue("Error message is unexpected",
exception.getMessage().contains("TreeWalker is not allowed as a parent of"));
}
}

Expand All @@ -169,7 +170,8 @@ public void testForInvalidCheckImplementation() throws Exception {
verify(checkConfig, pathToEmptyFile, expected);
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().contains("isCommentNodesRequired"));
assertTrue("Error message is unexpected",
ex.getMessage().contains("isCommentNodesRequired"));
}
}

Expand Down Expand Up @@ -212,7 +214,8 @@ public void testProcessWithParserThrowable() throws Exception {
treeWalker.processFiltered(file, lines);
}
catch (CheckstyleException exception) {
assertTrue(exception.getMessage().contains(
assertTrue("Error message is unexpected",
exception.getMessage().contains(
"occurred during the analysis of file"));
}
}
Expand All @@ -233,7 +236,8 @@ public void testProcessWithRecognitionException() throws Exception {
treeWalker.processFiltered(file, lines);
}
catch (CheckstyleException exception) {
assertTrue(exception.getMessage().contains(
assertTrue("Error message is unexpected",
exception.getMessage().contains(
"TokenStreamRecognitionException occurred during the analysis of file"));
}
}
Expand All @@ -250,7 +254,8 @@ public void testRequiredTokenIsNotInDefaultTokens() throws Exception {
fail("CheckstyleException is expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith("cannot initialize module"
assertTrue("Error message is unexpected",
ex.getMessage().startsWith("cannot initialize module"
+ " com.puppycrawl.tools.checkstyle.TreeWalker - Token \""
+ TokenTypes.ASSIGN + "\" from required"
+ " tokens was not found in default tokens list in check"));
Expand Down Expand Up @@ -364,9 +369,12 @@ public void testFinishLocalSetupFullyInitialized() throws Exception {
treeWalker.finishLocalSetup();

final Context context = (Context) Whitebox.getInternalState(treeWalker, "childContext");
assertEquals(contextClassLoader, context.get("classLoader"));
assertEquals("error", context.get("severity"));
assertEquals(String.valueOf(100), context.get("tabWidth"));
assertEquals("Classloader object differs from expected",
contextClassLoader, context.get("classLoader"));
assertEquals("Severity differs from expected",
"error", context.get("severity"));
assertEquals("Tab width differs from expected",
String.valueOf(100), context.get("tabWidth"));
}

@Test
Expand All @@ -376,7 +384,7 @@ public void testCheckInitIsCalledInTreeWalker() throws Exception {
final File file = temporaryFolder.newFile("file.pdf");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, file.getPath(), expected);
assertTrue(VerifyInitCheck.isInitWasCalled());
assertTrue("Init was not called", VerifyInitCheck.isInitWasCalled());
}

private static class BadJavaDocCheck extends AbstractCheck {
Expand Down
Expand Up @@ -50,20 +50,22 @@ private static Method getGetMessageBundleMethod() throws Exception {

@Test
public void testGetMessageBundleWithPackage() throws Exception {
assertEquals("com.mycompany.checks.messages",
assertEquals("Message bundle differs from expected",
"com.mycompany.checks.messages",
getGetMessageBundleMethod().invoke(null, "com.mycompany.checks.MyCoolCheck"));
}

@Test
public void testGetMessageBundleWithoutPackage() throws Exception {
assertEquals("messages",
assertEquals("Message bundle differs from expected",
"messages",
getGetMessageBundleMethod().invoke(null, "MyCoolCheck"));
}

@Test
public void testCustomId() {
emptyCheck.setId("MyId");
assertEquals("MyId", emptyCheck.getId());
assertEquals("Id differs from expected", "MyId", emptyCheck.getId());
}

@Test
Expand All @@ -78,8 +80,10 @@ public void testCustomMessage() throws Exception {
emptyCheck.log(0, "msgKey");

final SortedSet<LocalizedMessage> messages = collector.getMessages();
assertEquals(1, messages.size());
assertEquals("This is a custom message.", messages.first()
assertEquals("Amount of messages differs from expected",
1, messages.size());
assertEquals("Message differs from expected",
"This is a custom message.", messages.first()
.getMessage());
}

Expand All @@ -95,9 +99,11 @@ public void testCustomMessageWithParameters() throws Exception {
emptyCheck.log(0, "msgKey", "TestParam");

final SortedSet<LocalizedMessage> messages = collector.getMessages();
assertEquals(1, messages.size());
assertEquals("Amount of messages differs from expected",
1, messages.size());

assertEquals("This is a custom message with TestParam.",
assertEquals("Message differs from expected",
"This is a custom message with TestParam.",
messages.first().getMessage());
}

Expand All @@ -115,7 +121,8 @@ public void testCustomMessageWithParametersNegative() throws Exception {
fail("exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Unmatched braces in the pattern.", ex.getMessage());
assertEquals("Error message is unexpected",
"Unmatched braces in the pattern.", ex.getMessage());
}
}

Expand Down
Expand Up @@ -43,7 +43,8 @@ protected String getPath(String filename) throws IOException {
public void testGetRequiredTokens() {
final ArrayTypeStyleCheck checkObj = new ArrayTypeStyleCheck();
final int[] expected = {TokenTypes.ARRAY_DECLARATOR};
assertArrayEquals(expected, checkObj.getRequiredTokens());
assertArrayEquals("Required tokens differs from expected",
expected, checkObj.getRequiredTokens());
}

@Test
Expand Down Expand Up @@ -78,7 +79,9 @@ public void testGetAcceptableTokens() {
final int[] expected = {TokenTypes.ARRAY_DECLARATOR };
final ArrayTypeStyleCheck check = new ArrayTypeStyleCheck();
final int[] actual = check.getAcceptableTokens();
assertEquals(1, actual.length);
assertArrayEquals(expected, actual);
assertEquals("Amount of acceptable tokens differs from expected",
1, actual.length);
assertArrayEquals("Acceptable tokens differs from expected",
expected, actual);
}
}
Expand Up @@ -156,7 +156,8 @@ public void testGetRequiredTokens() {
TokenTypes.STRING_LITERAL,
TokenTypes.CHAR_LITERAL,
};
assertArrayEquals(expected, checkObj.getRequiredTokens());
assertArrayEquals("Required tokens differ from expected",
expected, checkObj.getRequiredTokens());
}

@Test
Expand Down Expand Up @@ -333,7 +334,8 @@ public void testGetAcceptableTokens() {
final AvoidEscapedUnicodeCharactersCheck check = new AvoidEscapedUnicodeCharactersCheck();
final int[] actual = check.getAcceptableTokens();
final int[] expected = {TokenTypes.STRING_LITERAL, TokenTypes.CHAR_LITERAL };
assertArrayEquals(expected, actual);
assertArrayEquals("Acceptable tokens differ from expected",
expected, actual);
}

@Test
Expand Down
Expand Up @@ -57,7 +57,8 @@ protected DefaultConfiguration createCheckerConfig(
@Test
public void testGetRequiredTokens() {
final FileContentsHolder checkObj = new FileContentsHolder();
assertArrayEquals(CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
assertArrayEquals("Required tokens array is not empty",
CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
}

@Test
Expand Down
Expand Up @@ -157,7 +157,8 @@ public void testSetLineSeparatorFailure()
fail("exception expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith(
assertTrue("Error message is unexpected",
ex.getMessage().startsWith(
"cannot initialize module com.puppycrawl.tools.checkstyle."
+ "checks.NewlineAtEndOfFileCheck - "
+ "Cannot set property 'lineSeparator' to 'ct' in module"));
Expand Down Expand Up @@ -187,9 +188,11 @@ public void testWrongFile() throws Exception {
lines.add("txt");
final File impossibleFile = new File("");
final Set<LocalizedMessage> messages = check.process(impossibleFile, lines);
assertEquals(1, messages.size());
assertEquals("Ammount of messages is unexpected",
1, messages.size());
final Iterator<LocalizedMessage> iterator = messages.iterator();
assertEquals(getCheckMessage(MSG_KEY_UNABLE_OPEN, ""), iterator.next().getMessage());
assertEquals("Violation message differs from expected",
getCheckMessage(MSG_KEY_UNABLE_OPEN, ""), iterator.next().getMessage());
}

@Test
Expand All @@ -207,12 +210,15 @@ public void testWrongSeparatorLength() throws Exception {
method.invoke(new NewlineAtEndOfFileCheck(), file);
}
catch (InvocationTargetException ex) {
assertTrue(ex.getCause() instanceof IOException);
assertTrue("Error type is unexpected",
ex.getCause() instanceof IOException);
if (System.getProperty("os.name").toLowerCase(ENGLISH).startsWith("windows")) {
assertEquals("Unable to read 2 bytes, got 0", ex.getCause().getMessage());
assertEquals("Error message is unexpected",
"Unable to read 2 bytes, got 0", ex.getCause().getMessage());
}
else {
assertEquals("Unable to read 1 bytes, got 0", ex.getCause().getMessage());
assertEquals("Error message is unexpected",
"Unable to read 1 bytes, got 0", ex.getCause().getMessage());
}
}
}
Expand Down
Expand Up @@ -52,7 +52,8 @@ public void testGetRequiredTokens() {
TokenTypes.ENUM_DEF,
TokenTypes.ANNOTATION_DEF,
};
assertArrayEquals(expected, checkObj.getRequiredTokens());
assertArrayEquals("Required tokens array differs from expected",
expected, checkObj.getRequiredTokens());
}

@Test
Expand Down Expand Up @@ -81,7 +82,8 @@ public void testGetAcceptableTokens() {
TokenTypes.ENUM_DEF,
TokenTypes.ANNOTATION_DEF,
};
assertArrayEquals(expected, actual);
assertArrayEquals("Acceptable tokens array differs from expected",
expected, actual);
}

@Test
Expand Down
Expand Up @@ -68,7 +68,8 @@ protected String getNonCompilablePath(String filename) throws IOException {
public void testGetRequiredTokens() {
final SuppressWarningsHolder checkObj = new SuppressWarningsHolder();
final int[] expected = {TokenTypes.ANNOTATION};
assertArrayEquals(expected, checkObj.getRequiredTokens());
assertArrayEquals("Required token array differs from expected",
expected, checkObj.getRequiredTokens());
}

@Test
Expand All @@ -91,8 +92,10 @@ public void testCustomAnnotation() throws Exception {

@Test
public void testGetDefaultAlias() {
assertEquals("somename", SuppressWarningsHolder.getDefaultAlias("SomeName"));
assertEquals("somename", SuppressWarningsHolder.getDefaultAlias("SomeNameCheck"));
assertEquals("Diffault alias differs from expected",
"somename", SuppressWarningsHolder.getDefaultAlias("SomeName"));
assertEquals("Diffault alias differs from expected",
"somename", SuppressWarningsHolder.getDefaultAlias("SomeNameCheck"));
}

@Test
Expand All @@ -105,7 +108,8 @@ public void testSetAliasListEmpty() {
public void testSetAliasListCorrect() {
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
holder.setAliasList("alias=value");
assertEquals("value", SuppressWarningsHolder.getAlias("alias"));
assertEquals("Alias differs from expected",
"value", SuppressWarningsHolder.getAlias("alias"));
}

@Test
Expand All @@ -117,7 +121,8 @@ public void testSetAliasListWrong() {
fail("Exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("'=' expected in alias list item: SomeAlias", ex.getMessage());
assertEquals("Error message is unexpected",
"'=' expected in alias list item: SomeAlias", ex.getMessage());
}

}
Expand Down Expand Up @@ -148,7 +153,7 @@ public void testIsSuppressed() throws Exception {
new LocalizedMessage(100, 10, null, null, null, "id", MemberNameCheck.class, "message");
final AuditEvent event = new AuditEvent(source, "fileName", message);

assertFalse(SuppressWarningsHolder.isSuppressed(event));
assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}

@Test
Expand Down Expand Up @@ -177,19 +182,22 @@ public void testIsSuppressedWithAllArgument() throws Exception {
new LocalizedMessage(100, 10, null, null, null, "id", MemberNameCheck.class, "msg");
final AuditEvent firstEventForTest =
new AuditEvent(source, "fileName", firstMessageForTest);
assertFalse(SuppressWarningsHolder.isSuppressed(firstEventForTest));
assertFalse("Event is suppressed",
SuppressWarningsHolder.isSuppressed(firstEventForTest));

final LocalizedMessage secondMessageForTest =
new LocalizedMessage(100, 150, null, null, null, "id", MemberNameCheck.class, "msg");
final AuditEvent secondEventForTest =
new AuditEvent(source, "fileName", secondMessageForTest);
assertTrue(SuppressWarningsHolder.isSuppressed(secondEventForTest));
assertTrue("Event is not suppressed",
SuppressWarningsHolder.isSuppressed(secondEventForTest));

final LocalizedMessage thirdMessageForTest =
new LocalizedMessage(200, 1, null, null, null, "id", MemberNameCheck.class, "msg");
final AuditEvent thirdEventForTest =
new AuditEvent(source, "fileName", thirdMessageForTest);
assertTrue(SuppressWarningsHolder.isSuppressed(thirdEventForTest));
assertTrue("Event is not suppressed",
SuppressWarningsHolder.isSuppressed(thirdEventForTest));
}

@Test
Expand Down Expand Up @@ -237,8 +245,10 @@ public void testGetAllAnnotationValuesWrongArg() throws ReflectiveOperationExcep
fail("Exception expected");
}
catch (InvocationTargetException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
assertEquals("Unexpected AST: Method Def[0x0]", ex.getCause().getMessage());
assertTrue("Error type is unexpected",
ex.getCause() instanceof IllegalArgumentException);
assertEquals("Error message is unexpected",
"Unexpected AST: Method Def[0x0]", ex.getCause().getMessage());
}
}

Expand All @@ -260,8 +270,10 @@ public void testGetAnnotationValuesWrongArg() throws ReflectiveOperationExceptio
fail("Exception expected");
}
catch (InvocationTargetException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
assertEquals("Expression or annotation array"
assertTrue("Error type is unexpected",
ex.getCause() instanceof IllegalArgumentException);
assertEquals("Error message is unexpected",
"Expression or annotation array"
+ " initializer AST expected: Method Def[0x0]", ex.getCause().getMessage());
}
}
Expand Down Expand Up @@ -289,8 +301,10 @@ public void testGetAnnotationTargetWrongArg() throws ReflectiveOperationExceptio
fail("Exception expected");
}
catch (InvocationTargetException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
assertEquals("Unexpected container AST: Parent ast[0x0]", ex.getCause().getMessage());
assertTrue("Error type is unexpected",
ex.getCause() instanceof IllegalArgumentException);
assertEquals("Error message is unexpected",
"Unexpected container AST: Parent ast[0x0]", ex.getCause().getMessage());
}
}

Expand All @@ -305,7 +319,8 @@ public void testAstWithoutChildren() {
fail("Exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Identifier AST expected, but get null.", ex.getMessage());
assertEquals("Error message is unexpected",
"Identifier AST expected, but get null.", ex.getMessage());
}

}
Expand Down

0 comments on commit 8dec45a

Please sign in to comment.