Attached is a patch with some additional StrBuilder tests and some small fixes. I'll try to put some more together later.
-----Original Message----- From: Nathan Beyer [mailto:[EMAIL PROTECTED] Sent: Saturday, July 30, 2005 3:57 PM To: 'Jakarta Commons Developers List' Subject: RE: [lang] Better coverage for StrBuilder and StrTokenizer. I'm willing to do some JUnit building. Any other areas that need more testing? I presume you're working off the main trunk. -----Original Message----- From: Gary Gregory [mailto:[EMAIL PROTECTED] Sent: Saturday, July 30, 2005 12:18 PM To: Jakarta Commons Developers List Subject: [lang] Better coverage for StrBuilder and StrTokenizer. Do any of the original authors of StrBuilder and StrTokenizer want to provide more unit tests to improve the current ~50% code coverage? ;-) For current coverage, do a local Maven build or see: http://people.apache.org/~ggregory/commons-lang/2.2-dev/docs/clover/org/ apache/commons/lang/text/pkg-summary.html Thanks, Gary --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Index: C:/dev/workspace/commons-lang/src/test/org/apache/commons/lang/text/StrBuilderTest.java =================================================================== --- C:/dev/workspace/commons-lang/src/test/org/apache/commons/lang/text/StrBuilderTest.java (revision 224558) +++ C:/dev/workspace/commons-lang/src/test/org/apache/commons/lang/text/StrBuilderTest.java (working copy) @@ -751,11 +751,6 @@ assertEquals("foo,null,baz", sb.toString()); } - //----------------------------------------------------------------------- - public void testCharArray() { - // TODO - } - public void testInsert() { StrBuilder sb = new StrBuilder(); @@ -1073,4 +1068,148 @@ assertEquals("foonullbarbaz", sb.toString()); } + public void testToCharArray ( ) { + + StrBuilder sb = new StrBuilder(); + + char[] a = sb.toCharArray(); + assertNotNull ("toCharArray() result is null", a); + assertEquals ("toCharArray() result is too large", 0, a.length); + + sb.append("junit"); + a = sb.toCharArray(); + assertEquals ("toCharArray() result incorrect length",5, a.length); + assertTrue ("toCharArray() result does not match",Arrays.equals("junit".toCharArray(), a)); + } + + public void testToCharArrayIntInt() { + StrBuilder sb = new StrBuilder(); + sb.append("junit"); + char[] a = sb.toCharArray(0, 20); //too large test + assertEquals ("toCharArray(int,int) result incorrect length",5, a.length); + assertTrue ("toCharArray(int,int) result does not match",Arrays.equals("junit".toCharArray(), a)); + + a = sb.toCharArray(0, 4); + assertEquals ("toCharArray(int,int) result incorrect length",4, a.length); + assertTrue ("toCharArray(int,int) result does not match",Arrays.equals("juni".toCharArray(), a)); + + a = sb.toCharArray(0, 4); + assertEquals ("toCharArray(int,int) result incorrect length",4, a.length); + assertTrue ("toCharArray(int,int) result does not match",Arrays.equals("juni".toCharArray(), a)); + + a = sb.toCharArray(0,1); + assertNotNull ("toCharArray(int,int) result is null", a); + + try { + sb.toCharArray(-1, 5); + fail ("no string index out of bound on -1"); + } + catch (StringIndexOutOfBoundsException e) {} + + try { + sb.toCharArray(6, 5); + fail ("no string index out of bound on -1"); + } + catch (StringIndexOutOfBoundsException e) {} + } + + public void testGetChars ( ) { + + StrBuilder sb = new StrBuilder(); + + char[] input = new char[10]; + char[] a = sb.getChars(input); + assertSame (input, a); + assertTrue(Arrays.equals(new char[10], a)); + + sb.append("junit"); + a = sb.getChars(input); + assertSame(input, a); + assertTrue(Arrays.equals(new char[] {'j','u','n','i','t',0,0,0,0,0},a)); + + a = sb.getChars(null); + assertNotSame(input,a); + assertEquals(5,a.length); + assertTrue(Arrays.equals("junit".toCharArray(),a)); + + input = new char[5]; + a = sb.getChars(input); + assertSame(input, a); + + input = new char[4]; + a = sb.getChars(input); + assertNotSame(input, a); + } + + public void testGetCharsIntIntCharArrayInt( ) { + + StrBuilder sb = new StrBuilder(); + + sb.append("junit"); + char[] a = new char[5]; + sb.getChars(0,5,a,0); + assertTrue(Arrays.equals(new char[] {'j','u','n','i','t'},a)); + + a = new char[5]; + sb.getChars(0,2,a,3); + assertTrue(Arrays.equals(new char[] {0,0,0,'j','u'},a)); + + try { + sb.getChars(-1,0,a,0); + fail("no exception"); + } + catch (StringIndexOutOfBoundsException e) { + } + + try { + sb.getChars(0,-1,a,0); + fail("no exception"); + } + catch (StringIndexOutOfBoundsException e) { + } + + try { + sb.getChars(0,20,a,0); + fail("no exception"); + } + catch (StringIndexOutOfBoundsException e) { + } + + try { + sb.getChars(4,2,a,0); + fail("no exception"); + } + catch (StringIndexOutOfBoundsException e) { + } + } + + public void testAppendStringBuffer() { + StrBuilder sb = new StrBuilder(); + + sb = sb.append(new StringBuffer()); + assertNotNull(sb); + + sb = sb.append(new StringBuffer("junit")); + + assertEquals ("junit", sb.toString()); + } + + public void testAppendStrBuilder() { + StrBuilder sb = new StrBuilder(); + + sb = sb.append((StrBuilder)null); + assertNotNull(sb); + + sb = sb.append(new StrBuilder()); + assertNotNull(sb); + assertEquals("", sb.toString()); + } + + public void toStringBuffer() { + StrBuilder sb = new StrBuilder(); + assertEquals (new StringBuffer().toString(), sb.toStringBuffer().toString()); + + sb.append("junit"); + assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString()); + } } Index: C:/dev/workspace/commons-lang/src/java/org/apache/commons/lang/text/StrBuilder.java =================================================================== --- C:/dev/workspace/commons-lang/src/java/org/apache/commons/lang/text/StrBuilder.java (revision 224558) +++ C:/dev/workspace/commons-lang/src/java/org/apache/commons/lang/text/StrBuilder.java (working copy) @@ -54,6 +54,7 @@ */ public class StrBuilder implements Cloneable { + private static final char[] EMPTY_ARRAY = new char[0]; /** * The extra capacity for new builders. */ @@ -271,6 +272,8 @@ * @return a new array that represents the contents of the builder */ public char[] toCharArray() { + if (size == 0) + return EMPTY_ARRAY; char chars[] = new char[size]; System.arraycopy(buf, 0, chars, 0, size); return chars; @@ -283,11 +286,18 @@ * @param endIndex the end index, exclusive, must be valid except * that if too large it is treated as end of string * @return a new array that holds part of the contents of the builder + * + * @throws StringIndexOutOfBoundsException when <code>startIndex</code> is less than 0; + * when <code>startIndex</code> is greater than <code>endIndex</code> (if <code>endIndex</code> + * is larger than [EMAIL PROTECTED] #size() }, then it is massaged to equal [EMAIL PROTECTED] #size()} before the validation). */ public char[] toCharArray(int startIndex, int endIndex) { endIndex = validateRange(startIndex, endIndex); - char chars[] = new char[size]; - System.arraycopy(buf, startIndex, chars, 0, endIndex - startIndex); + int len = endIndex - startIndex; + if (len == 0) + return EMPTY_ARRAY; + char chars[] = new char[len]; + System.arraycopy(buf, startIndex, chars, 0, len); return chars; }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]