Skip to content

Commit

Permalink
Fix NPE in QueryAssertions
Browse files Browse the repository at this point in the history
Before this change, NullPointerException was thrown in QueryAssertions
when formatting error message for mismatched query results.
If either the expected or the actual result contained a MaterializedRow
having a null field, NPE was caused by calling `toString()` on null.
  • Loading branch information
kasiafi authored and kokosing committed Oct 16, 2020
1 parent e87933e commit 36d2ab3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public String toStringOf(Object object)
MaterializedRow row = (MaterializedRow) object;

return row.getFields().stream()
.map(Object::toString)
.map(String::valueOf)
.collect(Collectors.joining(", ", "(", ")"));
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.prestosql.Session;
import io.prestosql.testing.LocalQueryRunner;
import io.prestosql.testing.QueryRunner;
import org.testng.annotations.Test;

import static io.airlift.testing.Closeables.closeAllSuppress;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -62,4 +63,30 @@ public void testIsFullyPushedDownWithSession()
.isInstanceOf(IllegalStateException.class)
.hasMessage("testIsFullyPushedDown() currently does not work with LocalQueryRunner");
}

@Test
public void testNullInErrorMessage()
{
assertThatThrownBy(() -> assertThat(query("SELECT CAST(null AS integer)")).matches("SELECT 1"))
.hasMessage("[Rows] \n" +
"Expecting:\n" +
" <(null)>\n" +
"to contain exactly in any order:\n" +
" <[(1)]>\n" +
"elements not found:\n" +
" <(1)>\n" +
"and elements not expected:\n" +
" <(null)>\n");

assertThatThrownBy(() -> assertThat(query("SELECT 1")).matches("SELECT CAST(null AS integer)"))
.hasMessage("[Rows] \n" +
"Expecting:\n" +
" <(1)>\n" +
"to contain exactly in any order:\n" +
" <[(null)]>\n" +
"elements not found:\n" +
" <(null)>\n" +
"and elements not expected:\n" +
" <(1)>\n");
}
}

0 comments on commit 36d2ab3

Please sign in to comment.