Author: borges
Date: 2012-01-05 06:28:33 -0500 (Thu, 05 Jan 2012)
New Revision: 11968
Added:
trunk/hornetq-core/src/test/java/org/hornetq/tests/util/SimpleStringTest.java
Removed:
trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java
Modified:
trunk/hornetq-commons/src/main/java/org/hornetq/api/core/SimpleString.java
Log:
HORNETQ-822 Avoid unnecessary call to contains(char) within split(char)
Also move SimpleStringTest into hornetq-core.
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-05
02:01:37 UTC (rev 11967)
+++ trunk/hornetq-commons/src/main/java/org/hornetq/api/core/SimpleString.java 2012-01-05
11:28:33 UTC (rev 11968)
@@ -17,7 +17,6 @@
import java.util.ArrayList;
import java.util.List;
-import org.hornetq.core.logging.Logger;
import org.hornetq.utils.DataConstants;
/**
@@ -34,8 +33,6 @@
{
private static final long serialVersionUID = 4204223851422244307L;
- private static final Logger log = Logger.getLogger(SimpleString.class);
-
// Attributes
// ------------------------------------------------------------------------
private final byte[] data;
@@ -264,12 +261,6 @@
*/
public SimpleString[] split(final char delim)
{
- if (!contains(delim))
- {
- return new SimpleString[] { this };
- }
- else
- {
List<SimpleString> all = new ArrayList<SimpleString>();
byte low = (byte)(delim & 0xFF); // low byte
@@ -286,12 +277,16 @@
all.add(new SimpleString(bytes));
}
}
+
+ if (all.isEmpty())
+ {
+ return new SimpleString[] { this };
+ }
byte[] bytes = new byte[data.length - lasPos];
System.arraycopy(data, lasPos, bytes, 0, bytes.length);
all.add(new SimpleString(bytes));
SimpleString[] parts = new SimpleString[all.size()];
return all.toArray(parts);
- }
}
/**
Copied: trunk/hornetq-core/src/test/java/org/hornetq/tests/util/SimpleStringTest.java
(from rev 11965,
trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java)
===================================================================
--- trunk/hornetq-core/src/test/java/org/hornetq/tests/util/SimpleStringTest.java
(rev 0)
+++
trunk/hornetq-core/src/test/java/org/hornetq/tests/util/SimpleStringTest.java 2012-01-05
11:28:33 UTC (rev 11968)
@@ -0,0 +1,439 @@
+/*
+ * Copyright 2009 Red Hat, Inc.
+ * Red Hat licenses this file to you under the Apache License, version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package org.hornetq.tests.util;
+
+import java.util.concurrent.CountDownLatch;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.hornetq.api.core.SimpleString;
+import org.hornetq.utils.DataConstants;
+
+/**
+ *
+ * A SimpleStringTest
+ *
+ * @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
+ *
+ */
+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)
+ * @see SimpleString#concat(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));
+
+ // test getChars(...)
+ char[] c1 = new char[1];
+ s.getChars(0, 1, c1, 0);
+ assertEquals(msg, c, c1[0]);
+
+ // test charAt(int)
+ assertEquals(msg, c, s.charAt(0));
+
+ // test concat(char)
+ 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";
+
+ SimpleString s = new SimpleString(str);
+
+ Assert.assertEquals(str, s.toString());
+
+ Assert.assertEquals(2 * str.length(), s.getData().length);
+
+ byte[] data = s.getData();
+
+ SimpleString s2 = new SimpleString(data);
+
+ Assert.assertEquals(str, s2.toString());
+ }
+
+ public void testStartsWith() throws Exception
+ {
+ SimpleString s1 = new SimpleString("abcdefghi");
+
+ Assert.assertTrue(s1.startsWith(new SimpleString("abc")));
+
+ Assert.assertTrue(s1.startsWith(new SimpleString("abcdef")));
+
+ Assert.assertTrue(s1.startsWith(new SimpleString("abcdefghi")));
+
+ Assert.assertFalse(s1.startsWith(new SimpleString("abcdefghijklmn")));
+
+ Assert.assertFalse(s1.startsWith(new SimpleString("aardvark")));
+
+ Assert.assertFalse(s1.startsWith(new SimpleString("z")));
+ }
+
+ public void testCharSequence() throws Exception
+ {
+ String s = "abcdefghijkl";
+ SimpleString s1 = new SimpleString(s);
+
+ Assert.assertEquals('a', s1.charAt(0));
+ Assert.assertEquals('b', s1.charAt(1));
+ Assert.assertEquals('c', s1.charAt(2));
+ Assert.assertEquals('k', s1.charAt(10));
+ Assert.assertEquals('l', s1.charAt(11));
+
+ try
+ {
+ s1.charAt(-1);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.charAt(-2);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.charAt(s.length());
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.charAt(s.length() + 1);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ Assert.assertEquals(s.length(), s1.length());
+
+ CharSequence ss = s1.subSequence(0, s1.length());
+
+ Assert.assertEquals(ss, s1);
+
+ ss = s1.subSequence(1, 4);
+ Assert.assertEquals(ss, new SimpleString("bcd"));
+
+ ss = s1.subSequence(5, 10);
+ Assert.assertEquals(ss, new SimpleString("fghij"));
+
+ ss = s1.subSequence(5, 12);
+ Assert.assertEquals(ss, new SimpleString("fghijkl"));
+
+ try
+ {
+ s1.subSequence(-1, 2);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.subSequence(-4, -2);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.subSequence(0, s1.length() + 1);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.subSequence(0, s1.length() + 2);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+
+ try
+ {
+ s1.subSequence(5, 1);
+ Assert.fail("Should throw exception");
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ // OK
+ }
+ }
+
+ public void testEquals() throws Exception
+ {
+ Assert.assertFalse(new SimpleString("abcdef").equals(new Object()));
+
+ Assert.assertEquals(new SimpleString("abcdef"), new
SimpleString("abcdef"));
+
+ Assert.assertFalse(new SimpleString("abcdef").equals(new
SimpleString("abggcdef")));
+ Assert.assertFalse(new SimpleString("abcdef").equals(new
SimpleString("ghijkl")));
+ }
+
+ public void testHashcode() throws Exception
+ {
+ SimpleString str = new SimpleString("abcdef");
+ SimpleString sameStr = new SimpleString("abcdef");
+ SimpleString differentStr = new SimpleString("ghijk");
+
+ Assert.assertTrue(str.hashCode() == sameStr.hashCode());
+ Assert.assertFalse(str.hashCode() == differentStr.hashCode());
+ }
+
+ public void testUnicode() throws Exception
+ {
+ String myString =
"abcdef&^*&!^ghijkl\uB5E2\uCAC7\uB2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5";
+
+ SimpleString s = new SimpleString(myString);
+ byte[] data = s.getData();
+ s = new SimpleString(data);
+
+ Assert.assertEquals(myString, s.toString());
+ }
+
+ public void testUnicodeWithSurrogates() throws Exception
+ {
+ String myString = "abcdef&^*&!^ghijkl\uD900\uDD00";
+
+ SimpleString s = new SimpleString(myString);
+ byte[] data = s.getData();
+ s = new SimpleString(data);
+
+ Assert.assertEquals(myString, s.toString());
+ }
+
+ public void testSizeofString() throws Exception
+ {
+ Assert.assertEquals(DataConstants.SIZE_INT, SimpleString.sizeofString(new
SimpleString("")));
+
+ SimpleString str = new SimpleString(RandomUtil.randomString());
+ Assert.assertEquals(DataConstants.SIZE_INT + str.getData().length,
SimpleString.sizeofString(str));
+ }
+
+ public void testSizeofNullableString() throws Exception
+ {
+ Assert.assertEquals(1, SimpleString.sizeofNullableString(null));
+
+ Assert.assertEquals(1 + DataConstants.SIZE_INT,
SimpleString.sizeofNullableString(new SimpleString("")));
+
+ SimpleString str = new SimpleString(RandomUtil.randomString());
+ Assert.assertEquals(1 + DataConstants.SIZE_INT + str.getData().length,
SimpleString.sizeofNullableString(str));
+ }
+
+ public void testSplitNoDelimeter() throws Exception
+ {
+ SimpleString s = new SimpleString("abcdefghi");
+ SimpleString[] strings = s.split('.');
+ Assert.assertNotNull(strings);
+ Assert.assertEquals(strings.length, 1);
+ Assert.assertEquals(strings[0], s);
+ }
+
+ public void testSplit1Delimeter() throws Exception
+ {
+ SimpleString s = new SimpleString("abcd.efghi");
+ SimpleString[] strings = s.split('.');
+ Assert.assertNotNull(strings);
+ Assert.assertEquals(strings.length, 2);
+ Assert.assertEquals(strings[0], new SimpleString("abcd"));
+ Assert.assertEquals(strings[1], new SimpleString("efghi"));
+ }
+
+ public void testSplitmanyDelimeters() throws Exception
+ {
+ SimpleString s = new SimpleString("abcd.efghi.jklmn.opqrs.tuvw.xyz");
+ SimpleString[] strings = s.split('.');
+ Assert.assertNotNull(strings);
+ Assert.assertEquals(strings.length, 6);
+ Assert.assertEquals(strings[0], new SimpleString("abcd"));
+ Assert.assertEquals(strings[1], new SimpleString("efghi"));
+ Assert.assertEquals(strings[2], new SimpleString("jklmn"));
+ Assert.assertEquals(strings[3], new SimpleString("opqrs"));
+ Assert.assertEquals(strings[4], new SimpleString("tuvw"));
+ Assert.assertEquals(strings[5], new SimpleString("xyz"));
+ }
+
+ public void testContains()
+ {
+ SimpleString simpleString = new SimpleString("abcdefghijklmnopqrst");
+ Assert.assertFalse(simpleString.contains('.'));
+ Assert.assertFalse(simpleString.contains('%'));
+ Assert.assertFalse(simpleString.contains('8'));
+ Assert.assertFalse(simpleString.contains('.'));
+ Assert.assertTrue(simpleString.contains('a'));
+ Assert.assertTrue(simpleString.contains('b'));
+ Assert.assertTrue(simpleString.contains('c'));
+ Assert.assertTrue(simpleString.contains('d'));
+ Assert.assertTrue(simpleString.contains('e'));
+ Assert.assertTrue(simpleString.contains('f'));
+ Assert.assertTrue(simpleString.contains('g'));
+ Assert.assertTrue(simpleString.contains('h'));
+ Assert.assertTrue(simpleString.contains('i'));
+ Assert.assertTrue(simpleString.contains('j'));
+ Assert.assertTrue(simpleString.contains('k'));
+ Assert.assertTrue(simpleString.contains('l'));
+ Assert.assertTrue(simpleString.contains('m'));
+ Assert.assertTrue(simpleString.contains('n'));
+ Assert.assertTrue(simpleString.contains('o'));
+ Assert.assertTrue(simpleString.contains('p'));
+ Assert.assertTrue(simpleString.contains('q'));
+ Assert.assertTrue(simpleString.contains('r'));
+ Assert.assertTrue(simpleString.contains('s'));
+ Assert.assertTrue(simpleString.contains('t'));
+ }
+
+ public void testConcat()
+ {
+ SimpleString start = new SimpleString("abcdefg");
+ SimpleString middle = new SimpleString("hijklmnop");
+ SimpleString end = new SimpleString("qrstuvwxyz");
+ Assert.assertEquals(start.concat(middle).concat(end), new
SimpleString("abcdefghijklmnopqrstuvwxyz"));
+ Assert.assertEquals(start.concat('.').concat(end), new
SimpleString("abcdefg.qrstuvwxyz"));
+ // Testing concat of SimpleString with String
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.assertEquals(new SimpleString("abcdefg-" + i),
start.concat("-" + Integer.toString(i)));
+
+ }
+ }
+
+ public void testMultithreadHashCode() throws Exception
+ {
+ for (int repeat = 0; repeat < 10; repeat++)
+ {
+
+ StringBuffer buffer = new StringBuffer();
+
+ for (int i = 0; i < 100; i++)
+ {
+ buffer.append("Some Big String " + i);
+ }
+ String strvalue = buffer.toString();
+
+ final int initialhash = new SimpleString(strvalue).hashCode();
+
+ final SimpleString value = new SimpleString(strvalue);
+
+ int nThreads = 100;
+ final CountDownLatch latch = new CountDownLatch(nThreads);
+ final CountDownLatch start = new CountDownLatch(1);
+
+ class T extends Thread
+ {
+ boolean failed = false;
+
+ @Override
+ public void run()
+ {
+ try
+ {
+ latch.countDown();
+ start.await();
+
+ int newhash = value.hashCode();
+
+ if (newhash != initialhash)
+ {
+ failed = true;
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ failed = true;
+ }
+ }
+ }
+
+ T x[] = new T[nThreads];
+ for (int i = 0; i < nThreads; i++)
+ {
+ x[i] = new T();
+ x[i].start();
+ }
+
+ latch.await();
+ start.countDown();
+
+ for (T t : x)
+ {
+ t.join();
+ }
+
+ for (T t : x)
+ {
+ Assert.assertFalse(t.failed);
+ }
+ }
+ }
+
+}
Deleted:
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-05
02:01:37 UTC (rev 11967)
+++
trunk/tests/unit-tests/src/test/java/org/hornetq/tests/unit/util/SimpleStringTest.java 2012-01-05
11:28:33 UTC (rev 11968)
@@ -1,434 +0,0 @@
-/*
- * Copyright 2009 Red Hat, Inc.
- * Red Hat licenses this file to you under the Apache License, version
- * 2.0 (the "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package org.hornetq.tests.unit.util;
-
-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.utils.DataConstants;
-
-/**
- *
- * A SimpleStringTest
- *
- * @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
- *
- */
-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";
-
- SimpleString s = new SimpleString(str);
-
- Assert.assertEquals(str, s.toString());
-
- Assert.assertEquals(2 * str.length(), s.getData().length);
-
- byte[] data = s.getData();
-
- SimpleString s2 = new SimpleString(data);
-
- Assert.assertEquals(str, s2.toString());
- }
-
- public void testStartsWith() throws Exception
- {
- SimpleString s1 = new SimpleString("abcdefghi");
-
- Assert.assertTrue(s1.startsWith(new SimpleString("abc")));
-
- Assert.assertTrue(s1.startsWith(new SimpleString("abcdef")));
-
- Assert.assertTrue(s1.startsWith(new SimpleString("abcdefghi")));
-
- Assert.assertFalse(s1.startsWith(new SimpleString("abcdefghijklmn")));
-
- Assert.assertFalse(s1.startsWith(new SimpleString("aardvark")));
-
- Assert.assertFalse(s1.startsWith(new SimpleString("z")));
- }
-
- public void testCharSequence() throws Exception
- {
- String s = "abcdefghijkl";
- SimpleString s1 = new SimpleString(s);
-
- Assert.assertEquals('a', s1.charAt(0));
- Assert.assertEquals('b', s1.charAt(1));
- Assert.assertEquals('c', s1.charAt(2));
- Assert.assertEquals('k', s1.charAt(10));
- Assert.assertEquals('l', s1.charAt(11));
-
- try
- {
- s1.charAt(-1);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.charAt(-2);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.charAt(s.length());
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.charAt(s.length() + 1);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- Assert.assertEquals(s.length(), s1.length());
-
- CharSequence ss = s1.subSequence(0, s1.length());
-
- Assert.assertEquals(ss, s1);
-
- ss = s1.subSequence(1, 4);
- Assert.assertEquals(ss, new SimpleString("bcd"));
-
- ss = s1.subSequence(5, 10);
- Assert.assertEquals(ss, new SimpleString("fghij"));
-
- ss = s1.subSequence(5, 12);
- Assert.assertEquals(ss, new SimpleString("fghijkl"));
-
- try
- {
- s1.subSequence(-1, 2);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.subSequence(-4, -2);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.subSequence(0, s1.length() + 1);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.subSequence(0, s1.length() + 2);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
-
- try
- {
- s1.subSequence(5, 1);
- Assert.fail("Should throw exception");
- }
- catch (IndexOutOfBoundsException e)
- {
- // OK
- }
- }
-
- public void testEquals() throws Exception
- {
- Assert.assertFalse(new SimpleString("abcdef").equals(new Object()));
-
- Assert.assertEquals(new SimpleString("abcdef"), new
SimpleString("abcdef"));
-
- Assert.assertFalse(new SimpleString("abcdef").equals(new
SimpleString("abggcdef")));
- Assert.assertFalse(new SimpleString("abcdef").equals(new
SimpleString("ghijkl")));
- }
-
- public void testHashcode() throws Exception
- {
- SimpleString str = new SimpleString("abcdef");
- SimpleString sameStr = new SimpleString("abcdef");
- SimpleString differentStr = new SimpleString("ghijk");
-
- Assert.assertTrue(str.hashCode() == sameStr.hashCode());
- Assert.assertFalse(str.hashCode() == differentStr.hashCode());
- }
-
- public void testUnicode() throws Exception
- {
- String myString =
"abcdef&^*&!^ghijkl\uB5E2\uCAC7\uB2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5";
-
- SimpleString s = new SimpleString(myString);
- byte[] data = s.getData();
- s = new SimpleString(data);
-
- Assert.assertEquals(myString, s.toString());
- }
-
- public void testUnicodeWithSurrogates() throws Exception
- {
- String myString = "abcdef&^*&!^ghijkl\uD900\uDD00";
-
- SimpleString s = new SimpleString(myString);
- byte[] data = s.getData();
- s = new SimpleString(data);
-
- Assert.assertEquals(myString, s.toString());
- }
-
- public void testSizeofString() throws Exception
- {
- Assert.assertEquals(DataConstants.SIZE_INT, SimpleString.sizeofString(new
SimpleString("")));
-
- SimpleString str = new SimpleString(RandomUtil.randomString());
- Assert.assertEquals(DataConstants.SIZE_INT + str.getData().length,
SimpleString.sizeofString(str));
- }
-
- public void testSizeofNullableString() throws Exception
- {
- Assert.assertEquals(1, SimpleString.sizeofNullableString(null));
-
- Assert.assertEquals(1 + DataConstants.SIZE_INT,
SimpleString.sizeofNullableString(new SimpleString("")));
-
- SimpleString str = new SimpleString(RandomUtil.randomString());
- Assert.assertEquals(1 + DataConstants.SIZE_INT + str.getData().length,
SimpleString.sizeofNullableString(str));
- }
-
- public void testSplitNoDelimeter() throws Exception
- {
- SimpleString s = new SimpleString("abcdefghi");
- SimpleString[] strings = s.split('.');
- Assert.assertNotNull(strings);
- Assert.assertEquals(strings.length, 1);
- Assert.assertEquals(strings[0], s);
- }
-
- public void testSplit1Delimeter() throws Exception
- {
- SimpleString s = new SimpleString("abcd.efghi");
- SimpleString[] strings = s.split('.');
- Assert.assertNotNull(strings);
- Assert.assertEquals(strings.length, 2);
- Assert.assertEquals(strings[0], new SimpleString("abcd"));
- Assert.assertEquals(strings[1], new SimpleString("efghi"));
- }
-
- public void testSplitmanyDelimeters() throws Exception
- {
- SimpleString s = new SimpleString("abcd.efghi.jklmn.opqrs.tuvw.xyz");
- SimpleString[] strings = s.split('.');
- Assert.assertNotNull(strings);
- Assert.assertEquals(strings.length, 6);
- Assert.assertEquals(strings[0], new SimpleString("abcd"));
- Assert.assertEquals(strings[1], new SimpleString("efghi"));
- Assert.assertEquals(strings[2], new SimpleString("jklmn"));
- Assert.assertEquals(strings[3], new SimpleString("opqrs"));
- Assert.assertEquals(strings[4], new SimpleString("tuvw"));
- Assert.assertEquals(strings[5], new SimpleString("xyz"));
- }
-
- public void testContains()
- {
- SimpleString simpleString = new SimpleString("abcdefghijklmnopqrst");
- Assert.assertFalse(simpleString.contains('.'));
- Assert.assertFalse(simpleString.contains('%'));
- Assert.assertFalse(simpleString.contains('8'));
- Assert.assertFalse(simpleString.contains('.'));
- Assert.assertTrue(simpleString.contains('a'));
- Assert.assertTrue(simpleString.contains('b'));
- Assert.assertTrue(simpleString.contains('c'));
- Assert.assertTrue(simpleString.contains('d'));
- Assert.assertTrue(simpleString.contains('e'));
- Assert.assertTrue(simpleString.contains('f'));
- Assert.assertTrue(simpleString.contains('g'));
- Assert.assertTrue(simpleString.contains('h'));
- Assert.assertTrue(simpleString.contains('i'));
- Assert.assertTrue(simpleString.contains('j'));
- Assert.assertTrue(simpleString.contains('k'));
- Assert.assertTrue(simpleString.contains('l'));
- Assert.assertTrue(simpleString.contains('m'));
- Assert.assertTrue(simpleString.contains('n'));
- Assert.assertTrue(simpleString.contains('o'));
- Assert.assertTrue(simpleString.contains('p'));
- Assert.assertTrue(simpleString.contains('q'));
- Assert.assertTrue(simpleString.contains('r'));
- Assert.assertTrue(simpleString.contains('s'));
- Assert.assertTrue(simpleString.contains('t'));
- }
-
- public void testConcat()
- {
- SimpleString start = new SimpleString("abcdefg");
- SimpleString middle = new SimpleString("hijklmnop");
- SimpleString end = new SimpleString("qrstuvwxyz");
- Assert.assertEquals(start.concat(middle).concat(end), new
SimpleString("abcdefghijklmnopqrstuvwxyz"));
- Assert.assertEquals(start.concat('.').concat(end), new
SimpleString("abcdefg.qrstuvwxyz"));
- // Testing concat of SimpleString with String
- for (int i = 0; i < 10; i++)
- {
- Assert.assertEquals(new SimpleString("abcdefg-" + i),
start.concat("-" + Integer.toString(i)));
-
- }
- }
-
- public void testMultithreadHashCode() throws Exception
- {
- for (int repeat = 0; repeat < 10; repeat++)
- {
-
- StringBuffer buffer = new StringBuffer();
-
- for (int i = 0; i < 100; i++)
- {
- buffer.append("Some Big String " + i);
- }
- String strvalue = buffer.toString();
-
- final int initialhash = new SimpleString(strvalue).hashCode();
-
- final SimpleString value = new SimpleString(strvalue);
-
- int nThreads = 100;
- final CountDownLatch latch = new CountDownLatch(nThreads);
- final CountDownLatch start = new CountDownLatch(1);
-
- class T extends Thread
- {
- boolean failed = false;
-
- @Override
- public void run()
- {
- try
- {
- latch.countDown();
- start.await();
-
- int newhash = value.hashCode();
-
- if (newhash != initialhash)
- {
- failed = true;
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- failed = true;
- }
- }
- }
-
- T x[] = new T[nThreads];
- for (int i = 0; i < nThreads; i++)
- {
- x[i] = new T();
- x[i].start();
- }
-
- latch.await();
- start.countDown();
-
- for (T t : x)
- {
- t.join();
- }
-
- for (T t : x)
- {
- Assert.assertFalse(t.failed);
- }
- }
- }
-
-}