Convert tests for Validate.validIndex overloads to @Nested test

Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/f6f8e5db
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/f6f8e5db
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/f6f8e5db

Branch: refs/heads/master
Commit: f6f8e5dbedfed0d10bf483b636abac87d90925b3
Parents: 74c24ad
Author: Benedikt Ritter <brit...@apache.org>
Authored: Thu Sep 6 15:41:44 2018 +0200
Committer: Benedikt Ritter <brit...@apache.org>
Committed: Thu Sep 6 15:41:44 2018 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ValidateTest.java  | 399 ++++++++++++-------
 1 file changed, 265 insertions(+), 134 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/f6f8e5db/src/test/java/org/apache/commons/lang3/ValidateTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java 
b/src/test/java/org/apache/commons/lang3/ValidateTest.java
index c2de05d..c6a0898 100644
--- a/src/test/java/org/apache/commons/lang3/ValidateTest.java
+++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java
@@ -25,7 +25,6 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.util.AbstractList;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -788,152 +787,284 @@ class ValidateTest {
         }
     }
 
-    //-----------------------------------------------------------------------
-    //-----------------------------------------------------------------------
-    @Test
-    void testValidIndex_withMessage_array() {
-        final Object[] array = new Object[2];
-        Validate.validIndex(array, 0, "Broken: ");
-        Validate.validIndex(array, 1, "Broken: ");
-        try {
-            Validate.validIndex(array, -1, "Broken: ");
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("Broken: ", ex.getMessage());
-        }
-        try {
-            Validate.validIndex(array, 2, "Broken: ");
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("Broken: ", ex.getMessage());
-        }
+    @Nested
+    class ValidIndex {
 
-        final String[] strArray = new String[]{"Hi"};
-        final String[] test = Validate.noNullElements(strArray, "Message");
-        assertSame(strArray, test);
-    }
+        @Nested
+        class WithArray {
 
-    @Test
-    void testValidIndex_array() {
-        final Object[] array = new Object[2];
-        Validate.validIndex(array, 0);
-        Validate.validIndex(array, 1);
-        try {
-            Validate.validIndex(array, -1);
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("The validated array index is invalid: -1", 
ex.getMessage());
-        }
-        try {
-            Validate.validIndex(array, 2);
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("The validated array index is invalid: 2", 
ex.getMessage());
-        }
+            @Nested
+            class WithoutMessage {
 
-        final String[] strArray = new String[]{"Hi"};
-        final String[] test = Validate.noNullElements(strArray);
-        assertSame(strArray, test);
-    }
+                @Test
+                void shouldNotThrowExceptionForValidIndex() {
+                    Validate.validIndex(new String[]{"a"}, 0);
+                }
 
-    //-----------------------------------------------------------------------
-    //-----------------------------------------------------------------------
-    @Test
-    void testValidIndex_withMessage_collection() {
-        final Collection<String> coll = new ArrayList<>();
-        coll.add(null);
-        coll.add(null);
-        Validate.validIndex(coll, 0, "Broken: ");
-        Validate.validIndex(coll, 1, "Broken: ");
-        try {
-            Validate.validIndex(coll, -1, "Broken: ");
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("Broken: ", ex.getMessage());
-        }
-        try {
-            Validate.validIndex(coll, 2, "Broken: ");
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("Broken: ", ex.getMessage());
-        }
+                @Test
+                void shouldReturnSameInstance() {
+                    final String[] array = {"a"};
+                    final String[] result = Validate.validIndex(array, 0);
 
-        final List<String> strColl = Arrays.asList("Hi");
-        final List<String> test = Validate.validIndex(strColl, 0, "Message");
-        assertSame(strColl, test);
-    }
+                    assertSame(array, result);
+                }
 
-    @Test
-    void testValidIndex_collection() {
-        final Collection<String> coll = new ArrayList<>();
-        coll.add(null);
-        coll.add(null);
-        Validate.validIndex(coll, 0);
-        Validate.validIndex(coll, 1);
-        try {
-            Validate.validIndex(coll, -1);
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("The validated collection index is invalid: -1", 
ex.getMessage());
-        }
-        try {
-            Validate.validIndex(coll, 2);
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("The validated collection index is invalid: 2", 
ex.getMessage());
-        }
+                @Test
+                void shouldThrowNullPointerExceptionWithDefaultForNullArray() {
+                    final NullPointerException ex = assertThrows(
+                            NullPointerException.class,
+                            () -> Validate.validIndex((Object[]) null, 1));
 
-        final List<String> strColl = Arrays.asList("Hi");
-        final List<String> test = Validate.validIndex(strColl, 0);
-        assertSame(strColl, test);
-    }
+                    assertEquals("The validated object is null", 
ex.getMessage());
+                }
 
-    //-----------------------------------------------------------------------
-    //-----------------------------------------------------------------------
-    @Test
-    void testValidIndex_withMessage_charSequence() {
-        final CharSequence str = "Hi";
-        Validate.validIndex(str, 0, "Broken: ");
-        Validate.validIndex(str, 1, "Broken: ");
-        try {
-            Validate.validIndex(str, -1, "Broken: ");
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("Broken: ", ex.getMessage());
-        }
-        try {
-            Validate.validIndex(str, 2, "Broken: ");
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("Broken: ", ex.getMessage());
-        }
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex(new String[]{"a"}, -1));
 
-        final String input = "Hi";
-        final String test = Validate.validIndex(input, 0, "Message");
-        assertSame(input, test);
-    }
+                    assertEquals("The validated array index is invalid: -1", 
ex.getMessage());
+                }
 
-    @Test
-    void testValidIndex_charSequence() {
-        final CharSequence str = "Hi";
-        Validate.validIndex(str, 0);
-        Validate.validIndex(str, 1);
-        try {
-            Validate.validIndex(str, -1);
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("The validated character sequence index is invalid: 
-1", ex.getMessage());
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex(new String[]{"a"}, 1));
+
+                    assertEquals("The validated array index is invalid: 1", 
ex.getMessage());
+                }
+            }
+
+            @Nested
+            class WithMessage {
+
+                @Test
+                void shouldNotThrowExceptionForValidIndex() {
+                    Validate.validIndex(new String[]{"a"}, 0, "MSG");
+                }
+
+                @Test
+                void shouldReturnSameInstance() {
+                    final String[] array = {"a"};
+                    final String[] result = Validate.validIndex(array, 0, 
"MSG");
+
+                    assertSame(array, result);
+                }
+
+                @Test
+                void 
shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() {
+                    final NullPointerException ex = assertThrows(
+                            NullPointerException.class,
+                            () -> Validate.validIndex((Object[]) null, 1, 
"MSG"));
+
+                    assertEquals("The validated object is null", 
ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex(new String[]{"a"}, -1, 
"MSG"));
+
+                    assertEquals("MSG", ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex(new String[]{"a"}, 1, 
"MSG"));
+
+                    assertEquals("MSG", ex.getMessage());
+                }
+            }
         }
