001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jbossmq.test;
023:
024: import java.io.ByteArrayInputStream;
025: import java.util.HashMap;
026:
027: import org.jboss.mq.selectors.ISelectorParser;
028: import org.jboss.mq.selectors.Identifier;
029: import org.jboss.mq.selectors.Operator;
030: import org.jboss.mq.selectors.SelectorParser;
031: import org.jboss.logging.Logger;
032: import junit.framework.TestCase;
033:
034: /** Tests of the JavaCC LL(1) parser.
035:
036: @author Scott.Stark@jboss.org
037: @author d_jencks@users.sourceforge.net
038:
039: @version $Revision: 57211 $
040:
041: * (david jencks) Used constructor of SelectorParser taking a stream
042: * to avoid reInit npe in all tests. Changed to JBossTestCase and logging.
043: */
044: public class SelectorParserUnitTestCase extends TestCase {
045: static Logger log = Logger
046: .getLogger(SelectorParserUnitTestCase.class);
047: static HashMap identifierMap = new HashMap();
048: static ISelectorParser parser;
049:
050: public SelectorParserUnitTestCase(String name) {
051: super (name);
052: }
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: identifierMap.clear();
057: if (parser == null) {
058: parser = new SelectorParser(new ByteArrayInputStream(
059: new byte[0]));
060: }
061: }
062:
063: public void testSimpleUnary() throws Exception {
064: // Neg Long
065: log.debug("parse(-12345 = -1 * 12345)");
066: Operator result = (Operator) parser.parse(
067: "-12345 = -1 * 12345", identifierMap);
068: log.debug("result -> " + result);
069: Boolean b = (Boolean) result.apply();
070: assertTrue("is true", b.booleanValue());
071:
072: // Neg Double
073: log.debug("parse(-1 * 12345.67 = -12345.67)");
074: result = (Operator) parser.parse("-1 * 12345.67 = -12345.67",
075: identifierMap);
076: log.debug("result -> " + result);
077: b = (Boolean) result.apply();
078: assertTrue("is true", b.booleanValue());
079:
080: log.debug("parse(-(1 * 12345.67) = -12345.67)");
081: result = (Operator) parser.parse("-(1 * 12345.67) = -12345.67",
082: identifierMap);
083: log.debug("result -> " + result);
084: b = (Boolean) result.apply();
085: assertTrue("is true", b.booleanValue());
086: }
087:
088: public void testPrecedenceNAssoc() throws Exception {
089: log.debug("parse(4 + 2 * 3 / 2 = 7)");
090: Operator result = (Operator) parser.parse("4 + 2 * 3 / 2 = 7",
091: identifierMap);
092: log.debug("result -> " + result);
093: Boolean b = (Boolean) result.apply();
094: assertTrue("is true", b.booleanValue());
095:
096: log.debug("parse(4 + ((2 * 3) / 2) = 7)");
097: result = (Operator) parser.parse("4 + ((2 * 3) / 2) = 7",
098: identifierMap);
099: log.debug("result -> " + result);
100: b = (Boolean) result.apply();
101: assertTrue("is true", b.booleanValue());
102:
103: log.debug("parse(4 * -2 / -1 - 4 = 4)");
104: result = (Operator) parser.parse("4 * -2 / -1 - 4 = 4",
105: identifierMap);
106: log.debug("result -> " + result);
107: b = (Boolean) result.apply();
108: assertTrue("is true", b.booleanValue());
109:
110: log.debug("parse(4 * ((-2 / -1) - 4) = -8)");
111: result = (Operator) parser.parse("4 * ((-2 / -1) - 4) = -8",
112: identifierMap);
113: log.debug("result -> " + result);
114: b = (Boolean) result.apply();
115: assertTrue("is true", b.booleanValue());
116: }
117:
118: public void testIds() throws Exception {
119: log.debug("parse(a + b * c / d = e)");
120: Operator result = (Operator) parser.parse("a + b * c / d = e",
121: identifierMap);
122: // 4 + 2 * 3 / 2 = 7
123: Identifier a = (Identifier) identifierMap.get("a");
124: a.setValue(new Long(4));
125: Identifier b = (Identifier) identifierMap.get("b");
126: b.setValue(new Long(2));
127: Identifier c = (Identifier) identifierMap.get("c");
128: c.setValue(new Long(3));
129: Identifier d = (Identifier) identifierMap.get("d");
130: d.setValue(new Long(2));
131: Identifier e = (Identifier) identifierMap.get("e");
132: e.setValue(new Long(7));
133: log.debug("result -> " + result);
134: Boolean bool = (Boolean) result.apply();
135: assertTrue("is true", bool.booleanValue());
136:
137: }
138:
139: public void testTrueINOperator() throws Exception {
140: log
141: .debug("parse(Status IN ('new', 'cleared', 'acknowledged'))");
142: Operator result = (Operator) parser.parse(
143: "Status IN ('new', 'cleared', 'acknowledged')",
144: identifierMap);
145: Identifier a = (Identifier) identifierMap.get("Status");
146: a.setValue("new");
147: log.debug("result -> " + result);
148: Boolean bool = (Boolean) result.apply();
149: assertTrue("is true", bool.booleanValue());
150: }
151:
152: public void testFalseINOperator() throws Exception {
153: log
154: .debug("parse(Status IN ('new', 'cleared', 'acknowledged'))");
155: Operator result = (Operator) parser.parse(
156: "Status IN ('new', 'cleared', 'acknowledged')",
157: identifierMap);
158: Identifier a = (Identifier) identifierMap.get("Status");
159: a.setValue("none");
160: log.debug("result -> " + result);
161: Boolean bool = (Boolean) result.apply();
162: assertTrue("is false", !bool.booleanValue());
163: }
164:
165: public void testTrueOROperator() throws Exception {
166: log
167: .debug("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))");
168: Operator result = (Operator) parser
169: .parse(
170: "(Status = 'new') OR (Status = 'cleared') OR (Status= 'acknowledged')",
171: identifierMap);
172: Identifier a = (Identifier) identifierMap.get("Status");
173: a.setValue("new");
174: log.debug("result -> " + result);
175: Boolean bool = (Boolean) result.apply();
176: assertTrue("is true", bool.booleanValue());
177: }
178:
179: public void testFalseOROperator() throws Exception {
180: log
181: .debug("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))");
182: Operator result = (Operator) parser
183: .parse(
184: "(Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged')",
185: identifierMap);
186: Identifier a = (Identifier) identifierMap.get("Status");
187: a.setValue("none");
188: log.debug("result -> " + result);
189: Boolean bool = (Boolean) result.apply();
190: assertTrue("is false", !bool.booleanValue());
191: }
192:
193: public void testInvalidSelector() throws Exception {
194: log.debug("parse(definitely not a message selector!)");
195: try {
196: Object result = parser
197: .parse("definitely not a message selector!",
198: identifierMap);
199: log.debug("result -> " + result);
200: fail("Should throw an Exception.\n");
201: } catch (Exception e) {
202: log.info("testInvalidSelector failed as expected", e);
203: }
204: }
205:
206: /**
207: * Test diffent syntax for approximate numeric literal (+6.2, -95.7, 7.)
208: */
209: public void testApproximateNumericLiteral1() {
210: try {
211: log.debug("parse(average = +6.2)");
212: Object result = parser.parse("average = +6.2",
213: identifierMap);
214: log.debug("result -> " + result);
215: } catch (Exception e) {
216: fail("" + e);
217: }
218: }
219:
220: public void testApproximateNumericLiteral2() {
221: try {
222: log.debug("parse(average = -95.7)");
223: Object result = parser.parse("average = -95.7",
224: identifierMap);
225: log.debug("result -> " + result);
226: } catch (Exception e) {
227: fail("" + e);
228: }
229: }
230:
231: public void testApproximateNumericLiteral3() {
232: try {
233: log.debug("parse(average = 7.)");
234: Object result = parser.parse("average = 7.", identifierMap);
235: log.debug("result -> " + result);
236: } catch (Exception e) {
237: fail("" + e);
238: }
239: }
240:
241: public void testGTExact() {
242: try {
243: log.debug("parse(weight > 2500)");
244: Operator result = (Operator) parser.parse("weight > 2500",
245: identifierMap);
246: ((Identifier) identifierMap.get("weight"))
247: .setValue(new Integer(3000));
248: log.debug("result -> " + result);
249: Boolean bool = (Boolean) result.apply();
250: assertTrue("is true", bool.booleanValue());
251: } catch (Exception e) {
252: log.debug("failed", e);
253: fail("" + e);
254: }
255: }
256:
257: public void testGTFloat() {
258: try {
259: log.debug("parse(weight > 2500)");
260: Operator result = (Operator) parser.parse("weight > 2500",
261: identifierMap);
262: ((Identifier) identifierMap.get("weight"))
263: .setValue(new Float(3000));
264: log.debug("result -> " + result);
265: Boolean bool = (Boolean) result.apply();
266: assertTrue("is true", bool.booleanValue());
267: } catch (Exception e) {
268: log.debug("failed", e);
269: fail("" + e);
270: }
271: }
272:
273: public void testLTDouble() {
274: try {
275: log.debug("parse(weight < 1.5)");
276: Operator result = (Operator) parser.parse("weight < 1.5",
277: identifierMap);
278: ((Identifier) identifierMap.get("weight"))
279: .setValue(new Double(1.2));
280: log.debug("result -> " + result);
281: Boolean bool = (Boolean) result.apply();
282: assertTrue("is true", bool.booleanValue());
283: } catch (Exception e) {
284: log.debug("failed", e);
285: fail("" + e);
286: }
287: }
288:
289: public void testAndCombination() {
290: try {
291: log
292: .debug("parse(JMSType = 'car' AND color = 'blue' AND weight > 2500)");
293: Operator result = (Operator) parser
294: .parse(
295: "JMSType = 'car' AND color = 'blue' AND weight > 2500",
296: identifierMap);
297: ((Identifier) identifierMap.get("JMSType")).setValue("car");
298: ((Identifier) identifierMap.get("color")).setValue("blue");
299: ((Identifier) identifierMap.get("weight")).setValue("3000");
300:
301: log.debug("result -> " + result);
302: Boolean bool = (Boolean) result.apply();
303: assertTrue("is false", !bool.booleanValue());
304: } catch (Exception e) {
305: log.debug("failed", e);
306: fail("" + e);
307: }
308: }
309:
310: public void testINANDCombination() {
311: try {
312: log
313: .debug("parse(Cateogry IN ('category1') AND Rating >= 2");
314: Operator result = (Operator) parser.parse(
315: "Cateogry IN ('category1') AND Rating >= 2",
316: identifierMap);
317: ((Identifier) identifierMap.get("Cateogry"))
318: .setValue("category1");
319: ((Identifier) identifierMap.get("Rating"))
320: .setValue(new Integer(3));
321: log.debug("result -> " + result);
322: Boolean bool = (Boolean) result.apply();
323: assertTrue("is true", bool.booleanValue());
324: } catch (Exception e) {
325: log.debug("failed", e);
326: fail("" + e);
327: }
328: }
329:
330: public static void main(java.lang.String[] args) {
331: junit.textui.TestRunner.run(SelectorParserUnitTestCase.class);
332: }
333: }
|