Skip to content

Commit

Permalink
SQL: Fix BasicFormatter NPE (elastic#37804)
Browse files Browse the repository at this point in the history
  • Loading branch information
astefan committed Jan 24, 2019
1 parent 7517e3a commit 163a27b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum FormatOption {
private final Function<Object, String> apply;

FormatOption(Function<Object, String> apply) {
this.apply = l -> l == null ? null : apply.apply(l);
this.apply = apply;
}

public final String apply(Object l) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public class BasicFormatterTests extends ESTestCase {
new ColumnInfo("", "foo", "string", 0),
new ColumnInfo("", "bar", "long", 15),
new ColumnInfo("", "15charwidename!", "double", 25),
new ColumnInfo("", "null_field1", "integer", 0),
new ColumnInfo("", "superduperwidename!!!", "double", 25),
new ColumnInfo("", "baz", "keyword", 0),
new ColumnInfo("", "date", "datetime", 24)),
new ColumnInfo("", "date", "datetime", 24),
new ColumnInfo("", "null_field2", "keyword", 0)),
Arrays.asList(
Arrays.asList("15charwidedata!", 1, 6.888, 12, "rabbit", "1953-09-02T00:00:00.000Z"),
Arrays.asList("dog", 1.7976931348623157E308, 123124.888, 9912, "goat", "2000-03-15T21:34:37.443Z")));
Arrays.asList("15charwidedata!", 1, 6.888, null, 12, "rabbit", "1953-09-02T00:00:00.000Z", null),
Arrays.asList("dog", 1.7976931348623157E308, 123124.888, null, 9912, "goat", "2000-03-15T21:34:37.443Z", null)));
private final BasicFormatter formatter = new BasicFormatter(firstResponse.columns(), firstResponse.rows(), format);

/**
Expand All @@ -40,14 +42,14 @@ public class BasicFormatterTests extends ESTestCase {
public void testFormatWithHeader() {
String[] result = formatter.formatWithHeader(firstResponse.columns(), firstResponse.rows()).split("\n");
assertThat(result, arrayWithSize(4));
assertEquals(" foo | bar |15charwidename!|superduperwidename!!!| baz |"
+ " date ", result[0]);
assertEquals("---------------+----------------------+---------------+---------------------+---------------+"
+ "------------------------", result[1]);
assertEquals("15charwidedata!|1 |6.888 |12 |rabbit |"
+ "1953-09-02T00:00:00.000Z", result[2]);
assertEquals("dog |1.7976931348623157E308|123124.888 |9912 |goat |"
+ "2000-03-15T21:34:37.443Z", result[3]);
assertEquals(" foo | bar |15charwidename!| null_field1 |superduperwidename!!!| baz |"
+ " date | null_field2 ", result[0]);
assertEquals("---------------+----------------------+---------------+---------------+---------------------+---------------+"
+ "------------------------+---------------", result[1]);
assertEquals("15charwidedata!|1 |6.888 |null |12 |rabbit |"
+ "1953-09-02T00:00:00.000Z|null ", result[2]);
assertEquals("dog |1.7976931348623157E308|123124.888 |null |9912 |goat |"
+ "2000-03-15T21:34:37.443Z|null ", result[3]);
}

/**
Expand All @@ -57,13 +59,13 @@ public void testFormatWithHeader() {
public void testFormatWithoutHeader() {
String[] result = formatter.formatWithoutHeader(
Arrays.asList(
Arrays.asList("ohnotruncateddata", 4, 1, 77, "wombat", "1955-01-21T01:02:03.342Z"),
Arrays.asList("dog", 2, 123124.888, 9912, "goat", "2231-12-31T23:59:59.999Z"))).split("\n");
Arrays.asList("ohnotruncateddata", 4, 1, null, 77, "wombat", "1955-01-21T01:02:03.342Z", null),
Arrays.asList("dog", 2, 123124.888, null, 9912, "goat", "2231-12-31T23:59:59.999Z", null))).split("\n");
assertThat(result, arrayWithSize(2));
assertEquals("ohnotruncatedd~|4 |1 |77 |wombat |"
+ "1955-01-21T01:02:03.342Z", result[0]);
assertEquals("dog |2 |123124.888 |9912 |goat |"
+ "2231-12-31T23:59:59.999Z", result[1]);
assertEquals("ohnotruncatedd~|4 |1 |null |77 |wombat |"
+ "1955-01-21T01:02:03.342Z|null ", result[0]);
assertEquals("dog |2 |123124.888 |null |9912 |goat |"
+ "2231-12-31T23:59:59.999Z|null ", result[1]);
}

/**
Expand Down

0 comments on commit 163a27b

Please sign in to comment.