[jboss-cvs] JBoss Messaging SVN: r5242 - in trunk: src/main/org/jboss/messaging/util and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Nov 3 08:48:03 EST 2008


Author: jmesnil
Date: 2008-11-03 08:48:03 -0500 (Mon, 03 Nov 2008)
New Revision: 5242

Added:
   trunk/src/main/org/jboss/messaging/util/SimpleStringReader.java
Modified:
   trunk/src/main/org/jboss/messaging/core/filter/impl/FilterImpl.java
   trunk/src/main/org/jboss/messaging/core/filter/impl/FilterParser.jj
   trunk/src/main/org/jboss/messaging/core/filter/impl/Identifier.java
   trunk/src/main/org/jboss/messaging/core/filter/impl/Operator.java
   trunk/src/main/org/jboss/messaging/util/SimpleString.java
   trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterParserTest.java
   trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterTest.java
Log:
JBMESSAGING-1307: Core filter should deal with SimpleString natively

The filter now deals with SimpleString instead of String
* updated FilterParser.jj to use SimpleString token instead of String token
* in Operator, replaced the STRING cases by a similar SIMPLE_STRING case
* updated the tests since the values of the identifiers must now be SimpleString (and no longer java.lang.String)
* one exception, the reg exp are still using regular String 
* added a SimpleStringReader to provide a Reader implementation (directly reading from a SimpleString) which is used by the FilterParser

Modified: trunk/src/main/org/jboss/messaging/core/filter/impl/FilterImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/filter/impl/FilterImpl.java	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/src/main/org/jboss/messaging/core/filter/impl/FilterImpl.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -35,6 +35,7 @@
 * This class implements a JBoss Messaging filter
 * 
 * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+* @author <a href="jmesnil at redhat.com">Jeff Mesnil</a>
 * 
 * JBM filters have the same syntax as JMS 1.1 selectors, but the identifiers are different.
 * 
@@ -47,6 +48,7 @@
 * JBMExpiration - the expiration of the message
 * Any other identifers that appear in a filter expression represent header values for the message
 * 
+* String values must be set as <code>SimpleString</code>, not <code>java.lang.String</code> (see JBMESSAGING-1307).
 * Derived from JBoss MQ version by
 * 
 * @author <a href="mailto:Norbert.Lataille at m4x.org">Norbert Lataille</a>
@@ -54,38 +56,57 @@
 * @author <a href="mailto:jason at planet57.com">Jason Dillon</a>
 * @author <a href="mailto:Scott.Stark at jboss.org">Scott Stark</a>
 * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
