Repository: commons-text Updated Branches: refs/heads/master d8dc95038 -> 6c3cc5239
Refactor some try/catch assertions to assertThrows Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/6c3cc523 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/6c3cc523 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/6c3cc523 Branch: refs/heads/master Commit: 6c3cc5239c2143256976c9e8a05dd76b31c74baf Parents: d8dc950 Author: Pascal Schumacher <pascalschumac...@gmx.net> Authored: Fri Oct 26 20:42:44 2018 +0200 Committer: Pascal Schumacher <pascalschumac...@gmx.net> Committed: Fri Oct 26 20:42:44 2018 +0200 ---------------------------------------------------------------------- .../text/StrBuilderAppendInsertTest.java | 203 ++++--------------- .../org/apache/commons/text/StrBuilderTest.java | 155 +++----------- .../org/apache/commons/text/StrLookupTest.java | 9 +- .../apache/commons/text/StrSubstitutorTest.java | 46 +---- .../apache/commons/text/StrTokenizerTest.java | 38 +--- .../commons/text/StringEscapeUtilsTest.java | 25 +-- .../commons/text/StringTokenizerTest.java | 39 +--- .../text/TextStringBuilderAppendInsertTest.java | 203 ++++--------------- .../commons/text/TextStringBuilderTest.java | 153 +++----------- .../matcher/StringSubstitutorGetSetTest.java | 30 +-- .../text/translate/UnicodeUnescaperTest.java | 13 +- 11 files changed, 166 insertions(+), 748 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StrBuilderAppendInsertTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrBuilderAppendInsertTest.java b/src/test/java/org/apache/commons/text/StrBuilderAppendInsertTest.java index 416e0ed..78b03d9 100644 --- a/src/test/java/org/apache/commons/text/StrBuilderAppendInsertTest.java +++ b/src/test/java/org/apache/commons/text/StrBuilderAppendInsertTest.java @@ -19,6 +19,7 @@ package org.apache.commons.text; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.text.DecimalFormatSymbols; import java.util.Arrays; @@ -1290,19 +1291,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, FOO); - fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, FOO)); - try { - sb.insert(7, FOO); - fail("insert(7, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, FOO)); sb.insert(0, (Object) null); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1314,19 +1305,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, "foo"); - fail("insert(-1, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, "foo")); - try { - sb.insert(7, "foo"); - fail("insert(7, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, "foo")); sb.insert(0, (String) null); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1338,19 +1319,9 @@ public class StrBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, new char[]{'f', 'o', 'o'})); - try { - sb.insert(7, new char[]{'f', 'o', 'o'}); - fail("insert(7, char[]) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, new char[]{'f', 'o', 'o'})); sb.insert(0, (char[]) null); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1365,19 +1336,11 @@ public class StrBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3)); sb.insert(0, (char[]) null, 0, 0); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1385,33 +1348,17 @@ public class StrBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10)); sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1423,19 +1370,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, true); - fail("insert(-1, boolean) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, true)); - try { - sb.insert(7, true); - fail("insert(7, boolean) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, true)); sb.insert(0, true); assertThat(sb.toString()).isEqualTo("truebarbaz"); @@ -1447,19 +1384,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, '!'); - fail("insert(-1, char) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, '!')); - try { - sb.insert(7, '!'); - fail("insert(7, char) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, '!')); sb.insert(0, '!'); assertThat(sb.toString()).isEqualTo("!barbaz"); @@ -1468,19 +1395,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, 0); - fail("insert(-1, int) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 0)); - try { - sb.insert(7, 0); - fail("insert(7, int) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 0)); sb.insert(0, '0'); assertThat(sb.toString()).isEqualTo("0barbaz"); @@ -1489,19 +1406,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, 1L); - fail("insert(-1, long) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 1L)); - try { - sb.insert(7, 1L); - fail("insert(7, long) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 1L)); sb.insert(0, 1L); assertThat(sb.toString()).isEqualTo("1barbaz"); @@ -1510,19 +1417,9 @@ public class StrBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 2.3F)); - try { - sb.insert(7, 2.3F); - fail("insert(7, float) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 2.3F)); sb.insert(0, 2.3F); assertThat(sb.toString()).isEqualTo("2.3barbaz"); @@ -1531,19 +1428,9 @@ public class StrBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 4.5D)); - try { - sb.insert(7, 4.5D); - fail("insert(7, double) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 4.5D)); sb.insert(0, 4.5D); assertThat(sb.toString()).isEqualTo("4.5barbaz"); @@ -1557,19 +1444,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, FOO); - fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, FOO)); - try { - sb.insert(7, FOO); - fail("insert(7, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, FOO)); sb.insert(0, (Object) null); assertThat(sb.toString()).isEqualTo("nullbarbaz"); @@ -1581,19 +1458,9 @@ public class StrBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, "foo"); - fail("insert(-1, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, "foo")); - try { - sb.insert(7, "foo"); - fail("insert(7, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, "foo")); sb.insert(0, (String) null); assertThat(sb.toString()).isEqualTo("nullbarbaz"); http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StrBuilderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrBuilderTest.java b/src/test/java/org/apache/commons/text/StrBuilderTest.java index a7fafe2..c17631a 100644 --- a/src/test/java/org/apache/commons/text/StrBuilderTest.java +++ b/src/test/java/org/apache/commons/text/StrBuilderTest.java @@ -18,6 +18,8 @@ package org.apache.commons.text; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -25,7 +27,6 @@ import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.io.Reader; @@ -36,7 +37,6 @@ import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.util.Arrays; - import org.junit.jupiter.api.Test; /** @@ -270,12 +270,7 @@ public class StrBuilderTest { assertEquals(33, sb.size()); assertFalse(sb.isEmpty()); - try { - sb.setLength(-1); - fail("setLength(-1) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setLength(-1)); sb.setLength(33); assertEquals(33, sb.capacity()); @@ -325,12 +320,7 @@ public class StrBuilderTest { sb.setLength(3); // lengthen assertEquals("He\0", sb.toString()); - try { - sb.setLength(-1); - fail("setLength(-1) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setLength(-1)); } // ----------------------------------------------------------------------- @@ -403,62 +393,27 @@ public class StrBuilderTest { @Test public void testCharAt() { final StrBuilder sb = new StrBuilder(); - try { - sb.charAt(0); - fail("charAt(0) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } - try { - sb.charAt(-1); - fail("charAt(-1) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(0)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(-1)); sb.append("foo"); assertEquals('f', sb.charAt(0)); assertEquals('o', sb.charAt(1)); assertEquals('o', sb.charAt(2)); - try { - sb.charAt(-1); - fail("charAt(-1) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } - try { - sb.charAt(3); - fail("charAt(3) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(3)); } // ----------------------------------------------------------------------- @Test public void testSetCharAt() { final StrBuilder sb = new StrBuilder(); - try { - sb.setCharAt(0, 'f'); - fail("setCharAt(0,) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } - try { - sb.setCharAt(-1, 'f'); - fail("setCharAt(-1,) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setCharAt(0, 'f')); + assertThrows(IndexOutOfBoundsException.class, () -> sb.setCharAt(-1, 'f')); sb.append("foo"); sb.setCharAt(0, 'b'); sb.setCharAt(1, 'a'); sb.setCharAt(2, 'r'); - try { - sb.setCharAt(3, '!'); - fail("setCharAt(3,) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setCharAt(3, '!')); assertEquals("bar", sb.toString()); } @@ -469,11 +424,7 @@ public class StrBuilderTest { sb.deleteCharAt(0); assertEquals("bc", sb.toString()); - try { - sb.deleteCharAt(1000); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.deleteCharAt(1000)); } // ----------------------------------------------------------------------- @@ -513,17 +464,9 @@ public class StrBuilderTest { a = sb.toCharArray(0, 1); assertNotNull(a, "toCharArray(int,int) result is null"); - try { - sb.toCharArray(-1, 5); - fail("no string index out of bound on -1"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.toCharArray(-1, 5)); - try { - sb.toCharArray(6, 5); - fail("no string index out of bound on -1"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.toCharArray(6, 5)); } @Test @@ -595,7 +538,7 @@ public class StrBuilderTest { // ----------------------------------------------------------------------- @Test public void testDeleteIntInt() { - StrBuilder sb = new StrBuilder("abc"); + final StrBuilder sb = new StrBuilder("abc"); sb.delete(0, 1); assertEquals("bc", sb.toString()); sb.delete(1, 2); @@ -605,23 +548,10 @@ public class StrBuilderTest { sb.delete(0, 1000); assertEquals("", sb.toString()); - try { - sb.delete(1, 2); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } - try { - sb.delete(-1, 1); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.delete(1, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.delete(-1, 1)); - sb = new StrBuilder("anything"); - try { - sb.delete(2, 1); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> new StrBuilder("anything").delete(2, 1)); } // ----------------------------------------------------------------------- @@ -1215,32 +1145,16 @@ public class StrBuilderTest { public void testSubSequenceIntInt() { final StrBuilder sb = new StrBuilder("hello goodbye"); // Start index is negative - try { - sb.subSequence(-1, 5); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(-1, 5)); // End index is negative - try { - sb.subSequence(2, -1); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(2, -1)); // End index greater than length() - try { - sb.subSequence(2, sb.length() + 1); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(2, sb.length() + 1)); // Start index greater then end index - try { - sb.subSequence(3, 2); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(3, 2)); // Normal cases assertEquals("hello", sb.subSequence(0, 5)); @@ -1256,17 +1170,8 @@ public class StrBuilderTest { assertEquals("hello goodbye".substring(6), sb.substring(6)); assertEquals("hello goodbye", sb.substring(0)); assertEquals("hello goodbye".substring(0), sb.substring(0)); - try { - sb.substring(-1); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } - - try { - sb.substring(15); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15)); } @@ -1281,17 +1186,9 @@ public class StrBuilderTest { assertEquals("goodbye", sb.substring(6, 20)); - try { - sb.substring(-1, 5); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(-1, 5)); - try { - sb.substring(15, 20); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15, 20)); } // ----------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StrLookupTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrLookupTest.java b/src/test/java/org/apache/commons/text/StrLookupTest.java index c765b5a..876bfc4 100644 --- a/src/test/java/org/apache/commons/text/StrLookupTest.java +++ b/src/test/java/org/apache/commons/text/StrLookupTest.java @@ -16,9 +16,9 @@ */ package org.apache.commons.text; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; import java.util.HashMap; import java.util.Map; @@ -48,12 +48,7 @@ public class StrLookupTest { assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().lookup("os.name")); assertNull(StrLookup.systemPropertiesLookup().lookup("")); assertNull(StrLookup.systemPropertiesLookup().lookup("other")); - try { - StrLookup.systemPropertiesLookup().lookup(null); - fail("Exception expected!"); - } catch (final NullPointerException ex) { - // expected - } + assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().lookup(null)); } /** http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StrSubstitutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrSubstitutorTest.java b/src/test/java/org/apache/commons/text/StrSubstitutorTest.java index dcd6297..760e6fc 100644 --- a/src/test/java/org/apache/commons/text/StrSubstitutorTest.java +++ b/src/test/java/org/apache/commons/text/StrSubstitutorTest.java @@ -18,12 +18,12 @@ package org.apache.commons.text; import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.util.HashMap; import java.util.Map; @@ -269,22 +269,12 @@ public class StrSubstitutorTest { map.put("critterColor", "brown"); map.put("critterType", "${animal}"); StrSubstitutor sub = new StrSubstitutor(map); - try { - sub.replace("The ${animal} jumps over the ${target}."); - fail("Cyclic replacement was not detected!"); - } catch (final IllegalStateException ex) { - // expected - } + assertThrows(IllegalStateException.class, () -> sub.replace("The ${animal} jumps over the ${target}.")); // also check even when default value is set. map.put("critterType", "${animal:-fox}"); - sub = new StrSubstitutor(map); - try { - sub.replace("The ${animal} jumps over the ${target}."); - fail("Cyclic replacement was not detected!"); - } catch (final IllegalStateException ex) { - // expected - } + assertThrows(IllegalStateException.class, + () -> new StrSubstitutor(map).replace("The ${animal} jumps over the ${target}.")); } /** @@ -495,23 +485,13 @@ public class StrSubstitutorTest { sub.setVariablePrefix("<<"); assertTrue(sub.getVariablePrefixMatcher() instanceof StrMatcher.StringMatcher); - try { - sub.setVariablePrefix((String) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefix((String) null)); assertTrue(sub.getVariablePrefixMatcher() instanceof StrMatcher.StringMatcher); final StrMatcher matcher = StrMatcher.commaMatcher(); sub.setVariablePrefixMatcher(matcher); assertSame(matcher, sub.getVariablePrefixMatcher()); - try { - sub.setVariablePrefixMatcher((StrMatcher) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefixMatcher((StrMatcher) null)); assertSame(matcher, sub.getVariablePrefixMatcher()); } @@ -527,23 +507,13 @@ public class StrSubstitutorTest { sub.setVariableSuffix("<<"); assertTrue(sub.getVariableSuffixMatcher() instanceof StrMatcher.StringMatcher); - try { - sub.setVariableSuffix((String) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffix((String) null)); assertTrue(sub.getVariableSuffixMatcher() instanceof StrMatcher.StringMatcher); final StrMatcher matcher = StrMatcher.commaMatcher(); sub.setVariableSuffixMatcher(matcher); assertSame(matcher, sub.getVariableSuffixMatcher()); - try { - sub.setVariableSuffixMatcher((StrMatcher) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffixMatcher((StrMatcher) null)); assertSame(matcher, sub.getVariableSuffixMatcher()); } http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StrTokenizerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StrTokenizerTest.java b/src/test/java/org/apache/commons/text/StrTokenizerTest.java index dc04b14..cf40b00 100644 --- a/src/test/java/org/apache/commons/text/StrTokenizerTest.java +++ b/src/test/java/org/apache/commons/text/StrTokenizerTest.java @@ -17,11 +17,11 @@ package org.apache.commons.text; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.util.Arrays; import java.util.Collections; @@ -574,11 +574,7 @@ public class StrTokenizerTest { assertFalse(tokenizer.hasPrevious()); assertNull(tokenizer.nextToken()); assertEquals(0, tokenizer.size()); - try { - tokenizer.next(); - fail("Exception expected!"); - } catch (final NoSuchElementException ex) { - } + assertThrows(NoSuchElementException.class, () -> tokenizer.next()); } @Test @@ -840,29 +836,13 @@ public class StrTokenizerTest { public void testIteration() { final StrTokenizer tkn = new StrTokenizer("a b c"); assertFalse(tkn.hasPrevious()); - try { - tkn.previous(); - fail("Exception expected!"); - } catch (final NoSuchElementException ex) { - } + assertThrows(NoSuchElementException.class, () -> tkn.previous()); assertTrue(tkn.hasNext()); assertEquals("a", tkn.next()); - try { - tkn.remove(); - fail("Exception expected!"); - } catch (final UnsupportedOperationException ex) { - } - try { - tkn.set("x"); - fail("Exception expected!"); - } catch (final UnsupportedOperationException ex) { - } - try { - tkn.add("y"); - fail("Exception expected!"); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, () -> tkn.remove()); + assertThrows(UnsupportedOperationException.class, () -> tkn.set("x")); + assertThrows(UnsupportedOperationException.class, () -> tkn.add("y")); assertTrue(tkn.hasPrevious()); assertTrue(tkn.hasNext()); @@ -874,11 +854,7 @@ public class StrTokenizerTest { assertTrue(tkn.hasPrevious()); assertFalse(tkn.hasNext()); - try { - tkn.next(); - fail("Exception expected!"); - } catch (final NoSuchElementException ex) { - } + assertThrows(NoSuchElementException.class, () -> tkn.next()); assertTrue(tkn.hasPrevious()); assertFalse(tkn.hasNext()); } http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java b/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java index 3cecfb8..16209f2 100644 --- a/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java +++ b/src/test/java/org/apache/commons/text/StringEscapeUtilsTest.java @@ -16,7 +16,15 @@ */ package org.apache.commons.text; -import org.junit.jupiter.api.Test; +import static org.apache.commons.text.StringEscapeUtils.escapeXSI; +import static org.apache.commons.text.StringEscapeUtils.unescapeXSI; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.io.StringWriter; @@ -27,14 +35,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; -import static org.apache.commons.text.StringEscapeUtils.escapeXSI; -import static org.apache.commons.text.StringEscapeUtils.unescapeXSI; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import org.junit.jupiter.api.Test; /** * Unit tests for {@link StringEscapeUtils}. @@ -143,11 +144,7 @@ public class StringEscapeUtilsTest { fail("Exception expected!"); } catch (final IllegalArgumentException ex) { } - try { - StringEscapeUtils.unescapeJava("\\u02-3"); - fail("Exception expected!"); - } catch (final RuntimeException ex) { - } + assertThrows(RuntimeException.class, () -> StringEscapeUtils.unescapeJava("\\u02-3")); assertUnescapeJava("", ""); assertUnescapeJava("test", "test"); http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/StringTokenizerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/StringTokenizerTest.java b/src/test/java/org/apache/commons/text/StringTokenizerTest.java index d0ffd84..2cbbf6b 100644 --- a/src/test/java/org/apache/commons/text/StringTokenizerTest.java +++ b/src/test/java/org/apache/commons/text/StringTokenizerTest.java @@ -21,8 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -573,11 +572,7 @@ public class StringTokenizerTest { assertFalse(tokenizer.hasPrevious()); assertNull(tokenizer.nextToken()); assertEquals(0, tokenizer.size()); - try { - tokenizer.next(); - fail("Exception expected!"); - } catch (final NoSuchElementException ex) { - } + assertThrows(NoSuchElementException.class, () -> tokenizer.next()); } @Test @@ -839,29 +834,13 @@ public class StringTokenizerTest { public void testIteration() { final StringTokenizer tkn = new StringTokenizer("a b c"); assertFalse(tkn.hasPrevious()); - try { - tkn.previous(); - fail("Exception expected!"); - } catch (final NoSuchElementException ex) { - } + assertThrows(NoSuchElementException.class, () -> tkn.previous()); assertTrue(tkn.hasNext()); assertEquals("a", tkn.next()); - try { - tkn.remove(); - fail("Exception expected!"); - } catch (final UnsupportedOperationException ex) { - } - try { - tkn.set("x"); - fail("Exception expected!"); - } catch (final UnsupportedOperationException ex) { - } - try { - tkn.add("y"); - fail("Exception expected!"); - } catch (final UnsupportedOperationException ex) { - } + assertThrows(UnsupportedOperationException.class, () -> tkn.remove()); + assertThrows(UnsupportedOperationException.class, () -> tkn.set("x")); + assertThrows(UnsupportedOperationException.class, () -> tkn.add("y")); assertTrue(tkn.hasPrevious()); assertTrue(tkn.hasNext()); @@ -873,11 +852,7 @@ public class StringTokenizerTest { assertTrue(tkn.hasPrevious()); assertFalse(tkn.hasNext()); - try { - tkn.next(); - fail("Exception expected!"); - } catch (final NoSuchElementException ex) { - } + assertThrows(NoSuchElementException.class, () -> tkn.next()); assertTrue(tkn.hasPrevious()); assertFalse(tkn.hasNext()); } http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/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 index ce8d044..3551f63 100644 --- a/src/test/java/org/apache/commons/text/TextStringBuilderAppendInsertTest.java +++ b/src/test/java/org/apache/commons/text/TextStringBuilderAppendInsertTest.java @@ -19,6 +19,7 @@ package org.apache.commons.text; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.text.DecimalFormatSymbols; import java.util.Arrays; @@ -1287,19 +1288,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, FOO); - fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, FOO)); - try { - sb.insert(7, FOO); - fail("insert(7, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, FOO)); sb.insert(0, (Object) null); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1311,19 +1302,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, "foo"); - fail("insert(-1, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, "foo")); - try { - sb.insert(7, "foo"); - fail("insert(7, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, "foo")); sb.insert(0, (String) null); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1335,19 +1316,9 @@ public class TextStringBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, new char[]{'f', 'o', 'o'})); - try { - sb.insert(7, new char[]{'f', 'o', 'o'}); - fail("insert(7, char[]) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, new char[]{'f', 'o', 'o'})); sb.insert(0, (char[]) null); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1362,19 +1333,11 @@ public class TextStringBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3)); sb.insert(0, (char[]) null, 0, 0); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1382,33 +1345,17 @@ public class TextStringBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1)); - 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 - } + assertThrows(IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10)); sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0); assertThat(sb.toString()).isEqualTo("barbaz"); @@ -1420,19 +1367,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, true); - fail("insert(-1, boolean) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, true)); - try { - sb.insert(7, true); - fail("insert(7, boolean) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, true)); sb.insert(0, true); assertThat(sb.toString()).isEqualTo("truebarbaz"); @@ -1444,19 +1381,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, '!'); - fail("insert(-1, char) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, '!')); - try { - sb.insert(7, '!'); - fail("insert(7, char) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, '!')); sb.insert(0, '!'); assertThat(sb.toString()).isEqualTo("!barbaz"); @@ -1465,19 +1392,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, 0); - fail("insert(-1, int) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 0)); - try { - sb.insert(7, 0); - fail("insert(7, int) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 0)); sb.insert(0, '0'); assertThat(sb.toString()).isEqualTo("0barbaz"); @@ -1486,19 +1403,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, 1L); - fail("insert(-1, long) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 1L)); - try { - sb.insert(7, 1L); - fail("insert(7, long) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 1L)); sb.insert(0, 1L); assertThat(sb.toString()).isEqualTo("1barbaz"); @@ -1507,19 +1414,9 @@ public class TextStringBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 2.3F)); - try { - sb.insert(7, 2.3F); - fail("insert(7, float) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 2.3F)); sb.insert(0, 2.3F); assertThat(sb.toString()).isEqualTo("2.3barbaz"); @@ -1528,19 +1425,9 @@ public class TextStringBuilderAppendInsertTest { 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 - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, 4.5D)); - try { - sb.insert(7, 4.5D); - fail("insert(7, double) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, 4.5D)); sb.insert(0, 4.5D); assertThat(sb.toString()).isEqualTo("4.5barbaz"); @@ -1554,19 +1441,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, FOO); - fail("insert(-1, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, FOO)); - try { - sb.insert(7, FOO); - fail("insert(7, Object) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, FOO)); sb.insert(0, (Object) null); assertThat(sb.toString()).isEqualTo("nullbarbaz"); @@ -1578,19 +1455,9 @@ public class TextStringBuilderAppendInsertTest { sb.append("barbaz"); assertThat(sb.toString()).isEqualTo("barbaz"); - try { - sb.insert(-1, "foo"); - fail("insert(-1, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(-1, "foo")); - try { - sb.insert(7, "foo"); - fail("insert(7, String) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.insert(7, "foo")); sb.insert(0, (String) null); assertThat(sb.toString()).isEqualTo("nullbarbaz"); http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/TextStringBuilderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java index 7f713dc..8edcbd7 100644 --- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java +++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java @@ -18,6 +18,8 @@ package org.apache.commons.text; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -25,7 +27,6 @@ import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.io.Reader; @@ -269,12 +270,7 @@ public class TextStringBuilderTest { assertEquals(33, sb.size()); assertFalse(sb.isEmpty()); - try { - sb.setLength(-1); - fail("setLength(-1) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setLength(-1)); sb.setLength(33); assertEquals(33, sb.capacity()); @@ -324,12 +320,7 @@ public class TextStringBuilderTest { sb.setLength(3); // lengthen assertEquals("He\0", sb.toString()); - try { - sb.setLength(-1); - fail("setLength(-1) expected StringIndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setLength(-1)); } // ----------------------------------------------------------------------- @@ -402,62 +393,27 @@ public class TextStringBuilderTest { @Test public void testCharAt() { final TextStringBuilder sb = new TextStringBuilder(); - try { - sb.charAt(0); - fail("charAt(0) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } - try { - sb.charAt(-1); - fail("charAt(-1) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(0)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(-1)); sb.append("foo"); assertEquals('f', sb.charAt(0)); assertEquals('o', sb.charAt(1)); assertEquals('o', sb.charAt(2)); - try { - sb.charAt(-1); - fail("charAt(-1) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } - try { - sb.charAt(3); - fail("charAt(3) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.charAt(3)); } // ----------------------------------------------------------------------- @Test public void testSetCharAt() { final TextStringBuilder sb = new TextStringBuilder(); - try { - sb.setCharAt(0, 'f'); - fail("setCharAt(0,) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } - try { - sb.setCharAt(-1, 'f'); - fail("setCharAt(-1,) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setCharAt(0, 'f')); + assertThrows(IndexOutOfBoundsException.class, () -> sb.setCharAt(-1, 'f')); sb.append("foo"); sb.setCharAt(0, 'b'); sb.setCharAt(1, 'a'); sb.setCharAt(2, 'r'); - try { - sb.setCharAt(3, '!'); - fail("setCharAt(3,) expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // expected - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.setCharAt(3, '!')); assertEquals("bar", sb.toString()); } @@ -468,11 +424,7 @@ public class TextStringBuilderTest { sb.deleteCharAt(0); assertEquals("bc", sb.toString()); - try { - sb.deleteCharAt(1000); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.deleteCharAt(1000)); } // ----------------------------------------------------------------------- @@ -512,17 +464,9 @@ public class TextStringBuilderTest { a = sb.toCharArray(0, 1); assertNotNull(a, "toCharArray(int,int) result is null"); - try { - sb.toCharArray(-1, 5); - fail("no string index out of bound on -1"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.toCharArray(-1, 5)); - try { - sb.toCharArray(6, 5); - fail("no string index out of bound on -1"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.toCharArray(6, 5)); } @Test @@ -594,7 +538,7 @@ public class TextStringBuilderTest { // ----------------------------------------------------------------------- @Test public void testDeleteIntInt() { - TextStringBuilder sb = new TextStringBuilder("abc"); + final TextStringBuilder sb = new TextStringBuilder("abc"); sb.delete(0, 1); assertEquals("bc", sb.toString()); sb.delete(1, 2); @@ -604,23 +548,10 @@ public class TextStringBuilderTest { sb.delete(0, 1000); assertEquals("", sb.toString()); - try { - sb.delete(1, 2); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } - try { - sb.delete(-1, 1); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.delete(1, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.delete(-1, 1)); - sb = new TextStringBuilder("anything"); - try { - sb.delete(2, 1); - fail("Expected IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> new TextStringBuilder("anything").delete(2, 1)); } // ----------------------------------------------------------------------- @@ -1214,32 +1145,16 @@ public class TextStringBuilderTest { public void testSubSequenceIntInt() { final TextStringBuilder sb = new TextStringBuilder("hello goodbye"); // Start index is negative - try { - sb.subSequence(-1, 5); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(-1, 5)); // End index is negative - try { - sb.subSequence(2, -1); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(2, -1)); // End index greater than length() - try { - sb.subSequence(2, sb.length() + 1); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(2, sb.length() + 1)); // Start index greater then end index - try { - sb.subSequence(3, 2); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(3, 2)); // Normal cases assertEquals("hello", sb.subSequence(0, 5)); @@ -1255,17 +1170,9 @@ public class TextStringBuilderTest { assertEquals("hello goodbye".substring(6), sb.substring(6)); assertEquals("hello goodbye", sb.substring(0)); assertEquals("hello goodbye".substring(0), sb.substring(0)); - try { - sb.substring(-1); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(-1)); - try { - sb.substring(15); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15)); } @@ -1280,17 +1187,9 @@ public class TextStringBuilderTest { assertEquals("goodbye", sb.substring(6, 20)); - try { - sb.substring(-1, 5); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(-1, 5)); - try { - sb.substring(15, 20); - fail("Exception expected!"); - } catch (final IndexOutOfBoundsException e) { - } + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15, 20)); } // ----------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java b/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java index cee1caa..244d6b5 100644 --- a/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java +++ b/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java @@ -17,10 +17,10 @@ package org.apache.commons.text.matcher; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import org.apache.commons.text.StringSubstitutor; import org.junit.jupiter.api.Test; @@ -42,23 +42,13 @@ public class StringSubstitutorGetSetTest { sub.setVariablePrefix("<<"); assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.StringMatcher); - try { - sub.setVariablePrefix((String) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefix((String) null)); assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.StringMatcher); final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher(); sub.setVariablePrefixMatcher(matcher); assertSame(matcher, sub.getVariablePrefixMatcher()); - try { - sub.setVariablePrefixMatcher((StringMatcher) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefixMatcher((StringMatcher) null)); assertSame(matcher, sub.getVariablePrefixMatcher()); } @@ -74,23 +64,13 @@ public class StringSubstitutorGetSetTest { sub.setVariableSuffix("<<"); assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.StringMatcher); - try { - sub.setVariableSuffix((String) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffix((String) null)); assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.StringMatcher); final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher(); sub.setVariableSuffixMatcher(matcher); assertSame(matcher, sub.getVariableSuffixMatcher()); - try { - sub.setVariableSuffixMatcher((StringMatcher) null); - fail("Exception expected!"); - } catch (final IllegalArgumentException ex) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffixMatcher((StringMatcher) null)); assertSame(matcher, sub.getVariableSuffixMatcher()); } http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c3cc523/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java b/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java index c209fa7..b329c98 100644 --- a/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java +++ b/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java @@ -17,10 +17,10 @@ package org.apache.commons.text.translate; -import org.junit.jupiter.api.Test; - import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; /** * Unit tests for {@link UnicodeEscaper}. @@ -50,11 +50,6 @@ public class UnicodeUnescaperTest { final UnicodeUnescaper uu = new UnicodeUnescaper(); final String input = "\\0047\\u006"; - try { - uu.translate(input); - fail("A lack of digits in a Unicode escape sequence failed to throw an exception"); - } catch (final IllegalArgumentException iae) { - // expected - } + assertThrows(IllegalArgumentException.class, () -> uu.translate(input)); } }