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