-* 
+*  
 * @version    $Revision: 3569 $
 *
 * $Id: Selector.java 3569 2008-01-15 21:14:04Z timfox $
 */
 public class FilterImpl implements Filter
 {
+
+  // Constants -----------------------------------------------------
+
   private static final Logger log = Logger.getLogger(FilterImpl.class);
+   
+  private static final SimpleString JBM_EXPIRATION = new SimpleString("JBMExpiration");
+
+  private static final SimpleString JBM_DURABLE = new SimpleString("JBMDurable");
+
+  private static final SimpleString NON_DURABLE = new SimpleString("NON_DURABLE");
+
+  private static final SimpleString DURABLE = new SimpleString("DURABLE");
+
+  private static final SimpleString JBM_TIMESTAMP = new SimpleString("JBMTimestamp");
+
+  private static final SimpleString JBM_PRIORITY = new SimpleString("JBMPriority");
+
+  private static final SimpleString JBM_MESSAGE_ID = new SimpleString("JBMMessageID");
+
+  private static final SimpleString JBM_PREFIX = new SimpleString("JBM");
+   
+  // Attributes -----------------------------------------------------
   
   private final SimpleString sfilterString;
   
-  private final String filterString;
-
-  private final Map<String, Identifier> identifiers = new HashMap<String, Identifier>();
+  private final Map<SimpleString, Identifier> identifiers = new HashMap<SimpleString, Identifier>();
   
   private final Operator operator;
   
   private final FilterParser parser = new FilterParser();
   
-  //TODO - convert to work natively with SimpleString
+  // Constructors ---------------------------------------------------
+  
   public FilterImpl(final SimpleString str) throws MessagingException
   {
-     this.filterString = str == null ? null : str.toString();
      this.sfilterString = str;
 
      try
      {
-        operator = (Operator)parser.parse(filterString, identifiers);
+        operator = (Operator)parser.parse(sfilterString, identifiers);
      }
      catch (Throwable e)
      {   	  
-        throw new MessagingException(MessagingException.INVALID_FILTER_EXPRESSION, "Invalid filter: " + filterString);
+        throw new MessagingException(MessagingException.INVALID_FILTER_EXPRESSION, "Invalid filter: " + sfilterString);
      }
   }
   
@@ -106,25 +127,15 @@
         { 
            Object val = null;
            
-           if (id.getName().startsWith("JBM"))
+           if (id.getName().startsWith(JBM_PREFIX))
            {           
-              //Look it up as header fields
-              
+              //Look it up as header fields              
               val = getHeaderFieldValue(message, id.getName());
            }
                      
            if (val == null)
            {
-              //First look it up in the headers
-              
-              //TODO - speed this up to avoid conversion
-              //Filter should be refactored to deal with SimpleString natively
-              val = message.getProperty(new SimpleString(id.getName()));             
-              
-              if (val instanceof SimpleString)
-              {
-                 val = val.toString();
-              }
+              val = message.getProperty(id.getName());             
            }
 
            id.setValue(val);
@@ -139,7 +150,7 @@
      }
      catch (Exception e)
      {
-        log.warn("Invalid filter string: " + filterString, e);
+        log.warn("Invalid filter string: " + sfilterString, e);
         
         return false;
      }
@@ -147,25 +158,25 @@
   
   // Private --------------------------------------------------------------------------
  
-  private Object getHeaderFieldValue(final ServerMessage msg, final String fieldName)
+  private Object getHeaderFieldValue(final ServerMessage msg, final SimpleString fieldName)
   {
-     if ("JBMMessageID".equals(fieldName))
+     if (JBM_MESSAGE_ID.equals(fieldName))
      {
         return msg.getMessageID();
      }
-     else if ("JBMPriority".equals(fieldName))
+     else if (JBM_PRIORITY.equals(fieldName))
      {
         return new Integer(msg.getPriority());
      }
-     else if ("JBMTimestamp".equals(fieldName))
+     else if (JBM_TIMESTAMP.equals(fieldName))
      {
         return msg.getTimestamp();
      }
-     else if ("JBMDurable".equals(fieldName))
+     else if (JBM_DURABLE.equals(fieldName))
      {
-        return msg.isDurable() ? "DURABLE" : "NON_DURABLE";
+        return msg.isDurable() ? DURABLE : NON_DURABLE;
      }
-     else if ("JBMExpiration".equals(fieldName))
+     else if (JBM_EXPIRATION.equals(fieldName))
      {
         return msg.getExpiration();
      }

Modified: trunk/src/main/org/jboss/messaging/core/filter/impl/FilterParser.jj
===================================================================
--- trunk/src/main/org/jboss/messaging/core/filter/impl/FilterParser.jj	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/src/main/org/jboss/messaging/core/filter/impl/FilterParser.jj	2008-11-03 13:48:03 UTC (rev 5242)
@@ -39,6 +39,9 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.jboss.messaging.util.SimpleString;
+import org.jboss.messaging.util.SimpleStringReader;
+
 /**
  * A JavaCC 2.0 grammar for JBoss Messaging filters
  *
@@ -63,16 +66,16 @@
       this(new StringReader(""));
    }
 
-   public Object parse(String selector, Map identifierMap)
+   public Object parse(SimpleString selector, Map identifierMap)
       throws ParseException
    {
       return parse(selector, identifierMap, false);
    }
    
-   public Object parse(String selector, Map identifierMap, boolean trace)
+   public Object parse(SimpleString selector, Map identifierMap, boolean trace)
       throws ParseException
    {
-      StringReader sr = new StringReader(selector);
+      SimpleStringReader sr = new SimpleStringReader(selector);
       ReInit(sr);
 
       // This will have no effect unless the debugging options are true      
@@ -93,7 +96,7 @@
     * Strip off the leading and trailing (quote) chars from the given string
     * and return it. 
     */
-   private String stripQuotes(String image)
+   private SimpleString stripQuotes(String image)
    {
       StringBuffer result = new StringBuffer(image.length()-2);
       int i = 1;
@@ -114,16 +117,16 @@
             result.append(image.charAt(i));
          ++i;
       }
-      return result.toString();
+      return new SimpleString(result.toString());
    }
    
-   public static Object doParse(String selector, Map identifierMap)
+   public static Object doParse(SimpleString selector, Map identifierMap)
       throws ParseException
    {
       return doParse(selector, identifierMap, false);
    }
    
-   public static Object doParse(String selector, Map identifierMap, boolean trace)
+   public static Object doParse(SimpleString selector, Map identifierMap, boolean trace)
       throws ParseException
    {
       FilterParser parser = new FilterParser();
@@ -216,7 +219,7 @@
 
 TOKEN :
 {
-   < STRING:
+   < SIMPLE_STRING:
       "'"
       (   (~["'","\n","\r"])
         | ("''")
@@ -416,7 +419,7 @@
    Token t = null;
 }
 {
-   [ t=<STRING> ]
+   [ t=<SIMPLE_STRING> ]
    {
       if (t != null)
         set.add(stripQuotes(t.image));
@@ -546,7 +549,7 @@
    Token string = null;
 }
 {
-   string=<STRING>
+   string=<SIMPLE_STRING>
    {
       return stripQuotes(string.image);
    }
@@ -630,11 +633,12 @@
 {
    id=<IDENTIFIER>
    {
-      Identifier identifier = (Identifier) identifierMap.get(id.image);
+      SimpleString simage = new SimpleString(id.image);
+      Identifier identifier = (Identifier) identifierMap.get(simage);
       if (identifier == null)
       {
-         identifier = new Identifier(id.image);
-         identifierMap.put(id.image, identifier);
+         identifier = new Identifier(simage);
+         identifierMap.put(simage, identifier);
 	   }
       return identifier;
    }

Modified: trunk/src/main/org/jboss/messaging/core/filter/impl/Identifier.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/filter/impl/Identifier.java	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/src/main/org/jboss/messaging/core/filter/impl/Identifier.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -22,6 +22,8 @@
 
 package org.jboss.messaging.core.filter.impl;
 
+import org.jboss.messaging.util.SimpleString;
+
 /**
  * 
  * A Identifier
@@ -34,13 +36,13 @@
  */
 public class Identifier
 {
-   private final String name;
+   private final SimpleString name;
    
    private Object value;
    
    private final int hash;
    
-   public Identifier(final String name)
+   public Identifier(final SimpleString name)
    {
       this.name = name;
       
@@ -72,7 +74,7 @@
       return hash;
    }
 
-   public String getName()
+   public SimpleString getName()
    {
       return name;
    }

Modified: trunk/src/main/org/jboss/messaging/core/filter/impl/Operator.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/filter/impl/Operator.java	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/src/main/org/jboss/messaging/core/filter/impl/Operator.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -24,6 +24,8 @@
 
 import java.util.HashSet;
 
+import org.jboss.messaging.util.SimpleString;
+
 /**
 * Implementations of the operators used in JBM filter expressions
 *
@@ -79,12 +81,12 @@
   public final static int IN = 22;
   public final static int NOT_IN = 23;
 
-  public final static int STRING = 0;
   public final static int DOUBLE = 1;
   //DOUBLE FLOAT
   public final static int LONG = 2;
   //LONG BYTE SHORT INTEGER
   public final static int BOOLEAN = 3;
+  public final static int SIMPLE_STRING = 4;
 
   public Operator(int operation, Object oper1, Object oper2, Object oper3)
   {
@@ -205,7 +207,7 @@
            if (class2 == DOUBLE)
               return Boolean.valueOf(((Number) arg1).doubleValue() == ((Number) arg2).doubleValue());
            return Boolean.FALSE;
-        case STRING:
+        case SIMPLE_STRING:
         case BOOLEAN:
            computeArgument2();
            if (arg2 == null)
@@ -464,7 +466,7 @@
            if (class2 == DOUBLE)
               return Boolean.valueOf(((Number) arg1).doubleValue() != ((Number) arg2).doubleValue());
            return Boolean.FALSE;
-        case STRING:
+        case SIMPLE_STRING:
         case BOOLEAN:
            computeArgument2();
            if (arg2 == null)
@@ -689,13 +691,13 @@
      computeArgument1();
      if (arg1 == null)
         return null;
-     if (class1 != STRING)
+     if (class1 != SIMPLE_STRING)
         throwBadObjectException(class1);
 
      computeArgument2();
      if (arg2 == null)
         return null;
-     if (class2 != STRING)
+     if (class2 != SIMPLE_STRING)
         throwBadObjectException(class2);
 
      if (use_escape)
@@ -704,7 +706,7 @@
         if (arg3 == null)
            return null;
 
-        if (class3 != STRING)
+        if (class3 != SIMPLE_STRING)
            throwBadObjectException(class3);
 
         StringBuffer escapeBuf = new StringBuffer((String) arg3);
@@ -716,7 +718,7 @@
 
      if (re == null)
         // the first time through we prepare the regular expression
-        re = new RegExp ((String) arg2, escapeChar);
+        re = new RegExp (arg2.toString(), escapeChar);
      
      boolean result = re.isMatch (arg1);
      if (not)
@@ -746,7 +748,7 @@
      computeArgument1();
      if (arg1 == null)
         return null;
-     if (class1 != STRING)
+     if (class1 != SIMPLE_STRING)
         throwBadObjectException(class1);
      if (((HashSet) oper2).contains(arg1))
         return Boolean.FALSE;
@@ -774,8 +776,8 @@
 
      className = arg1.getClass();
 
-     if (className == String.class)
-        class1 = STRING;
+     if (className == SimpleString.class)
+        class1 = SIMPLE_STRING;
      else if (className == Double.class)
         class1 = DOUBLE;
      else if (className == Long.class)
@@ -825,8 +827,8 @@
 
      className = arg2.getClass();
 
-     if (className == String.class)
-        class2 = STRING;
+     if (className == SimpleString.class)
+        class2 = SIMPLE_STRING;
      else if (className == Double.class)
         class2 = DOUBLE;
      else if (className == Long.class)
@@ -876,8 +878,8 @@
 
      className = arg3.getClass();
 
-     if (className == String.class)
-        class3 = STRING;
+     if (className == SimpleString.class)
+        class3 = SIMPLE_STRING;
      else if (className == Double.class)
         class3 = DOUBLE;
      else if (className == Long.class)
@@ -985,8 +987,8 @@
      String str = "Unknown";
      switch (class1)
      {
-        case STRING:
-           str = "String";
+        case SIMPLE_STRING:
+           str = "SimpleString";
            break;
         case LONG:
            str = "Long";

Modified: trunk/src/main/org/jboss/messaging/util/SimpleString.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/SimpleString.java	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/src/main/org/jboss/messaging/util/SimpleString.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -298,5 +298,28 @@
 		return SIZE_INT + str.data.length;
 	}
 
+   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
+      if (srcBegin < 0) {
+          throw new StringIndexOutOfBoundsException(srcBegin);
+      }
+      if (srcEnd > length()) {
+          throw new StringIndexOutOfBoundsException(srcEnd);
+      }
+      if (srcBegin > srcEnd) {
+          throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
+      }
+      
+      int j = 0;
 
+      for (int i = srcBegin; i <  srcEnd - srcBegin; i++)
+      {
+         int low = data[j++] & 0xFF;
+         
+         int high = (data[j++] << 8) & 0xFF00 ;
+         
+         dst[i] = (char)(low | high);
+      }
+  }
+
+
 }
\ No newline at end of file

Added: trunk/src/main/org/jboss/messaging/util/SimpleStringReader.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/SimpleStringReader.java	                        (rev 0)
+++ trunk/src/main/org/jboss/messaging/util/SimpleStringReader.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -0,0 +1,99 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
+ * 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.util;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A SimpleStringReader
+ *
+ * @author <a href="jmesnil at redhat.com">Jeff Mesnil</a>
+ * 
+ * Created 31 oct. 2008 14:41:18
+ *
+ *
+ */
+public class SimpleStringReader extends Reader
+{
+
+   // Constants -----------------------------------------------------
+
+   // Attributes ----------------------------------------------------
+
+   private final SimpleString simpleString;
+
+   private int next = 0;
+
+   // Static --------------------------------------------------------
+
+   // Constructors --------------------------------------------------
+
+   public SimpleStringReader(final SimpleString simpleString)
+   {
+      this.simpleString = simpleString;
+   }
+
+   // Public --------------------------------------------------------
+
+   // Reader overrides ----------------------------------------------
+
+   @Override
+   public int read(char[] cbuf, int off, int len) throws IOException
+   {
+      synchronized (simpleString)
+      {
+         if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0))
+         {
+            throw new IndexOutOfBoundsException();
+         }
+         else if (len == 0)
+         {
+            return 0;
+         }
+         int length = simpleString.length();
+         if (next >= length)
+         {
+            return -1;
+         }
+         int n = Math.min(length - next, len);
+         simpleString.getChars(next, next + n, cbuf, off);
+         next += n;
+         return n;
+      }
+   }
+
+   @Override
+   public void close() throws IOException
+   {
+   }
+
+   // Package protected ---------------------------------------------
+
+   // Protected -----------------------------------------------------
+
+   // Private -------------------------------------------------------
+
+   // Inner classes -------------------------------------------------
+
+}

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterParserTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterParserTest.java	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterParserTest.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -18,7 +18,7 @@
  * 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.tests.unit.core.filter.impl;
 
@@ -31,6 +31,7 @@
 import org.jboss.messaging.core.filter.impl.Operator;
 import org.jboss.messaging.core.logging.Logger;
 import org.jboss.messaging.tests.util.UnitTestCase;
+import org.jboss.messaging.util.SimpleString;
 
 /**
  Tests of the JavaCC LL(1) parser for the JBoss Messaging filters
@@ -47,140 +48,170 @@
 public class FilterParserTest extends UnitTestCase
 {
    private static final Logger log = Logger.getLogger(FilterParserTest.class);
-   
-   private Map<String, Identifier> identifierMap;
-   
+
+   private Map<SimpleString, 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]));      
+
+      identifierMap = new HashMap<SimpleString, 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();
+      Operator result = (Operator)parser.parse(new SimpleString("-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();
+      result = (Operator)parser.parse(new SimpleString("-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();
+      result = (Operator)parser.parse(new SimpleString("-(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();
+      Operator result = (Operator)parser.parse(new SimpleString("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();
+      result = (Operator)parser.parse(new SimpleString("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();
+      result = (Operator)parser.parse(new SimpleString("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();
+      result = (Operator)parser.parse(new SimpleString("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);
+      Operator result = (Operator)parser.parse(new SimpleString("a + b * c / d = e"), identifierMap);
       // 4 + 2 * 3 / 2 = 7
-      Identifier a = identifierMap.get("a");
+      Identifier a = identifierMap.get(new SimpleString("a"));
       a.setValue(new Long(4));
-      Identifier b = identifierMap.get("b");
+      Identifier b = identifierMap.get(new SimpleString("b"));
       b.setValue(new Long(2));
-      Identifier c = identifierMap.get("c");
+      Identifier c = identifierMap.get(new SimpleString("c"));
       c.setValue(new Long(3));
-      Identifier d = identifierMap.get("d");
+      Identifier d = identifierMap.get(new SimpleString("d"));
       d.setValue(new Long(2));
-      Identifier e = identifierMap.get("e");
+      Identifier e = identifierMap.get(new SimpleString("e"));
       e.setValue(new Long(7));
-      log.trace("result -> "+result);
-      Boolean bool = (Boolean) result.apply();
+      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();
+      Operator result = (Operator)parser.parse(new SimpleString("Status IN ('new', 'cleared', 'acknowledged')"),
+                                               identifierMap);
+      Identifier a = identifierMap.get(new SimpleString("Status"));
+      a.setValue(new SimpleString("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();
+      Operator result = (Operator)parser.parse(new SimpleString("Status IN ('new', 'cleared', 'acknowledged')"),
+                                               identifierMap);
+      Identifier a = identifierMap.get(new SimpleString("Status"));
+      a.setValue(new SimpleString("none"));
+      log.trace("result -> " + result);
+      Boolean bool = (Boolean)result.apply();
       assertTrue("is false", !bool.booleanValue());
    }
    
+   public void testTrueNOTINOperator() throws Exception
+   {
+      log.trace("parse(Status IN ('new', 'cleared', 'acknowledged'))");
+      Operator result = (Operator)parser.parse(new SimpleString("Status NOT IN ('new', 'cleared', 'acknowledged')"),
+                                               identifierMap);
+      Identifier a = identifierMap.get(new SimpleString("Status"));
+      a.setValue(new SimpleString("none"));
+      log.trace("result -> " + result);
+      Boolean bool = (Boolean)result.apply();
+      assertTrue(bool.booleanValue());
+   }
+   
+   public void testFalseNOTINOperator() throws Exception
+   {
+      log.trace("parse(Status IN ('new', 'cleared', 'acknowledged'))");
+      Operator result = (Operator)parser.parse(new SimpleString("Status NOT IN ('new', 'cleared', 'acknowledged')"),
+                                               identifierMap);
+      Identifier a = identifierMap.get(new SimpleString("Status"));
+      a.setValue(new SimpleString("new"));
+      log.trace("result -> " + result);
+      Boolean bool = (Boolean)result.apply();
+      assertFalse(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();
+      Operator result = (Operator)parser.parse(new SimpleString("(Status = 'new') OR (Status = 'cleared') OR (Status= 'acknowledged')"),
+                                               identifierMap);
+      Identifier a = identifierMap.get(new SimpleString("Status"));
+      a.setValue(new SimpleString("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();
+      Operator result = (Operator)parser.parse(new SimpleString("(Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged')"),
+                                               identifierMap);
+      Identifier a = identifierMap.get(new SimpleString("Status"));
+      a.setValue(new SimpleString("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);
+         Object result = parser.parse(new SimpleString("definitely not a message selector!"), identifierMap);
+         log.trace("result -> " + result);
          fail("Should throw an Exception.\n");
       }
       catch (Exception e)
@@ -188,7 +219,7 @@
          log.trace("testInvalidSelector failed as expected", e);
       }
    }
- 
+
    /**
     * Test diffent syntax for approximate numeric literal (+6.2, -95.7, 7.)
     */
@@ -197,53 +228,58 @@
       try
       {
          log.trace("parse(average = +6.2)");
-         Object result = parser.parse("average = +6.2", identifierMap);
-         log.trace("result -> "+result);
-      } catch (Exception e)
+         Object result = parser.parse(new SimpleString("average = +6.2"), identifierMap);
+         log.trace("result -> " + result);
+      }
+      catch (Exception e)
       {
-         fail(""+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)
+         Object result = parser.parse(new SimpleString("average = -95.7"), identifierMap);
+         log.trace("result -> " + result);
+      }
+      catch (Exception e)
       {
-         fail(""+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)
+         Object result = parser.parse(new SimpleString("average = 7."), identifierMap);
+         log.trace("result -> " + result);
+      }
+      catch (Exception e)
       {
-         fail(""+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();
+         Operator result = (Operator)parser.parse(new SimpleString("weight > 2500"), identifierMap);
+         (identifierMap.get(new SimpleString("weight"))).setValue(new Integer(3000));
+         log.trace("result -> " + result);
+         Boolean bool = (Boolean)result.apply();
          assertTrue("is true", bool.booleanValue());
-      } catch (Exception e)
+      }
+      catch (Exception e)
       {
          log.trace("failed", e);
-         fail(""+e);
+         fail("" + e);
       }
    }
 
@@ -252,15 +288,16 @@
       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();
+         Operator result = (Operator)parser.parse(new SimpleString("weight > 2500"), identifierMap);
+         (identifierMap.get(new SimpleString("weight"))).setValue(new Float(3000));
+         log.trace("result -> " + result);
+         Boolean bool = (Boolean)result.apply();
          assertTrue("is true", bool.booleanValue());
-      } catch (Exception e)
+      }
+      catch (Exception e)
       {
          log.trace("failed", e);
-         fail(""+e);
+         fail("" + e);
       }
    }
 
