[java] Fix NPE in JUnitTestsShouldIncludeAssert

- Fixes #330
This commit is contained in:
Juan Martín Sotuyo Dodero
2017-04-15 10:40:13 -03:00
parent 24a648e032
commit 6a3b509feb
2 changed files with 24 additions and 11 deletions

View File

@ -124,10 +124,13 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
if (expression != null) {
ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
if (pe != null) {
String img = pe.jjtGetChild(0).jjtGetChild(0).getImage();
if (img != null && (img.startsWith("assert") || img.startsWith("fail")
|| img.startsWith("Assert.assert") || img.startsWith("Assert.fail"))) {
return true;
Node name = pe.getFirstDescendantOfType(ASTName.class);
if (name != null) {
String img = name.getImage();
if (img != null && (img.startsWith("assert") || img.startsWith("fail")
|| img.startsWith("Assert.assert") || img.startsWith("Assert.fail"))) {
return true;
}
}
}
}
@ -141,14 +144,13 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
if (pe != null) {
Node subChild = pe.jjtGetChild(0).jjtGetChild(0);
// case of eg AllocationExpression
if (!(subChild instanceof ASTName)) {
Node name = pe.getFirstDescendantOfType(ASTName.class);
// case of an AllocationExpression
if (name == null) {
return false;
}
String img = subChild.getImage();
String img = name.getImage();
if (img.indexOf(".") == -1) {
return false;
}
@ -159,7 +161,7 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule {
}
for (NameOccurrence occ : expectables.get(varname)) {
if (occ.getLocation() == pe.jjtGetChild(0).jjtGetChild(0) && img.startsWith(varname + ".expect")) {
if (occ.getLocation() == name && img.startsWith(varname + ".expect")) {
return true;
}
}

View File

@ -328,5 +328,16 @@ public class BaseConsoleTest extends UART {
}
]]></code>
</test-code>
<test-code>
<description>#330 NPE accessing local field / method with this.XXX</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
@Test
public void testName() {
this.field = null;
}
}
]]></code>
</test-code>
</test-data>