Author: scolebourne
Date: Sat Jul  9 06:41:02 2005
New Revision: 209943

URL: http://svn.apache.org/viewcvs?rev=209943&view=rev
Log:
New methods and testing of StrBuilder

Modified:
    
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
    
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java

Modified: 
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=209943&r1=209942&r2=209943&view=diff
==============================================================================
--- 
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
 (original)
+++ 
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
 Sat Jul  9 06:41:02 2005
@@ -74,7 +74,7 @@
      * Constructor that creates an empty builder initial capacity 32 
characters.
      */
     public StrBuilder() {
-        this(32);
+        this(CAPACITY);
     }
 
     /**
@@ -212,24 +212,27 @@
     }
 
     /**
-     * Clears the string builder (convenience Collections API style method).
+     * Checks is the string builder is empty (convenience Collections API 
style method).
      * <p>
-     * This method is the same as [EMAIL PROTECTED] #setLength(int)} and is 
provided to match the
+     * This method is the same as checking [EMAIL PROTECTED] #length()} and is 
provided to match the
      * API of Collections.
+     * @return <code>true</code> if the size is <code>0</code>.
      */
-    public void clear() {
-        size = 0;
+    public boolean isEmpty() {
+        return size == 0;
     }
 
     /**
-     * Checks is the string builder is empty (convenience Collections API 
style method).
+     * Clears the string builder (convenience Collections API style method).
      * <p>
-     * This method is the same as checking [EMAIL PROTECTED] #length()} and is 
provided to match the
+     * This method does not reduce the size of the internal character buffer.
+     * To do that, call <code>clear()</code> followed by [EMAIL PROTECTED] 
#minimizeCapacity()}.
+     * <p>
+     * This method is the same as [EMAIL PROTECTED] #setLength(int)} and is 
provided to match the
      * API of Collections.
-     * @return <code>true</code> if the size is <code>0</code>.
      */
-    public boolean isEmpty() {
-        return size == 0;
+    public void clear() {
+        size = 0;
     }
 
     //-----------------------------------------------------------------------
@@ -457,6 +460,9 @@
         if (chars == null) {
             return this;
         }
+        if (startIndex < 0 || startIndex > chars.length) {
+            throw new StringIndexOutOfBoundsException("startIndex must be 
valid");
+        }
         if (length < 0) {
             throw new StringIndexOutOfBoundsException("length must not be 
negative");
         }
@@ -548,8 +554,8 @@
 
     //-----------------------------------------------------------------------
     /**
-     * Appends an array placing separators between each pair of values, but
-     * not after the last.
+     * Appends an array placing separators between each value, but
+     * not before the first or after the last.
      * Appending a null array will have no effect.
      * Each object is appended using [EMAIL PROTECTED] #append(Object)}.
      *
@@ -570,8 +576,8 @@
     }
 
     /**
-     * Appends a collection placing separators between each pair of values, but
-     * not after the last.
+     * Appends a collection placing separators between each value, but
+     * not before the first or after the last.
      * Appending a null collection will have no effect.
      * Each object is appended using [EMAIL PROTECTED] #append(Object)}.
      *
@@ -592,6 +598,28 @@
         return this;
     }
 
+    /**
+     * Appends an iterator placing separators between each value, but
+     * not before the first or after the last.
+     * Appending a null iterator will have no effect.
+     * Each object is appended using [EMAIL PROTECTED] #append(Object)}.
+     *
+     * @param it  the iterator to append
+     * @param separator  the separator to use, null means no separator
+     * @return this, to enable chaining
+     */
+    public StrBuilder appendWithSeparators(Iterator it, String separator) {
+        if (it != null) {
+            separator = (separator == null ? "" : separator);
+            append(it.next());
+            while (it.hasNext()) {
+                append(separator);
+                append(it.next());
+            }
+        }
+        return this;
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Appends the pad character to the builder the specified number of times.
@@ -610,64 +638,95 @@
         return this;
     }
 
+    //-----------------------------------------------------------------------
     /**
-     * Appends an object to the builder padding on the left to a fixed length.
+     * Appends an object to the builder padding on the left to a fixed width.
+     * The <code>toString</code> of the object is used.
      * If the object is larger than the length, the left hand side is lost.
      * If the object is null, the null text value is used.
      * 
-     * @param obj  the object to append
-     * @param length  the fixed field width
+     * @param obj  the object to append, null uses null text
+     * @param width  the fixed field width, zero or negative has no effect
      * @param padChar  the pad character to use
      * @return this, to enable chaining
      */
-    public StrBuilder appendFixedLengthPadLeft(Object obj, int length, char 
padChar) {
-        if (length > 0) {
-            ensureCapacity(size + length);
+    public StrBuilder appendFixedWidthPadLeft(Object obj, int width, char 
padChar) {
+        if (width > 0) {
+            ensureCapacity(size + width);
             String str = (obj == null ? getNullText() : obj.toString());
             int strLen = str.length();
-            if (strLen >= length) {
-                str.getChars(strLen - length, strLen, buf, size);
+            if (strLen >= width) {
+                str.getChars(strLen - width, strLen, buf, size);
             } else {
-                int padLen = length - strLen;
+                int padLen = width - strLen;
                 for (int i = 0; i < padLen; i++) {
                     buf[size + i] = padChar;
                 }
                 str.getChars(0, strLen, buf, size + padLen);
             }
-            size += length;
+            size += width;
         }
         return this;
     }
 
     /**
+     * Appends an object to the builder padding on the left to a fixed width.
+     * The <code>String.valueOf</code> of the object is used.
+     * If the formatted value is larger than the length, the left hand side is 
lost.
+     * 
+     * @param value  the value to append
+     * @param width  the fixed field width, zero or negative has no effect
+     * @param padChar  the pad character to use
+     * @return this, to enable chaining
+     */
+    public StrBuilder appendFixedWidthPadLeft(int value, int width, char 
padChar) {
+        return appendFixedWidthPadLeft(String.valueOf(value), width, padChar);
+    }
+
+    /**
      * Appends an object to the builder padding on the right to a fixed length.
+     * The <code>toString</code> of the object is used.
      * If the object is larger than the length, the right hand side is lost.
      * If the object is null, null text value is used.
      * 
-     * @param obj  the object to append
-     * @param length  the fixed field width
+     * @param obj  the object to append, null uses null text
+     * @param width  the fixed field width, zero or negative has no effect
      * @param padChar  the pad character to use
      * @return this, to enable chaining
      */
-    public StrBuilder appendFixedLengthPadRight(Object obj, int length, char 
padChar) {
-        if (length > 0) {
-            ensureCapacity(size + length);
+    public StrBuilder appendFixedWidthPadRight(Object obj, int width, char 
padChar) {
+        if (width > 0) {
+            ensureCapacity(size + width);
             String str = (obj == null ? getNullText() : obj.toString());
             int strLen = str.length();
-            if (strLen >= length) {
-                str.getChars(strLen - length, strLen, buf, size);
+            if (strLen >= width) {
+                str.getChars(0, strLen, buf, size);
             } else {
-                int padLen = length - strLen;
+                int padLen = width - strLen;
                 str.getChars(0, strLen, buf, size);
                 for (int i = 0; i < padLen; i++) {
                     buf[size + strLen + i] = padChar;
                 }
             }
-            size += length;
+            size += width;
         }
         return this;
     }
 
+    /**
+     * Appends an object to the builder padding on the right to a fixed length.
+     * The <code>String.valueOf</code> of the object is used.
+     * If the object is larger than the length, the right hand side is lost.
+     * 
+     * @param value  the value to append
+     * @param width  the fixed field width, zero or negative has no effect
+     * @param padChar  the pad character to use
+     * @return this, to enable chaining
+     */
+    public StrBuilder appendFixedWidthPadRight(int value, int width, char 
padChar) {
+        return appendFixedWidthPadRight(String.valueOf(value), width, padChar);
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Inserts the string representation of an object into this builder.
@@ -1400,6 +1459,35 @@
         return this;
     }
 
+//    /**
+//     * Gets a String version of the string builder by calling the internal
+//     * constructor of String by reflection.
+//     * <p>
+//     * WARNING: You must not use the StrBuilder after calling this method
+//     * as the buffer is now shared with the String object. To ensure this,
+//     * the internal character array is set to null, so you will get
+//     * NullPointerExceptions on all method calls.
+//     *
+//     * @return the builder as a String
+//     */
+//    public String toSharedString() {
+//        try {
+//            Constructor con = String.class.getDeclaredConstructor(
+//                new Class[] {int.class, int.class, char[].class});
+//            con.setAccessible(true);
+//            char[] buffer = buf;
+//            buf = null;
+//            size = -1;
+//            nullText = null;
+//            return (String) con.newInstance(
+//                new Object[] {new Integer(0), new Integer(size), buffer});
+//            
+//        } catch (Exception ex) {
+//            ex.printStackTrace();
+//            throw new 
UnsupportedOperationException("StrBuilder.toSharedString is unsupported: " + 
ex.getMessage());
+//        }
+//    }
+
     /**
      * Gets a String version of the string builder, creating a new instance
      * each time the method is called.
@@ -1411,6 +1499,16 @@
      */
     public String toString() {
         return new String(buf, 0, size);
+    }
+
+    /**
+     * Gets a StringBuffer version of the string builder, creating a
+     * new instance each time the method is called.
+     *
+     * @return the builder as a StringBuffer
+     */
+    public StringBuffer toStringBuffer() {
+        return new StringBuffer(size).append(buf, 0, size);
     }
 
     //-----------------------------------------------------------------------

Modified: 
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java?rev=209943&r1=209942&r2=209943&view=diff
==============================================================================
--- 
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
 (original)
+++ 
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
 Sat Jul  9 06:41:02 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2005 The Apache Software Foundation.
+ * Copyright 2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -33,6 +34,7 @@
  */
 public class StrBuilderTest extends TestCase {
 
+    /** Test subclass of Object, with a toString method. */
     private static Object FOO = new Object() {
         public String toString() {
             return "foo";
@@ -40,10 +42,9 @@
     };
 
     /**
-     * Main.
+     * Main method.
      * 
-     * @param args
-     *            command line arguments, ignored
+     * @param args  command line arguments, ignored
      */
     public static void main(String[] args) {
         TestRunner.run(suite());
@@ -70,12 +71,329 @@
         super(name);
     }
 
-    public void testAppend() {
+    //-----------------------------------------------------------------------
+    public void testConstructors() {
+        StrBuilder sb0 = new StrBuilder();
+        assertEquals(32, sb0.capacity());
+        assertEquals(0, sb0.length());
+        assertEquals(0, sb0.size());
+
+        StrBuilder sb1 = new StrBuilder(32);
+        assertEquals(32, sb1.capacity());
+        assertEquals(0, sb1.length());
+        assertEquals(0, sb1.size());
 
+        StrBuilder sb2 = new StrBuilder(0);
+        assertEquals(32, sb2.capacity());
+        assertEquals(0, sb2.length());
+        assertEquals(0, sb2.size());
+
+        StrBuilder sb3 = new StrBuilder(-1);
+        assertEquals(32, sb3.capacity());
+        assertEquals(0, sb3.length());
+        assertEquals(0, sb3.size());
+
+        StrBuilder sb4 = new StrBuilder(1);
+        assertEquals(1, sb4.capacity());
+        assertEquals(0, sb4.length());
+        assertEquals(0, sb4.size());
+
+        StrBuilder sb5 = new StrBuilder((String) null);
+        assertEquals(32, sb5.capacity());
+        assertEquals(0, sb5.length());
+        assertEquals(0, sb5.size());
+
+        StrBuilder sb6 = new StrBuilder("");
+        assertEquals(32, sb6.capacity());
+        assertEquals(0, sb6.length());
+        assertEquals(0, sb6.size());
+
+        StrBuilder sb7 = new StrBuilder("foo");
+        assertEquals(35, sb7.capacity());
+        assertEquals(3, sb7.length());
+        assertEquals(3, sb7.size());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testCapacityAndLength() {
         StrBuilder sb = new StrBuilder();
+        assertEquals(32, sb.capacity());
+        assertEquals(0, sb.length());
+        assertEquals(0, sb.size());
+        assertTrue(sb.isEmpty());
+
+        sb.minimizeCapacity();
+        assertEquals(0, sb.capacity());
+        assertEquals(0, sb.length());
+        assertEquals(0, sb.size());
+        assertTrue(sb.isEmpty());
+
+        sb.ensureCapacity(32);
+        assertTrue(sb.capacity() >= 32);
+        assertEquals(0, sb.length());
+        assertEquals(0, sb.size());
+        assertTrue(sb.isEmpty());
+
+        sb.append("foo");
+        assertTrue(sb.capacity() >= 32);
+        assertEquals(3, sb.length());
+        assertEquals(3, sb.size());
+        assertTrue(sb.isEmpty() == false);
+
+        sb.clear();
+        assertTrue(sb.capacity() >= 32);
+        assertEquals(0, sb.length());
+        assertEquals(0, sb.size());
+        assertTrue(sb.isEmpty());
+
+        sb.append("123456789012345678901234567890123");
+        assertTrue(sb.capacity() > 32);
+        assertEquals(33, sb.length());
+        assertEquals(33, sb.size());
+        assertTrue(sb.isEmpty() == false);
+
+        sb.ensureCapacity(16);
+        assertTrue(sb.capacity() > 16);
+        assertEquals(33, sb.length());
+        assertEquals(33, sb.size());
+        assertTrue(sb.isEmpty() == false);
+
+        sb.minimizeCapacity();
+        assertEquals(33, sb.capacity());
+        assertEquals(33, sb.length());
+        assertEquals(33, sb.size());
+        assertTrue(sb.isEmpty() == false);
+
+        try {
+            sb.setLength(-1);
+            fail("setLength(-1) expected StringIndexOutOfBoundsException");
+        } catch (StringIndexOutOfBoundsException e) {
+            // expected
+        }
+
+        sb.setLength(33);
+        assertEquals(33, sb.capacity());
+        assertEquals(33, sb.length());
+        assertEquals(33, sb.size());
+        assertTrue(sb.isEmpty() == false);
+
+        sb.setLength(16);
+        assertTrue(sb.capacity() >= 16);
+        assertEquals(16, sb.length());
+        assertEquals(16, sb.size());
+        assertEquals("1234567890123456", sb.toString());
+        assertTrue(sb.isEmpty() == false);
+
+        sb.setLength(32);
+        assertTrue(sb.capacity() >= 32);
+        assertEquals(32, sb.length());
+        assertEquals(32, sb.size());
+        assertEquals("1234567890123456\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 
sb.toString());
+        assertTrue(sb.isEmpty() == false);
+
+        sb.setLength(0);
+        assertTrue(sb.capacity() >= 32);
+        assertEquals(0, sb.length());
+        assertEquals(0, sb.size());
+        assertTrue(sb.isEmpty());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testLength() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(0, sb.length());
+        
+        sb.append("Hello");
+        assertEquals(5, sb.length());
+    }
+
+    public void testSetLength() {
+        StrBuilder sb = new StrBuilder();
+        sb.append("Hello");
+        sb.setLength(2);
+        assertEquals("He", sb.toString());
+
+        try {
+            sb.setLength(-1);
+            fail("setLength(-1) expected StringIndexOutOfBoundsException");
+        } catch (StringIndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testCapacity() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(sb.buf.length, sb.capacity());
+        
+        sb.append("HelloWorldHelloWorldHelloWorldHelloWorld");
+        assertEquals(sb.buf.length, sb.capacity());
+    }
+
+    public void testEnsureCapacity() {
+        StrBuilder sb = new StrBuilder();
+        sb.ensureCapacity(2);
+        assertEquals(true, sb.capacity() >= 2);
+        
+        sb.ensureCapacity(-1);
+        assertEquals(true, sb.capacity() >= 0);
+        
+        sb.append("HelloWorld");
+        sb.ensureCapacity(40);
+        assertEquals(true, sb.capacity() >= 40);
+    }
+
+    public void testMinimizeCapacity() {
+        StrBuilder sb = new StrBuilder();
+        sb.minimizeCapacity();
+        assertEquals(0, sb.capacity());
+        
+        sb.append("HelloWorld");
+        sb.minimizeCapacity();
+        assertEquals(10, sb.capacity());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testSize() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(0, sb.size());
+        
+        sb.append("Hello");
+        assertEquals(5, sb.size());
+    }
+
+    public void testIsEmpty() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(true, sb.isEmpty());
+        
+        sb.append("Hello");
+        assertEquals(false, sb.isEmpty());
+        
+        sb.clear();
+        assertEquals(true, sb.isEmpty());
+    }
+
+    public void testClear() {
+        StrBuilder sb = new StrBuilder();
+        sb.append("Hello");
+        sb.clear();
+        assertEquals(0, sb.length());
+        assertEquals(true, sb.buf.length >= 5);
+    }
+
+    //-----------------------------------------------------------------------
+    public void testCharAt() {
+        StrBuilder sb = new StrBuilder();
+        try {
+            sb.charAt(0);
+            fail("charAt(0) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        try {
+            sb.charAt(-1);
+            fail("charAt(-1) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        sb.append("foo");
+        assertEquals('f', sb.charAt(0));
+        assertEquals('o', sb.charAt(1));
+        assertEquals('o', sb.charAt(2));
+        try {
+            sb.charAt(-1);
+            fail("charAt(-1) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        try {
+            sb.charAt(3);
+            fail("charAt(3) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
+    public void testSetCharAt() {
+        StrBuilder sb = new StrBuilder();
+        try {
+            sb.setCharAt(0, 'f');
+            fail("setCharAt(0,) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        try {
+            sb.setCharAt(-1, 'f');
+            fail("setCharAt(-1,) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        sb.append("foo");
+        sb.setCharAt(0, 'b');
+        sb.setCharAt(1, 'a');
+        sb.setCharAt(2, 'r');
+        try {
+            sb.setCharAt(3, '!');
+            fail("setCharAt(3,) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        assertEquals("bar", sb.toString());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testNullText() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(null, sb.getNullText());
+
+        sb.setNullText("null");
+        assertEquals("null", sb.getNullText());
+
+        sb.setNullText("");
+        assertEquals(null, sb.getNullText());
+
+        sb.setNullText("NULL");
+        assertEquals("NULL", sb.getNullText());
+
+        sb.setNullText((String) null);
+        assertEquals(null, sb.getNullText());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testAppendWithNullText() {
+        StrBuilder sb = new StrBuilder();
+        sb.setNullText("NULL");
         assertEquals("", sb.toString());
 
         sb.appendNull();
+        assertEquals("NULL", sb.toString());
+
+        sb.append((Object) null);
+        assertEquals("NULLNULL", sb.toString());
+
+        sb.append(FOO);
+        assertEquals("NULLNULLfoo", sb.toString());
+
+        sb.append((String) null);
+        assertEquals("NULLNULLfooNULL", sb.toString());
+
+        sb.append("");
+        assertEquals("NULLNULLfooNULL", sb.toString());
+
+        sb.append("bar");
+        assertEquals("NULLNULLfooNULLbar", sb.toString());
+
+        sb.append((StringBuffer) null);
+        assertEquals("NULLNULLfooNULLbarNULL", sb.toString());
+
+        sb.append(new StringBuffer("baz"));
+        assertEquals("NULLNULLfooNULLbarNULLbaz", sb.toString());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testAppend_Object() {
+        StrBuilder sb = new StrBuilder();
+        sb.appendNull();
         assertEquals("", sb.toString());
 
         sb.append((Object) null);
@@ -99,9 +417,13 @@
         sb.append(new StringBuffer("baz"));
         assertEquals("foobarbaz", sb.toString());
 
-        sb.clear();
-        assertEquals("", sb.toString());
+        sb.append(new StrBuilder("yes"));
+        assertEquals("foobarbazyes", sb.toString());
+    }
 
+    public void testAppend_CharArray() {
+        StrBuilder sb = new StrBuilder();
+        
         sb.append((char[]) null);
         assertEquals("", sb.toString());
 
@@ -142,444 +464,296 @@
             // expected
         }
 
-        // These next two cases slip by the error condition checks but are 
silent modifications
-        sb.append(new char[]{'b', 'a', 'r'}, -1, 0);
-        assertEquals("foo", sb.toString());
+        try {
+            sb.append(new char[]{'b', 'a', 'r'}, -1, 3);
+            fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        try {
+            sb.append(new char[]{'b', 'a', 'r'}, 4, 0);
+            fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
 
         sb.append(new char[]{'b', 'a', 'r'}, 3, 0);
         assertEquals("foo", sb.toString());
 
         sb.append(new char[]{'a', 'b', 'c', 'b', 'a', 'r', 'd', 'e', 'f'}, 3, 
3);
         assertEquals("foobar", sb.toString());
+    }
 
+    public void testAppend_Primitive() {
+        StrBuilder sb = new StrBuilder();
         sb.append(true);
-        assertEquals("foobartrue", sb.toString());
+        assertEquals("true", sb.toString());
 
         sb.append(false);
-        assertEquals("foobartruefalse", sb.toString());
+        assertEquals("truefalse", sb.toString());
 
         sb.append('!');
-        assertEquals("foobartruefalse!", sb.toString());
-
-        sb.clear();
-        assertEquals("", sb.toString());
+        assertEquals("truefalse!", sb.toString());
+    }
 
+    public void testAppend_PrimitiveNumber() {
+        StrBuilder sb = new StrBuilder();
         sb.append(0);
         assertEquals("0", sb.toString());
 
         sb.append(1L);
         assertEquals("01", sb.toString());
 
-        sb.append(2.3F);
+        sb.append(2.3f);
         assertEquals("012.3", sb.toString());
 
-        sb.append(4.5D);
+        sb.append(4.5d);
         assertEquals("012.34.5", sb.toString());
     }
 
-    public void testAppendFixedLength() {
-
+    //-----------------------------------------------------------------------
+    public void testAppendPadding() {
         StrBuilder sb = new StrBuilder();
-        assertEquals("", sb.toString());
+        sb.append("foo");
+        assertEquals("foo", sb.toString());
+
+        sb.appendPadding(-1, '-');
+        assertEquals("foo", sb.toString());
+
+        sb.appendPadding(0, '-');
+        assertEquals("foo", sb.toString());
+
+        sb.appendPadding(1, '-');
+        assertEquals("foo-", sb.toString());
 
-        sb.appendFixedLengthPadLeft("foo", -1, '-');
+        sb.appendPadding(16, '-');
+        assertEquals(20, sb.length());
+        //            12345678901234567890
+        assertEquals("foo-----------------", sb.toString());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testAppendFixedWidthPadLeft() {
+        StrBuilder sb = new StrBuilder();
+        sb.appendFixedWidthPadLeft("foo", -1, '-');
         assertEquals("", sb.toString());
 
-        sb.appendFixedLengthPadLeft("foo", 0, '-');
+        sb.clear();
+        sb.appendFixedWidthPadLeft("foo", 0, '-');
         assertEquals("", sb.toString());
 
-        sb.appendFixedLengthPadLeft("foo", 1, '-');
+        sb.clear();
+        sb.appendFixedWidthPadLeft("foo", 1, '-');
         assertEquals("o", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
-        sb.appendFixedLengthPadLeft("foo", 2, '-');
+        sb.appendFixedWidthPadLeft("foo", 2, '-');
         assertEquals("oo", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
-        sb.appendFixedLengthPadLeft("foo", 3, '-');
+        sb.appendFixedWidthPadLeft("foo", 3, '-');
         assertEquals("foo", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
-        sb.appendFixedLengthPadLeft("foo", 4, '-');
+        sb.appendFixedWidthPadLeft("foo", 4, '-');
         assertEquals("-foo", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
-        sb.appendFixedLengthPadLeft("foo", 10, '-');
+        sb.appendFixedWidthPadLeft("foo", 10, '-');
         assertEquals(10, sb.length());
-        // 1234567890
+        //            1234567890
         assertEquals("-------foo", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
+        sb.setNullText("null");
+        sb.appendFixedWidthPadRight(null, 5, '-');
+        assertEquals("-null", sb.toString());
+    }
 
-        sb.appendFixedLengthPadRight("foo", -1, '-');
+    public void testAppendFixedWidthPadLeft_int() {
+        StrBuilder sb = new StrBuilder();
+        sb.appendFixedWidthPadLeft(123, -1, '-');
         assertEquals("", sb.toString());
 
-        sb.appendFixedLengthPadRight("foo", 0, '-');
+        sb.clear();
+        sb.appendFixedWidthPadLeft(123, 0, '-');
         assertEquals("", sb.toString());
 
-        /*
-         * 
-         * TODO: appears the implementation for appendFixedLengthPadRight is 
broken?
-         * 
-         * sb.appendFixedLengthPadRight("foo", 1, '-'); assertEquals("f", 
sb.toString());
-         * 
-         * sb.clear(); assertEquals("", sb.toString());
-         * 
-         * sb.appendFixedLengthPadRight("foo", 2, '-'); assertEquals("fo", 
sb.toString());
-         * 
-         * sb.clear(); assertEquals("", sb.toString());
-         * 
-         * sb.appendFixedLengthPadRight("foo", 3, '-'); assertEquals("foo", 
sb.toString());
-         * 
-         * sb.clear(); assertEquals("", sb.toString());
-         * 
-         * sb.appendFixedLengthPadRight("foo", 4, '-'); assertEquals("foo-", 
sb.toString());
-         * 
-         * sb.clear(); assertEquals("", sb.toString());
-         * 
-         * sb.appendFixedLengthPadRight("foo", 10, '-'); assertEquals(10, 
sb.length()); // 1234567890
-         * assertEquals("foo-------", sb.toString());
-         * 
-         */
-    }
+        sb.clear();
+        sb.appendFixedWidthPadLeft(123, 1, '-');
+        assertEquals("3", sb.toString());
 
-    public void testAppendPadding() {
+        sb.clear();
+        sb.appendFixedWidthPadLeft(123, 2, '-');
+        assertEquals("23", sb.toString());
+
+        sb.clear();
+        sb.appendFixedWidthPadLeft(123, 3, '-');
+        assertEquals("123", sb.toString());
+
+        sb.clear();
+        sb.appendFixedWidthPadLeft(123, 4, '-');
+        assertEquals("-123", sb.toString());
+
+        sb.clear();
+        sb.appendFixedWidthPadLeft(123, 10, '-');
+        assertEquals(10, sb.length());
+        //            1234567890
+        assertEquals("-------123", sb.toString());
+    }
 
+    //-----------------------------------------------------------------------
+    public void testAppendFixedWidthPadRight() {
         StrBuilder sb = new StrBuilder();
-        sb.append("foo");
-        assertEquals("foo", sb.toString());
+        sb.appendFixedWidthPadRight("foo", -1, '-');
+        assertEquals("", sb.toString());
 
-        sb.appendPadding(-1, '-');
-        assertEquals("foo", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight("foo", 0, '-');
+        assertEquals("", sb.toString());
 
-        sb.appendPadding(0, '-');
+        sb.clear();
+        sb.appendFixedWidthPadRight("foo", 1, '-');
+        assertEquals("f", sb.toString());
+
+        sb.clear();
+        sb.appendFixedWidthPadRight("foo", 2, '-');
+        assertEquals("fo", sb.toString());
+
+        sb.clear();
+        sb.appendFixedWidthPadRight("foo", 3, '-');
         assertEquals("foo", sb.toString());
 
-        sb.appendPadding(1, '-');
+        sb.clear();
+        sb.appendFixedWidthPadRight("foo", 4, '-');
         assertEquals("foo-", sb.toString());
 
-        sb.appendPadding(16, '-');
-        assertEquals(20, sb.length());
-        // 12345678901234567890
-        assertEquals("foo-----------------", sb.toString());
-    }
+        sb.clear();
+        sb.appendFixedWidthPadRight("foo", 10, '-');
+        assertEquals(10, sb.length());
+        //            1234567890
+        assertEquals("foo-------", sb.toString());
 
-    public void testAppendWithNullText() {
+        sb.clear();
+        sb.setNullText("null");
+        sb.appendFixedWidthPadRight(null, 5, '-');
+        assertEquals("null-", sb.toString());
+    }
 
+    public void testAppendFixedWidthPadRight_int() {
         StrBuilder sb = new StrBuilder();
-        sb.setNullText("null");
+        sb.appendFixedWidthPadRight(123, -1, '-');
         assertEquals("", sb.toString());
 
-        sb.appendNull();
-        assertEquals("null", sb.toString());
-
-        sb.append((Object) null);
-        assertEquals("nullnull", sb.toString());
-
-        sb.append(FOO);
-        assertEquals("nullnullfoo", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight(123, 0, '-');
+        assertEquals("", sb.toString());
 
-        sb.append((String) null);
-        assertEquals("nullnullfoonull", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight(123, 1, '-');
+        assertEquals("1", sb.toString());
 
-        sb.append("");
-        assertEquals("nullnullfoonull", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight(123, 2, '-');
+        assertEquals("12", sb.toString());
 
-        sb.append("bar");
-        assertEquals("nullnullfoonullbar", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight(123, 3, '-');
+        assertEquals("123", sb.toString());
 
-        sb.append((StringBuffer) null);
-        assertEquals("nullnullfoonullbarnull", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight(123, 4, '-');
+        assertEquals("123-", sb.toString());
 
-        sb.append(new StringBuffer("baz"));
-        assertEquals("nullnullfoonullbarnullbaz", sb.toString());
+        sb.clear();
+        sb.appendFixedWidthPadRight(123, 10, '-');
+        assertEquals(10, sb.length());
+        //            1234567890
+        assertEquals("123-------", sb.toString());
     }
 
-    public void testAppendWithSeparators() {
-
+    //-----------------------------------------------------------------------
+    public void testAppendWithSeparators_Array() {
         StrBuilder sb = new StrBuilder();
-        assertEquals("", sb.toString());
-
         sb.appendWithSeparators((Object[]) null, ",");
         assertEquals("", sb.toString());
 
+        sb.clear();
         sb.appendWithSeparators(new Object[0], ",");
         assertEquals("", sb.toString());
 
+        sb.clear();
         sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, ",");
         assertEquals("foo,bar,baz", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
         sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, null);
         assertEquals("foobarbaz", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
         sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ",");
         assertEquals("foo,,baz", sb.toString());
+    }
 
-        sb.clear();
-        assertEquals("", sb.toString());
-
+    public void testAppendWithSeparators_Collection() {
+        StrBuilder sb = new StrBuilder();
         sb.appendWithSeparators((Collection) null, ",");
         assertEquals("", sb.toString());
 
+        sb.clear();
         sb.appendWithSeparators(Collections.EMPTY_LIST, ",");
         assertEquals("", sb.toString());
 
+        sb.clear();
         sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", 
"baz"}), ",");
         assertEquals("foo,bar,baz", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
         sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", 
"baz"}), null);
         assertEquals("foobarbaz", sb.toString());
 
         sb.clear();
-        assertEquals("", sb.toString());
-
         sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, 
"baz"}), ",");
         assertEquals("foo,,baz", sb.toString());
     }
 
-    public void testAppendWithSeparatorsWithNullText() {
-
+    public void testAppendWithSeparators_Iterator() {
         StrBuilder sb = new StrBuilder();
-        sb.setNullText("null");
+        sb.appendWithSeparators((Iterator) null, ",");
         assertEquals("", sb.toString());
 
-        sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ",");
-        assertEquals("foo,null,baz", sb.toString());
-
         sb.clear();
+        sb.appendWithSeparators(Collections.EMPTY_LIST.iterator(), ",");
         assertEquals("", sb.toString());
 
-        sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, 
"baz"}), ",");
-        assertEquals("foo,null,baz", sb.toString());
-    }
-
-    public void testCapacityAndLength() {
-
-        StrBuilder sb = new StrBuilder();
-        assertEquals(32, sb.capacity());
-        assertEquals(0, sb.length());
-        assertEquals(0, sb.size());
-        assertTrue(sb.isEmpty());
-
-        sb.minimizeCapacity();
-        assertEquals(0, sb.capacity());
-        assertEquals(0, sb.length());
-        assertEquals(0, sb.size());
-        assertTrue(sb.isEmpty());
-
-        sb.ensureCapacity(32);
-        assertTrue(sb.capacity() >= 32);
-        assertEquals(0, sb.length());
-        assertEquals(0, sb.size());
-        assertTrue(sb.isEmpty());
-
-        sb.append("foo");
-        assertTrue(sb.capacity() >= 32);
-        assertEquals(3, sb.length());
-        assertEquals(3, sb.size());
-        assertTrue(sb.isEmpty() == false);
-
         sb.clear();
-        assertTrue(sb.capacity() >= 32);
-        assertEquals(0, sb.length());
-        assertEquals(0, sb.size());
-        assertTrue(sb.isEmpty());
-
-        sb.append("123456789012345678901234567890123");
-        assertTrue(sb.capacity() > 32);
-        assertEquals(33, sb.length());
-        assertEquals(33, sb.size());
-        assertTrue(sb.isEmpty() == false);
-
-        sb.ensureCapacity(16);
-        assertTrue(sb.capacity() > 16);
-        assertEquals(33, sb.length());
-        assertEquals(33, sb.size());
-        assertTrue(sb.isEmpty() == false);
-
-        sb.minimizeCapacity();
-        assertEquals(33, sb.capacity());
-        assertEquals(33, sb.length());
-        assertEquals(33, sb.size());
-        assertTrue(sb.isEmpty() == false);
-
-        try {
-            sb.setLength(-1);
-            fail("setLength(-1) expected StringIndexOutOfBoundsException");
-        } catch (StringIndexOutOfBoundsException e) {
-            // expected
-        }
-
-        sb.setLength(33);
-        assertEquals(33, sb.capacity());
-        assertEquals(33, sb.length());
-        assertEquals(33, sb.size());
-        assertTrue(sb.isEmpty() == false);
-
-        sb.setLength(16);
-        assertTrue(sb.capacity() >= 16);
-        assertEquals(16, sb.length());
-        assertEquals(16, sb.size());
-        assertEquals("1234567890123456", sb.toString());
-        assertTrue(sb.isEmpty() == false);
-
-        sb.setLength(32);
-        assertTrue(sb.capacity() >= 32);
-        assertEquals(32, sb.length());
-        assertEquals(32, sb.size());
-        assertEquals("1234567890123456\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 
sb.toString());
-        assertTrue(sb.isEmpty() == false);
-
-        sb.setLength(0);
-        assertTrue(sb.capacity() >= 32);
-        assertEquals(0, sb.length());
-        assertEquals(0, sb.size());
-        assertTrue(sb.isEmpty());
-    }
-
-    public void testCharArray() {
-        // TODO
-    }
+        sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", 
"baz"}).iterator(), ",");
+        assertEquals("foo,bar,baz", sb.toString());
 
-    public void testConstructor() {
+        sb.clear();
+        sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", 
"baz"}).iterator(), null);
+        assertEquals("foobarbaz", sb.toString());
 
-        StrBuilder sb0 = new StrBuilder();
-        assertTrue(sb0.isEmpty());
-        StrBuilder sb1 = new StrBuilder(32);
-        assertTrue(sb1.isEmpty());
-        StrBuilder sb2 = new StrBuilder(0);
-        assertTrue(sb2.isEmpty());
-        StrBuilder sb3 = new StrBuilder(-1);
-        assertTrue(sb3.isEmpty());
-        StrBuilder sb4 = new StrBuilder(1);
-        assertTrue(sb4.isEmpty());
-        StrBuilder sb5 = new StrBuilder((String) null);
-        assertTrue(sb5.isEmpty());
-        StrBuilder sb6 = new StrBuilder("");
-        assertTrue(sb6.isEmpty());
-        StrBuilder sb7 = new StrBuilder("foo");
-        assertFalse(sb7.isEmpty());
+        sb.clear();
+        sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, 
"baz"}).iterator(), ",");
+        assertEquals("foo,,baz", sb.toString());
     }
 
-    public void testGetSetChar() {
-
+    public void testAppendWithSeparatorsWithNullText() {
         StrBuilder sb = new StrBuilder();
+        sb.setNullText("null");
+        sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ",");
+        assertEquals("foo,null,baz", sb.toString());
 
-        try {
-            sb.charAt(0);
-            fail("charAt(0) expected IndexOutOfBoundsException");
-        } catch (IndexOutOfBoundsException e) {
-            // expected
-        }
-
-        try {
-            sb.charAt(-1);
-            fail("charAt(-1) expected IndexOutOfBoundsException");
-        } catch (IndexOutOfBoundsException e) {
-            // expected
-        }
-
-        try {
-            sb.setCharAt(0, 'f');
-            fail("setCharAt(0,) expected IndexOutOfBoundsException");
-        } catch (IndexOutOfBoundsException e) {
-            // expected
-        }
-
-        try {
-            sb.setCharAt(-1, 'f');
-            fail("setCharAt(-1,) expected IndexOutOfBoundsException");
-        } catch (IndexOutOfBoundsException e) {
-            // expected
-        }
-
-        sb.append("foo");
-        assertEquals('f', sb.charAt(0));
-        assertEquals('o', sb.charAt(1));
-        assertEquals('o', sb.charAt(2));
-
-        try {
-            sb.charAt(3);
-            fail("charAt(3) expected IndexOutOfBoundsException");
-        } catch (IndexOutOfBoundsException e) {
-            // expected
-        }
-
-        sb.setCharAt(0, 'b');
-        sb.setCharAt(1, 'a');
-        sb.setCharAt(2, 'r');
-
-        try {
-            sb.setCharAt(3, '!');
-            fail("setCharAt(3,) expected IndexOutOfBoundsException");
-        } catch (IndexOutOfBoundsException e) {
-            // expected
-        }
-
-        assertEquals('b', sb.charAt(0));
-        assertEquals('a', sb.charAt(1));
-        assertEquals('r', sb.charAt(2));
+        sb.clear();
+        sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, 
"baz"}), ",");
+        assertEquals("foo,null,baz", sb.toString());
     }
 
-    public void testInitialCapacityAndLength() {
-
-        StrBuilder sb0 = new StrBuilder();
-        assertEquals(32, sb0.capacity());
-        assertEquals(0, sb0.length());
-        assertEquals(0, sb0.size());
-
-        StrBuilder sb1 = new StrBuilder(32);
-        assertEquals(32, sb1.capacity());
-        assertEquals(0, sb1.length());
-        assertEquals(0, sb1.size());
-
-        StrBuilder sb2 = new StrBuilder(0);
-        assertEquals(32, sb2.capacity());
-        assertEquals(0, sb2.length());
-        assertEquals(0, sb2.size());
-
-        StrBuilder sb3 = new StrBuilder(-1);
-        assertEquals(32, sb3.capacity());
-        assertEquals(0, sb3.length());
-        assertEquals(0, sb3.size());
-
-        StrBuilder sb4 = new StrBuilder(1);
-        assertEquals(1, sb4.capacity());
-        assertEquals(0, sb4.length());
-        assertEquals(0, sb4.size());
-
-        StrBuilder sb5 = new StrBuilder((String) null);
-        assertEquals(32, sb5.capacity());
-        assertEquals(0, sb5.length());
-        assertEquals(0, sb5.size());
-
-        StrBuilder sb6 = new StrBuilder("");
-        assertEquals(32, sb6.capacity());
-        assertEquals(0, sb6.length());
-        assertEquals(0, sb6.size());
-
-        StrBuilder sb7 = new StrBuilder("foo");
-        assertEquals(35, sb7.capacity());
-        assertEquals(3, sb7.length());
-        assertEquals(3, sb7.size());
+    //-----------------------------------------------------------------------
+    public void testCharArray() {
+        // TODO
     }
 
     public void testInsert() {
@@ -899,21 +1073,4 @@
         assertEquals("foonullbarbaz", sb.toString());
     }
 
-    public void testNullText() {
-
-        StrBuilder sb = new StrBuilder();
-        assertEquals(null, sb.getNullText());
-
-        sb.setNullText("null");
-        assertEquals("null", sb.getNullText());
-
-        sb.setNullText("");
-        assertEquals(null, sb.getNullText());
-
-        sb.setNullText("foo");
-        assertEquals("foo", sb.getNullText());
-
-        sb.setNullText((String) null);
-        assertEquals(null, sb.getNullText());
-    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to