Skip to content

Commit

Permalink
Fix NPE in DataFormatMatcher#getMatchedFormatName when no match exists (
Browse files Browse the repository at this point in the history
#591)

Change DataFormatMatcher#getMatchedFormatName so it does what the Javadoc says to avoid NPE for non-match; add unit tests to verify.
  • Loading branch information
sleberknight authored and cowtowncoder committed Jan 10, 2020
1 parent dc9c40c commit e9998bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public MatchStrength getMatchStrength() {
*</pre>
*/
public String getMatchedFormatName() {
return _match.getFormatName();
return hasMatch() ? getMatch().getFormatName() : null;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@ public void testCreatesDataFormatMatcherTwo() throws IOException {
verifyException(e, "Illegal start/length");
}
}

public void testGetMatchedFormatNameReturnsNameWhenMatches() {
DataFormatMatcher dataFormatMatcher = new DataFormatMatcher(null,
new byte[2],
1,
0,
new JsonFactory(),
MatchStrength.SOLID_MATCH);
assertEquals(JsonFactory.FORMAT_NAME_JSON, dataFormatMatcher.getMatchedFormatName());
}

public void testGetMatchedFormatNameReturnsNullWhenNoMatch() {
DataFormatMatcher dataFormatMatcher = new DataFormatMatcher(null,
new byte[2],
1,
0,
null,
MatchStrength.NO_MATCH);
assertNull(dataFormatMatcher.getMatchedFormatName());
}
}

0 comments on commit e9998bf

Please sign in to comment.