Skip to content

Commit 99ac926

Browse files
authored
Merge pull request #434 from sahvx655-wq/isbn-convert-validate
Reject invalid ISBN-10 in convertToISBN13
2 parents 5533108 + 8c3a826 commit 99ac926

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎src/main/java/org/apache/commons/validator/routines/ISBNValidator.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ public String convertToISBN13(final String isbn10) {
179179
if (input.length() != ISBN_10_LEN) {
180180
throw new IllegalArgumentException("Invalid length " + input.length() + " for '" + input + "'");
181181
}
182+
if (!isbn10Validator.isValid(input)) {
183+
return null;
184+
}
182185

183186
// Calculate the new ISBN-13 code (drop the original checkdigit)
184187
String isbn13 = "978" + input.substring(0, ISBN_10_LEN - 1);

‎src/test/java/org/apache/commons/validator/routines/ISBNValidatorTest.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,22 @@ void testConversionErrors() {
9696
final String input3 = "";
9797
assertThrows(IllegalArgumentException.class, () -> validator.convertToISBN13(input3), "Expected IllegalArgumentException for '" + input3 + "'");
9898

99-
final String input4 = "X234567890";
99+
final String input4 = "1234567890X";
100100
assertThrows(IllegalArgumentException.class, () -> validator.convertToISBN13(input4), "Expected IllegalArgumentException for '" + input4 + "'");
101101
}
102102

103+
/**
104+
* Test method for {@link org.apache.commons.validator.routines.ISBNValidator#convertToISBN13(java.lang.String)}.
105+
*/
106+
@Test
107+
void testConvertToISBN13Invalid() {
108+
final ISBNValidator validator = ISBNValidator.getInstance();
109+
// Correct length, but not a valid ISBN-10.
110+
assertNull(validator.convertToISBN13("1234567890"), "wrong check digit");
111+
assertNull(validator.convertToISBN13("020163385Y"), "invalid check character");
112+
assertNull(validator.convertToISBN13("X234567890"), "non-digit body");
113+
}
114+
103115
/**
104116
* Test Invalid ISBN-10 codes
105117
*/

0 commit comments

Comments
 (0)