@@ -269,15 +306,16 @@
       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();
+         Operator result = (Operator)parser.parse(new SimpleString("weight < 1.5"), identifierMap);
+         (identifierMap.get(new SimpleString("weight"))).setValue(new Double(1.2));
+         log.trace("result -> " + result);
+         Boolean bool = (Boolean)result.apply();
          assertTrue("is true", bool.booleanValue());
-      } catch (Exception e)
+      }
+      catch (Exception e)
       {
          log.trace("failed", e);
-         fail(""+e);
+         fail("" + e);
       }
    }
 
@@ -286,36 +324,40 @@
       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();
+         Operator result = (Operator)parser.parse(new SimpleString("JMSType = 'car' AND color = 'blue' AND weight > 2500"),
+                                                  identifierMap);
+         (identifierMap.get(new SimpleString("JMSType"))).setValue(new SimpleString("car"));
+         (identifierMap.get(new SimpleString("color"))).setValue(new SimpleString("blue"));
+         (identifierMap.get(new SimpleString("weight"))).setValue(new SimpleString("3000"));
+
+         log.trace("result -> " + result);
+         Boolean bool = (Boolean)result.apply();
          assertTrue("is false", !bool.booleanValue());
-      } catch (Exception e)
+      }
+      catch (Exception e)
       {
          log.trace("failed", e);
-         fail(""+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();
+         Operator result = (Operator)parser.parse(new SimpleString("Category IN ('category1') AND Rating >= 2"),
+                                                  identifierMap);
+         (identifierMap.get(new SimpleString("Category"))).setValue(new SimpleString("category1"));
+         (identifierMap.get(new SimpleString("Rating"))).setValue(new Integer(3));
+         log.trace("result -> " + result);
+         Boolean bool = (Boolean)result.apply();
          assertTrue("is true", bool.booleanValue());
-      } catch (Exception e)
+      }
+      catch (Exception e)
       {
          log.trace("failed", e);
-         fail(""+e);
+         fail("" + e);
       }
    }
 
