Convert tests for Validate.noNullElements 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/ad97f202 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/ad97f202 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/ad97f202 Branch: refs/heads/master Commit: ad97f2020253c787e2978093976c3b6716955e32 Parents: c0779f4 Author: Benedikt Ritter <brit...@apache.org> Authored: Thu Sep 6 15:17:13 2018 +0200 Committer: Benedikt Ritter <brit...@apache.org> Committed: Thu Sep 6 15:17:13 2018 +0200 ---------------------------------------------------------------------- .../org/apache/commons/lang3/ValidateTest.java | 234 +++++++++++-------- 1 file changed, 142 insertions(+), 92 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ad97f202/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 e747fcf..9c00f66 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -626,106 +626,156 @@ class ValidateTest { } } - //----------------------------------------------------------------------- - //----------------------------------------------------------------------- - @Test - void testNoNullElementsArray1() { - String[] array = new String[]{"a", "b"}; - Validate.noNullElements(array); - try { - Validate.noNullElements((Object[]) null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - assertEquals("The validated object is null", ex.getMessage()); - } - array[1] = null; - try { - Validate.noNullElements(array); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("The validated array contains null element at index: 1", ex.getMessage()); - } + @Nested + class NoNullElements { - array = new String[]{"a", "b"}; - final String[] test = Validate.noNullElements(array); - assertSame(array, test); - } + @Nested + class WithArray { - //----------------------------------------------------------------------- - @Test - void testNoNullElementsArray2() { - String[] array = new String[]{"a", "b"}; - Validate.noNullElements(array, "MSG"); - try { - Validate.noNullElements((Object[]) null, "MSG"); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - assertEquals("The validated object is null", ex.getMessage()); - } - array[1] = null; - try { - Validate.noNullElements(array, "MSG"); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("MSG", ex.getMessage()); - } + @Nested + class WithoutMessage { - array = new String[]{"a", "b"}; - final String[] test = Validate.noNullElements(array, "Message"); - assertSame(array, test); - } + @Test + void shouldNotThrowExceptionForNonEmptyArray() { + Validate.noNullElements(new String[]{"a", "b"}); + } - //----------------------------------------------------------------------- - //----------------------------------------------------------------------- - @Test - void testNoNullElementsCollection1() { - final List<String> coll = new ArrayList<>(); - coll.add("a"); - coll.add("b"); - Validate.noNullElements(coll); - try { - Validate.noNullElements((Collection<?>) null); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - assertEquals("The validated object is null", ex.getMessage()); - } - coll.set(1, null); - try { - Validate.noNullElements(coll); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); - } + @Test + void shouldReturnSameInstance() { + final String[] array = {"a", "b"}; + final String[] result = Validate.noNullElements(array); - coll.set(1, "b"); - final List<String> test = Validate.noNullElements(coll); - assertSame(coll, test); - } + assertSame(array, result); + } - //----------------------------------------------------------------------- - @Test - void testNoNullElementsCollection2() { - final List<String> coll = new ArrayList<>(); - coll.add("a"); - coll.add("b"); - Validate.noNullElements(coll, "MSG"); - try { - Validate.noNullElements((Collection<?>) null, "MSG"); - fail("Expecting NullPointerException"); - } catch (final NullPointerException ex) { - assertEquals("The validated object is null", ex.getMessage()); - } - coll.set(1, null); - try { - Validate.noNullElements(coll, "MSG"); - fail("Expecting IllegalArgumentException"); - } catch (final IllegalArgumentException ex) { - assertEquals("MSG", ex.getMessage()); + @Test + void shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() { + final NullPointerException ex = assertThrows( + NullPointerException.class, + () -> Validate.noNullElements((Object[]) null)); + + assertEquals("The validated object is null", ex.getMessage()); + } + + @Test + void shouldThrowIllegalArgumentExceptionWithDefaultMessageForArrayWithNullElement() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.noNullElements(new String[]{"a", null})); + + assertEquals("The validated array contains null element at index: 1", ex.getMessage()); + } + } + + @Nested + class WithMessage { + + @Test + void shouldNotThrowExceptionForNonEmptyArray() { + Validate.noNullElements(new String[]{"a", "b"}, "MSG"); + } + + @Test + void shouldReturnSameInstance() { + final String[] array = {"a", "b"}; + final String[] result = Validate.noNullElements(array, "MSG"); + + assertSame(array, result); + } + + @Test + void shouldThrowNullPointerExceptionWithDefaultMessageForNullArray() { + final NullPointerException ex = assertThrows( + NullPointerException.class, + () -> Validate.noNullElements((Object[]) null, "MSG")); + + assertEquals("The validated object is null", ex.getMessage()); + } + + @Test + void shouldThrowIllegalArgumentExceptionWithGivenMessageForArrayWithNullElement() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.noNullElements(new String[]{"a", null}, "MSG")); + + assertEquals("MSG", ex.getMessage()); + } + } } - coll.set(1, "b"); - final List<String> test = Validate.noNullElements(coll, "Message"); - assertSame(coll, test); + @Nested + class WithCollection { + + @Nested + class WithoutMessage { + + @Test + void shouldNotThrowExceptionForNonEmptyCollection() { + Validate.noNullElements(Collections.singleton("a")); + } + + @Test + void shouldReturnSameInstance() { + Set<String> col = Collections.singleton("a"); + final Set<String> result = Validate.noNullElements(col); + + assertSame(col, result); + } + + @Test + void shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() { + final NullPointerException ex = assertThrows( + NullPointerException.class, + () -> Validate.noNullElements((Collection<?>) null)); + + assertEquals("The validated object is null", ex.getMessage()); + } + + @Test + void shouldThrowIllegalArgumentExceptionWithDefaultMessageForCollectionWithNullElement() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.noNullElements(Collections.singleton(null))); + + assertEquals("The validated collection contains null element at index: 0", ex.getMessage()); + } + } + + @Nested + class WithMessage { + + @Test + void shouldNotThrowExceptionForNonEmptyCollection() { + Validate.noNullElements(Collections.singleton("a"), "MSG"); + } + + @Test + void shouldReturnSameInstance() { + Set<String> col = Collections.singleton("a"); + final Set<String> result = Validate.noNullElements(col, "MSG"); + + assertSame(col, result); + } + + @Test + void shouldThrowNullPointerExceptionWithDefaultMessageForNullCollection() { + final NullPointerException ex = assertThrows( + NullPointerException.class, + () -> Validate.noNullElements((Collection<?>) null, "MSG")); + + assertEquals("The validated object is null", ex.getMessage()); + } + + @Test + void shouldThrowIllegalArgumentExceptionWithGivenMessageForCollectionWithNullElement() { + final IllegalArgumentException ex = assertThrows( + IllegalArgumentException.class, + () -> Validate.noNullElements(Collections.singleton(null), "MSG")); + + assertEquals("MSG", ex.getMessage()); + } + } + } } //-----------------------------------------------------------------------