View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines.checkdigit;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * ISBN-10/ISBN-13 Check Digit Test.
28   */
29  public class ISBNCheckDigitTest extends AbstractCheckDigitTest {
30  
31      /**
32       * Sets up routine & valid codes.
33       */
34      @BeforeEach
35      protected void setUp() {
36          routine = ISBNCheckDigit.ISBN_CHECK_DIGIT;
37          valid = new String[] { "9780072129519", "9780764558313", "1930110995", "020163385X", "1590596277", // ISBN-10 Ubuntu Book
38                  "9781590596272" // ISBN-13 Ubuntu Book
39          };
40          missingMessage = "ISBN Code is missing";
41          zeroSum = "000000000000";
42      }
43  
44      /**
45       * Sets up routine & valid codes.
46       */
47      @Test
48      public void testInvalidLength() {
49          assertFalse(routine.isValid("123456789"), "isValid() Lth 9 ");
50          assertFalse(routine.isValid("12345678901"), "isValid() Lth 11");
51          assertFalse(routine.isValid("123456789012"), "isValid() Lth 12");
52          assertFalse(routine.isValid("12345678901234"), "isValid() Lth 14");
53  
54          try {
55              routine.calculate("12345678");
56              fail("calculate() Lth 8 - expected exception");
57          } catch (final Exception e) {
58              assertEquals(e.getMessage(), "Invalid ISBN Length = 8", "calculate() Lth 8");
59          }
60  
61          try {
62              routine.calculate("1234567890");
63              fail("calculate() Lth 10 - expected exception");
64          } catch (final Exception e) {
65              assertEquals("Invalid ISBN Length = 10", e.getMessage(), "calculate() Lth 10");
66          }
67  
68          try {
69              routine.calculate("12345678901");
70              fail("calculate() Lth 11 - expected exception");
71          } catch (final Exception e) {
72              assertEquals("Invalid ISBN Length = 11", e.getMessage(), "calculate() Lth 11");
73          }
74  
75          try {
76              routine.calculate("1234567890123");
77              fail("calculate() Lth 13 - expected exception");
78          } catch (final Exception e) {
79              assertEquals("Invalid ISBN Length = 13", e.getMessage(), "calculate() Lth 13");
80          }
81      }
82  
83  }