1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 = { "",
44 " ",
45 "ISBN 0317-8471",
46 "'1050-124X",
47 "ISSN1562-6865",
48 "10637710",
49 "1748-7188'",
50 "ISSN 0264-2875",
51 "1750 0095",
52 "1188_1534",
53 "1911-1478",
54 };
55
56
57
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
68 }
69 try {
70 input = "9791090636071";
71 VALIDATOR.extractFromEAN13(input);
72 fail("Expected IllegalArgumentException for '" + input + "'");
73 } catch (final IllegalArgumentException e) {
74
75 }
76 try {
77 input = "03178471";
78 VALIDATOR.extractFromEAN13(input);
79 fail("Expected IllegalArgumentException for '" + input + "'");
80 } catch (final IllegalArgumentException e) {
81
82 }
83 }
84
85
86
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
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
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
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
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
151
152 @Test
153 public void testNull() {
154 assertFalse(VALIDATOR.isValid(null), "isValid");
155 }
156
157
158
159
160 @Test
161 public void testValidCheckDigitEan13() {
162 assertNull(VALIDATOR.extractFromEAN13("9771234567001"));
163 assertNull(VALIDATOR.extractFromEAN13("9771234567002"));
164 assertNotNull(VALIDATOR.extractFromEAN13("9771234567003"));
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 }