001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util;
019:
020: import java.util.Vector;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * Test for StringUtils
026: */
027: public class StringUtilsTest extends TestCase {
028: public StringUtilsTest(String s) {
029: super (s);
030: }
031:
032: public void testSplit() {
033: final String data = "a,b,,";
034: Vector res = StringUtils.split(data, ',');
035: assertEquals(4, res.size());
036: assertEquals("a", res.elementAt(0));
037: assertEquals("b", res.elementAt(1));
038: assertEquals("", res.elementAt(2));
039: assertEquals("", res.elementAt(3));
040: }
041:
042: public void testSplitLines() {
043: final String data = "a\r\nb\nc\nd\ne";
044: Vector res = StringUtils.lineSplit(data);
045: assertEquals(5, res.size());
046: assertEquals("a\r", res.elementAt(0));
047: assertEquals("b", res.elementAt(1));
048: assertEquals("c", res.elementAt(2));
049: assertEquals("d", res.elementAt(3));
050: assertEquals("e", res.elementAt(4));
051: }
052:
053: public void testReplace() {
054: final String data = "abcabcabca";
055: String res = StringUtils.replace(data, "a", "");
056: assertEquals("bcbcbc", res);
057: }
058:
059: public void testEndsWithBothEmpty() {
060: assertTrue(StringUtils.endsWith(new StringBuffer(), ""));
061: }
062:
063: public void testEndsWithEmptyString() {
064: assertTrue(StringUtils.endsWith(new StringBuffer("12234545"),
065: ""));
066: }
067:
068: public void testEndsWithShorterString() {
069: assertTrue(StringUtils.endsWith(new StringBuffer("12345678"),
070: "78"));
071: }
072:
073: public void testEndsWithSameString() {
074: assertTrue(StringUtils.endsWith(new StringBuffer("123"), "123"));
075: }
076:
077: public void testEndsWithLongerString() {
078: assertFalse(StringUtils
079: .endsWith(new StringBuffer("12"), "1245"));
080: }
081:
082: public void testEndsWithNoMatch() {
083: assertFalse(StringUtils.endsWith(new StringBuffer("12345678"),
084: "789"));
085: }
086:
087: public void testEndsWithEmptyBuffer() {
088: assertFalse(StringUtils.endsWith(new StringBuffer(""),
089: "12345667"));
090: }
091:
092: public void testEndsWithJDKPerf() {
093: StringBuffer buf = getFilledBuffer(1024 * 300, 'a');
094: for (int i = 0; i < 1000; i++) {
095: assertTrue(buf.toString().endsWith("aa"));
096: }
097: }
098:
099: public void testEndsWithPerf() {
100: StringBuffer buf = getFilledBuffer(1024 * 300, 'a');
101: for (int i = 0; i < 1000; i++) {
102: assertTrue(StringUtils.endsWith(buf, "aa"));
103: }
104: }
105:
106: private StringBuffer getFilledBuffer(int size, char ch) {
107: StringBuffer buf = new StringBuffer(size);
108: for (int i = 0; i < size; i++) {
109: buf.append(ch);
110: }
111: ;
112: return buf;
113: }
114:
115: public void testParseHumanSizes() throws Exception {
116: final long KILOBYTE = 1024;
117: final long MEGABYTE = KILOBYTE * 1024;
118: final long GIGABYTE = MEGABYTE * 1024;
119: final long TERABYTE = GIGABYTE * 1024;
120: final long PETABYTE = TERABYTE * 1024;
121: assertEquals(StringUtils.parseHumanSizes("1K"), KILOBYTE);
122: assertEquals(StringUtils.parseHumanSizes("1M"), MEGABYTE);
123: assertEquals(StringUtils.parseHumanSizes("1G"), GIGABYTE);
124: assertEquals(StringUtils.parseHumanSizes("1T"), TERABYTE);
125: assertEquals(StringUtils.parseHumanSizes("1P"), PETABYTE);
126: assertEquals(StringUtils.parseHumanSizes("1"), 1L);
127: }
128: }
|