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

View File

@ -328,5 +328,16 @@ public class BaseConsoleTest extends UART {
} }
]]></code> ]]></code>
</test-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> </test-data>