@@ -326,6 +368,22 @@
    {
    }
 
+   public void testParserPerf() throws Exception
+   {
+      SimpleString filter = new SimpleString("Cateogry IN ('category1') AND Rating >= 2");
+      SimpleString categoryKey = new SimpleString("Cateogry");
+      SimpleString ratingKey = new SimpleString("Rating");
+      long start = System.currentTimeMillis();
+      for (int i = 0; i < 100000; i++)
+      {
+         Operator result = (Operator)parser.parse(filter, identifierMap);
+         (identifierMap.get(categoryKey)).setValue(new SimpleString("category1"));
+         (identifierMap.get(ratingKey)).setValue(new Integer(3));
+         Boolean bool = (Boolean)result.apply();
+      }
+      System.out.println(System.currentTimeMillis() - start + " ms");
+   }
+
    public static void main(java.lang.String[] args)
    {
       junit.textui.TestRunner.run(FilterParserTest.class);

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterTest.java	2008-11-03 13:01:51 UTC (rev 5241)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/filter/impl/FilterTest.java	2008-11-03 13:48:03 UTC (rev 5242)
@@ -60,7 +60,7 @@
    {
       filter = new FilterImpl(new SimpleString("color = 'RED'"));  
 
-      message.putStringProperty(new  SimpleString("color"), new SimpleString("RED"));
+      message.putStringProperty(new SimpleString("color"), new SimpleString("RED"));
       assertTrue(filter.match(message));
       message = new ServerMessageImpl();
       assertFalse(filter.match(message));




More information about the jboss-cvs-commits mailing list