diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java b/src/main/java/org/apache/commons/lang3/LocaleUtils.java index 3b720a8c54c..8e372be060d 100644 --- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java +++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java @@ -17,14 +17,7 @@ package org.apache.commons.lang3; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Locale; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.function.Predicate; @@ -46,6 +39,298 @@ public class LocaleUtils { /** * Avoids synchronization, initializes on demand. */ + + private static final Map ISO2_TO_NUMERIC; + + static { + Map numeric = new HashMap<>(); + numeric.put("AF", "004"); + numeric.put("AL", "008"); + numeric.put("DZ", "012"); + numeric.put("AS", "016"); + numeric.put("AD", "020"); + numeric.put("AO", "024"); + numeric.put("AI", "660"); + numeric.put("AQ", "010"); + numeric.put("AG", "028"); + numeric.put("AR", "032"); + numeric.put("AM", "051"); + numeric.put("AW", "533"); + numeric.put("AU", "036"); + numeric.put("AT", "040"); + numeric.put("AZ", "031"); + numeric.put("BS", "044"); + numeric.put("BH", "048"); + numeric.put("BD", "050"); + numeric.put("BB", "052"); + numeric.put("BY", "112"); + numeric.put("BE", "056"); + numeric.put("BZ", "084"); + numeric.put("BJ", "204"); + numeric.put("BM", "060"); + numeric.put("BT", "064"); + numeric.put("BO", "068"); + numeric.put("BQ", "535"); + numeric.put("BA", "070"); + numeric.put("BW", "072"); + numeric.put("BV", "074"); + numeric.put("BR", "076"); + numeric.put("IO", "086"); + numeric.put("BN", "096"); + numeric.put("BG", "100"); + numeric.put("BF", "854"); + numeric.put("BI", "108"); + numeric.put("CV", "132"); + numeric.put("KH", "116"); + numeric.put("CM", "120"); + numeric.put("CA", "124"); + numeric.put("KY", "136"); + numeric.put("CF", "140"); + numeric.put("TD", "148"); + numeric.put("CL", "152"); + numeric.put("CN", "156"); + numeric.put("CX", "162"); + numeric.put("CC", "166"); + numeric.put("CO", "170"); + numeric.put("KM", "174"); + numeric.put("CG", "178"); + numeric.put("CD", "180"); + numeric.put("CK", "184"); + numeric.put("CR", "188"); + numeric.put("CI", "384"); + numeric.put("HR", "191"); + numeric.put("CU", "192"); + numeric.put("CW", "531"); + numeric.put("CY", "196"); + numeric.put("CZ", "203"); + numeric.put("DK", "208"); + numeric.put("DJ", "262"); + numeric.put("DM", "212"); + numeric.put("DO", "214"); + numeric.put("EC", "218"); + numeric.put("EG", "818"); + numeric.put("SV", "222"); + numeric.put("GQ", "226"); + numeric.put("ER", "232"); + numeric.put("EE", "233"); + numeric.put("SZ", "748"); + numeric.put("ET", "231"); + numeric.put("FK", "238"); + numeric.put("FO", "234"); + numeric.put("FJ", "242"); + numeric.put("FI", "246"); + numeric.put("FR", "250"); + numeric.put("GF", "254"); + numeric.put("PF", "258"); + numeric.put("TF", "260"); + numeric.put("GA", "266"); + numeric.put("GM", "270"); + numeric.put("GE", "268"); + numeric.put("DE", "276"); + numeric.put("GH", "288"); + numeric.put("GI", "292"); + numeric.put("GR", "300"); + numeric.put("GL", "304"); + numeric.put("GD", "308"); + numeric.put("GP", "312"); + numeric.put("GU", "316"); + numeric.put("GT", "320"); + numeric.put("GG", "831"); + numeric.put("GN", "324"); + numeric.put("GW", "624"); + numeric.put("GY", "328"); + numeric.put("HT", "332"); + numeric.put("HM", "334"); + numeric.put("VA", "336"); + numeric.put("HN", "340"); + numeric.put("HK", "344"); + numeric.put("HU", "348"); + numeric.put("IS", "352"); + numeric.put("IN", "356"); + numeric.put("ID", "360"); + numeric.put("IR", "364"); + numeric.put("IQ", "368"); + numeric.put("IE", "372"); + numeric.put("IM", "833"); + numeric.put("IL", "376"); + numeric.put("IT", "380"); + numeric.put("JM", "388"); + numeric.put("JP", "392"); + numeric.put("JE", "832"); + numeric.put("JO", "400"); + numeric.put("KZ", "398"); + numeric.put("KE", "404"); + numeric.put("KI", "296"); + numeric.put("KP", "408"); + numeric.put("KR", "410"); + numeric.put("KW", "414"); + numeric.put("KG", "417"); + numeric.put("LA", "418"); + numeric.put("LV", "428"); + numeric.put("LB", "422"); + numeric.put("LS", "426"); + numeric.put("LR", "430"); + numeric.put("LY", "434"); + numeric.put("LI", "438"); + numeric.put("LT", "440"); + numeric.put("LU", "442"); + numeric.put("MO", "446"); + numeric.put("MG", "450"); + numeric.put("MW", "454"); + numeric.put("MY", "458"); + numeric.put("MV", "462"); + numeric.put("ML", "466"); + numeric.put("MT", "470"); + numeric.put("MH", "584"); + numeric.put("MQ", "474"); + numeric.put("MR", "478"); + numeric.put("MU", "480"); + numeric.put("YT", "175"); + numeric.put("MX", "484"); + numeric.put("FM", "583"); + numeric.put("MD", "498"); + numeric.put("MC", "492"); + numeric.put("MN", "496"); + numeric.put("ME", "499"); + numeric.put("MS", "500"); + numeric.put("MA", "504"); + numeric.put("MZ", "508"); + numeric.put("MM", "104"); + numeric.put("NA", "516"); + numeric.put("NR", "520"); + numeric.put("NP", "524"); + numeric.put("NL", "528"); + numeric.put("NC", "540"); + numeric.put("NZ", "554"); + numeric.put("NI", "558"); + numeric.put("NE", "562"); + numeric.put("NG", "566"); + numeric.put("NU", "570"); + numeric.put("NF", "574"); + numeric.put("MK", "807"); + numeric.put("MP", "580"); + numeric.put("NO", "578"); + numeric.put("OM", "512"); + numeric.put("PK", "586"); + numeric.put("PW", "585"); + numeric.put("PS", "275"); + numeric.put("PA", "591"); + numeric.put("PG", "598"); + numeric.put("PY", "600"); + numeric.put("PE", "604"); + numeric.put("PH", "608"); + numeric.put("PN", "612"); + numeric.put("PL", "616"); + numeric.put("PT", "620"); + numeric.put("PR", "630"); + numeric.put("QA", "634"); + + numeric.put("RE", "638"); + numeric.put("RO", "642"); + numeric.put("RU", "643"); + numeric.put("RW", "646"); + numeric.put("BL", "652"); + numeric.put("SH", "654"); + numeric.put("KN", "659"); + numeric.put("LC", "662"); + numeric.put("MF", "663"); + numeric.put("PM", "666"); + numeric.put("VC", "670"); + numeric.put("WS", "882"); + numeric.put("SM", "674"); + numeric.put("ST", "678"); + numeric.put("SA", "682"); + numeric.put("SN", "686"); + numeric.put("RS", "688"); + numeric.put("SC", "690"); + numeric.put("SL", "694"); + numeric.put("SG", "702"); + numeric.put("SX", "534"); + numeric.put("SK", "703"); + numeric.put("SI", "705"); + numeric.put("ZW", "716"); + numeric.put("SB", "090"); + numeric.put("SO", "706"); + numeric.put("ZA", "710"); + numeric.put("GS", "239"); + numeric.put("SS", "728"); + numeric.put("ES", "724"); + numeric.put("LK", "144"); + numeric.put("SD", "729"); + numeric.put("SR", "740"); + numeric.put("SJ", "744"); + numeric.put("SE", "752"); + numeric.put("CH", "756"); + + numeric.put("SY", "760"); + numeric.put("TW", "158"); + numeric.put("TJ", "762"); + numeric.put("TZ", "834"); + numeric.put("TH", "764"); + numeric.put("TL", "626"); + numeric.put("TG", "768"); + numeric.put("TK", "772"); + numeric.put("TO", "776"); + numeric.put("TT", "780"); + numeric.put("TN", "788"); + numeric.put("TR", "792"); + numeric.put("TM", "795"); + numeric.put("TC", "796"); + numeric.put("TV", "798"); + numeric.put("UG", "800"); + numeric.put("UA", "804"); + numeric.put("AE", "784"); + numeric.put("GB", "826"); + numeric.put("UM", "581"); + numeric.put("US", "840"); + numeric.put("UY", "858"); + numeric.put("UZ", "860"); + numeric.put("VU", "548"); + numeric.put("VE", "862"); + numeric.put("VN", "704"); + numeric.put("VG", "092"); + numeric.put("VI", "850"); + numeric.put("WF", "876"); + numeric.put("EH", "732"); + numeric.put("YE", "887"); + numeric.put("ZM", "894"); + ISO2_TO_NUMERIC = Collections.unmodifiableMap(numeric); + + } + + /** + * + * This method performs a reverse lookup in the ISO2_TO_NUMERIC map to find the + * two-letter country code that corresponds to the given three-digit numeric code. + * + * + * @param numeric the three-digit numeric country code (e.g., "840" for US) + * @return the corresponding two-letter ISO country code (e.g., "US"), or null if not found + * */ + public static String numericToIso2(final String numeric) { + Objects.requireNonNull(numeric, "Numeric country code must not be null"); + for (Map.Entry entry : ISO2_TO_NUMERIC.entrySet()) { + if (entry.getValue().equals(numeric)) { + return entry.getKey(); + } + } + return null; + } + + /** + * Converts a two-letter ISO country code to its corresponding three-digit numeric code. + * + * @param iso2 the two-letter ISO country code (e.g., "US") + * @return the corresponding three-digit numeric country code (e.g., "840"), or null if not found + */ + public static String toNumeric(final String iso2) { + Objects.requireNonNull(iso2, "ISO2 country code must not be null"); + return ISO2_TO_NUMERIC.getOrDefault(iso2,null); + } + + + + private static final class SyncAvoid { /** Private unmodifiable and sorted list of available locales. */ diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java index fd816935df8..1bf585e6c1f 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java @@ -17,13 +17,12 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -524,7 +523,7 @@ void testAddObjectAtIndex() { // boolean tests boolean[] booleanArray = ArrayUtils.add(null, 0, true); assertArrayEquals(new boolean[] { true }, booleanArray); - IndexOutOfBoundsException e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(null, -1, true)); + IndexOutOfBoundsException e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(null, -1, true)); assertEquals("Index: -1, Length: 0", e.getMessage()); booleanArray = ArrayUtils.add(new boolean[] { true }, 0, false); assertArrayEquals(new boolean[] { false, true }, booleanArray); @@ -532,15 +531,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new boolean[] { false, true }, booleanArray); booleanArray = ArrayUtils.add(new boolean[] { true, false }, 1, true); assertArrayEquals(new boolean[] { true, true, false }, booleanArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new boolean[] { true, false }, 4, true)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new boolean[] { true, false }, 4, true)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new boolean[] { true, false }, -1, true)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new boolean[] { true, false }, -1, true)); assertEquals("Index: -1, Length: 2", e.getMessage()); // char tests char[] charArray = ArrayUtils.add((char[]) null, 0, 'a'); assertArrayEquals(new char[] { 'a' }, charArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((char[]) null, -1, 'a')); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((char[]) null, -1, 'a')); assertEquals("Index: -1, Length: 0", e.getMessage()); charArray = ArrayUtils.add(new char[] { 'a' }, 0, 'b'); assertArrayEquals(new char[] { 'b', 'a' }, charArray); @@ -550,15 +549,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new char[] { 'a', 'k', 'b' }, charArray); charArray = ArrayUtils.add(new char[] { 'a', 'b', 'c' }, 1, 't'); assertArrayEquals(new char[] { 'a', 't', 'b', 'c' }, charArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new char[] { 'a', 'b' }, 4, 'c')); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new char[] { 'a', 'b' }, 4, 'c')); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new char[] { 'a', 'b' }, -1, 'c')); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new char[] { 'a', 'b' }, -1, 'c')); assertEquals("Index: -1, Length: 2", e.getMessage()); // short tests short[] shortArray = ArrayUtils.add(new short[] { 1 }, 0, (short) 2); assertArrayEquals(new short[] { 2, 1 }, shortArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((short[]) null, -1, (short) 2)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((short[]) null, -1, (short) 2)); assertEquals("Index: -1, Length: 0", e.getMessage()); shortArray = ArrayUtils.add(new short[] { 2, 6 }, 2, (short) 10); assertArrayEquals(new short[] { 2, 6, 10 }, shortArray); @@ -566,15 +565,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new short[] { -4, 2, 6 }, shortArray); shortArray = ArrayUtils.add(new short[] { 2, 6, 3 }, 2, (short) 1); assertArrayEquals(new short[] { 2, 6, 1, 3 }, shortArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new short[] { 2, 6 }, 4, (short) 10)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new short[] { 2, 6 }, 4, (short) 10)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new short[] { 2, 6 }, -1, (short) 10)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new short[] { 2, 6 }, -1, (short) 10)); assertEquals("Index: -1, Length: 2", e.getMessage()); // byte tests byte[] byteArray = ArrayUtils.add(new byte[] { 1 }, 0, (byte) 2); assertArrayEquals(new byte[] { 2, 1 }, byteArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((byte[]) null, -1, (byte) 2)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((byte[]) null, -1, (byte) 2)); assertEquals("Index: -1, Length: 0", e.getMessage()); byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 2, (byte) 3); assertArrayEquals(new byte[] { 2, 6, 3 }, byteArray); @@ -582,15 +581,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new byte[] { 1, 2, 6 }, byteArray); byteArray = ArrayUtils.add(new byte[] { 2, 6, 3 }, 2, (byte) 1); assertArrayEquals(new byte[] { 2, 6, 1, 3 }, byteArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new byte[] { 2, 6 }, 4, (byte) 3)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new byte[] { 2, 6 }, 4, (byte) 3)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new byte[] { 2, 6 }, -1, (byte) 3)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new byte[] { 2, 6 }, -1, (byte) 3)); assertEquals("Index: -1, Length: 2", e.getMessage()); // int tests int[] intArray = ArrayUtils.add(new int[] { 1 }, 0, 2); assertArrayEquals(new int[] { 2, 1 }, intArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((int[]) null, -1, 2)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((int[]) null, -1, 2)); assertEquals("Index: -1, Length: 0", e.getMessage()); intArray = ArrayUtils.add(new int[] { 2, 6 }, 2, 10); assertArrayEquals(new int[] { 2, 6, 10 }, intArray); @@ -598,15 +597,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new int[] { -4, 2, 6 }, intArray); intArray = ArrayUtils.add(new int[] { 2, 6, 3 }, 2, 1); assertArrayEquals(new int[] { 2, 6, 1, 3 }, intArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new int[] { 2, 6 }, 4, 10)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new int[] { 2, 6 }, 4, 10)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new int[] { 2, 6 }, -1, 10)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new int[] { 2, 6 }, -1, 10)); assertEquals("Index: -1, Length: 2", e.getMessage()); // long tests long[] longArray = ArrayUtils.add(new long[] { 1L }, 0, 2L); assertArrayEquals(new long[] { 2L, 1L }, longArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((long[]) null, -1, 2L)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((long[]) null, -1, 2L)); assertEquals("Index: -1, Length: 0", e.getMessage()); longArray = ArrayUtils.add(new long[] { 2L, 6L }, 2, 10L); assertArrayEquals(new long[] { 2L, 6L, 10L }, longArray); @@ -614,15 +613,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new long[] { -4L, 2L, 6L }, longArray); longArray = ArrayUtils.add(new long[] { 2L, 6L, 3L }, 2, 1L); assertArrayEquals(new long[] { 2L, 6L, 1L, 3L }, longArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new long[] { 2L, 6L }, 4, 10L)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new long[] { 2L, 6L }, 4, 10L)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new long[] { 2L, 6L }, -1, 10L)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new long[] { 2L, 6L }, -1, 10L)); assertEquals("Index: -1, Length: 2", e.getMessage()); // float tests float[] floatArray = ArrayUtils.add(new float[] { 1.1f }, 0, 2.2f); assertArrayEquals(new float[] { 2.2f, 1.1f }, floatArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((float[]) null, -1, 2.2f)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((float[]) null, -1, 2.2f)); assertEquals("Index: -1, Length: 0", e.getMessage()); floatArray = ArrayUtils.add(new float[] { 2.3f, 6.4f }, 2, 10.5f); assertArrayEquals(new float[] { 2.3f, 6.4f, 10.5f }, floatArray); @@ -630,15 +629,15 @@ void testAddObjectAtIndex() { assertArrayEquals(new float[] { -4.8f, 2.6f, 6.7f }, floatArray); floatArray = ArrayUtils.add(new float[] { 2.9f, 6.0f, 0.3f }, 2, 1.0f); assertArrayEquals(new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, 4, 10.5f)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, 4, 10.5f)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, -1, 10.5f)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, -1, 10.5f)); assertEquals("Index: -1, Length: 2", e.getMessage()); // double tests double[] doubleArray = ArrayUtils.add(new double[] { 1.1 }, 0, 2.2); assertArrayEquals(new double[] { 2.2, 1.1 }, doubleArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(null, -1, 2.2)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(null, -1, 2.2)); assertEquals("Index: -1, Length: 0", e.getMessage()); doubleArray = ArrayUtils.add(new double[] { 2.3, 6.4 }, 2, 10.5); assertArrayEquals(new double[] { 2.3, 6.4, 10.5 }, doubleArray); @@ -646,9 +645,9 @@ void testAddObjectAtIndex() { assertArrayEquals(new double[] { -4.8, 2.6, 6.7 }, doubleArray); doubleArray = ArrayUtils.add(new double[] { 2.9, 6.0, 0.3 }, 2, 1.0); assertArrayEquals(new double[] { 2.9, 6.0, 1.0, 0.3 }, doubleArray); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new double[] { 2.3, 6.4 }, 4, 10.5)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new double[] { 2.3, 6.4 }, 4, 10.5)); assertEquals("Index: 4, Length: 2", e.getMessage()); - e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new double[] { 2.3, 6.4 }, -1, 10.5)); + e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new double[] { 2.3, 6.4 }, -1, 10.5)); assertEquals("Index: -1, Length: 2", e.getMessage()); } @@ -660,7 +659,7 @@ void testJira567() { assertEquals(2, n.length); assertEquals(Number.class, n.getClass().getComponentType()); // Invalid - can't store Long in Integer array - assertIllegalArgumentException(() -> ArrayUtils.addAll(new Integer[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) })); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.addAll(new Integer[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) })); } @Test @@ -668,8 +667,8 @@ void testJira567() { void testLANG571() { final String[] stringArray = null; final String aString = null; - assertIllegalArgumentException(() -> ArrayUtils.add(stringArray, aString)); - assertIllegalArgumentException(() -> ArrayUtils.add(stringArray, 0, aString)); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.add(stringArray, aString)); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.add(stringArray, 0, aString)); } } diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java index 293bf691178..2eff1ad04f5 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java @@ -17,10 +17,10 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -42,8 +42,8 @@ void testInsertBooleans() { assertArrayEquals(new boolean[0], ArrayUtils.insert(0, new boolean[0], null)); assertNull(ArrayUtils.insert(42, (boolean[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new boolean[]{false, true, false, true}, ArrayUtils.insert(0, array, false)); assertArrayEquals(new boolean[]{true, false, false, true}, ArrayUtils.insert(1, array, false)); @@ -66,8 +66,8 @@ void testInsertBytes() { assertArrayEquals(new byte[0], ArrayUtils.insert(0, new byte[0], null)); assertNull(ArrayUtils.insert(42, (byte[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new byte[]{0, 1, 2, 3}, ArrayUtils.insert(0, array, (byte) 0)); assertArrayEquals(new byte[]{1, 0, 2, 3}, ArrayUtils.insert(1, array, (byte) 0)); @@ -90,8 +90,8 @@ void testInsertChars() { assertArrayEquals(new char[0], ArrayUtils.insert(0, new char[0], null)); assertNull(ArrayUtils.insert(42, (char[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new char[]{'z', 'a', 'b', 'c'}, ArrayUtils.insert(0, array, 'z')); assertArrayEquals(new char[]{'a', 'z', 'b', 'c'}, ArrayUtils.insert(1, array, 'z')); @@ -115,8 +115,8 @@ void testInsertDoubles() { assertArrayEquals(new double[0], ArrayUtils.insert(0, new double[0], null), delta); assertNull(ArrayUtils.insert(42, (double[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new double[]{0, 1, 2, 3}, ArrayUtils.insert(0, array, 0), delta); assertArrayEquals(new double[]{1, 0, 2, 3}, ArrayUtils.insert(1, array, 0), delta); @@ -140,8 +140,8 @@ void testInsertFloats() { assertArrayEquals(new float[0], ArrayUtils.insert(0, new float[0], null), delta); assertNull(ArrayUtils.insert(42, (float[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new float[]{0, 1, 2, 3}, ArrayUtils.insert(0, array, 0), delta); assertArrayEquals(new float[]{1, 0, 2, 3}, ArrayUtils.insert(1, array, 0), delta); @@ -164,8 +164,8 @@ void testInsertGenericArray() { assertArrayEquals(new String[0], ArrayUtils.insert(0, new String[0], (String[]) null)); assertNull(ArrayUtils.insert(42, null, (String[]) null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new String[]{"z", "a", "b", "c"}, ArrayUtils.insert(0, array, "z")); assertArrayEquals(new String[]{"a", "z", "b", "c"}, ArrayUtils.insert(1, array, "z")); @@ -188,8 +188,8 @@ void testInsertInts() { assertArrayEquals(new int[0], ArrayUtils.insert(0, new int[0], null)); assertNull(ArrayUtils.insert(42, (int[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new int[]{0, 1, 2, 3}, ArrayUtils.insert(0, array, 0)); assertArrayEquals(new int[]{1, 0, 2, 3}, ArrayUtils.insert(1, array, 0)); @@ -212,8 +212,8 @@ void testInsertLongs() { assertArrayEquals(new long[0], ArrayUtils.insert(0, new long[0], null)); assertNull(ArrayUtils.insert(42, (long[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new long[]{0, 1, 2, 3}, ArrayUtils.insert(0, array, 0)); assertArrayEquals(new long[]{1, 0, 2, 3}, ArrayUtils.insert(1, array, 0)); @@ -236,8 +236,8 @@ void testInsertShorts() { assertArrayEquals(new short[0], ArrayUtils.insert(0, new short[0], null)); assertNull(ArrayUtils.insert(42, (short[]) null, null)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(-1, array, array)); - assertIndexOutOfBoundsException(() -> ArrayUtils.insert(array.length + 1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array)); assertArrayEquals(new short[]{0, 1, 2, 3}, ArrayUtils.insert(0, array, (short) 0)); assertArrayEquals(new short[]{1, 0, 2, 3}, ArrayUtils.insert(1, array, (short) 0)); diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java index 56d4f3ca28d..df4b95d8fab 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java @@ -17,11 +17,11 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -95,12 +95,12 @@ void testRemoveAllBooleanArray() { @Test void testRemoveAllBooleanArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new boolean[] { true, false }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new boolean[] { true, false }, -1)); } @Test void testRemoveAllBooleanArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new boolean[] { true, false }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new boolean[] { true, false }, 2)); } @Test @@ -167,12 +167,12 @@ void testRemoveAllByteArray() { @Test void testRemoveAllByteArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new byte[] { 1, 2 }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new byte[] { 1, 2 }, -1)); } @Test void testRemoveAllByteArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new byte[] { 1, 2 }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new byte[] { 1, 2 }, 2)); } @Test @@ -239,12 +239,12 @@ void testRemoveAllCharArray() { @Test void testRemoveAllCharArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new char[] { 'a', 'b' }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new char[] { 'a', 'b' }, -1)); } @Test void testRemoveAllCharArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new char[] { 'a', 'b' }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new char[] { 'a', 'b' }, 2)); } @Test @@ -311,12 +311,12 @@ void testRemoveAllDoubleArray() { @Test void testRemoveAllDoubleArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new double[] { 1, 2 }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new double[] { 1, 2 }, -1)); } @Test void testRemoveAllDoubleArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new double[] { 1, 2 }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new double[] { 1, 2 }, 2)); } @Test @@ -383,12 +383,12 @@ void testRemoveAllFloatArray() { @Test void testRemoveAllFloatArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new float[] { 1, 2 }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new float[] { 1, 2 }, -1)); } @Test void testRemoveAllFloatArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new float[] { 1, 2 }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new float[] { 1, 2 }, 2)); } @Test @@ -461,12 +461,12 @@ void testRemoveAllIntArray() { @Test void testRemoveAllIntArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new int[] { 1, 2 }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new int[] { 1, 2 }, -1)); } @Test void testRemoveAllIntArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new int[] { 1, 2 }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new int[] { 1, 2 }, 2)); } @Test @@ -533,12 +533,12 @@ void testRemoveAllLongArray() { @Test void testRemoveAllLongArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new long[] { 1, 2 }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new long[] { 1, 2 }, -1)); } @Test void testRemoveAllLongArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new long[] { 1, 2 }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new long[] { 1, 2 }, 2)); } @Test @@ -748,12 +748,12 @@ void testRemoveAllObjectArray() { @Test void testRemoveAllObjectArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new Object[] { "a", "b" }, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new Object[] { "a", "b" }, -1)); } @Test void testRemoveAllObjectArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new Object[] { "a", "b" }, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new Object[] { "a", "b" }, 2)); } @Test @@ -820,12 +820,12 @@ void testRemoveAllShortArray() { @Test void testRemoveAllShortArrayNegativeIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new short[] { 1, 2 }, -1, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new short[] { 1, 2 }, -1, 0)); } @Test void testRemoveAllShortArrayOutOfBoundsIndex() { - assertIndexOutOfBoundsException(() -> ArrayUtils.removeAll(new short[] { 1, 2 }, 2, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.removeAll(new short[] { 1, 2 }, 2, 0)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java index dc8cc77ae88..7021b741643 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java @@ -17,10 +17,10 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -422,9 +422,9 @@ void testRemoveBooleanArray() { array = ArrayUtils.remove(new boolean[] {true, false, true}, 1); assertArrayEquals(new boolean[]{true, true}, array); assertEquals(Boolean.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new boolean[] {true, false}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new boolean[] {true, false}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((boolean[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new boolean[] {true, false}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new boolean[] {true, false}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((boolean[]) null, 0)); } @Test @@ -442,9 +442,9 @@ void testRemoveByteArray() { array = ArrayUtils.remove(new byte[] {1, 2, 1}, 1); assertArrayEquals(new byte[]{1, 1}, array); assertEquals(Byte.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new byte[] {1, 2}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new byte[] {1, 2}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((byte[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new byte[] {1, 2}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new byte[] {1, 2}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((byte[]) null, 0)); } @Test @@ -462,9 +462,9 @@ void testRemoveCharArray() { array = ArrayUtils.remove(new char[] {'a', 'b', 'c'}, 1); assertArrayEquals(new char[]{'a', 'c'}, array); assertEquals(Character.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new char[] {'a', 'b'}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new char[] {'a', 'b'}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((char[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new char[] {'a', 'b'}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new char[] {'a', 'b'}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((char[]) null, 0)); } @Test @@ -482,9 +482,9 @@ void testRemoveDoubleArray() { array = ArrayUtils.remove(new double[] {1, 2, 1}, 1); assertArrayEquals(new double[]{1, 1}, array); assertEquals(Double.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new double[] {1, 2}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new double[] {1, 2}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((double[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new double[] {1, 2}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new double[] {1, 2}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((double[]) null, 0)); } @Test @@ -673,9 +673,9 @@ void testRemoveFloatArray() { array = ArrayUtils.remove(new float[] {1, 2, 1}, 1); assertArrayEquals(new float[]{1, 1}, array); assertEquals(Float.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new float[] {1, 2}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new float[] {1, 2}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((float[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new float[] {1, 2}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new float[] {1, 2}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((float[]) null, 0)); } @Test @@ -693,9 +693,9 @@ void testRemoveIntArray() { array = ArrayUtils.remove(new int[] {1, 2, 1}, 1); assertArrayEquals(new int[]{1, 1}, array); assertEquals(Integer.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new int[] {1, 2}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new int[] {1, 2}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((int[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new int[] {1, 2}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new int[] {1, 2}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((int[]) null, 0)); } @Test @@ -713,9 +713,9 @@ void testRemoveLongArray() { array = ArrayUtils.remove(new long[] {1, 2, 1}, 1); assertArrayEquals(new long[]{1, 1}, array); assertEquals(Long.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new long[] {1, 2}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new long[] {1, 2}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((long[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new long[] {1, 2}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new long[] {1, 2}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((long[]) null, 0)); } @Test @@ -749,9 +749,9 @@ void testRemoveObjectArray() { array = ArrayUtils.remove(new Object[] {"a", "b", "c"}, 1); assertArrayEquals(new Object[]{"a", "c"}, array); assertEquals(Object.class, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new Object[] {"a", "b"}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new Object[] {"a", "b"}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((Object[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new Object[] {"a", "b"}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new Object[] {"a", "b"}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((Object[]) null, 0)); } @Test @@ -769,8 +769,8 @@ void testRemoveShortArray() { array = ArrayUtils.remove(new short[] {1, 2, 1}, 1); assertArrayEquals(new short[]{1, 1}, array); assertEquals(Short.TYPE, array.getClass().getComponentType()); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new short[] {1, 2}, -1)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new short[] {1, 2}, 2)); - assertIndexOutOfBoundsException(() -> ArrayUtils.remove((short[]) null, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new short[] {1, 2}, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new short[] {1, 2}, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((short[]) null, 0)); } } diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index edd4af9b13f..9c77e944499 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -540,7 +539,7 @@ void testGetLength() { assertEquals(0, ArrayUtils.getLength(emptyBooleanArray)); assertEquals(1, ArrayUtils.getLength(notEmptyBooleanArray)); - assertIllegalArgumentException(() -> ArrayUtils.getLength("notAnArray")); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.getLength("notAnArray")); } @Test @@ -2205,7 +2204,7 @@ void testNullToEmptyGenericNull() { @Test void testNullToEmptyGenericNullType() { final TestClass[] input = {}; - assertIllegalArgumentException(() -> ArrayUtils.nullToEmpty(input, null)); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.nullToEmpty(input, null)); } @Test @@ -4373,9 +4372,9 @@ void testSameLengthShort() { @Test void testSameType() { - assertIllegalArgumentException(() -> ArrayUtils.isSameType(null, null)); - assertIllegalArgumentException(() -> ArrayUtils.isSameType(null, new Object[0])); - assertIllegalArgumentException(() -> ArrayUtils.isSameType(new Object[0], null)); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(null, null)); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(null, new Object[0])); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(new Object[0], null)); assertTrue(ArrayUtils.isSameType(new Object[0], new Object[0])); assertFalse(ArrayUtils.isSameType(new String[0], new Object[0])); @@ -6510,9 +6509,9 @@ void testToMap() { assertEquals("bar", map.get("foo")); assertEquals("world", map.get("hello")); assertNull(ArrayUtils.toMap(null)); - assertIllegalArgumentException(() -> ArrayUtils.toMap(new String[][] { { "foo", "bar" }, { "short" } })); - assertIllegalArgumentException(() -> ArrayUtils.toMap(new Object[] { new Object[] { "foo", "bar" }, "illegal type" })); - assertIllegalArgumentException(() -> ArrayUtils.toMap(new Object[] { new Object[] { "foo", "bar" }, null })); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toMap(new String[][] { { "foo", "bar" }, { "short" } })); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toMap(new Object[] { new Object[] { "foo", "bar" }, "illegal type" })); + assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toMap(new Object[] { new Object[] { "foo", "bar" }, null })); map = ArrayUtils.toMap(new Object[] { new Map.Entry() { @Override diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index 8907ab7e2f8..f0a14de9794 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3; import static org.apache.commons.lang3.ArraySorter.sort; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -25,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; @@ -116,7 +116,7 @@ void test_toBoolean_int_int_int() { @Test void test_toBoolean_int_int_int_noMatch() { - assertIllegalArgumentException(() -> BooleanUtils.toBoolean(8, 6, 7)); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean(8, 6, 7)); } @Test @@ -133,13 +133,13 @@ void test_toBoolean_Integer_Integer_Integer() { @Test void test_toBoolean_Integer_Integer_Integer_noMatch() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean(Integer.valueOf(8), Integer.valueOf(6), Integer.valueOf(7))); } @Test void test_toBoolean_Integer_Integer_Integer_nullValue() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean(null, Integer.valueOf(6), Integer.valueOf(7))); } @@ -207,12 +207,12 @@ void test_toBoolean_String_String_String() { @Test void test_toBoolean_String_String_String_noMatch() { - assertIllegalArgumentException(() -> BooleanUtils.toBoolean("X", "Y", "N")); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean("X", "Y", "N")); } @Test void test_toBoolean_String_String_String_nullValue() { - assertIllegalArgumentException(() -> BooleanUtils.toBoolean(null, "Y", "N")); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBoolean(null, "Y", "N")); } @Test @@ -241,7 +241,7 @@ void test_toBooleanObject_int_int_int() { @Test void test_toBooleanObject_int_int_int_noMatch() { - assertIllegalArgumentException(() -> BooleanUtils.toBooleanObject(9, 6, 7, 8)); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject(9, 6, 7, 8)); } @Test @@ -269,13 +269,13 @@ void test_toBooleanObject_Integer_Integer_Integer_Integer() { @Test void test_toBooleanObject_Integer_Integer_Integer_Integer_noMatch() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject(Integer.valueOf(9), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8))); } @Test void test_toBooleanObject_Integer_Integer_Integer_Integer_nullValue() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject(null, Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8))); } @@ -330,12 +330,12 @@ void test_toBooleanObject_String_String_String_String() { @Test void test_toBooleanObject_String_String_String_String_noMatch() { - assertIllegalArgumentException(() -> BooleanUtils.toBooleanObject("X", "Y", "N", "U")); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject("X", "Y", "N", "U")); } @Test void test_toBooleanObject_String_String_String_String_nullValue() { - assertIllegalArgumentException(() -> BooleanUtils.toBooleanObject(null, "Y", "N", "U")); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.toBooleanObject(null, "Y", "N", "U")); } @Test @@ -450,7 +450,7 @@ void test_values() { @Test void testAnd_object_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.and(new Boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.and(new Boolean[] {})); } @Test @@ -567,7 +567,7 @@ void testAnd_object_validInput_3items() { @Test void testAnd_primitive_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.and(new boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.and(new boolean[] {})); } @Test @@ -649,7 +649,7 @@ void testConstructor() { @Test void testOneHot_object_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.oneHot(new Boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.oneHot(new Boolean[] {})); } @Test @@ -716,7 +716,7 @@ void testOneHot_object_validInput_3items() { @Test void testOneHot_primitive_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.oneHot(new boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.oneHot(new boolean[] {})); } @Test @@ -767,7 +767,7 @@ void testOneHot_primitive_validInput_3items() { @Test void testOr_object_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.or(new Boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.or(new Boolean[] {})); } @Test @@ -884,7 +884,7 @@ void testOr_object_validInput_3items() { @Test void testOr_primitive_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.or(new boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.or(new boolean[] {})); } @Test @@ -948,7 +948,7 @@ void testOr_primitive_validInput_3items() { @Test void testXor_object_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.xor(new Boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.xor(new Boolean[] {})); } @Test @@ -1085,7 +1085,7 @@ void testXor_object_validInput_3items() { @Test void testXor_primitive_emptyInput() { - assertIllegalArgumentException(() -> BooleanUtils.xor(new boolean[] {})); + assertThrows(IllegalArgumentException.class, () -> BooleanUtils.xor(new boolean[] {})); } @Test diff --git a/src/test/java/org/apache/commons/lang3/CachedRandomBitsTest.java b/src/test/java/org/apache/commons/lang3/CachedRandomBitsTest.java index 0c49c5cde6c..3ee1dbfc44e 100644 --- a/src/test/java/org/apache/commons/lang3/CachedRandomBitsTest.java +++ b/src/test/java/org/apache/commons/lang3/CachedRandomBitsTest.java @@ -16,8 +16,8 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Objects; import java.util.Random; @@ -69,8 +69,8 @@ void testNext(final int cacheSize) { final CachedRandomBits arb = new CachedRandomBits(cacheSize, random); - assertIllegalArgumentException(() -> arb.nextBits(0)); - assertIllegalArgumentException(() -> arb.nextBits(33)); + assertThrows(IllegalArgumentException.class, () -> arb.nextBits(0)); + assertThrows(IllegalArgumentException.class, () -> arb.nextBits(33)); assertEquals(0x11, arb.nextBits(8)); assertEquals(0x12, arb.nextBits(8)); diff --git a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java index 1ea19165b89..33c001347b9 100644 --- a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -296,12 +295,12 @@ void testSubSequence() { @Test void testSubSequenceNegativeStart() { - assertIndexOutOfBoundsException(() -> CharSequenceUtils.subSequence(StringUtils.EMPTY, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> CharSequenceUtils.subSequence(StringUtils.EMPTY, -1)); } @Test void testSubSequenceTooLong() { - assertIndexOutOfBoundsException(() -> CharSequenceUtils.subSequence(StringUtils.EMPTY, 1)); + assertThrows(IndexOutOfBoundsException.class, () -> CharSequenceUtils.subSequence(StringUtils.EMPTY, 1)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java index f187b7de966..5d627394aa4 100644 --- a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -24,6 +23,7 @@ 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.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; @@ -216,7 +216,7 @@ void testToChar_String() { assertEquals('A', CharUtils.toChar("A")); assertEquals('B', CharUtils.toChar("BA")); assertNullPointerException(() -> CharUtils.toChar((String) null)); - assertIllegalArgumentException(() -> CharUtils.toChar("")); + assertThrows(IllegalArgumentException.class, () -> CharUtils.toChar("")); } @Test @@ -271,7 +271,7 @@ void testToIntValue_char() { assertEquals(7, CharUtils.toIntValue('7')); assertEquals(8, CharUtils.toIntValue('8')); assertEquals(9, CharUtils.toIntValue('9')); - assertIllegalArgumentException(() -> CharUtils.toIntValue('a')); + assertThrows(IllegalArgumentException.class, () -> CharUtils.toIntValue('a')); } @Test @@ -286,7 +286,7 @@ void testToIntValue_Character() { assertEquals(0, CharUtils.toIntValue(Character.valueOf('0'))); assertEquals(3, CharUtils.toIntValue(Character.valueOf('3'))); assertNullPointerException(() -> CharUtils.toIntValue(null)); - assertIllegalArgumentException(() -> CharUtils.toIntValue(CHARACTER_A)); + assertThrows(IllegalArgumentException.class, () -> CharUtils.toIntValue(CHARACTER_A)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java index 320391193aa..b823519fa05 100644 --- a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -171,13 +170,13 @@ void test_getAbbreviatedName_Class() { @Test @DisplayName("When the desired length is negative then exception is thrown") void test_getAbbreviatedName_Class_NegativeLen() { - assertIllegalArgumentException(() -> ClassUtils.getAbbreviatedName(String.class, -10)); + assertThrows(IllegalArgumentException.class, () -> ClassUtils.getAbbreviatedName(String.class, -10)); } @Test @DisplayName("When the desired length is zero then exception is thrown") void test_getAbbreviatedName_Class_ZeroLen() { - assertIllegalArgumentException(() -> ClassUtils.getAbbreviatedName(String.class, 0)); + assertThrows(IllegalArgumentException.class, () -> ClassUtils.getAbbreviatedName(String.class, 0)); } @Test @@ -575,7 +574,7 @@ void test_getShortCanonicalName_String() { // fixed without backward compatibility break assertEquals("int[]", int[].class.getCanonicalName()); assertEquals("[I", int[].class.getName()); - assertIllegalArgumentException(() -> ClassUtils.getShortCanonicalName(StringUtils.repeat("[", 256) + "I")); + assertThrows(IllegalArgumentException.class, () -> ClassUtils.getShortCanonicalName(StringUtils.repeat("[", 256) + "I")); assertEquals("int" + StringUtils.repeat("[]", 255), ClassUtils.getShortCanonicalName(StringUtils.repeat("[", 255) + "I")); // Inner types... the problem is that these are not canonical names, classes with this name do not even have canonical // name diff --git a/src/test/java/org/apache/commons/lang3/ConversionTest.java b/src/test/java/org/apache/commons/lang3/ConversionTest.java index 2fab1f91240..0031ebe2ded 100644 --- a/src/test/java/org/apache/commons/lang3/ConversionTest.java +++ b/src/test/java/org/apache/commons/lang3/ConversionTest.java @@ -16,8 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -49,9 +47,10 @@ static String dbgPrint(final boolean[] src) { } @ParameterizedTest - @ValueSource(ints = { Integer.MIN_VALUE, -1, 8, 9, Integer.MAX_VALUE }) + @ValueSource(ints = {-1, 8, 99}) public void binaryBeMsb0ToHexDigitPosOutsideArray(final int index) { - assertIndexOutOfBoundsException(() -> Conversion.binaryBeMsb0ToHexDigit(new boolean[8], index)); + assertThrows(IndexOutOfBoundsException.class, + () -> Conversion.binaryBeMsb0ToHexDigit(new boolean[8], index)); } /** @@ -59,25 +58,44 @@ public void binaryBeMsb0ToHexDigitPosOutsideArray(final int index) { */ @Test void testBinaryBeMsb0ToHexDigit() { - assertEquals('0', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, false, false, false })); - assertEquals('1', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, false, false, true })); - assertEquals('2', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, false, true, false })); - assertEquals('3', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, false, true, true })); - assertEquals('4', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, true, false, false })); - assertEquals('5', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, true, false, true })); - assertEquals('6', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, true, true, false })); - assertEquals('7', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { false, true, true, true })); - assertEquals('8', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, false, false, false })); - assertEquals('9', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, false, false, true })); - assertEquals('a', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, false, true, false })); - assertEquals('b', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, false, true, true })); - assertEquals('c', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, true, false, false })); - assertEquals('d', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, true, false, true })); - assertEquals('e', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, true, true, false })); - assertEquals('f', Conversion.binaryBeMsb0ToHexDigit(new boolean[] { true, true, true, true })); - assertEquals('4', Conversion.binaryBeMsb0ToHexDigit( - new boolean[] { true, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false })); - assertIllegalArgumentException(() -> Conversion.binaryBeMsb0ToHexDigit(new boolean[] {})); + assertEquals( + '0', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, false, false, false})); + assertEquals( + '1', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, false, false, true})); + assertEquals( + '2', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, false, true, false})); + assertEquals( + '3', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, false, true, true})); + assertEquals( + '4', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, true, false, false})); + assertEquals( + '5', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, true, false, true})); + assertEquals( + '6', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, true, true, false})); + assertEquals( + '7', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{false, true, true, true})); + assertEquals( + '8', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, false, false, false})); + assertEquals( + '9', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, false, false, true})); + assertEquals( + 'a', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, false, true, false})); + assertEquals( + 'b', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, false, true, true})); + assertEquals( + 'c', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, true, false, false})); + assertEquals( + 'd', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, true, false, true})); + assertEquals( + 'e', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, true, true, false})); + assertEquals( + 'f', Conversion.binaryBeMsb0ToHexDigit(new boolean[]{true, true, true, true})); + assertEquals( + '4', + Conversion.binaryBeMsb0ToHexDigit(new boolean[]{ + true, false, false, false, false, false, false, false, false, false, false, + false, false, true, false, false})); + assertThrows(IllegalArgumentException.class, () -> Conversion.binaryBeMsb0ToHexDigit(new boolean[]{})); } /** @@ -173,7 +191,7 @@ void testBinaryToHexDigit() { assertEquals('1', Conversion.binaryToHexDigit(new boolean[]{true})); assertEquals( 'f', Conversion.binaryToHexDigit(new boolean[]{true, true, true, true, true})); - assertIllegalArgumentException(() -> Conversion.binaryToHexDigit(new boolean[]{})); + assertThrows(IllegalArgumentException.class, () -> Conversion.binaryToHexDigit(new boolean[]{})); } /** @@ -272,7 +290,7 @@ void testBinaryToHexDigitMsb0_bits() { 'e', Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{true, true, true, false})); assertEquals( 'f', Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{true, true, true, true})); - assertIllegalArgumentException(() -> Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{})); + assertThrows(IllegalArgumentException.class, () -> Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{})); } @Test @@ -561,7 +579,7 @@ void testHexDigitMsb0ToBinary() { new boolean[]{true, true, true, true}, Conversion.hexDigitMsb0ToBinary('F')); assertArrayEquals( new boolean[]{true, true, true, true}, Conversion.hexDigitMsb0ToBinary('f')); - assertIllegalArgumentException(() -> Conversion.hexDigitMsb0ToBinary('G')); + assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitMsb0ToBinary('G')); } /** @@ -591,7 +609,7 @@ void testHexDigitMsb0ToInt() { assertEquals(0x7, Conversion.hexDigitMsb0ToInt('e')); assertEquals(0xF, Conversion.hexDigitMsb0ToInt('F')); assertEquals(0xF, Conversion.hexDigitMsb0ToInt('f')); - assertIllegalArgumentException(() -> Conversion.hexDigitMsb0ToInt('G')); + assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitMsb0ToInt('G')); } /** @@ -643,7 +661,7 @@ void testHexDigitToBinary() { new boolean[]{true, true, true, true}, Conversion.hexDigitToBinary('F')); assertArrayEquals( new boolean[]{true, true, true, true}, Conversion.hexDigitToBinary('f')); - assertIllegalArgumentException(() -> Conversion.hexDigitToBinary('G')); + assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitToBinary('G')); } /** @@ -673,7 +691,7 @@ void testHexDigitToInt() { assertEquals(14, Conversion.hexDigitToInt('e')); assertEquals(15, Conversion.hexDigitToInt('F')); assertEquals(15, Conversion.hexDigitToInt('f')); - assertIllegalArgumentException(() -> Conversion.hexDigitToInt('G')); + assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitToInt('G')); } /** @@ -688,9 +706,6 @@ void testHexToByte() { assertEquals((byte) 0xFD, Conversion.hexToByte(src, 1, (byte) 0, 0, 2)); assertEquals((byte) 0x34, Conversion.hexToByte(src, 0, (byte) 0x34, 0, 0)); assertEquals((byte) 0x84, Conversion.hexToByte(src, 17, (byte) 0x34, 4, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToByte(src, src.length(), (byte) 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToByte(src, Integer.MIN_VALUE, (byte) 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToByte(src, Integer.MAX_VALUE, (byte) 0, 0, 1)); } /** @@ -705,9 +720,6 @@ void testHexToInt() { assertEquals(0x01C0F1FD, Conversion.hexToInt(src, 1, 0, 0, 8)); assertEquals(0x12345679, Conversion.hexToInt(src, 0, 0x12345679, 0, 0)); assertEquals(0x87645679, Conversion.hexToInt(src, 15, 0x12345679, 20, 3)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToInt(src, src.length(), 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToInt(src, Integer.MIN_VALUE, 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToInt(src, Integer.MAX_VALUE, 0, 0, 1)); } /** @@ -720,11 +732,10 @@ void testHexToLong() { assertEquals(0x000000000000000CL, Conversion.hexToLong(src, 0, 0L, 0, 1)); assertEquals(0x000000001C0F1FDCL, Conversion.hexToLong(src, 0, 0L, 0, 8)); assertEquals(0x0000000001C0F1FDL, Conversion.hexToLong(src, 1, 0L, 0, 8)); - assertEquals(0x123456798ABCDEF0L, Conversion.hexToLong(src, 0, 0x123456798ABCDEF0L, 0, 0)); - assertEquals(0x1234567876BCDEF0L, Conversion.hexToLong(src, 15, 0x123456798ABCDEF0L, 24, 3)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToLong(src, src.length(), 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToLong(src, Integer.MIN_VALUE, 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToLong(src, Integer.MAX_VALUE, 0, 0, 1)); + assertEquals( + 0x123456798ABCDEF0L, Conversion.hexToLong(src, 0, 0x123456798ABCDEF0L, 0, 0)); + assertEquals( + 0x1234567876BCDEF0L, Conversion.hexToLong(src, 15, 0x123456798ABCDEF0L, 24, 3)); } /** @@ -739,9 +750,6 @@ void testHexToShort() { assertEquals((short) 0xF1FD, Conversion.hexToShort(src, 1, (short) 0, 0, 4)); assertEquals((short) 0x1234, Conversion.hexToShort(src, 0, (short) 0x1234, 0, 0)); assertEquals((short) 0x8764, Conversion.hexToShort(src, 15, (short) 0x1234, 4, 3)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToShort(src, src.length(), (short) 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToShort(src, Integer.MIN_VALUE, (short) 0, 0, 1)); - assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToShort(src, Integer.MAX_VALUE, (short) 0, 0, 1)); } /** @@ -1001,7 +1009,7 @@ void testIntToHexDigit() { assertEquals('d', Conversion.intToHexDigit(13)); assertEquals('e', Conversion.intToHexDigit(14)); assertEquals('f', Conversion.intToHexDigit(15)); - assertIllegalArgumentException(() -> Conversion.intToHexDigit(16)); + assertThrows(IllegalArgumentException.class, () -> Conversion.intToHexDigit(16)); } /** @@ -1025,7 +1033,7 @@ void testIntToHexDigitMsb0() { assertEquals('b', Conversion.intToHexDigitMsb0(13)); assertEquals('7', Conversion.intToHexDigitMsb0(14)); assertEquals('f', Conversion.intToHexDigitMsb0(15)); - assertIllegalArgumentException(() -> Conversion.intToHexDigitMsb0(16)); + assertThrows(IllegalArgumentException.class, () -> Conversion.intToHexDigitMsb0(16)); } /** diff --git a/src/test/java/org/apache/commons/lang3/DoubleRangeTest.java b/src/test/java/org/apache/commons/lang3/DoubleRangeTest.java index 64c0c261114..86ea745192b 100644 --- a/src/test/java/org/apache/commons/lang3/DoubleRangeTest.java +++ b/src/test/java/org/apache/commons/lang3/DoubleRangeTest.java @@ -17,13 +17,13 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Comparator; @@ -188,12 +188,12 @@ void testIntersectionWith() { @Test void testIntersectionWithNonOverlapping() { - assertIllegalArgumentException(() -> range1.intersectionWith(Range.of(0d, 9d))); + assertThrows(IllegalArgumentException.class, () -> range1.intersectionWith(Range.of(0d, 9d))); } @Test void testIntersectionWithNull() { - assertIllegalArgumentException(() -> range1.intersectionWith(null)); + assertThrows(IllegalArgumentException.class, () -> range1.intersectionWith(null)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java index 89c3161ebd8..856db2fdb74 100644 --- a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java @@ -18,11 +18,11 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; 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.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; @@ -79,13 +79,13 @@ void testGenerateBitVector() { @Test void testGenerateBitVector_longClass() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(TooMany.class, EnumSet.of(TooMany.A1))); } @Test void testGenerateBitVector_longClassWithArray() { - assertIllegalArgumentException(() -> EnumUtils.generateBitVector(TooMany.class, TooMany.A1)); + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(TooMany.class, TooMany.A1)); } @SuppressWarnings("unchecked") @@ -95,7 +95,7 @@ void testGenerateBitVector_nonEnumClass() { final Class rawType = Object.class; @SuppressWarnings("rawtypes") final List rawList = new ArrayList(); - assertIllegalArgumentException(() -> EnumUtils.generateBitVector(rawType, rawList)); + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType, rawList)); } @SuppressWarnings("unchecked") @@ -103,7 +103,7 @@ void testGenerateBitVector_nonEnumClass() { void testGenerateBitVector_nonEnumClassWithArray() { @SuppressWarnings("rawtypes") final Class rawType = Object.class; - assertIllegalArgumentException(() -> EnumUtils.generateBitVector(rawType)); + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType)); } @Test @@ -113,7 +113,7 @@ void testGenerateBitVector_nullArray() { @Test void testGenerateBitVector_nullArrayElement() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(Traffic.class, Traffic.RED, null)); } @@ -191,7 +191,7 @@ void testGenerateBitVectors_nonEnumClass() { final Class rawType = Object.class; @SuppressWarnings("rawtypes") final List rawList = new ArrayList(); - assertIllegalArgumentException(() -> EnumUtils.generateBitVectors(rawType, rawList)); + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType, rawList)); } @SuppressWarnings("unchecked") @@ -199,7 +199,7 @@ void testGenerateBitVectors_nonEnumClass() { void testGenerateBitVectors_nonEnumClassWithArray() { @SuppressWarnings("rawtypes") final Class rawType = Object.class; - assertIllegalArgumentException(() -> EnumUtils.generateBitVectors(rawType)); + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType)); } @Test @@ -209,7 +209,7 @@ void testGenerateBitVectors_nullArray() { @Test void testGenerateBitVectors_nullArrayElement() { - assertIllegalArgumentException( + assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, null)); } @@ -511,7 +511,7 @@ void testProcessBitVector() { @Test void testProcessBitVector_longClass() { - assertIllegalArgumentException(() -> EnumUtils.processBitVector(TooMany.class, 0L)); + assertThrows(IllegalArgumentException.class, () -> EnumUtils.processBitVector(TooMany.class, 0L)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java b/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java index 005bcaba075..3db63dcbe59 100644 --- a/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java +++ b/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java @@ -17,13 +17,13 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Comparator; @@ -187,12 +187,12 @@ void testIntersectionWith() { @Test void testIntersectionWithNonOverlapping() { - assertIllegalArgumentException(() -> range1.intersectionWith(Range.of(0, 9))); + assertThrows(IllegalArgumentException.class, () -> range1.intersectionWith(Range.of(0, 9))); } @Test void testIntersectionWithNull() { - assertIllegalArgumentException(() -> range1.intersectionWith(null)); + assertThrows(IllegalArgumentException.class, () -> range1.intersectionWith(null)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/LangAssertions.java b/src/test/java/org/apache/commons/lang3/LangAssertions.java index 9c6c903aca8..e6647f60367 100644 --- a/src/test/java/org/apache/commons/lang3/LangAssertions.java +++ b/src/test/java/org/apache/commons/lang3/LangAssertions.java @@ -27,82 +27,12 @@ */ public class LangAssertions { - /** - * Asserts that execution of the given {@code executable} throws a {@link IllegalArgumentException}. - * - *

- * The assertion passes if the thrown exception type is the same as {@link IllegalArgumentException} or a subtype. To check for the exact thrown type use - * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is - * thrown, this method fails. - *

- * - * @param executable What to test. - * @return The thrown IllegalArgumentException. - * @see Assertions#assertThrowsExactly(Class, Executable) - */ - public static IllegalArgumentException assertIllegalArgumentException(final Executable executable) { - return assertThrows(IllegalArgumentException.class, executable); - } - - /** - * Asserts that execution of the given {@code executable} throws a {@link IllegalArgumentException}. - * - *

- * The assertion passes if the thrown exception type is the same as {@link IllegalArgumentException} or a subtype. To check for the exact thrown type use - * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is - * thrown, this method fails with the given {@code message}. - *

- * - * @param executable What to test. - * @param message The message for the failure if the executable doesn't throw a IllegalArgumentException. - * @return The thrown IllegalArgumentException. - * @see Assertions#assertThrowsExactly(Class, Executable) - */ - public static IllegalArgumentException assertIllegalArgumentException(final Executable executable, final String message) { - return assertThrows(IllegalArgumentException.class, executable, message); - } - - /** - * Asserts that execution of the given {@code executable} throws a {@link IndexOutOfBoundsException}. - * - *

- * The assertion passes if the thrown exception type is the same as {@link IndexOutOfBoundsException} or a subtype. To check for the exact thrown type use - * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is - * thrown, this method fails. - *

- * - * @param executable What to test. - * @return The thrown IndexOutOfBoundsException. - * @see Assertions#assertThrowsExactly(Class, Executable) - */ - public static IndexOutOfBoundsException assertIndexOutOfBoundsException(final Executable executable) { - return assertThrows(IndexOutOfBoundsException.class, executable); - } - - /** - * Asserts that execution of the given {@code executable} throws a {@link IndexOutOfBoundsException}. - * - *

- * The assertion passes if the thrown exception type is the same as {@link IndexOutOfBoundsException} or a subtype. To check for the exact thrown type use - * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is - * thrown, this method fails with the given {@code message}. - *

- * - * @param executable What to test. - * @param message The message for the failure if the executable doesn't throw a IndexOutOfBoundsException. - * @return The thrown IndexOutOfBoundsException. - * @see Assertions#assertThrowsExactly(Class, Executable) - */ - public static IndexOutOfBoundsException assertIndexOutOfBoundsException(final Executable executable, final String message) { - return assertThrows(IndexOutOfBoundsException.class, executable, message); - } - /** * Asserts that execution of the given {@code executable} throws a {@link NullPointerException}. * *

- * The assertion passes if the thrown exception type is the same as {@link NullPointerException} or a subtype. To check for the exact thrown type use - * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is + * The assertion passes if the thrown exception type is the same as {@link NullPointerException} or a subtype. To check for the exact thrown type + * use {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is * thrown, this method fails. *

* @@ -118,13 +48,13 @@ public static NullPointerException assertNullPointerException(final Executable e * Asserts that execution of the given {@code executable} throws a {@link NullPointerException}. * *

- * The assertion passes if the thrown exception type is the same as {@link NullPointerException} or a subtype. To check for the exact thrown type use - * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is - * thrown, this method fails with the given {@code message}. + * The assertion passes if the thrown exception type is the same as {@link NullPointerException} or a subtype. To check for the exact thrown type + * use {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is + * thrown, this method fails. *

* * @param executable What to test. - * @param message The message for the failure if the executable doesn't throw a NullPointerException. + * @param message The message for the failure if the executable doesn't throw a NullPointerException. * @return The thrown NullPointerException. * @see Assertions#assertThrowsExactly(Class, Executable) */ diff --git a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java index 101d0246773..5c0b50b1af5 100644 --- a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3; import static org.apache.commons.lang3.JavaVersion.JAVA_1_4; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -41,7 +40,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.junitpioneer.jupiter.DefaultLocale; /** * Tests for {@link LocaleUtils}. @@ -305,13 +303,33 @@ void testLang865() { assertValidToLocale("_GB", "", "GB", ""); assertValidToLocale("_GB_P", "", "GB", "P"); assertValidToLocale("_GB_POSIX", "", "GB", "POSIX"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_G"), "Must be at least 3 chars if starts with underscore"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_Gb"), "Must be uppercase if starts with underscore"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_gB"), "Must be uppercase if starts with underscore"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_1B"), "Must be letter if starts with underscore"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_G1"), "Must be letter if starts with underscore"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_GB_"), "Must be at least 5 chars if starts with underscore"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("_GBAP"), + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_G"), + "Must be at least 3 chars if starts with underscore"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_Gb"), + "Must be uppercase if starts with underscore"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_gB"), + "Must be uppercase if starts with underscore"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_1B"), + "Must be letter if starts with underscore"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_G1"), + "Must be letter if starts with underscore"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_GB_"), + "Must be at least 5 chars if starts with underscore"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("_GBAP"), "Must have underscore after the country if starts with underscore and is at least 5 chars"); } @@ -425,7 +443,7 @@ void testParseAllLocales(final Locale actualLocale) { } String localeStr = str; if (suff >= 0) { // we have a suffix - assertIllegalArgumentException(() -> LocaleUtils.toLocale(str)); + assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale(str)); // try without suffix localeStr = str.substring(0, suff); } @@ -454,6 +472,7 @@ void testThreeCharsLocale() { @Test void testToLocale_1Part() { assertNull(LocaleUtils.toLocale((String) null)); + assertValidToLocale("us"); assertValidToLocale("fr"); assertValidToLocale("de"); @@ -462,11 +481,14 @@ void testToLocale_1Part() { assertValidToLocale("qq"); // LANG-941: JDK 8 introduced the empty locale as one of the default locales assertValidToLocale(""); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("Us"), "Should fail if not lowercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("uS"), "Should fail if not lowercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("u#"), "Should fail if not lowercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("u"), "Must be 2 chars if less than 5"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("uu_U"), "Must be 2 chars if less than 5"); + + assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("Us"), "Should fail if not lowercase"); + assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("uS"), "Should fail if not lowercase"); + assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("u#"), "Should fail if not lowercase"); + assertThrows( + IllegalArgumentException.class, () -> LocaleUtils.toLocale("u"), "Must be 2 chars if less than 5"); + assertThrows( + IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_U"), "Must be 2 chars if less than 5"); } /** @@ -476,13 +498,29 @@ void testToLocale_1Part() { void testToLocale_2Part() { assertValidToLocale("us_EN", "us", "EN"); assertValidToLocale("us-EN", "us", "EN"); - // valid though doesn't exist + //valid though doesn't exist assertValidToLocale("us_ZH", "us", "ZH"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("us_En"), "Should fail second part not uppercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("us_en"), "Should fail second part not uppercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("us_eN"), "Should fail second part not uppercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("uS_EN"), "Should fail first part not lowercase"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("us_E3"), "Should fail second part not uppercase"); + + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("us_En"), + "Should fail second part not uppercase"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("us_en"), + "Should fail second part not uppercase"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("us_eN"), + "Should fail second part not uppercase"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("uS_EN"), + "Should fail first part not lowercase"); + assertThrows( + IllegalArgumentException.class, + () -> LocaleUtils.toLocale("us_E3"), + "Should fail second part not uppercase"); } /** @@ -501,8 +539,8 @@ void testToLocale_3Part() { assertValidToLocale("us_EN_a", "us", "EN", "A"); assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFSAFDFDSDFF"); } - assertIllegalArgumentException(() -> LocaleUtils.toLocale("us_EN-a"), "Should fail as no consistent delimiter"); - assertIllegalArgumentException(() -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length"); + assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as no consistent delimiter"); + assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length"); // LANG-1741 assertEquals(new Locale("en", "001", "US_POSIX"), LocaleUtils.toLocale("en_001_US_POSIX")); } @@ -517,37 +555,6 @@ void testToLocale_Locale_defaults() { assertEquals(Locale.getDefault(), LocaleUtils.toLocale(Locale.getDefault())); } - @ParameterizedTest - @MethodSource("java.util.Locale#getISOCountries") - void testToLocaleGetIso3Country(final String country) { - assertEquals(LocaleUtils.ofCountry(country).getISO3Country(), LocaleUtils.toLocale(country).getISO3Country()); - } - - @Test - void testToLocaleGetIso3CountryKnown() { - assertEquals("USA", LocaleUtils.toLocale("US").getISO3Country()); - assertEquals("GBR", LocaleUtils.toLocale("GB").getISO3Country()); - assertEquals("PAK", LocaleUtils.toLocale("PK").getISO3Country()); - assertEquals("IND", LocaleUtils.toLocale("IN").getISO3Country()); - assertEquals("FRA", LocaleUtils.toLocale("FR").getISO3Country()); - } - - @Test - @DefaultLocale(country = "US", language = "en") - void testToLocaleGetIso3LanguageKown() { - assertEquals("United States", LocaleUtils.toLocale("US").getDisplayCountry()); - assertEquals("United Kingdom", LocaleUtils.toLocale("GB").getDisplayCountry()); - assertEquals("Pakistan", LocaleUtils.toLocale("PK").getDisplayCountry()); - assertEquals("India", LocaleUtils.toLocale("IN").getDisplayCountry()); - assertEquals("France", LocaleUtils.toLocale("FR").getDisplayCountry()); - } - - @ParameterizedTest - @MethodSource("java.util.Locale#getISOCountries") - void testToLocaleGetIso3LanguageKown(final String country) { - assertEquals(LocaleUtils.ofCountry(country).getDisplayCountry(), LocaleUtils.toLocale(country).getDisplayCountry()); - } - /** * Test toLocale(Locale) method. */ diff --git a/src/test/java/org/apache/commons/lang3/LongRangeTest.java b/src/test/java/org/apache/commons/lang3/LongRangeTest.java index 6c644d751ee..da86903157f 100644 --- a/src/test/java/org/apache/commons/lang3/LongRangeTest.java +++ b/src/test/java/org/apache/commons/lang3/LongRangeTest.java @@ -17,13 +17,13 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Comparator; @@ -202,12 +202,12 @@ void testIntersectionWith() { @Test void testIntersectionWithNonOverlapping() { - assertIllegalArgumentException(() -> range1.intersectionWith(Range.of(0L, 9L))); + assertThrows(IllegalArgumentException.class, () -> range1.intersectionWith(Range.of(0L, 9L))); } @Test void testIntersectionWithNull() { - assertIllegalArgumentException(() -> range1.intersectionWith(null)); + assertThrows(IllegalArgumentException.class, () -> range1.intersectionWith(null)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 93db200457a..0c20d841a5d 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -311,7 +310,7 @@ void testComparatorMedian() { @Test void testComparatorMedian_emptyItems() { - assertIllegalArgumentException(() -> ObjectUtils.median(new CharSequenceComparator())); + assertThrows(IllegalArgumentException.class, () -> ObjectUtils.median(new CharSequenceComparator())); } @Test @@ -350,9 +349,11 @@ void testCompare() { @Test void testConstMethods() { + // To truly test the CONST() method, we'd want to look in the // bytecode to see if the literals were folded into the // class, or if the bytecode kept the method call. + assertTrue(ObjectUtils.CONST(true), "CONST(boolean)"); assertEquals((byte) 3, ObjectUtils.CONST((byte) 3), "CONST(byte)"); assertEquals((char) 3, ObjectUtils.CONST((char) 3), "CONST(char)"); @@ -362,12 +363,13 @@ void testConstMethods() { assertEquals(3f, ObjectUtils.CONST(3f), "CONST(float)"); assertEquals(3.0, ObjectUtils.CONST(3.0), "CONST(double)"); assertEquals("abc", ObjectUtils.CONST("abc"), "CONST(Object)"); + // Make sure documentation examples from Javadoc all work // (this fixed a lot of my bugs when I these!) // // My bugs should be in a software engineering textbook - // for "Can you screw this up?" The answer is, yes, - // you can even screw this up. (When you == Julius) + // for "Can you screw this up?" The answer is, yes, + // you can even screw this up. (When you == Julius) // . final boolean MAGIC_FLAG = ObjectUtils.CONST(true); final byte MAGIC_BYTE1 = ObjectUtils.CONST((byte) 127); @@ -381,6 +383,7 @@ void testConstMethods() { final float MAGIC_FLOAT = ObjectUtils.CONST(1.0f); final double MAGIC_DOUBLE = ObjectUtils.CONST(1.0); final String MAGIC_STRING = ObjectUtils.CONST("abc"); + assertTrue(MAGIC_FLAG); assertEquals(127, MAGIC_BYTE1); assertEquals(127, MAGIC_BYTE2); @@ -393,11 +396,21 @@ void testConstMethods() { assertEquals(1.0f, MAGIC_FLOAT); assertEquals(1.0, MAGIC_DOUBLE); assertEquals("abc", MAGIC_STRING); - assertIllegalArgumentException(() -> ObjectUtils.CONST_BYTE(-129), "CONST_BYTE(-129): IllegalArgumentException should have been thrown."); - assertIllegalArgumentException(() -> ObjectUtils.CONST_BYTE(128), "CONST_BYTE(128): IllegalArgumentException should have been thrown."); - assertIllegalArgumentException(() -> ObjectUtils.CONST_SHORT(-32769), + assertThrows( + IllegalArgumentException.class, + () -> ObjectUtils.CONST_BYTE(-129), + "CONST_BYTE(-129): IllegalArgumentException should have been thrown."); + assertThrows( + IllegalArgumentException.class, + () -> ObjectUtils.CONST_BYTE(128), + "CONST_BYTE(128): IllegalArgumentException should have been thrown."); + assertThrows( + IllegalArgumentException.class, + () -> ObjectUtils.CONST_SHORT(-32769), "CONST_SHORT(-32769): IllegalArgumentException should have been thrown."); - assertIllegalArgumentException(() -> ObjectUtils.CONST_BYTE(32768), + assertThrows( + IllegalArgumentException.class, + () -> ObjectUtils.CONST_BYTE(32768), "CONST_SHORT(32768): IllegalArgumentException should have been thrown."); } @@ -575,11 +588,16 @@ void testIdentityHashCodeHex() { void testIdentityToStringAppendable() throws IOException { final Integer i = Integer.valueOf(121); final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + final Appendable appendable = new StringBuilder(); ObjectUtils.identityToString(appendable, i); assertEquals(expected, appendable.toString()); + assertNullPointerException(() -> ObjectUtils.identityToString((Appendable) null, "tmp")); - assertNullPointerException(() -> ObjectUtils.identityToString((Appendable) new StringBuilder(), null)); + + assertThrows( + NullPointerException.class, + () -> ObjectUtils.identityToString((Appendable) new StringBuilder(), null)); } @Test @@ -599,10 +617,13 @@ void testIdentityToStringObjectNull() { void testIdentityToStringStrBuilder() { final Integer i = Integer.valueOf(102); final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + final StrBuilder builder = new StrBuilder(); ObjectUtils.identityToString(builder, i); assertEquals(expected, builder.toString()); + assertNullPointerException(() -> ObjectUtils.identityToString((StrBuilder) null, "tmp")); + assertNullPointerException(() -> ObjectUtils.identityToString(new StrBuilder(), null)); } @@ -617,9 +638,11 @@ void testIdentityToStringString() { void testIdentityToStringStringBuffer() { final Integer i = Integer.valueOf(45); final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + final StringBuffer buffer = new StringBuffer(); ObjectUtils.identityToString(buffer, i); assertEquals(expected, buffer.toString()); + assertNullPointerException(() -> ObjectUtils.identityToString((StringBuffer) null, "tmp")); assertNullPointerException(() -> ObjectUtils.identityToString(new StringBuffer(), null)); } @@ -638,6 +661,7 @@ void testIdentityToStringStringBuilder() { void testIdentityToStringStringBuilderInUse() { final Integer i = Integer.valueOf(90); final String expected = "ABC = java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + final StringBuilder builder = new StringBuilder("ABC = "); ObjectUtils.identityToString(builder, i); assertEquals(expected, builder.toString()); @@ -740,7 +764,7 @@ void testMedian() { @Test void testMedian_emptyItems() { - assertIllegalArgumentException(ObjectUtils::median); + assertThrows(IllegalArgumentException.class, ObjectUtils::median); } @Test @@ -842,8 +866,8 @@ void testRequireNonEmpty() { assertNullPointerException(() -> ObjectUtils.requireNonEmpty(null)); assertNullPointerException(() -> ObjectUtils.requireNonEmpty(null, "foo")); // - assertIllegalArgumentException(() -> ObjectUtils.requireNonEmpty("")); - assertIllegalArgumentException(() -> ObjectUtils.requireNonEmpty("", "foo")); + assertThrows(IllegalArgumentException.class, () -> ObjectUtils.requireNonEmpty("")); + assertThrows(IllegalArgumentException.class, () -> ObjectUtils.requireNonEmpty("", "foo")); } @Test diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java index 3602fbc6461..a84b65a43c2 100644 --- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java @@ -16,10 +16,10 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -92,89 +92,89 @@ void testConstructor() { @Test void testExceptionsRandom() { - assertIllegalArgumentException(() -> RandomStringUtils.random(-1)); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, true, true)); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, new char[] { 'a' })); - assertIllegalArgumentException(() -> RandomStringUtils.random(1, new char[0])); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, "")); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, (String) null)); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, 'a', 'z', false, false)); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, 'a', 'z', false, false, new char[] { 'a' })); - assertIllegalArgumentException(() -> RandomStringUtils.random(-1, 'a', 'z', false, false, new char[] { 'a' }, new Random())); - assertIllegalArgumentException(() -> RandomStringUtils.random(8, 32, 48, false, true)); - assertIllegalArgumentException(() -> RandomStringUtils.random(8, 32, 65, true, false)); - assertIllegalArgumentException(() -> RandomStringUtils.random(1, Integer.MIN_VALUE, -10, false, false, null)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, true, true)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, new char[] { 'a' })); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(1, new char[0])); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, "")); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, (String) null)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false, new char[] { 'a' })); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(-1, 'a', 'z', false, false, new char[] { 'a' }, new Random())); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 48, false, true)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 65, true, false)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(1, Integer.MIN_VALUE, -10, false, false, null)); } @ParameterizedTest @MethodSource("randomProvider") void testExceptionsRandom(final RandomStringUtils rsu) { - assertIllegalArgumentException(() -> rsu.next(-1)); - assertIllegalArgumentException(() -> rsu.next(-1, true, true)); - assertIllegalArgumentException(() -> rsu.next(-1, new char[] { 'a' })); - assertIllegalArgumentException(() -> rsu.next(1, new char[0])); - assertIllegalArgumentException(() -> rsu.next(-1, "")); - assertIllegalArgumentException(() -> rsu.next(-1, (String) null)); - assertIllegalArgumentException(() -> rsu.next(-1, 'a', 'z', false, false)); - assertIllegalArgumentException(() -> rsu.next(-1, 'a', 'z', false, false, new char[] { 'a' })); - assertIllegalArgumentException(() -> rsu.next(8, 32, 48, false, true)); - assertIllegalArgumentException(() -> rsu.next(8, 32, 65, true, false)); - assertIllegalArgumentException(() -> rsu.next(1, Integer.MIN_VALUE, -10, false, false, null)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1, true, true)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1, new char[] { 'a' })); + assertThrows(IllegalArgumentException.class, () -> rsu.next(1, new char[0])); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1, "")); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1, (String) null)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1, 'a', 'z', false, false)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(-1, 'a', 'z', false, false, new char[] { 'a' })); + assertThrows(IllegalArgumentException.class, () -> rsu.next(8, 32, 48, false, true)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(8, 32, 65, true, false)); + assertThrows(IllegalArgumentException.class, () -> rsu.next(1, Integer.MIN_VALUE, -10, false, false, null)); } @Test void testExceptionsRandomAlphabetic() { - assertIllegalArgumentException(() -> RandomStringUtils.randomAlphabetic(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomAlphabetic(-1)); } @ParameterizedTest @MethodSource("randomProvider") void testExceptionsRandomAlphabetic(final RandomStringUtils rsu) { - assertIllegalArgumentException(() -> rsu.nextAlphabetic(-1)); + assertThrows(IllegalArgumentException.class, () -> rsu.nextAlphabetic(-1)); } @Test void testExceptionsRandomAscii() { - assertIllegalArgumentException(() -> RandomStringUtils.randomAscii(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomAscii(-1)); } @ParameterizedTest @MethodSource("randomProvider") void testExceptionsRandomAscii(final RandomStringUtils rsu) { - assertIllegalArgumentException(() -> rsu.nextAscii(-1)); + assertThrows(IllegalArgumentException.class, () -> rsu.nextAscii(-1)); } @Test void testExceptionsRandomGraph() { - assertIllegalArgumentException(() -> RandomStringUtils.randomGraph(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomGraph(-1)); } @ParameterizedTest @MethodSource("randomProvider") void testExceptionsRandomGraph(final RandomStringUtils rsu) { - assertIllegalArgumentException(() -> rsu.nextGraph(-1)); + assertThrows(IllegalArgumentException.class, () -> rsu.nextGraph(-1)); } @Test void testExceptionsRandomNumeric() { - assertIllegalArgumentException(() -> RandomStringUtils.randomNumeric(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomNumeric(-1)); } @ParameterizedTest @MethodSource("randomProvider") void testExceptionsRandomNumeric(final RandomStringUtils rsu) { - assertIllegalArgumentException(() -> rsu.nextNumeric(-1)); + assertThrows(IllegalArgumentException.class, () -> rsu.nextNumeric(-1)); } @Test void testExceptionsRandomPrint() { - assertIllegalArgumentException(() -> RandomStringUtils.randomPrint(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomPrint(-1)); } @ParameterizedTest @MethodSource("randomProvider") void testExceptionsRandomPrint(final RandomStringUtils rsu) { - assertIllegalArgumentException(() -> rsu.nextPrint(-1)); + assertThrows(IllegalArgumentException.class, () -> rsu.nextPrint(-1)); } /** @@ -289,7 +289,7 @@ void testLANG805() { @ParameterizedTest @MethodSource("randomProvider") void testLANG807(final RandomStringUtils rsu) { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> rsu.next(3, 5, 5, false, false)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> rsu.next(3, 5, 5, false, false)); final String msg = ex.getMessage(); assertTrue(msg.contains("start"), "Message (" + msg + ") must contain 'start'"); assertTrue(msg.contains("end"), "Message (" + msg + ") must contain 'end'"); diff --git a/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java index d42d7ad2258..8ebb2db98a2 100644 --- a/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java @@ -16,11 +16,11 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.Stream; @@ -211,13 +211,13 @@ void testNextBytes(final RandomUtils ru) { @Test void testNextBytesNegative() { - assertIllegalArgumentException(() -> RandomUtils.nextBytes(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextBytes(-1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextBytesNegative(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomBytes(-1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomBytes(-1)); } /** @@ -243,13 +243,13 @@ void testNextDouble(final RandomUtils ru) { @Test void testNextDoubleLowerGreaterUpper() { - assertIllegalArgumentException(() -> RandomUtils.nextDouble(2, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextDouble(2, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextDoubleLowerGreaterUpper(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomDouble(2, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomDouble(2, 1)); } /** @@ -271,13 +271,13 @@ void testNextDoubleMinimalRange(final RandomUtils ru) { @Test void testNextDoubleNegative() { - assertIllegalArgumentException(() -> RandomUtils.nextDouble(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextDouble(-1, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextDoubleNegative(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomDouble(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomDouble(-1, 1)); } /** @@ -324,13 +324,13 @@ void testNextFloat(final RandomUtils ru) { @Test void testNextFloatLowerGreaterUpper() { - assertIllegalArgumentException(() -> RandomUtils.nextFloat(2, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextFloat(2, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextFloatLowerGreaterUpper(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomFloat(2, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomFloat(2, 1)); } /** @@ -352,13 +352,13 @@ void testNextFloatMinimalRange(final RandomUtils ru) { @Test void testNextFloatNegative() { - assertIllegalArgumentException(() -> RandomUtils.nextFloat(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextFloat(-1, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextFloatNegative(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomFloat(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomFloat(-1, 1)); } /** @@ -405,13 +405,13 @@ void testNextInt(final RandomUtils ru) { @Test void testNextIntLowerGreaterUpper() { - assertIllegalArgumentException(() -> RandomUtils.nextInt(2, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextInt(2, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextIntLowerGreaterUpper(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomInt(2, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomInt(2, 1)); } /** @@ -433,13 +433,13 @@ void testNextIntMinimalRange(final RandomUtils ru) { @Test void testNextIntNegative() { - assertIllegalArgumentException(() -> RandomUtils.nextInt(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextInt(-1, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextIntNegative(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomInt(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomInt(-1, 1)); } /** @@ -486,13 +486,13 @@ void testNextLong(final RandomUtils ru) { @Test void testNextLongLowerGreaterUpper() { - assertIllegalArgumentException(() -> RandomUtils.nextLong(2, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextLong(2, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextLongLowerGreaterUpper(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomLong(2, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomLong(2, 1)); } /** @@ -514,13 +514,13 @@ void testNextLongMinimalRange(final RandomUtils ru) { @Test void testNextLongNegative() { - assertIllegalArgumentException(() -> RandomUtils.nextLong(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextLong(-1, 1)); } @ParameterizedTest @MethodSource("randomProvider") void testNextLongNegative(final RandomUtils ru) { - assertIllegalArgumentException(() -> ru.randomLong(-1, 1)); + assertThrows(IllegalArgumentException.class, () -> ru.randomLong(-1, 1)); } /** diff --git a/src/test/java/org/apache/commons/lang3/RangeTest.java b/src/test/java/org/apache/commons/lang3/RangeTest.java index 167dfdfd6fb..d1512eace12 100644 --- a/src/test/java/org/apache/commons/lang3/RangeTest.java +++ b/src/test/java/org/apache/commons/lang3/RangeTest.java @@ -17,13 +17,13 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Comparator; @@ -252,12 +252,12 @@ void testIntersectionWith() { @Test void testIntersectionWithNonOverlapping() { - assertIllegalArgumentException(() -> intRange.intersectionWith(Range.between(0, 9))); + assertThrows(IllegalArgumentException.class, () -> intRange.intersectionWith(Range.between(0, 9))); } @Test void testIntersectionWithNull() { - assertIllegalArgumentException(() -> intRange.intersectionWith(null)); + assertThrows(IllegalArgumentException.class, () -> intRange.intersectionWith(null)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java index 5d4db727a5b..2386f3b118a 100644 --- a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java @@ -71,14 +71,19 @@ void testRemoveAll() { void testRemoveAll_StringString() { assertNull(RegExUtils.removeAll(null, "")); assertEquals("any", RegExUtils.removeAll("any", (String) null)); + assertEquals("any", RegExUtils.removeAll("any", "")); assertEquals("", RegExUtils.removeAll("any", ".*")); assertEquals("", RegExUtils.removeAll("any", ".+")); assertEquals("", RegExUtils.removeAll("any", ".?")); + assertEquals("A\nB", RegExUtils.removeAll("A<__>\n<__>B", "<.*>")); assertEquals("AB", RegExUtils.removeAll("A<__>\n<__>B", "(?s)<.*>")); assertEquals("ABC123", RegExUtils.removeAll("ABCabc123abc", "[a-z]")); - assertThrows(PatternSyntaxException.class, () -> RegExUtils.removeAll("any", "{badRegexSyntax}"), + + assertThrows( + PatternSyntaxException.class, + () -> RegExUtils.removeAll("any", "{badRegexSyntax}"), "RegExUtils.removeAll expecting PatternSyntaxException"); } @@ -122,15 +127,20 @@ void testRemoveFirst() { void testRemoveFirst_StringString() { assertNull(RegExUtils.removeFirst(null, "")); assertEquals("any", RegExUtils.removeFirst("any", (String) null)); + assertEquals("any", RegExUtils.removeFirst("any", "")); assertEquals("", RegExUtils.removeFirst("any", ".*")); assertEquals("", RegExUtils.removeFirst("any", ".+")); assertEquals("bc", RegExUtils.removeFirst("abc", ".?")); + assertEquals("A\n<__>B", RegExUtils.removeFirst("A<__>\n<__>B", "<.*>")); assertEquals("AB", RegExUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>")); assertEquals("ABCbc123", RegExUtils.removeFirst("ABCabc123", "[a-z]")); assertEquals("ABC123abc", RegExUtils.removeFirst("ABCabc123abc", "[a-z]+")); - assertThrows(PatternSyntaxException.class, () -> RegExUtils.removeFirst("any", "{badRegexSyntax}"), + + assertThrows( + PatternSyntaxException.class, + () -> RegExUtils.removeFirst("any", "{badRegexSyntax}"), "RegExUtils.removeFirst expecting PatternSyntaxException"); } @@ -229,7 +239,9 @@ void testReplaceAll_StringStringString() { assertEquals("ABC123", RegExUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "")); assertEquals("Lorem_ipsum_dolor_sit", RegExUtils.replaceAll("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); - assertThrows(PatternSyntaxException.class, () -> RegExUtils.replaceAll("any", "{badRegexSyntax}", ""), + assertThrows( + PatternSyntaxException.class, + () -> RegExUtils.replaceAll("any", "{badRegexSyntax}", ""), "RegExUtils.replaceAll expecting PatternSyntaxException"); } @@ -300,7 +312,9 @@ void testReplaceFirst_StringStringString() { assertEquals("Lorem_ipsum dolor sit", RegExUtils.replaceFirst("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); - assertThrows(PatternSyntaxException.class, () -> RegExUtils.replaceFirst("any", "{badRegexSyntax}", ""), + assertThrows( + PatternSyntaxException.class, + () -> RegExUtils.replaceFirst("any", "{badRegexSyntax}", ""), "RegExUtils.replaceFirst expecting PatternSyntaxException"); } diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java index 93e8a277d0b..14aa56c04bc 100644 --- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java @@ -199,7 +199,8 @@ void testDeserializeStream() throws Exception { @Test void testDeserializeStreamBadStream() { - assertThrows(SerializationException.class, () -> SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0]))); + assertThrows(SerializationException.class, + () -> SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0]))); } @Test diff --git a/src/test/java/org/apache/commons/lang3/StreamsTest.java b/src/test/java/org/apache/commons/lang3/StreamsTest.java index f7ecb74deaa..d58f2cb2ceb 100644 --- a/src/test/java/org/apache/commons/lang3/StreamsTest.java +++ b/src/test/java/org/apache/commons/lang3/StreamsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -86,7 +85,7 @@ public Stream simpleStreamFilterFailing() { .map(Integer::valueOf) .filter(asIntPredicate(iae)) .collect(Collectors.toList()); - final IllegalArgumentException thrown = assertIllegalArgumentException(testMethod); + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod); assertEquals("Invalid argument: " + 5, thrown.getMessage()); }), dynamicTest("OutOfMemoryError", () -> { @@ -118,7 +117,7 @@ public Stream simpleStreamForEachFailing() { dynamicTest("IllegalArgumentException", () -> { final IllegalArgumentException ise = new IllegalArgumentException(); final Executable testMethod = () -> Functions.stream(input).forEach(asIntConsumer(ise)); - final IllegalArgumentException thrown = assertIllegalArgumentException(testMethod); + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod); assertNull(thrown.getMessage()); }), dynamicTest("OutOfMemoryError", () -> { diff --git a/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java index d9aba2cd136..1d5dd6327aa 100644 --- a/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java @@ -203,8 +203,8 @@ void testEscapeHtmlHighUnicode() { assertEquals(original, unescaped, "High Unicode should have been unchanged"); // TODO: I think this should hold, needs further investigation -// String unescapedFromEntity = StringEscapeUtils.unescapeHtml4("𝍢"); -// assertEquals("High Unicode should have been unescaped", original, unescapedFromEntity); +// String unescapedFromEntity = StringEscapeUtils.unescapeHtml4( "𝍢" ); +// assertEquals( "High Unicode should have been unescaped", original, unescapedFromEntity); } @Test diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java index 49a188d06de..a54b2bbd893 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java @@ -17,10 +17,10 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -67,7 +67,7 @@ void testAbbreviate_StringInt() { assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", 8)); assertEquals("a...", StringUtils.abbreviate("abcdefg", 4)); assertEquals("", StringUtils.abbreviate("", 4)); - assertIllegalArgumentException(() -> StringUtils.abbreviate("abc", 3), "StringUtils.abbreviate expecting IllegalArgumentException"); + assertThrows(IllegalArgumentException.class, () -> StringUtils.abbreviate("abc", 3), "StringUtils.abbreviate expecting IllegalArgumentException"); } @Test @@ -75,9 +75,9 @@ void testAbbreviate_StringIntInt() { assertNull(StringUtils.abbreviate(null, 10, 12)); assertEquals("", StringUtils.abbreviate("", 0, 10)); assertEquals("", StringUtils.abbreviate("", 2, 10)); - assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 0, 3), + assertThrows(IllegalArgumentException.class, () -> StringUtils.abbreviate("abcdefghij", 0, 3), "StringUtils.abbreviate expecting IllegalArgumentException"); - assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", 5, 6), + assertThrows(IllegalArgumentException.class, () -> StringUtils.abbreviate("abcdefghij", 5, 6), "StringUtils.abbreviate expecting IllegalArgumentException"); final String raspberry = "raspberry peach"; assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15)); @@ -120,7 +120,7 @@ void testAbbreviate_StringStringInt() { assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "_-", 8)); assertEquals("abc.", StringUtils.abbreviate("abcdefg", ".", 4)); assertEquals("", StringUtils.abbreviate("", 4)); - assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "...", 3), + assertThrows(IllegalArgumentException.class, () -> StringUtils.abbreviate("abcdefghij", "...", 3), "StringUtils.abbreviate expecting IllegalArgumentException"); } @@ -130,9 +130,9 @@ void testAbbreviate_StringStringIntInt() { assertNull(StringUtils.abbreviate(null, "...", 10, 12)); assertEquals("", StringUtils.abbreviate("", null, 0, 10)); assertEquals("", StringUtils.abbreviate("", "...", 2, 10)); - assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "::", 0, 2), + assertThrows(IllegalArgumentException.class, () -> StringUtils.abbreviate("abcdefghij", "::", 0, 2), "StringUtils.abbreviate expecting IllegalArgumentException"); - assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6), + assertThrows(IllegalArgumentException.class, () -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6), "StringUtils.abbreviate expecting IllegalArgumentException"); final String raspberry = "raspberry peach"; assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, "--", 12, 15)); diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index cc803e4909e..bff9d842dce 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -705,22 +704,22 @@ void testGetFuzzyDistance() { @Test void testGetFuzzyDistance_NullNullNull() { - assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(null, null, null)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(null, null, null)); } @Test void testGetFuzzyDistance_NullStringLocale() { - assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(null, "clear", Locale.ENGLISH)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(null, "clear", Locale.ENGLISH)); } @Test void testGetFuzzyDistance_StringNullLoclae() { - assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(" ", null, Locale.ENGLISH)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(" ", null, Locale.ENGLISH)); } @Test void testGetFuzzyDistance_StringStringNull() { - assertIllegalArgumentException(() -> StringUtils.getFuzzyDistance(" ", "clear", null)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getFuzzyDistance(" ", "clear", null)); } @Test @@ -776,17 +775,17 @@ void testGetIfEmpty_StringStringSupplier() { @Test void testGetJaroWinklerDistance_NullNull() { - assertIllegalArgumentException(() -> StringUtils.getJaroWinklerDistance(null, null)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getJaroWinklerDistance(null, null)); } @Test void testGetJaroWinklerDistance_NullString() { - assertIllegalArgumentException(() -> StringUtils.getJaroWinklerDistance(null, "clear")); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getJaroWinklerDistance(null, "clear")); } @Test void testGetJaroWinklerDistance_StringNull() { - assertIllegalArgumentException(() -> StringUtils.getJaroWinklerDistance(" ", null)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getJaroWinklerDistance(" ", null)); } @Test @@ -804,22 +803,22 @@ void testGetJaroWinklerDistance_StringString() { @Test void testGetLevenshteinDistance_NullString() { - assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance("a", null)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance("a", null)); } @Test void testGetLevenshteinDistance_NullStringInt() { - assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance(null, "a", 0)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance(null, "a", 0)); } @Test void testGetLevenshteinDistance_StringNull() { - assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance(null, "a")); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance(null, "a")); } @Test void testGetLevenshteinDistance_StringNullInt() { - assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance("a", null, 0)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance("a", null, 0)); } @Test @@ -902,7 +901,7 @@ void testGetLevenshteinDistance_StringStringInt() { @Test void testGetLevenshteinDistance_StringStringNegativeInt() { - assertIllegalArgumentException(() -> StringUtils.getLevenshteinDistance("a", "a", -1)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.getLevenshteinDistance("a", "a", -1)); } /** @@ -1227,7 +1226,7 @@ void testJoinWith(final String delimiter) { @Test void testJoinWithThrowsException() { - assertIllegalArgumentException(() -> StringUtils.joinWith(",", (Object[]) null)); + assertThrows(IllegalArgumentException.class, () -> StringUtils.joinWith(",", (Object[]) null)); } @Disabled @@ -1511,7 +1510,9 @@ void testRemoveAll_StringString() { assertEquals("AB", StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>")); assertEquals("ABC123", StringUtils.removeAll("ABCabc123abc", "[a-z]")); - assertThrows(PatternSyntaxException.class, () -> StringUtils.removeAll("any", "{badRegexSyntax}"), + assertThrows( + PatternSyntaxException.class, + () -> StringUtils.removeAll("any", "{badRegexSyntax}"), "StringUtils.removeAll expecting PatternSyntaxException"); } @@ -1574,7 +1575,9 @@ void testRemoveFirst_StringString() { assertEquals("ABCbc123", StringUtils.removeFirst("ABCabc123", "[a-z]")); assertEquals("ABC123abc", StringUtils.removeFirst("ABCabc123abc", "[a-z]+")); - assertThrows(PatternSyntaxException.class, () -> StringUtils.removeFirst("any", "{badRegexSyntax}"), + assertThrows( + PatternSyntaxException.class, + () -> StringUtils.removeFirst("any", "{badRegexSyntax}"), "StringUtils.removeFirst expecting PatternSyntaxException"); } @@ -1763,7 +1766,9 @@ void testReplace_StringStringArrayStringArray() { assertEquals(StringUtils.replaceEach("aba", new String[]{"a"}, new String[]{null}), "aba"); assertEquals(StringUtils.replaceEach("aba", new String[]{"a", "b"}, new String[]{"c", null}), "cbc"); - assertIllegalArgumentException(() -> StringUtils.replaceEach("abba", new String[] { "a" }, new String[] { "b", "a" }), + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.replaceEach("abba", new String[]{"a"}, new String[]{"b", "a"}), "StringUtils.replaceEach(String, String[], String[]) expecting IllegalArgumentException"); } @@ -1794,9 +1799,12 @@ void testReplace_StringStringArrayStringArrayBoolean() { new String[]{"aa"}, new String[]{"a"})); // Test default TTL exceeded. 33 characters reduced to 17, then 9, 5, 3, 2 (still found). - assertThrows(IllegalStateException.class, - () -> StringUtils.replaceEachRepeatedly("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", new String[] { "aa" }, new String[] { "a" }), - "Cannot be resolved within the default time-to-live limit"); + assertThrows( + IllegalStateException.class, + () -> StringUtils.replaceEachRepeatedly("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + new String[]{"aa"}, new String[]{"a"}), + "Cannot be resolved within the default time-to-live limit"); + // Test larger TTL for larger search lists. Replace repeatedly until there are no more possible replacements. assertEquals("000000000", StringUtils.replaceEachRepeatedly("aA0aA0aA0", new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", @@ -1809,7 +1817,8 @@ void testReplace_StringStringArrayStringArrayBoolean() { "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"})); // Test long infinite cycle: a -> b -> ... -> 9 -> 0 -> a -> b -> ... - assertThrows(IllegalStateException.class, + assertThrows( + IllegalStateException.class, () -> StringUtils.replaceEachRepeatedly("a", new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", @@ -1821,13 +1830,15 @@ void testReplace_StringStringArrayStringArrayBoolean() { "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a"}), "Should be a circular reference"); - assertThrows(IllegalStateException.class, + assertThrows( + IllegalStateException.class, () -> StringUtils.replaceEachRepeatedly("%{key1}", new String[] {"%{key1}", "%{key2}", "%{key3}"}, new String[] {"Key1 %{key2}", "Key2 %{key3}", "Key3 %{key1}"}), "Should be a circular reference"); - assertThrows(IllegalStateException.class, + assertThrows( + IllegalStateException.class, () -> StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}), "Should be a circular reference"); @@ -1903,7 +1914,9 @@ void testReplaceAll_StringStringString() { assertEquals("Lorem_ipsum_dolor_sit", StringUtils.replaceAll("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); - assertThrows(PatternSyntaxException.class, () -> StringUtils.replaceAll("any", "{badRegexSyntax}", ""), + assertThrows( + PatternSyntaxException.class, + () -> StringUtils.replaceAll("any", "{badRegexSyntax}", ""), "StringUtils.replaceAll expecting PatternSyntaxException"); } @@ -1982,7 +1995,9 @@ void testReplaceFirst_StringStringString() { assertEquals("Lorem_ipsum dolor sit", StringUtils.replaceFirst("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); - assertThrows(PatternSyntaxException.class, () -> StringUtils.replaceFirst("any", "{badRegexSyntax}", ""), + assertThrows( + PatternSyntaxException.class, + () -> StringUtils.replaceFirst("any", "{badRegexSyntax}", ""), "StringUtils.replaceFirst expecting PatternSyntaxException"); } @@ -2951,16 +2966,30 @@ void testToString() throws UnsupportedEncodingException { @Test void testTruncate_StringInt() { assertNull(StringUtils.truncate(null, 12)); - assertIllegalArgumentException(() -> StringUtils.truncate(null, -1), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate(null, -10), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate(null, Integer.MIN_VALUE), "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, () -> StringUtils.truncate(null, -1), "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, () -> StringUtils.truncate(null, -10), "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate(null, Integer.MIN_VALUE), + "maxWith cannot be negative"); assertEquals("", StringUtils.truncate("", 10)); assertEquals("abc", StringUtils.truncate("abcdefghij", 3)); assertEquals("abcdef", StringUtils.truncate("abcdefghij", 6)); assertEquals("", StringUtils.truncate("abcdefghij", 0)); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -1), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -100), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE), "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -1), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -100), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE), + "maxWith cannot be negative"); assertEquals("abcdefghij", StringUtils.truncate("abcdefghijklmno", 10)); assertEquals("abcdefghijklmno", StringUtils.truncate("abcdefghijklmno", Integer.MAX_VALUE)); assertEquals("abcde", StringUtils.truncate("abcdefghijklmno", 5)); @@ -2970,28 +2999,73 @@ void testTruncate_StringInt() { @Test void testTruncate_StringIntInt() { assertNull(StringUtils.truncate(null, 0, 12)); - assertIllegalArgumentException(() -> StringUtils.truncate(null, -1, 0), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate(null, -10, -4), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate(null, Integer.MIN_VALUE, Integer.MIN_VALUE), "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, () -> StringUtils.truncate(null, -1, 0), "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate(null, -10, -4), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate(null, Integer.MIN_VALUE, Integer.MIN_VALUE), + "offset cannot be negative"); assertNull(StringUtils.truncate(null, 10, 12)); assertEquals("", StringUtils.truncate("", 0, 10)); assertEquals("", StringUtils.truncate("", 2, 10)); assertEquals("abc", StringUtils.truncate("abcdefghij", 0, 3)); assertEquals("fghij", StringUtils.truncate("abcdefghij", 5, 6)); assertEquals("", StringUtils.truncate("abcdefghij", 0, 0)); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", 0, -1), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", 0, -10), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", 0, -100), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", 1, -100), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", 0, Integer.MIN_VALUE), "maxWith cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -1, 0), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -10, 0), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -100, 1), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE, 0), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -1, -1), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -10, -10), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", -100, -100), "offset cannot be negative"); - assertIllegalArgumentException(() -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE, Integer.MIN_VALUE), + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", 0, -1), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", 0, -10), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", 0, -100), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", 1, -100), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", 0, Integer.MIN_VALUE), + "maxWith cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -1, 0), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -10, 0), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -100, 1), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE, 0), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -1, -1), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -10, -10), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", -100, -100), + "offset cannot be negative"); + assertThrows( + IllegalArgumentException.class, + () -> StringUtils.truncate("abcdefghij", Integer.MIN_VALUE, Integer.MIN_VALUE), "offset cannot be negative"); final String raspberry = "raspberry peach"; assertEquals("peach", StringUtils.truncate(raspberry, 10, 15)); diff --git a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java index 2fe8b45f74e..1da73747f1f 100644 --- a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java @@ -19,13 +19,13 @@ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; 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.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; @@ -180,7 +180,7 @@ void testGetAllThreadsDoesNotReturnNull() { @Test void testInvalidThreadId() { - assertIllegalArgumentException(() -> ThreadUtils.findThreadById(-5L)); + assertThrows(IllegalArgumentException.class, () -> ThreadUtils.findThreadById(-5L)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index 1d80d1fc546..aa1b62aa838 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -18,8 +18,6 @@ */ package org.apache.commons.lang3; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -60,28 +58,28 @@ void shouldNotThrowExceptionWhenValueIsBetweenBounds() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4", "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0", "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -96,28 +94,28 @@ void shouldNotThrowExceptionWhenValueIsBetweenBounds() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4")); assertEquals("The value 4 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0")); assertEquals("The value 0 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND)); assertEquals("The value 1 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND)); assertEquals("The value 3 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @@ -141,28 +139,28 @@ void shouldNotThrowExceptionWhenValueIsBetweenBounds() { @Test void shouldThrowIllegalArgumentExceptionWhenValueIsLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWhenValueIsUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -177,28 +175,28 @@ void shouldNotThrowExceptionWhenValueIsBetweenBounds() { @Test void shouldThrowIllegalArgumentExceptionWhenValueIsLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND)); assertEquals("The value 0.1 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWhenValueIsUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND)); assertEquals("The value 3.1 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1)); assertEquals("The value 4.1 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01)); assertEquals("The value 0.01 is not in the specified exclusive range of 0.1 to 3.1", ex.getMessage()); } @@ -222,28 +220,28 @@ void shouldNotThrowExceptionWhenValueIsBetweenBounds() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -258,28 +256,28 @@ void shouldNotThrowExceptionWhenValueIsBetweenBounds() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4)); assertEquals("The value 4 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0)); assertEquals("The value 0 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, LOWER_BOUND)); assertEquals("The value 1 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.exclusiveBetween(LOWER_BOUND, UPPER_BOUND, UPPER_BOUND)); assertEquals("The value 3 is not in the specified exclusive range of 1 to 3", ex.getMessage()); } @@ -304,19 +302,19 @@ void shouldNotThrowExceptionForFiniteValue() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNaN() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NaN, "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.finite(Double.NaN, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNegativeInfinity() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NEGATIVE_INFINITY, "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.finite(Double.NEGATIVE_INFINITY, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForPositiveInfinity() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.POSITIVE_INFINITY, "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.finite(Double.POSITIVE_INFINITY, "MSG")); assertEquals("MSG", ex.getMessage()); } } @@ -331,19 +329,19 @@ void shouldNotThrowExceptionForFiniteValue() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNaN() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NaN)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.finite(Double.NaN)); assertEquals("The value is invalid: NaN", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNegativeInfinity() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.NEGATIVE_INFINITY)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.finite(Double.NEGATIVE_INFINITY)); assertEquals("The value is invalid: -Infinity", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForPositiveInfinity() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.finite(Double.POSITIVE_INFINITY)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.finite(Double.POSITIVE_INFINITY)); assertEquals("The value is invalid: Infinity", ex.getMessage()); } } @@ -375,14 +373,14 @@ void shouldNotThrowExceptionWhenValueIsUpperBound() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4", "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0", "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -407,14 +405,14 @@ void shouldNotThrowExceptionWhenValueIsUpperBound() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "4")); assertEquals("The value 4 is not in the specified inclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, "0")); assertEquals("The value 0 is not in the specified inclusive range of 1 to 3", ex.getMessage()); } @@ -448,14 +446,14 @@ void shouldNotThrowExceptionWhenValueIsUpperBound() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -480,14 +478,14 @@ void shouldNotThrowExceptionWhenValueIsUpperBound() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4.1)); assertEquals("The value 4.1 is not in the specified inclusive range of 0.1 to 3.1", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0.01)); assertEquals("The value 0.01 is not in the specified inclusive range of 0.1 to 3.1", ex.getMessage()); } @@ -521,14 +519,14 @@ void shouldNotThrowExceptionWhenValueIsUpperBound() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -553,14 +551,14 @@ void shouldNotThrowExceptionWhenValueIsUpperBound() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsAboveUpperBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 4)); assertEquals("The value 4 is not in the specified inclusive range of 1 to 3", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsBelowLowerBound() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.inclusiveBetween(LOWER_BOUND, UPPER_BOUND, 0)); assertEquals("The value 0 is not in the specified inclusive range of 1 to 3", ex.getMessage()); } @@ -585,7 +583,7 @@ void shouldNotThrowExceptionWhenClassIsAssignable() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenClassIsNotAssignable() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isAssignableFrom(List.class, String.class, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -601,25 +599,25 @@ void shouldNotThrowExceptionWhenClassIsAssignable() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenClassIsNotAssignable() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(List.class, String.class)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isAssignableFrom(List.class, String.class)); assertEquals("Cannot assign a java.lang.String to a java.util.List", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithNullSuperType() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(null, String.class)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isAssignableFrom(null, String.class)); assertEquals("Cannot assign a java.lang.String to a null type", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithNullType() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(List.class, null)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isAssignableFrom(List.class, null)); assertEquals("Cannot assign a null type to a java.util.List", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithNullTypes() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isAssignableFrom(null, null)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isAssignableFrom(null, null)); assertEquals("Cannot assign a null type to a null type", ex.getMessage()); } } @@ -638,7 +636,7 @@ void shouldNotThrowExceptionWhenValueIsInstanceOfClass() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isInstanceOf(List.class, "hi", "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isInstanceOf(List.class, "hi", "MSG")); assertEquals("MSG", ex.getMessage()); } } @@ -653,7 +651,7 @@ void shouldNotThrowExceptionWhenValueIsInstanceOfClass() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageWhenValueIsNotInstanceOfClass() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isInstanceOf(List.class, "hi", "Error %s=%s", "Name", "Value")); assertEquals("Error Name=Value", ex.getMessage()); } @@ -669,7 +667,7 @@ void shouldNotThrowExceptionWhenValueIsInstanceOfClass() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenValueIsNotInstanceOfClass() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isInstanceOf(List.class, "hi")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isInstanceOf(List.class, "hi")); assertEquals("Expected type: java.util.List, actual: java.lang.String", ex.getMessage()); } } @@ -688,7 +686,7 @@ void shouldNotThrowForTrueExpression() { @Test void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "MSG %s", 7.4d)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false, "MSG %s", 7.4d)); assertEquals("MSG 7.4", ex.getMessage()); } } @@ -703,7 +701,7 @@ void shouldNotThrowForTrueExpression() { @Test void shouldThrowExceptionWithLongInsertedIntoTemplateMessageForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "MSG %s", 6)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false, "MSG %s", 6)); assertEquals("MSG 6", ex.getMessage()); } } @@ -718,13 +716,13 @@ void shouldNotThrowForTrueExpression() { @Test void shouldThrowExceptionWithGivenMessageContainingSpecialCharacterForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "%")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false, "%")); assertEquals("%", ex.getMessage()); } @Test void shouldThrowExceptionWithGivenMessageForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false, "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false, "MSG")); assertEquals("MSG", ex.getMessage()); } } @@ -739,7 +737,7 @@ void shouldNotThrowForTrueExpression() { @Test void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false, () -> String.format("MSG %s %s", "Object 1", "Object 2"))); assertEquals("MSG Object 1 Object 2", ex.getMessage()); } @@ -755,7 +753,7 @@ void shouldNotThrowForTrueExpression() { @Test void shouldThrowExceptionWithDoubleInsertedIntoTemplateMessageForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false, "MSG %s %s", "Object 1", "Object 2")); assertEquals("MSG Object 1 Object 2", ex.getMessage()); } @@ -771,7 +769,7 @@ void shouldNotThrowForTrueExpression() { @Test void shouldThrowExceptionWithDefaultMessageForFalseExpression() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.isTrue(false)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.isTrue(false)); assertEquals("The validated expression is false", ex.getMessage()); } @@ -791,7 +789,7 @@ void shouldNotThrowExceptionWhenStringMatchesPattern() { @Test void shouldThrowIllegalArgumentExceptionWhenStringDoesNotMatchPattern() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.matchesPattern("hi", "[0-9]*", "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.matchesPattern("hi", "[0-9]*", "MSG")); assertEquals("MSG", ex.getMessage()); } } @@ -806,7 +804,7 @@ void shouldNotThrowExceptionWhenStringMatchesPattern() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageWhenStringDoesNotMatchPattern() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.matchesPattern("hi", "[0-9]*")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.matchesPattern("hi", "[0-9]*")); assertEquals("The string hi does not match the pattern [0-9]*", ex.getMessage()); } } @@ -834,7 +832,7 @@ void shouldReturnSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForArrayWithNullElement() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.noNullElements(new String[] {"a", null}, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -862,7 +860,7 @@ void shouldReturnSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForArrayWithNullElement() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.noNullElements(new String[] {"a", null})); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.noNullElements(new String[] {"a", null})); assertEquals("The validated array contains null element at index: 1", ex.getMessage()); } @@ -893,7 +891,7 @@ void shouldReturnSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForCollectionWithNullElement() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.noNullElements(Collections.singleton(null), "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -921,7 +919,7 @@ void shouldReturnSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForCollectionWithNullElement() { - final IllegalArgumentException ex = assertIllegalArgumentException( + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.noNullElements(Collections.singleton(null))); assertEquals("The validated collection contains null element at index: 0", ex.getMessage()); } @@ -963,19 +961,19 @@ void shouldReturnNonBlankValue() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForBlankString() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(" ", "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notBlank(" ", "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyString() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank("", "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notBlank("", "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForStringContainingOnlyWhitespaceChars() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(" \n \t \r \n ", "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notBlank(" \n \t \r \n ", "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1011,19 +1009,19 @@ void shouldReturnNonBlankValue() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForBlankString() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(" ")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notBlank(" ")); assertEquals("The validated character sequence is blank", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyString() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank("")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notBlank("")); assertEquals("The validated character sequence is blank", ex.getMessage()); } @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForStringContainingOnlyWhitespaceChars() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notBlank(" \n \t \r \n ")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notBlank(" \n \t \r \n ")); assertEquals("The validated character sequence is blank", ex.getMessage()); } @@ -1057,7 +1055,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyArray() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(new Object[0], "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty(new Object[0], "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1084,7 +1082,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyArray() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(new Object[0])); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty(new Object[0])); assertEquals("The validated array is empty", ex.getMessage()); } @@ -1114,7 +1112,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyString() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty("", "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty("", "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1140,7 +1138,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyString() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty("")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty("")); assertEquals("The validated character sequence is empty", ex.getMessage()); } @@ -1171,7 +1169,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyCollection() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptySet(), "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty(Collections.emptySet(), "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1198,7 +1196,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyCollection() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptySet())); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty(Collections.emptySet())); assertEquals("The validated collection is empty", ex.getMessage()); } @@ -1229,7 +1227,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForEmptyMap() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptyMap(), "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty(Collections.emptyMap(), "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1256,7 +1254,7 @@ void shouldReturnTheSameInstance() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForEmptyMap() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notEmpty(Collections.emptyMap())); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notEmpty(Collections.emptyMap())); assertEquals("The validated map is empty", ex.getMessage()); } @@ -1292,7 +1290,7 @@ void shouldNotThrowExceptionForPositiveInfinity() { @Test void shouldThrowIllegalArgumentExceptionWithGivenMessageForNaN() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notNaN(Double.NaN, "MSG")); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notNaN(Double.NaN, "MSG")); assertEquals("MSG", ex.getMessage()); } } @@ -1317,7 +1315,7 @@ void shouldNotThrowExceptionForPositiveInfinity() { @Test void shouldThrowIllegalArgumentExceptionWithDefaultMessageForNaN() { - final IllegalArgumentException ex = assertIllegalArgumentException(() -> Validate.notNaN(Double.NaN)); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> Validate.notNaN(Double.NaN)); assertEquals("The validated value is not a number", ex.getMessage()); } } @@ -1414,13 +1412,13 @@ void shouldReturnSameInstance() { @Test void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(new String[] {"a"}, 1, "MSG")); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(new String[] {"a"}, 1, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException( + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(new String[] {"a"}, -1, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1448,13 +1446,13 @@ void shouldReturnSameInstance() { @Test void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(new String[] {"a"}, 1)); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(new String[] {"a"}, 1)); assertEquals("The validated array index is invalid: 1", ex.getMessage()); } @Test void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(new String[] {"a"}, -1)); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(new String[] {"a"}, -1)); assertEquals("The validated array index is invalid: -1", ex.getMessage()); } @@ -1485,13 +1483,13 @@ void shouldReturnSameInstance() { @Test void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", 1, "MSG")); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex("a", 1, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", -1, "MSG")); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex("a", -1, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1518,13 +1516,13 @@ void shouldReturnSameInstance() { @Test void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", 1)); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex("a", 1)); assertEquals("The validated character sequence index is invalid: 1", ex.getMessage()); } @Test void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex("a", -1)); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex("a", -1)); assertEquals("The validated character sequence index is invalid: -1", ex.getMessage()); } @@ -1555,13 +1553,13 @@ void shouldReturnSameInstance() { @Test void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException(() -> Validate.validIndex(Collections.singleton("a"), 1, "MSG")); + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(Collections.singleton("a"), 1, "MSG")); assertEquals("MSG", ex.getMessage()); } @Test void shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException( + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(Collections.singleton("a"), -1, "MSG")); assertEquals("MSG", ex.getMessage()); } @@ -1589,14 +1587,14 @@ void shouldReturnSameInstance() { @Test void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException( + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(Collections.singleton("a"), 1)); assertEquals("The validated collection index is invalid: 1", ex.getMessage()); } @Test void shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() { - final IndexOutOfBoundsException ex = assertIndexOutOfBoundsException( + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> Validate.validIndex(Collections.singleton("a"), -1)); assertEquals("The validated collection index is invalid: -1", ex.getMessage()); } diff --git a/src/test/java/org/apache/commons/lang3/builder/ConversionTest.java b/src/test/java/org/apache/commons/lang3/builder/ConversionTest.java index 75e0637c0ab..37352fd212a 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ConversionTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ConversionTest.java @@ -16,9 +16,9 @@ */ package org.apache.commons.lang3.builder; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.apache.commons.lang3.Conversion; import org.junit.jupiter.api.Test; @@ -33,7 +33,7 @@ void testHexToByte() { @Test void testHexToByte_IllegalArgument() { - assertIllegalArgumentException(() -> Conversion.hexToByte("A0", 0, (byte) 0, 4, 2)); + assertThrows(IllegalArgumentException.class, () -> Conversion.hexToByte("A0", 0, (byte) 0, 4, 2)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java index e09e8849257..2cf2154e139 100644 --- a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java @@ -17,10 +17,10 @@ package org.apache.commons.lang3.builder; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.commons.lang3.AbstractLangTest; @@ -290,22 +290,22 @@ void testCharArrayAsObject() { @Test void testConstructorExEvenFirst() { - assertIllegalArgumentException(() -> new HashCodeBuilder(2, 3)); + assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(2, 3)); } @Test void testConstructorExEvenNegative() { - assertIllegalArgumentException(() -> new HashCodeBuilder(-2, -2)); + assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(-2, -2)); } @Test void testConstructorExEvenSecond() { - assertIllegalArgumentException(() -> new HashCodeBuilder(3, 2)); + assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(3, 2)); } @Test void testConstructorExZero() { - assertIllegalArgumentException(() -> new HashCodeBuilder(0, 0)); + assertThrows(IllegalArgumentException.class, () -> new HashCodeBuilder(0, 0)); } @Test @@ -496,12 +496,12 @@ void testReflectionHashCode() { @Test void testReflectionHashCodeEx1() { - assertIllegalArgumentException(() -> HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true)); + assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true)); } @Test void testReflectionHashCodeEx2() { - assertIllegalArgumentException(() -> HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0), true)); + assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(2, 2, new TestObject(0), true)); } @Test @@ -540,12 +540,12 @@ void testReflectionHierarchyHashCode() { @Test void testReflectionHierarchyHashCodeEx1() { - assertIllegalArgumentException(() -> HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true)); + assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true)); } @Test void testReflectionHierarchyHashCodeEx2() { - assertIllegalArgumentException(() -> HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true)); + assertThrows(IllegalArgumentException.class, () -> HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true)); } /** diff --git a/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java b/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java index 10036ea83d6..745ee99ffd0 100644 --- a/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/JsonToStringStyleTest.java @@ -226,42 +226,72 @@ void testBlank() { @Test void testBooleanArray() { - final boolean[] array = { true, false }; + final boolean[] array = {true, false}; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"booleanArray\":[true,false]}", toStringBuilder.append("booleanArray", array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertEquals("{\"booleanArray\":[true,false]}", toStringBuilder.append("booleanArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test void testByteArray() { - final byte[] array = { 1, 2, -3, 4 }; + final byte[] array = {1, 2, -3, 4}; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"byteArray\":[1,2,-3,4]}", toStringBuilder.append("byteArray", array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertEquals("{\"byteArray\":[1,2,-3,4]}", toStringBuilder.append("byteArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test void testChar() { assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append('A').toString()); - assertEquals("{\"a\":\"A\"}", new ToStringBuilder(base).append("a", 'A').toString()); - assertEquals("{\"a\":\"A\",\"b\":\"B\"}", new ToStringBuilder(base).append("a", 'A').append("b", 'B').toString()); + + assertEquals("{\"a\":\"A\"}", new ToStringBuilder(base).append("a", 'A') + .toString()); + assertEquals("{\"a\":\"A\",\"b\":\"B\"}", new ToStringBuilder(base).append("a", 'A').append("b", 'B') + .toString()); } @Test void testCharArray() { - final char[] array = { '1', '2', '3', '4' }; + final char[] array = {'1', '2', '3', '4'}; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"charArray\":[\"1\",\"2\",\"3\",\"4\"]}", toStringBuilder.append("charArray", array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertEquals("{\"charArray\":[\"1\",\"2\",\"3\",\"4\"]}", toStringBuilder.append("charArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test @@ -311,33 +341,48 @@ void testDate() { @Test void testDoubleArray() { final double[] array = { 1, 2, -3, 4 }; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); + assertEquals("{\"doubleArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("doubleArray", array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test void testFloatArray() { final float[] array = { 1, 2, -3, 4 }; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); + assertEquals("{\"floatArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("floatArray", array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test void testIntArray() { final int[] array = { 1, 2, -3, 4 }; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); + assertEquals("{\"intArray\":[1,2,-3,4]}", toStringBuilder.append("intArray", array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @@ -387,21 +432,30 @@ void testLong() { @Test void testLongArray() { final long[] array = { 1, 2, -3, 4 }; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); + assertEquals("{\"longArray\":[1,2,-3,4]}", toStringBuilder.append("longArray", array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @Test void testLongArrayArray() { final long[][] array = { { 1, 2 }, null, { 5 } }; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[][]) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @@ -466,33 +520,52 @@ void testNull() { void testObject() { final Integer i3 = Integer.valueOf(3); final Integer i4 = Integer.valueOf(4); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(i3).toString()); + assertEquals("{\"a\":null}", new ToStringBuilder(base).append("a", (Object) null).toString()); assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", i3).toString()); assertEquals("{\"a\":3,\"b\":4}", new ToStringBuilder(base).append("a", i3).append("b", i4).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", i3, false).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", new ArrayList<>(), false).toString()); + assertEquals("{\"a\":[]}", new ToStringBuilder(base).append("a", new ArrayList<>(), true).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", new HashMap<>(), false).toString()); + assertEquals("{\"a\":{}}", new ToStringBuilder(base).append("a", new HashMap<>(), true).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new String[0], false).toString()); + assertEquals("{\"a\":[]}", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new int[] { 1, 2, 3 }, false).toString()); + assertEquals("{\"a\":[1,2,3]}", new ToStringBuilder(base).append("a", (Object) new int[] { 1, 2, 3 }, true).toString()); + assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, false).toString()); + assertEquals("{\"a\":[\"v\",\"x\",\"y\",\"z\"]}", new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, true).toString()); } @Test void testObjectArray() { final Object[] array = { null, base, new int[] { 3, 6 } }; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); + assertEquals("{\"objectArray\":[null,5,[3,6]]}", toStringBuilder.append("objectArray", array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object[]) null).toString()); + assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } @@ -541,12 +614,21 @@ void testRootMap() { @Test void testShortArray() { - final short[] array = { 1, 2, -3, 4 }; + final short[] array = {1, 2, -3, 4}; + final ToStringBuilder toStringBuilder = new ToStringBuilder(base); assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString()); - assertEquals("{\"shortArray\":[1,2,-3,4]}", toStringBuilder.append("shortArray", array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); - assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertEquals("{\"shortArray\":[1,2,-3,4]}", toStringBuilder.append("shortArray", array) + .toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString()); + + assertThrows( + UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString()); } } diff --git a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderIncludeTest.java b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderIncludeTest.java index 04107fd1d18..60d7cdc1795 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderIncludeTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderIncludeTest.java @@ -157,7 +157,9 @@ void test_toStringSetIncludeAndExcludeWithIntersection() { final ReflectionToStringBuilder builder = new ReflectionToStringBuilder(new TestFeature()); builder.setExcludeFieldNames(FIELDS[1], FIELDS[4]); builder.setIncludeFieldNames(FIELDS[0], FIELDS[1]); - Assertions.assertThrows(IllegalStateException.class, () -> builder.toString()); + Assertions.assertThrows(IllegalStateException.class, () -> { + builder.toString(); + }); } @Test @@ -174,7 +176,9 @@ void test_toStringSetIncludeAndExcludeWithRandomFieldsWithIntersection() { final ReflectionToStringBuilder builder = new ReflectionToStringBuilder(new TestFeature()); builder.setExcludeFieldNames(FIELDS[1], "random1"); builder.setIncludeFieldNames("random1"); - Assertions.assertThrows(IllegalStateException.class, () -> builder.toString()); + Assertions.assertThrows(IllegalStateException.class, () -> { + builder.toString(); + }); } @Test diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java index befd4ae8bf2..3c3604b7868 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java @@ -16,11 +16,11 @@ */ package org.apache.commons.lang3.builder; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.HashMap; @@ -232,7 +232,7 @@ public void assertReflectionArray(final String expected, final Object actual) { void test_setUpToClass_invalid() { final Integer val = Integer.valueOf(5); final ReflectionToStringBuilder test = new ReflectionToStringBuilder(val); - assertIllegalArgumentException(() -> test.setUpToClass(String.class)); + assertThrows(IllegalArgumentException.class, () -> test.setUpToClass(String.class)); test.toString(); } diff --git a/src/test/java/org/apache/commons/lang3/concurrent/ConcurrentUtilsTest.java b/src/test/java/org/apache/commons/lang3/concurrent/ConcurrentUtilsTest.java index dc925aada49..a984817adf9 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/ConcurrentUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/ConcurrentUtilsTest.java @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.commons.lang3.concurrent; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,6 +38,54 @@ * Test class for {@link ConcurrentUtils}. */ class ConcurrentUtilsTest extends AbstractLangTest { + /** + * Tests creating a ConcurrentException with an error as cause. + */ + @Test + void testConcurrentExceptionCauseError() { + assertThrows(IllegalArgumentException.class, () -> new ConcurrentException("An error", new Error())); + } + + /** + * Tests creating a ConcurrentException with null as cause. + */ + @Test + void testConcurrentExceptionCauseNull() { + assertThrows(IllegalArgumentException.class, () -> new ConcurrentException(null)); + } + + /** + * Tests creating a ConcurrentException with a runtime exception as cause. + */ + @Test + void testConcurrentExceptionCauseUnchecked() { + assertThrows(IllegalArgumentException.class, () -> new ConcurrentException(new RuntimeException())); + } + + /** + * Tries to create a ConcurrentRuntimeException with an error as cause. + */ + @Test + void testConcurrentRuntimeExceptionCauseError() { + assertThrows(IllegalArgumentException.class, () -> new ConcurrentRuntimeException("An error", new Error())); + } + + /** + * Tries to create a ConcurrentRuntimeException with null as cause. + */ + @Test + void testConcurrentRuntimeExceptionCauseNull() { + assertThrows(IllegalArgumentException.class, () -> new ConcurrentRuntimeException(null)); + } + + /** + * Tries to create a ConcurrentRuntimeException with a runtime as cause. + */ + @Test + void testConcurrentRuntimeExceptionCauseUnchecked() { + assertThrows(IllegalArgumentException.class, () -> new ConcurrentRuntimeException(new RuntimeException())); + } + /** * Tests constant future. * diff --git a/src/test/java/org/apache/commons/lang3/concurrent/MemoizerFunctionTest.java b/src/test/java/org/apache/commons/lang3/concurrent/MemoizerFunctionTest.java index be0b99f9bd2..570d5f12e56 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/MemoizerFunctionTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/MemoizerFunctionTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.concurrent; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -47,7 +46,7 @@ void testDefaultBehaviourNotToRecalculateExecutionExceptions() throws Exception replay(function); assertThrows(Throwable.class, () -> memoizer.compute(input)); - assertIllegalArgumentException(() -> memoizer.compute(input)); + assertThrows(IllegalArgumentException.class, () -> memoizer.compute(input)); } @Test @@ -59,7 +58,7 @@ void testDoesNotRecalculateWhenSetToFalse() throws Exception { replay(function); assertThrows(Throwable.class, () -> memoizer.compute(input)); - assertIllegalArgumentException(() -> memoizer.compute(input)); + assertThrows(IllegalArgumentException.class, () -> memoizer.compute(input)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java index e7dff576291..26b040fa8ab 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java @@ -230,7 +230,9 @@ public void setUp() { @Test void testAddInitializerAfterStart() throws ConcurrentException { initializer.start(); - assertThrows(IllegalStateException.class, () -> initializer.addInitializer(CHILD_INIT, createChildBackgroundInitializer()), + assertThrows( + IllegalStateException.class, + () -> initializer.addInitializer(CHILD_INIT, createChildBackgroundInitializer()), "Could add initializer after start()!"); initializer.get(); } diff --git a/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java b/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java index f6b4e206271..a1d83eac44b 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.concurrent; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -410,7 +409,7 @@ void testInitDefaultService() { */ @Test void testInitInvalidPeriod() { - assertIllegalArgumentException(() -> new TimedSemaphore(0L, UNIT, LIMIT)); + assertThrows(IllegalArgumentException.class, () -> new TimedSemaphore(0L, UNIT, LIMIT)); } /** diff --git a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java index 79f6fc00ec7..530085a4203 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3.event; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -99,7 +98,7 @@ void testAddNullListener() { @Test void testCreateWithNonInterfaceParameter() { - assertIllegalArgumentException(() -> EventListenerSupport.create(String.class)); + assertThrows(IllegalArgumentException.class, () -> EventListenerSupport.create(String.class)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java index 373ab4412ac..9e47c731491 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.event; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -68,11 +67,12 @@ public int getCount() { } private static final class EventCountingInvocationHandler implements InvocationHandler { - private final Map eventCounts = new TreeMap<>(); public L createListener(final Class listenerType) { - return listenerType.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { listenerType }, this)); + return listenerType.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), + new Class[]{listenerType}, + this)); } public int getEventCount(final String eventName) { @@ -151,9 +151,11 @@ void testAddEventListener() { @Test void testAddEventListenerThrowsException() { final ExceptionEventSource src = new ExceptionEventSource(); - assertThrows(RuntimeException.class, () -> EventUtils.addEventListener(src, PropertyChangeListener.class, e -> { - // Do nothing! - })); + assertThrows(RuntimeException.class, () -> + EventUtils.addEventListener(src, PropertyChangeListener.class, e -> { + // Do nothing! + }) + ); } @Test @@ -161,7 +163,8 @@ void testAddEventListenerWithNoAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvocationHandler handler = new EventCountingInvocationHandler(); final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); - final IllegalArgumentException e = assertIllegalArgumentException(() -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", e.getMessage()); } @@ -171,7 +174,8 @@ void testAddEventListenerWithPrivateAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvocationHandler handler = new EventCountingInvocationHandler(); final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); - final IllegalArgumentException e = assertIllegalArgumentException(() -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", e.getMessage()); } diff --git a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java index 03cf3e4e9ab..cac4880b1f8 100644 --- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java @@ -695,12 +695,17 @@ void testPrintRootCauseStackTrace_ThrowableStream() { ExceptionUtils.printRootCauseStackTrace(null, (PrintStream) null); ExceptionUtils.printRootCauseStackTrace(null, new PrintStream(out)); assertEquals(0, out.toString().length()); - assertNullPointerException(() -> ExceptionUtils.printRootCauseStackTrace(withCause, (PrintStream) null)); + + assertThrows( + NullPointerException.class, + () -> ExceptionUtils.printRootCauseStackTrace(withCause, (PrintStream) null)); + out = new ByteArrayOutputStream(1024); final Throwable cause = createExceptionWithCause(); ExceptionUtils.printRootCauseStackTrace(cause, new PrintStream(out)); String stackTrace = out.toString(); assertTrue(stackTrace.contains(ExceptionUtils.WRAPPED_MARKER)); + out = new ByteArrayOutputStream(1024); ExceptionUtils.printRootCauseStackTrace(withoutCause, new PrintStream(out)); stackTrace = out.toString(); @@ -713,12 +718,17 @@ void testPrintRootCauseStackTrace_ThrowableWriter() { ExceptionUtils.printRootCauseStackTrace(null, (PrintWriter) null); ExceptionUtils.printRootCauseStackTrace(null, new PrintWriter(writer)); assertEquals(0, writer.getBuffer().length()); - assertNullPointerException(() -> ExceptionUtils.printRootCauseStackTrace(withCause, (PrintWriter) null)); + + assertThrows( + NullPointerException.class, + () -> ExceptionUtils.printRootCauseStackTrace(withCause, (PrintWriter) null)); + writer = new StringWriter(1024); final Throwable cause = createExceptionWithCause(); ExceptionUtils.printRootCauseStackTrace(cause, new PrintWriter(writer)); String stackTrace = writer.toString(); assertTrue(stackTrace.contains(ExceptionUtils.WRAPPED_MARKER)); + writer = new StringWriter(1024); ExceptionUtils.printRootCauseStackTrace(withoutCause, new PrintWriter(writer)); stackTrace = writer.toString(); diff --git a/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java index 87fe329d47e..9f99d9a3225 100644 --- a/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java @@ -16,9 +16,8 @@ */ package org.apache.commons.lang3.math; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; -import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.commons.lang3.AbstractLangTest; @@ -36,14 +35,45 @@ void testConstructorExists() { @Test void testEnforceExceptions() { - assertNullPointerException(() -> IEEE754rUtils.min((float[]) null), "IllegalArgumentException expected for null input"); - assertIllegalArgumentException(IEEE754rUtils::min, "IllegalArgumentException expected for empty input"); - assertNullPointerException(() -> IEEE754rUtils.max((float[]) null), "IllegalArgumentException expected for null input"); - assertIllegalArgumentException(IEEE754rUtils::max, "IllegalArgumentException expected for empty input"); - assertNullPointerException(() -> IEEE754rUtils.min((double[]) null), "IllegalArgumentException expected for null input"); - assertIllegalArgumentException(IEEE754rUtils::min, "IllegalArgumentException expected for empty input"); - assertNullPointerException(() -> IEEE754rUtils.max((double[]) null), "IllegalArgumentException expected for null input"); - assertIllegalArgumentException(IEEE754rUtils::max, "IllegalArgumentException expected for empty input"); + assertThrows( + NullPointerException.class, + () -> IEEE754rUtils.min((float[]) null), + "IllegalArgumentException expected for null input"); + + assertThrows( + IllegalArgumentException.class, + IEEE754rUtils::min, + "IllegalArgumentException expected for empty input"); + + assertThrows( + NullPointerException.class, + () -> IEEE754rUtils.max((float[]) null), + "IllegalArgumentException expected for null input"); + + assertThrows( + IllegalArgumentException.class, + IEEE754rUtils::max, + "IllegalArgumentException expected for empty input"); + + assertThrows( + NullPointerException.class, + () -> IEEE754rUtils.min((double[]) null), + "IllegalArgumentException expected for null input"); + + assertThrows( + IllegalArgumentException.class, + IEEE754rUtils::min, + "IllegalArgumentException expected for empty input"); + + assertThrows( + NullPointerException.class, + () -> IEEE754rUtils.max((double[]) null), + "IllegalArgumentException expected for null input"); + + assertThrows( + IllegalArgumentException.class, + IEEE754rUtils::max, + "IllegalArgumentException expected for empty input"); } @Test diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index beaeaf41dc0..e73d6132269 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.math; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -1195,7 +1194,7 @@ void testMaxByte() { @Test void testMaxByte_emptyArray() { - assertIllegalArgumentException(NumberUtils::max); + assertThrows(IllegalArgumentException.class, NumberUtils::max); } @Test @@ -1208,7 +1207,7 @@ void testMaxDouble() { final double[] d = null; assertNullPointerException(() -> NumberUtils.max(d), "No exception was thrown for null input."); - assertIllegalArgumentException(NumberUtils::max, "No exception was thrown for empty input."); + assertThrows(IllegalArgumentException.class, NumberUtils::max, "No exception was thrown for empty input."); assertEquals(5.1f, NumberUtils.max(5.1f), "max(double[]) failed for array length 1"); assertEquals(9.2f, NumberUtils.max(6.3f, 9.2f), "max(double[]) failed for array length 2"); @@ -1219,7 +1218,7 @@ void testMaxDouble() { @Test void testMaxDouble_emptyArray() { - assertIllegalArgumentException(NumberUtils::max); + assertThrows(IllegalArgumentException.class, NumberUtils::max); } @Test @@ -1238,7 +1237,7 @@ void testMaxFloat() { @Test void testMaxFloat_emptyArray() { - assertIllegalArgumentException(NumberUtils::max); + assertThrows(IllegalArgumentException.class, NumberUtils::max); } @Test @@ -1321,7 +1320,7 @@ void testMaxInt() { @Test void testMaxInt_emptyArray() { - assertIllegalArgumentException(NumberUtils::max); + assertThrows(IllegalArgumentException.class, NumberUtils::max); } @Test @@ -1340,7 +1339,7 @@ void testMaxLong() { @Test void testMaxLong_emptyArray() { - assertIllegalArgumentException(NumberUtils::max); + assertThrows(IllegalArgumentException.class, NumberUtils::max); } @Test @@ -1360,7 +1359,7 @@ void testMaxShort() { @Test void testMaxShort_emptyArray() { - assertIllegalArgumentException(NumberUtils::max); + assertThrows(IllegalArgumentException.class, NumberUtils::max); } @Test @@ -1379,7 +1378,7 @@ void testMinByte() { @Test void testMinByte_emptyArray() { - assertIllegalArgumentException(NumberUtils::min); + assertThrows(IllegalArgumentException.class, NumberUtils::min); } @Test @@ -1399,7 +1398,7 @@ void testMinDouble() { @Test void testMinDouble_emptyArray() { - assertIllegalArgumentException(NumberUtils::min); + assertThrows(IllegalArgumentException.class, NumberUtils::min); } @Test @@ -1420,7 +1419,7 @@ void testMinFloat() { @Test void testMinFloat_emptyArray() { - assertIllegalArgumentException(NumberUtils::min); + assertThrows(IllegalArgumentException.class, NumberUtils::min); } @Test @@ -1503,7 +1502,7 @@ void testMinInt() { @Test void testMinInt_emptyArray() { - assertIllegalArgumentException(NumberUtils::min); + assertThrows(IllegalArgumentException.class, NumberUtils::min); } @Test @@ -1522,7 +1521,7 @@ void testMinLong() { @Test void testMinLong_emptyArray() { - assertIllegalArgumentException(NumberUtils::min); + assertThrows(IllegalArgumentException.class, NumberUtils::min); } @Test @@ -1541,7 +1540,7 @@ void testMinShort() { @Test void testMinShort_emptyArray() { - assertIllegalArgumentException(NumberUtils::min); + assertThrows(IllegalArgumentException.class, NumberUtils::min); } @Test diff --git a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java index 4666ec7fbdc..91ee3a5441b 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java @@ -14,10 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.commons.lang3.reflect; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -33,10 +31,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; +import java.util.*; import org.apache.commons.lang3.AbstractLangTest; import org.apache.commons.lang3.ArraySorter; @@ -44,6 +39,7 @@ import org.apache.commons.lang3.JavaVersion; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.compare.ObjectToStringComparator; +import org.apache.commons.lang3.function.Objects; import org.apache.commons.lang3.reflect.testbed.Ambig; import org.apache.commons.lang3.reflect.testbed.Annotated; import org.apache.commons.lang3.reflect.testbed.AnotherParent; @@ -66,6 +62,7 @@ class FieldUtilsTest extends AbstractLangTest { static final Integer I1 = Integer.valueOf(1); static final Double D0 = Double.valueOf(0.0); static final Double D1 = Double.valueOf(1.0); + @Annotated private PublicChild publicChild; private PubliclyShadowedChild publiclyShadowedChild; @@ -74,10 +71,13 @@ class FieldUtilsTest extends AbstractLangTest { private final Class parentClass = PublicChild.class.getSuperclass(); /** - * Reads the {@code @deprecated} notice on {@link FieldUtils#removeFinalModifier(Field, boolean)}. + * Reads the {@code @deprecated} notice on + * {@link FieldUtils#removeFinalModifier(Field, boolean)}. * - * @param field {@link Field} to be curried into {@link FieldUtils#removeFinalModifier(Field, boolean)}. - * @param forceAccess {@link Boolean} to be curried into {@link FieldUtils#removeFinalModifier(Field, boolean)}. + * @param field {@link Field} to be curried into + * {@link FieldUtils#removeFinalModifier(Field, boolean)}. + * @param forceAccess {@link Boolean} to be curried into + * {@link FieldUtils#removeFinalModifier(Field, boolean)}. */ private void callRemoveFinalModifierCheckForException(final Field field, final Boolean forceAccess) { try { @@ -106,7 +106,7 @@ private Field[] sort(final Field[] fields) { @Test void testAmbig() { - assertIllegalArgumentException(() -> FieldUtils.getField(Ambig.class, "VALUE")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(Ambig.class, "VALUE")); } @Test @@ -117,6 +117,7 @@ void testConstructor() { assertTrue(Modifier.isPublic(cons[0].getModifiers())); assertTrue(Modifier.isPublic(FieldUtils.class.getModifiers())); assertFalse(Modifier.isFinal(FieldUtils.class.getModifiers())); + } @Test @@ -142,6 +143,8 @@ void testGetAllFields() { assertEquals(expected, allFields.length, Arrays.toString(allFields)); } + + @Test void testGetAllFieldsList() { assertEquals(0, FieldUtils.getAllFieldsList(Object.class).size()); @@ -160,6 +163,7 @@ void testGetAllFieldsList() { } } assertEquals(expected, allFields.size(), allFields.toString()); + } @Test @@ -188,17 +192,17 @@ void testGetDeclaredField() { @Test void testGetDeclaredFieldAccessIllegalArgumentException1() { - assertIllegalArgumentException(() -> FieldUtils.getDeclaredField(PublicChild.class, null)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, null)); } @Test void testGetDeclaredFieldAccessIllegalArgumentException2() { - assertIllegalArgumentException(() -> FieldUtils.getDeclaredField(PublicChild.class, "")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, "")); } @Test void testGetDeclaredFieldAccessIllegalArgumentException3() { - assertIllegalArgumentException(() -> FieldUtils.getDeclaredField(PublicChild.class, " ")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, " ")); } @Test @@ -227,17 +231,17 @@ void testGetDeclaredFieldForceAccess() { @Test void testGetDeclaredFieldForceAccessIllegalArgumentException1() { - assertIllegalArgumentException(() -> FieldUtils.getDeclaredField(PublicChild.class, null, true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, null, true)); } @Test void testGetDeclaredFieldForceAccessIllegalArgumentException2() { - assertIllegalArgumentException(() -> FieldUtils.getDeclaredField(PublicChild.class, "", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, "", true)); } @Test void testGetDeclaredFieldForceAccessIllegalArgumentException3() { - assertIllegalArgumentException(() -> FieldUtils.getDeclaredField(PublicChild.class, " ", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, " ", true)); } @Test @@ -285,17 +289,17 @@ void testGetFieldForceAccess() { @Test void testGetFieldForceAccessIllegalArgumentException1() { - assertIllegalArgumentException(() -> FieldUtils.getField(PublicChild.class, null, true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, null, true)); } @Test void testGetFieldForceAccessIllegalArgumentException2() { - assertIllegalArgumentException(() -> FieldUtils.getField(PublicChild.class, "", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, "", true)); } @Test void testGetFieldForceAccessIllegalArgumentException3() { - assertIllegalArgumentException(() -> FieldUtils.getField(PublicChild.class, " ", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, " ", true)); } @Test @@ -305,17 +309,17 @@ void testGetFieldForceAccessNullPointerException() { @Test void testGetFieldIllegalArgumentException1() { - assertIllegalArgumentException(() -> FieldUtils.getField(PublicChild.class, null)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, null)); } @Test void testGetFieldIllegalArgumentException2() { - assertIllegalArgumentException(() -> FieldUtils.getField(PublicChild.class, "")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, "")); } @Test void testGetFieldIllegalArgumentException3() { - assertIllegalArgumentException(() -> FieldUtils.getField(PublicChild.class, " ")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, " ")); } @Test @@ -326,8 +330,10 @@ void testGetFieldNullPointerException() { @Test void testGetFieldsListWithAnnotation() throws NoSuchFieldException { assertEquals(0, FieldUtils.getFieldsListWithAnnotation(Object.class, Annotated.class).size()); - final List annotatedFields = Arrays.asList(FieldUtilsTest.class.getDeclaredField("publicChild"), - FieldUtilsTest.class.getDeclaredField("privatelyShadowedChild")); + final List annotatedFields = Arrays.asList( + FieldUtilsTest.class.getDeclaredField("publicChild"), + FieldUtilsTest.class.getDeclaredField("privatelyShadowedChild") + ); final List fieldUtilsTestAnnotatedFields = FieldUtils.getFieldsListWithAnnotation(FieldUtilsTest.class, Annotated.class); assertEquals(annotatedFields.size(), fieldUtilsTestAnnotatedFields.size()); assertTrue(fieldUtilsTestAnnotatedFields.contains(annotatedFields.get(0))); @@ -352,9 +358,11 @@ void testGetFieldsListWithAnnotationNullPointerException3() { @Test void testGetFieldsWithAnnotation() throws NoSuchFieldException { assertArrayEquals(new Field[0], FieldUtils.getFieldsWithAnnotation(Object.class, Annotated.class)); - final Field[] annotatedFields = sort( - new Field[] { FieldUtilsTest.class.getDeclaredField("publicChild"), FieldUtilsTest.class.getDeclaredField("privatelyShadowedChild") }); - assertArrayEquals(annotatedFields, sort(FieldUtils.getFieldsWithAnnotation(FieldUtilsTest.class, Annotated.class))); + final Field[] annotatedFields = sort(new Field[] { + FieldUtilsTest.class.getDeclaredField("publicChild"), + FieldUtilsTest.class.getDeclaredField("privatelyShadowedChild") }); + assertArrayEquals(annotatedFields, + sort(FieldUtils.getFieldsWithAnnotation(FieldUtilsTest.class, Annotated.class))); } @Test @@ -374,46 +382,72 @@ void testGetFieldsWithAnnotationNullPointerException3() { @Test void testReadDeclaredNamedField() throws Exception { - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, null), + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredField(publicChild, null), "a null field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, ""), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredField(publicChild, ""), "an empty field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, " "), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredField(publicChild, " "), "a blank field name should cause an IllegalArgumentException"); - assertNullPointerException(() -> FieldUtils.readDeclaredField(null, "none"), "a null target should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "s")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readDeclaredField(null, "none"), + "a null target should cause an NullPointerException"); + + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "s")); assertEquals("ss", FieldUtils.readDeclaredField(publiclyShadowedChild, "s")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(privatelyShadowedChild, "s")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "b")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "s")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "b")); assertEquals(Boolean.TRUE, FieldUtils.readDeclaredField(publiclyShadowedChild, "b")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(privatelyShadowedChild, "b")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "i")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "b")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "i")); assertEquals(I1, FieldUtils.readDeclaredField(publiclyShadowedChild, "i")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(privatelyShadowedChild, "i")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "d")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "i")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "d")); assertEquals(D1, FieldUtils.readDeclaredField(publiclyShadowedChild, "d")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(privatelyShadowedChild, "d")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(privatelyShadowedChild, "d")); } @Test void testReadDeclaredNamedFieldForceAccess() throws Exception { - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, null, true), + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredField(publicChild, null, true), "a null field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "", true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredField(publicChild, "", true), "an empty field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, " ", true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredField(publicChild, " ", true), "a blank field name should cause an IllegalArgumentException"); - assertNullPointerException(() -> FieldUtils.readDeclaredField(null, "none", true), "a null target should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "s", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readDeclaredField(null, "none", true), + "a null target should cause an NullPointerException"); + + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "s", true)); assertEquals("ss", FieldUtils.readDeclaredField(publiclyShadowedChild, "s", true)); assertEquals("ss", FieldUtils.readDeclaredField(privatelyShadowedChild, "s", true)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "b", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "b", true)); assertEquals(Boolean.TRUE, FieldUtils.readDeclaredField(publiclyShadowedChild, "b", true)); assertEquals(Boolean.TRUE, FieldUtils.readDeclaredField(privatelyShadowedChild, "b", true)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "i", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "i", true)); assertEquals(I1, FieldUtils.readDeclaredField(publiclyShadowedChild, "i", true)); assertEquals(I1, FieldUtils.readDeclaredField(privatelyShadowedChild, "i", true)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredField(publicChild, "d", true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "d", true)); assertEquals(D1, FieldUtils.readDeclaredField(publiclyShadowedChild, "d", true)); assertEquals(D1, FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true)); } @@ -422,24 +456,47 @@ void testReadDeclaredNamedFieldForceAccess() throws Exception { void testReadDeclaredNamedStaticField() throws Exception { assertNullPointerException(() -> FieldUtils.readDeclaredStaticField(AnotherParent.class, "doesNotExist")); assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE")); - assertNullPointerException(() -> FieldUtils.readDeclaredField(null, "VALUE")); - assertNullPointerException(() -> FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE")); - assertNullPointerException(() -> FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE")); - assertNullPointerException(() -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredStaticField(PublicChild.class, null)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredStaticField(PublicChild.class, "")); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredStaticField(PublicChild.class, " ")); + assertThrows( + NullPointerException.class, () -> + FieldUtils.readDeclaredField(null, "VALUE")); + assertThrows( + NullPointerException.class, () -> FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PublicChild.class, null)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PublicChild.class, "")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PublicChild.class, " ")); } @Test void testReadDeclaredNamedStaticFieldForceAccess() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE", true)); assertEquals("child", FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE", true)); - assertNullPointerException(() -> FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE", true)); - assertNullPointerException(() -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE", true)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, null, true)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "", true)); - assertIllegalArgumentException(() -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, " ", true)); + assertThrows( + NullPointerException.class, + () -> FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE", true)); + assertThrows( + NullPointerException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, null, true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, " ", true)); } @Test @@ -460,7 +517,11 @@ void testReadField() throws Exception { assertEquals(D0, FieldUtils.readField(parentD, publicChild)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild)); - assertNullPointerException(() -> FieldUtils.readField(null, publicChild), "a null field should cause an NullPointerException"); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readField(null, publicChild), + "a null field should cause an NullPointerException"); } @Test @@ -485,7 +546,11 @@ void testReadFieldForceAccess() throws Exception { assertEquals(D0, FieldUtils.readField(parentD, publicChild, true)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild, true)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild, true)); - assertNullPointerException(() -> FieldUtils.readField(null, publicChild, true), "a null field should cause an NullPointerException"); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readField(null, publicChild, true), + "a null field should cause an NullPointerException"); } @Test @@ -493,22 +558,37 @@ void testReadNamedField() throws Exception { assertEquals("s", FieldUtils.readField(publicChild, "s")); assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s")); assertEquals("s", FieldUtils.readField(privatelyShadowedChild, "s")); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, null), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readField(publicChild, null), "a null field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, ""), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readField(publicChild, ""), "an empty field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, " "), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readField(publicChild, " "), "a blank field name should cause an IllegalArgumentException"); - assertNullPointerException(() -> FieldUtils.readField((Object) null, "none"), "a null target should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, "b")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readField((Object) null, "none"), + "a null target should cause an NullPointerException"); + + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "b")); + assertEquals(Boolean.TRUE, FieldUtils.readField(publiclyShadowedChild, "b")); - assertIllegalArgumentException(() -> FieldUtils.readField(privatelyShadowedChild, "b")); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, "i")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(privatelyShadowedChild, "b")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "i")); assertEquals(I1, FieldUtils.readField(publiclyShadowedChild, "i")); - assertIllegalArgumentException(() -> FieldUtils.readField(privatelyShadowedChild, "i")); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, "d")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(privatelyShadowedChild, "i")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "d")); assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d")); - assertIllegalArgumentException(() -> FieldUtils.readField(privatelyShadowedChild, "d")); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(privatelyShadowedChild, "d")); } @Test @@ -525,13 +605,26 @@ void testReadNamedFieldForceAccess() throws Exception { assertEquals(D0, FieldUtils.readField(publicChild, "d", true)); assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d", true)); assertEquals(D1, FieldUtils.readField(privatelyShadowedChild, "d", true)); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, null, true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readField(publicChild, null, true), "a null field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, "", true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readField(publicChild, "", true), "an empty field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readField(publicChild, " ", true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readField(publicChild, " ", true), "a blank field name should cause an IllegalArgumentException"); - assertNullPointerException(() -> FieldUtils.readField((Object) null, "none", true), "a null target should cause an NullPointerException"); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readField((Object) null, "none", true), + "a null target should cause an NullPointerException"); } @Test @@ -540,16 +633,35 @@ void testReadNamedStaticField() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PublicChild.class, "VALUE")); - assertNullPointerException(() -> FieldUtils.readStaticField(null, "none"), "null class should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(Foo.class, null), + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readStaticField(null, "none"), + "null class should cause an NullPointerException"); + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(Foo.class, null), "null field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(Foo.class, ""), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(Foo.class, ""), "empty field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(Foo.class, " "), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(Foo.class, " "), "blank field name should cause an IllegalArgumentException"); - assertNullPointerException(() -> FieldUtils.readStaticField(Foo.class, "does_not_exist"), + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readStaticField(Foo.class, "does_not_exist"), "a field that doesn't exist should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(PublicChild.class, "s"), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(PublicChild.class, "s"), "non-static field should cause an IllegalArgumentException"); } @@ -559,16 +671,35 @@ void testReadNamedStaticFieldForceAccess() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE", true)); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE", true)); assertEquals("child", FieldUtils.readStaticField(PublicChild.class, "VALUE", true)); - assertNullPointerException(() -> FieldUtils.readStaticField(null, "none", true), "null class should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(Foo.class, null, true), + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readStaticField(null, "none", true), + "null class should cause an NullPointerException"); + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(Foo.class, null, true), "null field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(Foo.class, "", true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(Foo.class, "", true), "empty field name should cause an IllegalArgumentException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(Foo.class, " ", true), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(Foo.class, " ", true), "blank field name should cause an IllegalArgumentException"); - assertNullPointerException(() -> FieldUtils.readStaticField(Foo.class, "does_not_exist", true), + + assertThrows( + NullPointerException.class, + () -> FieldUtils.readStaticField(Foo.class, "does_not_exist", true), "a field that doesn't exist should cause an NullPointerException"); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(PublicChild.class, "s", false), + + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readStaticField(PublicChild.class, "s", false), "non-static field should cause an IllegalArgumentException"); } @@ -587,7 +718,7 @@ void testReadStaticFieldForceAccess() throws Exception { void testReadStaticFieldForceAccessIllegalArgumentException() { final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true); assumeTrue(nonStaticField != null); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(nonStaticField, true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readStaticField(nonStaticField, true)); } @Test @@ -600,7 +731,7 @@ void testReadStaticFieldIllegalArgumentException() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s"); assumeTrue(nonStaticField != null); - assertIllegalArgumentException(() -> FieldUtils.readStaticField(nonStaticField)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readStaticField(nonStaticField)); } @Test @@ -668,10 +799,16 @@ void testRemoveFinalModifierWithoutAccess() throws Exception { @Test void testWriteDeclaredNamedField() throws Exception { - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "s", "S")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1))); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0))); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(publicChild, "s", "S")); + assertThrows( + IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1))); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0))); + FieldUtils.writeDeclaredField(publiclyShadowedChild, "s", "S"); assertEquals("S", FieldUtils.readDeclaredField(publiclyShadowedChild, "s")); FieldUtils.writeDeclaredField(publiclyShadowedChild, "b", Boolean.FALSE); @@ -680,22 +817,46 @@ void testWriteDeclaredNamedField() throws Exception { assertEquals(Integer.valueOf(0), FieldUtils.readDeclaredField(publiclyShadowedChild, "i")); FieldUtils.writeDeclaredField(publiclyShadowedChild, "d", Double.valueOf(0.0)); assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(publiclyShadowedChild, "d")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "s", "S")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "b", Boolean.TRUE)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "i", Integer.valueOf(1))); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(1.0))); - assertNullPointerException(() -> FieldUtils.writeDeclaredField(null, "s", "S")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "null", "S")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "", "S")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, " ", "S")); + + assertThrows( + IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "s", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "b", Boolean.TRUE)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "i", Integer.valueOf(1))); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(1.0))); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredField(null, "s", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "null", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, " ", "S")); } @Test void testWriteDeclaredNamedFieldForceAccess() throws Exception { - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "s", "S", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE, true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1), true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0), true)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(publicChild, "s", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "b", Boolean.TRUE, true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "i", Integer.valueOf(1), true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "d", Double.valueOf(1.0), true)); + FieldUtils.writeDeclaredField(publiclyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readDeclaredField(publiclyShadowedChild, "s", true)); FieldUtils.writeDeclaredField(publiclyShadowedChild, "b", Boolean.FALSE, true); @@ -704,6 +865,7 @@ void testWriteDeclaredNamedFieldForceAccess() throws Exception { assertEquals(Integer.valueOf(0), FieldUtils.readDeclaredField(publiclyShadowedChild, "i", true)); FieldUtils.writeDeclaredField(publiclyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(publiclyShadowedChild, "d", true)); + FieldUtils.writeDeclaredField(privatelyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readDeclaredField(privatelyShadowedChild, "s", true)); FieldUtils.writeDeclaredField(privatelyShadowedChild, "b", Boolean.FALSE, true); @@ -712,27 +874,59 @@ void testWriteDeclaredNamedFieldForceAccess() throws Exception { assertEquals(Integer.valueOf(0), FieldUtils.readDeclaredField(privatelyShadowedChild, "i", true)); FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true)); - assertNullPointerException(() -> FieldUtils.writeDeclaredField(null, "s", "S", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "null", "S", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, "", "S", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredField(publicChild, " ", "S", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredField(null, "s", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "null", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, " ", "S", true)); } @Test void testWriteDeclaredNamedStaticField() throws Exception { FieldUtils.writeStaticField(StaticContainer.class, "mutablePublic", "new"); assertEquals("new", StaticContainer.mutablePublic); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutableProtected", "new")); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePackage", "new")); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new")); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new")); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new")); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new")); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(null, "mutablePublic", "new")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, null, "new")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "", "new")); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, " ", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutableProtected", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePackage", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(null, "mutablePublic", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, null, "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, " ", "new")); } @Test @@ -745,14 +939,31 @@ void testWriteDeclaredNamedStaticFieldForceAccess() throws Exception { assertEquals("new", StaticContainer.getMutablePackage()); FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new", true); assertEquals("new", StaticContainer.getMutablePrivate()); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new", true)); - assertNullPointerException(() -> FieldUtils.writeDeclaredStaticField(null, "mutablePublic", "new", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, null, "new", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "", "new", true)); - assertIllegalArgumentException(() -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, " ", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(null, "mutablePublic", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, null, "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, " ", "new", true)); } @Test @@ -760,12 +971,19 @@ void testWriteField() throws Exception { final Field field = parentClass.getDeclaredField("s"); FieldUtils.writeField(field, publicChild, "S"); assertEquals("S", field.get(publicChild)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeField(parentClass.getDeclaredField("b"), publicChild, Boolean.TRUE)); - assertThrows(IllegalAccessException.class, + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeField(parentClass.getDeclaredField("b"), publicChild, Boolean.TRUE)); + assertThrows( + IllegalAccessException.class, () -> FieldUtils.writeField(parentClass.getDeclaredField("i"), publicChild, Integer.valueOf(Integer.MAX_VALUE))); - assertThrows(IllegalAccessException.class, + assertThrows( + IllegalAccessException.class, () -> FieldUtils.writeField(parentClass.getDeclaredField("d"), publicChild, Double.valueOf(Double.MAX_VALUE))); - assertNullPointerException(() -> FieldUtils.writeField(null, publicChild, "S")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField(null, publicChild, "S")); } @Test @@ -782,16 +1000,21 @@ void testWriteFieldForceAccess() throws Exception { field = parentClass.getDeclaredField("d"); FieldUtils.writeField(field, publicChild, Double.valueOf(Double.MAX_VALUE), true); assertEquals(Double.valueOf(Double.MAX_VALUE), field.get(publicChild)); - assertNullPointerException(() -> FieldUtils.writeField(null, publicChild, "S", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField(null, publicChild, "S", true)); } @Test void testWriteNamedField() throws Exception { FieldUtils.writeField(publicChild, "s", "S"); assertEquals("S", FieldUtils.readField(publicChild, "s")); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, "b", Boolean.TRUE)); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, "i", Integer.valueOf(1))); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0))); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeField(publicChild, "b", Boolean.TRUE)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.writeField(publicChild, "i", Integer.valueOf(1))); + assertThrows( + IllegalArgumentException.class, () -> FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0))); + FieldUtils.writeField(publiclyShadowedChild, "s", "S"); assertEquals("S", FieldUtils.readField(publiclyShadowedChild, "s")); FieldUtils.writeField(publiclyShadowedChild, "b", Boolean.FALSE); @@ -800,15 +1023,31 @@ void testWriteNamedField() throws Exception { assertEquals(Integer.valueOf(0), FieldUtils.readField(publiclyShadowedChild, "i")); FieldUtils.writeField(publiclyShadowedChild, "d", Double.valueOf(0.0)); assertEquals(Double.valueOf(0.0), FieldUtils.readField(publiclyShadowedChild, "d")); + FieldUtils.writeField(privatelyShadowedChild, "s", "S"); assertEquals("S", FieldUtils.readField(privatelyShadowedChild, "s")); - assertIllegalArgumentException(() -> FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.TRUE)); - assertIllegalArgumentException(() -> FieldUtils.writeField(privatelyShadowedChild, "i", Integer.valueOf(1))); - assertIllegalArgumentException(() -> FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(1.0))); - assertNullPointerException(() -> FieldUtils.writeField((Object) null, "s", "s")); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, null, "s")); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, "", "s")); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, " ", "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.TRUE)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(privatelyShadowedChild, "i", Integer.valueOf(1))); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(1.0))); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField((Object) null, "s", "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, null, "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, "", "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, " ", "s")); } @Test @@ -821,6 +1060,7 @@ void testWriteNamedFieldForceAccess() throws Exception { assertEquals(Integer.valueOf(1), FieldUtils.readField(publicChild, "i", true)); FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0), true); assertEquals(Double.valueOf(1.0), FieldUtils.readField(publicChild, "d", true)); + FieldUtils.writeField(publiclyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readField(publiclyShadowedChild, "s", true)); FieldUtils.writeField(publiclyShadowedChild, "b", Boolean.FALSE, true); @@ -829,6 +1069,7 @@ void testWriteNamedFieldForceAccess() throws Exception { assertEquals(Integer.valueOf(0), FieldUtils.readField(publiclyShadowedChild, "i", true)); FieldUtils.writeField(publiclyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(publiclyShadowedChild, "d", true)); + FieldUtils.writeField(privatelyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readField(privatelyShadowedChild, "s", true)); FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.FALSE, true); @@ -837,27 +1078,59 @@ void testWriteNamedFieldForceAccess() throws Exception { assertEquals(Integer.valueOf(0), FieldUtils.readField(privatelyShadowedChild, "i", true)); FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(privatelyShadowedChild, "d", true)); - assertNullPointerException(() -> FieldUtils.writeField((Object) null, "s", "s", true)); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, null, "s", true)); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, "", "s", true)); - assertIllegalArgumentException(() -> FieldUtils.writeField(publicChild, " ", "s", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField((Object) null, "s", "s", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, null, "s", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, "", "s", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, " ", "s", true)); } @Test void testWriteNamedStaticField() throws Exception { FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePublic", "new"); assertEquals("new", StaticContainer.mutablePublic); - assertNullPointerException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutableProtected", "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePackage", "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(null, "IMMUTABLE_PRIVATE", "new")); - assertIllegalArgumentException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, null, "new")); - assertIllegalArgumentException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "", "new")); - assertIllegalArgumentException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, " ", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutableProtected", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePackage", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new")); + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "IMMUTABLE_PRIVATE", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, null, "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, " ", "new")); } @Test @@ -870,14 +1143,31 @@ void testWriteNamedStaticFieldForceAccess() throws Exception { assertEquals("new", StaticContainer.getMutablePackage()); FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new", true); assertEquals("new", StaticContainer.getMutablePrivate()); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new", true)); - assertNullPointerException(() -> FieldUtils.writeStaticField(null, "IMMUTABLE_PRIVATE", "new", true)); - assertIllegalArgumentException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, null, "new", true)); - assertIllegalArgumentException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, "", "new", true)); - assertIllegalArgumentException(() -> FieldUtils.writeStaticField(StaticContainerChild.class, " ", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "IMMUTABLE_PRIVATE", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, null, "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, " ", "new", true)); } @Test @@ -885,14 +1175,31 @@ void testWriteStaticField() throws Exception { final Field field = StaticContainer.class.getDeclaredField("mutablePublic"); FieldUtils.writeStaticField(field, "new"); assertEquals("new", StaticContainer.mutablePublic); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutableProtected"), "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutablePackage"), "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutablePrivate"), "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"), "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED"), "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE"), "new")); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new")); - assertNullPointerException(() -> FieldUtils.writeStaticField(null, "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutableProtected"), "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutablePackage"), "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("mutablePrivate"), "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"), "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED"), "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE"), "new")); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "new")); } @Test @@ -909,11 +1216,22 @@ void testWriteStaticFieldForceAccess() throws Exception { field = StaticContainer.class.getDeclaredField("mutablePrivate"); FieldUtils.writeStaticField(field, "new", true); assertEquals("new", StaticContainer.getMutablePrivate()); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"), "new", true)); - assertThrows(IllegalAccessException.class, + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"), "new", true)); + assertThrows( + IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PROTECTED"), "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE"), "new", true)); - assertThrows(IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new", true)); - assertNullPointerException(() -> FieldUtils.writeStaticField(null, "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PACKAGE"), "new", true)); + assertThrows( + IllegalAccessException.class, + () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "new", true)); } + } diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java index f90fb291817..0cd189237c9 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -29,7 +29,6 @@ import java.awt.Color; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.LinkOption; @@ -52,36 +51,23 @@ import org.apache.commons.lang3.reflect.testbed.GenericConsumer; import org.apache.commons.lang3.reflect.testbed.GenericParent; import org.apache.commons.lang3.reflect.testbed.PublicChild; -import org.apache.commons.lang3.reflect.testbed.PublicSubBeanOtherPackage; import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; /** - * {@link Tests MethodUtils}. + * Tests MethodUtils */ class MethodUtilsTest extends AbstractLangTest { - protected abstract static class AbstractGetMatchingMethod implements InterfaceGetMatchingMethod { public abstract void testMethod5(Exception exception); } - protected abstract static class AbstractGetMatchingMethod2 implements InterfaceGetMatchingMethod { - @Override - public void testMethod6() { } - } - - public static class ChildObject extends ParentObject implements PackagePrivateEmptyInterface { + interface ChildInterface { } - private static final class ConcreteGetMatchingMethod2 extends AbstractGetMatchingMethod2 { } - - private static final class ConcreteGetMatchingMethod22 extends AbstractGetMatchingMethod2 { - @Override - public void testMethod6() { } + public static class ChildObject extends ParentObject implements ChildInterface { } private static final class GetMatchingMethodClass { @@ -138,13 +124,13 @@ public void testOne(final Object obj) { public void testOne(final ParentObject obj) { } - public void testTwo(final GrandParentObject obj) { + public void testTwo(final ChildInterface obj) { } - public void testTwo(final Object obj) { + public void testTwo(final GrandParentObject obj) { } - public void testTwo(final PackagePrivateEmptyInterface obj) { + public void testTwo(final Object obj) { } } @@ -152,6 +138,7 @@ interface InterfaceGetMatchingMethod { default void testMethod6() { } } + private static final class MethodDescriptor { final Class declaringClass; final String name; @@ -164,24 +151,10 @@ private static final class MethodDescriptor { } } - interface PackagePrivateEmptyInterface { - // empty - } - public static class ParentObject extends GrandParentObject { - // empty - } - - private interface PrivateEmptyInterface { - // empty - } - - public static class PublicImpl1OfPackagePrivateEmptyInterface implements PackagePrivateEmptyInterface { - // empty } - public static class PublicImpl2OfPackagePrivateEmptyInterface implements PackagePrivateEmptyInterface { - // empty + private interface PrivateInterface { } public static class TestBean { @@ -203,7 +176,7 @@ public static String bar(final Integer i) { } public static String bar(final Integer i, final String... s) { - return "bar(Integer, String...)"; + return "bar(int, String...)"; } public static String bar(final long... s) { @@ -256,26 +229,6 @@ public static void oneParameterStatic(final String s) { // empty } - public static String staticInt(final int intArg) { - return "static int"; - } - - public static String staticIntIntVarArg(final int intArg, final int... args) { - return "static int, int..."; - } - - public static String staticIntLongVarArg(final int intArg, final long... args) { - return "static int, long..."; - } - - public static String staticIntStringVarArg(final int intArg, final String... args) { - return "static int, String..."; - } - - public static String staticPackagePrivateEmptyInterface(final PackagePrivateEmptyInterface... args) { - return "static PackagePrivateEmptyInterface..."; - } - public static String varOverload(final Boolean... args) { return "Boolean..."; } @@ -341,22 +294,6 @@ static void verify(final ImmutablePair a, final Object obj) { verify(a, pair); } - boolean unboxBooleanArray; - - boolean unboxByteArray; - - boolean unboxCharArray; - - boolean unboxDoubleArray; - - boolean unboxFloatArray; - - boolean unboxIntArray; - - boolean unboxLongArray; - - boolean unboxShortArray; - public String foo() { return "foo()"; } @@ -401,26 +338,10 @@ public String foo(final String... s) { return "foo(String...)"; } - public String intIntVarArg(final int intArg, final int... args) { - return "int, int..."; - } - - public String intLongVarArg(final int intArg, final long... args) { - return "int, long..."; - } - - public String intStringVarArg(final int intArg, final String... args) { - return "int, String..."; - } - public void oneParameter(final String s) { // empty } - public String packagePrivateEmptyInterface(final PackagePrivateEmptyInterface... args) { - return "PackagePrivateEmptyInterface..."; - } - @SuppressWarnings("unused") private String privateStringStuff() { return "privateStringStuff()"; @@ -455,43 +376,7 @@ private String privateStringStuff(final String s) { private void privateStuff() { } - public boolean[] unboxing(final boolean... values) { - unboxBooleanArray = true; - return values; - } - - public byte[] unboxing(final byte... values) { - unboxByteArray = true; - return values; - } - - public char[] unboxing(final char... values) { - unboxCharArray = true; - return values; - } - - public double[] unboxing(final double... values) { - unboxDoubleArray = true; - return values; - } - - public float[] unboxing(final float... values) { - unboxFloatArray = true; - return values; - } - public int[] unboxing(final int... values) { - unboxIntArray = true; - return values; - } - - public long[] unboxing(final long... values) { - unboxLongArray = true; - return values; - } - - public short[] unboxing(final short... values) { - unboxShortArray = true; return values; } @@ -508,16 +393,13 @@ public ImmutablePair varOverloadEcho(final String... args) { } - static class TestBeanSubclass extends TestBean { - } - - static class TestBeanWithInterfaces implements PrivateEmptyInterface { + static class TestBeanWithInterfaces implements PrivateInterface { public String foo() { return "foo()"; } } - private static class TestMutable implements Mutable { + private static final class TestMutable implements Mutable { @Override public Object getValue() { return null; @@ -528,18 +410,16 @@ public void setValue(final Object value) { } } - private static final class TestMutableSubclass extends TestMutable { - // empty - } + private TestBean testBean; private final Map, Class[]> classCache = new HashMap<>(); - private TestBean testBean; - - private void expectMatchingAccessibleMethodParameterTypes(final Class cls, final String methodName, final Class[] requestTypes, - final Class[] actualTypes) { - final Method m = MethodUtils.getMatchingAccessibleMethod(cls, methodName, requestTypes); - assertNotNull(m, "could not find any matches for " + methodName + " (" + (requestTypes == null ? null : toString(requestTypes)) + ")"); + private void expectMatchingAccessibleMethodParameterTypes(final Class cls, + final String methodName, final Class[] requestTypes, final Class[] actualTypes) { + final Method m = MethodUtils.getMatchingAccessibleMethod(cls, methodName, + requestTypes); + assertNotNull(m, "could not find any matches for " + methodName + + " (" + (requestTypes == null ? null : toString(requestTypes)) + ")"); assertArrayEquals(actualTypes, m.getParameterTypes(), toString(m.getParameterTypes()) + " not equals " + toString(actualTypes)); } @@ -567,43 +447,41 @@ void testConstructor() throws Exception { void testDistance() throws Exception { final Method distanceMethod = MethodUtils.getMatchingMethod(MethodUtils.class, "distance", Class[].class, Class[].class); distanceMethod.setAccessible(true); - assertEquals(-1, distanceMethod.invoke(null, new Class[] { String.class }, new Class[] { Date.class })); - assertEquals(0, distanceMethod.invoke(null, new Class[] { Date.class }, new Class[] { Date.class })); - assertEquals(1, distanceMethod.invoke(null, new Class[] { Integer.class }, new Class[] { ClassUtils.wrapperToPrimitive(Integer.class) })); - assertEquals(2, distanceMethod.invoke(null, new Class[] { Integer.class }, new Class[] { Object.class })); + + assertEquals(-1, distanceMethod.invoke(null, new Class[]{String.class}, new Class[]{Date.class})); + assertEquals(0, distanceMethod.invoke(null, new Class[]{Date.class}, new Class[]{Date.class})); + assertEquals(1, distanceMethod.invoke(null, new Class[]{Integer.class}, new Class[]{ClassUtils.wrapperToPrimitive(Integer.class)})); + assertEquals(2, distanceMethod.invoke(null, new Class[]{Integer.class}, new Class[]{Object.class})); + distanceMethod.setAccessible(false); } - @ParameterizedTest - @ValueSource(classes = {TestMutable.class, TestMutableSubclass.class}) - void testGetAccessibleInterfaceMethod(final Class clazz) throws Exception { + @Test + void testGetAccessibleInterfaceMethod() throws Exception { final Class[][] p = {ArrayUtils.EMPTY_CLASS_ARRAY, null}; for (final Class[] element : p) { - final Method method = clazz.getMethod("getValue", element); + final Method method = TestMutable.class.getMethod("getValue", element); final Method accessibleMethod = MethodUtils.getAccessibleMethod(method); assertNotSame(accessibleMethod, method); assertSame(Mutable.class, accessibleMethod.getDeclaringClass()); - final Method accessibleMethod2 = MethodUtils.getAccessibleMethod(clazz, method); - assertNotSame(accessibleMethod2, method); - assertSame(Mutable.class, accessibleMethod2.getDeclaringClass()); } } - @ParameterizedTest - @ValueSource(classes = {TestMutable.class, TestMutableSubclass.class}) - void testGetAccessibleInterfaceMethodFromDescription(final Class clazz) { - final Class[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null }; + @Test + void testGetAccessibleInterfaceMethodFromDescription() { + final Class[][] p = {ArrayUtils.EMPTY_CLASS_ARRAY, null}; for (final Class[] element : p) { - final Method accessibleMethod = MethodUtils.getAccessibleMethod(clazz, "getValue", element); + final Method accessibleMethod = MethodUtils.getAccessibleMethod( + TestMutable.class, "getValue", element); assertSame(Mutable.class, accessibleMethod.getDeclaringClass()); } } @Test void testGetAccessibleMethodInaccessible() throws Exception { - assertNull(MethodUtils.getAccessibleMethod(TestBean.class.getDeclaredMethod("privateStuff"))); - assertNull(MethodUtils.getAccessibleMethod(TestBean.class, TestBean.class.getDeclaredMethod("privateStuff"))); - assertNull(MethodUtils.getAccessibleMethod(TestBeanSubclass.class, TestBean.class.getDeclaredMethod("privateStuff"))); + final Method expected = TestBean.class.getDeclaredMethod("privateStuff"); + final Method actual = MethodUtils.getAccessibleMethod(expected); + assertNull(actual); } @Test @@ -614,87 +492,18 @@ void testGetAccessibleMethodPrivateInterface() throws Exception { assertNull(actual); } - @Test - void testGetAccessibleMethodPublicSub() throws Exception { - // PackageBean class is package-private - final int modifiers = PackageBean.class.getModifiers(); - assertFalse(Modifier.isPrivate(modifiers)); - assertFalse(Modifier.isProtected(modifiers)); - assertFalse(Modifier.isPublic(modifiers)); - // make sure that bean does what it should: compile - new PublicSubBean().setBar(""); - // make sure that bean does what it should - final PublicSubBean bean = new PublicSubBean(); - assertEquals(bean.getFoo(), "This is foo", "Start value (foo)"); - assertEquals(bean.getBar(), "This is bar", "Start value (bar)"); - bean.setFoo("new foo"); - bean.setBar("new bar"); - assertEquals(bean.getFoo(), "new foo", "Set value (foo)"); - assertEquals(bean.getBar(), "new bar", "Set value (bar)"); - // see if we can access public methods in a default access superclass - // from a public access subclass instance - MethodUtils.invokeExactMethod(bean, "setFoo", "alpha"); - assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)"); - MethodUtils.invokeExactMethod(bean, "setBar", "beta"); - assertEquals(bean.getBar(), "beta", "Set value (bar:2)"); - // PublicSubBean.setFoo(String) - Method method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setFoo", String.class); - assertNotNull(method, "getAccessibleMethod() setFoo is Null"); - method.invoke(bean, "1111"); - assertEquals("1111", bean.getFoo(), "Set value (foo:3)"); - // PublicSubBean.setBar(String) - method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setBar", String.class); - assertNotNull(method, "getAccessibleMethod() setBar is Null"); - method.invoke(bean, "2222"); - assertEquals("2222", bean.getBar(), "Set value (bar:3)"); - } - - @Test - void testGetAccessibleMethodPublicSubOtherPackage() throws Exception { - // PackageBeanOtherPackage class is package-private - final int modifiers = Class.forName("org.apache.commons.lang3.reflect.testbed.PackageBeanOtherPackage").getModifiers(); - assertFalse(Modifier.isPrivate(modifiers)); - assertFalse(Modifier.isProtected(modifiers)); - assertFalse(Modifier.isPublic(modifiers)); - // make sure that bean does what it should: compile - new PublicSubBeanOtherPackage().setBar(""); - // make sure that bean does what it should - final PublicSubBeanOtherPackage bean = new PublicSubBeanOtherPackage(); - assertEquals(bean.getFoo(), "This is foo", "Start value (foo)"); - assertEquals(bean.getBar(), "This is bar", "Start value (bar)"); - bean.setFoo("new foo"); - bean.setBar("new bar"); - assertEquals(bean.getFoo(), "new foo", "Set value (foo)"); - assertEquals(bean.getBar(), "new bar", "Set value (bar)"); - // see if we can access public methods in a default access superclass - // from a public access subclass instance - MethodUtils.invokeExactMethod(bean, "setFoo", "alpha"); - assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)"); - MethodUtils.invokeExactMethod(bean, "setBar", "beta"); - assertEquals(bean.getBar(), "beta", "Set value (bar:2)"); - // PublicSubBean.setFoo(String) - Method method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setFoo", String.class); - assertNotNull(method, "getAccessibleMethod() setFoo is Null"); - method.invoke(bean, "1111"); - assertEquals("1111", bean.getFoo(), "Set value (foo:3)"); - // PublicSubBean.setBar(String) - method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setBar", String.class); - assertNotNull(method, "getAccessibleMethod() setBar is Null"); - method.invoke(bean, "2222"); - assertEquals("2222", bean.getBar(), "Set value (bar:3)"); - } - @Test void testGetAccessiblePublicMethod() throws Exception { - assertSame(MutableObject.class, - MethodUtils.getAccessibleMethod(MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass()); - assertSame(MutableObject.class, MethodUtils - .getAccessibleMethod(MutableObject.class, MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass()); + assertSame(MutableObject.class, MethodUtils.getAccessibleMethod( + MutableObject.class.getMethod("getValue", + ArrayUtils.EMPTY_CLASS_ARRAY)).getDeclaringClass()); } @Test void testGetAccessiblePublicMethodFromDescription() { - assertSame(MutableObject.class, MethodUtils.getAccessibleMethod(MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY).getDeclaringClass()); + assertSame(MutableObject.class, MethodUtils.getAccessibleMethod( + MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY) + .getDeclaringClass()); } @Test @@ -816,40 +625,51 @@ void testGetMatchingAccessibleMethod() { expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testOne", singletonArray(ChildObject.class), singletonArray(ParentObject.class)); expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo", singletonArray(ParentObject.class), singletonArray(GrandParentObject.class)); - expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo", singletonArray(ChildObject.class), singletonArray(PackagePrivateEmptyInterface.class)); + expectMatchingAccessibleMethodParameterTypes(InheritanceBean.class, "testTwo", singletonArray(ChildObject.class), singletonArray(ChildInterface.class)); // LANG-1757 expectMatchingAccessibleMethodParameterTypes(Files.class, "exists", singletonArray(Path.class), new Class[] { Path.class, LinkOption[].class }); } @Test void testGetMatchingMethod() throws NoSuchMethodException { - assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod"), GetMatchingMethodClass.class.getMethod("testMethod")); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod"), + GetMatchingMethodClass.class.getMethod("testMethod")); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod", Long.TYPE), GetMatchingMethodClass.class.getMethod("testMethod", Long.TYPE)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod", Long.class), GetMatchingMethodClass.class.getMethod("testMethod", Long.class)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod", (Class) null), GetMatchingMethodClass.class.getMethod("testMethod", Long.class)); - assertThrows(IllegalStateException.class, () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod2", (Class) null)); + + assertThrows(IllegalStateException.class, + () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod2", (Class) null)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.TYPE, Long.class), GetMatchingMethodClass.class.getMethod("testMethod3", Long.TYPE, Long.class)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.class, Long.TYPE), GetMatchingMethodClass.class.getMethod("testMethod3", Long.class, Long.TYPE)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", null, Long.TYPE), GetMatchingMethodClass.class.getMethod("testMethod3", Long.class, Long.TYPE)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod3", Long.TYPE, null), GetMatchingMethodClass.class.getMethod("testMethod3", Long.TYPE, Long.class)); - assertThrows(IllegalStateException.class, () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod4", null, null)); + + assertThrows(IllegalStateException.class, + () -> MethodUtils.getMatchingMethod(GetMatchingMethodClass.class, "testMethod4", null, null)); + assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod5", RuntimeException.class), GetMatchingMethodImpl.class.getMethod("testMethod5", Exception.class)); - assertEquals(GetMatchingMethodImpl.class.getMethod("testMethod6"), MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod6")); - assertNullPointerException(() -> MethodUtils.getMatchingMethod(null, "testMethod5", RuntimeException.class)); - Method testMethod6 = MethodUtils.getMatchingMethod(ConcreteGetMatchingMethod2.class, "testMethod6"); - assertNotNull(testMethod6); - assertEquals(AbstractGetMatchingMethod2.class, testMethod6.getDeclaringClass()); - testMethod6 = MethodUtils.getMatchingMethod(ConcreteGetMatchingMethod22.class, "testMethod6"); - assertNotNull(testMethod6); - assertEquals(ConcreteGetMatchingMethod22.class, testMethod6.getDeclaringClass()); + + assertEquals(GetMatchingMethodImpl.class.getMethod("testMethod6"), + MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod6")); + + assertNullPointerException( + () -> MethodUtils.getMatchingMethod(null, "testMethod5", RuntimeException.class)); } @Test @@ -857,19 +677,8 @@ void testGetMethodObject() throws Exception { assertEquals(MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY), MethodUtils.getMethodObject(MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY)); assertNull(MethodUtils.getMethodObject(MutableObject.class, "does not exist, at all", ArrayUtils.EMPTY_CLASS_ARRAY)); - assertNull(MethodUtils.getMethodObject(null, "does not exist, at all", ArrayUtils.EMPTY_CLASS_ARRAY)); - assertNull(MethodUtils.getMethodObject(null, null, ArrayUtils.EMPTY_CLASS_ARRAY)); - assertNull(MethodUtils.getMethodObject(MutableObject.class, null, ArrayUtils.EMPTY_CLASS_ARRAY)); - // 0 args - assertNull(MethodUtils.getMethodObject(MutableObject.class, "getValue", new Class[] { null })); - // 1 args - assertNull(MethodUtils.getMethodObject(MutableObject.class, "equals", new Class[] { null })); - assertNull(MethodUtils.getMethodObject(MutableObject.class, "equals", new Class[] { String.class, null, String.class })); } - /** - * Tests a {@code public} method. - */ @Test @Annotated public void testGetMethodsListWithAnnotation() throws NoSuchMethodException { @@ -924,17 +733,24 @@ void testGetMethodsWithAnnotationIllegalArgumentException3() { @Test void testGetMethodsWithAnnotationNotSearchSupersAndNotIgnoreAccess() { - assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, false, false)); - final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, false, false); + assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, + false, false)); + + final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, + false, false); assertEquals(1, methodsWithAnnotation.length); assertEquals("PublicChild.publicAnnotatedMethod", - methodsWithAnnotation[0].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[0].getName()); + methodsWithAnnotation[0].getDeclaringClass().getSimpleName() + '.' + + methodsWithAnnotation[0].getName()); } @Test void testGetMethodsWithAnnotationNotSearchSupersButIgnoreAccess() { - assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, false, true)); - final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, false, true); + assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, + false, true)); + + final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, + false, true); assertEquals(2, methodsWithAnnotation.length); assertEquals("PublicChild", methodsWithAnnotation[0].getDeclaringClass().getSimpleName()); assertEquals("PublicChild", methodsWithAnnotation[1].getDeclaringClass().getSimpleName()); @@ -944,26 +760,38 @@ void testGetMethodsWithAnnotationNotSearchSupersButIgnoreAccess() { @Test void testGetMethodsWithAnnotationSearchSupersAndIgnoreAccess() { - assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, true, true)); - final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, true, true); + assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, + true, true)); + + final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, + true, true); assertEquals(4, methodsWithAnnotation.length); assertEquals("PublicChild", methodsWithAnnotation[0].getDeclaringClass().getSimpleName()); assertEquals("PublicChild", methodsWithAnnotation[1].getDeclaringClass().getSimpleName()); assertTrue(methodsWithAnnotation[0].getName().endsWith("AnnotatedMethod")); assertTrue(methodsWithAnnotation[1].getName().endsWith("AnnotatedMethod")); - assertEquals("Foo.doIt", methodsWithAnnotation[2].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[2].getName()); + assertEquals("Foo.doIt", + methodsWithAnnotation[2].getDeclaringClass().getSimpleName() + '.' + + methodsWithAnnotation[2].getName()); assertEquals("Parent.parentProtectedAnnotatedMethod", - methodsWithAnnotation[3].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[3].getName()); + methodsWithAnnotation[3].getDeclaringClass().getSimpleName() + '.' + + methodsWithAnnotation[3].getName()); } @Test void testGetMethodsWithAnnotationSearchSupersButNotIgnoreAccess() { - assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, true, false)); - final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, true, false); + assertArrayEquals(new Method[0], MethodUtils.getMethodsWithAnnotation(Object.class, Annotated.class, + true, false)); + + final Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(PublicChild.class, Annotated.class, + true, false); assertEquals(2, methodsWithAnnotation.length); assertEquals("PublicChild.publicAnnotatedMethod", - methodsWithAnnotation[0].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[0].getName()); - assertEquals("Foo.doIt", methodsWithAnnotation[1].getDeclaringClass().getSimpleName() + '.' + methodsWithAnnotation[1].getName()); + methodsWithAnnotation[0].getDeclaringClass().getSimpleName() + '.' + + methodsWithAnnotation[0].getName()); + assertEquals("Foo.doIt", + methodsWithAnnotation[1].getDeclaringClass().getSimpleName() + '.' + + methodsWithAnnotation[1].getName()); } @Test @@ -1009,126 +837,187 @@ void testGetOverrideHierarchyIncludingInterfaces() { @Test void testInvokeExactMethod() throws Exception { - assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); + assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", + (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo")); - assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", (Object[]) null)); - assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", null, null)); - assertEquals("foo(String)", MethodUtils.invokeExactMethod(testBean, "foo", "")); - assertEquals("foo(Object)", MethodUtils.invokeExactMethod(testBean, "foo", new Object())); - assertEquals("foo(Integer)", MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.INTEGER_ONE)); - assertEquals("foo(double)", MethodUtils.invokeExactMethod(testBean, "foo", new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE })); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.BYTE_ONE)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE)); + assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", + (Object[]) null)); + assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo", + null, null)); + assertEquals("foo(String)", MethodUtils.invokeExactMethod(testBean, + "foo", "")); + assertEquals("foo(Object)", MethodUtils.invokeExactMethod(testBean, + "foo", new Object())); + assertEquals("foo(Integer)", MethodUtils.invokeExactMethod(testBean, + "foo", NumberUtils.INTEGER_ONE)); + assertEquals("foo(double)", MethodUtils.invokeExactMethod(testBean, + "foo", new Object[]{NumberUtils.DOUBLE_ONE}, + new Class[]{Double.TYPE})); + + assertThrows( + NoSuchMethodException.class, + () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.BYTE_ONE)); + + assertThrows( + NoSuchMethodException.class, + () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE)); assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", Boolean.TRUE)); - assertThrows(NullPointerException.class, () -> MethodUtils.invokeExactMethod(null, "foo", NumberUtils.BYTE_ONE)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, null, NumberUtils.BYTE_ONE)); - assertThrows(NullPointerException.class, - () -> MethodUtils.invokeExactMethod(null, "foo", new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE })); - assertThrows(NoSuchMethodException.class, - () -> MethodUtils.invokeExactMethod(testBean, null, new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE })); + + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(null, "foo", NumberUtils.BYTE_ONE)); + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(testBean, null, NumberUtils.BYTE_ONE)); + + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(null, "foo", new Object[]{NumberUtils.DOUBLE_ONE}, + new Class[]{Double.TYPE})); + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(testBean, null, new Object[]{NumberUtils.DOUBLE_ONE}, + new Class[]{Double.TYPE})); } @Test void testInvokeExactStaticMethod() throws Exception { - assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); - assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", (Object[]) null)); - assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", null, null)); - assertEquals("bar(String)", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", "")); - assertEquals("bar(Object)", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", new Object())); - assertEquals("bar(Integer)", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.INTEGER_ONE)); - assertEquals("bar(double)", - MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", new Object[] { NumberUtils.DOUBLE_ONE }, new Class[] { Double.TYPE })); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.BYTE_ONE)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.LONG_ONE)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", Boolean.TRUE)); + assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, + "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); + assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, + "bar", (Object[]) null)); + assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, + "bar", null, null)); + assertEquals("bar(String)", MethodUtils.invokeExactStaticMethod( + TestBean.class, "bar", "")); + assertEquals("bar(Object)", MethodUtils.invokeExactStaticMethod( + TestBean.class, "bar", new Object())); + assertEquals("bar(Integer)", MethodUtils.invokeExactStaticMethod( + TestBean.class, "bar", NumberUtils.INTEGER_ONE)); + assertEquals("bar(double)", MethodUtils.invokeExactStaticMethod( + TestBean.class, "bar", new Object[]{NumberUtils.DOUBLE_ONE}, + new Class[]{Double.TYPE})); + + assertThrows( + NoSuchMethodException.class, + () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.BYTE_ONE)); + assertThrows( + NoSuchMethodException.class, + () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", NumberUtils.LONG_ONE)); + assertThrows( + NoSuchMethodException.class, + () -> MethodUtils.invokeExactStaticMethod(TestBean.class, "bar", Boolean.TRUE)); } @Test - void testInvokeJavaVarArgsOverloadingResolution() throws Exception { - // Primitive wrappers - assertEquals("Byte...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (byte) 1, (byte) 2)); - assertEquals("Short...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (short) 1, (short) 2)); - assertEquals("Integer...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 2)); - assertEquals("Long...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1L, 2L)); - assertEquals("Float...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1f, 2f)); - assertEquals("Double...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1d, 2d)); - assertEquals("Boolean...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", true, false)); - // Number - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 1.1)); - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 1L)); - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1d, 1f)); - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (short) 1, (byte) 1)); - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", ArrayUtils.EMPTY_OBJECT_ARRAY)); - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); - assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", (Object[]) ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY)); - // Object - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, "s")); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, true)); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1.1, true)); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 'c', true)); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 1, 'c')); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 'c', "s")); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", ArrayUtils.EMPTY_OBJECT_ARRAY)); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", (Object[]) ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY)); - assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload")); - // Other - assertEquals("String...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", "a", "b")); - assertEquals("Character...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", 'a', 'b')); + void testInvokeJavaVarargsOverloadingResolution() throws Exception { + assertEquals("Byte...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", (byte) 1, (byte) 2)); + assertEquals("Short...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", (short) 1, (short) 2)); + assertEquals("Integer...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1, 2)); + assertEquals("Long...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1L, 2L)); + assertEquals("Float...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1f, 2f)); + assertEquals("Double...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1d, 2d)); + assertEquals("Character...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 'a', 'b')); + assertEquals("String...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", "a", "b")); + assertEquals("Boolean...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", true, false)); + + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1, "s")); + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1, true)); + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1.1, true)); + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 'c', true)); + assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1, 1.1)); + assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1, 1L)); + assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1d, 1f)); + assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", (short) 1, (byte) 1)); + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 1, 'c')); + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, + "varOverload", 'c', "s")); + + assertEquals("Object...", MethodUtils.invokeStaticMethod(TestBean.class, "varOverload", + (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); + assertEquals("Number...", MethodUtils.invokeStaticMethod(TestBean.class, "numOverload", + (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); } @Test void testInvokeMethod() throws Exception { - assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); + assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", + (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo")); - assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", (Object[]) null)); - assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", null, null)); - assertEquals("foo(String)", MethodUtils.invokeMethod(testBean, "foo", "")); - assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", new Object())); - assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", Boolean.TRUE)); - assertEquals("foo(Integer)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.INTEGER_ONE)); - assertEquals("foo(int)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.BYTE_ONE)); - assertEquals("foo(long)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.LONG_ONE)); - assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.DOUBLE_ONE)); - assertEquals("foo(String...)", MethodUtils.invokeMethod(testBean, "foo", "a", "b", "c")); - assertEquals("foo(String...)", MethodUtils.invokeMethod(testBean, "foo", "a", "b", "c")); - assertEquals("foo(int, String...)", MethodUtils.invokeMethod(testBean, "foo", 5, "a", "b", "c")); - assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", 1L, 2L)); - assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", 1, 2)); - assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", (byte) 1, (byte) 2)); // widen - assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", (short) 1, (short) 2)); // widen - assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", (char) 1, (char) 2)); // widen - TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y")); - TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42)); - TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y")); - TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }), MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42)); + assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", + (Object[]) null)); + assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", + null, null)); + assertEquals("foo(String)", MethodUtils.invokeMethod(testBean, "foo", + "")); + assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", + new Object())); + assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", + Boolean.TRUE)); + assertEquals("foo(Integer)", MethodUtils.invokeMethod(testBean, "foo", + NumberUtils.INTEGER_ONE)); + assertEquals("foo(int)", MethodUtils.invokeMethod(testBean, "foo", + NumberUtils.BYTE_ONE)); + assertEquals("foo(long)", MethodUtils.invokeMethod(testBean, "foo", + NumberUtils.LONG_ONE)); + assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo", + NumberUtils.DOUBLE_ONE)); + assertEquals("foo(String...)", MethodUtils.invokeMethod(testBean, "foo", + "a", "b", "c")); + assertEquals("foo(String...)", MethodUtils.invokeMethod(testBean, "foo", + "a", "b", "c")); + assertEquals("foo(int, String...)", MethodUtils.invokeMethod(testBean, "foo", + 5, "a", "b", "c")); + assertEquals("foo(long...)", MethodUtils.invokeMethod(testBean, "foo", + 1L, 2L)); + + assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "foo", 1, 2)); + + TestBean.verify(new ImmutablePair<>("String...", new String[]{"x", "y"}), + MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y")); + TestBean.verify(new ImmutablePair<>("Number...", new Number[]{17, 23, 42}), + MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42)); + TestBean.verify(new ImmutablePair<>("String...", new String[]{"x", "y"}), + MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y")); + TestBean.verify(new ImmutablePair<>("Number...", new Number[]{17, 23, 42}), + MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42)); + assertNullPointerException(() -> MethodUtils.invokeMethod(null, "foo", 1, 2)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, null, 1, 2)); + assertNullPointerException(() -> MethodUtils.invokeMethod(testBean, null, 1, 2)); } @Test - void testInvokeMethod_VarArgsWithNullValues() throws Exception { - assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", "a", null, "c")); - assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", "a", "b", null)); + void testInvokeMethod_VarArgsNotUniqueResolvable() throws Exception { + assertEquals("Boolean...", MethodUtils.invokeMethod(testBean, "varOverload", + new Object[] {null})); + assertEquals("Object...", MethodUtils.invokeMethod(testBean, "varOverload", + (Object[]) null)); } @Test - void testInvokeMethod1PlusVarArgs() throws Exception { - // intStringVarArg - assertEquals("int, String...", MethodUtils.invokeMethod(testBean, "intStringVarArg", 1)); - assertEquals("int, String...", MethodUtils.invokeMethod(testBean, "intStringVarArg", 1, "s")); - assertEquals("int, String...", MethodUtils.invokeMethod(testBean, "intStringVarArg", 1, "s1", "s2")); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intStringVarArg", 1, "s1", 5)); - // intLongVarArg - assertEquals("int, long...", MethodUtils.invokeMethod(testBean, "intLongVarArg", 1)); - assertEquals("int, long...", MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, 2L)); - assertEquals("int, long...", MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, 2L, 3L)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, "s1", 5)); - // intIntVarArg - assertEquals("int, int...", MethodUtils.invokeMethod(testBean, "intIntVarArg", 1)); - assertEquals("int, int...", MethodUtils.invokeMethod(testBean, "intIntVarArg", 1, 2)); - assertEquals("int, int...", MethodUtils.invokeMethod(testBean, "intIntVarArg", 1, 2, 3)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "intLongVarArg", 1, "s1", 5)); + void testInvokeMethod_VarArgsWithNullValues() throws Exception { + assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", + "a", null, "c")); + assertEquals("String...", MethodUtils.invokeMethod(testBean, "varOverload", + "a", "b", null)); } @Test @@ -1142,167 +1031,72 @@ void testInvokeMethodForceAccessWithArgs() throws Exception { assertEquals("privateStringStuff(double)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", 5.0d)); assertEquals("privateStringStuff(String)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", "Hi There")); assertEquals("privateStringStuff(Object)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", new Date())); - assertNullPointerException(() -> MethodUtils.invokeMethod(null, true, "privateStringStuff", "Hi There")); - assertNullPointerException(() -> MethodUtils.invokeMethod(testBean, true, null, "Hi There")); - } - - @Test - void testInvokeMethodVarArgsNotUniqueResolvable() throws Exception { - assertEquals("Boolean...", MethodUtils.invokeMethod(testBean, "varOverload", new Object[] { null })); - assertEquals("Object...", MethodUtils.invokeMethod(testBean, "varOverload", (Object[]) null)); - } - - @Test - void testInvokeMethodVarArgsOfInterface() throws Exception { - // packagePrivateEmptyInterface - assertEquals("PackagePrivateEmptyInterface...", MethodUtils.invokeMethod(testBean, "packagePrivateEmptyInterface", - new PublicImpl1OfPackagePrivateEmptyInterface(), new PublicImpl2OfPackagePrivateEmptyInterface())); - assertEquals("PackagePrivateEmptyInterface...", MethodUtils.invokeMethod(testBean, "packagePrivateEmptyInterface", new PackagePrivateEmptyInterface() { - // empty - }, new PackagePrivateEmptyInterface() { - // empty - })); - } - - @Test - void testInvokeMethodVarArgsUnboxingBooleanArray() throws Exception { - final TestBean testBean = new TestBean(); - final boolean[] actual = (boolean[]) MethodUtils.invokeMethod(testBean, "unboxing", Boolean.TRUE, Boolean.FALSE); - assertArrayEquals(new boolean[] { true, false }, actual); - assertTrue(testBean.unboxBooleanArray); - } - @Test - void testInvokeMethodVarArgsUnboxingByteArray() throws Exception { - final TestBean testBean = new TestBean(); - final byte[] actual = (byte[]) MethodUtils.invokeMethod(testBean, "unboxing", Byte.valueOf((byte) 1), Byte.valueOf((byte) 2)); - assertArrayEquals(new byte[] { 1, 2 }, actual); - assertTrue(testBean.unboxByteArray); - } - - @Test - void testInvokeMethodVarArgsUnboxingCharArray() throws Exception { - final TestBean testBean = new TestBean(); - final char[] actual = (char[]) MethodUtils.invokeMethod(testBean, "unboxing", Character.valueOf((char) 1), Character.valueOf((char) 2)); - assertArrayEquals(new char[] { 1, 2 }, actual); - assertTrue(testBean.unboxCharArray); - } - - @Test - void testInvokeMethodVarArgsUnboxingDoubleArray() throws Exception { - final TestBean testBean = new TestBean(); - final double[] actual = (double[]) MethodUtils.invokeMethod(testBean, "unboxing", Double.valueOf(1), Double.valueOf(2)); - assertArrayEquals(new double[] { 1, 2 }, actual); - assertTrue(testBean.unboxDoubleArray); - } - - @Test - void testInvokeMethodVarArgsUnboxingFloatArray() throws Exception { - final TestBean testBean = new TestBean(); - final float[] actual = (float[]) MethodUtils.invokeMethod(testBean, "unboxing", Float.valueOf(1), Float.valueOf(2)); - assertArrayEquals(new float[] { 1, 2 }, actual); - assertTrue(testBean.unboxFloatArray); - } - - @Test - void testInvokeMethodVarArgsUnboxingIntArray() throws Exception { - final TestBean testBean = new TestBean(); - final int[] actual = (int[]) MethodUtils.invokeMethod(testBean, "unboxing", Integer.valueOf(1), Integer.valueOf(2)); - assertArrayEquals(new int[] { 1, 2 }, actual); - assertTrue(testBean.unboxIntArray); - } - - @Test - void testInvokeMethodVarArgsUnboxingLongArray() throws Exception { - final TestBean testBean = new TestBean(); - final long[] actual = (long[]) MethodUtils.invokeMethod(testBean, "unboxing", Long.valueOf(1), Long.valueOf(2)); - assertArrayEquals(new long[] { 1, 2 }, actual); - assertTrue(testBean.unboxLongArray); - } - - @Test - void testInvokeMethodVarArgsUnboxingShortArray() throws Exception { - final TestBean testBean = new TestBean(); - final short[] actual = (short[]) MethodUtils.invokeMethod(testBean, "unboxing", Short.valueOf((short) 1), Short.valueOf((short) 2)); - assertArrayEquals(new short[] { 1, 2 }, actual); - assertTrue(testBean.unboxShortArray); + assertNullPointerException( + () -> MethodUtils.invokeMethod(null, true, "privateStringStuff", "Hi There")); + assertNullPointerException( + () -> MethodUtils.invokeMethod(testBean, true, null, "Hi There")); } @Test void testInvokeStaticMethod() throws Exception { - assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar")); - assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); - assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (Object[]) null)); - assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar", null, null)); - assertEquals("bar(String)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", "")); - assertEquals("bar(Object)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", new Object())); - assertEquals("bar(Object)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", Boolean.TRUE)); - assertEquals("bar(Integer)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.INTEGER_ONE)); - assertEquals("bar(int)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.BYTE_ONE)); - assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", NumberUtils.BYTE_ONE)); - assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", NumberUtils.SHORT_ONE)); - assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", NumberUtils.INTEGER_ONE)); - assertEquals("static int", MethodUtils.invokeStaticMethod(TestBean.class, "staticInt", 'a')); - assertEquals("bar(double)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.DOUBLE_ONE)); - assertEquals("bar(String...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", "a", "b")); - assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", 1L, 2L)); - assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (byte) 1, (byte) 2)); // widen - assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", (short) 1, (short) 2)); // widen - assertEquals("bar(long...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", 1, 2)); // widen - assertEquals("bar(Integer, String...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.INTEGER_ONE, "a", "b")); - // You cannot widen a Short to an Integer in Java source, but you can a short to an int but this API declares an Integer, not an int. - assertThrows(NoSuchMethodException.class, - () -> assertEquals("bar(Integer, String...)", MethodUtils.invokeStaticMethod(TestBean.class, "bar", NumberUtils.SHORT_ONE, "a", "b"))); // widen - TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }), + assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, + "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY)); + assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, + "bar", (Object[]) null)); + assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, + "bar", null, null)); + assertEquals("bar(String)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", "")); + assertEquals("bar(Object)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", new Object())); + assertEquals("bar(Object)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", Boolean.TRUE)); + assertEquals("bar(Integer)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", NumberUtils.INTEGER_ONE)); + assertEquals("bar(int)", MethodUtils.invokeStaticMethod(TestBean.class, + "bar", NumberUtils.BYTE_ONE)); + assertEquals("bar(double)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", NumberUtils.DOUBLE_ONE)); + assertEquals("bar(String...)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", "a", "b")); + assertEquals("bar(long...)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", 1L, 2L)); + assertEquals("bar(int, String...)", MethodUtils.invokeStaticMethod( + TestBean.class, "bar", NumberUtils.INTEGER_ONE, "a", "b")); + + TestBean.verify(new ImmutablePair<>("String...", new String[]{"x", "y"}), MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", "x", "y")); - TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }), + TestBean.verify(new ImmutablePair<>("Number...", new Number[]{17, 23, 42}), MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", 17, 23, 42)); - TestBean.verify(new ImmutablePair<>("String...", new String[] { "x", "y" }), + TestBean.verify(new ImmutablePair<>("String...", new String[]{"x", "y"}), MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", "x", "y")); - TestBean.verify(new ImmutablePair<>("Number...", new Number[] { 17, 23, 42 }), + TestBean.verify(new ImmutablePair<>("Number...", new Number[]{17, 23, 42}), MethodUtils.invokeStaticMethod(TestBean.class, "varOverloadEchoStatic", 17, 23, 42)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "does_not_exist")); + + assertThrows( + NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "does_not_exist")); } @Test - void testInvokeStaticMethod1PlusVarArgs() throws Exception { - // staticIntStringVarArg - assertEquals("static int, String...", MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1)); - assertEquals("static int, String...", MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1, "s")); - assertEquals("static int, String...", MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1, "s1", "s2")); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeStaticMethod(TestBean.class, "staticIntStringVarArg", 1, "s1", 5)); - // staticIntLongVarArg - assertEquals("static int, long...", MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1)); - assertEquals("static int, long...", MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1, 2L)); - assertEquals("static int, long...", MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1, 2L, 3L)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "staticIntLongVarArg", 1, "s1", 5)); - // staticIntIntVarArg - assertEquals("static int, int...", MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1)); - assertEquals("static int, int...", MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, 2)); - assertEquals("static int, int...", MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, 2, 3)); - assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(testBean, "staticIntIntVarArg", 1, "s1", 5)); + void testNullArgument() { + expectMatchingAccessibleMethodParameterTypes(TestBean.class, "oneParameter", + singletonArray(null), singletonArray(String.class)); } @Test - void testInvokeStaticMethodVarArgsOfInterface() throws Exception { - // staticPackagePrivateEmptyInterface - assertEquals("static PackagePrivateEmptyInterface...", MethodUtils.invokeStaticMethod(TestBean.class, "staticPackagePrivateEmptyInterface", - new PublicImpl1OfPackagePrivateEmptyInterface(), new PublicImpl2OfPackagePrivateEmptyInterface())); - assertEquals("static PackagePrivateEmptyInterface...", - MethodUtils.invokeStaticMethod(TestBean.class, "staticPackagePrivateEmptyInterface", new PackagePrivateEmptyInterface() { - // empty - }, new PackagePrivateEmptyInterface() { - // empty - })); + void testVarArgsUnboxing() throws Exception { + final TestBean testBean = new TestBean(); + final int[] actual = (int[]) MethodUtils.invokeMethod(testBean, "unboxing", Integer.valueOf(1), Integer.valueOf(2)); + assertArrayEquals(new int[]{1, 2}, actual); } - @Test - void testNullArgument() { - expectMatchingAccessibleMethodParameterTypes(TestBean.class, "oneParameter", singletonArray(null), singletonArray(String.class)); + private String toString(final Class[] c) { + return Arrays.asList(c).toString(); } @Test - void testVarargsOverloadingResolution() { + public void verifyJavaVarargsOverloadingResolution() { // This code is not a test of MethodUtils. // Rather it makes explicit the behavior of the Java specification for // various cases of overload resolution. @@ -1315,6 +1109,7 @@ void testVarargsOverloadingResolution() { assertEquals("Character...", TestBean.varOverload('a', 'b')); assertEquals("String...", TestBean.varOverload("a", "b")); assertEquals("Boolean...", TestBean.varOverload(true, false)); + assertEquals("Object...", TestBean.varOverload(1, "s")); assertEquals("Object...", TestBean.varOverload(1, true)); assertEquals("Object...", TestBean.varOverload(1.1, true)); @@ -1326,8 +1121,4 @@ void testVarargsOverloadingResolution() { assertEquals("Object...", TestBean.varOverload(1, 'c')); assertEquals("Object...", TestBean.varOverload('c', "s")); } - - private String toString(final Class[] c) { - return Arrays.asList(c).toString(); - } } diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java index e9a6709d3e0..30d28951117 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.reflect; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -24,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.awt.Insets; @@ -1073,7 +1073,7 @@ void testToLongString() { @Test void testToString() { assertNullPointerException(() -> TypeUtils.toString(null)); - assertIllegalArgumentException(() -> TypeUtils.toString(new Type() { + assertThrows(IllegalArgumentException.class, () -> TypeUtils.toString(new Type() { // empty })); } diff --git a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java index f790a55e0a6..4069fbef123 100644 --- a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java +++ b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.stream; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -85,7 +84,7 @@ public Stream simpleStreamFilterFailing() { dynamicTest("IllegalArgumentException", () -> { final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5); final Executable testMethod = () -> Failable.stream(input).map(Integer::valueOf).filter(asIntPredicate(iae)).collect(Collectors.toList()); - final IllegalArgumentException thrown = assertIllegalArgumentException(testMethod); + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod); assertEquals("Invalid argument: " + 5, thrown.getMessage()); }), dynamicTest("OutOfMemoryError", () -> { @@ -110,7 +109,7 @@ public Stream simpleStreamForEachFailing() { dynamicTest("IllegalArgumentException", () -> { final IllegalArgumentException ise = new IllegalArgumentException(); final Executable testMethod = () -> Failable.stream(input).forEach(asIntConsumer(ise)); - final IllegalArgumentException thrown = assertIllegalArgumentException(testMethod); + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod); assertNull(thrown.getMessage()); }), dynamicTest("OutOfMemoryError", () -> { diff --git a/src/test/java/org/apache/commons/lang3/text/FormattableUtilsTest.java b/src/test/java/org/apache/commons/lang3/text/FormattableUtilsTest.java index 20aff0d1114..59c1d58f4e6 100644 --- a/src/test/java/org/apache/commons/lang3/text/FormattableUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/text/FormattableUtilsTest.java @@ -16,8 +16,8 @@ */ package org.apache.commons.lang3.text; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.FormattableFlags; import java.util.Formatter; @@ -112,7 +112,7 @@ void testEllipsis() { @Test void testIllegalEllipsis() { - assertIllegalArgumentException(() -> FormattableUtils.append("foo", new Formatter(), 0, -1, 1, "xx")); + assertThrows(IllegalArgumentException.class, () -> FormattableUtils.append("foo", new Formatter(), 0, -1, 1, "xx")); } } diff --git a/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java b/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java index 0fabd264fbf..e1641bd2d72 100644 --- a/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java +++ b/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java @@ -17,8 +17,8 @@ package org.apache.commons.lang3.text; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.text.DecimalFormatSymbols; import java.util.Arrays; @@ -78,25 +78,46 @@ void testAppend_CharArray_int_int() { StrBuilder sb = new StrBuilder(); sb.setNullText("NULL").append((char[]) null, 0, 1); assertEquals("NULL", sb.toString()); + sb = new StrBuilder(); - sb.append(new char[] { 'f', 'o', 'o' }, 0, 3); + sb.append(new char[]{'f', 'o', 'o'}, 0, 3); assertEquals("foo", sb.toString()); + final StrBuilder sb1 = sb; - assertIndexOutOfBoundsException(() -> sb1.append(new char[] { 'b', 'a', 'r' }, -1, 1), + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new char[]{'b', 'a', 'r'}, -1, 1), "append(char[], -1,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new char[] { 'b', 'a', 'r' }, 3, 1), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new char[]{'b', 'a', 'r'}, 3, 1), "append(char[], 3,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new char[] { 'b', 'a', 'r' }, 1, -1), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new char[]{'b', 'a', 'r'}, 1, -1), "append(char[],, -1) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new char[] { 'b', 'a', 'r' }, 1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new char[]{'b', 'a', 'r'}, 1, 3), "append(char[], 1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new char[] { 'b', 'a', 'r' }, -1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new char[]{'b', 'a', 'r'}, -1, 3), "append(char[], -1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new char[] { 'b', 'a', 'r' }, 4, 0), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new char[]{'b', 'a', 'r'}, 4, 0), "append(char[], 4, 0) expected IndexOutOfBoundsException"); - sb.append(new char[] { 'b', 'a', 'r' }, 3, 0); + + sb.append(new char[]{'b', 'a', 'r'}, 3, 0); assertEquals("foo", sb.toString()); - sb.append(new char[] { 'a', 'b', 'c', 'b', 'a', 'r', 'd', 'e', 'f' }, 3, 3); + + sb.append(new char[]{'a', 'b', 'c', 'b', 'a', 'r', 'd', 'e', 'f'}, 3, 3); assertEquals("foobar", sb.toString()); } @@ -190,19 +211,45 @@ void testAppend_StrBuilder_int_int() { StrBuilder sb = new StrBuilder(); sb.setNullText("NULL").append((StrBuilder) null, 0, 1); assertEquals("NULL", sb.toString()); + sb = new StrBuilder(); sb.append(new StrBuilder("foo"), 0, 3); assertEquals("foo", sb.toString()); + final StrBuilder sb1 = sb; - assertIndexOutOfBoundsException(() -> sb1.append(new StrBuilder("bar"), -1, 1), "append(char[], -1,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StrBuilder("bar"), 3, 1), "append(char[], 3,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StrBuilder("bar"), 1, -1), "append(char[],, -1) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StrBuilder("bar"), 1, 3), "append(char[], 1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StrBuilder("bar"), -1, 3), + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StrBuilder("bar"), -1, 1), + "append(char[], -1,) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StrBuilder("bar"), 3, 1), + "append(char[], 3,) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StrBuilder("bar"), 1, -1), + "append(char[],, -1) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StrBuilder("bar"), 1, 3), + "append(char[], 1, 3) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StrBuilder("bar"), -1, 3), "append(char[], -1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StrBuilder("bar"), 4, 0), "append(char[], 4, 0) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StrBuilder("bar"), 4, 0), + "append(char[], 4, 0) expected IndexOutOfBoundsException"); + sb.append(new StrBuilder("bar"), 3, 0); assertEquals("foo", sb.toString()); + sb.append(new StrBuilder("abcbardef"), 3, 3); assertEquals("foobar", sb.toString()); } @@ -229,20 +276,48 @@ void testAppend_String_int_int() { StrBuilder sb = new StrBuilder(); sb.setNullText("NULL").append((String) null, 0, 1); assertEquals("NULL", sb.toString()); + sb = new StrBuilder(); sb.append("foo", 0, 3); assertEquals("foo", sb.toString()); + final StrBuilder sb1 = sb; - assertIndexOutOfBoundsException(() -> sb1.append("bar", -1, 1), "append(char[], -1,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append("bar", 3, 1), "append(char[], 3,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append("bar", 1, -1), "append(char[],, -1) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append("bar", 1, 3), "append(char[], 1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append("bar", -1, 3), "append(char[], -1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append("bar", 4, 0), "append(char[], 4, 0) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append("bar", -1, 1), + "append(char[], -1,) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append("bar", 3, 1), + "append(char[], 3,) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append("bar", 1, -1), + "append(char[],, -1) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append("bar", 1, 3), + "append(char[], 1, 3) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append("bar", -1, 3), + "append(char[], -1, 3) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append("bar", 4, 0), + "append(char[], 4, 0) expected IndexOutOfBoundsException"); + sb.append("bar", 3, 0); assertEquals("foo", sb.toString()); + sb.append("abcbardef", 3, 3); assertEquals("foobar", sb.toString()); + sb.append((CharSequence) "abcbardef", 4, 3); assertEquals("foobarard", sb.toString()); } @@ -269,23 +344,45 @@ void testAppend_StringBuffer_int_int() { StrBuilder sb = new StrBuilder(); sb.setNullText("NULL").append((StringBuffer) null, 0, 1); assertEquals("NULL", sb.toString()); + sb = new StrBuilder(); sb.append(new StringBuffer("foo"), 0, 3); assertEquals("foo", sb.toString()); + final StrBuilder sb1 = sb; - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuffer("bar"), -1, 1), + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuffer("bar"), -1, 1), "append(char[], -1,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuffer("bar"), 3, 1), "append(char[], 3,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuffer("bar"), 1, -1), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuffer("bar"), 3, 1), + "append(char[], 3,) expected IndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuffer("bar"), 1, -1), "append(char[],, -1) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuffer("bar"), 1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuffer("bar"), 1, 3), "append(char[], 1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuffer("bar"), -1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuffer("bar"), -1, 3), "append(char[], -1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuffer("bar"), 4, 0), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuffer("bar"), 4, 0), "append(char[], 4, 0) expected IndexOutOfBoundsException"); + sb.append(new StringBuffer("bar"), 3, 0); assertEquals("foo", sb.toString()); + sb.append(new StringBuffer("abcbardef"), 3, 3); assertEquals("foobar", sb.toString()); } @@ -312,26 +409,48 @@ void testAppend_StringBuilder_int_int() { StrBuilder sb = new StrBuilder(); sb.setNullText("NULL").append((String) null, 0, 1); assertEquals("NULL", sb.toString()); + sb = new StrBuilder(); sb.append(new StringBuilder("foo"), 0, 3); assertEquals("foo", sb.toString()); + final StrBuilder sb1 = sb; - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuilder("bar"), -1, 1), + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuilder("bar"), -1, 1), "append(StringBuilder, -1,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuilder("bar"), 3, 1), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuilder("bar"), 3, 1), "append(StringBuilder, 3,) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuilder("bar"), 1, -1), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuilder("bar"), 1, -1), "append(StringBuilder,, -1) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuilder("bar"), 1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuilder("bar"), 1, 3), "append(StringBuilder, 1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuilder("bar"), -1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuilder("bar"), -1, 3), "append(StringBuilder, -1, 3) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb1.append(new StringBuilder("bar"), 4, 0), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.append(new StringBuilder("bar"), 4, 0), "append(StringBuilder, 4, 0) expected IndexOutOfBoundsException"); + sb.append(new StringBuilder("bar"), 3, 0); assertEquals("foo", sb.toString()); + sb.append(new StringBuilder("abcbardef"), 3, 3); assertEquals("foobar", sb.toString()); + sb.append(new StringBuilder("abcbardef"), 4, 3); assertEquals("foobarard", sb.toString()); } @@ -1054,102 +1173,218 @@ void testAppendWithSeparatorsWithNullText() { @Test void testInsert() { + final StrBuilder sb = new StrBuilder(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, FOO), "insert(-1, Object) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, FOO), "insert(7, Object) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, FOO), + "insert(-1, Object) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, FOO), + "insert(7, Object) expected StringIndexOutOfBoundsException"); + sb.insert(0, (Object) null); assertEquals("barbaz", sb.toString()); + sb.insert(0, FOO); assertEquals("foobarbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, "foo"), "insert(-1, String) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, "foo"), "insert(7, String) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, "foo"), + "insert(-1, String) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, "foo"), + "insert(7, String) expected StringIndexOutOfBoundsException"); + sb.insert(0, (String) null); assertEquals("barbaz", sb.toString()); + sb.insert(0, "foo"); assertEquals("foobarbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, new char[] { 'f', 'o', 'o' }), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, new char[]{'f', 'o', 'o'}), "insert(-1, char[]) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, new char[] { 'f', 'o', 'o' }), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, new char[]{'f', 'o', 'o'}), "insert(7, char[]) expected StringIndexOutOfBoundsException"); + sb.insert(0, (char[]) null); assertEquals("barbaz", sb.toString()); + sb.insert(0, new char[0]); assertEquals("barbaz", sb.toString()); - sb.insert(0, new char[] { 'f', 'o', 'o' }); + + sb.insert(0, new char[]{'f', 'o', 'o'}); assertEquals("foobarbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 3, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3), "insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 3, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3), "insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException"); + sb.insert(0, null, 0, 0); assertEquals("barbaz", sb.toString()); + sb.insert(0, new char[0], 0, 0); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, -1, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3), "insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 10, 3), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3), "insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 0, -1), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1), "insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 0, 10), + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10), "insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException"); - sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 0, 0); + + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0); assertEquals("barbaz", sb.toString()); - sb.insert(0, new char[] { 'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f' }, 3, 3); + + sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3); assertEquals("foobarbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, true), "insert(-1, boolean) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, true), "insert(7, boolean) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, true), + "insert(-1, boolean) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, true), + "insert(7, boolean) expected StringIndexOutOfBoundsException"); + sb.insert(0, true); assertEquals("truebarbaz", sb.toString()); + sb.insert(0, false); assertEquals("falsetruebarbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, '!'), "insert(-1, char) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, '!'), "insert(7, char) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, '!'), + "insert(-1, char) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, '!'), + "insert(7, char) expected StringIndexOutOfBoundsException"); + sb.insert(0, '!'); assertEquals("!barbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, 0), "insert(-1, int) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, 0), "insert(7, int) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, 0), + "insert(-1, int) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, 0), + "insert(7, int) expected StringIndexOutOfBoundsException"); + sb.insert(0, '0'); assertEquals("0barbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, 1L), "insert(-1, long) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, 1L), "insert(7, long) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, 1L), + "insert(-1, long) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, 1L), + "insert(7, long) expected StringIndexOutOfBoundsException"); + sb.insert(0, 1L); assertEquals("1barbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, 2.3F), "insert(-1, float) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, 2.3F), "insert(7, float) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, 2.3F), + "insert(-1, float) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, 2.3F), + "insert(7, float) expected StringIndexOutOfBoundsException"); + sb.insert(0, 2.3F); assertEquals("2.3barbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, 4.5D), "insert(-1, double) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, 4.5D), "insert(7, double) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, 4.5D), + "insert(-1, double) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, 4.5D), + "insert(7, double) expected StringIndexOutOfBoundsException"); + sb.insert(0, 4.5D); assertEquals("4.5barbaz", sb.toString()); } @@ -1160,23 +1395,46 @@ void testInsertWithNullText() { sb.setNullText("null"); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, FOO), "insert(-1, Object) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, FOO), "insert(7, Object) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, FOO), + "insert(-1, Object) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, FOO), + "insert(7, Object) expected StringIndexOutOfBoundsException"); + sb.insert(0, (Object) null); assertEquals("nullbarbaz", sb.toString()); + sb.insert(0, FOO); assertEquals("foonullbarbaz", sb.toString()); + sb.clear(); sb.append("barbaz"); assertEquals("barbaz", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.insert(-1, "foo"), "insert(-1, String) expected StringIndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.insert(7, "foo"), "insert(7, String) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(-1, "foo"), + "insert(-1, String) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.insert(7, "foo"), + "insert(7, String) expected StringIndexOutOfBoundsException"); + sb.insert(0, (String) null); assertEquals("nullbarbaz", sb.toString()); + sb.insert(0, "foo"); assertEquals("foonullbarbaz", sb.toString()); + sb.insert(0, (char[]) null); assertEquals("nullfoonullbarbaz", sb.toString()); + sb.insert(0, null, 0, 0); assertEquals("nullnullfoonullbarbaz", sb.toString()); } diff --git a/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java b/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java index 982ff149f01..c0ed9b62689 100644 --- a/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3.text; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -26,6 +25,7 @@ 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.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; @@ -181,11 +181,11 @@ void testAsReader() throws Exception { } try (Reader r = sb.asReader()) { final char[] arr = new char[3]; - assertIndexOutOfBoundsException(() -> r.read(arr, -1, 0)); - assertIndexOutOfBoundsException(() -> r.read(arr, 0, -1)); - assertIndexOutOfBoundsException(() -> r.read(arr, 100, 1)); - assertIndexOutOfBoundsException(() -> r.read(arr, 0, 100)); - assertIndexOutOfBoundsException(() -> r.read(arr, Integer.MAX_VALUE, Integer.MAX_VALUE)); + assertThrows(IndexOutOfBoundsException.class, () -> r.read(arr, -1, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> r.read(arr, 0, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> r.read(arr, 100, 1)); + assertThrows(IndexOutOfBoundsException.class, () -> r.read(arr, 0, 100)); + assertThrows(IndexOutOfBoundsException.class, () -> r.read(arr, Integer.MAX_VALUE, Integer.MAX_VALUE)); assertEquals(0, r.read(arr, 0, 0)); assertEquals(0, arr[0]); @@ -333,7 +333,10 @@ void testCapacityAndLength() { assertEquals(33, sb.size()); assertFalse(sb.isEmpty()); - assertIndexOutOfBoundsException(() -> sb.setLength(-1), "setLength(-1) expected StringIndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.setLength(-1), + "setLength(-1) expected StringIndexOutOfBoundsException"); sb.setLength(33); assertEquals(33, sb.capacity()); @@ -379,14 +382,18 @@ void testChaining() { @Test void testCharAt() { final StrBuilder sb = new StrBuilder(); - assertIndexOutOfBoundsException(() -> sb.charAt(0), "charAt(0) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.charAt(-1), "charAt(-1) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, () -> sb.charAt(0), "charAt(0) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, () -> sb.charAt(-1), "charAt(-1) expected IndexOutOfBoundsException"); sb.append("foo"); assertEquals('f', sb.charAt(0)); assertEquals('o', sb.charAt(1)); assertEquals('o', sb.charAt(2)); - assertIndexOutOfBoundsException(() -> sb.charAt(-1), "charAt(-1) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.charAt(3), "charAt(3) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, () -> sb.charAt(-1), "charAt(-1) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, () -> sb.charAt(3), "charAt(3) expected IndexOutOfBoundsException"); } @Test @@ -541,7 +548,7 @@ void testDeleteCharAt() { sb.deleteCharAt(0); assertEquals("bc", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.deleteCharAt(1000)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.deleteCharAt(1000)); } @Test @@ -616,9 +623,9 @@ void testDeleteIntInt() { sb.delete(0, 1000); assertEquals("", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.delete(1, 2)); - assertIndexOutOfBoundsException(() -> sb.delete(-1, 1)); - assertIndexOutOfBoundsException(() -> new StrBuilder("anything").delete(2, 1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.delete(1, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.delete(-1, 1)); + assertThrows(IndexOutOfBoundsException.class, () -> new StrBuilder("anything").delete(2, 1)); } @Test @@ -741,10 +748,10 @@ void testGetCharsIntIntCharArrayInt() { sb.getChars(0, 2, b, 3); assertArrayEquals(new char[]{0, 0, 0, 'j', 'u'}, b); - assertIndexOutOfBoundsException(() -> sb.getChars(-1, 0, b, 0)); - assertIndexOutOfBoundsException(() -> sb.getChars(0, -1, b, 0)); - assertIndexOutOfBoundsException(() -> sb.getChars(0, 20, b, 0)); - assertIndexOutOfBoundsException(() -> sb.getChars(4, 2, b, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.getChars(-1, 0, b, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.getChars(0, -1, b, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.getChars(0, 20, b, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.getChars(4, 2, b, 0)); } @Test @@ -1272,11 +1279,11 @@ void testReplace_int_int_String() { final StrBuilder sb1 = new StrBuilder("atext"); sb1.replace(1, 1, "ny"); assertEquals("anytext", sb1.toString()); - assertIndexOutOfBoundsException(() -> sb1.replace(2, 1, "anything")); + assertThrows(IndexOutOfBoundsException.class, () -> sb1.replace(2, 1, "anything")); final StrBuilder sb2 = new StrBuilder(); - assertIndexOutOfBoundsException(() -> sb2.replace(1, 2, "anything")); - assertIndexOutOfBoundsException(() -> sb2.replace(-1, 1, "anything")); + assertThrows(IndexOutOfBoundsException.class, () -> sb2.replace(1, 2, "anything")); + assertThrows(IndexOutOfBoundsException.class, () -> sb2.replace(-1, 1, "anything")); } @Test @@ -1357,7 +1364,9 @@ void testReplace_StrMatcher_String_int_int_int_VaryEndIndex() { assertEquals("-x--y-", sb.toString()); final StrBuilder sb1 = new StrBuilder("aaxaaaayaa"); - assertIndexOutOfBoundsException(() -> sb1.replace(StrMatcher.stringMatcher("aa"), "-", 2, 1, -1)); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.replace(StrMatcher.stringMatcher("aa"), "-", 2, 1, -1)); assertEquals("aaxaaaayaa", sb1.toString()); } @@ -1452,10 +1461,15 @@ void testReplace_StrMatcher_String_int_int_int_VaryStartIndex() { assertEquals("aaxaaaayaa", sb.toString()); final StrBuilder sb1 = new StrBuilder("aaxaaaayaa"); - assertIndexOutOfBoundsException(() -> sb1.replace(StrMatcher.stringMatcher("aa"), "-", 11, sb1.length(), -1)); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb1.replace(StrMatcher.stringMatcher("aa"), "-", 11, sb1.length(), -1)); assertEquals("aaxaaaayaa", sb1.toString()); + final StrBuilder sb2 = new StrBuilder("aaxaaaayaa"); - assertIndexOutOfBoundsException(() -> sb2.replace(StrMatcher.stringMatcher("aa"), "-", -1, sb2.length(), -1)); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb2.replace(StrMatcher.stringMatcher("aa"), "-", -1, sb2.length(), -1)); assertEquals("aaxaaaayaa", sb2.toString()); } @@ -1643,13 +1657,22 @@ void testRightString() { @Test void testSetCharAt() { final StrBuilder sb = new StrBuilder(); - assertIndexOutOfBoundsException(() -> sb.setCharAt(0, 'f'), "setCharAt(0, ) expected IndexOutOfBoundsException"); - assertIndexOutOfBoundsException(() -> sb.setCharAt(-1, 'f'), "setCharAt(-1, ) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.setCharAt(0, 'f'), + "setCharAt(0, ) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.setCharAt(-1, 'f'), + "setCharAt(-1, ) expected IndexOutOfBoundsException"); sb.append("foo"); sb.setCharAt(0, 'b'); sb.setCharAt(1, 'a'); sb.setCharAt(2, 'r'); - assertIndexOutOfBoundsException(() -> sb.setCharAt(3, '!'), "setCharAt(3, ) expected IndexOutOfBoundsException"); + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.setCharAt(3, '!'), + "setCharAt(3, ) expected IndexOutOfBoundsException"); assertEquals("bar", sb.toString()); } @@ -1657,13 +1680,17 @@ void testSetCharAt() { void testSetLength() { final StrBuilder sb = new StrBuilder(); sb.append("Hello"); - sb.setLength(2); // shorten + sb.setLength(2); // shorten assertEquals("He", sb.toString()); - sb.setLength(2); // no change + sb.setLength(2); // no change assertEquals("He", sb.toString()); - sb.setLength(3); // lengthen + sb.setLength(3); // lengthen assertEquals("He\0", sb.toString()); - assertIndexOutOfBoundsException(() -> sb.setLength(-1), "setLength(-1) expected StringIndexOutOfBoundsException"); + + assertThrows( + IndexOutOfBoundsException.class, + () -> sb.setLength(-1), + "setLength(-1) expected StringIndexOutOfBoundsException"); } @Test @@ -1692,16 +1719,16 @@ void testStartsWith() { void testSubSequenceIntInt() { final StrBuilder sb = new StrBuilder ("hello goodbye"); // Start index is negative - assertIndexOutOfBoundsException(() -> sb.subSequence(-1, 5)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(-1, 5)); // End index is negative - assertIndexOutOfBoundsException(() -> sb.subSequence(2, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(2, -1)); // End index greater than length() - assertIndexOutOfBoundsException(() -> sb.subSequence(2, sb.length() + 1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(2, sb.length() + 1)); // Start index greater then end index - assertIndexOutOfBoundsException(() -> sb.subSequence(3, 2)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.subSequence(3, 2)); // Normal cases assertEquals ("hello", sb.subSequence(0, 5)); @@ -1717,9 +1744,9 @@ void testSubstringInt() { assertEquals ("hello goodbye".substring(6), sb.substring(6)); assertEquals ("hello goodbye", sb.substring(0)); assertEquals ("hello goodbye".substring(0), sb.substring(0)); - assertIndexOutOfBoundsException(() -> sb.substring(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(-1)); - assertIndexOutOfBoundsException(() -> sb.substring(15)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15)); } @Test @@ -1733,8 +1760,8 @@ void testSubstringIntInt() { assertEquals ("goodbye", sb.substring(6, 20)); - assertIndexOutOfBoundsException(() -> sb.substring(-1, 5)); - assertIndexOutOfBoundsException(() -> sb.substring(15, 20)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(-1, 5)); + assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15, 20)); } @Test @@ -1773,8 +1800,11 @@ void testToCharArrayIntInt() { a = sb.toCharArray(0, 1); assertNotNull(a, "toCharArray(int, int) result is null"); - assertIndexOutOfBoundsException(() -> sb.toCharArray(-1, 5), "no string index out of bound on -1"); - assertIndexOutOfBoundsException(() -> sb.toCharArray(6, 5), "no string index out of bound on -1"); + assertThrows( + IndexOutOfBoundsException.class, () -> sb.toCharArray(-1, 5), "no string index out of bound on -1"); + + assertThrows( + IndexOutOfBoundsException.class, () -> sb.toCharArray(6, 5), "no string index out of bound on -1"); } @Test diff --git a/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java b/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java index 251b5ba7f86..1d89a70092c 100644 --- a/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java +++ b/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java @@ -204,11 +204,18 @@ void testCyclicReplacement() { map.put("critterColor", "brown"); map.put("critterType", "${animal}"); final StrSubstitutor sub = new StrSubstitutor(map); - assertThrows(IllegalStateException.class, () -> sub.replace("The ${animal} jumps over the ${target}."), "Cyclic replacement was not detected!"); + assertThrows( + IllegalStateException.class, + () -> sub.replace("The ${animal} jumps over the ${target}."), + "Cyclic replacement was not detected!"); + // also check even when default value is set. map.put("critterType", "${animal:-fox}"); final StrSubstitutor sub2 = new StrSubstitutor(map); - assertThrows(IllegalStateException.class, () -> sub2.replace("The ${animal} jumps over the ${target}."), "Cyclic replacement was not detected!"); + assertThrows( + IllegalStateException.class, + () -> sub2.replace("The ${animal} jumps over the ${target}."), + "Cyclic replacement was not detected!"); } @Test diff --git a/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java index 52a5cf76d9c..384f0b92c00 100644 --- a/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java +++ b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityUnescaperTest.java @@ -17,8 +17,8 @@ package org.apache.commons.lang3.text.translate; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.apache.commons.lang3.AbstractLangTest; import org.junit.jupiter.api.Test; @@ -71,7 +71,7 @@ void testUnfinishedEntity() { final NumericEntityUnescaper failingNeu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon); final String failingInput = "Test 0 not test"; - assertIllegalArgumentException(() -> failingNeu.translate(failingInput)); + assertThrows(IllegalArgumentException.class, () -> failingNeu.translate(failingInput)); } } diff --git a/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java b/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java index 1079acb5691..3a28bff99f9 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java @@ -16,9 +16,9 @@ */ package org.apache.commons.lang3.time; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Calendar; import java.util.Date; @@ -155,20 +155,20 @@ void testHoursOfYearWithDate() { @Test void testInvalidFragmentWithCalendar() { - assertIllegalArgumentException(() -> DateUtils.getFragmentInMilliseconds(aCalendar, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInSeconds(aCalendar, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInMinutes(aCalendar, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInHours(aCalendar, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInDays(aCalendar, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMilliseconds(aCalendar, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInSeconds(aCalendar, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMinutes(aCalendar, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInHours(aCalendar, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInDays(aCalendar, 0)); } @Test void testInvalidFragmentWithDate() { - assertIllegalArgumentException(() -> DateUtils.getFragmentInMilliseconds(aDate, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInSeconds(aDate, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInMinutes(aDate, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInHours(aDate, 0)); - assertIllegalArgumentException(() -> DateUtils.getFragmentInDays(aDate, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMilliseconds(aDate, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInSeconds(aDate, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMinutes(aDate, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInHours(aDate, 0)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInDays(aDate, 0)); } // Calendar.MINUTE as useful fragment diff --git a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java index 5dac357771b..c857d7a4d7b 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.time; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.apache.commons.lang3.LangAssertions.assertNullPointerException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -629,7 +628,7 @@ void testCeiling() throws Exception { assertNullPointerException(() -> DateUtils.ceiling((Calendar) null, Calendar.SECOND)); assertNullPointerException(() -> DateUtils.ceiling((Object) null, Calendar.SECOND)); assertThrows(ClassCastException.class, () -> DateUtils.ceiling("", Calendar.SECOND)); - assertIllegalArgumentException(() -> DateUtils.ceiling(date1, -9999)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.ceiling(date1, -9999)); } @@ -860,7 +859,7 @@ void testIsSameLocalTime_CalNullNull() { */ @Test void testIteratorEx() { - assertIllegalArgumentException(() -> DateUtils.iterator(Calendar.getInstance(), -9999)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.iterator(Calendar.getInstance(), -9999)); assertNullPointerException(() -> DateUtils.iterator((Date) null, DateUtils.RANGE_WEEK_CENTER)); assertNullPointerException(() -> DateUtils.iterator((Calendar) null, DateUtils.RANGE_WEEK_CENTER)); assertNullPointerException(() -> DateUtils.iterator((Object) null, DateUtils.RANGE_WEEK_CENTER)); @@ -1059,7 +1058,7 @@ void testRound() throws Exception { assertNullPointerException(() -> DateUtils.round((Calendar) null, Calendar.SECOND)); assertNullPointerException(() -> DateUtils.round((Object) null, Calendar.SECOND)); assertThrows(ClassCastException.class, () -> DateUtils.round("", Calendar.SECOND)); - assertIllegalArgumentException(() -> DateUtils.round(date1, -9999)); + assertThrows(IllegalArgumentException.class, () -> DateUtils.round(date1, -9999)); assertEquals(dateTimeParser.parse("February 3, 2002 00:00:00.000"), DateUtils.round((Object) calAmPm1, Calendar.AM_PM), "round ampm-1 failed"); assertEquals(dateTimeParser.parse("February 3, 2002 12:00:00.000"), DateUtils.round((Object) calAmPm2, Calendar.AM_PM), "round ampm-2 failed"); @@ -1178,13 +1177,22 @@ void testSetDays() throws Exception { assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 1, 4, 3, 2, 1); + result = DateUtils.setDays(BASE_DATE, 29); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 29, 4, 3, 2, 1); + final String outsideOfRangeAssertionMessage = "DateUtils.setDays did not throw an expected IllegalArgumentException for amount outside of range 1 to 31."; - assertIllegalArgumentException(() -> DateUtils.setDays(BASE_DATE, 32), outsideOfRangeAssertionMessage); - assertIllegalArgumentException(() -> DateUtils.setDays(BASE_DATE, 0), outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setDays(BASE_DATE, 32), + outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setDays(BASE_DATE, 0), + outsideOfRangeAssertionMessage); + assertNullPointerException(() -> DateUtils.setDays(null, 1)); } @@ -1194,13 +1202,22 @@ void testSetHours() throws Exception { assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 0, 3, 2, 1); + result = DateUtils.setHours(BASE_DATE, 23); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 23, 3, 2, 1); + final String outsideOfRangeAssertionMessage = "DateUtils.setHours did not throw an expected IllegalArgumentException for amount outside of range 0 to 23."; - assertIllegalArgumentException(() -> DateUtils.setHours(BASE_DATE, 24), outsideOfRangeAssertionMessage); - assertIllegalArgumentException(() -> DateUtils.setHours(BASE_DATE, -1), outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setHours(BASE_DATE, 24), + outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setHours(BASE_DATE, -1), + outsideOfRangeAssertionMessage); + assertNullPointerException(() -> DateUtils.setHours(null, 0)); } @@ -1210,13 +1227,22 @@ void testSetMilliseconds() throws Exception { assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 4, 3, 2, 0); + result = DateUtils.setMilliseconds(BASE_DATE, 999); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 4, 3, 2, 999); + final String outsideOfRangeAssertionMessage = "DateUtils.setMilliseconds did not throw an expected IllegalArgumentException for range outside of 0 to 999."; - assertIllegalArgumentException(() -> DateUtils.setMilliseconds(BASE_DATE, 1000), outsideOfRangeAssertionMessage); - assertIllegalArgumentException(() -> DateUtils.setMilliseconds(BASE_DATE, -1), outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setMilliseconds(BASE_DATE, 1000), + outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setMilliseconds(BASE_DATE, -1), + outsideOfRangeAssertionMessage); + assertNullPointerException(() -> DateUtils.setMilliseconds(null, 0)); } @@ -1226,13 +1252,22 @@ void testSetMinutes() throws Exception { assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 4, 0, 2, 1); + result = DateUtils.setMinutes(BASE_DATE, 59); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 4, 59, 2, 1); + final String outsideOfRangeAssertionMessage = "DateUtils.setMinutes did not throw an expected IllegalArgumentException for amount outside of range 0 to 59."; - assertIllegalArgumentException(() -> DateUtils.setMinutes(BASE_DATE, 60), outsideOfRangeAssertionMessage); - assertIllegalArgumentException(() -> DateUtils.setMinutes(BASE_DATE, -1), outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setMinutes(BASE_DATE, 60), + outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setMinutes(BASE_DATE, -1), + outsideOfRangeAssertionMessage); + assertNullPointerException(() -> DateUtils.setMinutes(null, 0)); } @@ -1242,17 +1277,27 @@ void testSetMonths() throws Exception { assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 5, 5, 4, 3, 2, 1); + result = DateUtils.setMonths(BASE_DATE, 1); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 1, 5, 4, 3, 2, 1); + result = DateUtils.setMonths(BASE_DATE, 0); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 0, 5, 4, 3, 2, 1); + final String outsideOfRangeAssertionMessage = "DateUtils.setMonths did not throw an expected IllegalArgumentException for amount outside of range 0 to 11."; - assertIllegalArgumentException(() -> DateUtils.setMonths(BASE_DATE, 12), outsideOfRangeAssertionMessage); - assertIllegalArgumentException(() -> DateUtils.setMonths(BASE_DATE, -1), outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setMonths(BASE_DATE, 12), + outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setMonths(BASE_DATE, -1), + outsideOfRangeAssertionMessage); + assertNullPointerException(() -> DateUtils.setMonths(null, 0)); } @@ -1262,13 +1307,22 @@ void testSetSeconds() throws Exception { assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 4, 3, 0, 1); + result = DateUtils.setSeconds(BASE_DATE, 59); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 6, 5, 4, 3, 59, 1); + final String outsideOfRangeAssertionMessage = "DateUtils.setSeconds did not throw an expected IllegalArgumentException for amount outside of range 0 to 59."; - assertIllegalArgumentException(() -> DateUtils.setSeconds(BASE_DATE, 60), outsideOfRangeAssertionMessage); - assertIllegalArgumentException(() -> DateUtils.setSeconds(BASE_DATE, -1), outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setSeconds(BASE_DATE, 60), + outsideOfRangeAssertionMessage); + assertThrows( + IllegalArgumentException.class, + () -> DateUtils.setSeconds(BASE_DATE, -1), + outsideOfRangeAssertionMessage); + assertNullPointerException(() -> DateUtils.setSeconds(null, 0)); } @@ -1581,34 +1635,50 @@ void testTruncateLang59() { TimeZone.setDefault(denverZone); final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX"); format.setTimeZone(denverZone); + final Date oct31_01MDT = new Date(1099206000000L); + final Date oct31MDT = new Date(oct31_01MDT.getTime() - 3600000L); // - 1 hour final Date oct31_01_02MDT = new Date(oct31_01MDT.getTime() + 120000L); // + 2 minutes final Date oct31_01_02_03MDT = new Date(oct31_01_02MDT.getTime() + 3000L); // + 3 seconds final Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L); // + 4 milliseconds + assertEquals("2004-10-31 00:00:00.000 -06:00", format.format(oct31MDT), "Check 00:00:00.000"); assertEquals("2004-10-31 01:00:00.000 -06:00", format.format(oct31_01MDT), "Check 01:00:00.000"); assertEquals("2004-10-31 01:02:00.000 -06:00", format.format(oct31_01_02MDT), "Check 01:02:00.000"); assertEquals("2004-10-31 01:02:03.000 -06:00", format.format(oct31_01_02_03MDT), "Check 01:02:03.000"); assertEquals("2004-10-31 01:02:03.004 -06:00", format.format(oct31_01_02_03_04MDT), "Check 01:02:03.004"); + // Demonstrate Problem final Calendar gval = Calendar.getInstance(); gval.setTime(new Date(oct31_01MDT.getTime())); gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value assertEquals(gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L, "Demonstrate Problem"); + // Test Truncate assertEquals(oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND), "Truncate Calendar.MILLISECOND"); + assertEquals(oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND), "Truncate Calendar.SECOND"); + assertEquals(oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE), "Truncate Calendar.MINUTE"); + assertEquals(oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY), "Truncate Calendar.HOUR_OF_DAY"); + assertEquals(oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR), "Truncate Calendar.HOUR"); + assertEquals(oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE), "Truncate Calendar.DATE"); + // Test Round (down) assertEquals(oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND), "Round Calendar.MILLISECOND"); + assertEquals(oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND), "Round Calendar.SECOND"); + assertEquals(oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE), "Round Calendar.MINUTE"); + assertEquals(oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY), "Round Calendar.HOUR_OF_DAY"); + assertEquals(oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR), "Round Calendar.HOUR"); + assertEquals(oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE), "Round Calendar.DATE"); } diff --git a/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java index 378b0ec2d36..139453bfb06 100644 --- a/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java @@ -17,12 +17,12 @@ package org.apache.commons.lang3.time; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; @@ -396,22 +396,22 @@ void testFormatDurationWords() { @Test void testFormatNegativeDuration() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatDuration(-5000, "S", true)); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(-5000, "S", true)); } @Test void testFormatNegativeDurationHMS() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatDurationHMS(-5000)); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationHMS(-5000)); } @Test void testFormatNegativeDurationISO() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatDurationISO(-5000)); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationISO(-5000)); } @Test void testFormatNegativeDurationWords() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatDurationWords(-5000, true, true)); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationWords(-5000, true, true)); } @Test @@ -475,7 +475,7 @@ void testFormatPeriod() { @Test void testFormatPeriodeStartGreaterEnd() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM")); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM")); } @SuppressWarnings("deprecation") @@ -518,7 +518,7 @@ void testFormatPeriodISOMethod() { @Test void testFormatPeriodISOStartGreaterEnd() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatPeriodISO(5000, 2000)); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriodISO(5000, 2000)); } /** @@ -554,7 +554,7 @@ void testLANG815() { @Test void testLANG981() { // unmatched quote char in lexx - assertIllegalArgumentException(() -> DurationFormatUtils.lexx("'yMdHms''S")); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.lexx("'yMdHms''S")); } @Test void testLANG982() { // More than 3 millisecond digits following a second @@ -723,8 +723,8 @@ void testOptionalToken() { @Test void testUnmatchedOptionalTokens() { - assertIllegalArgumentException(() -> DurationFormatUtils.formatDuration(1, "[s")); - assertIllegalArgumentException(() -> DurationFormatUtils.formatDuration(1, "[[s")); - assertIllegalArgumentException(() -> DurationFormatUtils.formatDuration(1, "[s]]")); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(1, "[s")); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(1, "[[s")); + assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(1, "[s]]")); } } diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java index 3369ff38287..29b91a6dbb0 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.time; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -273,7 +272,7 @@ void test1806() throws ParseException { @Test void test1806Argument() { - assertIllegalArgumentException(() -> getInstance("XXXX")); + assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX")); } @ParameterizedTest diff --git a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java index cb8c9deca01..0593f14c1e3 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java @@ -16,9 +16,9 @@ */ package org.apache.commons.lang3.time; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.Serializable; @@ -122,7 +122,7 @@ void test1806() { @Test void test1806Argument() { - assertIllegalArgumentException(() -> getInstance("XXXX")); + assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX")); } @Test diff --git a/src/test/java/org/apache/commons/lang3/time/GmtTimeZoneTest.java b/src/test/java/org/apache/commons/lang3/time/GmtTimeZoneTest.java index e0f4bbf6cce..8f0d9336fbd 100644 --- a/src/test/java/org/apache/commons/lang3/time/GmtTimeZoneTest.java +++ b/src/test/java/org/apache/commons/lang3/time/GmtTimeZoneTest.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang3.time; -import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -57,7 +56,7 @@ void testHoursInRange() { @Test void testHoursOutOfRange() { - assertIllegalArgumentException(() -> new GmtTimeZone(false, 24, 0)); + assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 24, 0)); } @Test @@ -72,7 +71,7 @@ void testMinutesInRange() { @Test void testMinutesOutOfRange() { - assertIllegalArgumentException(() -> new GmtTimeZone(false, 0, 60)); + assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 0, 60)); } @Test diff --git a/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java b/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java index cb94598a4e8..24b2462481f 100644 --- a/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java +++ b/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3.util; -import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -215,7 +214,7 @@ void test_clearI() { eightFbs.clear(165); assertFalse(eightFbs.get(165), "Failed to clear bit"); // Try out of range - assertIndexOutOfBoundsException(() -> eightFbs.clear(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> eightFbs.clear(-1)); final FluentBitSet bs = newInstance(0); assertEquals(0, bs.length(), "Test1: Wrong length,"); @@ -383,17 +382,17 @@ void test_clearII() { // test illegal args bs = newInstance(10); - assertIndexOutOfBoundsException(() -> newInstance(10).clear(-1, 3), + assertThrows(IndexOutOfBoundsException.class, () -> newInstance(10).clear(-1, 3), "Test1: Attempt to flip with negative index failed to generate exception"); - assertIndexOutOfBoundsException(() -> newInstance(10).clear(2, -1), + assertThrows(IndexOutOfBoundsException.class, () -> newInstance(10).clear(2, -1), "Test2: Attempt to flip with negative index failed to generate exception"); bs.set(2, 4); bs.clear(2, 2); assertTrue(bs.get(2), "Bit got cleared incorrectly "); - assertIndexOutOfBoundsException(() -> newInstance(10).clear(4, 2), + assertThrows(IndexOutOfBoundsException.class, () -> newInstance(10).clear(4, 2), "Test4: Attempt to flip with illegal args failed to generate exception"); bs = newInstance(0); @@ -451,7 +450,7 @@ void test_clearIntArray() { eightFbs.clear(165); assertFalse(eightFbs.get(165), "Failed to clear bit"); // Try out of range - assertIndexOutOfBoundsException(() -> eightFbs.clear(-1)); + assertThrows(IndexOutOfBoundsException.class, () -> eightFbs.clear(-1)); final FluentBitSet bs = newInstance(0); assertEquals(0, bs.length(), "Test1: Wrong length,"); @@ -560,7 +559,7 @@ void test_flipI() { assertFalse(bs.get(9), "Failed to flip bit"); assertFalse(bs.get(10), "Failed to flip bit"); - assertIndexOutOfBoundsException(() -> newInstance().flip(-1), "Attempt to flip at negative index failed to generate exception"); + assertThrows(IndexOutOfBoundsException.class, () -> newInstance().flip(-1), "Attempt to flip at negative index failed to generate exception"); // Try setting a bit on a 64 boundary bs.flip(128); @@ -770,7 +769,7 @@ void test_getI() { assertTrue(eightFbs.get(3), "Get returned false for set value"); assertFalse(bs.get(0), "Get returned true for a non set value"); - assertIndexOutOfBoundsException(() -> newInstance().get(-1), "Attempt to get at negative index failed to generate exception"); + assertThrows(IndexOutOfBoundsException.class, () -> newInstance().get(-1), "Attempt to get at negative index failed to generate exception"); bs = newInstance(1); assertFalse(bs.get(64), "Access greater than size");