001: /**********************************************************************
002: Copyright (c) 26-Oct-2004 Andy Jefferson and others.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016:
017: Contributors:
018: ...
019: **********************************************************************/package org.jpox.util;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.HashSet;
024:
025: import org.apache.log4j.Category;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Series of tests for string manipulation utilities.
031: * @version $Revision: 1.3 $
032: */
033: public class StringUtilsTest extends TestCase {
034: protected static final Category LOG = Category
035: .getInstance("JPOX.TEST");
036:
037: /**
038: * Start of the test, so log it and initialise.
039: * @param name Name of the test case (not used)
040: */
041: public StringUtilsTest(String name) {
042: super (name);
043: }
044:
045: /**
046: * Test for isWhitespace method.
047: */
048: public void testIsWhitespace() {
049: assertTrue(
050: "null String should have been confirmed as whitespace but wasn't",
051: StringUtils.isWhitespace(null));
052: assertTrue(
053: "empty String should have been confirmed as whitespace but wasn't",
054: StringUtils.isWhitespace(" "));
055: assertTrue(
056: "Non empty String should have been defined as not whitespace but was",
057: !StringUtils.isWhitespace("JPOX"));
058: assertTrue(
059: "Partly empty String should have been defined as not whitespace but was",
060: !StringUtils.isWhitespace(" JPOXWorld "));
061: }
062:
063: /**
064: * Test for areStringsEqual method
065: */
066: public void testAreStringsEqual() {
067: assertTrue(
068: "Identical Strings should have been decreed equal but weren't",
069: StringUtils.areStringsEqual("JPOX", "JPOX"));
070: assertTrue(
071: "Non-identical Strings should have been decreed as non-equal but were",
072: !StringUtils.areStringsEqual("JPOX", "Kodo"));
073: assertTrue(
074: "Null strings should have been decreed equal but weren't",
075: StringUtils.areStringsEqual(null, null));
076: assertTrue(
077: "Null and non-null Strings should have been decreed as non-equal, but were",
078: !StringUtils.areStringsEqual("null", null));
079: }
080:
081: /**
082: * Test for left-aligned padded strings.
083: */
084: public void testLeftAlignedPaddedString() {
085: assertTrue(
086: "String left aligned (extending the string) was incorrect",
087: StringUtils.leftAlignedPaddedString("JPOX", 10).equals(
088: "JPOX "));
089: assertTrue("String left aligned (same length) was incorrect",
090: StringUtils.leftAlignedPaddedString("JPOX", 4).equals(
091: "JPOX"));
092: }
093:
094: /**
095: * Test for right-aligned padded strings.
096: */
097: public void testRightAlignedPaddedString() {
098: assertTrue(
099: "String right aligned (extending the string) was incorrect",
100: StringUtils.rightAlignedPaddedString("JPOX", 10)
101: .equals(" JPOX"));
102: assertTrue("String right aligned (same length) was incorrect",
103: StringUtils.rightAlignedPaddedString("JPOX", 4).equals(
104: "JPOX"));
105: }
106:
107: /**
108: * Test for splitting strings at identifiers.
109: */
110: public void testSplit() {
111: String[] tokens = StringUtils.split("JPOX Kodo JDOGenie", " ");
112: assertTrue("First token of split string is incorrect",
113: tokens[0].equals("JPOX"));
114: assertTrue("Second token of split string is incorrect",
115: tokens[1].equals("Kodo"));
116: assertTrue("Third token of split string is incorrect",
117: tokens[2].equals("JDOGenie"));
118:
119: String[] tokens2 = StringUtils.split(
120: "Andy Jefferson::Erik Bengtson::David Ezzio::Eagle",
121: "::");
122: assertTrue("First token of split string is incorrect",
123: tokens2[0].equals("Andy Jefferson"));
124: assertTrue("Second token of split string is incorrect",
125: tokens2[1].equals("Erik Bengtson"));
126: assertTrue("Third token of split string is incorrect",
127: tokens2[2].equals("David Ezzio"));
128: assertTrue("Fourth token of split string is incorrect",
129: tokens2[3].equals("Eagle"));
130: }
131:
132: /**
133: * Test for conversion of a boolean array to a String.
134: */
135: public void testBooleanArrayToString() {
136: boolean[] values = new boolean[] { true, false, false, true,
137: true };
138: assertTrue("String version of boolean array [5] is incorrect",
139: StringUtils.booleanArrayToString(values).equals(
140: "[YNNYY]"));
141:
142: assertTrue("String version of null boolean array is incorrect",
143: StringUtils.booleanArrayToString(null).equals("null"));
144:
145: boolean[] values2 = new boolean[] { false };
146: assertTrue("String version of boolean array [1] is incorrect",
147: StringUtils.booleanArrayToString(values2).equals("[N]"));
148: }
149:
150: /**
151: * Test for conversion of a int array to a String.
152: */
153: public void testIntArrayToString() {
154: int[] values = new int[] { 9876, 5432, 1, -6, 5 };
155: assertTrue("String version of int array [5] is incorrect",
156: StringUtils.intArrayToString(values).equals(
157: "[9876, 5432, 1, -6, 5]"));
158:
159: assertTrue("String version of null int array is incorrect",
160: StringUtils.intArrayToString(null).equals("null"));
161:
162: int[] values2 = new int[] { 1234567 };
163: assertTrue("String version of int array [1] is incorrect",
164: StringUtils.intArrayToString(values2).equals(
165: "[1234567]"));
166: }
167:
168: /**
169: * Test for conversion of a collection to a String.
170: */
171: public void testCollectionToString() {
172: Collection coll = new ArrayList();
173: coll.add("JPOX version 1.0");
174: coll.add("JPOX version 1.1.0-alpha-3");
175: coll.add("BCEL version 5.1");
176: assertTrue(
177: "String version of Collection is incorrect",
178: StringUtils
179: .collectionToString(coll)
180: .equals(
181: "JPOX version 1.0, JPOX version 1.1.0-alpha-3, BCEL version 5.1"));
182:
183: assertTrue("String version of empty Collection is incorrect",
184: StringUtils.collectionToString(new HashSet()).equals(
185: "<none>"));
186: }
187: }
|