This is an automated email from the ASF dual-hosted git repository. ebourg pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-validator.git
commit 55fde6597217eabbbf38b5be5ad1724b5ed70b9a Author: Emmanuel Bourg <ebo...@apache.org> AuthorDate: Mon Nov 18 13:25:20 2024 +0100 Use assertThrows() to test the expected exceptions --- .../apache/commons/validator/ExceptionTest.java | 17 +++----- .../commons/validator/ValidatorResourcesTest.java | 14 ++---- .../validator/routines/CalendarValidatorTest.java | 10 ++--- .../routines/CreditCardValidatorTest.java | 9 +--- .../validator/routines/ISBNValidatorTest.java | 44 ++++++------------- .../validator/routines/ISSNValidatorTest.java | 31 ++++--------- .../validator/routines/RegexValidatorTest.java | 51 ++++++---------------- .../checkdigit/AbstractCheckDigitTest.java | 25 +++-------- .../routines/checkdigit/ISBNCheckDigitTest.java | 34 ++++----------- 9 files changed, 63 insertions(+), 172 deletions(-) diff --git a/src/test/java/org/apache/commons/validator/ExceptionTest.java b/src/test/java/org/apache/commons/validator/ExceptionTest.java index e66789c2..e3ac90f8 100644 --- a/src/test/java/org/apache/commons/validator/ExceptionTest.java +++ b/src/test/java/org/apache/commons/validator/ExceptionTest.java @@ -17,6 +17,7 @@ package org.apache.commons.validator; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; @@ -80,12 +81,8 @@ public class ExceptionTest extends AbstractCommonTest { } // This will be true in Validator 2.0 -// try { -// validator.validate(); -// fail("ValidatorException should occur here!"); -// } catch (ValidatorException expected) { -// assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage())); -// } +// Exception expected = assertThrows(ValidatorException.class, validator::validate); +// assertTrue("CHECKED-EXCEPTION".equals(expected.getMessage())); } /** @@ -135,11 +132,7 @@ public class ExceptionTest extends AbstractCommonTest { validator.setParameter(Validator.BEAN_PARAM, info); // Get results of the validation which can throw ValidatorException - try { - validator.validate(); - fail("ValidatorException should occur here!"); - } catch (final ValidatorException expected) { - assertEquals("VALIDATOR-EXCEPTION", expected.getMessage()); - } + Exception expected = assertThrows(ValidatorException.class, validator::validate); + assertEquals("VALIDATOR-EXCEPTION", expected.getMessage()); } } diff --git a/src/test/java/org/apache/commons/validator/ValidatorResourcesTest.java b/src/test/java/org/apache/commons/validator/ValidatorResourcesTest.java index e9421dc2..f308d73e 100644 --- a/src/test/java/org/apache/commons/validator/ValidatorResourcesTest.java +++ b/src/test/java/org/apache/commons/validator/ValidatorResourcesTest.java @@ -16,7 +16,7 @@ */ package org.apache.commons.validator; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.InputStream; @@ -31,16 +31,8 @@ public class ValidatorResourcesTest { * Test null Input Stream for Validator Resources. */ @Test - public void testNullInputStream() throws Exception { - - try { - new ValidatorResources((InputStream) null); - fail("Expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - // expected result - // System.out.println("Exception: " + e); - } - + public void testNullInputStream() { + assertThrows(IllegalArgumentException.class, () -> new ValidatorResources((InputStream) null)); } } diff --git a/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java index 08fa93b2..ebbcd25a 100644 --- a/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/CalendarValidatorTest.java @@ -21,8 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; 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 static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.text.DateFormat; import java.text.Format; @@ -219,12 +219,8 @@ public class CalendarValidatorTest extends AbstractCalendarValidatorTest { assertEquals(1, calValidator.compareYears(value, cal20041231), "year GT"); // -1 year // invalid compare - try { - calValidator.compare(value, value, -1); - fail("Invalid Compare field - expected IllegalArgumentException to be thrown"); - } catch (final IllegalArgumentException e) { - assertEquals(e.getMessage(), "Invalid field: -1", "check message"); - } + Exception e = assertThrows(IllegalArgumentException.class, () -> calValidator.compare(value, value, -1), "Invalid Compare field"); + assertEquals(e.getMessage(), "Invalid field: -1", "check message"); } /** diff --git a/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java index 20cd45d4..69580605 100644 --- a/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java @@ -19,8 +19,8 @@ package org.apache.commons.validator.routines; 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 static org.junit.jupiter.api.Assertions.fail; import org.apache.commons.validator.routines.CreditCardValidator.CreditCardRange; import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit; @@ -155,12 +155,7 @@ public class CreditCardValidatorTest { assertFalse(ccv.isValid(ERROR_MASTERCARD)); assertFalse(ccv.isValid(ERROR_DISCOVER)); - try { - new CreditCardValidator((CodeValidator[]) null); - fail("Expected IllegalArgumentException"); - } catch (final IllegalArgumentException iae) { - // expected result - } + assertThrows(IllegalArgumentException.class, () -> new CreditCardValidator((CodeValidator[]) null)); } /** diff --git a/src/test/java/org/apache/commons/validator/routines/ISBNValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/ISBNValidatorTest.java index 0a662252..35512f03 100644 --- a/src/test/java/org/apache/commons/validator/routines/ISBNValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/ISBNValidatorTest.java @@ -19,8 +19,8 @@ package org.apache.commons.validator.routines; 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 static org.junit.jupiter.api.Assertions.fail; import java.util.regex.Pattern; @@ -86,36 +86,18 @@ public class ISBNValidatorTest { */ @Test public void testConversionErrors() { - final ISBNValidator validator = ISBNValidator.getInstance(); - String input = null; - try { - input = "123456789 "; - validator.convertToISBN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } - try { - input = "12345678901"; - validator.convertToISBN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } - try { - input = ""; - validator.convertToISBN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } - try { - input = "X234567890"; - validator.convertToISBN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } + ISBNValidator validator = ISBNValidator.getInstance(); + String input1 = "123456789 "; + assertThrows(IllegalArgumentException.class, () -> validator.convertToISBN13(input1), "Expected IllegalArgumentException for '" + input1 + "'"); + + String input2 = "12345678901"; + assertThrows(IllegalArgumentException.class, () -> validator.convertToISBN13(input2), "Expected IllegalArgumentException for '" + input2 + "'"); + + String input3 = ""; + assertThrows(IllegalArgumentException.class, () -> validator.convertToISBN13(input3), "Expected IllegalArgumentException for '" + input3 + "'"); + + String input4 = "X234567890"; + assertThrows(IllegalArgumentException.class, () -> validator.convertToISBN13(input4), "Expected IllegalArgumentException for '" + input4 + "'"); } /** diff --git a/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java index f539dcd9..6d09f5d6 100644 --- a/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/ISSNValidatorTest.java @@ -22,7 +22,6 @@ 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 static org.junit.jupiter.api.Assertions.fail; import java.util.Random; @@ -58,28 +57,14 @@ public class ISSNValidatorTest { */ @Test public void testConversionErrors() { - String input = null; - try { - input = "9780072129519"; - VALIDATOR.extractFromEAN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } - try { - input = "9791090636071"; - VALIDATOR.extractFromEAN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } - try { - input = "03178471"; - VALIDATOR.extractFromEAN13(input); - fail("Expected IllegalArgumentException for '" + input + "'"); - } catch (final IllegalArgumentException e) { - // expected result - } + String input1 = "9780072129519"; + assertThrows(IllegalArgumentException.class, () -> VALIDATOR.extractFromEAN13(input1), "Expected IllegalArgumentException for '" + input1 + "'"); + + String input2 = "9791090636071"; + assertThrows(IllegalArgumentException.class, () -> VALIDATOR.extractFromEAN13(input2), "Expected IllegalArgumentException for '" + input2 + "'"); + + String input3 = "03178471"; + assertThrows(IllegalArgumentException.class, () -> VALIDATOR.extractFromEAN13(input3), "Expected IllegalArgumentException for '" + input3 + "'"); } /** diff --git a/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java index 810829ff..8ab10f22 100644 --- a/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; 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 static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -104,54 +105,28 @@ public class RegexValidatorTest { public void testMissingRegex() { // Single Regular Expression - null - try { - new RegexValidator((String) null); - fail("Single Null - expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Regular expression[0] is missing", e.getMessage(), "Single Null"); - } + Exception e = assertThrows(IllegalArgumentException.class, () -> new RegexValidator((String) null), "Single Null"); + assertEquals("Regular expression[0] is missing", e.getMessage(), "Single Null"); // Single Regular Expression - Zero Length - try { - new RegexValidator(""); - fail("Single Zero Length - expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Regular expression[0] is missing", e.getMessage(), "Single Zero Length"); - } + e = assertThrows(IllegalArgumentException.class, () -> new RegexValidator(""), "Single Zero Length"); + assertEquals("Regular expression[0] is missing", e.getMessage(), "Single Zero Length"); // Multiple Regular Expression - Null array - try { - new RegexValidator((String[]) null); - fail("Null Array - expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Regular expressions are missing", e.getMessage(), "Null Array"); - } + e = assertThrows(IllegalArgumentException.class, () -> new RegexValidator((String[]) null), "Null Array"); + assertEquals("Regular expressions are missing", e.getMessage(), "Null Array"); // Multiple Regular Expression - Zero Length array - try { - new RegexValidator(); - fail("Zero Length Array - expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Regular expressions are missing", e.getMessage(), "Zero Length Array"); - } + e = assertThrows(IllegalArgumentException.class, () -> new RegexValidator(), "Zero Length Array"); + assertEquals("Regular expressions are missing", e.getMessage(), "Zero Length Array"); // Multiple Regular Expression - Array has Null - String[] expressions = { "ABC", null }; - try { - new RegexValidator(expressions); - fail("Array has Null - expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Regular expression[1] is missing", e.getMessage(), "Array has Null"); - } + e = assertThrows(IllegalArgumentException.class, () -> new RegexValidator(new String[]{"ABC", null}), "Array has Null"); + assertEquals("Regular expression[1] is missing", e.getMessage(), "Array has Null"); // Multiple Regular Expression - Array has Zero Length - expressions = new String[] { "", "ABC" }; - try { - new RegexValidator(expressions); - fail("Array has Zero Length - expected IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - assertEquals("Regular expression[0] is missing", e.getMessage(), "Array has Zero Length"); - } + e = assertThrows(IllegalArgumentException.class, () -> new RegexValidator(new String[]{"", "ABC"}), "Array has Zero Length"); + assertEquals("Regular expression[0] is missing", e.getMessage(), "Array has Zero Length"); } /** diff --git a/src/test/java/org/apache/commons/validator/routines/checkdigit/AbstractCheckDigitTest.java b/src/test/java/org/apache/commons/validator/routines/checkdigit/AbstractCheckDigitTest.java index ae7fcf19..85de68af 100644 --- a/src/test/java/org/apache/commons/validator/routines/checkdigit/AbstractCheckDigitTest.java +++ b/src/test/java/org/apache/commons/validator/routines/checkdigit/AbstractCheckDigitTest.java @@ -19,6 +19,7 @@ package org.apache.commons.validator.routines.checkdigit; 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.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -263,20 +264,12 @@ public abstract class AbstractCheckDigitTest { assertFalse(routine.isValid("9"), "isValid() Length 1"); // calculate() null - try { - routine.calculate(null); - fail("calculate() Null - expected exception"); - } catch (final Exception e) { - assertEquals(missingMessage, e.getMessage(), "calculate() Null"); - } + Exception e = assertThrows(Exception.class, () -> routine.calculate(null), "calculate() Null"); + assertEquals(missingMessage, e.getMessage(), "calculate() Null"); // calculate() zero length - try { - routine.calculate(""); - fail("calculate() Zero Length - expected exception"); - } catch (final Exception e) { - assertEquals(missingMessage, e.getMessage(), "calculate() Zero Length"); - } + e = assertThrows(Exception.class, () -> routine.calculate(""), "calculate() Zero Length"); + assertEquals(missingMessage, e.getMessage(), "calculate() Zero Length"); } /** @@ -311,12 +304,8 @@ public abstract class AbstractCheckDigitTest { @Test public void testZeroSum() { assertFalse(routine.isValid(zeroSum), "isValid() Zero Sum"); - try { - routine.calculate(zeroSum); - fail("Zero Sum - expected exception"); - } catch (final Exception e) { - assertEquals("Invalid code, sum is zero", e.getMessage(), "isValid() Zero Sum"); - } + Exception e = assertThrows(Exception.class, () -> routine.calculate(zeroSum), "Zero Sum"); + assertEquals("Invalid code, sum is zero", e.getMessage(), "isValid() Zero Sum"); } } diff --git a/src/test/java/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java b/src/test/java/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java index 8d8760c2..de954703 100644 --- a/src/test/java/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java +++ b/src/test/java/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java @@ -18,7 +18,7 @@ package org.apache.commons.validator.routines.checkdigit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -51,33 +51,17 @@ public class ISBNCheckDigitTest extends AbstractCheckDigitTest { assertFalse(routine.isValid("123456789012"), "isValid() Lth 12"); assertFalse(routine.isValid("12345678901234"), "isValid() Lth 14"); - try { - routine.calculate("12345678"); - fail("calculate() Lth 8 - expected exception"); - } catch (final Exception e) { - assertEquals(e.getMessage(), "Invalid ISBN Length = 8", "calculate() Lth 8"); - } + Exception e = assertThrows(CheckDigitException.class, () -> routine.calculate("12345678"), "calculate() Lth 8"); + assertEquals(e.getMessage(), "Invalid ISBN Length = 8", "calculate() Lth 8"); - try { - routine.calculate("1234567890"); - fail("calculate() Lth 10 - expected exception"); - } catch (final Exception e) { - assertEquals("Invalid ISBN Length = 10", e.getMessage(), "calculate() Lth 10"); - } + e = assertThrows(CheckDigitException.class, () -> routine.calculate("1234567890"), "calculate() Lth 10"); + assertEquals("Invalid ISBN Length = 10", e.getMessage(), "calculate() Lth 10"); - try { - routine.calculate("12345678901"); - fail("calculate() Lth 11 - expected exception"); - } catch (final Exception e) { - assertEquals("Invalid ISBN Length = 11", e.getMessage(), "calculate() Lth 11"); - } + e = assertThrows(CheckDigitException.class, () -> routine.calculate("12345678901"), "calculate() Lth 11"); + assertEquals("Invalid ISBN Length = 11", e.getMessage(), "calculate() Lth 11"); - try { - routine.calculate("1234567890123"); - fail("calculate() Lth 13 - expected exception"); - } catch (final Exception e) { - assertEquals("Invalid ISBN Length = 13", e.getMessage(), "calculate() Lth 13"); - } + e = assertThrows(CheckDigitException.class, () -> routine.calculate("1234567890123"), "calculate() Lth 13"); + assertEquals("Invalid ISBN Length = 13", e.getMessage(), "calculate() Lth 13"); } }