1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.selector;
19  
20  import junit.framework.TestCase;
21  import org.codehaus.activemq.filter.ComparisonExpression;
22  import org.codehaus.activemq.filter.Expression;
23  import org.codehaus.activemq.filter.ExpressionFilter;
24  import org.codehaus.activemq.filter.Filter;
25  import org.codehaus.activemq.filter.LogicExpression;
26  import org.codehaus.activemq.filter.PropertyExpression;
27  
28  /***
29   * @version $Revision: 1.4 $
30   */
31  public class SelectorParserTest extends TestCase {
32  
33      public void testParseWithParensAround() throws Exception {
34          String[] values = {"x = 1 and y = 2", "(x = 1) and (y = 2)", "((x = 1) and (y = 2))"};
35  
36          for (int i = 0; i < values.length; i++) {
37              String value = values[i];
38              System.out.println("Parsing: " + value);
39  
40              Filter filter = parse(value);
41              assertTrue("Created ExpressionFilter filter", filter instanceof ExpressionFilter);
42              Expression andExpression = ((ExpressionFilter) filter).getExpression();
43              assertTrue("Created LogicExpression expression", andExpression instanceof LogicExpression);
44              LogicExpression logicExpression = (LogicExpression) andExpression;
45              Expression left = logicExpression.getLeft();
46              Expression right = logicExpression.getRight();
47  
48              assertTrue("Left is a binary filter", left instanceof ComparisonExpression);
49              assertTrue("Right is a binary filter", right instanceof ComparisonExpression);
50              ComparisonExpression leftCompare = (ComparisonExpression) left;
51              ComparisonExpression rightCompare = (ComparisonExpression) right;
52              assertPropertyExpression("left", leftCompare.getLeft(), "x");
53              assertPropertyExpression("right", rightCompare.getLeft(), "y");
54          }
55      }
56  
57      protected void assertPropertyExpression(String message, Expression expression, String expected) {
58          assertTrue(message + ". Must be PropertyExpression", expression instanceof PropertyExpression);
59          PropertyExpression propExp = (PropertyExpression) expression;
60          assertEquals(message + ". Property name", expected, propExp.getName());
61      }
62  
63      protected Filter parse(String text) throws Exception {
64          return new SelectorParser().parse(text);
65      }
66  }