Skip to content

Commit d7279c1

Browse files
committed
Refactor some test assertions
1 parent 7a033f8 commit d7279c1

File tree

59 files changed

+952
-1868
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+952
-1868
lines changed

src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717

1818
package org.apache.commons.lang3;
1919

20+
import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
21+
import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException;
2022
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2123
import static org.junit.jupiter.api.Assertions.assertEquals;
2224
import static org.junit.jupiter.api.Assertions.assertNotNull;
2325
import static org.junit.jupiter.api.Assertions.assertNotSame;
2426
import static org.junit.jupiter.api.Assertions.assertNull;
25-
import static org.junit.jupiter.api.Assertions.assertThrows;
2627

2728
import org.junit.jupiter.api.Test;
2829

@@ -523,23 +524,23 @@ void testAddObjectAtIndex() {
523524
// boolean tests
524525
boolean[] booleanArray = ArrayUtils.add(null, 0, true);
525526
assertArrayEquals(new boolean[] { true }, booleanArray);
526-
IndexOutOfBoundsException e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(null, -1, true));
527+
IndexOutOfBoundsException e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(null, -1, true));
527528
assertEquals("Index: -1, Length: 0", e.getMessage());
528529
booleanArray = ArrayUtils.add(new boolean[] { true }, 0, false);
529530
assertArrayEquals(new boolean[] { false, true }, booleanArray);
530531
booleanArray = ArrayUtils.add(new boolean[] { false }, 1, true);
531532
assertArrayEquals(new boolean[] { false, true }, booleanArray);
532533
booleanArray = ArrayUtils.add(new boolean[] { true, false }, 1, true);
533534
assertArrayEquals(new boolean[] { true, true, false }, booleanArray);
534-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new boolean[] { true, false }, 4, true));
535+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new boolean[] { true, false }, 4, true));
535536
assertEquals("Index: 4, Length: 2", e.getMessage());
536-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new boolean[] { true, false }, -1, true));
537+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new boolean[] { true, false }, -1, true));
537538
assertEquals("Index: -1, Length: 2", e.getMessage());
538539

539540
// char tests
540541
char[] charArray = ArrayUtils.add((char[]) null, 0, 'a');
541542
assertArrayEquals(new char[] { 'a' }, charArray);
542-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((char[]) null, -1, 'a'));
543+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((char[]) null, -1, 'a'));
543544
assertEquals("Index: -1, Length: 0", e.getMessage());
544545
charArray = ArrayUtils.add(new char[] { 'a' }, 0, 'b');
545546
assertArrayEquals(new char[] { 'b', 'a' }, charArray);
@@ -549,105 +550,105 @@ void testAddObjectAtIndex() {
549550
assertArrayEquals(new char[] { 'a', 'k', 'b' }, charArray);
550551
charArray = ArrayUtils.add(new char[] { 'a', 'b', 'c' }, 1, 't');
551552
assertArrayEquals(new char[] { 'a', 't', 'b', 'c' }, charArray);
552-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new char[] { 'a', 'b' }, 4, 'c'));
553+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new char[] { 'a', 'b' }, 4, 'c'));
553554
assertEquals("Index: 4, Length: 2", e.getMessage());
554-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new char[] { 'a', 'b' }, -1, 'c'));
555+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new char[] { 'a', 'b' }, -1, 'c'));
555556
assertEquals("Index: -1, Length: 2", e.getMessage());
556557

557558
// short tests
558559
short[] shortArray = ArrayUtils.add(new short[] { 1 }, 0, (short) 2);
559560
assertArrayEquals(new short[] { 2, 1 }, shortArray);
560-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((short[]) null, -1, (short) 2));
561+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((short[]) null, -1, (short) 2));
561562
assertEquals("Index: -1, Length: 0", e.getMessage());
562563
shortArray = ArrayUtils.add(new short[] { 2, 6 }, 2, (short) 10);
563564
assertArrayEquals(new short[] { 2, 6, 10 }, shortArray);
564565
shortArray = ArrayUtils.add(new short[] { 2, 6 }, 0, (short) -4);
565566
assertArrayEquals(new short[] { -4, 2, 6 }, shortArray);
566567
shortArray = ArrayUtils.add(new short[] { 2, 6, 3 }, 2, (short) 1);
567568
assertArrayEquals(new short[] { 2, 6, 1, 3 }, shortArray);
568-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new short[] { 2, 6 }, 4, (short) 10));
569+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new short[] { 2, 6 }, 4, (short) 10));
569570
assertEquals("Index: 4, Length: 2", e.getMessage());
570-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new short[] { 2, 6 }, -1, (short) 10));
571+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new short[] { 2, 6 }, -1, (short) 10));
571572
assertEquals("Index: -1, Length: 2", e.getMessage());
572573

