001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import junit.framework.TestCase;
008:
009: public final class StringUtilTest extends TestCase {
010:
011: public void testSafeToString() {
012: assertEquals(StringUtil.NULL_STRING, StringUtil
013: .safeToString(null));
014: assertEquals("10", StringUtil.safeToString(new Integer(10)));
015: }
016:
017: public void testIndentLinesNegativeIndent() {
018: try {
019: StringUtil.indentLines(new StringBuffer(), -10, ' ');
020: Assert.fail("Expected IllegalArgumentException");
021: } catch (IllegalArgumentException e) {
022: // expected
023: }
024: }
025:
026: public void testIndentLines() {
027: // null StringBuffer -> null result
028: assertEquals(null, StringUtil.indentLines(null, 2, ' '));
029:
030: // 0 indent -> unchanged
031: String start = "abc\ndef\nghi";
032: StringBuffer sb = new StringBuffer(start);
033: assertEquals(start, StringUtil.indentLines(sb, 0, ' ')
034: .toString());
035: assertEquals(start, sb.toString());
036:
037: // check indent after line breaks
038: String after = "\t\tabc\n\t\tdef\n\t\tghi";
039: assertEquals(after, StringUtil.indentLines(sb, 2, '\t')
040: .toString());
041: assertEquals(after, sb.toString());
042: }
043:
044: public void testIndentLinesStringNull() {
045: try {
046: StringUtil.indentLines((String) null, 5);
047: fail("Expected NPE");
048: } catch (NullPointerException e) {
049: // expected exception
050: }
051: }
052:
053: public void testToStringObjectArrayStringStringString() {
054: // Test 1, all nulls
055: String expected = StringUtil.NULL_STRING;
056: String rv = StringUtil.toString((Object[]) null, null, null,
057: null);
058: assertNotNull(
059: "StringUtil.toString(Object[],String,String,String) returned null",
060: rv);
061: assertEquals("Returned string was not the same as expected",
062: expected, rv);
063:
064: // Test 2, objects with no formatting
065: Object[] arr = new Object[] { "one", "two", "three" };
066: expected = "onetwothree";
067: rv = StringUtil.toString(arr, null, null, null);
068: assertNotNull(
069: "StringUtil.toString(Object[],String,String,String) returned null",
070: rv);
071: assertEquals("Returned string was not the same as expected",
072: expected, rv);
073:
074: // Test 3, objects with some formatting
075: expected = "[one:[two:[three";
076: rv = StringUtil.toString(arr, ":", "[", null);
077: assertNotNull(
078: "StringUtil.toString(Object[],String,String,String) returned null",
079: rv);
080: assertEquals("Returned string was not the same as expected",
081: expected, rv);
082:
083: // Test 4, objects with all formatting
084: expected = "<one>,<two>,<three>";
085: rv = StringUtil.toString(arr, ",", "<", ">");
086: assertNotNull(
087: "StringUtil.toString(Object[],String,String,String) returned null",
088: rv);
089: assertEquals("Returned string was not the same as expected",
090: expected, rv);
091:
092: // Test 5, objects with all formatting and null elements
093: arr = new Object[] { "one", null, null, "four" };
094: expected = "<one>,<null>,<null>,<four>";
095: rv = StringUtil.toString(arr, ",", "<", ">");
096: assertNotNull(
097: "StringUtil.toString(Object[],String,String,String) returned null",
098: rv);
099: assertEquals("Returned string was not the same as expected",
100: expected, rv);
101: }
102:
103: public void testToString() {
104: Long[] vals = new Long[] { new Long(1), new Long(2),
105: new Long(3) };
106: final String actual = StringUtil.toString(vals);
107: assertEquals("1, 2, 3", actual);
108: }
109:
110: public void testToPaddedString() {
111: // Too big for padding
112: assertEquals("123", StringUtil.toPaddedString(123, 10, 1));
113:
114: // Pad out with 0's
115: assertEquals("0000000123", StringUtil.toPaddedString(123, 10,
116: 10));
117:
118: // Base 16
119: assertEquals("00cd", StringUtil.toPaddedString(205, 16, 4));
120: }
121:
122: }
|