Skip to content

Commit

Permalink
Added unit tests for errorListener.isErrorFound
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Bayer <[email protected]>
  • Loading branch information
sbayer55 committed Feb 25, 2022
1 parent 30bde1e commit 7192f19
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CompositeException extends RuntimeException {

public CompositeException(final List<Throwable> exceptions) {
if (exceptions.isEmpty()) {
throw new IllegalArgumentException("errors is empty");
throw new IllegalArgumentException("exceptions is empty");
}

this.exceptions = exceptions.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ public ParseTreeParser(final DataPrepperExpressionParser parser, final ParserErr
/**
* @since 1.3
*
* Converts an expression to a parse tree base on grammar rules
* Converts an expression to a parse tree base on grammar rules. {@link ParserErrorListener#resetErrors()} must be called
* before {@link DataPrepperExpressionParser#expression()} to prevent duplicate errors from being reported.
*
* @param expression String to be parsed
* @return ParseTree data structure containing a hierarchy of the tokens found while parsing.
* @throws CompositeException thrown when ANTLR parser creates an exception event
*/
private ParseTree createParseTree(final String expression) throws CompositeException {
errorListener.resetErrors();

final IntStream input = CharStreams.fromString(expression);
lexer.setInputStream(input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ void testGivenSingleExceptionThenExceptionIsCause() {
}

@Test
void foo() throws CompositeException {
void testMultipleExceptionsPrinted() throws CompositeException {
final CompositeException compositeException = new CompositeException(Arrays.asList(
new RuntimeException("Error"),
new RuntimeException("Error 2"),
new RuntimeException("Error 3")
new RuntimeException("Error 3"),
null
));

assertThat(compositeException.getCause() instanceof ExceptionOverview, is(true));

final String message = compositeException.getCause().getMessage();
assertThat(message, containsString("Multiple exceptions (3)"));
assertThat(message, containsString("Multiple exceptions (4)"));
assertThat(message, containsString("|-- java.lang.RuntimeException: Error 3"));
assertThat(message, containsString("|-- java.lang.RuntimeException: Error 2"));
assertThat(message, containsString("|-- java.lang.RuntimeException: Error"));
assertThat(message, containsString("|-- java.lang.NullPointerException: Throwable was null!"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -81,6 +82,8 @@ void testCacheGivenMultipleExpressionCalls() throws CompositeException {
parseTree = parseTreeParser.parse(VALID_STATEMENT);
assertThat(parseTree, is(expected));

verify(errorListener).isErrorFound();

// Verify parser.expression() called 1 time
verify(parser).expression();
}
Expand All @@ -94,4 +97,18 @@ void testExceptionThrowWhenParsingErrorPresent() {
assertThrows(CompositeException.class, () -> parseTreeParser.parse("Error should throw"));
}

@Test
void testResetErrorsIsCalled() throws CompositeException {
final ParseTree expected = mock(DataPrepperExpressionParser.ExpressionContext.class);
doReturn(expected).when(parser).expression();

ParseTree parseTree = parseTreeParser.parse(VALID_STATEMENT);
assertThat(parseTree, is(expected));
verify(errorListener).isErrorFound();

parseTree = parseTreeParser.parse("true == true");
assertThat(parseTree, is(expected));
verify(errorListener, times(2)).isErrorFound();
}

}

0 comments on commit 7192f19

Please sign in to comment.