573574
// byte tests
574575
byte[] byteArray = ArrayUtils.add(new byte[] { 1 }, 0, (byte) 2);
575576
assertArrayEquals(new byte[] { 2, 1 }, byteArray);
576-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((byte[]) null, -1, (byte) 2));
577+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((byte[]) null, -1, (byte) 2));
577578
assertEquals("Index: -1, Length: 0", e.getMessage());
578579
byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 2, (byte) 3);
579580
assertArrayEquals(new byte[] { 2, 6, 3 }, byteArray);
580581
byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 0, (byte) 1);
581582
assertArrayEquals(new byte[] { 1, 2, 6 }, byteArray);
582583
byteArray = ArrayUtils.add(new byte[] { 2, 6, 3 }, 2, (byte) 1);
583584
assertArrayEquals(new byte[] { 2, 6, 1, 3 }, byteArray);
584-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new byte[] { 2, 6 }, 4, (byte) 3));
585+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new byte[] { 2, 6 }, 4, (byte) 3));
585586
assertEquals("Index: 4, Length: 2", e.getMessage());
586-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new byte[] { 2, 6 }, -1, (byte) 3));
587+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new byte[] { 2, 6 }, -1, (byte) 3));
587588
assertEquals("Index: -1, Length: 2", e.getMessage());
588589

589590
// int tests
590591
int[] intArray = ArrayUtils.add(new int[] { 1 }, 0, 2);
591592
assertArrayEquals(new int[] { 2, 1 }, intArray);
592-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((int[]) null, -1, 2));
593+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((int[]) null, -1, 2));
593594
assertEquals("Index: -1, Length: 0", e.getMessage());
594595
intArray = ArrayUtils.add(new int[] { 2, 6 }, 2, 10);
595596
assertArrayEquals(new int[] { 2, 6, 10 }, intArray);
596597
intArray = ArrayUtils.add(new int[] { 2, 6 }, 0, -4);
597598
assertArrayEquals(new int[] { -4, 2, 6 }, intArray);
598599
intArray = ArrayUtils.add(new int[] { 2, 6, 3 }, 2, 1);
599600
assertArrayEquals(new int[] { 2, 6, 1, 3 }, intArray);
600-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new int[] { 2, 6 }, 4, 10));
601+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new int[] { 2, 6 }, 4, 10));
601602
assertEquals("Index: 4, Length: 2", e.getMessage());
602-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new int[] { 2, 6 }, -1, 10));
603+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new int[] { 2, 6 }, -1, 10));
603604
assertEquals("Index: -1, Length: 2", e.getMessage());
604605

605606
// long tests
606607
long[] longArray = ArrayUtils.add(new long[] { 1L }, 0, 2L);
607608
assertArrayEquals(new long[] { 2L, 1L }, longArray);
608-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((long[]) null, -1, 2L));
609+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((long[]) null, -1, 2L));
609610
assertEquals("Index: -1, Length: 0", e.getMessage());
610611
longArray = ArrayUtils.add(new long[] { 2L, 6L }, 2, 10L);
611612
assertArrayEquals(new long[] { 2L, 6L, 10L }, longArray);
612613
longArray = ArrayUtils.add(new long[] { 2L, 6L }, 0, -4L);
613614
assertArrayEquals(new long[] { -4L, 2L, 6L }, longArray);
614615
longArray = ArrayUtils.add(new long[] { 2L, 6L, 3L }, 2, 1L);
615616
assertArrayEquals(new long[] { 2L, 6L, 1L, 3L }, longArray);
616-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new long[] { 2L, 6L }, 4, 10L));
617+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new long[] { 2L, 6L }, 4, 10L));
617618
assertEquals("Index: 4, Length: 2", e.getMessage());
618-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new long[] { 2L, 6L }, -1, 10L));
619+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new long[] { 2L, 6L }, -1, 10L));
619620
assertEquals("Index: -1, Length: 2", e.getMessage());
620621

