1 18 package org.apache.activemq.selector; 19 20 import junit.framework.TestCase; 21 22 import org.apache.activemq.filter.BooleanExpression; 23 import org.apache.activemq.filter.ComparisonExpression; 24 import org.apache.activemq.filter.Expression; 25 import org.apache.activemq.filter.LogicExpression; 26 import org.apache.activemq.filter.PropertyExpression; 27 import org.apache.activemq.filter.XPathExpression; 28 import org.apache.activemq.selector.SelectorParser; 29 30 33 public class SelectorParserTest extends TestCase { 34 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 35 .getLog(SelectorParserTest.class); 36 37 public void testParseXPath() throws Exception { 38 BooleanExpression filter = parse("XPATH '//title[@lang=''eng'']'"); 39 assertTrue("Created XPath expression", filter instanceof XPathExpression); 40 log.info("Expression: "+filter); 41 } 42 43 public void testParseWithParensAround() throws Exception { 44 String [] values = {"x = 1 and y = 2", "(x = 1) and (y = 2)", "((x = 1) and (y = 2))"}; 45 46 for (int i = 0; i < values.length; i++) { 47 String value = values[i]; 48 log.info("Parsing: " + value); 49 50 BooleanExpression andExpression = parse(value); 51 assertTrue("Created LogicExpression expression", andExpression instanceof LogicExpression); 52 LogicExpression logicExpression = (LogicExpression) andExpression; 53 Expression left = logicExpression.getLeft(); 54 Expression right = logicExpression.getRight(); 55 56 assertTrue("Left is a binary filter", left instanceof ComparisonExpression); 57 assertTrue("Right is a binary filter", right instanceof ComparisonExpression); 58 ComparisonExpression leftCompare = (ComparisonExpression) left; 59 ComparisonExpression rightCompare = (ComparisonExpression) right; 60 assertPropertyExpression("left", leftCompare.getLeft(), "x"); 61 assertPropertyExpression("right", rightCompare.getLeft(), "y"); 62 } 63 } 64 65 protected void assertPropertyExpression(String message, Expression expression, String expected) { 66 assertTrue(message + ". Must be PropertyExpression", expression instanceof PropertyExpression); 67 PropertyExpression propExp = (PropertyExpression) expression; 68 assertEquals(message + ". Property name", expected, propExp.getName()); 69 } 70 71 protected BooleanExpression parse(String text) throws Exception { 72 return new SelectorParser().parse(text); 73 } 74 } 75 | Popular Tags |