[hornetq-commits] JBoss hornetq SVN: r11961 - in trunk: tests/unit-tests/src/test/java/org/hornetq/tests/unit/util and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Jan 4 09:11:58 EST 2012


Author: borges
Date: 2012-01-04 09:11:58 -0500 (Wed, 04 Jan 2012)
New Revision: 11961

Modified:
   trunk/hornetq-commons/src/main/java/org/hornetq/api/core/SimpleString.java
   trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java
Log:
HORNETQ-822 Fix SimpleString.charAt(int) conversion error

Modified: trunk/hornetq-commons/src/main/java/org/hornetq/api/core/SimpleString.java
===================================================================
--- trunk/hornetq-commons/src/main/java/org/hornetq/api/core/SimpleString.java	2012-01-04 13:26:24 UTC (rev 11960)
+++ trunk/hornetq-commons/src/main/java/org/hornetq/api/core/SimpleString.java	2012-01-04 14:11:58 UTC (rev 11961)
@@ -25,7 +25,7 @@
  * this minimises expensive copying between String objects.
  *
  * This object is used heavily throughout HornetQ for performance reasons.
- * 
+ *
  * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
  *
  */
@@ -116,7 +116,7 @@
       }
       pos <<= 1;
 
-      return (char)(data[pos] | data[pos + 1] << 8);
+      return (char)((data[pos] & 0xFF) | (data[pos + 1] << 8) & 0xFF00);
    }
 
    public CharSequence subSequence(final int start, final int end)
@@ -215,7 +215,7 @@
       {
          return true;
       }
-      
+
       if (other instanceof SimpleString)
       {
          SimpleString s = (SimpleString)other;
@@ -258,9 +258,8 @@
    }
 
    /**
-    * splits this SimpleString into an array of SimpleString using the char param as the delimeter.
-    *
-    * i.e. "a.b" would return "a" and "b" if . was the delimeter
+    * Splits this SimpleString into an array of SimpleString using the char param as the delimiter.
+    * i.e. "a.b" would return "a" and "b" if . was the delimiter
     * @param delim
     */
    public SimpleString[] split(final char delim)
@@ -272,11 +271,13 @@
       else
       {
          List<SimpleString> all = new ArrayList<SimpleString>();
+
+         byte low = (byte)(delim & 0xFF); // low byte
+         byte high = (byte)(delim >> 8 & 0xFF); // high byte
+
          int lasPos = 0;
          for (int i = 0; i < data.length; i += 2)
          {
-            byte low = (byte)(delim & 0xFF); // low byte
-            byte high = (byte)(delim >> 8 & 0xFF); // high byte
             if (data[i] == low && data[i + 1] == high)
             {
                byte[] bytes = new byte[i - lasPos];
@@ -301,10 +302,11 @@
     */
    public boolean contains(final char c)
    {
+      final byte low = (byte)(c & 0xFF); // low byte
+      final byte high = (byte)(c >> 8 & 0xFF); // high byte
+
       for (int i = 0; i < data.length; i += 2)
       {
-         byte low = (byte)(c & 0xFF); // low byte
-         byte high = (byte)(c >> 8 & 0xFF); // high byte
          if (data[i] == low && data[i + 1] == high)
          {
             return true;
@@ -314,10 +316,9 @@
    }
 
    /**
-    * concatanates a SimpleString and a String
-    *
-    * @param toAdd the String to concate with.
-    * @return the concatanated SimpleString
+    * Concatenates a SimpleString and a String
+    * @param toAdd the String to concatenate with.
+    * @return the concatenated SimpleString
     */
    public SimpleString concat(final String toAdd)
    {
@@ -325,10 +326,9 @@
    }
 
    /**
-    * concatanates 2 SimpleString's
-    *
-    * @param toAdd the SimpleString to concate with.
-    * @return the concatanated SimpleString
+    * Concatenates 2 SimpleString's
+    * @param toAdd the SimpleString to concatenate with.
+    * @return the concatenated SimpleString
     */
    public SimpleString concat(final SimpleString toAdd)
    {
@@ -339,10 +339,9 @@
    }
 
    /**
-    * concatanates a SimpleString and a char
-    *
+    * Concatenates a SimpleString and a char
     * @param c the char to concate with.
-    * @return the concatanated SimpleString
+    * @return the concatenated SimpleString
     */
    public SimpleString concat(final char c)
    {
@@ -390,7 +389,7 @@
    }
 
    /**
-    * 
+    *
     * @param srcBegin
     * @param srcEnd
     * @param dst

Modified: trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java
===================================================================
--- trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java	2012-01-04 13:26:24 UTC (rev 11960)
+++ trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java	2012-01-04 14:11:58 UTC (rev 11961)
@@ -16,21 +16,63 @@
 import java.util.concurrent.CountDownLatch;
 
 import junit.framework.Assert;
+import junit.framework.TestCase;
 
 import org.hornetq.api.core.SimpleString;
 import org.hornetq.tests.util.RandomUtil;
-import org.hornetq.tests.util.UnitTestCase;
 import org.hornetq.utils.DataConstants;
 
 /**
- * 
+ *
  * A SimpleStringTest
- * 
+ *
  * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
  *
  */
-public class SimpleStringTest extends UnitTestCase
+public class SimpleStringTest extends TestCase
 {
+   /**
+    * Converting back and forth between char and byte requires care as char is unsigned.
+    * @see SimpleString#getChars(int, int, char[], int)
+    * @see SimpleString#charAt(int)
+    * @see SimpleString#split(char)
+    */
+   public void testGetChar()
+   {
+      SimpleString p1 = new SimpleString("foo");
+      SimpleString p2 = new SimpleString("bar");
+      for (int i = 0; i < 1 << 16; i++)
+      {
+         String msg = "expecting " + i;
+         char c = (char)i;
+         SimpleString s = new SimpleString(String.valueOf(c));
+
+         char[] c1 = new char[1];
+         s.getChars(0, 1, c1, 0);
+         assertEquals(msg, c, c1[0]);
+         assertEquals(msg, c, s.charAt(0));
+         SimpleString s2 = s.concat(c);
+         assertEquals(msg, c, s2.charAt(1));
+
+         // test splitting with chars
+         SimpleString sSplit = new SimpleString("foo" + String.valueOf(c) + "bar");
+         SimpleString[] chunks = sSplit.split(c);
+         SimpleString[] split1 = p1.split(c);
+         SimpleString[] split2 = p2.split(c);
+         assertEquals(split1.length + split2.length, chunks.length);
+         int j = 0;
+
+         for (SimpleString iS : split1)
+         {
+            assertEquals(iS.toString(), iS, chunks[j++]);
+         }
+         for (SimpleString iS : split2)
+         {
+            assertEquals(iS.toString(), iS, chunks[j++]);
+         }
+      }
+   }
+
    public void testString() throws Exception
    {
       final String str = "hello123ABC__524`16254`6125!%^$!%$!%$!%$!%!$%!$$!\uA324";



More information about the hornetq-commits mailing list