You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2022/03/25 21:43:18 UTC
[commons-text] branch master updated: (doc) add missing exception javadocs, add a couple more unit tests
This is an automated email from the ASF dual-hosted git repository.
kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new f0d378a (doc) add missing exception javadocs, add a couple more unit tests
new fbe4013 Merge branch 'pr-311'
f0d378a is described below
commit f0d378a90be6949f5bf4c38ef37d57f23159816e
Author: Diego Marcilio <dv...@gmail.com>
AuthorDate: Fri Mar 25 15:50:45 2022 +0100
(doc) add missing exception javadocs, add a couple more unit tests
---
.../org/apache/commons/text/FormattableUtils.java | 4 ++++
.../org/apache/commons/text/StringSubstitutor.java | 8 ++++++++
.../org/apache/commons/text/TextStringBuilder.java | 8 ++++++++
.../text/translate/SinglePassTranslator.java | 4 ++++
.../apache/commons/text/FormattableUtilsTest.java | 8 ++++++++
.../apache/commons/text/StringSubstitutorTest.java | 23 ++++++++++++++++++++++
6 files changed, 55 insertions(+)
diff --git a/src/main/java/org/apache/commons/text/FormattableUtils.java b/src/main/java/org/apache/commons/text/FormattableUtils.java
index 62a2380..642a141 100644
--- a/src/main/java/org/apache/commons/text/FormattableUtils.java
+++ b/src/main/java/org/apache/commons/text/FormattableUtils.java
@@ -86,6 +86,8 @@ public class FormattableUtils {
* @param ellipsis the ellipsis to use when precision dictates truncation, null or
* empty causes a hard truncation
* @return The {@code formatter} instance, not null
+ * @throws IllegalArgumentException if {@code ellipsis.length() > precision},
+ * given that {@code ellipsis} is not null and {@code precision >= 0}
*/
public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
final int precision, final char padChar, final CharSequence ellipsis) {
@@ -125,6 +127,8 @@ public class FormattableUtils {
* @param ellipsis the ellipsis to use when precision dictates truncation, null or
* empty causes a hard truncation
* @return The {@code formatter} instance, not null
+ * @throws IllegalArgumentException if {@code ellipsis.length() > precision},
+ * given that {@code ellipsis} is not null and {@code precision >= 0}
*/
public static Formatter append(final CharSequence seq, final Formatter formatter, final int flags, final int width,
final int precision, final CharSequence ellipsis) {
diff --git a/src/main/java/org/apache/commons/text/StringSubstitutor.java b/src/main/java/org/apache/commons/text/StringSubstitutor.java
index 5502943..71fb5c4 100644
--- a/src/main/java/org/apache/commons/text/StringSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StringSubstitutor.java
@@ -734,6 +734,10 @@ public class StringSubstitutor {
* @param length the length within the array to be processed, must be valid
* @return The result of the replace operation
* @throws IllegalArgumentException if variable is not found when its allowed to throw exception
+ * @throws StringIndexOutOfBoundsException if {@code offset} is not in the
+ * range {@code 0 <= offset <= chars.length}
+ * @throws StringIndexOutOfBoundsException if {@code length < 0}
+ * @throws StringIndexOutOfBoundsException if {@code offset + length > chars.length}
*/
public String replace(final char[] source, final int offset, final int length) {
if (source == null) {
@@ -831,6 +835,10 @@ public class StringSubstitutor {
* @param length the length within the source to be processed, must be valid
* @return The result of the replace operation
* @throws IllegalArgumentException if variable is not found when its allowed to throw exception
+ * @throws StringIndexOutOfBoundsException if {@code offset} is not in the
+ * range {@code 0 <= offset <= source.length()}
+ * @throws StringIndexOutOfBoundsException if {@code length < 0}
+ * @throws StringIndexOutOfBoundsException if {@code offset + length > source.length()}
*/
public String replace(final String source, final int offset, final int length) {
if (source == null) {
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index cbf9b22..1d3186f 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -440,6 +440,10 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @param startIndex the start index, inclusive, must be valid
* @param length the length to append, must be valid
* @return this, to enable chaining
+ * @throws StringIndexOutOfBoundsException if {@code startIndex} is not in the
+ * range {@code 0 <= startIndex <= chars.length}
+ * @throws StringIndexOutOfBoundsException if {@code length < 0}
+ * @throws StringIndexOutOfBoundsException if {@code startIndex + length > chars.length}
*/
public TextStringBuilder append(final char[] chars, final int startIndex, final int length) {
if (chars == null) {
@@ -621,6 +625,10 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @param startIndex the start index, inclusive, must be valid
* @param length the length to append, must be valid
* @return this, to enable chaining
+ * @throws StringIndexOutOfBoundsException if {@code startIndex} is not in the
+ * range {@code 0 <= startIndex <= str.length()}
+ * @throws StringIndexOutOfBoundsException if {@code length < 0}
+ * @throws StringIndexOutOfBoundsException if {@code startIndex + length > str.length()}
*/
public TextStringBuilder append(final String str, final int startIndex, final int length) {
if (str == null) {
diff --git a/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
index db82bdb..dd19ab5 100644
--- a/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
+++ b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
@@ -35,6 +35,10 @@ abstract class SinglePassTranslator extends CharSequenceTranslator {
return clazz.isAnonymousClass() ? clazz.getName() : clazz.getSimpleName();
}
+ /**
+ * {@inheritDoc}
+ * @throws IllegalArgumentException if {@code index != 0}
+ */
@Override
public int translate(final CharSequence input, final int index, final Writer writer) throws IOException {
if (index != 0) {
diff --git a/src/test/java/org/apache/commons/text/FormattableUtilsTest.java b/src/test/java/org/apache/commons/text/FormattableUtilsTest.java
index 4dda6e6..8e68936 100644
--- a/src/test/java/org/apache/commons/text/FormattableUtilsTest.java
+++ b/src/test/java/org/apache/commons/text/FormattableUtilsTest.java
@@ -161,6 +161,14 @@ public class FormattableUtilsTest {
}
@Test
+ public void testIllegalEllipsisWith7Args() {
+ String ellipsis = "xxxx";
+ int precisionLessThanEllipsisLength = ellipsis.length() - 1;
+ assertThatIllegalArgumentException().isThrownBy(() -> FormattableUtils.append("foo", createFormatter(), 0, 0,
+ precisionLessThanEllipsisLength, '}', ellipsis));
+ }
+
+ @Test
public void testPublicConstructorExists() {
new FormattableUtils();
}
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
index 25a5ad5..2dbe6e9 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
@@ -17,6 +17,7 @@
package org.apache.commons.text;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -1085,4 +1086,26 @@ public class StringSubstitutorTest {
assertEqualsCharSeq("value $${escaped}", replace(sub, org));
}
+ @Test
+ public void testReplaceThrowsStringIndexOutOfBoundsException() {
+ final StringSubstitutor sub = new StringSubstitutor();
+
+ // replace(char[], int, int)
+ final char[] emptyCharArray = new char[] {};
+ // offset greater than array length
+ assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(
+ () -> sub.replace(emptyCharArray, 0, 1));
+ // source != null && (offset > source.length || offset < 0)
+ assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(
+ () -> sub.replace(emptyCharArray, 1, 0));
+
+ // replace(String, int, int)
+ // offset greater than source length
+ assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(
+ () -> sub.replace("", 1, 1));
+ // source != null && offset >= 0 && offset <= source.length() && (length > -offset + source.length() || length < 0)
+ assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(
+ () -> sub.replace("", 0, 1));
+ }
+
}