621622
// float tests
622623
float[] floatArray = ArrayUtils.add(new float[] { 1.1f }, 0, 2.2f);
623624
assertArrayEquals(new float[] { 2.2f, 1.1f }, floatArray);
624-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add((float[]) null, -1, 2.2f));
625+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((float[]) null, -1, 2.2f));
625626
assertEquals("Index: -1, Length: 0", e.getMessage());
626627
floatArray = ArrayUtils.add(new float[] { 2.3f, 6.4f }, 2, 10.5f);
627628
assertArrayEquals(new float[] { 2.3f, 6.4f, 10.5f }, floatArray);
628629
floatArray = ArrayUtils.add(new float[] { 2.6f, 6.7f }, 0, -4.8f);
629630
assertArrayEquals(new float[] { -4.8f, 2.6f, 6.7f }, floatArray);
630631
floatArray = ArrayUtils.add(new float[] { 2.9f, 6.0f, 0.3f }, 2, 1.0f);
631632
assertArrayEquals(new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray);
632-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, 4, 10.5f));
633+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, 4, 10.5f));
633634
assertEquals("Index: 4, Length: 2", e.getMessage());
634-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, -1, 10.5f));
635+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, -1, 10.5f));
635636
assertEquals("Index: -1, Length: 2", e.getMessage());
636637

637638
// double tests
638639
double[] doubleArray = ArrayUtils.add(new double[] { 1.1 }, 0, 2.2);
639640
assertArrayEquals(new double[] { 2.2, 1.1 }, doubleArray);
640-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(null, -1, 2.2));
641+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(null, -1, 2.2));
641642
assertEquals("Index: -1, Length: 0", e.getMessage());
642643
doubleArray = ArrayUtils.add(new double[] { 2.3, 6.4 }, 2, 10.5);
643644
assertArrayEquals(new double[] { 2.3, 6.4, 10.5 }, doubleArray);
644645
doubleArray = ArrayUtils.add(new double[] { 2.6, 6.7 }, 0, -4.8);
645646
assertArrayEquals(new double[] { -4.8, 2.6, 6.7 }, doubleArray);
646647
doubleArray = ArrayUtils.add(new double[] { 2.9, 6.0, 0.3 }, 2, 1.0);
647648
assertArrayEquals(new double[] { 2.9, 6.0, 1.0, 0.3 }, doubleArray);
648-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new double[] { 2.3, 6.4 }, 4, 10.5));
649+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new double[] { 2.3, 6.4 }, 4, 10.5));
649650
assertEquals("Index: 4, Length: 2", e.getMessage());
650-
e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new double[] { 2.3, 6.4 }, -1, 10.5));
651+
e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new double[] { 2.3, 6.4 }, -1, 10.5));
651652
assertEquals("Index: -1, Length: 2", e.getMessage());
652653
}
653654

@@ -659,16 +660,16 @@ void testJira567() {
659660
assertEquals(2, n.length);
660661
assertEquals(Number.class, n.getClass().getComponentType());
661662
// Invalid - can't store Long in Integer array
662-
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.addAll(new Integer[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) }));
663+
assertIllegalArgumentException(() -> ArrayUtils.addAll(new Integer[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) }));
663664
}
664665

665666
@Test
666667
@SuppressWarnings("deprecation")
667668
void testLANG571() {
668669
final String[] stringArray = null;
669670
final String aString = null;
670-
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.add(stringArray, aString));
671-
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.add(stringArray, 0, aString));
671+
assertIllegalArgumentException(() -> ArrayUtils.add(stringArray, aString));
672+
assertIllegalArgumentException(() -> ArrayUtils.add(stringArray, 0, aString));
672673
}
673674

674675
}

0 commit comments

Comments
 (0)