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