[jboss-cvs] JBoss Messaging SVN: r3592 - in trunk/tests/src/org/jboss: messaging/core/impl/filter and 6 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Jan 18 08:45:36 EST 2008
Author: timfox
Date: 2008-01-18 08:45:35 -0500 (Fri, 18 Jan 2008)
New Revision: 3592
Added:
trunk/tests/src/org/jboss/messaging/core/impl/filter/
trunk/tests/src/org/jboss/messaging/core/impl/filter/test/
trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/
trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterParserTest.java
trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterTest.java
trunk/tests/src/org/jboss/test/messaging/jms/client/
trunk/tests/src/org/jboss/test/messaging/jms/client/test/
trunk/tests/src/org/jboss/test/messaging/jms/client/test/unit/
trunk/tests/src/org/jboss/test/messaging/jms/client/test/unit/SelectorTranslatorTest.java
Log:
Added tests
Added: trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterParserTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterParserTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterParserTest.java 2008-01-18 13:45:35 UTC (rev 3592)
@@ -0,0 +1,332 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.core.impl.filter.test.unit;
+
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.logging.Logger;
+import org.jboss.messaging.core.impl.filter.FilterParser;
+import org.jboss.messaging.core.impl.filter.Identifier;
+import org.jboss.messaging.core.impl.filter.Operator;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ Tests of the JavaCC LL(1) parser for the JBoss Messaging filters
+
+ @author Scott.Stark at jboss.org
+ @author d_jencks at users.sourceforge.net
+ @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+
+ @version $Revision: 3465 $
+
+ * (david jencks) Used constructor of SelectorParser taking a stream
+ * to avoid reInit npe in all tests. Changed to JBossTestCase and logging.
+ */
+public class FilterParserTest extends UnitTestCase
+{
+ private static final Logger log = Logger.getLogger(FilterParserTest.class);
+
+ private Map<String, Identifier> identifierMap;
+
+ private FilterParser parser;
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ identifierMap = new HashMap<String, Identifier>();
+
+ parser = new FilterParser(new ByteArrayInputStream(new byte[0]));
+ }
+
+ public void testSimpleUnary() throws Exception
+ {
+ // Neg Long
+ log.trace("parse(-12345 = -1 * 12345)");
+ Operator result = (Operator) parser.parse("-12345 = -1 * 12345", identifierMap);
+ log.trace("result -> "+result);
+ Boolean b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+
+ // Neg Double
+ log.trace("parse(-1 * 12345.67 = -12345.67)");
+ result = (Operator) parser.parse("-1 * 12345.67 = -12345.67", identifierMap);
+ log.trace("result -> "+result);
+ b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+
+ log.trace("parse(-(1 * 12345.67) = -12345.67)");
+ result = (Operator) parser.parse("-(1 * 12345.67) = -12345.67", identifierMap);
+ log.trace("result -> "+result);
+ b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+ }
+
+ public void testPrecedenceNAssoc() throws Exception
+ {
+ log.trace("parse(4 + 2 * 3 / 2 = 7)");
+ Operator result = (Operator) parser.parse("4 + 2 * 3 / 2 = 7", identifierMap);
+ log.trace("result -> "+result);
+ Boolean b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+
+ log.trace("parse(4 + ((2 * 3) / 2) = 7)");
+ result = (Operator) parser.parse("4 + ((2 * 3) / 2) = 7", identifierMap);
+ log.trace("result -> "+result);
+ b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+
+ log.trace("parse(4 * -2 / -1 - 4 = 4)");
+ result = (Operator) parser.parse("4 * -2 / -1 - 4 = 4", identifierMap);
+ log.trace("result -> "+result);
+ b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+
+ log.trace("parse(4 * ((-2 / -1) - 4) = -8)");
+ result = (Operator) parser.parse("4 * ((-2 / -1) - 4) = -8", identifierMap);
+ log.trace("result -> "+result);
+ b = (Boolean) result.apply();
+ assertTrue("is true", b.booleanValue());
+ }
+
+ public void testIds() throws Exception
+ {
+ log.trace("parse(a + b * c / d = e)");
+ Operator result = (Operator) parser.parse("a + b * c / d = e", identifierMap);
+ // 4 + 2 * 3 / 2 = 7
+ Identifier a = identifierMap.get("a");
+ a.setValue(new Long(4));
+ Identifier b = identifierMap.get("b");
+ b.setValue(new Long(2));
+ Identifier c = identifierMap.get("c");
+ c.setValue(new Long(3));
+ Identifier d = identifierMap.get("d");
+ d.setValue(new Long(2));
+ Identifier e = identifierMap.get("e");
+ e.setValue(new Long(7));
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+
+ }
+
+ public void testTrueINOperator() throws Exception
+ {
+ log.trace("parse(Status IN ('new', 'cleared', 'acknowledged'))");
+ Operator result = (Operator) parser.parse("Status IN ('new', 'cleared', 'acknowledged')", identifierMap);
+ Identifier a = identifierMap.get("Status");
+ a.setValue("new");
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+ }
+ public void testFalseINOperator() throws Exception
+ {
+ log.trace("parse(Status IN ('new', 'cleared', 'acknowledged'))");
+ Operator result = (Operator) parser.parse("Status IN ('new', 'cleared', 'acknowledged')", identifierMap);
+ Identifier a = identifierMap.get("Status");
+ a.setValue("none");
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is false", !bool.booleanValue());
+ }
+
+ public void testTrueOROperator() throws Exception
+ {
+ log.trace("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))");
+ Operator result = (Operator) parser.parse("(Status = 'new') OR (Status = 'cleared') OR (Status= 'acknowledged')", identifierMap);
+ Identifier a = identifierMap.get("Status");
+ a.setValue("new");
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+ }
+ public void testFalseOROperator() throws Exception
+ {
+ log.trace("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))");
+ Operator result = (Operator) parser.parse("(Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged')", identifierMap);
+ Identifier a = identifierMap.get("Status");
+ a.setValue("none");
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is false", !bool.booleanValue());
+ }
+
+ public void testInvalidSelector() throws Exception
+ {
+ log.trace("parse(definitely not a message selector!)");
+ try
+ {
+ Object result = parser.parse("definitely not a message selector!", identifierMap);
+ log.trace("result -> "+result);
+ fail("Should throw an Exception.\n");
+ }
+ catch (Exception e)
+ {
+ log.trace("testInvalidSelector failed as expected", e);
+ }
+ }
+
+ /**
+ * Test diffent syntax for approximate numeric literal (+6.2, -95.7, 7.)
+ */
+ public void testApproximateNumericLiteral1()
+ {
+ try
+ {
+ log.trace("parse(average = +6.2)");
+ Object result = parser.parse("average = +6.2", identifierMap);
+ log.trace("result -> "+result);
+ } catch (Exception e)
+ {
+ fail(""+e);
+ }
+ }
+
+ public void testApproximateNumericLiteral2()
+ {
+ try
+ {
+ log.trace("parse(average = -95.7)");
+ Object result = parser.parse("average = -95.7", identifierMap);
+ log.trace("result -> "+result);
+ } catch (Exception e)
+ {
+ fail(""+e);
+ }
+ }
+ public void testApproximateNumericLiteral3()
+ {
+ try
+ {
+ log.trace("parse(average = 7.)");
+ Object result = parser.parse("average = 7.", identifierMap);
+ log.trace("result -> "+result);
+ } catch (Exception e)
+ {
+ fail(""+e);
+ }
+ }
+
+ public void testGTExact()
+ {
+ try
+ {
+ log.trace("parse(weight > 2500)");
+ Operator result = (Operator)parser.parse("weight > 2500", identifierMap);
+ (identifierMap.get("weight")).setValue(new Integer(3000));
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+ } catch (Exception e)
+ {
+ log.trace("failed", e);
+ fail(""+e);
+ }
+ }
+
+ public void testGTFloat()
+ {
+ try
+ {
+ log.trace("parse(weight > 2500)");
+ Operator result = (Operator)parser.parse("weight > 2500", identifierMap);
+ (identifierMap.get("weight")).setValue(new Float(3000));
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+ } catch (Exception e)
+ {
+ log.trace("failed", e);
+ fail(""+e);
+ }
+ }
+
+ public void testLTDouble()
+ {
+ try
+ {
+ log.trace("parse(weight < 1.5)");
+ Operator result = (Operator)parser.parse("weight < 1.5", identifierMap);
+ (identifierMap.get("weight")).setValue(new Double(1.2));
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+ } catch (Exception e)
+ {
+ log.trace("failed", e);
+ fail(""+e);
+ }
+ }
+
+ public void testAndCombination()
+ {
+ try
+ {
+ log.trace("parse(JMSType = 'car' AND color = 'blue' AND weight > 2500)");
+ Operator result = (Operator)parser.parse("JMSType = 'car' AND color = 'blue' AND weight > 2500", identifierMap);
+ (identifierMap.get("JMSType")).setValue("car");
+ (identifierMap.get("color")).setValue("blue");
+ (identifierMap.get("weight")).setValue("3000");
+
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is false", !bool.booleanValue());
+ } catch (Exception e)
+ {
+ log.trace("failed", e);
+ fail(""+e);
+ }
+ }
+
+ public void testINANDCombination()
+ {
+ try
+ {
+ log.trace("parse(Cateogry IN ('category1') AND Rating >= 2");
+ Operator result = (Operator)parser.parse("Cateogry IN ('category1') AND Rating >= 2", identifierMap);
+ (identifierMap.get("Cateogry")).setValue("category1");
+ (identifierMap.get("Rating")).setValue(new Integer(3));
+ log.trace("result -> "+result);
+ Boolean bool = (Boolean) result.apply();
+ assertTrue("is true", bool.booleanValue());
+ } catch (Exception e)
+ {
+ log.trace("failed", e);
+ fail(""+e);
+ }
+ }
+
+ /** This testcase does not use the JBossServer so override
+ the testServerFound to be a noop
+ */
+ public void testServerFound()
+ {
+ }
+
+ public static void main(java.lang.String[] args)
+ {
+ junit.textui.TestRunner.run(FilterParserTest.class);
+ }
+}
Added: trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/core/impl/filter/test/unit/FilterTest.java 2008-01-18 13:45:35 UTC (rev 3592)
@@ -0,0 +1,614 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.core.impl.filter.test.unit;
+
+import org.jboss.messaging.core.Filter;
+import org.jboss.messaging.core.Message;
+import org.jboss.messaging.core.impl.MessageImpl;
+import org.jboss.messaging.core.impl.filter.FilterImpl;
+import org.jboss.test.messaging.JBMBaseTestCase;
+
+/**
+ * Tests the compliance with the JBoss Messaging Filter syntax.
+ *
+ * <p>Needs a lot of work...
+ *
+ * @author <a href="mailto:jason at planet57.com">Jason Dillon</a>
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ * @version $Revision: 3514 $
+ */
+public class FilterTest extends JBMBaseTestCase
+{
+ private Filter filter;
+
+ private Message message;
+
+ public FilterTest(String name)
+ {
+ super(name);
+ }
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ message = new MessageImpl();
+ }
+
+ public void testInvalidString() throws Exception
+ {
+ testInvalidFilter("invalid");
+
+ testInvalidFilter("color = 'red");
+
+ testInvalidFilter("3");
+
+ testInvalidFilter(null);
+ }
+
+ public void testJBMDurable() throws Exception
+ {
+ filter = new FilterImpl("JBMDurable='DURABLE'");
+
+ Message message = new MessageImpl();
+ message.setDurable(true);
+
+ assertTrue(filter.match(message));
+
+ message.setDurable(false);
+
+ assertFalse(filter.match(message));
+
+ filter = new FilterImpl("JBMDurable='NON_DURABLE'");
+
+ message = new MessageImpl();
+ message.setDurable(true);
+
+ assertFalse(filter.match(message));
+
+ message.setDurable(false);
+
+ assertTrue(filter.match(message));
+
+ }
+
+ public void testJBMPriority() throws Exception
+ {
+ filter = new FilterImpl("JBMPriority=3");
+
+ Message message = new MessageImpl();
+
+ for (int i = 0; i < 10; i++)
+ {
+ message.setPriority((byte)i);
+
+ if (i == 3)
+ {
+ assertTrue(filter.match(message));
+ }
+ else
+ {
+ assertFalse(filter.match(message));
+ }
+ }
+ }
+
+ public void testJBMMessageID() throws Exception
+ {
+ filter = new FilterImpl("JBMMessageID=11223344");
+
+ Message message = new MessageImpl();
+
+ message.setMessageID(78676);
+
+ assertFalse(filter.match(message));
+
+ message.setMessageID(11223344);
+
+ assertTrue(filter.match(message));
+ }
+
+ public void testJBMTimestamp() throws Exception
+ {
+ filter = new FilterImpl("JBMTimestamp=12345678");
+
+ Message message = new MessageImpl();
+
+ message.setTimestamp(87654321);
+
+ assertFalse(filter.match(message));
+
+ message.setTimestamp(12345678);
+
+ assertTrue(filter.match(message));
+ }
+
+ public void testBooleanTrue() throws Exception
+ {
+ filter = new FilterImpl("MyBoolean=true");
+
+ testBoolean("MyBoolean", true);
+ }
+
+ public void testBooleanFalse() throws Exception
+ {
+ filter = new FilterImpl("MyBoolean=false");
+ testBoolean("MyBoolean", false);
+ }
+
+ private void testBoolean(String name, boolean flag) throws Exception
+ {
+ message.putHeader(name, flag);
+ assertTrue(filter.match(message));
+
+ message.putHeader(name, !flag);
+ assertTrue(!filter.match(message));
+ }
+
+ public void testStringEquals() throws Exception
+ {
+ // First, simple test of string equality and inequality
+ filter = new FilterImpl("MyString='astring'");
+
+ message.putHeader("MyString", "astring");
+ assertTrue(filter.match(message));
+
+ message.putHeader("MyString", "NOTastring");
+ assertTrue(!filter.match(message));
+
+ // test empty string
+ filter = new FilterImpl("MyString=''");
+
+ message.putHeader("MyString", "");
+ assertTrue("test 1", filter.match(message));
+
+ message.putHeader("MyString", "NOTastring");
+ assertTrue("test 2", !filter.match(message));
+
+ // test literal apostrophes (which are escaped using two apostrophes
+ // in selectors)
+ filter = new FilterImpl("MyString='test JBoss''s filter'");
+
+ // note: apostrophes are not escaped in string properties
+ message.putHeader("MyString", "test JBoss's filter");
+ // this test fails -- bug 530120
+ //assertTrue("test 3", filter.match(message));
+
+ message.putHeader("MyString", "NOTastring");
+ assertTrue("test 4", !filter.match(message));
+
+ }
+
+ public void testStringLike() throws Exception
+ {
+ // test LIKE operator with no wildcards
+ filter = new FilterImpl("MyString LIKE 'astring'");
+
+ // test where LIKE operand matches
+ message.putHeader("MyString", "astring");
+ assertTrue(filter.match(message));
+
+ // test one character string
+ filter = new FilterImpl("MyString LIKE 'a'");
+ message.putHeader("MyString","a");
+ assertTrue(filter.match(message));
+
+ // test empty string
+ filter = new FilterImpl("MyString LIKE ''");
+ message.putHeader("MyString", "");
+ assertTrue(filter.match(message));
+
+ // tests where operand does not match
+ filter = new FilterImpl("MyString LIKE 'astring'");
+
+ // test with extra characters at beginning
+ message.putHeader("MyString", "NOTastring");
+ assertTrue(!filter.match(message));
+
+ // test with extra characters at end
+ message.putHeader("MyString", "astringNOT");
+ assertTrue(!filter.match(message));
+
+ // test with extra characters in the middle
+ message.putHeader("MyString", "astNOTring");
+ assertTrue(!filter.match(message));
+
+ // test where operand is entirely different
+ message.putHeader("MyString", "totally different");
+ assertTrue(!filter.match(message));
+
+ // test case sensitivity
+ message.putHeader("MyString", "ASTRING");
+ assertTrue(!filter.match(message));
+
+ // test empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+
+ // test lower-case 'like' operator?
+ }
+
+ public void testStringLikeUnderbarWildcard() throws Exception
+ {
+ // test LIKE operator with the _ wildcard, which
+ // matches any single character
+
+ // first, some tests with the wildcard by itself
+ filter = new FilterImpl("MyString LIKE '_'");
+
+ // test match against single character
+ message.putHeader("MyString", "a");
+ assertTrue(filter.match(message));
+
+ // test match failure against multiple characters
+ message.putHeader("MyString", "aaaaa");
+ assertTrue(!filter.match(message));
+
+ // test match failure against the empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+
+ // next, tests with wildcard at the beginning of the string
+ filter = new FilterImpl("MyString LIKE '_bcdf'");
+
+ // test match at beginning of string
+ message.putHeader("MyString", "abcdf");
+ assertTrue(filter.match(message));
+
+ // match failure in first character after wildcard
+ message.putHeader("MyString", "aXcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in middle character
+ message.putHeader("MyString", "abXdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in last character
+ message.putHeader("MyString", "abcdX");
+ assertTrue(!filter.match(message));
+
+ // match failure with empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at beginning
+ message.putHeader("MyString", "XXXabcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at the end
+ message.putHeader("MyString", "abcdfXXX");
+ assertTrue(!filter.match(message));
+
+ // test that the _ wildcard does not match the 'empty' character
+ message.putHeader("MyString", "bcdf");
+ assertTrue(!filter.match(message));
+
+ // next, tests with wildcard at the end of the string
+ filter = new FilterImpl("MyString LIKE 'abcd_'");
+
+ // test match at end of string
+ message.putHeader("MyString", "abcdf");
+ assertTrue(filter.match(message));
+
+ // match failure in first character before wildcard
+ message.putHeader("MyString", "abcXf");
+ assertTrue(!filter.match(message));
+
+ // match failure in middle character
+ message.putHeader("MyString", "abXdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in first character
+ message.putHeader("MyString", "Xbcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure with empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at beginning
+ message.putHeader("MyString", "XXXabcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at the end
+ message.putHeader("MyString", "abcdfXXX");
+ assertTrue(!filter.match(message));
+
+ // test that the _ wildcard does not match the 'empty' character
+ message.putHeader("MyString", "abcd");
+ assertTrue(!filter.match(message));
+
+ // test match in middle of string
+
+ // next, tests with wildcard in the middle of the string
+ filter = new FilterImpl("MyString LIKE 'ab_df'");
+
+ // test match in the middle of string
+ message.putHeader("MyString", "abcdf");
+ assertTrue(filter.match(message));
+
+ // match failure in first character before wildcard
+ message.putHeader("MyString", "aXcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in first character after wildcard
+ message.putHeader("MyString", "abcXf");
+ assertTrue(!filter.match(message));
+
+ // match failure in last character
+ message.putHeader("MyString", "abcdX");
+ assertTrue(!filter.match(message));
+
+ // match failure with empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at beginning
+ message.putHeader("MyString", "XXXabcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at the end
+ message.putHeader("MyString", "abcdfXXX");
+ assertTrue(!filter.match(message));
+
+ // test that the _ wildcard does not match the 'empty' character
+ message.putHeader("MyString", "abdf");
+ assertTrue(!filter.match(message));
+
+ // test match failures
+ }
+
+ public void testStringLikePercentWildcard() throws Exception
+ {
+ // test LIKE operator with the % wildcard, which
+ // matches any sequence of characters
+ // note many of the tests are similar to those for _
+
+
+ // first, some tests with the wildcard by itself
+ filter = new FilterImpl("MyString LIKE '%'");
+
+ // test match against single character
+ message.putHeader("MyString", "a");
+ assertTrue(filter.match(message));
+
+ // test match against multiple characters
+ message.putHeader("MyString", "aaaaa");
+ assertTrue(filter.match(message));
+
+ message.putHeader("MyString", "abcdf");
+ assertTrue(filter.match(message));
+
+ // test match against the empty string
+ message.putHeader("MyString", "");
+ assertTrue(filter.match(message));
+
+
+ // next, tests with wildcard at the beginning of the string
+ filter = new FilterImpl("MyString LIKE '%bcdf'");
+
+ // test match with single character at beginning of string
+ message.putHeader("MyString", "Xbcdf");
+ assertTrue(filter.match(message));
+
+ // match with multiple characters at beginning
+ message.putHeader("MyString", "XXbcdf");
+ assertTrue(filter.match(message));
+
+ // match failure in middle character
+ message.putHeader("MyString", "abXdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in last character
+ message.putHeader("MyString", "abcdX");
+ assertTrue(!filter.match(message));
+
+ // match failure with empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at the end
+ message.putHeader("MyString", "abcdfXXX");
+ assertTrue(!filter.match(message));
+
+ // test that the % wildcard matches the empty string
+ message.putHeader("MyString", "bcdf");
+ assertTrue(filter.match(message));
+
+ // next, tests with wildcard at the end of the string
+ filter = new FilterImpl("MyString LIKE 'abcd%'");
+
+ // test match of single character at end of string
+ message.putHeader("MyString", "abcdf");
+ assertTrue(filter.match(message));
+
+ // test match of multiple characters at end of string
+ message.putHeader("MyString", "abcdfgh");
+ assertTrue(filter.match(message));
+
+ // match failure in first character before wildcard
+ message.putHeader("MyString", "abcXf");
+ assertTrue(!filter.match(message));
+
+ // match failure in middle character
+ message.putHeader("MyString", "abXdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in first character
+ message.putHeader("MyString", "Xbcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure with empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at beginning
+ message.putHeader("MyString", "XXXabcdf");
+ assertTrue(!filter.match(message));
+
+ // test that the % wildcard matches the empty string
+ message.putHeader("MyString", "abcd");
+ assertTrue(filter.match(message));
+
+ // next, tests with wildcard in the middle of the string
+ filter = new FilterImpl("MyString LIKE 'ab%df'");
+
+ // test match with single character in the middle of string
+ message.putHeader("MyString", "abXdf");
+ assertTrue(filter.match(message));
+
+ // test match with multiple characters in the middle of string
+ message.putHeader("MyString", "abXXXdf");
+ assertTrue(filter.match(message));
+
+ // match failure in first character before wildcard
+ message.putHeader("MyString", "aXcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure in first character after wildcard
+ message.putHeader("MyString", "abcXf");
+ assertTrue(!filter.match(message));
+
+ // match failure in last character
+ message.putHeader("MyString", "abcdX");
+ assertTrue(!filter.match(message));
+
+ // match failure with empty string
+ message.putHeader("MyString", "");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at beginning
+ message.putHeader("MyString", "XXXabcdf");
+ assertTrue(!filter.match(message));
+
+ // match failure due to extra characters at the end
+ message.putHeader("MyString", "abcdfXXX");
+ assertTrue(!filter.match(message));
+
+ // test that the % wildcard matches the empty string
+ message.putHeader("MyString", "abdf");
+ assertTrue(filter.match(message));
+
+ }
+
+ public void testStringLikePunctuation() throws Exception
+ {
+ // test proper handling of some punctuation characters.
+ // non-trivial since the underlying implementation might
+ // (and in fact currently does) use a general-purpose
+ // RE library, which has a different notion of which
+ // characters are wildcards
+
+ // the particular tests here are motivated by the
+ // wildcards of the current underlying RE engine,
+ // GNU regexp.
+
+ filter = new FilterImpl("MyString LIKE 'a^$b'");
+ message.putHeader("MyString", "a^$b");
+ assertTrue(filter.match(message));
+
+ // this one has a double backslash since backslash
+ // is interpreted specially by Java
+ filter = new FilterImpl("MyString LIKE 'a\\dc'");
+ message.putHeader("MyString", "a\\dc");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE 'a.c'");
+ message.putHeader("MyString", "abc");
+ assertTrue(!filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '[abc]'");
+ message.putHeader("MyString", "[abc]");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '[^abc]'");
+ message.putHeader("MyString", "[^abc]");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '[a-c]'");
+ message.putHeader("MyString", "[a-c]");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '[:alpha]'");
+ message.putHeader("MyString", "[:alpha]");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc)'");
+ message.putHeader("MyString", "(abc)");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE 'a|bc'");
+ message.putHeader("MyString", "a|bc");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc)?'");
+ message.putHeader("MyString", "(abc)?");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc)*'");
+ message.putHeader("MyString", "(abc)*");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc)+'");
+ message.putHeader("MyString", "(abc)+");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc){3}'");
+ message.putHeader("MyString", "(abc){3}");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc){3,5}'");
+ message.putHeader("MyString", "(abc){3,5}");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(abc){3,}'");
+ message.putHeader("MyString", "(abc){3,}");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(?=abc)'");
+ message.putHeader("MyString", "(?=abc)");
+ assertTrue(filter.match(message));
+
+ filter = new FilterImpl("MyString LIKE '(?!abc)'");
+ message.putHeader("MyString", "(?!abc)");
+ assertTrue(filter.match(message));
+ }
+
+ // Private -----------------------------------------------------------------------------------
+
+ private void testInvalidFilter(String filterString) throws Exception
+ {
+ try
+ {
+ filter = new FilterImpl(filterString);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals("Invalid filter: " + filterString, e.getMessage());
+ }
+ }
+
+}
Added: trunk/tests/src/org/jboss/test/messaging/jms/client/test/unit/SelectorTranslatorTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/client/test/unit/SelectorTranslatorTest.java (rev 0)
+++ trunk/tests/src/org/jboss/test/messaging/jms/client/test/unit/SelectorTranslatorTest.java 2008-01-18 13:45:35 UTC (rev 3592)
@@ -0,0 +1,241 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.messaging.jms.client.test.unit;
+
+import org.jboss.jms.client.SelectorTranslator;
+import org.jboss.messaging.test.unit.UnitTestCase;
+
+/**
+ *
+ * A SelectorTranslatorTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class SelectorTranslatorTest extends UnitTestCase
+{
+ public void testParseNull()
+ {
+ assertNull(SelectorTranslator.convertToJBMFilterString(null));
+ }
+
+ public void testParseSimple()
+ {
+ final String selector = "color = 'red'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+ }
+
+ public void testParseMoreComplex()
+ {
+ final String selector = "color = 'red' OR cheese = 'stilton' OR (age = 3 AND shoesize = 12)";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+ }
+
+ public void testParseJMSDeliveryMode()
+ {
+ String selector = "JMSDeliveryMode='NON_PERSISTENT'";
+
+ assertEquals("JBMDurable='NON_DURABLE'", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "JMSDeliveryMode='PERSISTENT'";
+
+ assertEquals("JBMDurable='DURABLE'", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "color = 'red' AND 'NON_PERSISTENT' = JMSDeliveryMode";
+
+ assertEquals("color = 'red' AND 'NON_DURABLE' = JBMDurable", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "color = 'red' AND 'PERSISTENT' = JMSDeliveryMode";
+
+ assertEquals("color = 'red' AND 'DURABLE' = JBMDurable", SelectorTranslator.convertToJBMFilterString(selector));
+
+ checkNoSubstitute("JMSDeliveryMode");
+ }
+
+ public void testParseJMSPriority()
+ {
+ String selector = "JMSPriority=5";
+
+ assertEquals("JBMPriority=5", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSPriority = 7";
+
+ assertEquals(" JBMPriority = 7", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSPriority = 7 OR 1 = JMSPriority AND (JMSPriority= 1 + 4)";
+
+ assertEquals(" JBMPriority = 7 OR 1 = JBMPriority AND (JBMPriority= 1 + 4)", SelectorTranslator.convertToJBMFilterString(selector));
+
+ checkNoSubstitute("JMSPriority");
+
+ selector = "animal = 'lion' JMSPriority = 321 OR animal_name = 'xyzJMSPriorityxyz'";
+
+ assertEquals("animal = 'lion' JBMPriority = 321 OR animal_name = 'xyzJMSPriorityxyz'", SelectorTranslator.convertToJBMFilterString(selector));
+
+ }
+
+ public void testParseJMSMessageID()
+ {
+ String selector = "JMSMessageID='ID:JBM-12435678";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSMessageID='ID:JBM-12435678";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSMessageID = 'ID:JBM-12435678";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " myHeader = JMSMessageID";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " myHeader = JMSMessageID OR (JMSMessageID = 'ID-JBM' + '12345')";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ checkNoSubstitute("JMSMessageID");
+ }
+
+ public void testParseJMSTimestamp()
+ {
+ String selector = "JMSTimestamp=12345678";
+
+ assertEquals("JBMTimestamp=12345678", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSTimestamp=12345678";
+
+ assertEquals(" JBMTimestamp=12345678", SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSTimestamp=12345678 OR 78766 = JMSTimestamp AND (JMSTimestamp= 1 + 4878787)";
+
+ assertEquals(" JBMTimestamp=12345678 OR 78766 = JBMTimestamp AND (JBMTimestamp= 1 + 4878787)", SelectorTranslator.convertToJBMFilterString(selector));
+
+
+ checkNoSubstitute("JMSTimestamp");
+
+ selector = "animal = 'lion' JMSTimestamp = 321 OR animal_name = 'xyzJMSTimestampxyz'";
+
+ assertEquals("animal = 'lion' JBMTimestamp = 321 OR animal_name = 'xyzJMSTimestampxyz'", SelectorTranslator.convertToJBMFilterString(selector));
+
+ }
+
+ public void testParseJMSCorrelationID()
+ {
+ String selector = "JMSCorrelationID='ID:JBM-12435678";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSCorrelationID='ID:JBM-12435678";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSCorrelationID = 'ID:JBM-12435678";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " myHeader = JMSCorrelationID";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " myHeader = JMSCorrelationID OR (JMSCorrelationID = 'ID-JBM' + '12345')";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ checkNoSubstitute("JMSCorrelationID");
+ }
+
+ public void testParseJMSType()
+ {
+ String selector = "JMSType='aardvark'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSType='aardvark'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " JMSType = 'aardvark'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " myHeader = JMSType";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = " myHeader = JMSType OR (JMSType = 'aardvark' + 'sandwich')";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ checkNoSubstitute("JMSType");
+ }
+
+ // Private -------------------------------------------------------------------------------------
+
+ private void checkNoSubstitute(String fieldName)
+ {
+ String selector = "Other" + fieldName + " = 767868";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "cheese = 'cheddar' AND Wrong" + fieldName +" = 54";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "fruit = 'pomegranate' AND " + fieldName + "NotThisOne = 'tuesday'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = '" + fieldName + "'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = ' " + fieldName + "'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = ' " + fieldName + " '";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = 'xyz " + fieldName +"'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = 'xyz" + fieldName + "'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = '" + fieldName + "xyz'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+
+ selector = "animal = 'lion' AND animal_name = 'xyz" + fieldName + "xyz'";
+
+ assertEquals(selector, SelectorTranslator.convertToJBMFilterString(selector));
+ }
+
+}
More information about the jboss-cvs-commits
mailing list