Fix setLength()

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230906 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-08-08 21:50:57 +00:00
parent 8048e49d99
commit e6f6b0dbfa
2 changed files with 6 additions and 2 deletions

View File

@ -155,7 +155,7 @@ public class StrBuilder implements Cloneable {
}
if (length < size) {
size = length;
} else {
} else if (length > size) {
ensureCapacity(length);
int oldEnd = size;
int newEnd = length;

View File

@ -269,8 +269,12 @@ public class StrBuilderTest extends TestCase {
public void testSetLength() {
StrBuilder sb = new StrBuilder();
sb.append("Hello");
sb.setLength(2);
sb.setLength(2); // shorten
assertEquals("He", sb.toString());
sb.setLength(2); // no change
assertEquals("He", sb.toString());
sb.setLength(3); // lengthen
assertEquals("He\0", sb.toString());
try {
sb.setLength(-1);