Skip to content

Commit 353db30

Browse files
committed
Fix NPE caused by bad scan() in TreeDiffer.java
Old scan() ignored the possibility of an expected node having a null value when an actual node existed. The new scan() accounts for this possibility and also throws a more relevant NPE should another similar situation arise. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=68173457
1 parent 6f2ca45 commit 353db30

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/main/java/com/google/testing/compile/TreeDiffer.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ private void checkForDiff(boolean p, String message, Object... formatArgs) {
181181
}
182182

183183
private TreePath actualPathPlus(Tree actual) {
184+
checkNotNull(actual, "Tried to push null actual tree onto path.");
184185
return new TreePath(actualPath, actual);
185186
}
186187

187188
private TreePath expectedPathPlus(Tree expected) {
189+
checkNotNull(expected, "Tried to push null expected tree onto path.");
188190
return new TreePath(expectedPath, expected);
189191
}
190192

@@ -209,13 +211,14 @@ private Void pushPathAndAccept(Tree expected, Tree actual) {
209211
}
210212

211213
public Void scan(@Nullable Tree expected, @Nullable Tree actual) {
212-
if (expected == null) {
213-
if (actual != null) {
214-
diffBuilder.addExtraActualNode(actualPathPlus(actual));
215-
}
216-
return null;
214+
if (expected == null && actual != null) {
215+
diffBuilder.addExtraActualNode(actualPathPlus(actual));
216+
} else if (expected != null && actual == null) {
217+
diffBuilder.addExtraExpectedNode(expectedPathPlus(expected));
218+
} else if (actual != null && expected != null) {
219+
pushPathAndAccept(expected, actual);
217220
}
218-
return pushPathAndAccept(expected, actual);
221+
return null;
219222
}
220223

221224
private Void parallelScan(Iterable<? extends Tree> expecteds,

src/test/java/com/google/testing/compile/TreeDifferTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public class TreeDifferTest {
5656
MoreTrees.parseLinesToTree("package test;",
5757
"import java.util.List;",
5858
"",
59-
"",
6059
"final class TestClass {",
6160
" public String toString() {",
6261
" Object variable = new Object();",
@@ -71,11 +70,24 @@ public class TreeDifferTest {
7170
" }",
7271
" }",
7372
" }",
74-
"",
7573
" public int extraNonsense() {",
7674
" return 0;",
7775
" }",
7876
"}");
77+
private static final CompilationUnitTree ASSERT_TREE_WITH_MESSAGE =
78+
MoreTrees.parseLinesToTree("package test;",
79+
"final class TestClass {",
80+
" private void fail() {",
81+
" assert false : \"details\";",
82+
" }",
83+
"}");
84+
private static final CompilationUnitTree ASSERT_TREE_WITHOUT_MESSAGE =
85+
MoreTrees.parseLinesToTree("package test;",
86+
"final class TestClass {",
87+
" private void fail() {",
88+
" assert false;",
89+
" }",
90+
"}");
7991

8092
@Test
8193
public void scan_differingCompilationUnits() {
@@ -108,7 +120,15 @@ public void scan_differingCompilationUnits() {
108120
differingNodesFound.add(SimplifiedDiff.create(differingNode));
109121
}
110122
ASSERT.that(differingNodesExpected).iteratesAs(differingNodesFound.build());
123+
}
111124

125+
@Test
126+
public void scan_testExtraFields() {
127+
TreeDifference diff =
128+
TreeDiffer.diffCompilationUnits(ASSERT_TREE_WITH_MESSAGE, ASSERT_TREE_WITHOUT_MESSAGE);
129+
ASSERT.that(diff.isEmpty()).isFalse();
130+
diff = TreeDiffer.diffCompilationUnits(ASSERT_TREE_WITHOUT_MESSAGE, ASSERT_TREE_WITH_MESSAGE);
131+
ASSERT.that(diff.isEmpty()).isFalse();
112132
}
113133

114134
@Test

0 commit comments

Comments
 (0)