KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > SelectorParserUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmq.test;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.jboss.mq.selectors.ISelectorParser;
28 import org.jboss.mq.selectors.Identifier;
29 import org.jboss.mq.selectors.Operator;
30 import org.jboss.mq.selectors.SelectorParser;
31 import org.jboss.test.JBossTestCase;
32
33 /** Tests of the JavaCC LL(1) parser.
34  
35  @author Scott.Stark@jboss.org
36  @author d_jencks@users.sourceforge.net
37  
38  @version $Revision: 37406 $
39  
40  * (david jencks) Used constructor of SelectorParser taking a stream
41  * to avoid reInit npe in all tests. Changed to JBossTestCase and logging.
42  */

43 public class SelectorParserUnitTestCase extends JBossTestCase
44 {
45    static HashMap JavaDoc identifierMap = new HashMap JavaDoc();
46    static ISelectorParser parser;
47    
48    public SelectorParserUnitTestCase(String JavaDoc name)
49    {
50       super(name);
51    }
52    
53    protected void setUp() throws Exception JavaDoc
54    {
55       super.setUp();
56       identifierMap.clear();
57       if( parser == null )
58       {
59          parser = new SelectorParser(new ByteArrayInputStream JavaDoc(new byte[0]));
60       }
61    }
62  
63    public void testSimpleUnary() throws Exception JavaDoc
64    {
65       // Neg Long
66
getLog().debug("parse(-12345 = -1 * 12345)");
67       Operator result = (Operator) parser.parse("-12345 = -1 * 12345", identifierMap);
68       getLog().debug("result -> "+result);
69       Boolean JavaDoc b = (Boolean JavaDoc) result.apply();
70       assertTrue("is true", b.booleanValue());
71
72       // Neg Double
73
getLog().debug("parse(-1 * 12345.67 = -12345.67)");
74       result = (Operator) parser.parse("-1 * 12345.67 = -12345.67", identifierMap);
75       getLog().debug("result -> "+result);
76       b = (Boolean JavaDoc) result.apply();
77       assertTrue("is true", b.booleanValue());
78
79       getLog().debug("parse(-(1 * 12345.67) = -12345.67)");
80       result = (Operator) parser.parse("-(1 * 12345.67) = -12345.67", identifierMap);
81       getLog().debug("result -> "+result);
82       b = (Boolean JavaDoc) result.apply();
83       assertTrue("is true", b.booleanValue());
84    }
85    
86    public void testPrecedenceNAssoc() throws Exception JavaDoc
87    {
88       getLog().debug("parse(4 + 2 * 3 / 2 = 7)");
89       Operator result = (Operator) parser.parse("4 + 2 * 3 / 2 = 7", identifierMap);
90       getLog().debug("result -> "+result);
91       Boolean JavaDoc b = (Boolean JavaDoc) result.apply();
92       assertTrue("is true", b.booleanValue());
93       
94       getLog().debug("parse(4 + ((2 * 3) / 2) = 7)");
95       result = (Operator) parser.parse("4 + ((2 * 3) / 2) = 7", identifierMap);
96       getLog().debug("result -> "+result);
97       b = (Boolean JavaDoc) result.apply();
98       assertTrue("is true", b.booleanValue());
99       
100       getLog().debug("parse(4 * -2 / -1 - 4 = 4)");
101       result = (Operator) parser.parse("4 * -2 / -1 - 4 = 4", identifierMap);
102       getLog().debug("result -> "+result);
103       b = (Boolean JavaDoc) result.apply();
104       assertTrue("is true", b.booleanValue());
105       
106       getLog().debug("parse(4 * ((-2 / -1) - 4) = -8)");
107       result = (Operator) parser.parse("4 * ((-2 / -1) - 4) = -8", identifierMap);
108       getLog().debug("result -> "+result);
109       b = (Boolean JavaDoc) result.apply();
110       assertTrue("is true", b.booleanValue());
111    }
112    
113    public void testIds() throws Exception JavaDoc
114    {
115       getLog().debug("parse(a + b * c / d = e)");
116       Operator result = (Operator) parser.parse("a + b * c / d = e", identifierMap);
117       // 4 + 2 * 3 / 2 = 7
118
Identifier a = (Identifier) identifierMap.get("a");
119       a.setValue(new Long JavaDoc(4));
120       Identifier b = (Identifier) identifierMap.get("b");
121       b.setValue(new Long JavaDoc(2));
122       Identifier c = (Identifier) identifierMap.get("c");
123       c.setValue(new Long JavaDoc(3));
124       Identifier d = (Identifier) identifierMap.get("d");
125       d.setValue(new Long JavaDoc(2));
126       Identifier e = (Identifier) identifierMap.get("e");
127       e.setValue(new Long JavaDoc(7));
128       getLog().debug("result -> "+result);
129       Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
130       assertTrue("is true", bool.booleanValue());
131       
132    }
133    
134    public void testTrueINOperator() throws Exception JavaDoc
135    {
136       getLog().debug("parse(Status IN ('new', 'cleared', 'acknowledged'))");
137       Operator result = (Operator) parser.parse("Status IN ('new', 'cleared', 'acknowledged')", identifierMap);
138       Identifier a = (Identifier) identifierMap.get("Status");
139       a.setValue("new");
140       getLog().debug("result -> "+result);
141       Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
142       assertTrue("is true", bool.booleanValue());
143    }
144    public void testFalseINOperator() throws Exception JavaDoc
145    {
146       getLog().debug("parse(Status IN ('new', 'cleared', 'acknowledged'))");
147       Operator result = (Operator) parser.parse("Status IN ('new', 'cleared', 'acknowledged')", identifierMap);
148       Identifier a = (Identifier) identifierMap.get("Status");
149       a.setValue("none");
150       getLog().debug("result -> "+result);
151       Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
152       assertTrue("is false", !bool.booleanValue());
153    }
154    
155    public void testTrueOROperator() throws Exception JavaDoc
156    {
157       getLog().debug("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))");
158       Operator result = (Operator) parser.parse("(Status = 'new') OR (Status = 'cleared') OR (Status= 'acknowledged')", identifierMap);
159       Identifier a = (Identifier) identifierMap.get("Status");
160       a.setValue("new");
161       getLog().debug("result -> "+result);
162       Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
163       assertTrue("is true", bool.booleanValue());
164    }
165    public void testFalseOROperator() throws Exception JavaDoc
166    {
167       getLog().debug("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))");
168       Operator result = (Operator) parser.parse("(Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged')", identifierMap);
169       Identifier a = (Identifier) identifierMap.get("Status");
170       a.setValue("none");
171       getLog().debug("result -> "+result);
172       Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
173       assertTrue("is false", !bool.booleanValue());
174    }
175    
176    public void testInvalidSelector() throws Exception JavaDoc
177    {
178       getLog().debug("parse(definitely not a message selector!)");
179       try
180       {
181          Object JavaDoc result = parser.parse("definitely not a message selector!", identifierMap);
182          getLog().debug("result -> "+result);
183          fail("Should throw an Exception.\n");
184       }
185       catch (Exception JavaDoc e)
186       {
187          getLog().info("testInvalidSelector failed as expected", e);
188       }
189    }
190  
191    /**
192     * Test diffent syntax for approximate numeric literal (+6.2, -95.7, 7.)
193     */

194    public void testApproximateNumericLiteral1()
195    {
196       try
197       {
198          getLog().debug("parse(average = +6.2)");
199          Object JavaDoc result = parser.parse("average = +6.2", identifierMap);
200          getLog().debug("result -> "+result);
201       } catch (Exception JavaDoc e)
202       {
203          fail(""+e);
204       }
205    }
206    
207    public void testApproximateNumericLiteral2()
208    {
209       try
210       {
211          getLog().debug("parse(average = -95.7)");
212          Object JavaDoc result = parser.parse("average = -95.7", identifierMap);
213          getLog().debug("result -> "+result);
214       } catch (Exception JavaDoc e)
215       {
216          fail(""+e);
217       }
218    }
219    public void testApproximateNumericLiteral3()
220    {
221       try
222       {
223          getLog().debug("parse(average = 7.)");
224          Object JavaDoc result = parser.parse("average = 7.", identifierMap);
225          getLog().debug("result -> "+result);
226       } catch (Exception JavaDoc e)
227       {
228          fail(""+e);
229       }
230    }
231    
232    public void testGTExact()
233    {
234       try
235       {
236          getLog().debug("parse(weight > 2500)");
237          Operator result = (Operator)parser.parse("weight > 2500", identifierMap);
238          ((Identifier) identifierMap.get("weight")).setValue(new Integer JavaDoc(3000));
239          getLog().debug("result -> "+result);
240          Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
241          assertTrue("is true", bool.booleanValue());
242       } catch (Exception JavaDoc e)
243       {
244          getLog().debug("failed", e);
245          fail(""+e);
246       }
247    }
248
249    public void testGTFloat()
250    {
251       try
252       {
253          getLog().debug("parse(weight > 2500)");
254          Operator result = (Operator)parser.parse("weight > 2500", identifierMap);
255          ((Identifier) identifierMap.get("weight")).setValue(new Float JavaDoc(3000));
256          getLog().debug("result -> "+result);
257          Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
258          assertTrue("is true", bool.booleanValue());
259       } catch (Exception JavaDoc e)
260       {
261          getLog().debug("failed", e);
262          fail(""+e);
263       }
264    }
265
266    public void testLTDouble()
267    {
268       try
269       {
270          getLog().debug("parse(weight < 1.5)");
271          Operator result = (Operator)parser.parse("weight < 1.5", identifierMap);
272          ((Identifier) identifierMap.get("weight")).setValue(new Double JavaDoc(1.2));
273          getLog().debug("result -> "+result);
274          Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
275          assertTrue("is true", bool.booleanValue());
276       } catch (Exception JavaDoc e)
277       {
278          getLog().debug("failed", e);
279          fail(""+e);
280       }
281    }
282
283    public void testAndCombination()
284    {
285       try
286       {
287          getLog().debug("parse(JMSType = 'car' AND color = 'blue' AND weight > 2500)");
288          Operator result = (Operator)parser.parse("JMSType = 'car' AND color = 'blue' AND weight > 2500", identifierMap);
289          ((Identifier) identifierMap.get("JMSType")).setValue("car");
290          ((Identifier) identifierMap.get("color")).setValue("blue");
291          ((Identifier) identifierMap.get("weight")).setValue("3000");
292          
293          getLog().debug("result -> "+result);
294          Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
295          assertTrue("is false", !bool.booleanValue());
296       } catch (Exception JavaDoc e)
297       {
298          getLog().debug("failed", e);
299          fail(""+e);
300       }
301    }
302    
303    public void testINANDCombination()
304    {
305       try
306       {
307          getLog().debug("parse(Cateogry IN ('category1') AND Rating >= 2");
308          Operator result = (Operator)parser.parse("Cateogry IN ('category1') AND Rating >= 2", identifierMap);
309          ((Identifier) identifierMap.get("Cateogry")).setValue("category1");
310          ((Identifier) identifierMap.get("Rating")).setValue(new Integer JavaDoc(3));
311          getLog().debug("result -> "+result);
312          Boolean JavaDoc bool = (Boolean JavaDoc) result.apply();
313          assertTrue("is true", bool.booleanValue());
314       } catch (Exception JavaDoc e)
315       {
316          getLog().debug("failed", e);
317          fail(""+e);
318       }
319    }
320
321    /** This testcase does not use the JBossServer so override
322    the testServerFound to be a noop
323    */

324    public void testServerFound()
325    {
326    }
327
328    public static void main(java.lang.String JavaDoc[] args)
329    {
330       junit.textui.TestRunner.run(SelectorParserUnitTestCase.class);
331    }
332 }
333
Popular Tags