-        try {
-            Validate.validIndex(str, 2);
-            fail("Expecting IndexOutOfBoundsException");
-        } catch (final IndexOutOfBoundsException ex) {
-            assertEquals("The validated character sequence index is invalid: 
2", ex.getMessage());
+
+        @Nested
+        class WithCollection {
+
+            @Nested
+            class WithoutMessage {
+
+                @Test
+                void shouldNotThrowExceptionForValidIndex() {
+                    Validate.validIndex(Collections.singleton("a"), 0);
+                }
+
+                @Test
+                void shouldReturnSameInstance() {
+                    final Set<String> col = Collections.singleton("a");
+                    final Set<String> result = Validate.validIndex(col, 0);
+
+                    assertSame(col, result);
+                }
+
+                @Test
+                void 
shouldThrowNullPointerExceptionWithDefaultForNullCollection() {
+                    final NullPointerException ex = assertThrows(
+                            NullPointerException.class,
+                            () -> Validate.validIndex((Collection<?>) null, 
1));
+
+                    assertEquals("The validated object is null", 
ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> 
Validate.validIndex(Collections.singleton("a"), -1));
+
+                    assertEquals("The validated collection index is invalid: 
-1", ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> 
Validate.validIndex(Collections.singleton("a"), 1));
+
+                    assertEquals("The validated collection index is invalid: 
1", ex.getMessage());
+                }
+            }
+
+            @Nested
+            class WithMessage {
+
+                @Test
+                void shouldNotThrowExceptionForValidIndex() {
+                    Validate.validIndex(Collections.singleton("a"), 0, "MSG");
+                }
+
+                @Test
+                void shouldReturnSameInstance() {
+                    final Set<String> col = Collections.singleton("a");
+                    final Set<String> result = Validate.validIndex(col, 0, 
"MSG");
+
+                    assertSame(col, result);
+                }
+
+                @Test
+                void 
shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() {
+                    final NullPointerException ex = assertThrows(
+                            NullPointerException.class,
+                            () -> Validate.validIndex((Collection<?>) null, 1, 
"MSG"));
+
+                    assertEquals("The validated object is null", 
ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> 
Validate.validIndex(Collections.singleton("a"), -1, "MSG"));
+
+                    assertEquals("MSG", ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> 
Validate.validIndex(Collections.singleton("a"), 1, "MSG"));
+
+                    assertEquals("MSG", ex.getMessage());
+                }
+            }
         }
 
-        final String input = "Hi";
-        final String test = Validate.validIndex(input, 0);
-        assertSame(input, test);
+        @Nested
+        class WithCharSequence {
+
+            @Nested
+            class WithoutMessage {
+
+                @Test
+                void shouldNotThrowExceptionForValidIndex() {
+                    Validate.validIndex("a", 0);
+                }
+
+                @Test
+                void shouldReturnSameInstance() {
+                    final String str = "a";
+                    final String result = Validate.validIndex(str, 0);
+
+                    assertSame(str, result);
+                }
+
+                @Test
+                void shouldThrowNullPointerExceptionWithDefaultForNullString() 
{
+                    final NullPointerException ex = assertThrows(
+                            NullPointerException.class,
+                            () -> Validate.validIndex((String) null, 1));
+
+                    assertEquals("The validated object is null", 
ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForNegativeIndex() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex("a", -1));
+
+                    assertEquals("The validated character sequence index is 
invalid: -1", ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithDefaultMessageForIndexOutOfBounds() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex("a", 1));
+
+                    assertEquals("The validated character sequence index is 
invalid: 1", ex.getMessage());
+                }
+            }
+
+            @Nested
+            class WithMessage {
+
+                @Test
+                void shouldNotThrowExceptionForValidIndex() {
+                    Validate.validIndex("a", 0, "MSG");
+                }
+
+                @Test
+                void shouldReturnSameInstance() {
+                    final String str = "a";
+                    final String result = Validate.validIndex(str, 0, "MSG");
+
+                    assertSame(str, result);
+                }
+
+                @Test
+                void 
shouldThrowNullPointerExceptionWithDefaultMessageForNullStr() {
+                    final NullPointerException ex = assertThrows(
+                            NullPointerException.class,
+                            () -> Validate.validIndex((String) null, 1, 
"MSG"));
+
+                    assertEquals("The validated object is null", 
ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForNegativeIndex() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex("a", -1, "MSG"));
+
+                    assertEquals("MSG", ex.getMessage());
+                }
+
+                @Test
+                void 
shouldThrowIndexOutOfBoundsExceptionWithGivenMessageForIndexOutOfBounds() {
+                    final IndexOutOfBoundsException ex = assertThrows(
+                            IndexOutOfBoundsException.class,
+                            () -> Validate.validIndex("a", 1, "MSG"));
+
+                    assertEquals("MSG", ex.getMessage());
+                }
+            }
+        }
     }
 
     @Test

Reply via email to