You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/07/02 13:03:52 UTC
[commons-text] branch master updated (a6eead2 -> 0bb289b)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git.
from a6eead2 Javadoc.
new 84f0b2e Add org.apache.commons.text.TextStringBuilder.isReallocated().
new a14cca1 Simplify by reusing own methods.
new 0bb289b Better tests.
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 1 +
.../org/apache/commons/text/TextStringBuilder.java | 94 ++++++-----------
.../apache/commons/text/StringSubstitutorTest.java | 85 ++++++++--------
.../apache/commons/text/TextStringBuilderTest.java | 112 +++++++++------------
4 files changed, 126 insertions(+), 166 deletions(-)
[commons-text] 01/03: Add
org.apache.commons.text.TextStringBuilder.isReallocated().
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
commit 84f0b2e1a5f46cdc141758dcc4900f63573dc742
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 2 08:13:18 2020 -0400
Add org.apache.commons.text.TextStringBuilder.isReallocated().
---
src/changes/changes.xml | 1 +
.../org/apache/commons/text/TextStringBuilder.java | 28 +++++-
.../apache/commons/text/TextStringBuilderTest.java | 112 +++++++++------------
3 files changed, 74 insertions(+), 67 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6a50798..7ac4643 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,6 +65,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.wrap(char[]).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.wrap(char[], int).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.StringSubstitutor.StringSubstitutor(StringSubstitutor).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.isReallocated().</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.34.</action>
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 0aae728..818a328 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -313,6 +313,9 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
/** The null text. */
private String nullText;
+ /** Incremented when the buffer is reallocated. */
+ private int reallocations;
+
/** Current size of the buffer. */
private int size;
@@ -1849,7 +1852,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
*/
public TextStringBuilder ensureCapacity(final int capacity) {
if (capacity > buffer.length) {
- buffer = Arrays.copyOf(buffer, capacity * 2);
+ reallocate(capacity * 2);
}
return this;
}
@@ -2289,6 +2292,16 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
}
/**
+ * Gets whether the internal buffer has been reallocated.
+ *
+ * @return Whether the internal buffer has been reallocated.
+ * @since 1.9
+ */
+ public boolean isReallocated() {
+ return reallocations > 0;
+ }
+
+ /**
* Searches the string builder to find the last reference to the specified char.
*
* @param ch the character to find
@@ -2470,11 +2483,10 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
*/
public TextStringBuilder minimizeCapacity() {
if (buffer.length > size) {
- buffer = Arrays.copyOf(buffer, size);
+ reallocate(size);
}
return this;
}
-
/**
* If possible, reads chars from the provided {@link CharBuffer} directly into underlying character buffer without
* making extra copies.
@@ -2572,6 +2584,16 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
}
/**
+ * Reallocates the buffer to the new length.
+ *
+ * @param newLength the length of the copy to be returned
+ */
+ private void reallocate(final int newLength) {
+ this.buffer = Arrays.copyOf(buffer, newLength);
+ this.reallocations++;
+ }
+
+ /**
* Replaces a portion of the string builder with another string. The length of the inserted string does not have to
* match the removed length.
*
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index 0ebf3f7..15b3b46 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -80,7 +80,6 @@ public class TextStringBuilderTest {
assertFalse(sb.equals(other));
}
- // -----------------------------------------------------------------------
@Test
public void testAppendCharBuffer() {
final TextStringBuilder sb1 = new TextStringBuilder();
@@ -230,7 +229,6 @@ public class TextStringBuilderTest {
assertEquals("Test 1234567890", builder.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testAppendToWriter() throws Exception {
final TextStringBuilder sb = new TextStringBuilder("1234567890");
@@ -248,7 +246,6 @@ public class TextStringBuilderTest {
assertEquals(sb.toString(), sb.build());
}
- // -----------------------------------------------------------------------
@Test
public void testAsReader() throws Exception {
final TextStringBuilder sb = new TextStringBuilder("some text");
@@ -332,7 +329,6 @@ public class TextStringBuilderTest {
}
}
- // -----------------------------------------------------------------------
@Test
public void testAsTokenizer() throws Exception {
// from Javadoc
@@ -371,7 +367,6 @@ public class TextStringBuilderTest {
assertEquals("a b c d ", t.getContent());
}
- // -----------------------------------------------------------------------
@Test
public void testAsWriter() throws Exception {
final TextStringBuilder sb = new TextStringBuilder("base");
@@ -407,7 +402,6 @@ public class TextStringBuilderTest {
}
}
- // -----------------------------------------------------------------------
@Test
public void testCapacity() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -417,7 +411,6 @@ public class TextStringBuilderTest {
assertEquals(sb.getBuffer().length, sb.capacity());
}
- // -----------------------------------------------------------------------
@Test
public void testCapacityAndLength() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -497,7 +490,6 @@ public class TextStringBuilderTest {
assertTrue(sb.isEmpty());
}
- // -----------------------------------------------------------------------
@Test
public void testChaining() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -512,7 +504,6 @@ public class TextStringBuilderTest {
assertSame(sb, sb.trim());
}
- // -----------------------------------------------------------------------
@Test
public void testCharAt() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -568,7 +559,6 @@ public class TextStringBuilderTest {
assertEquals(0, sb.toCharArray().length);
}
- // -----------------------------------------------------------------------
@Test
public void testConstructors() {
final TextStringBuilder sb0 = new TextStringBuilder();
@@ -621,7 +611,6 @@ public class TextStringBuilderTest {
assertEquals(length, sb.toCharArray().length);
}
- // -----------------------------------------------------------------------
@Test
public void testContains_char() {
final TextStringBuilder sb = new TextStringBuilder("abcdefghijklmnopqrstuvwxyz");
@@ -656,7 +645,6 @@ public class TextStringBuilderTest {
assertTrue(sb.contains(A_NUMBER_MATCHER));
}
- // -----------------------------------------------------------------------
@Test
public void testDeleteAll_char() {
TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -674,7 +662,6 @@ public class TextStringBuilderTest {
assertEquals("", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testDeleteAll_String() {
TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -701,7 +688,6 @@ public class TextStringBuilderTest {
assertEquals("", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testDeleteAll_StringMatcher() {
TextStringBuilder sb = new TextStringBuilder("A0xA1A2yA3");
@@ -803,7 +789,6 @@ public class TextStringBuilderTest {
assertEquals("", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testDeleteIntInt() {
final TextStringBuilder sb = new TextStringBuilder("abc");
@@ -853,7 +838,6 @@ public class TextStringBuilderTest {
assertTrue(sb.capacity() >= 40);
}
- // -----------------------------------------------------------------------
@Test
public void testEquals() {
final TextStringBuilder sb1 = new TextStringBuilder();
@@ -879,7 +863,6 @@ public class TextStringBuilderTest {
assertFalse(sb1.equals("abc"));
}
- // -----------------------------------------------------------------------
@Test
public void testEqualsIgnoreCase() {
final TextStringBuilder sb1 = new TextStringBuilder();
@@ -973,7 +956,6 @@ public class TextStringBuilderTest {
}
}
- // -----------------------------------------------------------------------
@Test
public void testGetSetNewLineText() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -989,7 +971,6 @@ public class TextStringBuilderTest {
assertNull(sb.getNewLineText());
}
- // -----------------------------------------------------------------------
@Test
public void testGetSetNullText() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -1008,7 +989,6 @@ public class TextStringBuilderTest {
assertNull(sb.getNullText());
}
- // -----------------------------------------------------------------------
@Test
public void testHashCode() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -1025,7 +1005,6 @@ public class TextStringBuilderTest {
assertEquals(hc2a, hc2b);
}
- // -----------------------------------------------------------------------
@Test
public void testIndexOf_char() {
final TextStringBuilder sb = new TextStringBuilder("abab");
@@ -1062,7 +1041,6 @@ public class TextStringBuilderTest {
assertEquals(-1, sb.indexOf('z', 3));
}
- // -----------------------------------------------------------------------
@Test
public void testIndexOf_String() {
final TextStringBuilder sb = new TextStringBuilder("abab");
@@ -1123,7 +1101,6 @@ public class TextStringBuilderTest {
assertEquals(-1, sb.indexOf((String) null, 2));
}
- // -----------------------------------------------------------------------
@Test
public void testIndexOf_StringMatcher() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -1201,7 +1178,6 @@ public class TextStringBuilderTest {
assertTrue(sb.isEmpty());
}
- // -----------------------------------------------------------------------
@Test
public void testLang294() {
final TextStringBuilder sb = new TextStringBuilder("\n%BLAH%\nDo more stuff\neven more stuff\n%BLAH%\n");
@@ -1209,7 +1185,6 @@ public class TextStringBuilderTest {
assertEquals("\nDo more stuff\neven more stuff\n", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testLang295() {
final TextStringBuilder sb = new TextStringBuilder("onetwothree");
@@ -1225,7 +1200,6 @@ public class TextStringBuilderTest {
assertEquals("**********", sb.toString(), "Failed to invoke appendFixedWidthPadLeft correctly");
}
- // -----------------------------------------------------------------------
@Test
public void testLang412Right() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -1399,7 +1373,6 @@ public class TextStringBuilderTest {
assertEquals("left right", sb.leftString(15));
}
- // -----------------------------------------------------------------------
@Test
public void testLength() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -1409,7 +1382,6 @@ public class TextStringBuilderTest {
assertEquals(5, sb.length());
}
- // -----------------------------------------------------------------------
@Test
public void testMidString() {
final TextStringBuilder sb = new TextStringBuilder("hello goodbye hello");
@@ -1557,7 +1529,6 @@ public class TextStringBuilderTest {
assertEquals(source.substring(0, sourceLen), sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReplace_int_int_String() {
TextStringBuilder sb = new TextStringBuilder("abc");
@@ -1686,7 +1657,6 @@ public class TextStringBuilderTest {
assertEquals("aaxaaaayaa", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReplace_StringMatcher_String_int_int_int_VaryMatcher() {
TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -1796,7 +1766,6 @@ public class TextStringBuilderTest {
assertEquals("aaxaaaayaa", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReplaceAll_char_char() {
final TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -1812,7 +1781,6 @@ public class TextStringBuilderTest {
assertEquals("defeffed", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReplaceAll_String_String() {
TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -1843,7 +1811,6 @@ public class TextStringBuilderTest {
assertEquals("xbxxbx", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReplaceAll_StringMatcher_String() {
TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -1882,7 +1849,6 @@ public class TextStringBuilderTest {
assertEquals("Dear 012345678901234567, hello 012345678901234567.", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReplaceFirst_char_char() {
final TextStringBuilder sb = new TextStringBuilder("abcbccba");
@@ -1962,7 +1928,6 @@ public class TextStringBuilderTest {
assertEquals("***-A2A3-A4", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReverse() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -1982,7 +1947,6 @@ public class TextStringBuilderTest {
assertEquals("left right", sb.rightString(15));
}
- // -----------------------------------------------------------------------
@Test
public void testSetCharAt() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2010,7 +1974,6 @@ public class TextStringBuilderTest {
assertThrows(IndexOutOfBoundsException.class, () -> sb.setLength(-1));
}
- // -----------------------------------------------------------------------
@Test
public void testSize() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2020,7 +1983,6 @@ public class TextStringBuilderTest {
assertEquals(5, sb.size());
}
- // -----------------------------------------------------------------------
@Test
public void testStartsWith() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2034,7 +1996,6 @@ public class TextStringBuilderTest {
assertFalse(sb.startsWith("cba"));
}
- // -----------------------------------------------------------------------
@Test
public void testSubSequenceIntInt() {
final TextStringBuilder sb = new TextStringBuilder("hello goodbye");
@@ -2086,7 +2047,6 @@ public class TextStringBuilderTest {
assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15, 20));
}
- // -----------------------------------------------------------------------
@Test
public void testToCharArray() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2128,14 +2088,12 @@ public class TextStringBuilderTest {
assertThrows(IndexOutOfBoundsException.class, () -> sb.toCharArray(6, 5));
}
- // -----------------------------------------------------------------------
@Test
public void testToString() {
final TextStringBuilder sb = new TextStringBuilder("abc");
assertEquals("abc", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testToStringBuffer() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2145,7 +2103,6 @@ public class TextStringBuilderTest {
assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString());
}
- // -----------------------------------------------------------------------
@Test
public void testToStringBuilder() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2201,36 +2158,63 @@ public class TextStringBuilderTest {
assertEquals(1, initEmpty.length());
//
final char[] test = "abc".toCharArray();
- final TextStringBuilder sb = TextStringBuilder.wrap(test);
- assertArrayEquals(test, sb.getBuffer());
- assertEquals(test.length, sb.length());
- assertEquals(test.length, sb.size());
- sb.ensureCapacity(sb.capacity() * 2);
- assertFalse(Arrays.equals(test, sb.getBuffer()));
+ final TextStringBuilder builder = TextStringBuilder.wrap(test);
+ assertArrayEquals(test, builder.getBuffer());
+ assertEquals(test.length, builder.length());
+ assertEquals(test.length, builder.size());
+ assertFalse(builder.isReallocated());
+ builder.ensureCapacity(builder.capacity() * 2);
+ assertFalse(Arrays.equals(test, builder.getBuffer()));
+ assertTrue(builder.isReallocated());
+ }
+
+ private void testWrap_CharArray_Int(final String test, final int expectedLen) {
+ final char[] input = "abc".toCharArray();
+ final TextStringBuilder builder = TextStringBuilder.wrap(input, expectedLen);
+ assertArrayEquals(input, builder.getBuffer());
+ assertEquals(expectedLen, builder.length());
+ assertEquals(expectedLen, builder.size());
+ assertFalse(builder.isReallocated());
+ builder.ensureCapacity(builder.capacity() * 2);
+ assertFalse(Arrays.equals(input, builder.getBuffer()));
+ assertTrue(builder.isReallocated());
}
@Test
- public void testWrap_CharArray_Int_at0() {
- final char[] test = "abc".toCharArray();
- //
- assertThrows(NullPointerException.class, () -> TextStringBuilder.wrap(null, 0));
- assertThrows(IllegalArgumentException.class, () -> TextStringBuilder.wrap(test, -1));
- //
- assertThrows(IllegalArgumentException.class, () -> TextStringBuilder.wrap(ArrayUtils.EMPTY_CHAR_ARRAY, 1));
- //
+ public void testWrap_CharArray_Int_0() {
+ testWrap_CharArray_Int("abc", 0);
+ }
+
+ @Test
+ public void testWrap_CharArray_Int_1() {
+ testWrap_CharArray_Int("abc", 1);
+ }
+
+ @Test
+ public void testWrap_CharArray_Int_2() {
+ testWrap_CharArray_Int("abc", 2);
+ }
+
+ @Test
+ public void testWrap_CharArray_Int_3() {
+ testWrap_CharArray_Int("abc", 3);
+ }
+
+ @Test
+ public void testWrap_CharArray_Int_Empty_0() {
final TextStringBuilder initEmpty = TextStringBuilder.wrap(ArrayUtils.EMPTY_CHAR_ARRAY, 0);
assertEquals(0, initEmpty.size());
assertEquals(0, initEmpty.length());
initEmpty.append('a');
assertEquals(1, initEmpty.size());
assertEquals(1, initEmpty.length());
- //
- final TextStringBuilder sb = TextStringBuilder.wrap(test, test.length);
- assertArrayEquals(test, sb.getBuffer());
- assertEquals(test.length, sb.length());
- assertEquals(test.length, sb.size());
- sb.ensureCapacity(sb.capacity() * 2);
- assertFalse(Arrays.equals(test, sb.getBuffer()));
+ }
+
+ @Test
+ public void testWrap_CharArray_Int_Exceptions() {
+ assertThrows(NullPointerException.class, () -> TextStringBuilder.wrap(null, 0));
+ assertThrows(IllegalArgumentException.class, () -> TextStringBuilder.wrap("abc".toCharArray(), -1));
+ assertThrows(IllegalArgumentException.class, () -> TextStringBuilder.wrap(ArrayUtils.EMPTY_CHAR_ARRAY, 1));
}
}
[commons-text] 03/03: Better tests.
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
commit 0bb289b0994467863ff99876197a00630f5df424
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 2 09:03:11 2020 -0400
Better tests.
---
.../apache/commons/text/StringSubstitutorTest.java | 85 ++++++++++++----------
1 file changed, 45 insertions(+), 40 deletions(-)
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
index 327d466..dc14b11 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
@@ -55,6 +55,11 @@ public class StringSubstitutorTest {
private static final String EMPTY_EXPR = "${}";
protected Map<String, String> values;
+ private void assertEqualsCharSeq(final CharSequence expected, final CharSequence actual) {
+ assertEquals(expected, actual, () -> String.format("expected.length()=%,d, actual.length()=%,d",
+ StringUtils.length(expected), StringUtils.length(actual)));
+ }
+
protected void doTestNoReplace(final String replaceTemplate) throws IOException {
doTestNoReplace(new StringSubstitutor(values), replaceTemplate);
}
@@ -243,7 +248,7 @@ public class StringSubstitutorTest {
values.put("code", "GBP");
values.put("amount", "12.50");
final StringSubstitutor sub = new StringSubstitutor(values);
- assertEquals("Amount is GBP12.50", replace(sub, "Amount is ${code}${amount}"));
+ assertEqualsCharSeq("Amount is GBP12.50", replace(sub, "Amount is ${code}${amount}"));
}
/**
@@ -254,7 +259,7 @@ public class StringSubstitutorTest {
values.put("code", "GBP");
values.put("amount", "12.50");
final StringSubstitutor sub = new StringSubstitutor(values);
- assertEquals("GBP12.50 charged", replace(sub, "${code}${amount} charged"));
+ assertEqualsCharSeq("GBP12.50 charged", replace(sub, "${code}${amount} charged"));
}
/**
@@ -265,10 +270,10 @@ public class StringSubstitutorTest {
final StringSubstitutor sub = new StringSubstitutor(values);
// no map change
final String template = CLASSIC_TEMPLATE;
- assertEquals(CLASSIC_RESULT, replace(sub, template));
+ assertEqualsCharSeq(CLASSIC_RESULT, replace(sub, template));
// map change
values.put("target", "moon");
- assertEquals("The quick brown fox jumps over the moon.", replace(sub, template));
+ assertEqualsCharSeq("The quick brown fox jumps over the moon.", replace(sub, template));
}
/**
@@ -301,8 +306,7 @@ public class StringSubstitutorTest {
*/
@Test
public void testReplaceEmptyKeyExtraFirst() throws IOException {
- final String expected = "." + EMPTY_EXPR;
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq("." + EMPTY_EXPR, replace(new StringSubstitutor(values), "." + EMPTY_EXPR));
}
/**
@@ -310,8 +314,7 @@ public class StringSubstitutorTest {
*/
@Test
public void testReplaceEmptyKeyExtraLast() throws IOException {
- final String expected = EMPTY_EXPR + ".";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(EMPTY_EXPR + ".", replace(new StringSubstitutor(values), EMPTY_EXPR + "."));
}
/**
@@ -378,10 +381,11 @@ public class StringSubstitutorTest {
.withMessage("Cannot resolve variable 'unknown' (enableSubstitutionInVariables=false).");
// if default value is available, exception will not be thrown
- assertEquals("The statement is a sample for missing variable.",
+ assertEqualsCharSeq("The statement is a sample for missing variable.",
replace(sub, "The ${test:-statement} is a sample for missing ${unknown:-variable}."));
- assertEquals("The fox jumps over the lazy dog.", replace(sub, "The ${animal.1} jumps over the ${target}."));
+ assertEqualsCharSeq("The fox jumps over the lazy dog.",
+ replace(sub, "The ${animal.1} jumps over the ${target}."));
}
/**
@@ -400,10 +404,10 @@ public class StringSubstitutorTest {
sub.setEnableUndefinedVariableException(true);
sub.setEnableSubstitutionInVariables(true);
- assertEquals("The mouse jumps over the lazy dog.",
+ assertEqualsCharSeq("The mouse jumps over the lazy dog.",
replace(sub, "The ${animal.${species}} jumps over the ${target}."));
values.put("species", "1");
- assertEquals("The fox jumps over the lazy dog.",
+ assertEqualsCharSeq("The fox jumps over the lazy dog.",
replace(sub, "The ${animal.${species}} jumps over the ${target}."));
// exception is thrown here because variable with name test.1 is missing
@@ -416,7 +420,7 @@ public class StringSubstitutorTest {
.isThrownBy(() -> replace(sub, "The ${test.${statement.${recursive}}} is a sample for missing ${word}."))
.withMessage("Cannot resolve variable 'test.2' (enableSubstitutionInVariables=true).");
- assertEquals("The statement is a sample for missing variable.",
+ assertEqualsCharSeq("The statement is a sample for missing variable.",
replace(sub, "The ${testok.${statement.${recursive}}} is a sample for missing ${word}."));
}
@@ -480,12 +484,12 @@ public class StringSubstitutorTest {
values.put("species", "2");
final StringSubstitutor sub = new StringSubstitutor(values);
sub.setEnableSubstitutionInVariables(true);
- assertEquals("The mouse jumps over the lazy dog.",
+ assertEqualsCharSeq("The mouse jumps over the lazy dog.",
replace(sub, "The ${animal.${species}} jumps over the ${target}."));
values.put("species", "1");
- assertEquals("The fox jumps over the lazy dog.",
+ assertEqualsCharSeq("The fox jumps over the lazy dog.",
replace(sub, "The ${animal.${species}} jumps over the ${target}."));
- assertEquals("The fox jumps over the lazy dog.", replace(sub,
+ assertEqualsCharSeq("The fox jumps over the lazy dog.", replace(sub,
"The ${unknown.animal.${unknown.species:-1}:-fox} " + "jumps over the ${unknow.target:-lazy dog}."));
}
@@ -498,9 +502,9 @@ public class StringSubstitutorTest {
values.put("animal.2", "mouse");
values.put("species", "2");
final StringSubstitutor sub = new StringSubstitutor(values);
- assertEquals("The ${animal.${species}} jumps over the lazy dog.",
+ assertEqualsCharSeq("The ${animal.${species}} jumps over the lazy dog.",
replace(sub, "The ${animal.${species}} jumps over the ${target}."));
- assertEquals("The ${animal.${species:-1}} jumps over the lazy dog.",
+ assertEqualsCharSeq("The ${animal.${species:-1}} jumps over the lazy dog.",
replace(sub, "The ${animal.${species:-1}} jumps over the ${target}."));
}
@@ -516,10 +520,10 @@ public class StringSubstitutorTest {
values.put("species.brown", "2");
final StringSubstitutor sub = new StringSubstitutor(values);
sub.setEnableSubstitutionInVariables(true);
- assertEquals("white mouse", replace(sub, "${animal.${species.${color}}}"));
- assertEquals("The white mouse jumps over the lazy dog.",
+ assertEqualsCharSeq("white mouse", replace(sub, "${animal.${species.${color}}}"));
+ assertEqualsCharSeq("The white mouse jumps over the lazy dog.",
replace(sub, "The ${animal.${species.${color}}} jumps over the ${target}."));
- assertEquals("The brown fox jumps over the lazy dog.",
+ assertEqualsCharSeq("The brown fox jumps over the lazy dog.",
replace(sub, "The ${animal.${species.${unknownColor:-brown}}} jumps over the ${target}."));
}
@@ -529,7 +533,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceKeyStartChars() throws IOException {
final String substring = StringSubstitutor.DEFAULT_VAR_START + "a";
- assertEquals(substring, replace(new StringSubstitutor(values), substring));
+ assertEqualsCharSeq(substring, replace(new StringSubstitutor(values), substring));
}
/**
@@ -538,7 +542,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceKeyStartChars1Only() throws IOException {
final String substring = StringSubstitutor.DEFAULT_VAR_START.substring(0, 1);
- assertEquals(substring, replace(new StringSubstitutor(values), substring));
+ assertEqualsCharSeq(substring, replace(new StringSubstitutor(values), substring));
}
/**
@@ -547,7 +551,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceKeyStartChars2Only() throws IOException {
final String substring = StringSubstitutor.DEFAULT_VAR_START.substring(0, 2);
- assertEquals(substring, replace(new StringSubstitutor(values), substring));
+ assertEqualsCharSeq(substring, replace(new StringSubstitutor(values), substring));
}
/**
@@ -588,7 +592,7 @@ public class StringSubstitutorTest {
@Test
public void testReplacePartialString_noReplace() {
final StringSubstitutor sub = new StringSubstitutor();
- assertEquals("${animal} jumps", sub.replace(CLASSIC_TEMPLATE, 4, 15));
+ assertEqualsCharSeq("${animal} jumps", sub.replace(CLASSIC_TEMPLATE, 4, 15));
}
/**
@@ -674,7 +678,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceUnknownKeyOnly() throws IOException {
final String expected = "${person}";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(expected, replace(new StringSubstitutor(values), expected));
}
/**
@@ -683,7 +687,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceUnknownKeyOnlyExtraFirst() throws IOException {
final String expected = ".${person}";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(expected, replace(new StringSubstitutor(values), expected));
}
/**
@@ -692,7 +696,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceUnknownKeyOnlyExtraLast() throws IOException {
final String expected = "${person}.";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(expected, replace(new StringSubstitutor(values), expected));
}
/**
@@ -701,7 +705,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceUnknownShortestKeyOnly() throws IOException {
final String expected = "${U}";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(expected, replace(new StringSubstitutor(values), expected));
}
/**
@@ -710,7 +714,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceUnknownShortestKeyOnlyExtraFirst() throws IOException {
final String expected = ".${U}";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(expected, replace(new StringSubstitutor(values), expected));
}
/**
@@ -719,7 +723,7 @@ public class StringSubstitutorTest {
@Test
public void testReplaceUnknownShortestKeyOnlyExtraLast() throws IOException {
final String expected = "${U}.";
- assertEquals(expected, replace(new StringSubstitutor(values), expected));
+ assertEqualsCharSeq(expected, replace(new StringSubstitutor(values), expected));
}
/**
@@ -857,7 +861,7 @@ public class StringSubstitutorTest {
}
};
sub.replaceIn(builder);
- assertEquals("Hi jakarta!", builder.toString());
+ assertEqualsCharSeq("Hi jakarta!", builder.toString());
}
@Test
@@ -866,8 +870,9 @@ public class StringSubstitutorTest {
map.put("greeting", "Hello");
map.put(" there ", "XXX");
map.put("name", "commons");
- assertEquals("Hi commons!", StringSubstitutor.replace("Hi @name@!", map, "@", "@"));
- assertEquals("Hello there commons!", StringSubstitutor.replace("@greeting@ there @name@!", map, "@", "@"));
+ assertEqualsCharSeq("Hi commons!", StringSubstitutor.replace("Hi @name@!", map, "@", "@"));
+ assertEqualsCharSeq("Hello there commons!",
+ StringSubstitutor.replace("@greeting@ there @name@!", map, "@", "@"));
}
/**
@@ -877,7 +882,7 @@ public class StringSubstitutorTest {
public void testStaticReplace() {
final Map<String, String> map = new HashMap<>();
map.put("name", "commons");
- assertEquals("Hi commons!", StringSubstitutor.replace("Hi ${name}!", map));
+ assertEqualsCharSeq("Hi commons!", StringSubstitutor.replace("Hi ${name}!", map));
}
/**
@@ -887,7 +892,7 @@ public class StringSubstitutorTest {
public void testStaticReplacePrefixSuffix() {
final Map<String, String> map = new HashMap<>();
map.put("name", "commons");
- assertEquals("Hi commons!", StringSubstitutor.replace("Hi <name>!", map, "<", ">"));
+ assertEqualsCharSeq("Hi commons!", StringSubstitutor.replace("Hi <name>!", map, "<", ">"));
}
/**
@@ -901,7 +906,7 @@ public class StringSubstitutorTest {
buf.append(System.getProperty("os.name"));
buf.append(", your home directory is ");
buf.append(System.getProperty("user.home")).append('.');
- assertEquals(buf.toString(), StringSubstitutor.replaceSystemProperties(
+ assertEqualsCharSeq(buf.toString(), StringSubstitutor.replaceSystemProperties(
"Hi ${user.name}, you are " + "working with ${os.name}, your home " + "directory is ${user.home}."));
}
@@ -916,7 +921,7 @@ public class StringSubstitutorTest {
// create a new Properties object with the System.getProperties as default
final Properties props = new Properties(System.getProperties());
- assertEquals("It works!", StringSubstitutor.replace(org, props));
+ assertEqualsCharSeq("It works!", StringSubstitutor.replace(org, props));
}
@Test
@@ -927,11 +932,11 @@ public class StringSubstitutorTest {
final StringSubstitutor sub = new StringSubstitutor(map, "${", "}", '$');
assertFalse(sub.isPreserveEscapes());
- assertEquals("value ${escaped}", replace(sub, org));
+ assertEqualsCharSeq("value ${escaped}", replace(sub, org));
sub.setPreserveEscapes(true);
assertTrue(sub.isPreserveEscapes());
- assertEquals("value $${escaped}", replace(sub, org));
+ assertEqualsCharSeq("value $${escaped}", replace(sub, org));
}
}
[commons-text] 02/03: Simplify by reusing own methods.
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
commit a14cca1f7bd51ec11b5ef3dd805a05dfc76de236
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 2 08:44:11 2020 -0400
Simplify by reusing own methods.
---
.../org/apache/commons/text/TextStringBuilder.java | 66 +++-------------------
1 file changed, 7 insertions(+), 59 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 818a328..b1646e3 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -459,23 +459,11 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
/**
* Appends the contents of a char buffer to this string builder. Appending null will call {@link #appendNull()}.
*
- * @param buf the char buffer to append
+ * @param str the char buffer to append
* @return this, to enable chaining
*/
- public TextStringBuilder append(final CharBuffer buf) {
- if (buf == null) {
- return appendNull();
- }
- if (buf.hasArray()) {
- final int length = buf.remaining();
- final int len = length();
- ensureCapacity(len + length);
- System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), buffer, len, length);
- size += length;
- } else {
- append(buf.toString());
- }
- return this;
+ public TextStringBuilder append(final CharBuffer str) {
+ return append(str, 0, StringUtils.length(str));
}
/**
@@ -619,17 +607,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @return this, to enable chaining
*/
public TextStringBuilder append(final String str) {
- if (str == null) {
- return appendNull();
- }
- final int strLen = str.length();
- if (strLen > 0) {
- final int len = length();
- ensureCapacity(len + strLen);
- str.getChars(0, strLen, buffer, len);
- size += strLen;
- }
- return this;
+ return append(str, 0, StringUtils.length(str));
}
/**
@@ -678,17 +656,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @return this, to enable chaining
*/
public TextStringBuilder append(final StringBuffer str) {
- if (str == null) {
- return appendNull();
- }
- final int strLen = str.length();
- if (strLen > 0) {
- final int len = length();
- ensureCapacity(len + strLen);
- str.getChars(0, strLen, buffer, len);
- size += strLen;
- }
- return this;
+ return append(str, 0, StringUtils.length(str));
}
/**
@@ -725,17 +693,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @return this, to enable chaining
*/
public TextStringBuilder append(final StringBuilder str) {
- if (str == null) {
- return appendNull();
- }
- final int strLen = str.length();
- if (strLen > 0) {
- final int len = length();
- ensureCapacity(len + strLen);
- str.getChars(0, strLen, buffer, len);
- size += strLen;
- }
- return this;
+ return append(str, 0, StringUtils.length(str));
}
/**
@@ -772,17 +730,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @return this, to enable chaining
*/
public TextStringBuilder append(final TextStringBuilder str) {
- if (str == null) {
- return appendNull();
- }
- final int strLen = str.length();
- if (strLen > 0) {
- final int len = length();
- ensureCapacity(len + strLen);
- System.arraycopy(str.buffer, 0, buffer, len, strLen);
- size += strLen;
- }
- return this;
+ return append(str, 0, str.length());
}
/**