Skip to content

Commit

Permalink
Use assertThrows() to test the expected exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ebourg committed Nov 18, 2024
1 parent dfd4a9e commit 55fde65
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 172 deletions.
17 changes: 5 additions & 12 deletions src/test/java/org/apache/commons/validator/ExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,12 +81,8 @@ public void testCheckedException() {
}

// 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()));
}

/**
Expand Down Expand Up @@ -135,11 +132,7 @@ public void testValidatorException() {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
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;
Expand Down Expand Up @@ -219,12 +219,8 @@ public void testCompare() {
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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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;
Expand Down Expand Up @@ -155,12 +155,7 @@ public void testArrayConstructor() {
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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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;

Expand Down Expand Up @@ -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 + "'");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;

Expand Down Expand Up @@ -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 + "'");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;

Expand Down Expand Up @@ -104,54 +105,28 @@ public void testGetPatterns() {
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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -263,20 +264,12 @@ public void testMissingCode() {
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");
}

/**
Expand Down Expand Up @@ -311,12 +304,8 @@ public void testSerialization() {
@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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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;
Expand Down Expand Up @@ -51,33 +51,17 @@ public void testInvalidLength() {
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");
}

}

0 comments on commit 55fde65

Please sign in to comment.