You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/02/22 06:57:45 UTC
cvs commit: jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang StringsTest.java
bayard 02/02/21 21:57:45
Added: lang/src/test/org/apache/commons/lang StringsTest.java
Log:
unit test for Strings class
Revision Changes Path
1.1 jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/StringsTest.java
Index: StringsTest.java
===================================================================
package org.apache.commons.lang;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.Arrays;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit tests {@link org.apache.commons.lang.Strings}.
*
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
* @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
*/
public class StringsTest extends TestCase
{
private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
private static final String SEPARATOR = ",";
private static final String TEXT_LIST = "foo,bar,baz";
private static final String FOO = "foo";
private static final String CAP_FOO = "Foo";
private static final String UPPER_FOO = "FOO";
private static final String SENTENCE = "foo bar baz";
public StringsTest(String name)
{
super(name);
}
public void testClean()
{
assertEquals("clean(String) failed",
FOO, Strings.clean(FOO + " "));
assertEquals("clean(String) failed", "", Strings.clean(null));
}
public void testTrim()
{
assertEquals("trim(String) failed", FOO, Strings.trim(FOO + " "));
assertTrue("trim(String) failed", Strings.trim(null) == null);
}
public void testIsValid()
{
assertTrue("isValid(String) failed",
Strings.isValid(FOO) && !Strings.isValid(""));
}
public void testIsEmpty()
{
assertTrue("isEmpty(String) failed",
Strings.isEmpty(" ") && !Strings.isEmpty(FOO));
}
public void testEquals()
{
assertTrue("equals(String, String) failed",
Strings.equals(null, null) &&
!Strings.equals(null, FOO) &&
Strings.equals(FOO, FOO));
}
public void testCapitalizeFunctions()
{
assertEquals("firstLetterCaps(String) failed",
CAP_FOO, Strings.firstLetterCaps(FOO));
assertEquals("capitalise(String) failed",
CAP_FOO, Strings.capitalise(FOO) );
assertEquals("capitaliseAllWords(String) failed",
"Foo Bar Baz", Strings.capitaliseAllWords(SENTENCE) );
assertEquals("uncapitalise(String) failed",
FOO, Strings.uncapitalise(CAP_FOO) );
}
public void testJoin()
{
assertEquals("join(Object[], String) failed", TEXT_LIST,
Strings.join(ARRAY_LIST, SEPARATOR));
assertEquals("join(Iterator, String) failed", TEXT_LIST,
Strings.join(Arrays.asList(ARRAY_LIST).iterator(),
SEPARATOR));
}
public void testSplit()
{
String[] result = Strings.split(TEXT_LIST, SEPARATOR, 2);
String[] expected = { "foo", "bar,baz" };
assertEquals("split(Object[], String, int) yielded unexpected length",
expected.length, result.length);
for (int i = 0; i < result.length; i++)
{
assertEquals("split(Object[], String, int) failed", expected[i],
result[i]);
}
}
public void testReplaceFunctions()
{
assertEquals("replace(String, String, String, int) failed",
FOO, Strings.replace("oo" + FOO, "o", "", 2));
// removed the 4th argument of -1.
assertEquals("replace(String, String, String) failed",
"", Strings.replace(FOO + FOO + FOO, FOO, ""));
assertEquals("replaceOnce(String, String, String) failed",
FOO, Strings.replaceOnce(FOO + FOO, FOO, ""));
}
public void testOverlayString()
{
assertEquals("overlayString(String, String, int, int) failed",
"foo foor baz", Strings.overlayString(SENTENCE, FOO, 4, 6) );
}
public void testRepeat()
{
assertEquals("repeat(String, int) failed",
FOO + FOO + FOO, Strings.repeat(FOO, 3) );
}
public void testCenter()
{
assertEquals("center(String, int) failed",
" "+FOO+" ", Strings.center(FOO, 9) );
}
public void testChompFunctions()
{
assertEquals("chomp(String) failed",
FOO, Strings.chomp(FOO + "\n" + FOO) );
assertEquals("chompLast(String) failed",
FOO, Strings.chompLast(FOO + "\n") );
assertEquals("getChomp(String, String) failed",
"\n" + FOO, Strings.getChomp(FOO + "\n" + FOO, "\n") );
assertEquals("prechomp(String, String) failed",
FOO, Strings.prechomp(FOO + "\n" + FOO, "\n") );
assertEquals("getPrechomp(String, String) failed",
FOO + "\n", Strings.getPrechomp(FOO + "\n" + FOO, "\n") );
assertEquals("chop(String, String) failed",
FOO, Strings.chop(FOO + "\r\n") );
assertEquals("chopNewline(String, String) failed",
FOO, Strings.chopNewline(FOO + "\r\n") );
}
public void testPadFunctions()
{
assertEquals("rightPad(String, int) failed",
"1234 ", Strings.rightPad ("1234", 8) );
assertEquals("rightPad(String, int, String) failed",
"1234-+-+", Strings.rightPad ("1234", 8, "-+") );
assertEquals("rightPad(String, int, String) failed",
"123456-+~", Strings.rightPad ("123456", 9, "-+~") );
assertEquals("leftPad(String, int) failed",
" 1234", Strings.leftPad("1234", 8) );
assertEquals("leftPad(String, int, String) failed",
"-+-+1234", Strings.leftPad("1234", 8, "-+") );
assertEquals("leftPad(String, int, String) failed",
"-+~123456", Strings.leftPad("123456", 9, "-+~") );
}
public void testUnicodeFunctions() throws java.io.IOException
{
/* this test fails on my window box with an Sun english JDK 1.3.1
I think that the input string is not right
*/
/* Kept out for the moment.
String input = "これは日本楔譴離謄好箸任后#";
String unicode = Strings.convertNativeToUnicode(input, "iso-2022-jp");
String iso = Strings.convertUnicodeToNative(unicode, "iso-2022-jp");
assertEquals("Unicode conversions failed", input, iso);
*/
}
public void testSubstring()
{
assertEquals("substring(String, int, int) failed",
FOO, Strings.substring(SENTENCE, 0, 4));
assertEquals("substring(String, int, negative int) failed",
FOO, Strings.substring(SENTENCE, 0, -8));
assertEquals("substring(String, int, overflow int) failed",
SENTENCE, Strings.substring(SENTENCE, 0, 80));
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>