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