### Question:
FilterParser { public Node parse(String filterString) throws Exception { if (filterString == null) { throw new FilterStringSyntaxException("filter parsing ended with no result"); } return parse(filterString.getBytes(DEFAULT_CHARSET)); } FilterParser(); Node parse(String filterString); static final String DEFAULT_CHARSET; }### Answer:
@Test public void testNullFilterString() throws Exception { thrown.expect(FilterParser.FilterStringSyntaxException.class); thrown.expectMessage("filter parsing ended with no result"); filterParser.parse(null); }
@Test public void testLongIndexFails() throws Exception { thrown.expect(FilterParser.FilterStringSyntaxException.class); thrown.expectMessage("value 2147483648 larger than intmax ending at 11"); filter = "a2147483647o8"; filterParser.parse(filter); filter = "a2147483648o8"; filterParser.parse(filter); }
@Test public void parseFilterWith2Operations() throws Exception { filter = "a1c25s5dfirsto5a2c20s1d1o2l0"; Node result = filterParser.parse(filter); assertNotNull(result); assertOperatorEquals(AND, result); assertEquals(2, result.childCount()); assertOperatorEquals(EQUALS, result.getLeft()); assertOperatorEquals(GREATER_THAN, result.getRight()); }
@Test public void parseLogicalAndOperator() throws Exception { filter = "a1c20s1d0o5a2c20s1d3o2l0"; Node result = filterParser.parse(filter); assertNotNull(result); assertOperatorEquals(AND, result); assertEquals(2, result.childCount()); assertOperatorEquals(EQUALS, result.getLeft()); assertOperatorEquals(GREATER_THAN, result.getRight()); }
@Test public void parseLogicalOrOperator() throws Exception { filter = "a1c20s1d0o5a2c20s1d3o2l1"; Node result = filterParser.parse(filter); assertNotNull(result); assertOperatorEquals(OR, result); assertEquals(2, result.childCount()); assertOperatorEquals(EQUALS, result.getLeft()); assertOperatorEquals(GREATER_THAN, result.getRight()); assertTrue(result.getLeft().getLeft() instanceof ColumnIndexOperandNode); assertEquals(1, ((ColumnIndexOperandNode) result.getLeft().getLeft()).index()); assertTrue(result.getLeft().getRight() instanceof ScalarOperandNode); assertEquals("0", ((ScalarOperandNode) result.getLeft().getRight()).getValue()); assertTrue(result.getRight().getLeft() instanceof ColumnIndexOperandNode); assertEquals(2, ((ColumnIndexOperandNode) result.getRight().getLeft()).index()); assertTrue(result.getRight().getRight() instanceof ScalarOperandNode); assertEquals("3", ((ScalarOperandNode) result.getRight().getRight()).getValue()); }
@Test public void parseLogicalNotOperator() throws Exception { filter = "a1c20s1d0o5l2"; Node result = filterParser.parse(filter); assertNotNull(result); assertOperatorEquals(Operator.NOT, result); assertEquals(1, result.childCount()); assertOperatorEquals(Operator.EQUALS, result.getLeft()); }
@Test public void parseLogicalUnknownCodeError() throws Exception { thrown.expect(FilterParser.FilterStringSyntaxException.class); thrown.expectMessage("unknown op ending at 2"); filter = "l7"; filterParser.parse(filter); }
@Test public void parseLogicalOperatorNotExpression() throws Exception { filter = "a1c25s5dfirsto5a2c20s1d2o2l0l2"; Node result = filterParser.parse(filter); assertOperatorEquals(NOT, result); assertEquals(1, result.childCount()); assertOperatorEquals(AND, result.getLeft()); assertEquals(2, result.getLeft().childCount()); assertOperatorEquals(EQUALS, result.getLeft().getLeft()); assertOperatorEquals(GREATER_THAN, result.getLeft().getRight()); }