From 810e69f7a470eb9dce71de314f0a98c0f7840a24 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 14 Aug 2005 21:45:47 +0000 Subject: [PATCH] Add StrMatcher and update StrBuilder and test cases to use it, plus fix other bugs git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@232652 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/text/StrBuilder.java | 466 +++- .../apache/commons/lang/text/StrMatcher.java | 405 +++ .../lang/text/StrBuilderAppendInsertTest.java | 1017 ++++++++ .../commons/lang/text/StrBuilderTest.java | 2308 +++++++---------- .../commons/lang/text/StrMatcherTest.java | 227 ++ .../commons/lang/text/TextTestSuite.java | 2 + 6 files changed, 2990 insertions(+), 1435 deletions(-) create mode 100644 src/java/org/apache/commons/lang/text/StrMatcher.java create mode 100644 src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java create mode 100644 src/test/org/apache/commons/lang/text/StrMatcherTest.java diff --git a/src/java/org/apache/commons/lang/text/StrBuilder.java b/src/java/org/apache/commons/lang/text/StrBuilder.java index 2d52da538..089331fb7 100644 --- a/src/java/org/apache/commons/lang/text/StrBuilder.java +++ b/src/java/org/apache/commons/lang/text/StrBuilder.java @@ -242,6 +242,8 @@ public void clear() { /** * Gets the character at the specified index. * + * @see #setCharAt(int, char) + * @see #deleteCharAt(int) * @param index the index to retrieve, must be valid * @return the character at the index * @throws IndexOutOfBoundsException if the index is invalid @@ -256,6 +258,8 @@ public char charAt(int index) { /** * Sets the character at the specified index. * + * @see #charAt(int) + * @see #deleteCharAt(int) * @param index the index to set * @param ch the new character * @throws IndexOutOfBoundsException if the index is invalid @@ -267,6 +271,23 @@ public void setCharAt(int index, char ch) { buffer[index] = ch; } + /** + * Deletes the character at the specified index. + * + * @see #charAt(int) + * @see #setCharAt(int, char) + * @param index the index to delete + * @return this, to enable chaining + * @throws IndexOutOfBoundsException if the index is invalid + */ + public StrBuilder deleteCharAt(int index) { + if (index < 0 || index >= size) { + throw new StringIndexOutOfBoundsException(index); + } + deleteImpl(index, index + 1, 1); + return this; + } + //----------------------------------------------------------------------- /** * Copies the builder's character array into a new character array. @@ -1024,6 +1045,19 @@ public StrBuilder insert(int index, double value) { } //----------------------------------------------------------------------- + /** + * Internal method to delete a range without validation. + * + * @param startIndex the start index, must be valid + * @param endIndex the end index (exclusive), must be valid + * @param len the length, must be valid + * @throws IndexOutOfBoundsException if any index is invalid + */ + private void deleteImpl(int startIndex, int endIndex, int len) { + System.arraycopy(buffer, endIndex, buffer, startIndex, size - endIndex); + size -= len; + } + /** * Deletes the characters between the two specified indices. * @@ -1037,35 +1071,19 @@ public StrBuilder delete(int startIndex, int endIndex) { endIndex = validateRange(startIndex, endIndex); int len = endIndex - startIndex; if (len > 0) { - System.arraycopy(buffer, endIndex, buffer, startIndex, size - endIndex); - size -= len; + deleteImpl(startIndex, endIndex, len); } return this; } - /** - * Deletes the character at the specified index. - * - * @param index the index to delete - * @return this, to enable chaining - * @throws IndexOutOfBoundsException if the index is invalid - */ - public StrBuilder deleteCharAt(int index) { - if (index < 0 || index >= size) { - throw new StringIndexOutOfBoundsException(index); - } - System.arraycopy(buffer, index + 1, buffer, index, size - index - 1); - size--; - return this; - } - + //----------------------------------------------------------------------- /** * Deletes the character wherever it occurs in the builder. - * + * * @param ch the character to delete * @return this, to enable chaining */ - public StrBuilder delete(char ch) { + public StrBuilder deleteAll(char ch) { for (int i = 0; i < size; i++) { if (buffer[i] == ch) { int start = i; @@ -1074,26 +1092,25 @@ public StrBuilder delete(char ch) { break; } } - System.arraycopy(buffer, i, buffer, start, size - i); - size -= (i - start); + int len = i - start; + deleteImpl(start, i, len); + i -= len; } } return this; } /** - * Deletes the string wherever it occurs in the builder. - * - * @param str the string to delete, null causes no action + * Deletes the character wherever it occurs in the builder. + * + * @param ch the character to delete * @return this, to enable chaining */ - public StrBuilder delete(String str) { - int len = (str == null ? 0 : str.length()); - if (len > 0) { - int index = indexOf(str, 0); - while (index >= 0) { - delete(index, index + len); - index = indexOf(str, index); + public StrBuilder deleteFirst(char ch) { + for (int i = 0; i < size; i++) { + if (buffer[i] == ch) { + deleteImpl(i, i + 1, 1); + break; } } return this; @@ -1101,68 +1118,121 @@ public StrBuilder delete(String str) { //----------------------------------------------------------------------- /** - * Replaces a portion of the string builder with another string. - * The length of the inserted string does not have to match the removed length. - * - * @param startIndex the start index, inclusive, must be valid - * @param endIndex the end index, exclusive, must be valid except - * that if too large it is treated as end of string - * @param str the string to replace with + * Deletes the string wherever it occurs in the builder. + * + * @param str the string to delete, null causes no action * @return this, to enable chaining - * @throws IndexOutOfBoundsException if the index is invalid */ - public StrBuilder replace(int startIndex, int endIndex, String str) { - endIndex = validateRange(startIndex, endIndex); - int insertLen = str.length(); - int removeLen = endIndex - startIndex; - int newSize = size - removeLen + insertLen; - if (insertLen > removeLen) { - ensureCapacity(newSize); + public StrBuilder deleteAll(String str) { + int len = (str == null ? 0 : str.length()); + if (len > 0) { + int index = indexOf(str, 0); + while (index >= 0) { + deleteImpl(index, index + len, len); + index = indexOf(str, index); + } } + return this; + } + + /** + * Deletes the string wherever it occurs in the builder. + * + * @param str the string to delete, null causes no action + * @return this, to enable chaining + */ + public StrBuilder deleteFirst(String str) { + int len = (str == null ? 0 : str.length()); + if (len > 0) { + int index = indexOf(str, 0); + if (index >= 0) { + deleteImpl(index, index + len, len); + } + } + return this; + } + + //----------------------------------------------------------------------- + /** + * Deletes all parts of the builder that the matcher matches. + *

+ * Matchers can be used to perform advanced deletion behaviour. + * For example you could write a matcher to delete all occurances + * where the character 'a' is followed by a number. + * + * @param matcher the matcher to use to find the deletion, null causes no action + * @return this, to enable chaining + */ + public StrBuilder deleteAll(StrMatcher matcher) { + return replace(matcher, null, 0, size, -1); + } + + /** + * Deletes the first match within the builder using the specified matcher. + *

+ * Matchers can be used to perform advanced deletion behaviour. + * For example you could write a matcher to delete + * where the character 'a' is followed by a number. + * + * @param matcher the matcher to use to find the deletion, null causes no action + * @return this, to enable chaining + */ + public StrBuilder deleteFirst(StrMatcher matcher) { + return replace(matcher, null, 0, size, 1); + } + + //----------------------------------------------------------------------- + /** + * Internal method to delete a range without validation. + * + * @param startIndex the start index, must be valid + * @param endIndex the end index (exclusive), must be valid + * @param removeLen the length to remove (endIndex - startIndex), must be valid + * @param insertStr the string to replace with, null means delete range + * @param insertLen the length of the insert string, must be valid + * @param len the length, must be valid + * @throws IndexOutOfBoundsException if any index is invalid + */ + private void replaceImpl(int startIndex, int endIndex, int removeLen, String insertStr, int insertLen) { + int newSize = size - removeLen + insertLen; if (insertLen != removeLen) { + ensureCapacity(newSize); System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex); size = newSize; } - str.getChars(0, insertLen, buffer, startIndex); - return this; + if (insertLen > 0) { + insertStr.getChars(0, insertLen, buffer, startIndex); + } } /** - * Replaces a portion of the string builder with another string builder. + * Replaces a portion of the string builder with another string. * The length of the inserted string does not have to match the removed length. - * + * * @param startIndex the start index, inclusive, must be valid * @param endIndex the end index, exclusive, must be valid except * that if too large it is treated as end of string - * @param builder the string builder to replace with + * @param replaceStr the string to replace with, null means delete range * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ - public StrBuilder replace(int startIndex, int endIndex, StrBuilder builder) { + public StrBuilder replace(int startIndex, int endIndex, String replaceStr) { endIndex = validateRange(startIndex, endIndex); - int insertLen = builder.length(); - int removeLen = endIndex - startIndex; - if (insertLen > removeLen) { - ensureCapacity(size - removeLen + insertLen); - } - if (insertLen != removeLen) { - //shift the current characters to the right - System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex); - //adjust the size accordingly - size += (insertLen - removeLen); - } - builder.getChars(0, insertLen, buffer, startIndex); + int insertLen = (replaceStr == null ? 0 : replaceStr.length()); + replaceImpl(startIndex, endIndex, endIndex - startIndex, replaceStr, insertLen); return this; } + //----------------------------------------------------------------------- /** - * Replaces the search character with the replace character throughout the builder. - * - * @param search the search string, null causes no action to occur - * @param replace the replace string, null is equivalent to an empty string + * Replaces the search character with the replace character + * throughout the builder. + * + * @param search the search character + * @param replace the replace character * @return this, to enable chaining */ - public StrBuilder replace(char search, char replace) { + public StrBuilder replaceAll(char search, char replace) { if (search != replace) { for (int i = 0; i < size; i++) { if (buffer[i] == search) { @@ -1173,21 +1243,153 @@ public StrBuilder replace(char search, char replace) { return this; } + /** + * Replaces the first instance of the search character with the + * replace character in the builder. + * + * @param search the search character + * @param replace the replace character + * @return this, to enable chaining + */ + public StrBuilder replaceFirst(char search, char replace) { + if (search != replace) { + for (int i = 0; i < size; i++) { + if (buffer[i] == search) { + buffer[i] = replace; + break; + } + } + } + return this; + } + + //----------------------------------------------------------------------- /** * Replaces the search string with the replace string throughout the builder. - * + * * @param searchStr the search string, null causes no action to occur * @param replaceStr the replace string, null is equivalent to an empty string * @return this, to enable chaining */ - public StrBuilder replace(String searchStr, String replaceStr) { + public StrBuilder replaceAll(String searchStr, String replaceStr) { int searchLen = (searchStr == null ? 0 : searchStr.length()); if (searchLen > 0) { - replaceStr = (replaceStr == null ? "" : replaceStr); + int replaceLen = (replaceStr == null ? 0 : replaceStr.length()); int index = indexOf(searchStr, 0); while (index >= 0) { - replace(index, index + searchLen, replaceStr); - index = indexOf(searchStr, index); + replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen); + index = indexOf(searchStr, index + replaceLen); + } + } + return this; + } + + /** + * Replaces the first instance of the search string with the replace string. + * + * @param searchStr the search string, null causes no action to occur + * @param replaceStr the replace string, null is equivalent to an empty string + * @return this, to enable chaining + */ + public StrBuilder replaceFirst(String searchStr, String replaceStr) { + int searchLen = (searchStr == null ? 0 : searchStr.length()); + if (searchLen > 0) { + int index = indexOf(searchStr, 0); + if (index >= 0) { + int replaceLen = (replaceStr == null ? 0 : replaceStr.length()); + replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen); + } + } + return this; + } + + //----------------------------------------------------------------------- + /** + * Replaces all matches within the builder with the replace string. + *

+ * Matchers can be used to perform advanced replace behaviour. + * For example you could write a matcher to replace all occurances + * where the character 'a' is followed by a number. + * + * @param matcher the matcher to use to find the deletion, null causes no action + * @param replaceStr the replace string, null is equivalent to an empty string + * @return this, to enable chaining + */ + public StrBuilder replaceAll(StrMatcher matcher, String replaceStr) { + return replace(matcher, replaceStr, 0, size, -1); + } + + /** + * Replaces the first match within the builder with the replace string. + *

+ * Matchers can be used to perform advanced replace behaviour. + * For example you could write a matcher to replace + * where the character 'a' is followed by a number. + * + * @param matcher the matcher to use to find the deletion, null causes no action + * @param replaceStr the replace string, null is equivalent to an empty string + * @return this, to enable chaining + */ + public StrBuilder replaceFirst(StrMatcher matcher, String replaceStr) { + return replace(matcher, replaceStr, 0, size, 1); + } + + // ----------------------------------------------------------------------- + /** + * Advanced search and replaces within the builder using a matcher. + *

+ * Matchers can be used to perform advanced behaviour. + * For example you could write a matcher to delete all occurances + * where the character 'a' is followed by a number. + * + * @param matcher the matcher to use to find the deletion, null causes no action + * @param replaceStr the string to replace the match with, null is a delete + * @param startIndex the start index, inclusive, must be valid + * @param endIndex the end index, exclusive, must be valid except + * that if too large it is treated as end of string + * @param replaceCount the number of times to replace, -1 for replace all + * @return this, to enable chaining + * @throws IndexOutOfBoundsException if start index is invalid + */ + public StrBuilder replace( + StrMatcher matcher, String replaceStr, + int startIndex, int endIndex, int replaceCount) { + endIndex = validateRange(startIndex, endIndex); + return replaceImpl(matcher, replaceStr, startIndex, endIndex, replaceCount); + } + + /** + * Replaces within the builder using a matcher. + *

+ * Matchers can be used to perform advanced behaviour. + * For example you could write a matcher to delete all occurances + * where the character 'a' is followed by a number. + * + * @param matcher the matcher to use to find the deletion, null causes no action + * @param replaceStr the string to replace the match with, null is a delete + * @param from the start index, must be valid + * @param to the end index (exclusive), must be valid + * @param replaceCount the number of times to replace, -1 for replace all + * @return this, to enable chaining + * @throws IndexOutOfBoundsException if any index is invalid + */ + private StrBuilder replaceImpl( + StrMatcher matcher, String replaceStr, + int from, int to, int replaceCount) { + if (matcher == null || size == 0) { + return this; + } + int replaceLen = (replaceStr == null ? 0 : replaceStr.length()); + char[] buf = buffer; + for (int i = from; i < to && replaceCount != 0; i++) { + int removeLen = matcher.isMatch(buf, i, from, to); + if (removeLen > 0) { + replaceImpl(i, i + removeLen, removeLen, replaceStr, replaceLen); + to = to - removeLen + replaceLen; + i = i + replaceLen - 1; + if (replaceCount > 0) { + replaceCount--; + } } } return this; @@ -1375,8 +1577,8 @@ public String midString(int index, int length) { //----------------------------------------------------------------------- /** - * Checks of the string builder contains the specified char. - * + * Checks if the string builder contains the specified char. + * * @param ch the character to find * @return true if the builder contains the character */ @@ -1391,8 +1593,8 @@ public boolean contains(char ch) { } /** - * Checks of the string builder contains the specified string. - * + * Checks if the string builder contains the specified string. + * * @param str the string to find * @return true if the builder contains the string */ @@ -1400,6 +1602,21 @@ public boolean contains(String str) { return indexOf(str, 0) >= 0; } + /** + * Checks if the string builder contains a string matched using the + * specified matcher. + *

+ * Matchers can be used to perform advanced searching behaviour. + * For example you could write a matcher to search for the character + * 'a' followed by a number. + * + * @param matcher the matcher to use, null returns -1 + * @return true if the matcher finds a match in the builder + */ + public boolean contains(StrMatcher matcher) { + return indexOf(matcher, 0) >= 0; + } + //----------------------------------------------------------------------- /** * Searches the string builder to find the first reference to the specified char. @@ -1415,7 +1632,7 @@ public int indexOf(char ch) { * Searches the string builder to find the first reference to the specified char. * * @param ch the character to find - * @param startIndex the index to start at, must be valid + * @param startIndex the index to start at, invalid index rounded to edge * @return the first index of the character, or -1 if not found */ public int indexOf(char ch, int startIndex) { @@ -1451,7 +1668,7 @@ public int indexOf(String str) { * Note that a null input string will return -1, whereas the JDK throws an exception. * * @param str the string to find, null returns -1 - * @param startIndex the index to start at, must be valid + * @param startIndex the index to start at, invalid index rounded to edge * @return the first index of the string, or -1 if not found */ public int indexOf(String str, int startIndex) { @@ -1481,6 +1698,49 @@ public int indexOf(String str, int startIndex) { return -1; } + /** + * Searches the string builder using the matcher to find the first match. + *

+ * Matchers can be used to perform advanced searching behaviour. + * For example you could write a matcher to find the character 'a' + * followed by a number. + * + * @param matcher the matcher to use, null returns -1 + * @return the first index matched, or -1 if not found + */ + public int indexOf(StrMatcher matcher) { + return indexOf(matcher, 0); + } + + /** + * Searches the string builder using the matcher to find the first + * match searching from the given index. + *

+ * Matchers can be used to perform advanced searching behaviour. + * For example you could write a matcher to find the character 'a' + * followed by a number. + * + * @param matcher the matcher to use, null returns -1 + * @param startIndex the index to start at, invalid index rounded to edge + * @return the first index matched, or -1 if not found + */ + public int indexOf(StrMatcher matcher, int startIndex) { + startIndex = (startIndex < 0 ? 0 : startIndex); + if (matcher == null || startIndex >= size) { + return -1; + } + int len = size; + if (len > 0) { + char[] buf = buffer; + for (int i = startIndex; i < len; i++) { + if (matcher.isMatch(buf, i, startIndex, len) > 0) { + return i; + } + } + } + return -1; + } + //----------------------------------------------------------------------- /** * Searches the string builder to find the last reference to the specified char. @@ -1561,6 +1821,50 @@ public int lastIndexOf(String str, int startIndex) { return -1; } + /** + * Searches the string builder using the matcher to find the last match. + *

+ * Matchers can be used to perform advanced searching behaviour. + * For example you could write a matcher to find the character 'a' + * followed by a number. + * + * @param matcher the matcher to use, null returns -1 + * @return the last index matched, or -1 if not found + */ + public int lastIndexOf(StrMatcher matcher) { + return lastIndexOf(matcher, size); + } + + /** + * Searches the string builder using the matcher to find the last + * match searching from the given index. + *

+ * Matchers can be used to perform advanced searching behaviour. + * For example you could write a matcher to find the character 'a' + * followed by a number. + * + * @param matcher the matcher to use, null returns -1 + * @param startIndex the index to start at, invalid index rounded to edge + * @return the last index matched, or -1 if not found + */ + public int lastIndexOf(StrMatcher matcher, int startIndex) { + startIndex = (startIndex >= size ? size - 1 : startIndex); + if (matcher == null || startIndex < 0) { + return -1; + } + int len = size; + if (len > 0) { + char[] buf = buffer; + int endIndex = startIndex + 1; + for (int i = startIndex; i >= 0; i--) { + if (matcher.isMatch(buf, i, 0, endIndex) > 0) { + return i; + } + } + } + return -1; + } + //----------------------------------------------------------------------- /** * Gets the contents of this builder as a Reader. diff --git a/src/java/org/apache/commons/lang/text/StrMatcher.java b/src/java/org/apache/commons/lang/text/StrMatcher.java new file mode 100644 index 000000000..5231a6bc6 --- /dev/null +++ b/src/java/org/apache/commons/lang/text/StrMatcher.java @@ -0,0 +1,405 @@ +/* + * Copyright 2003-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang.text; + +import java.util.Arrays; + +/** + * A matcher class that can be queried to determine if a character array + * portion matches. + *

+ * This class comes complete with various constants and factory methods. + * If these do not suffice, you can subclass and implement your own matcher. + * + * @author Stephen Colebourne + * @since 2.2 + * @version $Id$ + */ +public abstract class StrMatcher { + + /** + * Matches the comma character. + */ + private static final StrMatcher COMMA_MATCHER = new CharMatcher(','); + /** + * Matches the tab character. + */ + private static final StrMatcher TAB_MATCHER = new CharMatcher('\t'); + /** + * Matches the space character. + */ + private static final StrMatcher SPACE_MATCHER = new CharMatcher(' '); + /** + * Matches the same characters as StringTokenizer, + * namely space, tab, newline, formfeed. + */ + private static final StrMatcher SPLIT_MATCHER = new CharSetMatcher(" \t\n\r\f".toCharArray()); + /** + * Matches the String trim() whitespace characters. + */ + private static final StrMatcher TRIM_MATCHER = new TrimMatcher(); + /** + * Matches the double quote character. + */ + private static final StrMatcher SINGLE_QUOTE_MATCHER = new CharMatcher('\''); + /** + * Matches the double quote character. + */ + private static final StrMatcher DOUBLE_QUOTE_MATCHER = new CharMatcher('"'); + /** + * Matches the single or double quote character. + */ + private static final StrMatcher QUOTE_MATCHER = new CharSetMatcher("'\"".toCharArray()); + /** + * Matches no characters. + */ + private static final StrMatcher NONE_MATCHER = new NoMatcher(); + + // ----------------------------------------------------------------------- + + /** + * Returns a matcher which matches the comma character. + * + * @return a matcher for a comma + */ + public static StrMatcher commaMatcher() { + return COMMA_MATCHER; + } + + /** + * Returns a matcher which matches the tab character. + * + * @return a matcher for a tab + */ + public static StrMatcher tabMatcher() { + return TAB_MATCHER; + } + + /** + * Returns a matcher which matches the space character. + * + * @return a matcher for a space + */ + public static StrMatcher spaceMatcher() { + return SPACE_MATCHER; + } + + /** + * Matches the same characters as StringTokenizer, + * namely space, tab, newline and formfeed. + * + * @return the split matcher + */ + public static StrMatcher splitMatcher() { + return SPLIT_MATCHER; + } + + /** + * Matches the String trim() whitespace characters. + * + * @return the trim matcher + */ + public static StrMatcher trimMatcher() { + return TRIM_MATCHER; + } + + /** + * Returns a matcher which matches the single quote character. + * + * @return a matcher for a single quote + */ + public static StrMatcher singleQuoteMatcher() { + return SINGLE_QUOTE_MATCHER; + } + + /** + * Returns a matcher which matches the double quote character. + * + * @return a matcher for a double quote + */ + public static StrMatcher doubleQuoteMatcher() { + return DOUBLE_QUOTE_MATCHER; + } + + /** + * Returns a matcher which matches the single or double quote character. + * + * @return a matcher for a single or double quote + */ + public static StrMatcher quoteMatcher() { + return QUOTE_MATCHER; + } + + /** + * Matches no characters. + * + * @return a matcher that matches nothing + */ + public static StrMatcher noneMatcher() { + return NONE_MATCHER; + } + + /** + * Constructor that creates a matcher from a character. + * + * @param ch the character to match, must not be null + * @return a new Matcher for the given char + */ + public static StrMatcher charMatcher(char ch) { + return new CharMatcher(ch); + } + + /** + * Constructor that creates a matcher from a set of characters. + * + * @param chars the characters to match, null or empty matches nothing + * @return a new matcher for the given char[] + */ + public static StrMatcher charSetMatcher(char[] chars) { + if (chars == null || chars.length == 0) { + return NONE_MATCHER; + } + if (chars.length == 1) { + return new CharMatcher(chars[0]); + } + return new CharSetMatcher(chars); + } + + /** + * Constructor that creates a matcher from a string representing a set of characters. + * + * @param chars the characters to match, null or empty matches nothing + * @return a new Matcher for the given characters + */ + public static StrMatcher charSetMatcher(String chars) { + if (chars == null || chars.length() == 0) { + return NONE_MATCHER; + } + if (chars.length() == 1) { + return new CharMatcher(chars.charAt(0)); + } + return new CharSetMatcher(chars.toCharArray()); + } + + /** + * Constructor that creates a matcher from a string. + * + * @param str the string to match, null or empty matches nothing + * @return a new Matcher for the given String + */ + public static StrMatcher stringMatcher(String str) { + if (str == null || str.length() == 0) { + return NONE_MATCHER; + } + return new StringMatcher(str); + } + + //----------------------------------------------------------------------- + /** + * Constructor. + */ + protected StrMatcher() { + super(); + } + + /** + * Returns the number of matching characters, zero for no match. + *

+ * This method is called to check for a match. + * The parameter pos represents the current position to be + * checked in the string buffer (a character array which must + * not be changed). + * The API guarantees that pos is a valid index for buffer. + *

+ * The character array may be larger than the active area to be matched. + * Only values in the buffer between the specifed indices may be accessed. + *

+ * The matching code may check one character or many. + * It may check characters preceeding pos as well as those + * after, so long as no checks exceed the bounds specified. + *

+ * It must return zero for no match, or a positive number if a match was found. + * The number indicates the number of characters that matched. + * + * @param buffer the text content to match against, do not change + * @param pos the starting position for the match, valid for buffer + * @param bufferStart the first active index in the buffer, valid for buffer + * @param bufferEnd the end index (exclusive) of the active buffer, valid for buffer + * @return the number of matching characters, zero for no match + */ + public abstract int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd); + + //----------------------------------------------------------------------- + /** + * Class used to define a set of characters for matching purposes. + */ + static final class CharSetMatcher extends StrMatcher { + /** The set of characters to match. */ + private char[] chars; + + /** + * Constructor that creates a matcher from a character array. + * + * @param chars the characters to match, must not be null + */ + CharSetMatcher(char chars[]) { + super(); + this.chars = (char[]) chars.clone(); + Arrays.sort(this.chars); + } + + /** + * Returns whether or not the given charatcer matches. + * + * @param buffer the text content to match against, do not change + * @param pos the starting position for the match, valid for buffer + * @param bufferStart the first active index in the buffer, valid for buffer + * @param bufferEnd the end index of the active buffer, valid for buffer + * @return the number of matching characters, zero for no match + */ + public int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd) { + return Arrays.binarySearch(chars, buffer[pos]) >= 0 ? 1 : 0; + } + } + + //----------------------------------------------------------------------- + /** + * Class used to define a character for matching purposes. + */ + static final class CharMatcher extends StrMatcher { + /** The character to match. */ + private char ch; + + /** + * Constructor that creates a matcher that matches a single character. + * + * @param ch the character to match + */ + CharMatcher(char ch) { + super(); + this.ch = ch; + } + + /** + * Returns whether or not the given character matches. + * + * @param buffer the text content to match against, do not change + * @param pos the starting position for the match, valid for buffer + * @param bufferStart the first active index in the buffer, valid for buffer + * @param bufferEnd the end index of the active buffer, valid for buffer + * @return the number of matching characters, zero for no match + */ + public int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd) { + return ch == buffer[pos] ? 1 : 0; + } + } + + //----------------------------------------------------------------------- + /** + * Class used to define a set of characters for matching purposes. + */ + static final class StringMatcher extends StrMatcher { + /** The string to match, as a character array. */ + private char[] chars; + + /** + * Constructor that creates a matcher from a String. + * + * @param str the string to match, must not be null + */ + StringMatcher(String str) { + super(); + chars = str.toCharArray(); + } + + /** + * Returns whether or not the given text matches the stored string. + * + * @param buffer the text content to match against, do not change + * @param pos the starting position for the match, valid for buffer + * @param bufferStart the first active index in the buffer, valid for buffer + * @param bufferEnd the end index of the active buffer, valid for buffer + * @return the number of matching characters, zero for no match + */ + public int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd) { + int len = chars.length; + if (pos + len > bufferEnd) { + return 0; + } + for (int i = 0; i < chars.length; i++, pos++) { + if (chars[i] != buffer[pos]) { + return 0; + } + } + return len; + } + } + + //----------------------------------------------------------------------- + /** + * Class used to match no characters. + */ + static final class NoMatcher extends StrMatcher { + + /** + * Constructs a new instance of NoMatcher. + */ + NoMatcher() { + super(); + } + + /** + * Always returns false. + * + * @param buffer the text content to match against, do not change + * @param pos the starting position for the match, valid for buffer + * @param bufferStart the first active index in the buffer, valid for buffer + * @param bufferEnd the end index of the active buffer, valid for buffer + * @return the number of matching characters, zero for no match + */ + public int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd) { + return 0; + } + } + + //----------------------------------------------------------------------- + /** + * Class used to match whitespace as per trim(). + */ + static final class TrimMatcher extends StrMatcher { + + /** + * Constructs a new instance of TrimMatcher. + */ + TrimMatcher() { + super(); + } + + /** + * Returns whether or not the given charatcer matches. + * + * @param buffer the text content to match against, do not change + * @param pos the starting position for the match, valid for buffer + * @param bufferStart the first active index in the buffer, valid for buffer + * @param bufferEnd the end index of the active buffer, valid for buffer + * @return the number of matching characters, zero for no match + */ + public int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd) { + return buffer[pos] <= 32 ? 1 : 0; + } + } + +} diff --git a/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java b/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java new file mode 100644 index 000000000..7ec26b406 --- /dev/null +++ b/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java @@ -0,0 +1,1017 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang.text; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * Unit tests for {@link org.apache.commons.lang.text.StrBuilder}. + * + * @version $Id$ + */ +public class StrBuilderAppendInsertTest extends TestCase { + + /** Test subclass of Object, with a toString method. */ + private static Object FOO = new Object() { + public String toString() { + return "foo"; + } + }; + + /** + * Main method. + * + * @param args command line arguments, ignored + */ + public static void main(String[] args) { + TestRunner.run(suite()); + } + + /** + * Return a new test suite containing this test case. + * + * @return a new test suite containing this test case + */ + public static Test suite() { + TestSuite suite = new TestSuite(StrBuilderAppendInsertTest.class); + suite.setName("StrBuilder Tests"); + return suite; + } + + /** + * Create a new test case with the specified name. + * + * @param name the name + */ + public StrBuilderAppendInsertTest(String name) { + super(name); + } + + //----------------------------------------------------------------------- + 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); + assertEquals("", sb.toString()); + + sb.append(FOO); + assertEquals("foo", sb.toString()); + + sb.append((StringBuffer) null); + assertEquals("foo", sb.toString()); + + sb.append(new StringBuffer("baz")); + assertEquals("foobaz", sb.toString()); + + sb.append(new StrBuilder("yes")); + assertEquals("foobazyes", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_String() { + StrBuilder sb = new StrBuilder(); + + sb.append("foo"); + assertEquals("foo", sb.toString()); + + sb.append((String) null); + assertEquals("foo", sb.toString()); + + sb.append(""); + assertEquals("foo", sb.toString()); + + sb.append("bar"); + assertEquals("foobar", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_String_int_int() { + StrBuilder sb = new StrBuilder(); + + sb.append("foo", 0, 3); + assertEquals("foo", sb.toString()); + + sb.append((String) null, 0, 1); + assertEquals("foo", sb.toString()); + + try { + sb.append("bar", -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.append("bar", 3, 0); + assertEquals("foo", sb.toString()); + + sb.append("abcbardef", 3, 3); + assertEquals("foobar", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_StringBuffer() { + StrBuilder sb = new StrBuilder(); + + sb.append(new StringBuffer("foo")); + assertEquals("foo", sb.toString()); + + sb.append((StringBuffer) null); + assertEquals("foo", sb.toString()); + + sb.append(new StringBuffer("")); + assertEquals("foo", sb.toString()); + + sb.append(new StringBuffer("bar")); + assertEquals("foobar", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_StringBuffer_int_int() { + StrBuilder sb = new StrBuilder(); + + sb.append(new StringBuffer("foo"), 0, 3); + assertEquals("foo", sb.toString()); + + sb.append((StringBuffer) null, 0, 1); + assertEquals("foo", sb.toString()); + + try { + sb.append(new StringBuffer("bar"), -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.append(new StringBuffer("bar"), 3, 0); + assertEquals("foo", sb.toString()); + + sb.append(new StringBuffer("abcbardef"), 3, 3); + assertEquals("foobar", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_StrBuilder() { + StrBuilder sb = new StrBuilder(); + + sb.append(new StrBuilder("foo")); + assertEquals("foo", sb.toString()); + + sb.append((StrBuilder) null); + assertEquals("foo", sb.toString()); + + sb.append(new StrBuilder("")); + assertEquals("foo", sb.toString()); + + sb.append(new StrBuilder("bar")); + assertEquals("foobar", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_StrBuilder_int_int() { + StrBuilder sb = new StrBuilder(); + + sb.append(new StrBuilder("foo"), 0, 3); + assertEquals("foo", sb.toString()); + + sb.append((StrBuilder) null, 0, 1); + assertEquals("foo", sb.toString()); + + try { + sb.append(new StrBuilder("bar"), -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StrBuilder("bar"), 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StrBuilder("bar"), 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StrBuilder("bar"), 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StrBuilder("bar"), -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StrBuilder("bar"), 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.append(new StrBuilder("bar"), 3, 0); + assertEquals("foo", sb.toString()); + + sb.append(new StrBuilder("abcbardef"), 3, 3); + assertEquals("foobar", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_CharArray() { + StrBuilder sb = new StrBuilder(); + + sb.append((char[]) null); + assertEquals("", sb.toString()); + + sb.append(new char[0]); + assertEquals("", sb.toString()); + + sb.append(new char[]{'f', 'o', 'o'}); + assertEquals("foo", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppend_CharArray_int_int() { + StrBuilder sb = new StrBuilder(); + + sb.append(new char[]{'f', 'o', 'o'}, 0, 3); + assertEquals("foo", sb.toString()); + + sb.append((char[]) null, 0, 1); + assertEquals("foo", sb.toString()); + + try { + sb.append(new char[]{'b', 'a', 'r'}, -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + 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'}, -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("true", sb.toString()); + + sb.append(false); + assertEquals("truefalse", sb.toString()); + + sb.append('!'); + 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); + assertEquals("012.3", sb.toString()); + + sb.append(4.5d); + assertEquals("012.34.5", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppendPadding() { + StrBuilder sb = new StrBuilder(); + 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.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.clear(); + sb.appendFixedWidthPadLeft("foo", 0, '-'); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 1, '-'); + assertEquals("o", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 2, '-'); + assertEquals("oo", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 3, '-'); + assertEquals("foo", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 4, '-'); + assertEquals("-foo", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 10, '-'); + assertEquals(10, sb.length()); + // 1234567890 + assertEquals("-------foo", sb.toString()); + + sb.clear(); + sb.setNullText("null"); + sb.appendFixedWidthPadLeft(null, 5, '-'); + assertEquals("-null", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppendFixedWidthPadLeft_int() { + StrBuilder sb = new StrBuilder(); + sb.appendFixedWidthPadLeft(123, -1, '-'); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 0, '-'); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 1, '-'); + assertEquals("3", sb.toString()); + + 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.appendFixedWidthPadRight("foo", -1, '-'); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 0, '-'); + assertEquals("", sb.toString()); + + 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.clear(); + sb.appendFixedWidthPadRight("foo", 4, '-'); + assertEquals("foo-", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 10, '-'); + assertEquals(10, sb.length()); + // 1234567890 + assertEquals("foo-------", sb.toString()); + + sb.clear(); + sb.setNullText("null"); + sb.appendFixedWidthPadRight(null, 5, '-'); + assertEquals("null-", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppendFixedWidthPadRight_int() { + StrBuilder sb = new StrBuilder(); + sb.appendFixedWidthPadRight(123, -1, '-'); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 0, '-'); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 1, '-'); + assertEquals("1", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 2, '-'); + assertEquals("12", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 3, '-'); + assertEquals("123", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 4, '-'); + assertEquals("123-", sb.toString()); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 10, '-'); + assertEquals(10, sb.length()); + // 1234567890 + assertEquals("123-------", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppendWithSeparators_Array() { + StrBuilder sb = new StrBuilder(); + 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(); + sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, null); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); + assertEquals("foo,,baz", 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(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}), null); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); + assertEquals("foo,,baz", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppendWithSeparators_Iterator() { + StrBuilder sb = new StrBuilder(); + sb.appendWithSeparators((Iterator) null, ","); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(Collections.EMPTY_LIST.iterator(), ","); + assertEquals("", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator(), ","); + assertEquals("foo,bar,baz", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator(), null); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}).iterator(), ","); + assertEquals("foo,,baz", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testAppendWithSeparatorsWithNullText() { + StrBuilder sb = new StrBuilder(); + sb.setNullText("null"); + sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); + assertEquals("foo,null,baz", sb.toString()); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); + assertEquals("foo,null,baz", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testInsert() { + + StrBuilder sb = new StrBuilder(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, FOO); + fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, FOO); + fail("insert(7, Object) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (Object) null); + assertEquals("barbaz", sb.toString()); + + sb.insert(0, FOO); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, "foo"); + fail("insert(-1, String) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, "foo"); + fail("insert(7, String) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (String) null); + assertEquals("barbaz", sb.toString()); + + sb.insert(0, "foo"); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, new char[]{'f', 'o', 'o'}); + fail("insert(-1, char[]) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, new char[]{'f', 'o', 'o'}); + fail("insert(7, char[]) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (char[]) null); + assertEquals("barbaz", sb.toString()); + + sb.insert(0, new char[0]); + assertEquals("barbaz", sb.toString()); + + sb.insert(0, new char[]{'f', 'o', 'o'}); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); + fail("insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); + fail("insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (char[]) null, 0, 0); + assertEquals("barbaz", sb.toString()); + + sb.insert(0, new char[0], 0, 0); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3); + fail("insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3); + fail("insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1); + fail("insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10); + fail("insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0); + assertEquals("barbaz", sb.toString()); + + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); + assertEquals("foobarbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, true); + fail("insert(-1, boolean) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, true); + fail("insert(7, boolean) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, true); + assertEquals("truebarbaz", sb.toString()); + + sb.insert(0, false); + assertEquals("falsetruebarbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, '!'); + fail("insert(-1, char) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, '!'); + fail("insert(7, char) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, '!'); + assertEquals("!barbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, 0); + fail("insert(-1, int) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 0); + fail("insert(7, int) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, '0'); + assertEquals("0barbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, 1L); + fail("insert(-1, long) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 1L); + fail("insert(7, long) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, 1L); + assertEquals("1barbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, 2.3F); + fail("insert(-1, float) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 2.3F); + fail("insert(7, float) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, 2.3F); + assertEquals("2.3barbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, 4.5D); + fail("insert(-1, double) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 4.5D); + fail("insert(7, double) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, 4.5D); + assertEquals("4.5barbaz", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testInsertWithNullText() { + StrBuilder sb = new StrBuilder(); + sb.setNullText("null"); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, FOO); + fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, FOO); + fail("insert(7, Object) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (Object) null); + assertEquals("nullbarbaz", sb.toString()); + + sb.insert(0, FOO); + assertEquals("foonullbarbaz", sb.toString()); + + sb.clear(); + sb.append("barbaz"); + assertEquals("barbaz", sb.toString()); + + try { + sb.insert(-1, "foo"); + fail("insert(-1, String) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, "foo"); + fail("insert(7, String) expected StringIndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (String) null); + assertEquals("nullbarbaz", sb.toString()); + + sb.insert(0, "foo"); + assertEquals("foonullbarbaz", sb.toString()); + } + +} diff --git a/src/test/org/apache/commons/lang/text/StrBuilderTest.java b/src/test/org/apache/commons/lang/text/StrBuilderTest.java index 0474ed6cc..c09eb2f26 100644 --- a/src/test/org/apache/commons/lang/text/StrBuilderTest.java +++ b/src/test/org/apache/commons/lang/text/StrBuilderTest.java @@ -19,17 +19,14 @@ import java.io.Reader; import java.io.Writer; import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; - -import org.apache.commons.lang.ArrayUtils; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.textui.TestRunner; +import org.apache.commons.lang.ArrayUtils; + /** * Unit tests for {@link org.apache.commons.lang.text.StrBuilder}. * @@ -38,13 +35,6 @@ */ public class StrBuilderTest extends TestCase { - /** Test subclass of Object, with a toString method. */ - private static Object FOO = new Object() { - public String toString() { - return "foo"; - } - }; - /** * Main method. * @@ -118,62 +108,6 @@ public void testConstructors() { assertEquals(3, sb7.size()); } - public void testDeleteChar() { - StrBuilder sb = new StrBuilder("abc"); - sb.delete('X'); - assertEquals("abc",sb.toString()); - sb.delete('a'); - assertEquals("bc",sb.toString()); - sb.delete('c'); - assertEquals("b",sb.toString()); - sb.delete('b'); - assertEquals("",sb.toString()); - } - - public void testDeleteIntInt() { - StrBuilder sb = new StrBuilder("abc"); - sb.delete(0, 1); - assertEquals("bc",sb.toString()); - sb.delete(1, 2); - assertEquals("b",sb.toString()); - sb.delete(0, 1); - assertEquals("",sb.toString()); - sb.delete(0, 1); - assertEquals("",sb.toString()); - } - - public void testDeleteString() { - StrBuilder sb = new StrBuilder("abc"); - sb.delete(null); - assertEquals("abc",sb.toString()); - sb.delete(""); - assertEquals("abc",sb.toString()); - sb.delete("X"); - assertEquals("abc",sb.toString()); - sb.delete("a"); - assertEquals("bc",sb.toString()); - sb.delete("c"); - assertEquals("b",sb.toString()); - sb.delete("b"); - assertEquals("",sb.toString()); - } - - public void testDeleteCharAt() { - StrBuilder sb = new StrBuilder("abc"); - sb.deleteCharAt(0); - assertEquals("bc",sb.toString()); - } - - public void testDeleteCharAtExceptions() { - StrBuilder sb = new StrBuilder("abc"); - try { - sb.deleteCharAt(1000); - fail("Expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // Expected - } - } - //----------------------------------------------------------------------- public void testCapacityAndLength() { StrBuilder sb = new StrBuilder(); @@ -378,54 +312,7 @@ public void testCharAt() { } } - /** - * Tests {@link StrBuilder#replace(char, char)}. - */ - public void testReplaceCharChar() { - StrBuilder sb = new StrBuilder("abc"); - sb.replace('a', 'd'); - assertEquals("dbc", sb.toString()); - sb.replace('a', 'd'); - assertEquals("dbc", sb.toString()); - - sb = new StrBuilder("aabbcc"); - sb.replace('a', 'd'); - assertEquals("ddbbcc", sb.toString()); - sb.replace('a', 'd'); - assertEquals("ddbbcc", sb.toString()); - sb.replace('d', 'd'); - assertEquals("ddbbcc", sb.toString()); - } - - /** - * Tests {@link StrBuilder#replace(String, String)}. - */ - public void testReplaceStringString() { - StrBuilder sb = new StrBuilder("abc"); - sb.replace("a", "d"); - assertEquals("dbc", sb.toString()); - sb.replace("a", "d"); - assertEquals("dbc", sb.toString()); - - sb = new StrBuilder("aabbcc"); - sb.replace("a", "d"); - assertEquals("ddbbcc", sb.toString()); - sb.replace("a", "d"); - assertEquals("ddbbcc", sb.toString()); - } - - public void testReplaceIntIntStrBuilder() { - StrBuilder sb = new StrBuilder("abc"); - sb.replace(0, 1, new StrBuilder ("d")); - assertEquals("dbc", sb.toString()); - sb.replace(0, 1, new StrBuilder ("aaa")); - assertEquals("aaabc", sb.toString()); - - sb = new StrBuilder("aabbcc"); - sb.replace(0, 2, new StrBuilder("d")); - assertEquals("dbbcc", sb.toString()); - } - + //----------------------------------------------------------------------- public void testSetCharAt() { StrBuilder sb = new StrBuilder(); try { @@ -453,1041 +340,67 @@ public void testSetCharAt() { assertEquals("bar", sb.toString()); } - public void testStartsWith() { - this.testStartsWith(new StrBuilder()); - this.testStartsWith(new StrBuilder("")); - this.testStartsWith(new StrBuilder(null)); - } - - void testStartsWith(StrBuilder sb ) { - assertFalse(sb.startsWith("a")); - assertFalse(sb.startsWith(null)); - assertTrue(sb.startsWith("")); - sb.append("abc"); - assertTrue(sb.startsWith("a")); - assertTrue(sb.startsWith("ab")); - assertTrue(sb.startsWith("abc")); - assertFalse(sb.startsWith("cba")); - } - - public void testEndsWith() { - this.testEndsWith(new StrBuilder()); - this.testEndsWith(new StrBuilder("")); - this.testEndsWith(new StrBuilder(null)); - } - - void testEndsWith(StrBuilder sb) { - assertFalse(sb.endsWith("a")); - assertFalse(sb.endsWith("c")); - assertTrue(sb.endsWith("")); - assertFalse(sb.endsWith(null)); - sb.append("abc"); - assertTrue(sb.endsWith("c")); - assertTrue(sb.endsWith("bc")); - assertTrue(sb.endsWith("abc")); - assertFalse(sb.endsWith("cba")); - assertFalse(sb.endsWith("abcd")); - assertFalse(sb.endsWith(" abc")); - assertFalse(sb.endsWith("abc ")); - } - //----------------------------------------------------------------------- - 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); - assertEquals("", sb.toString()); - - sb.append(FOO); - assertEquals("foo", sb.toString()); - - sb.append((StringBuffer) null); - assertEquals("foo", sb.toString()); - - sb.append(new StringBuffer("baz")); - assertEquals("foobaz", sb.toString()); - - sb.append(new StrBuilder("yes")); - assertEquals("foobazyes", sb.toString()); - } - - public void testAppend_String() { - StrBuilder sb = new StrBuilder(); - - sb.append("foo"); - assertEquals("foo", sb.toString()); - - sb.append((String) null); - assertEquals("foo", sb.toString()); - - sb.append(""); - assertEquals("foo", sb.toString()); - - sb.append("bar"); - assertEquals("foobar", sb.toString()); - } - - public void testAppend_String_int_int() { - StrBuilder sb = new StrBuilder(); + public void testDeleteCharAt() { + StrBuilder sb = new StrBuilder("abc"); + sb.deleteCharAt(0); + assertEquals("bc", sb.toString()); - sb.append("foo", 0, 3); - assertEquals("foo", sb.toString()); - - sb.append((String) null, 0, 1); - assertEquals("foo", sb.toString()); - try { - sb.append("bar", -1, 1); - fail("append(char[], -1,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append("bar", 3, 1); - fail("append(char[], 3,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append("bar", 1, -1); - fail("append(char[],, -1) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append("bar", 1, 3); - fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append("bar", -1, 3); - fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append("bar", 4, 0); - fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.append("bar", 3, 0); - assertEquals("foo", sb.toString()); - - sb.append("abcbardef", 3, 3); - assertEquals("foobar", sb.toString()); - } - - public void testAppend_StringBuffer() { - StrBuilder sb = new StrBuilder(); - - sb.append(new StringBuffer("foo")); - assertEquals("foo", sb.toString()); - - sb.append((StringBuffer) null); - assertEquals("foo", sb.toString()); - - sb.append(new StringBuffer("")); - assertEquals("foo", sb.toString()); - - sb.append(new StringBuffer("bar")); - assertEquals("foobar", sb.toString()); - } - - public void testAppend_StringBuffer_int_int() { - StrBuilder sb = new StrBuilder(); - - sb.append(new StringBuffer("foo"), 0, 3); - assertEquals("foo", sb.toString()); - - sb.append((StringBuffer) null, 0, 1); - assertEquals("foo", sb.toString()); - - try { - sb.append(new StringBuffer("bar"), -1, 1); - fail("append(char[], -1,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StringBuffer("bar"), 3, 1); - fail("append(char[], 3,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StringBuffer("bar"), 1, -1); - fail("append(char[],, -1) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StringBuffer("bar"), 1, 3); - fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StringBuffer("bar"), -1, 3); - fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StringBuffer("bar"), 4, 0); - fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.append(new StringBuffer("bar"), 3, 0); - assertEquals("foo", sb.toString()); - - sb.append(new StringBuffer("abcbardef"), 3, 3); - assertEquals("foobar", sb.toString()); - } - - public void testAppend_StrBuilder() { - StrBuilder sb = new StrBuilder(); - - sb.append(new StrBuilder("foo")); - assertEquals("foo", sb.toString()); - - sb.append((StrBuilder) null); - assertEquals("foo", sb.toString()); - - sb.append(new StrBuilder("")); - assertEquals("foo", sb.toString()); - - sb.append(new StrBuilder("bar")); - assertEquals("foobar", sb.toString()); - } - - public void testAppend_StrBuilder_int_int() { - StrBuilder sb = new StrBuilder(); - - sb.append(new StrBuilder("foo"), 0, 3); - assertEquals("foo", sb.toString()); - - sb.append((StrBuilder) null, 0, 1); - assertEquals("foo", sb.toString()); - - try { - sb.append(new StrBuilder("bar"), -1, 1); - fail("append(char[], -1,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StrBuilder("bar"), 3, 1); - fail("append(char[], 3,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StrBuilder("bar"), 1, -1); - fail("append(char[],, -1) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StrBuilder("bar"), 1, 3); - fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StrBuilder("bar"), -1, 3); - fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new StrBuilder("bar"), 4, 0); - fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.append(new StrBuilder("bar"), 3, 0); - assertEquals("foo", sb.toString()); - - sb.append(new StrBuilder("abcbardef"), 3, 3); - assertEquals("foobar", sb.toString()); - } - - public void testAppend_CharArray() { - StrBuilder sb = new StrBuilder(); - - sb.append((char[]) null); - assertEquals("", sb.toString()); - - sb.append(new char[0]); - assertEquals("", sb.toString()); - - sb.append(new char[]{'f', 'o', 'o'}); - assertEquals("foo", sb.toString()); - } - - public void testAppend_CharArray_int_int() { - StrBuilder sb = new StrBuilder(); - - sb.append(new char[]{'f', 'o', 'o'}, 0, 3); - assertEquals("foo", sb.toString()); - - sb.append((char[]) null, 0, 1); - assertEquals("foo", sb.toString()); - - try { - sb.append(new char[]{'b', 'a', 'r'}, -1, 1); - fail("append(char[], -1,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new char[]{'b', 'a', 'r'}, 3, 1); - fail("append(char[], 3,) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.append(new char[]{'b', 'a', 'r'}, 1, -1); - fail("append(char[],, -1) expected IndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - 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'}, -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("true", sb.toString()); - - sb.append(false); - assertEquals("truefalse", sb.toString()); - - sb.append('!'); - 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); - assertEquals("012.3", sb.toString()); - - sb.append(4.5d); - assertEquals("012.34.5", sb.toString()); + sb.deleteCharAt(1000); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} } //----------------------------------------------------------------------- - public void testAppendPadding() { + public void testToCharArray() { StrBuilder sb = new StrBuilder(); - 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.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.clear(); - sb.appendFixedWidthPadLeft("foo", 0, '-'); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft("foo", 1, '-'); - assertEquals("o", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft("foo", 2, '-'); - assertEquals("oo", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft("foo", 3, '-'); - assertEquals("foo", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft("foo", 4, '-'); - assertEquals("-foo", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft("foo", 10, '-'); - assertEquals(10, sb.length()); - // 1234567890 - assertEquals("-------foo", sb.toString()); - - sb.clear(); - sb.setNullText("null"); - sb.appendFixedWidthPadLeft(null, 5, '-'); - assertEquals("-null", sb.toString()); - } - - public void testAppendFixedWidthPadLeft_int() { - StrBuilder sb = new StrBuilder(); - sb.appendFixedWidthPadLeft(123, -1, '-'); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft(123, 0, '-'); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadLeft(123, 1, '-'); - assertEquals("3", sb.toString()); - - 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.appendFixedWidthPadRight("foo", -1, '-'); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight("foo", 0, '-'); - assertEquals("", sb.toString()); - - 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.clear(); - sb.appendFixedWidthPadRight("foo", 4, '-'); - assertEquals("foo-", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight("foo", 10, '-'); - assertEquals(10, sb.length()); - // 1234567890 - assertEquals("foo-------", sb.toString()); - - sb.clear(); - sb.setNullText("null"); - sb.appendFixedWidthPadRight(null, 5, '-'); - assertEquals("null-", sb.toString()); - } - - public void testAppendFixedWidthPadRight_int() { - StrBuilder sb = new StrBuilder(); - sb.appendFixedWidthPadRight(123, -1, '-'); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight(123, 0, '-'); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight(123, 1, '-'); - assertEquals("1", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight(123, 2, '-'); - assertEquals("12", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight(123, 3, '-'); - assertEquals("123", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight(123, 4, '-'); - assertEquals("123-", sb.toString()); - - sb.clear(); - sb.appendFixedWidthPadRight(123, 10, '-'); - assertEquals(10, sb.length()); - // 1234567890 - assertEquals("123-------", sb.toString()); - } - - //----------------------------------------------------------------------- - public void testAppendWithSeparators_Array() { - StrBuilder sb = new StrBuilder(); - 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(); - sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, null); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); - assertEquals("foo,,baz", 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(); - sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}), null); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); - assertEquals("foo,,baz", sb.toString()); - } - - public void testAppendWithSeparators_Iterator() { - StrBuilder sb = new StrBuilder(); - sb.appendWithSeparators((Iterator) null, ","); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(Collections.EMPTY_LIST.iterator(), ","); - assertEquals("", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator(), ","); - assertEquals("foo,bar,baz", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator(), null); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}).iterator(), ","); - assertEquals("foo,,baz", sb.toString()); - } - - public void testAppendWithSeparatorsWithNullText() { - StrBuilder sb = new StrBuilder(); - sb.setNullText("null"); - sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); - assertEquals("foo,null,baz", sb.toString()); - - sb.clear(); - sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); - assertEquals("foo,null,baz", sb.toString()); - } - - public void testInsert() { - - StrBuilder sb = new StrBuilder(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, FOO); - fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, FOO); - fail("insert(7, Object) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, (Object) null); - assertEquals("barbaz", sb.toString()); - - sb.insert(0, FOO); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, "foo"); - fail("insert(-1, String) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, "foo"); - fail("insert(7, String) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, (String) null); - assertEquals("barbaz", sb.toString()); - - sb.insert(0, "foo"); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, new char[]{'f', 'o', 'o'}); - fail("insert(-1, char[]) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, new char[]{'f', 'o', 'o'}); - fail("insert(7, char[]) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, (char[]) null); - assertEquals("barbaz", sb.toString()); - - sb.insert(0, new char[0]); - assertEquals("barbaz", sb.toString()); - - sb.insert(0, new char[]{'f', 'o', 'o'}); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); - fail("insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); - fail("insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, (char[]) null, 0, 0); - assertEquals("barbaz", sb.toString()); - - sb.insert(0, new char[0], 0, 0); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3); - fail("insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3); - fail("insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1); - fail("insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10); - fail("insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0); - assertEquals("barbaz", sb.toString()); - - sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); - assertEquals("foobarbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, true); - fail("insert(-1, boolean) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, true); - fail("insert(7, boolean) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, true); - assertEquals("truebarbaz", sb.toString()); - - sb.insert(0, false); - assertEquals("falsetruebarbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, '!'); - fail("insert(-1, char) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, '!'); - fail("insert(7, char) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, '!'); - assertEquals("!barbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, 0); - fail("insert(-1, int) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, 0); - fail("insert(7, int) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, '0'); - assertEquals("0barbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, 1L); - fail("insert(-1, long) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, 1L); - fail("insert(7, long) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, 1L); - assertEquals("1barbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, 2.3F); - fail("insert(-1, float) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, 2.3F); - fail("insert(7, float) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, 2.3F); - assertEquals("2.3barbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, 4.5D); - fail("insert(-1, double) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, 4.5D); - fail("insert(7, double) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, 4.5D); - assertEquals("4.5barbaz", sb.toString()); - } - - public void testInsertWithNullText() { - - StrBuilder sb = new StrBuilder(); - sb.setNullText("null"); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, FOO); - fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, FOO); - fail("insert(7, Object) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, (Object) null); - assertEquals("nullbarbaz", sb.toString()); - - sb.insert(0, FOO); - assertEquals("foonullbarbaz", sb.toString()); - - sb.clear(); - sb.append("barbaz"); - assertEquals("barbaz", sb.toString()); - - try { - sb.insert(-1, "foo"); - fail("insert(-1, String) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - try { - sb.insert(7, "foo"); - fail("insert(7, String) expected StringIndexOutOfBoundsException"); - } catch (IndexOutOfBoundsException e) { - // expected - } - - sb.insert(0, (String) null); - assertEquals("nullbarbaz", sb.toString()); - - sb.insert(0, "foo"); - assertEquals("foonullbarbaz", sb.toString()); - } - - public void testToCharArray ( ) { - - StrBuilder sb = new StrBuilder(); assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray()); - + char[] a = sb.toCharArray(); - assertNotNull ("toCharArray() result is null", a); - assertEquals ("toCharArray() result is too large", 0, a.length); - + 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)); + 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(); assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray(0, 0)); 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)); - + 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)); - + 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); - + 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"); + fail("no string index out of bound on -1"); + } catch (IndexOutOfBoundsException e) { } - catch (IndexOutOfBoundsException e) {} try { sb.toCharArray(6, 5); - fail ("no string index out of bound on -1"); + fail("no string index out of bound on -1"); + } catch (IndexOutOfBoundsException e) { } - catch (IndexOutOfBoundsException e) {} } - + public void testGetChars ( ) { - StrBuilder sb = new StrBuilder(); char[] input = new char[10]; @@ -1513,9 +426,8 @@ public void testGetChars ( ) { a = sb.getChars(input); assertNotSame(input, a); } - + public void testGetCharsIntIntCharArrayInt( ) { - StrBuilder sb = new StrBuilder(); sb.append("junit"); @@ -1555,248 +467,582 @@ public void testGetCharsIntIntCharArrayInt( ) { catch (IndexOutOfBoundsException e) { } } - - public void testAppendStringBuffer() { + + //----------------------------------------------------------------------- + public void testNullText() { StrBuilder sb = new StrBuilder(); - - sb = sb.append(new StringBuffer()); - assertNotNull(sb); - - sb = sb.append(new StringBuffer("junit")); - - assertEquals ("junit", sb.toString()); + 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 testAppendStrBuilder() { - StrBuilder sb = new StrBuilder(); + + //----------------------------------------------------------------------- + public void testDeleteIntInt() { + StrBuilder sb = new StrBuilder("abc"); + sb.delete(0, 1); + assertEquals("bc", sb.toString()); + sb.delete(1, 2); + assertEquals("b", sb.toString()); + sb.delete(0, 1); + assertEquals("", sb.toString()); + sb.delete(0, 1000); + assertEquals("", sb.toString()); - sb = sb.append((StrBuilder)null); - assertNotNull(sb); + try { + sb.delete(1, 2); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} + try { + sb.delete(-1, 1); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} - sb = sb.append(new StrBuilder()); - assertNotNull(sb); + sb = new StrBuilder("anything"); + try { + sb.delete(2, 1); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} + } + + //----------------------------------------------------------------------- + public void testDeleteAll_char() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.deleteAll('X'); + assertEquals("abcbccba", sb.toString()); + sb.deleteAll('a'); + assertEquals("bcbccb", sb.toString()); + sb.deleteAll('c'); + assertEquals("bbb", sb.toString()); + sb.deleteAll('b'); + assertEquals("", sb.toString()); + + sb = new StrBuilder(""); + sb.deleteAll('b'); assertEquals("", sb.toString()); } - - public void testStringBuffer() { - StrBuilder sb = new StrBuilder(); - assertEquals (new StringBuffer().toString(), sb.toStringBuffer().toString()); - - sb.append("junit"); - assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString()); + + public void testDeleteFirst_char() { + StrBuilder sb = new StrBuilder("abcba"); + sb.deleteFirst('X'); + assertEquals("abcba", sb.toString()); + sb.deleteFirst('a'); + assertEquals("bcba", sb.toString()); + sb.deleteFirst('c'); + assertEquals("bba", sb.toString()); + sb.deleteFirst('b'); + assertEquals("ba", sb.toString()); + + sb = new StrBuilder(""); + sb.deleteFirst('b'); + assertEquals("", sb.toString()); } - + + // ----------------------------------------------------------------------- + public void testDeleteAll_String() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.deleteAll((String) null); + assertEquals("abcbccba", sb.toString()); + sb.deleteAll(""); + assertEquals("abcbccba", sb.toString()); + + sb.deleteAll("X"); + assertEquals("abcbccba", sb.toString()); + sb.deleteAll("a"); + assertEquals("bcbccb", sb.toString()); + sb.deleteAll("c"); + assertEquals("bbb", sb.toString()); + sb.deleteAll("b"); + assertEquals("", sb.toString()); + + sb = new StrBuilder("abcbccba"); + sb.deleteAll("bc"); + assertEquals("acba", sb.toString()); + + sb = new StrBuilder(""); + sb.deleteAll("bc"); + assertEquals("", sb.toString()); + } + + public void testDeleteFirst_String() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.deleteFirst((String) null); + assertEquals("abcbccba", sb.toString()); + sb.deleteFirst(""); + assertEquals("abcbccba", sb.toString()); + + sb.deleteFirst("X"); + assertEquals("abcbccba", sb.toString()); + sb.deleteFirst("a"); + assertEquals("bcbccba", sb.toString()); + sb.deleteFirst("c"); + assertEquals("bbccba", sb.toString()); + sb.deleteFirst("b"); + assertEquals("bccba", sb.toString()); + + sb = new StrBuilder("abcbccba"); + sb.deleteFirst("bc"); + assertEquals("abccba", sb.toString()); + + sb = new StrBuilder(""); + sb.deleteFirst("bc"); + assertEquals("", sb.toString()); + } + + // ----------------------------------------------------------------------- + public void testDeleteAll_StrMatcher() { + StrBuilder sb = new StrBuilder("A0xA1A2yA3"); + sb.deleteAll((StrMatcher) null); + assertEquals("A0xA1A2yA3", sb.toString()); + sb.deleteAll(A_NUMBER_MATCHER); + assertEquals("xy", sb.toString()); + + sb = new StrBuilder("Ax1"); + sb.deleteAll(A_NUMBER_MATCHER); + assertEquals("Ax1", sb.toString()); + + sb = new StrBuilder(""); + sb.deleteAll(A_NUMBER_MATCHER); + assertEquals("", sb.toString()); + } + + public void testDeleteFirst_StrMatcher() { + StrBuilder sb = new StrBuilder("A0xA1A2yA3"); + sb.deleteFirst((StrMatcher) null); + assertEquals("A0xA1A2yA3", sb.toString()); + sb.deleteFirst(A_NUMBER_MATCHER); + assertEquals("xA1A2yA3", sb.toString()); + + sb = new StrBuilder("Ax1"); + sb.deleteFirst(A_NUMBER_MATCHER); + assertEquals("Ax1", sb.toString()); + + sb = new StrBuilder(""); + sb.deleteFirst(A_NUMBER_MATCHER); + assertEquals("", sb.toString()); + } + + // ----------------------------------------------------------------------- + public void testReplace_int_int_String() { + StrBuilder sb = new StrBuilder("abc"); + sb.replace(0, 1, "d"); + assertEquals("dbc", sb.toString()); + sb.replace(0, 1, "aaa"); + assertEquals("aaabc", sb.toString()); + sb.replace(0, 3, ""); + assertEquals("bc", sb.toString()); + sb.replace(1, 2, (String) null); + assertEquals("b", sb.toString()); + sb.replace(1, 1000, "text"); + assertEquals("btext", sb.toString()); + sb.replace(0, 1000, "text"); + assertEquals("text", sb.toString()); + + sb = new StrBuilder("atext"); + sb.replace(1, 1, "ny"); + assertEquals("anytext", sb.toString()); + try { + sb.replace(2, 1, "anything"); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} + + sb = new StrBuilder(); + try { + sb.replace(1, 2, "anything"); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} + try { + sb.replace(-1, 1, "anything"); + fail("Expected IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) {} + } + + //----------------------------------------------------------------------- + public void testReplaceAll_char_char() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replaceAll('x', 'y'); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll('a', 'd'); + assertEquals("dbcbccbd", sb.toString()); + sb.replaceAll('b', 'e'); + assertEquals("dececced", sb.toString()); + sb.replaceAll('c', 'f'); + assertEquals("defeffed", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testReplaceFirst_char_char() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replaceFirst('x', 'y'); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst('a', 'd'); + assertEquals("dbcbccba", sb.toString()); + sb.replaceFirst('b', 'e'); + assertEquals("decbccba", sb.toString()); + sb.replaceFirst('c', 'f'); + assertEquals("defbccba", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testReplaceAll_String_String() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replaceAll((String) null, null); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll((String) null, "anything"); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll("", null); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll("", "anything"); + assertEquals("abcbccba", sb.toString()); + + sb.replaceAll("x", "y"); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll("a", "d"); + assertEquals("dbcbccbd", sb.toString()); + sb.replaceAll("d", null); + assertEquals("bcbccb", sb.toString()); + sb.replaceAll("cb", "-"); + assertEquals("b-c-", sb.toString()); + + sb = new StrBuilder("abcba"); + sb.replaceAll("b", "xbx"); + assertEquals("axbxcxbxa", sb.toString()); + + sb = new StrBuilder("bb"); + sb.replaceAll("b", "xbx"); + assertEquals("xbxxbx", sb.toString()); + } + + public void testReplaceFirst_String_String() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replaceFirst((String) null, null); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst((String) null, "anything"); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst("", null); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst("", "anything"); + assertEquals("abcbccba", sb.toString()); + + sb.replaceFirst("x", "y"); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst("a", "d"); + assertEquals("dbcbccba", sb.toString()); + sb.replaceFirst("d", null); + assertEquals("bcbccba", sb.toString()); + sb.replaceFirst("cb", "-"); + assertEquals("b-ccba", sb.toString()); + + sb = new StrBuilder("abcba"); + sb.replaceFirst("b", "xbx"); + assertEquals("axbxcba", sb.toString()); + + sb = new StrBuilder("bb"); + sb.replaceFirst("b", "xbx"); + assertEquals("xbxb", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testReplaceAll_StrMatcher_String() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replaceAll((StrMatcher) null, null); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll((StrMatcher) null, "anything"); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll(StrMatcher.noneMatcher(), null); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll(StrMatcher.noneMatcher(), "anything"); + assertEquals("abcbccba", sb.toString()); + + sb.replaceAll(StrMatcher.charMatcher('x'), "y"); + assertEquals("abcbccba", sb.toString()); + sb.replaceAll(StrMatcher.charMatcher('a'), "d"); + assertEquals("dbcbccbd", sb.toString()); + sb.replaceAll(StrMatcher.charMatcher('d'), null); + assertEquals("bcbccb", sb.toString()); + sb.replaceAll(StrMatcher.stringMatcher("cb"), "-"); + assertEquals("b-c-", sb.toString()); + + sb = new StrBuilder("abcba"); + sb.replaceAll(StrMatcher.charMatcher('b'), "xbx"); + assertEquals("axbxcxbxa", sb.toString()); + + sb = new StrBuilder("bb"); + sb.replaceAll(StrMatcher.charMatcher('b'), "xbx"); + assertEquals("xbxxbx", sb.toString()); + + sb = new StrBuilder("A1-A2A3-A4"); + sb.replaceAll(A_NUMBER_MATCHER, "***"); + assertEquals("***-******-***", sb.toString()); + } + + public void testReplaceFirst_StrMatcher_String() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replaceFirst((StrMatcher) null, null); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst((StrMatcher) null, "anything"); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst(StrMatcher.noneMatcher(), null); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst(StrMatcher.noneMatcher(), "anything"); + assertEquals("abcbccba", sb.toString()); + + sb.replaceFirst(StrMatcher.charMatcher('x'), "y"); + assertEquals("abcbccba", sb.toString()); + sb.replaceFirst(StrMatcher.charMatcher('a'), "d"); + assertEquals("dbcbccba", sb.toString()); + sb.replaceFirst(StrMatcher.charMatcher('d'), null); + assertEquals("bcbccba", sb.toString()); + sb.replaceFirst(StrMatcher.stringMatcher("cb"), "-"); + assertEquals("b-ccba", sb.toString()); + + sb = new StrBuilder("abcba"); + sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx"); + assertEquals("axbxcba", sb.toString()); + + sb = new StrBuilder("bb"); + sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx"); + assertEquals("xbxb", sb.toString()); + + sb = new StrBuilder("A1-A2A3-A4"); + sb.replaceFirst(A_NUMBER_MATCHER, "***"); + assertEquals("***-A2A3-A4", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replace((StrMatcher) null, "x", 0, sb.length(), -1); + assertEquals("abcbccba", sb.toString()); + + sb.replace(StrMatcher.charMatcher('a'), "x", 0, sb.length(), -1); + assertEquals("xbcbccbx", sb.toString()); + + sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(), -1); + assertEquals("xbxcxx", sb.toString()); + + sb = new StrBuilder("A1-A2A3-A4"); + sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); + assertEquals("***-******-***", sb.toString()); + + sb = new StrBuilder(); + sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); + assertEquals("", sb.toString()); + } + + public void testReplace_StrMatcher_String_int_int_int_VaryReplace() { + StrBuilder sb = new StrBuilder("abcbccba"); + sb.replace(StrMatcher.stringMatcher("cb"), "cb", 0, sb.length(), -1); + assertEquals("abcbccba", sb.toString()); + + sb = new StrBuilder("abcbccba"); + sb.replace(StrMatcher.stringMatcher("cb"), "-", 0, sb.length(), -1); + assertEquals("ab-c-a", sb.toString()); + + sb = new StrBuilder("abcbccba"); + sb.replace(StrMatcher.stringMatcher("cb"), "+++", 0, sb.length(), -1); + assertEquals("ab+++c+++a", sb.toString()); + + sb = new StrBuilder("abcbccba"); + sb.replace(StrMatcher.stringMatcher("cb"), "", 0, sb.length(), -1); + assertEquals("abca", sb.toString()); + + sb = new StrBuilder("abcbccba"); + sb.replace(StrMatcher.stringMatcher("cb"), null, 0, sb.length(), -1); + assertEquals("abca", sb.toString()); + } + + public void testReplace_StrMatcher_String_int_int_int_VaryStartIndex() { + StrBuilder sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, sb.length(), -1); + assertEquals("-x--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 1, sb.length(), -1); + assertEquals("aax--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, sb.length(), -1); + assertEquals("aax--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 3, sb.length(), -1); + assertEquals("aax--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 4, sb.length(), -1); + assertEquals("aaxa-ay-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 5, sb.length(), -1); + assertEquals("aaxaa-y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 6, sb.length(), -1); + assertEquals("aaxaaaay-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 7, sb.length(), -1); + assertEquals("aaxaaaay-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 8, sb.length(), -1); + assertEquals("aaxaaaay-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 9, sb.length(), -1); + assertEquals("aaxaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 10, sb.length(), -1); + assertEquals("aaxaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + try { + sb.replace(StrMatcher.stringMatcher("aa"), "-", 11, sb.length(), -1); + fail(); + } catch (IndexOutOfBoundsException ex) {} + assertEquals("aaxaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + try { + sb.replace(StrMatcher.stringMatcher("aa"), "-", -1, sb.length(), -1); + fail(); + } catch (IndexOutOfBoundsException ex) {} + assertEquals("aaxaaaayaa", sb.toString()); + } + + public void testReplace_StrMatcher_String_int_int_int_VaryEndIndex() { + StrBuilder sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 0, -1); + assertEquals("aaxaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 2, -1); + assertEquals("-xaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 3, -1); + assertEquals("-xaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 4, -1); + assertEquals("-xaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 5, -1); + assertEquals("-x-aayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 6, -1); + assertEquals("-x-aayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 7, -1); + assertEquals("-x--yaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 8, -1); + assertEquals("-x--yaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 9, -1); + assertEquals("-x--yaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1); + assertEquals("-x--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 1000, -1); + assertEquals("-x--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + try { + sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, 1, -1); + fail(); + } catch (IndexOutOfBoundsException ex) {} + assertEquals("aaxaaaayaa", sb.toString()); + } + + public void testReplace_StrMatcher_String_int_int_int_VaryCount() { + StrBuilder sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1); + assertEquals("-x--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 0); + assertEquals("aaxaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 1); + assertEquals("-xaaaayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 2); + assertEquals("-x-aayaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 3); + assertEquals("-x--yaa", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 4); + assertEquals("-x--y-", sb.toString()); + + sb = new StrBuilder("aaxaaaayaa"); + sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 5); + assertEquals("-x--y-", sb.toString()); + } + + //----------------------------------------------------------------------- public void testReverse() { StrBuilder sb = new StrBuilder(); - - String actual = sb.reverse().toString(); - assertEquals ("", actual); - + assertEquals("", sb.reverse().toString()); sb.append(true); - actual = sb.reverse().toString(); - assertEquals("eurt", actual); - - actual = sb.reverse().toString(); - assertEquals("true", actual); + assertEquals("eurt", sb.reverse().toString()); + assertEquals("true", sb.reverse().toString()); } - - public void testIndexOfChar() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals (0, sb.indexOf('a')); - //should work like String#indexOf - assertEquals ("abab".indexOf('a'), sb.indexOf('a')); - - assertEquals(1, sb.indexOf('b')); - assertEquals ("abab".indexOf('b'), sb.indexOf('b')); - - assertEquals (-1, sb.indexOf('z')); + + //----------------------------------------------------------------------- + public void testStartsWith() { + StrBuilder sb = new StrBuilder(); + assertFalse(sb.startsWith("a")); + assertFalse(sb.startsWith(null)); + assertTrue(sb.startsWith("")); + sb.append("abc"); + assertTrue(sb.startsWith("a")); + assertTrue(sb.startsWith("ab")); + assertTrue(sb.startsWith("abc")); + assertFalse(sb.startsWith("cba")); } - - public void testLastIndexOfChar() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals (2, sb.lastIndexOf('a')); - //should work like String#lastIndexOf - assertEquals ("abab".lastIndexOf('a'), sb.lastIndexOf('a')); - - assertEquals(3, sb.lastIndexOf('b')); - assertEquals ("abab".lastIndexOf('b'), sb.lastIndexOf('b')); - - assertEquals (-1, sb.lastIndexOf('z')); + + public void testEndsWith() { + StrBuilder sb = new StrBuilder(); + assertFalse(sb.endsWith("a")); + assertFalse(sb.endsWith("c")); + assertTrue(sb.endsWith("")); + assertFalse(sb.endsWith(null)); + sb.append("abc"); + assertTrue(sb.endsWith("c")); + assertTrue(sb.endsWith("bc")); + assertTrue(sb.endsWith("abc")); + assertFalse(sb.endsWith("cba")); + assertFalse(sb.endsWith("abcd")); + assertFalse(sb.endsWith(" abc")); + assertFalse(sb.endsWith("abc ")); } - - public void testIndexOfCharInt() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals (2, sb.indexOf('a', 1)); - //should work like String#indexOf - assertEquals ("abab".indexOf('a', 1), sb.indexOf('a', 1)); - - assertEquals(3, sb.indexOf('b', 2)); - assertEquals ("abab".indexOf('b', 2), sb.indexOf('b', 2)); - - assertEquals (-1, sb.indexOf('z', 2)); - - sb = new StrBuilder("xyzabc"); - assertEquals (2, sb.indexOf('z', 0)); - assertEquals (-1, sb.indexOf('z', 3)); - } - - public void testLastIndexOfCharInt() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals (0, sb.lastIndexOf('a', 1)); - //should work like String#lastIndexOf - assertEquals ("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1)); - - assertEquals(1, sb.lastIndexOf('b', 2)); - assertEquals ("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2)); - - assertEquals (-1, sb.lastIndexOf('z', 2)); - - sb = new StrBuilder("xyzabc"); - assertEquals (2, sb.lastIndexOf('z', sb.length())); - assertEquals (-1, sb.lastIndexOf('z', 1)); - } - - public void testIndexOfString() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals(0, sb.indexOf("a")); - //should work like String#indexOf - assertEquals("abab".indexOf("a"), sb.indexOf("a")); - - assertEquals(0, sb.indexOf("ab")); - //should work like String#indexOf - assertEquals("abab".indexOf("ab"), sb.indexOf("ab")); - - assertEquals(1, sb.indexOf("b")); - assertEquals("abab".indexOf("b"), sb.indexOf("b")); - - assertEquals(1, sb.indexOf("ba")); - assertEquals("abab".indexOf("ba"), sb.indexOf("ba")); - - assertEquals(-1, sb.indexOf("z")); - - assertEquals(-1, sb.indexOf((String) null)); - } - - public void testLastIndexOfString() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals(2, sb.lastIndexOf("a")); - //should work like String#lastIndexOf - assertEquals("abab".lastIndexOf("a"), sb.lastIndexOf("a")); - - assertEquals(2, sb.lastIndexOf("ab")); - //should work like String#lastIndexOf - assertEquals("abab".lastIndexOf("ab"), sb.lastIndexOf("ab")); - - assertEquals(3, sb.lastIndexOf("b")); - assertEquals("abab".lastIndexOf("b"), sb.lastIndexOf("b")); - - assertEquals(1, sb.lastIndexOf("ba")); - assertEquals("abab".lastIndexOf("ba"), sb.lastIndexOf("ba")); - - assertEquals(-1, sb.lastIndexOf("z")); - - assertEquals(-1, sb.lastIndexOf((String) null)); - } - - public void testIndexOfStringInt() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals(2, sb.indexOf("a", 1)); - //should work like String#indexOf - assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1)); - - assertEquals(2, sb.indexOf("ab", 1)); - //should work like String#indexOf - assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1)); - - assertEquals(3, sb.indexOf("b", 2)); - assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2)); - - assertEquals(1, sb.indexOf("ba", 1)); - assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2)); - - assertEquals(-1, sb.indexOf("z", 2)); - - sb = new StrBuilder("xyzabc"); - assertEquals(2, sb.indexOf("za", 0)); - assertEquals(-1, sb.indexOf("za", 3)); - - assertEquals(-1, sb.indexOf((String) null, 2)); - } - - public void testLastIndexOfStringInt() { - StrBuilder sb = new StrBuilder("abab"); - - assertEquals(0, sb.lastIndexOf("a", 1)); - //should work like String#lastIndexOf - assertEquals("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1)); - - assertEquals(0, sb.lastIndexOf("ab", 1)); - //should work like String#lastIndexOf - assertEquals("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1)); - - assertEquals(1, sb.lastIndexOf("b", 2)); - assertEquals("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2)); - - assertEquals(1, sb.lastIndexOf("ba", 2)); - assertEquals("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2)); - - assertEquals(-1, sb.lastIndexOf("z", 2)); - - sb = new StrBuilder("xyzabc"); - assertEquals(2, sb.lastIndexOf("za", sb.length())); - assertEquals(-1, sb.lastIndexOf("za", 1)); - - assertEquals(-1, sb.lastIndexOf((String) null, 2)); - } - - public void testContainsChar() { - StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz"); - assertTrue (sb.contains('a')); - assertTrue (sb.contains('o')); - assertTrue (sb.contains('z')); - assertFalse (sb.contains('1')); - } - - public void testContainsString() { - StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz"); - assertTrue (sb.contains("a")); - assertTrue (sb.contains("pq")); - assertTrue (sb.contains("z")); - assertFalse (sb.contains("zyx")); - } - - public void testMidString() { - StrBuilder sb = new StrBuilder("hello goodbye hello"); - assertEquals ("goodbye", sb.midString(6, 7)); - assertEquals ("hello", sb.midString(0, 5)); - assertEquals ("hello", sb.midString(-5, 5)); - assertEquals ("", sb.midString(0, -1)); - assertEquals ("", sb.midString(20, 2)); - } - - public void testRightString() { - StrBuilder sb = new StrBuilder("left right"); - assertEquals ("right", sb.rightString(5)); - assertEquals ("", sb.rightString(0)); - assertEquals ("", sb.rightString(-5)); - assertEquals ("left right", sb.rightString(15)); - } - - public void testLeftString() { - StrBuilder sb = new StrBuilder("left right"); - assertEquals ("left", sb.leftString(4)); - assertEquals ("", sb.leftString(0)); - assertEquals ("", sb.leftString(-5)); - assertEquals ("left right", sb.leftString(15)); - } - + + //----------------------------------------------------------------------- public void testSubstringInt() { StrBuilder sb = new StrBuilder ("hello goodbye"); assertEquals ("goodbye", sb.substring(6)); @@ -1836,7 +1082,346 @@ public void testSubstringIntInt() { } catch (IndexOutOfBoundsException e) {} } + // ----------------------------------------------------------------------- + public void testMidString() { + StrBuilder sb = new StrBuilder("hello goodbye hello"); + assertEquals("goodbye", sb.midString(6, 7)); + assertEquals("hello", sb.midString(0, 5)); + assertEquals("hello", sb.midString(-5, 5)); + assertEquals("", sb.midString(0, -1)); + assertEquals("", sb.midString(20, 2)); + } + + public void testRightString() { + StrBuilder sb = new StrBuilder("left right"); + assertEquals("right", sb.rightString(5)); + assertEquals("", sb.rightString(0)); + assertEquals("", sb.rightString(-5)); + assertEquals("left right", sb.rightString(15)); + } + + public void testLeftString() { + StrBuilder sb = new StrBuilder("left right"); + assertEquals("left", sb.leftString(4)); + assertEquals("", sb.leftString(0)); + assertEquals("", sb.leftString(-5)); + assertEquals("left right", sb.leftString(15)); + } + + // ----------------------------------------------------------------------- + public void testContains_char() { + StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz"); + assertEquals(true, sb.contains('a')); + assertEquals(true, sb.contains('o')); + assertEquals(true, sb.contains('z')); + assertEquals(false, sb.contains('1')); + } + + public void testContains_String() { + StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz"); + assertEquals(true, sb.contains("a")); + assertEquals(true, sb.contains("pq")); + assertEquals(true, sb.contains("z")); + assertEquals(false, sb.contains("zyx")); + assertEquals(false, sb.contains((String) null)); + } + + public void testContains_StrMatcher() { + StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz"); + assertEquals(true, sb.contains(StrMatcher.charMatcher('a'))); + assertEquals(true, sb.contains(StrMatcher.stringMatcher("pq"))); + assertEquals(true, sb.contains(StrMatcher.charMatcher('z'))); + assertEquals(false, sb.contains(StrMatcher.stringMatcher("zy"))); + assertEquals(false, sb.contains((StrMatcher) null)); + + sb = new StrBuilder(); + assertEquals(false, sb.contains(A_NUMBER_MATCHER)); + sb.append("B A1 C"); + assertEquals(true, sb.contains(A_NUMBER_MATCHER)); + } + + // ----------------------------------------------------------------------- + public void testIndexOf_char() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals (0, sb.indexOf('a')); + //should work like String#indexOf + assertEquals ("abab".indexOf('a'), sb.indexOf('a')); + + assertEquals(1, sb.indexOf('b')); + assertEquals ("abab".indexOf('b'), sb.indexOf('b')); + + assertEquals (-1, sb.indexOf('z')); + } + + public void testIndexOf_char_int() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals (2, sb.indexOf('a', 1)); + //should work like String#indexOf + assertEquals ("abab".indexOf('a', 1), sb.indexOf('a', 1)); + + assertEquals(3, sb.indexOf('b', 2)); + assertEquals ("abab".indexOf('b', 2), sb.indexOf('b', 2)); + + assertEquals (-1, sb.indexOf('z', 2)); + + sb = new StrBuilder("xyzabc"); + assertEquals (2, sb.indexOf('z', 0)); + assertEquals (-1, sb.indexOf('z', 3)); + } + + public void testLastIndexOf_char() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals (2, sb.lastIndexOf('a')); + //should work like String#lastIndexOf + assertEquals ("abab".lastIndexOf('a'), sb.lastIndexOf('a')); + + assertEquals(3, sb.lastIndexOf('b')); + assertEquals ("abab".lastIndexOf('b'), sb.lastIndexOf('b')); + + assertEquals (-1, sb.lastIndexOf('z')); + } + + public void testLastIndexOf_char_int() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals (0, sb.lastIndexOf('a', 1)); + //should work like String#lastIndexOf + assertEquals ("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1)); + + assertEquals(1, sb.lastIndexOf('b', 2)); + assertEquals ("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2)); + + assertEquals (-1, sb.lastIndexOf('z', 2)); + + sb = new StrBuilder("xyzabc"); + assertEquals (2, sb.lastIndexOf('z', sb.length())); + assertEquals (-1, sb.lastIndexOf('z', 1)); + } + //----------------------------------------------------------------------- + public void testIndexOf_String() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals(0, sb.indexOf("a")); + //should work like String#indexOf + assertEquals("abab".indexOf("a"), sb.indexOf("a")); + + assertEquals(0, sb.indexOf("ab")); + //should work like String#indexOf + assertEquals("abab".indexOf("ab"), sb.indexOf("ab")); + + assertEquals(1, sb.indexOf("b")); + assertEquals("abab".indexOf("b"), sb.indexOf("b")); + + assertEquals(1, sb.indexOf("ba")); + assertEquals("abab".indexOf("ba"), sb.indexOf("ba")); + + assertEquals(-1, sb.indexOf("z")); + + assertEquals(-1, sb.indexOf((String) null)); + } + + public void testIndexOf_String_int() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals(2, sb.indexOf("a", 1)); + //should work like String#indexOf + assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1)); + + assertEquals(2, sb.indexOf("ab", 1)); + //should work like String#indexOf + assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1)); + + assertEquals(3, sb.indexOf("b", 2)); + assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2)); + + assertEquals(1, sb.indexOf("ba", 1)); + assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2)); + + assertEquals(-1, sb.indexOf("z", 2)); + + sb = new StrBuilder("xyzabc"); + assertEquals(2, sb.indexOf("za", 0)); + assertEquals(-1, sb.indexOf("za", 3)); + + assertEquals(-1, sb.indexOf((String) null, 2)); + } + + public void testLastIndexOf_String() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals(2, sb.lastIndexOf("a")); + //should work like String#lastIndexOf + assertEquals("abab".lastIndexOf("a"), sb.lastIndexOf("a")); + + assertEquals(2, sb.lastIndexOf("ab")); + //should work like String#lastIndexOf + assertEquals("abab".lastIndexOf("ab"), sb.lastIndexOf("ab")); + + assertEquals(3, sb.lastIndexOf("b")); + assertEquals("abab".lastIndexOf("b"), sb.lastIndexOf("b")); + + assertEquals(1, sb.lastIndexOf("ba")); + assertEquals("abab".lastIndexOf("ba"), sb.lastIndexOf("ba")); + + assertEquals(-1, sb.lastIndexOf("z")); + + assertEquals(-1, sb.lastIndexOf((String) null)); + } + + public void testLastIndexOf_String_int() { + StrBuilder sb = new StrBuilder("abab"); + + assertEquals(0, sb.lastIndexOf("a", 1)); + //should work like String#lastIndexOf + assertEquals("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1)); + + assertEquals(0, sb.lastIndexOf("ab", 1)); + //should work like String#lastIndexOf + assertEquals("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1)); + + assertEquals(1, sb.lastIndexOf("b", 2)); + assertEquals("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2)); + + assertEquals(1, sb.lastIndexOf("ba", 2)); + assertEquals("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2)); + + assertEquals(-1, sb.lastIndexOf("z", 2)); + + sb = new StrBuilder("xyzabc"); + assertEquals(2, sb.lastIndexOf("za", sb.length())); + assertEquals(-1, sb.lastIndexOf("za", 1)); + + assertEquals(-1, sb.lastIndexOf((String) null, 2)); + } + + // ----------------------------------------------------------------------- + public void testIndexOf_StrMatcher() { + StrBuilder sb = new StrBuilder(); + assertEquals(-1, sb.indexOf((StrMatcher) null)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'))); + + sb.append("ab bd"); + assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'))); + assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'))); + assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher())); + assertEquals(4, sb.indexOf(StrMatcher.charMatcher('d'))); + assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher())); + assertEquals(-1, sb.indexOf((StrMatcher) null)); + + sb.append(" A1 junction"); + assertEquals(6, sb.indexOf(A_NUMBER_MATCHER)); + } + + public void testIndexOf_StrMatcher_int() { + StrBuilder sb = new StrBuilder(); + assertEquals(-1, sb.indexOf((StrMatcher) null, 2)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2)); + + sb.append("ab bd"); + assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), -2)); + assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), 0)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 20)); + + assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), -1)); + assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 0)); + assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 1)); + assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 2)); + assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 3)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 4)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 5)); + assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 6)); + + assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), -2)); + assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 0)); + assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 2)); + assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 4)); + assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 20)); + + assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher(), 0)); + assertEquals(-1, sb.indexOf((StrMatcher) null, 0)); + + sb.append(" A1 junction with A2"); + assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 5)); + assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 6)); + assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 7)); + assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 22)); + assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 23)); + assertEquals(-1, sb.indexOf(A_NUMBER_MATCHER, 24)); + } + + public void testLastIndexOf_StrMatcher() { + StrBuilder sb = new StrBuilder(); + assertEquals(-1, sb.lastIndexOf((StrMatcher) null)); + assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'))); + + sb.append("ab bd"); + assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'))); + assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'))); + assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher())); + assertEquals(4, sb.lastIndexOf(StrMatcher.charMatcher('d'))); + assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher())); + assertEquals(-1, sb.lastIndexOf((StrMatcher) null)); + + sb.append(" A1 junction"); + assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER)); + } + + public void testLastIndexOf_StrMatcher_int() { + StrBuilder sb = new StrBuilder(); + assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 2)); + assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2)); + + sb.append("ab bd"); + assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), -2)); + assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0)); + assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2)); + assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 20)); + + assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), -1)); + assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 0)); + assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 1)); + assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 2)); + assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 3)); + assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 4)); + assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 5)); + assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 6)); + + assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), -2)); + assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), 0)); + assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 2)); + assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 4)); + assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 20)); + + assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher(), 0)); + assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 0)); + + sb.append(" A1 junction with A2"); + assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 5)); + assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 6)); // A matches, 1 is outside bounds + assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 7)); + assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 22)); + assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 23)); // A matches, 2 is outside bounds + assertEquals(23, sb.lastIndexOf(A_NUMBER_MATCHER, 24)); + } + + static final StrMatcher A_NUMBER_MATCHER = new StrMatcher() { + public int isMatch(char[] buffer, int pos, int bufferStart, int bufferEnd) { + if (buffer[pos] == 'A') { + pos++; + if (pos < bufferEnd && buffer[pos] >= '0' && buffer[pos] <= '9') { + return 2; + } + } + return 0; + } + }; + + // ----------------------------------------------------------------------- public void testAsReader() throws Exception { StrBuilder sb = new StrBuilder ("some text"); Reader reader = sb.asReader(); @@ -1882,4 +1467,19 @@ public void testAsWriter() throws Exception { assertEquals("based", sb.toString()); } + //----------------------------------------------------------------------- + public void testToString() { + StrBuilder sb = new StrBuilder("abc"); + assertEquals("abc", sb.toString()); + } + + //----------------------------------------------------------------------- + public void testToStringBuffer() { + StrBuilder sb = new StrBuilder(); + assertEquals(new StringBuffer().toString(), sb.toStringBuffer().toString()); + + sb.append("junit"); + assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString()); + } + } diff --git a/src/test/org/apache/commons/lang/text/StrMatcherTest.java b/src/test/org/apache/commons/lang/text/StrMatcherTest.java new file mode 100644 index 000000000..fb2cfd0f7 --- /dev/null +++ b/src/test/org/apache/commons/lang/text/StrMatcherTest.java @@ -0,0 +1,227 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang.text; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * Unit tests for {@link org.apache.commons.lang.text.StrMatcher}. + * + * @version $Id$ + */ +public class StrMatcherTest extends TestCase { + + private static final char[] BUFFER1 = "0,1\t2 3\n\r\f\u0000'\"".toCharArray(); + + private static final char[] BUFFER2 = "abcdef".toCharArray(); + + /** + * Main method. + * + * @param args command line arguments, ignored + */ + public static void main(String[] args) { + TestRunner.run(suite()); + } + + /** + * Return a new test suite containing this test case. + * + * @return a new test suite containing this test case + */ + public static Test suite() { + TestSuite suite = new TestSuite(StrMatcherTest.class); + suite.setName("StrMatcher Tests"); + return suite; + } + + /** + * Create a new test case with the specified name. + * + * @param name the name + */ + public StrMatcherTest(String name) { + super(name); + } + + //----------------------------------------------------------------------- + public void testCommaMatcher() { + StrMatcher matcher = StrMatcher.commaMatcher(); + assertSame(matcher, StrMatcher.commaMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 0, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 1, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testTabMatcher() { + StrMatcher matcher = StrMatcher.tabMatcher(); + assertSame(matcher, StrMatcher.tabMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testSpaceMatcher() { + StrMatcher matcher = StrMatcher.spaceMatcher(); + assertSame(matcher, StrMatcher.spaceMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testSplitMatcher() { + StrMatcher matcher = StrMatcher.splitMatcher(); + assertSame(matcher, StrMatcher.splitMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 7, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 8, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 9, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testTrimMatcher() { + StrMatcher matcher = StrMatcher.trimMatcher(); + assertSame(matcher, StrMatcher.trimMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 7, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 8, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 9, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testSingleQuoteMatcher() { + StrMatcher matcher = StrMatcher.singleQuoteMatcher(); + assertSame(matcher, StrMatcher.singleQuoteMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testDoubleQuoteMatcher() { + StrMatcher matcher = StrMatcher.doubleQuoteMatcher(); + assertSame(matcher, StrMatcher.doubleQuoteMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testQuoteMatcher() { + StrMatcher matcher = StrMatcher.quoteMatcher(); + assertSame(matcher, StrMatcher.quoteMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length)); + assertEquals(1, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testNoneMatcher() { + StrMatcher matcher = StrMatcher.noneMatcher(); + assertSame(matcher, StrMatcher.noneMatcher()); + assertEquals(0, matcher.isMatch(BUFFER1, 0, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 1, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 2, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 3, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 4, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 5, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 6, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 7, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 8, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 9, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 10, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 11, 0, BUFFER1.length)); + assertEquals(0, matcher.isMatch(BUFFER1, 12, 0, BUFFER1.length)); + } + + //----------------------------------------------------------------------- + public void testCharMatcher_char() { + StrMatcher matcher = StrMatcher.charMatcher('c'); + assertEquals(0, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length)); + assertEquals(1, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length)); + } + + //----------------------------------------------------------------------- + public void testCharSetMatcher_String() { + StrMatcher matcher = StrMatcher.charSetMatcher("ace"); + assertEquals(1, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length)); + assertEquals(1, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length)); + assertEquals(1, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length)); + assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher("")); + assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher((String) null)); + } + + //----------------------------------------------------------------------- + public void testCharSetMatcher_charArray() { + StrMatcher matcher = StrMatcher.charSetMatcher("ace".toCharArray()); + assertEquals(1, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length)); + assertEquals(1, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length)); + assertEquals(1, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length)); + assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher(new char[0])); + assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher((char[]) null)); + } + + //----------------------------------------------------------------------- + public void testStringMatcher_String() { + StrMatcher matcher = StrMatcher.stringMatcher("bc"); + assertEquals(0, matcher.isMatch(BUFFER2, 0, 0, BUFFER2.length)); + assertEquals(2, matcher.isMatch(BUFFER2, 1, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 2, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 3, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 4, 0, BUFFER2.length)); + assertEquals(0, matcher.isMatch(BUFFER2, 5, 0, BUFFER2.length)); + assertSame(StrMatcher.noneMatcher(), StrMatcher.stringMatcher("")); + assertSame(StrMatcher.noneMatcher(), StrMatcher.stringMatcher((String) null)); + } + + //----------------------------------------------------------------------- + public void testMatcherIndices() { + // remember that the API contract is tight for the isMatch() method + // all the onus is on the caller, so invalid inputs are not + // the concern of StrMatcher, and are not bugs + StrMatcher matcher = StrMatcher.stringMatcher("bc"); + assertEquals(2, matcher.isMatch(BUFFER2, 1, 1, BUFFER2.length)); + assertEquals(2, matcher.isMatch(BUFFER2, 1, 0, 3)); + assertEquals(0, matcher.isMatch(BUFFER2, 1, 0, 2)); + } + +} diff --git a/src/test/org/apache/commons/lang/text/TextTestSuite.java b/src/test/org/apache/commons/lang/text/TextTestSuite.java index 598458396..8ae87a332 100644 --- a/src/test/org/apache/commons/lang/text/TextTestSuite.java +++ b/src/test/org/apache/commons/lang/text/TextTestSuite.java @@ -49,6 +49,8 @@ public static Test suite() { TestSuite suite = new TestSuite(); suite.setName("Commons-Lang-Text Tests"); suite.addTest(StrBuilderTest.suite()); + suite.addTest(StrBuilderAppendInsertTest.suite()); + suite.addTest(StrMatcherTest.suite()); suite.addTest(StrTokenizerTest.suite()); suite.addTestSuite(VariableFormatterTest.class); return suite;