http://git-wip-us.apache.org/repos/asf/commons-text/blob/978e2896/src/test/java/org/apache/commons/text/StringSubstitutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java index 6f62fe8..e389342 100644 --- a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java +++ b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; + import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -52,16 +53,16 @@ public class StringSubstitutorTest { assertNull(sub.replace((char[]) null, 0, 100)); assertNull(sub.replace((StringBuffer) null)); assertNull(sub.replace((StringBuffer) null, 0, 100)); - assertNull(sub.replace((StrBuilder) null)); - assertNull(sub.replace((StrBuilder) null, 0, 100)); + assertNull(sub.replace((TextStringBuilder) null)); + assertNull(sub.replace((TextStringBuilder) null, 0, 100)); assertNull(sub.replace((Object) null)); assertFalse(sub.replaceIn((StringBuffer) null)); assertFalse(sub.replaceIn((StringBuffer) null, 0, 100)); - assertFalse(sub.replaceIn((StrBuilder) null)); - assertFalse(sub.replaceIn((StrBuilder) null, 0, 100)); + assertFalse(sub.replaceIn((TextStringBuilder) null)); + assertFalse(sub.replaceIn((TextStringBuilder) null, 0, 100)); } else { assertEquals(replaceTemplate, sub.replace(replaceTemplate)); - final StrBuilder bld = new StrBuilder(replaceTemplate); + final TextStringBuilder bld = new TextStringBuilder(replaceTemplate); assertFalse(sub.replaceIn(bld)); assertEquals(replaceTemplate, bld.toString()); } @@ -72,7 +73,7 @@ public class StringSubstitutorTest { doTestReplace(sub, expectedResult, replaceTemplate, substring); } - //----------------------------------------------------------------------- + // ----------------------------------------------------------------------- private void doTestReplace(final StringSubstitutor sub, final String expectedResult, final String replaceTemplate, final boolean substring) { final String expectedShortResult = expectedResult.substring(1, expectedResult.length() - 1); @@ -104,15 +105,15 @@ public class StringSubstitutorTest { assertEquals(expectedShortResult, sub.replace(builder, 1, builder.length() - 2)); } - // replace using StrBuilder - StrBuilder bld = new StrBuilder(replaceTemplate); + // replace using TextStringBuilder + TextStringBuilder bld = new TextStringBuilder(replaceTemplate); assertEquals(expectedResult, sub.replace(bld)); if (substring) { assertEquals(expectedShortResult, sub.replace(bld, 1, bld.length() - 2)); } // replace using object - final MutableObject<String> obj = new MutableObject<>(replaceTemplate); // toString returns template + final MutableObject<String> obj = new MutableObject<>(replaceTemplate); // toString returns template assertEquals(expectedResult, sub.replace(obj)); // replace in StringBuffer @@ -122,7 +123,7 @@ public class StringSubstitutorTest { if (substring) { buf = new StringBuffer(replaceTemplate); assertTrue(sub.replaceIn(buf, 1, buf.length() - 2)); - assertEquals(expectedResult, buf.toString()); // expect full result as remainder is untouched + assertEquals(expectedResult, buf.toString()); // expect full result as remainder is untouched } // replace in StringBuilder @@ -132,17 +133,17 @@ public class StringSubstitutorTest { if (substring) { builder = new StringBuilder(replaceTemplate); assertTrue(sub.replaceIn(builder, 1, builder.length() - 2)); - assertEquals(expectedResult, builder.toString()); // expect full result as remainder is untouched + assertEquals(expectedResult, builder.toString()); // expect full result as remainder is untouched } - // replace in StrBuilder - bld = new StrBuilder(replaceTemplate); + // replace in TextStringBuilder + bld = new TextStringBuilder(replaceTemplate); assertTrue(sub.replaceIn(bld)); assertEquals(expectedResult, bld.toString()); if (substring) { - bld = new StrBuilder(replaceTemplate); + bld = new TextStringBuilder(replaceTemplate); assertTrue(sub.replaceIn(bld, 1, bld.length() - 2)); - assertEquals(expectedResult, bld.toString()); // expect full result as remainder is untouched + assertEquals(expectedResult, bld.toString()); // expect full result as remainder is untouched } } @@ -158,7 +159,7 @@ public class StringSubstitutorTest { values = null; } - //----------------------------------------------------------------------- + // ----------------------------------------------------------------------- /** * Tests get set. */ @@ -220,8 +221,8 @@ public class StringSubstitutorTest { */ @Test public void testReplaceComplexEscaping() { - doTestReplace("The ${quick brown fox} jumps over the lazy dog.", - "The $${${animal}} jumps over the ${target}.", true); + doTestReplace("The ${quick brown fox} jumps over the lazy dog.", "The $${${animal}} jumps over the ${target}.", + true); doTestReplace("The ${quick brown fox} jumps over the lazy dog. ${1234567890}.", "The $${${animal}} jumps over the ${target}. $${${undefined.number:-1234567890}}.", true); } @@ -261,8 +262,8 @@ public class StringSubstitutorTest { @Test public void testReplaceInTakingStringBufferWithNonNull() { - final StringSubstitutor strSubstitutor = - new StringSubstitutor(new HashMap<String, String>(), "WV@i#y?N*[", "WV@i#y?N*[", '*'); + final StringSubstitutor strSubstitutor = new StringSubstitutor(new HashMap<String, String>(), "WV@i#y?N*[", + "WV@i#y?N*[", '*'); assertFalse(strSubstitutor.isPreserveEscapes()); assertFalse(strSubstitutor.replaceIn(new StringBuffer("WV@i#y?N*["))); @@ -292,8 +293,8 @@ public class StringSubstitutorTest { final Map<String, Object> hashMap = new HashMap<>(); final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.mapStringLookup(hashMap); final StringMatcher strMatcher = StringMatcherFactory.INSTANCE.tabMatcher(); - final StringSubstitutor strSubstitutor = - new StringSubstitutor(mapStringLookup, strMatcher, strMatcher, 'b', strMatcher); + final StringSubstitutor strSubstitutor = new StringSubstitutor(mapStringLookup, strMatcher, strMatcher, 'b', + strMatcher); assertFalse(strSubstitutor.replaceIn((StringBuilder) null, 1315, (-1369))); assertEquals('b', strSubstitutor.getEscapeChar()); @@ -310,20 +311,13 @@ public class StringSubstitutorTest { values.put("species", "2"); final StringSubstitutor sub = new StringSubstitutor(values); sub.setEnableSubstitutionInVariables(true); - assertEquals( - "Wrong result (1)", - "The mouse jumps over the lazy dog.", + assertEquals("Wrong result (1)", "The mouse jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); values.put("species", "1"); - assertEquals( - "Wrong result (2)", - "The fox jumps over the lazy dog.", + assertEquals("Wrong result (2)", "The fox jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); - assertEquals( - "Wrong result (3)", - "The fox jumps over the lazy dog.", - sub.replace("The ${unknown.animal.${unknown.species:-1}:-fox} " - + "jumps over the ${unknow.target:-lazy dog}.")); + assertEquals("Wrong result (3)", "The fox jumps over the lazy dog.", sub.replace( + "The ${unknown.animal.${unknown.species:-1}:-fox} " + "jumps over the ${unknow.target:-lazy dog}.")); } /** @@ -335,13 +329,9 @@ public class StringSubstitutorTest { values.put("animal.2", "mouse"); values.put("species", "2"); final StringSubstitutor sub = new StringSubstitutor(values); - assertEquals( - "Wrong result (1)", - "The ${animal.${species}} jumps over the lazy dog.", + assertEquals("Wrong result (1)", "The ${animal.${species}} jumps over the lazy dog.", sub.replace("The ${animal.${species}} jumps over the ${target}.")); - assertEquals( - "Wrong result (2)", - "The ${animal.${species:-1}} jumps over the lazy dog.", + assertEquals("Wrong result (2)", "The ${animal.${species:-1}} jumps over the lazy dog.", sub.replace("The ${animal.${species:-1}} jumps over the ${target}.")); } @@ -357,13 +347,9 @@ public class StringSubstitutorTest { values.put("species.brown", "2"); final StringSubstitutor sub = new StringSubstitutor(values); sub.setEnableSubstitutionInVariables(true); - assertEquals( - "Wrong result (1)", - "The white mouse jumps over the lazy dog.", + assertEquals("Wrong result (1)", "The white mouse jumps over the lazy dog.", sub.replace("The ${animal.${species.${color}}} jumps over the ${target}.")); - assertEquals( - "Wrong result (2)", - "The brown fox jumps over the lazy dog.", + assertEquals("Wrong result (2)", "The brown fox jumps over the lazy dog.", sub.replace("The ${animal.${species.${unknownColor:-brown}}} jumps over the ${target}.")); } @@ -436,7 +422,7 @@ public class StringSubstitutorTest { doTestReplace("The quick brown fox jumps over the lazy dog.", "The ${animal} jumps over the ${target}.", true); } - //----------------------------------------------------------------------- + // ----------------------------------------------------------------------- /** * Tests simple key replace. */ @@ -519,18 +505,18 @@ public class StringSubstitutorTest { doTestNoReplace("${${ }}"); } - //----------------------------------------------------------------------- + // ----------------------------------------------------------------------- /** * Tests protected. */ @Test public void testResolveVariable() { - final StrBuilder builder = new StrBuilder("Hi ${name}!"); + final TextStringBuilder builder = new TextStringBuilder("Hi ${name}!"); final Map<String, String> map = new HashMap<>(); map.put("name", "commons"); final StringSubstitutor sub = new StringSubstitutor(map) { @Override - protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, + protected String resolveVariable(final String variableName, final TextStringBuilder buf, final int startPos, final int endPos) { assertEquals("name", variableName); assertSame(builder, buf); @@ -553,7 +539,7 @@ public class StringSubstitutorTest { assertEquals("Hello there commons!", StringSubstitutor.replace("@greeting@ there @name@!", map, "@", "@")); } - //----------------------------------------------------------------------- + // ----------------------------------------------------------------------- /** * Tests static. */ @@ -579,15 +565,14 @@ public class StringSubstitutorTest { */ @Test public void testStaticReplaceSystemProperties() { - final StrBuilder buf = new StrBuilder(); + final TextStringBuilder buf = new TextStringBuilder(); buf.append("Hi ").append(System.getProperty("user.name")); buf.append(", you are working with "); buf.append(System.getProperty("os.name")); buf.append(", your home directory is "); buf.append(System.getProperty("user.home")).append('.'); - assertEquals(buf.toString(), StringSubstitutor.replaceSystemProperties("Hi ${user.name}, you are " - + "working with ${os.name}, your home " - + "directory is ${user.home}.")); + assertEquals(buf.toString(), StringSubstitutor.replaceSystemProperties( + "Hi ${user.name}, you are " + "working with ${os.name}, your home " + "directory is ${user.home}.")); } /**
http://git-wip-us.apache.org/repos/asf/commons-text/blob/978e2896/src/test/java/org/apache/commons/text/TextStringBuilderAppendInsertTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderAppendInsertTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderAppendInsertTest.java new file mode 100644 index 0000000..9a944c7 --- /dev/null +++ b/src/test/java/org/apache/commons/text/TextStringBuilderAppendInsertTest.java @@ -0,0 +1,1607 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.text; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + +import java.text.DecimalFormatSymbols; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; + +import org.junit.Test; + +/** + * Unit tests for {@link TextStringBuilder}. + */ +public class TextStringBuilderAppendInsertTest { + + /** The system line separator. */ + private static final String SEP = System.lineSeparator(); + + /** Test subclass of Object, with a toString method. */ + private static final Object FOO = new Object() { + @Override + public String toString() { + return "foo"; + } + }; + + //----------------------------------------------------------------------- + @Test + public void testAppendNewLine() { + TextStringBuilder sb = new TextStringBuilder("---"); + sb.appendNewLine().append("+++"); + assertThat(sb.toString()).isEqualTo("---" + SEP + "+++"); + + sb = new TextStringBuilder("---"); + sb.setNewLineText("#").appendNewLine().setNewLineText(null).appendNewLine(); + assertThat(sb.toString()).isEqualTo("---#" + SEP); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendWithNullText() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL"); + assertThat(sb.toString()).isEqualTo(""); + + sb.appendNull(); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb.append((Object) null); + assertThat(sb.toString()).isEqualTo("NULLNULL"); + + sb.append(FOO); + assertThat(sb.toString()).isEqualTo("NULLNULLfoo"); + + sb.append((String) null); + assertThat(sb.toString()).isEqualTo("NULLNULLfooNULL"); + + sb.append(""); + assertThat(sb.toString()).isEqualTo("NULLNULLfooNULL"); + + sb.append("bar"); + assertThat(sb.toString()).isEqualTo("NULLNULLfooNULLbar"); + + sb.append((StringBuffer) null); + assertThat(sb.toString()).isEqualTo("NULLNULLfooNULLbarNULL"); + + sb.append(new StringBuffer("baz")); + assertThat(sb.toString()).isEqualTo("NULLNULLfooNULLbarNULLbaz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_Object() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendNull(); + assertThat(sb.toString()).isEqualTo(""); + + sb.append((Object) null); + assertThat(sb.toString()).isEqualTo(""); + + sb.append(FOO); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append((StringBuffer) null); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuffer("baz")); + assertThat(sb.toString()).isEqualTo("foobaz"); + + sb.append(new TextStringBuilder("yes")); + assertThat(sb.toString()).isEqualTo("foobazyes"); + + sb.append((CharSequence) "Seq"); + assertThat(sb.toString()).isEqualTo("foobazyesSeq"); + + sb.append(new StringBuilder("bld")); // Check it supports StringBuilder + assertThat(sb.toString()).isEqualTo("foobazyesSeqbld"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_StringBuilder() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((String) null); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new StringBuilder("foo")); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuilder("")); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuilder("bar")); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_String() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((String) null); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append("foo"); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(""); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append("bar"); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_String_int_int() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((String) null, 0, 1); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append("foo", 0, 3); + assertThat(sb.toString()).isEqualTo("foo"); + + try { + sb.append("bar", -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append("bar", 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.append("bar", 3, 0); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append("abcbardef", 3, 3); + assertThat(sb.toString()).isEqualTo("foobar"); + + sb.append((CharSequence) "abcbardef", 4, 3); + assertThat(sb.toString()).isEqualTo("foobarard"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_StringBuilder_int_int() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((String) null, 0, 1); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new StringBuilder("foo"), 0, 3); + assertThat(sb.toString()).isEqualTo("foo"); + + try { + sb.append(new StringBuilder("bar"), -1, 1); + fail("append(StringBuilder, -1,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuilder("bar"), 3, 1); + fail("append(StringBuilder, 3,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuilder("bar"), 1, -1); + fail("append(StringBuilder,, -1) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuilder("bar"), 1, 3); + fail("append(StringBuilder, 1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuilder("bar"), -1, 3); + fail("append(StringBuilder, -1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuilder("bar"), 4, 0); + fail("append(StringBuilder, 4, 0) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.append(new StringBuilder("bar"), 3, 0); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuilder("abcbardef"), 3, 3); + assertThat(sb.toString()).isEqualTo("foobar"); + + sb.append(new StringBuilder("abcbardef"), 4, 3); + assertThat(sb.toString()).isEqualTo("foobarard"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_StringBuffer() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((StringBuffer) null); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new StringBuffer("foo")); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuffer("")); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuffer("bar")); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_StringBuffer_int_int() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((StringBuffer) null, 0, 1); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new StringBuffer("foo"), 0, 3); + assertThat(sb.toString()).isEqualTo("foo"); + + try { + sb.append(new StringBuffer("bar"), -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new StringBuffer("bar"), 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.append(new StringBuffer("bar"), 3, 0); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new StringBuffer("abcbardef"), 3, 3); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_TextStringBuilder() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((TextStringBuilder) null); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new TextStringBuilder("foo")); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new TextStringBuilder("")); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new TextStringBuilder("bar")); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_TextStringBuilder_int_int() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((TextStringBuilder) null, 0, 1); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new TextStringBuilder("foo"), 0, 3); + assertThat(sb.toString()).isEqualTo("foo"); + + try { + sb.append(new TextStringBuilder("bar"), -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new TextStringBuilder("bar"), 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new TextStringBuilder("bar"), 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new TextStringBuilder("bar"), 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new TextStringBuilder("bar"), -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new TextStringBuilder("bar"), 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.append(new TextStringBuilder("bar"), 3, 0); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new TextStringBuilder("abcbardef"), 3, 3); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_CharArray() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((char[]) null); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new char[0]); + assertThat(sb.toString()).isEqualTo(""); + + sb.append(new char[]{'f', 'o', 'o'}); + assertThat(sb.toString()).isEqualTo("foo"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_CharArray_int_int() { + TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("NULL").append((char[]) null, 0, 1); + assertThat(sb.toString()).isEqualTo("NULL"); + + sb = new TextStringBuilder(); + sb.append(new char[]{'f', 'o', 'o'}, 0, 3); + assertThat(sb.toString()).isEqualTo("foo"); + + try { + sb.append(new char[]{'b', 'a', 'r'}, -1, 1); + fail("append(char[], -1,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, 3, 1); + fail("append(char[], 3,) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, 1, -1); + fail("append(char[],, -1) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, 1, 3); + fail("append(char[], 1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, -1, 3); + fail("append(char[], -1, 3) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.append(new char[]{'b', 'a', 'r'}, 4, 0); + fail("append(char[], 4, 0) expected IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.append(new char[]{'b', 'a', 'r'}, 3, 0); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.append(new char[]{'a', 'b', 'c', 'b', 'a', 'r', 'd', 'e', 'f'}, 3, 3); + assertThat(sb.toString()).isEqualTo("foobar"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_Boolean() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.append(true); + assertThat(sb.toString()).isEqualTo("true"); + + sb.append(false); + assertThat(sb.toString()).isEqualTo("truefalse"); + + sb.append('!'); + assertThat(sb.toString()).isEqualTo("truefalse!"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_PrimitiveNumber() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.append(0); + assertThat(sb.toString()).isEqualTo("0"); + + sb.append(1L); + assertThat(sb.toString()).isEqualTo("01"); + + sb.append(2.3f); + assertThat(sb.toString()).isEqualTo("012.3"); + + sb.append(4.5d); + assertThat(sb.toString()).isEqualTo("012.34.5"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_FormattedString() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final String str) { + count[0]++; + return super.append(str); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln("Hello %s", "Alice"); + assertThat(sb.toString()).isEqualTo("Hello Alice" + SEP); + assertThat(count[0]); // appendNewLine() calls append(String).isEqualTo(2) + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_Object() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendln((Object) null); + assertThat(sb.toString()).isEqualTo("" + SEP); + + sb.appendln(FOO); + assertThat(sb.toString()).isEqualTo(SEP + "foo" + SEP); + + sb.appendln(Integer.valueOf(6)); + assertThat(sb.toString()).isEqualTo(SEP + "foo" + SEP + "6" + SEP); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_String() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final String str) { + count[0]++; + return super.append(str); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln("foo"); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]); // appendNewLine() calls append(String).isEqualTo(2) + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_String_int_int() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final String str, final int startIndex, final int length) { + count[0]++; + return super.append(str, startIndex, length); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln("foo", 0, 3); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_StringBuffer() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final StringBuffer str) { + count[0]++; + return super.append(str); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln(new StringBuffer("foo")); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_StringBuilder() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final StringBuilder str) { + count[0]++; + return super.append(str); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln(new StringBuilder("foo")); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_StringBuffer_int_int() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final StringBuffer str, final int startIndex, final int length) { + count[0]++; + return super.append(str, startIndex, length); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln(new StringBuffer("foo"), 0, 3); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_StringBuilder_int_int() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final StringBuilder str, final int startIndex, final int length) { + count[0]++; + return super.append(str, startIndex, length); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln(new StringBuilder("foo"), 0, 3); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_TextStringBuilder() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final TextStringBuilder str) { + count[0]++; + return super.append(str); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln(new TextStringBuilder("foo")); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_TextStringBuilder_int_int() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final TextStringBuilder str, final int startIndex, final int length) { + count[0]++; + return super.append(str, startIndex, length); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln(new TextStringBuilder("foo"), 0, 3); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_CharArray() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final char[] str) { + count[0]++; + return super.append(str); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln("foo".toCharArray()); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_CharArray_int_int() { + final int[] count = new int[2]; + final TextStringBuilder sb = new TextStringBuilder() { + private static final long serialVersionUID = 1L; + + @Override + public TextStringBuilder append(final char[] str, final int startIndex, final int length) { + count[0]++; + return super.append(str, startIndex, length); + } + @Override + public TextStringBuilder appendNewLine() { + count[1]++; + return super.appendNewLine(); + } + }; + sb.appendln("foo".toCharArray(), 0, 3); + assertThat(sb.toString()).isEqualTo("foo" + SEP); + assertThat(count[0]).isEqualTo(1); + assertThat(count[1]).isEqualTo(1); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_Boolean() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendln(true); + assertThat(sb.toString()).isEqualTo("true" + SEP); + + sb.clear(); + sb.appendln(false); + assertThat(sb.toString()).isEqualTo("false" + SEP); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendln_PrimitiveNumber() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendln(0); + assertThat(sb.toString()).isEqualTo("0" + SEP); + + sb.clear(); + sb.appendln(1L); + assertThat(sb.toString()).isEqualTo("1" + SEP); + + sb.clear(); + sb.appendln(2.3f); + assertThat(sb.toString()).isEqualTo("2.3" + SEP); + + sb.clear(); + sb.appendln(4.5d); + assertThat(sb.toString()).isEqualTo("4.5" + SEP); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendPadding() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.append("foo"); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.appendPadding(-1, '-'); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.appendPadding(0, '-'); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.appendPadding(1, '-'); + assertThat(sb.toString()).isEqualTo("foo-"); + + sb.appendPadding(16, '-'); + assertThat(sb.length()).isEqualTo(20); + // 12345678901234567890 + assertThat(sb.toString()).isEqualTo("foo-----------------"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendFixedWidthPadLeft() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendFixedWidthPadLeft("foo", -1, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 0, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 1, '-'); + assertThat(sb.toString()).isEqualTo("o"); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 2, '-'); + assertThat(sb.toString()).isEqualTo("oo"); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 3, '-'); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 4, '-'); + assertThat(sb.toString()).isEqualTo("-foo"); + + sb.clear(); + sb.appendFixedWidthPadLeft("foo", 10, '-'); + assertThat(sb.length()).isEqualTo(10); + // 1234567890 + assertThat(sb.toString()).isEqualTo("-------foo"); + + sb.clear(); + sb.setNullText("null"); + sb.appendFixedWidthPadLeft(null, 5, '-'); + assertThat(sb.toString()).isEqualTo("-null"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendFixedWidthPadLeft_int() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendFixedWidthPadLeft(123, -1, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 0, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 1, '-'); + assertThat(sb.toString()).isEqualTo("3"); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 2, '-'); + assertThat(sb.toString()).isEqualTo("23"); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 3, '-'); + assertThat(sb.toString()).isEqualTo("123"); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 4, '-'); + assertThat(sb.toString()).isEqualTo("-123"); + + sb.clear(); + sb.appendFixedWidthPadLeft(123, 10, '-'); + assertThat(sb.length()).isEqualTo(10); + // 1234567890 + assertThat(sb.toString()).isEqualTo("-------123"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendFixedWidthPadRight() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendFixedWidthPadRight("foo", -1, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 0, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 1, '-'); + assertThat(sb.toString()).isEqualTo("f"); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 2, '-'); + assertThat(sb.toString()).isEqualTo("fo"); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 3, '-'); + assertThat(sb.toString()).isEqualTo("foo"); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 4, '-'); + assertThat(sb.toString()).isEqualTo("foo-"); + + sb.clear(); + sb.appendFixedWidthPadRight("foo", 10, '-'); + assertThat(sb.length()).isEqualTo(10); + // 1234567890 + assertThat(sb.toString()).isEqualTo("foo-------"); + + sb.clear(); + sb.setNullText("null"); + sb.appendFixedWidthPadRight(null, 5, '-'); + assertThat(sb.toString()).isEqualTo("null-"); + } + + // See: http://issues.apache.org/jira/browse/LANG-299 + @Test + public void testLang299() { + final TextStringBuilder sb = new TextStringBuilder(1); + sb.appendFixedWidthPadRight("foo", 1, '-'); + assertThat(sb.toString()).isEqualTo("f"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendFixedWidthPadRight_int() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendFixedWidthPadRight(123, -1, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 0, '-'); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 1, '-'); + assertThat(sb.toString()).isEqualTo("1"); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 2, '-'); + assertThat(sb.toString()).isEqualTo("12"); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 3, '-'); + assertThat(sb.toString()).isEqualTo("123"); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 4, '-'); + assertThat(sb.toString()).isEqualTo("123-"); + + sb.clear(); + sb.appendFixedWidthPadRight(123, 10, '-'); + assertThat(sb.length()).isEqualTo(10); + // 1234567890 + assertThat(sb.toString()).isEqualTo("123-------"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppend_FormattedString() { + TextStringBuilder sb; + + sb = new TextStringBuilder(); + sb.append("Hi", (Object[]) null); + assertThat(sb.toString()).isEqualTo("Hi"); + + sb = new TextStringBuilder(); + sb.append("Hi", "Alice"); + assertThat(sb.toString()).isEqualTo("Hi"); + + sb = new TextStringBuilder(); + sb.append("Hi %s", "Alice"); + assertThat(sb.toString()).isEqualTo("Hi Alice"); + + sb = new TextStringBuilder(); + sb.append("Hi %s %,d", "Alice", 5000); + // group separator depends on system locale + final char groupingSeparator = DecimalFormatSymbols.getInstance().getGroupingSeparator(); + final String expected = "Hi Alice 5" + groupingSeparator + "000"; + assertThat(sb.toString()).isEqualTo(expected); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendAll_Array() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendAll((Object[]) null); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendAll(new Object[0]); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendAll(new Object[]{"foo", "bar", "baz"}); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.appendAll("foo", "bar", "baz"); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendAll_Collection() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendAll((Collection<?>) null); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendAll(Collections.EMPTY_LIST); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendAll(Arrays.asList(new Object[]{"foo", "bar", "baz"})); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendAll_Iterator() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendAll((Iterator<?>) null); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendAll(Collections.EMPTY_LIST.iterator()); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendAll(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator()); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendWithSeparators_Array() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendWithSeparators((Object[]) null, ","); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendWithSeparators(new Object[0], ","); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, ","); + assertThat(sb.toString()).isEqualTo("foo,bar,baz"); + + sb.clear(); + sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, null); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); + assertThat(sb.toString()).isEqualTo("foo,,baz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendWithSeparators_Collection() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendWithSeparators((Collection<?>) null, ","); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendWithSeparators(Collections.EMPTY_LIST, ","); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}), ","); + assertThat(sb.toString()).isEqualTo("foo,bar,baz"); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}), null); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); + assertThat(sb.toString()).isEqualTo("foo,,baz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendWithSeparators_Iterator() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendWithSeparators((Iterator<?>) null, ","); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendWithSeparators(Collections.EMPTY_LIST.iterator(), ","); + assertThat(sb.toString()).isEqualTo(""); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator(), ","); + assertThat(sb.toString()).isEqualTo("foo,bar,baz"); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", "bar", "baz"}).iterator(), null); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}).iterator(), ","); + assertThat(sb.toString()).isEqualTo("foo,,baz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendWithSeparatorsWithNullText() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("null"); + sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ","); + assertThat(sb.toString()).isEqualTo("foo,null,baz"); + + sb.clear(); + sb.appendWithSeparators(Arrays.asList(new Object[]{"foo", null, "baz"}), ","); + assertThat(sb.toString()).isEqualTo("foo,null,baz"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendSeparator_String() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendSeparator(","); // no effect + assertThat(sb.toString()).isEqualTo(""); + sb.append("foo"); + assertThat(sb.toString()).isEqualTo("foo"); + sb.appendSeparator(","); + assertThat(sb.toString()).isEqualTo("foo,"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendSeparator_String_String() { + final TextStringBuilder sb = new TextStringBuilder(); + final String startSeparator = "order by "; + final String standardSeparator = ","; + final String foo = "foo"; + sb.appendSeparator(null, null); + assertThat(sb.toString()).isEqualTo(""); + sb.appendSeparator(standardSeparator, null); + assertThat(sb.toString()).isEqualTo(""); + sb.appendSeparator(standardSeparator, startSeparator); + assertThat(sb.toString()).isEqualTo(startSeparator); + sb.appendSeparator(null, null); + assertThat(sb.toString()).isEqualTo(startSeparator); + sb.appendSeparator(null, startSeparator); + assertThat(sb.toString()).isEqualTo(startSeparator); + sb.append(foo); + assertThat(sb.toString()).isEqualTo(startSeparator + foo); + sb.appendSeparator(standardSeparator, startSeparator); + assertThat(sb.toString()).isEqualTo(startSeparator + foo + standardSeparator); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendSeparator_char() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendSeparator(','); // no effect + assertThat(sb.toString()).isEqualTo(""); + sb.append("foo"); + assertThat(sb.toString()).isEqualTo("foo"); + sb.appendSeparator(','); + assertThat(sb.toString()).isEqualTo("foo,"); + } + @Test + public void testAppendSeparator_char_char() { + final TextStringBuilder sb = new TextStringBuilder(); + final char startSeparator = ':'; + final char standardSeparator = ','; + final String foo = "foo"; + sb.appendSeparator(standardSeparator, startSeparator); // no effect + assertThat(sb.toString()).isEqualTo(String.valueOf(startSeparator)); + sb.append(foo); + assertThat(sb.toString()).isEqualTo(String.valueOf(startSeparator) + foo); + sb.appendSeparator(standardSeparator, startSeparator); + assertThat(sb.toString()).isEqualTo(String.valueOf(startSeparator) + foo + standardSeparator); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendSeparator_String_int() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendSeparator(",", 0); // no effect + assertThat(sb.toString()).isEqualTo(""); + sb.append("foo"); + assertThat(sb.toString()).isEqualTo("foo"); + sb.appendSeparator(",", 1); + assertThat(sb.toString()).isEqualTo("foo,"); + + sb.appendSeparator(",", -1); // no effect + assertThat(sb.toString()).isEqualTo("foo,"); + } + + //----------------------------------------------------------------------- + @Test + public void testAppendSeparator_char_int() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.appendSeparator(',', 0); // no effect + assertThat(sb.toString()).isEqualTo(""); + sb.append("foo"); + assertThat(sb.toString()).isEqualTo("foo"); + sb.appendSeparator(',', 1); + assertThat(sb.toString()).isEqualTo("foo,"); + + sb.appendSeparator(',', -1); // no effect + assertThat(sb.toString()).isEqualTo("foo,"); + } + + //----------------------------------------------------------------------- + @Test + public void testInsert() { + + final TextStringBuilder sb = new TextStringBuilder(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, FOO); + fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, FOO); + fail("insert(7, Object) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (Object) null); + assertThat(sb.toString()).isEqualTo("barbaz"); + + sb.insert(0, FOO); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, "foo"); + fail("insert(-1, String) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, "foo"); + fail("insert(7, String) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (String) null); + assertThat(sb.toString()).isEqualTo("barbaz"); + + sb.insert(0, "foo"); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, new char[]{'f', 'o', 'o'}); + fail("insert(-1, char[]) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, new char[]{'f', 'o', 'o'}); + fail("insert(7, char[]) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (char[]) null); + assertThat(sb.toString()).isEqualTo("barbaz"); + + sb.insert(0, new char[0]); + assertThat(sb.toString()).isEqualTo("barbaz"); + + sb.insert(0, new char[]{'f', 'o', 'o'}); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + 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 (final 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 (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (char[]) null, 0, 0); + assertThat(sb.toString()).isEqualTo("barbaz"); + + sb.insert(0, new char[0], 0, 0); + assertThat(sb.toString()).isEqualTo("barbaz"); + + 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 (final 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 (final 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 (final 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 (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0); + assertThat(sb.toString()).isEqualTo("barbaz"); + + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); + assertThat(sb.toString()).isEqualTo("foobarbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, true); + fail("insert(-1, boolean) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, true); + fail("insert(7, boolean) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, true); + assertThat(sb.toString()).isEqualTo("truebarbaz"); + + sb.insert(0, false); + assertThat(sb.toString()).isEqualTo("falsetruebarbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, '!'); + fail("insert(-1, char) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, '!'); + fail("insert(7, char) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, '!'); + assertThat(sb.toString()).isEqualTo("!barbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, 0); + fail("insert(-1, int) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 0); + fail("insert(7, int) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, '0'); + assertThat(sb.toString()).isEqualTo("0barbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, 1L); + fail("insert(-1, long) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 1L); + fail("insert(7, long) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, 1L); + assertThat(sb.toString()).isEqualTo("1barbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, 2.3F); + fail("insert(-1, float) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 2.3F); + fail("insert(7, float) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, 2.3F); + assertThat(sb.toString()).isEqualTo("2.3barbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, 4.5D); + fail("insert(-1, double) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, 4.5D); + fail("insert(7, double) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, 4.5D); + assertThat(sb.toString()).isEqualTo("4.5barbaz"); + } + + //----------------------------------------------------------------------- + @Test + public void testInsertWithNullText() { + final TextStringBuilder sb = new TextStringBuilder(); + sb.setNullText("null"); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, FOO); + fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, FOO); + fail("insert(7, Object) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (Object) null); + assertThat(sb.toString()).isEqualTo("nullbarbaz"); + + sb.insert(0, FOO); + assertThat(sb.toString()).isEqualTo("foonullbarbaz"); + + sb.clear(); + sb.append("barbaz"); + assertThat(sb.toString()).isEqualTo("barbaz"); + + try { + sb.insert(-1, "foo"); + fail("insert(-1, String) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + try { + sb.insert(7, "foo"); + fail("insert(7, String) expected StringIndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // expected + } + + sb.insert(0, (String) null); + assertThat(sb.toString()).isEqualTo("nullbarbaz"); + + sb.insert(0, "foo"); + assertThat(sb.toString()).isEqualTo("foonullbarbaz"); + + sb.insert(0, (char[]) null); + assertThat(sb.toString()).isEqualTo("nullfoonullbarbaz"); + + sb.insert(0, (char[]) null, 0, 0); + assertThat(sb.toString()).isEqualTo("nullnullfoonullbarbaz"); + } +}