Fixes issue #591 (http://code.google.com/p/google-web-toolkit/issues/detail?id=591) and adds a regression test to StringBufferTest. Also contains some formatting and cleanup.
Patch by: tobyr
Review by: me
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@816 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/StringBuffer.java b/user/super/com/google/gwt/emul/java/lang/StringBuffer.java
index f07b78f..42bc3c3 100644
--- a/user/super/com/google/gwt/emul/java/lang/StringBuffer.java
+++ b/user/super/com/google/gwt/emul/java/lang/StringBuffer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006 Google Inc.
+ * Copyright 2007 Google Inc.
*
* Licensed 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
@@ -73,14 +73,17 @@
}
public native StringBuffer append(String toAppend) /*-{
- var last = this.js.length-1;
- var lastLength=this.js[last].length;
- if(this.length > lastLength*lastLength) {
- this.js[last]=this.js[last]+toAppend;
+ if ( toAppend === null ) {
+ toAppend = 'null';
+ }
+ var last = this.js.length - 1;
+ var lastLength = this.js[last].length;
+ if(this.length > lastLength * lastLength) {
+ this.js[last] = this.js[last] + toAppend;
} else {
this.js.push(toAppend);
}
- this.length+=toAppend.length;
+ this.length += toAppend.length;
return this;
}-*/;
@@ -171,7 +174,7 @@
start = Math.max(Math.min(this.length,start),0);
end = Math.max(Math.min(this.length,end),0);
this.length = this.length - end + start + toInsert.length;
- var i=0;
+ var i = 0;
// Searching for the start
var len = this.js[i].length;
while(i < this.js.length && len < start) {
@@ -199,7 +202,7 @@
// making sure that end falls between two chunks
if(end > 0) {
- this.js[startOfDelete]=this.js[startOfDelete].substring(end);
+ this.js[startOfDelete] = this.js[startOfDelete].substring(end);
}
this.js.splice(startOfDelete,0,toInsert);
diff --git a/user/test/com/google/gwt/emultest/java/lang/StringBufferTest.java b/user/test/com/google/gwt/emultest/java/lang/StringBufferTest.java
index e5c26c0..a8c86c0 100644
--- a/user/test/com/google/gwt/emultest/java/lang/StringBufferTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/StringBufferTest.java
@@ -22,128 +22,14 @@
*/
public class StringBufferTest extends GWTTestCase {
- public StringBufferTest() {
- testAppend();
- }
-
/**
- * This method does interleaved inserts and deletes.
+ * This method gets the module name.
+ *
+ * @return the module name.
+ * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
*/
- public void testInterleavedInsertAndDelete() {
- StringBuffer x = new StringBuffer();
- for (int i = 0; i < 9; i++) {
- x.append("1234567890");
- }
- for (int i = 0; i < 10; i++) {
- x.delete(5, 15);
- }
- assertEquals(x.toString(), "12345");
- }
-
-
- /**
- * Tests correctness under repeated insertion and append.
- */
- public void testRepeatedAppendsAndInserts() {
- StringBuffer x = new StringBuffer();
- final int SIZE = 1000;
- for (int i = 0; i < SIZE; i++) {
- x.append("" + i % 10);
- }
- assertTrue("endwith1", x.toString().endsWith("0123456789"));
- assertTrue("startswith1", x.toString().startsWith("0123456789"));
- assertEquals(x.length(), SIZE);
- x = new StringBuffer();
- for (int i = 0; i < SIZE * 4; i++) {
- x.append("" + i % 10);
- }
- assertTrue("endswith2", x.toString().endsWith("0123456789"));
- assertTrue("startswith2", x.toString().startsWith("0123456789"));
- assertEquals("length2", x.length(), SIZE * 4);
- x = new StringBuffer();
- for (int i = 0; i < SIZE; i++) {
- x.insert(0, "" + i % 10);
- }
- assertTrue("endswith3", x.toString().endsWith("9876543210"));
- assertTrue("startswith3", x.toString().startsWith("9876543210"));
- assertEquals("length3", x.length(), SIZE);
- x = new StringBuffer();
- for (int i = 0; i < SIZE * 4; i++) {
- x.insert(0, "" + i % 10);
- }
- assertTrue("endswith4", x.toString().endsWith("9876543210"));
- assertTrue("startswith4", x.toString().startsWith("9876543210"));
- assertEquals("size4", x.length(), SIZE * 4);
- }
-
- /**
- * This method tests string creation and equality.
- */
- public void testContructor() {
- String constant = "abcdef";
- assertEquals(new StringBuffer(constant).toString(), constant);
- assertEquals(new StringBuffer().toString(), "");
- }
-
- /**
- * This method tests <code>ubstring</code>.
- */
- public void testSubstring() {
- StringBuffer haystack = new StringBuffer("abcdefghi");
- assertEquals("cd", haystack.substring(2, 4));
- assertEquals("bc", "abcdef".substring(1, 3));
- assertEquals("bcdef", "abcdef".substring(1));
- }
-
- /**
- * This method tests <code>delete</code>.
- */
- public void testDelete() {
- StringBuffer haystack = new StringBuffer("abcdefghi");
- haystack.delete(2, 4);
- assertEquals(haystack.toString(), "abefghi");
- haystack.deleteCharAt(0);
- assertEquals(haystack.toString(), "befghi");
- haystack.deleteCharAt(1);
- assertEquals(haystack.toString(), "bfghi");
- }
-
- /**
- * This method tests <code>length</code>, and tests moderately long
- * StringBuffers.
- */
- public void testLength() {
- assertEquals(3, new StringBuffer("abc").length());
- StringBuffer str = new StringBuffer("x");
- for (int i = 0; i < 16; i++) {
- str.append(str);
- }
- assertEquals(1 << 16, str.length());
- }
-
- /**
- * This method tests <code>startsWith</code>.
- */
- public void testStartsWith() {
- String haystack = "abcdefghi";
- assertTrue(haystack.startsWith("abc"));
- assertTrue(haystack.startsWith("bc", 1));
- assertTrue(haystack.startsWith(haystack));
- assertFalse(haystack.startsWith(haystack + "j"));
- }
-
- /**
- * This method tests <code>indexOf</code>.
- */
- public void testIndexOf() {
- String haystack = "abcdefghi";
- assertEquals(haystack.indexOf("q"), -1);
- assertEquals(haystack.indexOf('q'), -1);
- assertEquals(haystack.indexOf("a"), 0);
- assertEquals(haystack.indexOf('a'), 0);
- assertEquals(haystack.indexOf('a', 1), -1);
- assertEquals(haystack.indexOf("bc"), 1);
- assertEquals(haystack.indexOf(""), 0);
+ public String getModuleName() {
+ return "com.google.gwt.emultest.EmulSuite";
}
/**
@@ -177,8 +63,66 @@
x = new StringBuffer();
x.append(C.TRUE_VALUE);
assertEquals(C.TRUE_STRING, x.toString());
+ x = new StringBuffer();
+ x.append((String) null);
+ assertEquals("null", x.toString());
}
+ /**
+ * This method tests <code>charAt</code>.
+ */
+ public void testCharAt() {
+ assertEquals(new StringBuffer("abc").charAt(1), 'b');
+ }
+
+ /**
+ * This method tests string creation and equality.
+ */
+ public void testContructor() {
+ String constant = "abcdef";
+ assertEquals(new StringBuffer(constant).toString(), constant);
+ assertEquals(new StringBuffer().toString(), "");
+ }
+
+ /**
+ * This method tests <code>delete</code>.
+ */
+ public void testDelete() {
+ StringBuffer haystack = new StringBuffer("abcdefghi");
+ haystack.delete(2, 4);
+ assertEquals(haystack.toString(), "abefghi");
+ haystack.deleteCharAt(0);
+ assertEquals(haystack.toString(), "befghi");
+ haystack.deleteCharAt(1);
+ assertEquals(haystack.toString(), "bfghi");
+ }
+
+ /**
+ * Tests toCharArray.
+ */
+ public void testGetChars() {
+ StringBuffer x = new StringBuffer("ABCDEFGHIJ");
+ char[] a1 = "abcdefghij".toCharArray();
+ char[] desired = "abcDEFghij".toCharArray();
+ x.getChars(3, 6, a1, 3);
+ for (int i = 0; i < a1.length; i++) {
+ assertEquals(a1[i], desired[i]);
+ }
+ }
+
+ /**
+ * This method tests <code>indexOf</code>.
+ */
+ public void testIndexOf() {
+ String haystack = "abcdefghi";
+ assertEquals(haystack.indexOf("q"), -1);
+ assertEquals(haystack.indexOf('q'), -1);
+ assertEquals(haystack.indexOf("a"), 0);
+ assertEquals(haystack.indexOf('a'), 0);
+ assertEquals(haystack.indexOf('a', 1), -1);
+ assertEquals(haystack.indexOf("bc"), 1);
+ assertEquals(haystack.indexOf(""), 0);
+ }
/**
* This method tests <code>insert</code>.
@@ -214,12 +158,17 @@
}
/**
- * This method tests <code>toLowerCase</code>.
+ * This method does interleaved inserts and deletes.
*/
- public void testLowerCase() {
- assertEquals("abc", "AbC".toLowerCase());
- assertEquals("abc", "abc".toLowerCase());
- assertEquals("", "".toLowerCase());
+ public void testInterleavedInsertAndDelete() {
+ StringBuffer x = new StringBuffer();
+ for (int i = 0; i < 9; i++) {
+ x.append("1234567890");
+ }
+ for (int i = 0; i < 10; i++) {
+ x.delete(5, 15);
+ }
+ assertEquals(x.toString(), "12345");
}
/**
@@ -232,6 +181,62 @@
assertEquals(-1, x.lastIndexOf("f", 1));
}
+ /**
+ * This method tests <code>length</code>, and tests moderately long
+ * StringBuffers.
+ */
+ public void testLength() {
+ assertEquals(3, new StringBuffer("abc").length());
+ StringBuffer str = new StringBuffer("x");
+ for (int i = 0; i < 16; i++) {
+ str.append(str);
+ }
+ assertEquals(1 << 16, str.length());
+ }
+
+ /**
+ * This method tests <code>toLowerCase</code>.
+ */
+ public void testLowerCase() {
+ assertEquals("abc", "AbC".toLowerCase());
+ assertEquals("abc", "abc".toLowerCase());
+ assertEquals("", "".toLowerCase());
+ }
+
+ /**
+ * Tests correctness under repeated insertion and append.
+ */
+ public void testRepeatedAppendsAndInserts() {
+ StringBuffer x = new StringBuffer();
+ final int size = 1000;
+ for (int i = 0; i < size; i++) {
+ x.append("" + i % 10);
+ }
+ assertTrue("endwith1", x.toString().endsWith("0123456789"));
+ assertTrue("startswith1", x.toString().startsWith("0123456789"));
+ assertEquals(x.length(), size);
+ x = new StringBuffer();
+ for (int i = 0; i < size * 4; i++) {
+ x.append("" + i % 10);
+ }
+ assertTrue("endswith2", x.toString().endsWith("0123456789"));
+ assertTrue("startswith2", x.toString().startsWith("0123456789"));
+ assertEquals("length2", x.length(), size * 4);
+ x = new StringBuffer();
+ for (int i = 0; i < size; i++) {
+ x.insert(0, "" + i % 10);
+ }
+ assertTrue("endswith3", x.toString().endsWith("9876543210"));
+ assertTrue("startswith3", x.toString().startsWith("9876543210"));
+ assertEquals("length3", x.length(), size);
+ x = new StringBuffer();
+ for (int i = 0; i < size * 4; i++) {
+ x.insert(0, "" + i % 10);
+ }
+ assertTrue("endswith4", x.toString().endsWith("9876543210"));
+ assertTrue("startswith4", x.toString().startsWith("9876543210"));
+ assertEquals("size4", x.length(), size * 4);
+ }
/**
* This method tests <code>replace</code>.
@@ -243,24 +248,6 @@
}
/**
- * This method tests <code>charAt</code>.
- */
- public void testCharAt() {
- assertEquals(new StringBuffer("abc").charAt(1), 'b');
- }
-
- /** tests toCharArray */
- public void testGetChars() {
- StringBuffer x = new StringBuffer("ABCDEFGHIJ");
- char[] a1 = "abcdefghij".toCharArray();
- char[] desired = "abcDEFghij".toCharArray();
- x.getChars(3, 6, a1, 3);
- for (int i = 0; i < a1.length; i++) {
- assertEquals(a1[i], desired[i]);
- }
- }
-
- /**
* This method tests <code>setLength</code>.
*/
public void testSetLength() {
@@ -273,16 +260,24 @@
assertEquals(x.toString(), "abcde");
}
-
/**
- * This method gets the module name.
- *
- * @return the module name.
- * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
+ * This method tests <code>startsWith</code>.
*/
- public String getModuleName() {
- return "com.google.gwt.emultest.EmulSuite";
+ public void testStartsWith() {
+ String haystack = "abcdefghi";
+ assertTrue(haystack.startsWith("abc"));
+ assertTrue(haystack.startsWith("bc", 1));
+ assertTrue(haystack.startsWith(haystack));
+ assertFalse(haystack.startsWith(haystack + "j"));
}
-
-}
\ No newline at end of file
+ /**
+ * This method tests <code>ubstring</code>.
+ */
+ public void testSubstring() {
+ StringBuffer haystack = new StringBuffer("abcdefghi");
+ assertEquals("cd", haystack.substring(2, 4));
+ assertEquals("bc", "abcdef".substring(1, 3));
+ assertEquals("bcdef", "abcdef".substring(1));
+ }
+}