001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.lang;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * TODO: COMPILER OPTIMIZATIONS HAVE MADE THIS TEST NOT ACTUALLY TEST ANYTHING!
022: * NEED A VERSION THAT DOESN'T USE STATICALLY DETERMINABLE STRINGS!
023: */
024: public class StringTest extends GWTTestCase {
025:
026: public String getModuleName() {
027: return "com.google.gwt.emultest.EmulSuite";
028: }
029:
030: public void testCharAt() {
031: assertEquals("abc".charAt(1), 'b');
032: }
033:
034: public void testConcat() {
035: assertEquals("abcdef", "abc" + "def");
036: assertEquals("abcdef", "abc".concat("def"));
037: assertEquals("".concat(""), "");
038: char c = 'd';
039: String s = "abc";
040: assertEquals("abcd", "abc" + 'd');
041: assertEquals("abcd", "abc" + c);
042: assertEquals("abcd", s + 'd');
043: assertEquals("abcd", s + c);
044: s += c;
045: assertEquals("abcd", s);
046: }
047:
048: public void testContructor() {
049: char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
050: String constant = "abcdef";
051: String shortString = "cde";
052: assertEquals(new String(constant), constant);
053: assertEquals(new String(chars), constant);
054: assertEquals(new String(chars, 2, 3), shortString);
055: assertEquals(new String(""), "");
056: assertEquals(
057: new String(new String(new String(new String("")))), "");
058: assertEquals(new String(new char[] {}), "");
059: }
060:
061: public void testEndsWith() {
062: String haystack = "abcdefghi";
063: assertTrue("a", haystack.endsWith("defghi"));
064: assertTrue("b", haystack.endsWith(haystack));
065: assertFalse("c", haystack.endsWith(haystack + "j"));
066: }
067:
068: public void testEquals() {
069: assertFalse("ABC".equals("abc"));
070: assertFalse("abc".equals("ABC"));
071: assertTrue("abc".equals("abc"));
072: assertTrue("ABC".equals("ABC"));
073: assertFalse("AbC".equals("aBC"));
074: assertFalse("AbC".equals("aBC"));
075: assertTrue("".equals(""));
076: assertFalse("".equals(null));
077: }
078:
079: public void testEqualsIgnoreCase() {
080: assertTrue("ABC".equalsIgnoreCase("abc"));
081: assertTrue("abc".equalsIgnoreCase("ABC"));
082: assertTrue("abc".equalsIgnoreCase("abc"));
083: assertTrue("ABC".equalsIgnoreCase("ABC"));
084: assertTrue("AbC".equalsIgnoreCase("aBC"));
085: assertTrue("AbC".equalsIgnoreCase("aBC"));
086: assertTrue("".equalsIgnoreCase(""));
087: assertFalse("".equalsIgnoreCase(null));
088: }
089:
090: /**
091: * Tests hashing with strings.
092: *
093: * The specific strings used in this test used to trigger failures because we
094: * use a JavaScript object as a hash map to cache the computed hash codes.
095: * This conflicts with built-in properties defined on objects -- see issue
096: * #631.
097: *
098: */
099: public void testHashCode() {
100: String[] testStrings = { "watch", "unwatch", "toString",
101: "toSource", "eval", "valueOf", "constructor",
102: "__proto__" };
103: int[] savedHash = new int[testStrings.length];
104: for (int i = 0; i < testStrings.length; ++i) {
105: savedHash[i] = testStrings[i].hashCode();
106:
107: /*
108: * Verify that the resulting hash code is numeric, since this is not
109: * enforced in web mode.
110: */
111: String str = Integer.toString(savedHash[i]);
112: for (int j = 0; j < str.length(); ++j) {
113: char ch = str.charAt(j);
114: assertTrue("Bad character '" + ch + "' (U+0"
115: + Integer.toHexString(ch) + ")", ch == '-'
116: || ch == ' ' || Character.isDigit(ch));
117: }
118: }
119: // verify the hash codes are constant for a given string
120: for (int i = 0; i < testStrings.length; ++i) {
121: assertEquals(savedHash[i], testStrings[i].hashCode());
122: }
123: }
124:
125: public void testIndexOf() {
126: String haystack = "abcdefghi";
127: assertEquals(haystack.indexOf("q"), -1);
128: assertEquals(haystack.indexOf('q'), -1);
129: assertEquals(haystack.indexOf("a"), 0);
130: assertEquals(haystack.indexOf('a'), 0);
131: assertEquals(haystack.indexOf('a', 1), -1);
132: assertEquals(haystack.indexOf("bc"), 1);
133: assertEquals(haystack.indexOf(""), 0);
134: }
135:
136: public void testLastIndexOf() {
137: String x = "abcdeabcdef";
138: assertEquals(9, x.lastIndexOf("e"));
139: assertEquals(10, x.lastIndexOf("f"));
140: assertEquals(-1, x.lastIndexOf("f", 1));
141: }
142:
143: public void testLength() {
144: assertEquals(3, "abc".length());
145: String str = "x";
146: for (int i = 0; i < 16; i++) {
147: str = str + str;
148: }
149: assertEquals(1 << 16, str.length());
150: }
151:
152: public void testLowerCase() {
153: assertEquals("abc", "AbC".toLowerCase());
154: assertEquals("abc", "abc".toLowerCase());
155: assertEquals("", "".toLowerCase());
156: }
157:
158: public void testMatch() {
159: assertFalse("1f", "abbbbcd".matches("b*"));
160: assertFalse("2f", "abbbbcd".matches("b+"));
161: assertTrue("3t", "abbbbcd".matches("ab*bcd"));
162: assertTrue("4t", "abbbbcd".matches("ab+cd"));
163: assertTrue("5t", "abbbbcd".matches("ab+bcd"));
164: assertFalse("6f", "abbbbcd".matches(""));
165: assertTrue("7t", "abbbbcd".matches("a.*d"));
166: assertFalse("8f", "abbbbcd".matches("a.*e"));
167: }
168:
169: public void testNull() {
170: assertNull(returnNull());
171: String a = returnNull() + returnNull();
172: assertEquals("nullnull", a);
173: }
174:
175: public void testReplace() {
176: assertEquals("axax".replace('x', 'a'), "aaaa");
177: assertEquals("aaaa".replace('x', 'a'), "aaaa");
178: for (char from = 32; from < 250; ++from) {
179: char to = (char) (from + 5);
180: assertEquals(toS(to), toS(from).replace(from, to));
181: }
182: for (char to = 32; to < 250; ++to) {
183: char from = (char) (to + 5);
184: assertEquals(toS(to), toS(from).replace(from, to));
185: }
186: }
187:
188: public void testReplaceAll() {
189: assertEquals("abcdef", "xxxxabcxxdexf".replaceAll("x*", ""));
190: assertEquals("1\\1abc123\\123de1234\\1234f", "1abc123de1234f"
191: .replaceAll("([1234]+)", "$1\\\\$1"));
192: assertEquals("\n \n", "x x".replaceAll("x", "\n"));
193: assertEquals("x x", "\n \n".replaceAll("\\\n", "x"));
194: assertEquals("x\"\\", "x".replaceAll("x", "\\x\\\"\\\\"));
195: assertEquals("$$x$", "x".replaceAll("(x)", "\\$\\$$1\\$"));
196: }
197:
198: public void testSplit() {
199: compareList("fullSplit", new String[] { "abc", "", "", "de",
200: "f" }, "abcxxxdexfxx".split("x"));
201: compareList("emptyRegexSplit", new String[] { "", "a", "b",
202: "c", "x", "x", "d", "e", "x", "f", "x" }, "abcxxdexfx"
203: .split(""));
204: compareList("2:", "boo:and:foo".split(":", 2), new String[] {
205: "boo", "and:foo" });
206: compareList("5:", "boo:and:foo".split(":", 5), new String[] {
207: "boo", "and", "foo" });
208: compareList("-2:", "boo:and:foo".split(":", -2), new String[] {
209: "boo", "and", "foo" });
210: compareList("5o", "boo:and:foo".split("o", 5), new String[] {
211: "b", "", ":and:f", "", "" });
212: compareList("-2o", "boo:and:foo".split("o", -2), new String[] {
213: "b", "", ":and:f", "", "" });
214: compareList("0o", "boo:and:foo".split("o", 0), new String[] {
215: "b", "", ":and:f" });
216: compareList("0:", "boo:and:foo".split(":", 0), new String[] {
217: "boo", "and", "foo" });
218: }
219:
220: public void testStartsWith() {
221: String haystack = "abcdefghi";
222: assertTrue(haystack.startsWith("abc"));
223: assertTrue(haystack.startsWith("bc", 1));
224: assertTrue(haystack.startsWith(haystack));
225: assertFalse(haystack.startsWith(haystack + "j"));
226: }
227:
228: public void testSubstring() {
229: String haystack = "abcdefghi";
230: assertEquals("cd", haystack.substring(2, 4));
231: assertEquals("bc", "abcdef".substring(1, 3));
232: assertEquals("bcdef", "abcdef".substring(1));
233: }
234:
235: public void testToCharArray() {
236: char[] a1 = "abc".toCharArray();
237: char[] a2 = new char[] { 'a', 'b', 'c' };
238: for (int i = 0; i < a1.length; i++) {
239: assertEquals(a1[i], a2[i]);
240: }
241: }
242:
243: public void testTrim() {
244: trimRightAssertEquals("abc", " \t abc \n ");
245: trimRightAssertEquals("abc", "abc".trim());
246: trimRightAssertSame("abc", "abc");
247: String s = '\u0023' + "hi";
248: trimRightAssertSame(s, s);
249: trimRightAssertEquals("abc", " abc".trim());
250: trimRightAssertEquals("abc", "abc ".trim());
251: trimRightAssertEquals("", "".trim());
252: trimRightAssertEquals("", " \t ".trim());
253: }
254:
255: public void testUpperCase() {
256: assertEquals("abc", "AbC".toLowerCase());
257: assertEquals("abc", "abc".toLowerCase());
258: assertEquals("", "".toLowerCase());
259: }
260:
261: public void testValueOf() {
262: assertTrue(String.valueOf(C.FLOAT_VALUE).startsWith(
263: C.FLOAT_STRING));
264: assertEquals(C.INT_STRING, String.valueOf(C.INT_VALUE));
265: assertEquals(C.LONG_STRING, String.valueOf(C.LONG_VALUE));
266: assertTrue(String.valueOf(C.DOUBLE_VALUE).startsWith(
267: C.DOUBLE_STRING));
268: assertEquals(C.CHAR_STRING, String.valueOf(C.CHAR_VALUE));
269: assertEquals(C.CHAR_ARRAY_STRING, String
270: .valueOf(C.CHAR_ARRAY_VALUE));
271: assertEquals(C.CHAR_ARRAY_STRING_SUB, String.valueOf(
272: C.CHAR_ARRAY_VALUE, 1, 4));
273: assertEquals(C.FALSE_STRING, String.valueOf(C.FALSE_VALUE));
274: assertEquals(C.TRUE_STRING, String.valueOf(C.TRUE_VALUE));
275: }
276:
277: /**
278: * Helper method for testTrim to avoid compiler optimizations.
279: */
280: public void trimRightAssertEquals(String left, String right) {
281: assertEquals(left, right.trim());
282: }
283:
284: /**
285: * Helper method for testTrim to avoid compiler optimizations.
286: */
287: public void trimRightAssertSame(String left, String right) {
288: assertSame(left, right.trim());
289: }
290:
291: private void compareList(String category, String[] desired,
292: String[] got) {
293: assertEquals(category + " length", desired.length, got.length);
294: for (int i = 0; i < desired.length; i++) {
295: assertEquals(category + " " + i, desired[i], got[i]);
296: }
297: }
298:
299: private String returnNull() {
300: return null;
301: }
302:
303: private String toS(char from) {
304: return Character.toString(from);
305: }
306:
307: }
|