001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * @author gregw
021: *
022: */
023: public class StringUtilTest extends TestCase {
024:
025: /**
026: * Constructor for StringUtilTest.
027: * @param arg0
028: */
029: public StringUtilTest(String arg0) {
030: super (arg0);
031: }
032:
033: /*
034: * @see TestCase#setUp()
035: */
036: protected void setUp() throws Exception {
037: super .setUp();
038: }
039:
040: /*
041: * @see TestCase#tearDown()
042: */
043: protected void tearDown() throws Exception {
044: super .tearDown();
045: }
046:
047: public void testAsciiToLowerCase() {
048: String lc = "\u0690bc def 1\u06903";
049: assertEquals(StringUtil
050: .asciiToLowerCase("\u0690Bc DeF 1\u06903"), lc);
051: assertTrue(StringUtil.asciiToLowerCase(lc) == lc);
052: }
053:
054: public void testStartsWithIgnoreCase() {
055:
056: assertTrue(StringUtil.startsWithIgnoreCase("\u0690b\u0690defg",
057: "\u0690b\u0690"));
058: assertTrue(StringUtil.startsWithIgnoreCase("\u0690bcdefg",
059: "\u0690bc"));
060: assertTrue(StringUtil.startsWithIgnoreCase("\u0690bcdefg",
061: "\u0690Bc"));
062: assertTrue(StringUtil.startsWithIgnoreCase("\u0690Bcdefg",
063: "\u0690bc"));
064: assertTrue(StringUtil.startsWithIgnoreCase("\u0690Bcdefg",
065: "\u0690Bc"));
066: assertTrue(StringUtil.startsWithIgnoreCase("\u0690bcdefg", ""));
067: assertTrue(StringUtil
068: .startsWithIgnoreCase("\u0690bcdefg", null));
069: assertTrue(StringUtil.startsWithIgnoreCase("\u0690bcdefg",
070: "\u0690bcdefg"));
071:
072: assertFalse(StringUtil.startsWithIgnoreCase(null, "xyz"));
073: assertFalse(StringUtil.startsWithIgnoreCase("\u0690bcdefg",
074: "xyz"));
075: assertFalse(StringUtil.startsWithIgnoreCase("\u0690", "xyz"));
076: }
077:
078: public void testEndsWithIgnoreCase() {
079: assertTrue(StringUtil.endsWithIgnoreCase(
080: "\u0690bcd\u0690f\u0690", "\u0690f\u0690"));
081: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdefg", "efg"));
082: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdefg", "eFg"));
083: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdeFg", "efg"));
084: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdeFg", "eFg"));
085: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdefg", ""));
086: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdefg", null));
087: assertTrue(StringUtil.endsWithIgnoreCase("\u0690bcdefg",
088: "\u0690bcdefg"));
089:
090: assertFalse(StringUtil.endsWithIgnoreCase(null, "xyz"));
091: assertFalse(StringUtil
092: .endsWithIgnoreCase("\u0690bcdefg", "xyz"));
093: assertFalse(StringUtil.endsWithIgnoreCase("\u0690", "xyz"));
094: }
095:
096: public void testIndexFrom() {
097: assertEquals(StringUtil.indexFrom("\u0690bcd", "xyz"), -1);
098: assertEquals(StringUtil.indexFrom("\u0690bcd", "\u0690bcz"), 0);
099: assertEquals(StringUtil.indexFrom("\u0690bcd", "bcz"), 1);
100: assertEquals(StringUtil.indexFrom("\u0690bcd", "dxy"), 3);
101: }
102:
103: public void testReplace() {
104: String s = "\u0690bc \u0690bc \u0690bc";
105: assertEquals(StringUtil.replace(s, "\u0690bc", "xyz"),
106: "xyz xyz xyz");
107: assertTrue(StringUtil.replace(s, "xyz", "pqy") == s);
108:
109: s = " \u0690bc ";
110: assertEquals(StringUtil.replace(s, "\u0690bc", "xyz"), " xyz ");
111:
112: }
113:
114: public void testUnquote() {
115: String uq = " not quoted ";
116: assertTrue(StringUtil.unquote(uq) == uq);
117: assertEquals(StringUtil.unquote("' quoted string '"),
118: " quoted string ");
119: assertEquals(StringUtil.unquote("\" quoted string \""),
120: " quoted string ");
121: assertEquals(StringUtil.unquote("' quoted\"string '"),
122: " quoted\"string ");
123: assertEquals(StringUtil.unquote("\" quoted'string \""),
124: " quoted'string ");
125: }
126:
127: public void testNonNull() {
128: String nn = "";
129: assertTrue(nn == StringUtil.nonNull(nn));
130: assertEquals("", StringUtil.nonNull(null));
131: }
132:
133: /*
134: * Test for boolean equals(String, char[], int, int)
135: */
136: public void testEqualsStringcharArrayintint() {
137: assertTrue(StringUtil.equals("\u0690bc", new char[] { 'x',
138: '\u0690', 'b', 'c', 'z' }, 1, 3));
139: assertFalse(StringUtil.equals("axc", new char[] { 'x', 'a',
140: 'b', 'c', 'z' }, 1, 3));
141: }
142:
143: public void testAppend() {
144: StringBuffer buf = new StringBuffer();
145: buf.append('a');
146: StringUtil.append(buf, "abc", 1, 1);
147: StringUtil.append(buf, (byte) 12, 16);
148: StringUtil.append(buf, (byte) 16, 16);
149: StringUtil.append(buf, (byte) -1, 16);
150: StringUtil.append(buf, (byte) -16, 16);
151: assertEquals("ab0c10fff0", buf.toString());
152:
153: }
154:
155: public static void main(String[] arg) throws Exception {
156: String string = "Now \u0690xxxxxxxx";
157: System.err.println(string);
158: byte[] bytes = string.getBytes("UTF-8");
159: System.err.println(new String(bytes));
160: System.err.println(bytes.length);
161: long calc = 0;
162: Utf8StringBuffer strbuf = new Utf8StringBuffer(bytes.length);
163: for (int i = 0; i < 10; i++) {
164: long s1 = System.currentTimeMillis();
165: for (int j = 1000000; j-- > 0;) {
166: calc += new String(bytes, 0, bytes.length, "UTF-8")
167: .hashCode();
168: }
169: long s2 = System.currentTimeMillis();
170: for (int j = 1000000; j-- > 0;) {
171: calc += StringUtil.toUTF8String(bytes, 0, bytes.length)
172: .hashCode();
173: }
174: long s3 = System.currentTimeMillis();
175: for (int j = 1000000; j-- > 0;) {
176: Utf8StringBuffer buffer = new Utf8StringBuffer(
177: bytes.length);
178: buffer.append(bytes, 0, bytes.length);
179: calc += buffer.toString().hashCode();
180: }
181: long s4 = System.currentTimeMillis();
182: for (int j = 1000000; j-- > 0;) {
183: strbuf.reset();
184: strbuf.append(bytes, 0, bytes.length);
185: calc += strbuf.toString().hashCode();
186: }
187: long s5 = System.currentTimeMillis();
188:
189: System.err.println((s2 - s1) + ", " + (s3 - s2) + ", "
190: + (s4 - s3) + ", " + (s5 - s4));
191: }
192: System.err.println(calc);
193: }
194: }
|