KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > selector > SelectorParserTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 /**
31  * @version $Revision: 1.2 $
32  */

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 JavaDoc {
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 JavaDoc {
44         String JavaDoc[] 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 JavaDoc 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 JavaDoc message, Expression expression, String JavaDoc 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 JavaDoc text) throws Exception JavaDoc {
72         return new SelectorParser().parse(text);
73     }
74 }
75
Popular Tags