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    *      https://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;
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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.Random;
27  
28  import org.apache.commons.validator.routines.checkdigit.CheckDigit;
29  import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link ISSNValidator}.
34   */
35  class ISSNValidatorTest {
36  
37      private static final ISSNValidator VALIDATOR = ISSNValidator.getInstance();
38  
39      private static final String[] VALID_FORMAT = { "ISSN 0317-8471", "1050-124X", "ISSN 1562-6865", "1063-7710", "1748-7188", "ISSN 0264-2875", "1750-0095",
40              "1188-1534", "1911-1479", "ISSN 1911-1460", "0001-6772", "1365-201X", "0264-3596", "1144-875X", };
41  
42      private static final String[] INVALID_FORMAT = { "", // empty
43              "   ", // empty
44              "ISBN 0317-8471", // wrong prefix
45              "'1050-124X", // leading garbage
46              "ISSN1562-6865", // missing separator
47              "10637710", // missing separator
48              "1748-7188'", // trailing garbage
49              "ISSN  0264-2875", // extra space
50              "1750 0095", // invalid separator
51              "1188_1534", // invalid separator
52              "1911-1478", // invalid checkdigit
53      };
54  
55      /**
56       * Test Invalid EAN-13 ISSN prefix codes Test Input length
57       */
58      @Test
59      void testConversionErrors() {
60          final String input1 = "9780072129519";
61          assertThrows(IllegalArgumentException.class, () -> VALIDATOR.extractFromEAN13(input1), "Expected IllegalArgumentException for '" + input1 + "'");
62  
63          final String input2 = "9791090636071";
64          assertThrows(IllegalArgumentException.class, () -> VALIDATOR.extractFromEAN13(input2), "Expected IllegalArgumentException for '" + input2 + "'");
65  
66          final String input3 = "03178471";
67          assertThrows(IllegalArgumentException.class, () -> VALIDATOR.extractFromEAN13(input3), "Expected IllegalArgumentException for '" + input3 + "'");
68      }
69  
70      /**
71       * Test Invalid ISSN codes
72       */
73      @Test
74      void testInvalid() {
75          for (final String f : INVALID_FORMAT) {
76              assertFalse(VALIDATOR.isValid(f), f);
77          }
78      }
79  
80      /**
81       * Test valid EAN-13 ISSN codes and extract the ISSN
82       */
83      @Test
84      void testIsValidExtract() {
85          assertEquals("12345679", VALIDATOR.extractFromEAN13("9771234567003"));
86          assertEquals("00014664", VALIDATOR.extractFromEAN13("9770001466006"));
87          assertEquals("03178471", VALIDATOR.extractFromEAN13("9770317847001"));
88          assertEquals("1144875X", VALIDATOR.extractFromEAN13("9771144875007"));
89      }
90  
91      /**
92       * Test isValid() ISSN codes
93       */
94      @Test
95      void testIsValidISSN() {
96          for (final String f : VALID_FORMAT) {
97              assertTrue(VALIDATOR.isValid(f), f);
98          }
99      }
100 
101     /**
102      * Test isValid() ISSN codes and convert them
103      */
104     @Test
105     void testIsValidISSNConvert() {
106         final CheckDigit ean13cd = EAN13CheckDigit.EAN13_CHECK_DIGIT;
107         final Random r = new Random();
108         for (final String f : VALID_FORMAT) {
109             final String suffix = String.format("%02d", r.nextInt(100));
110             final String ean13 = VALIDATOR.convertToEAN13(f, suffix);
111             assertTrue(ean13cd.isValid(ean13), ean13);
112         }
113         // internet samples
114         assertEquals(VALIDATOR.convertToEAN13("1144-875X", "00"), "9771144875007");
115         assertEquals(VALIDATOR.convertToEAN13("0264-3596", "00"), "9770264359008");
116         assertEquals(VALIDATOR.convertToEAN13("1234-5679", "00"), "9771234567003");
117     }
118 
119     @Test
120     void testIsValidISSNConvertNull() {
121         assertNull(VALIDATOR.convertToEAN13(null, "00"));
122     }
123 
124     @Test
125     void testIsValidISSNConvertSuffix() {
126         assertThrows(IllegalArgumentException.class, () -> VALIDATOR.convertToEAN13(null, null));
127         assertThrows(IllegalArgumentException.class, () -> VALIDATOR.convertToEAN13(null, ""));
128         assertThrows(IllegalArgumentException.class, () -> VALIDATOR.convertToEAN13(null, "0"));
129         assertThrows(IllegalArgumentException.class, () -> VALIDATOR.convertToEAN13(null, "A"));
130         assertThrows(IllegalArgumentException.class, () -> VALIDATOR.convertToEAN13(null, "AA"));
131         assertThrows(IllegalArgumentException.class, () -> VALIDATOR.convertToEAN13(null, "999"));
132     }
133 
134     /**
135      * Test null values
136      */
137     @Test
138     void testNull() {
139         assertFalse(VALIDATOR.isValid(null), "isValid");
140     }
141 
142     /**
143      * Test Invalid EAN-13 ISSN codes
144      */
145     @Test
146     void testValidCheckDigitEan13() {
147         assertNull(VALIDATOR.extractFromEAN13("9771234567001"));
148         assertNull(VALIDATOR.extractFromEAN13("9771234567002"));
149         assertNotNull(VALIDATOR.extractFromEAN13("9771234567003")); // valid check digit
150         assertNull(VALIDATOR.extractFromEAN13("9771234567004"));
151         assertNull(VALIDATOR.extractFromEAN13("9771234567005"));
152         assertNull(VALIDATOR.extractFromEAN13("9771234567006"));
153         assertNull(VALIDATOR.extractFromEAN13("9771234567007"));
154         assertNull(VALIDATOR.extractFromEAN13("9771234567008"));
155         assertNull(VALIDATOR.extractFromEAN13("9771234567009"));
156         assertNull(VALIDATOR.extractFromEAN13("9771234567000"));
157     }
158 }