1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.validator.routines.checkdigit;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
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.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.junit.jupiter.api.Assertions.fail;
26 import static org.junit.jupiter.api.Assumptions.assumeTrue;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.Serializable;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.junit.jupiter.api.AfterEach;
39 import org.junit.jupiter.api.Test;
40
41
42
43
44 public abstract class AbstractCheckDigitTest {
45
46 private static final String POSSIBLE_CHECK_DIGITS = "0123456789 ABCDEFHIJKLMNOPQRSTUVWXYZ\tabcdefghijklmnopqrstuvwxyz!@£$%^&*()_+";
47
48
49 protected Log log = LogFactory.getLog(getClass());
50
51
52 protected int checkDigitLth = 1;
53
54
55 protected CheckDigit routine;
56
57
58
59
60
61
62
63
64 protected String[] valid;
65
66
67
68
69
70
71
72
73
74 protected String[] invalid = { "12345678A" };
75
76
77 protected String zeroSum = "0000000000";
78
79
80 protected String missingMessage = "Code is missing";
81
82
83
84
85
86
87
88 protected String checkDigit(final String code) {
89 if (code == null || code.length() <= checkDigitLth) {
90 return "";
91 }
92 final int start = code.length() - checkDigitLth;
93 return code.substring(start);
94 }
95
96
97
98
99
100
101
102 protected String[] createInvalidCodes(final String[] codes) {
103 final List<String> list = new ArrayList<>();
104
105 for (final String fullCode : codes) {
106 final String code = removeCheckDigit(fullCode);
107 final String check = checkDigit(fullCode);
108 for (int j = 0; j < POSSIBLE_CHECK_DIGITS.length(); j++) {
109 final String curr = POSSIBLE_CHECK_DIGITS.substring(j, j + 1);
110 if (!curr.equals(check)) {
111 list.add(code + curr);
112 }
113 }
114 }
115 return list.toArray(new String[0]);
116 }
117
118
119
120
121
122
123
124 protected String removeCheckDigit(final String code) {
125 if (code == null || code.length() <= checkDigitLth) {
126 return null;
127 }
128 return code.substring(0, code.length() - checkDigitLth);
129 }
130
131
132
133
134 @AfterEach
135 protected void tearDown() {
136 valid = null;
137 routine = null;
138 }
139
140
141
142
143 @Test
144 void testCalculateInvalid() {
145 if (log.isDebugEnabled()) {
146 log.debug("testCalculateInvalid() for " + routine.getClass().getName());
147 }
148
149 for (int i = 0; i < invalid.length; i++) {
150 try {
151 final String code = invalid[i];
152 if (log.isDebugEnabled()) {
153 log.debug(" " + i + " Testing Invalid Check Digit, Code=[" + code + "]");
154 }
155 final String expected = checkDigit(code);
156 final String codeWithNoCheckDigit = removeCheckDigit(code);
157 if (codeWithNoCheckDigit == null) {
158 throw new CheckDigitException("Invalid Code=[" + code + "]");
159 }
160 final String actual = routine.calculate(codeWithNoCheckDigit);
161
162 if (expected.equals(actual)) {
163 fail("Expected mismatch for " + code + " expected " + expected + " actual " + actual);
164 }
165 } catch (final CheckDigitException e) {
166
167
168
169
170 assertTrue(e.getMessage().startsWith("Invalid "), "Invalid Character[" + i + "]=" + e.getMessage());
171 }
172 }
173 }
174
175
176
177
178 @Test
179 void testCalculateValid() {
180 if (log.isDebugEnabled()) {
181 log.debug("testCalculateValid() for " + routine.getClass().getName());
182 }
183
184 for (int i = 0; i < valid.length; i++) {
185 final String code = removeCheckDigit(valid[i]);
186 final String expected = checkDigit(valid[i]);
187 try {
188 if (log.isDebugEnabled()) {
189 log.debug(" " + i + " Testing Valid Check Digit, Code=[" + code + "] expected=[" + expected + "]");
190 }
191 assertEquals(expected, routine.calculate(code), "valid[" + i + "]: " + valid[i]);
192 } catch (final Exception e) {
193 fail("valid[" + i + "]=" + valid[i] + " threw " + e);
194 }
195 }
196 }
197
198
199
200
201 @Test
202 void testIsValidFalse() {
203 if (log.isDebugEnabled()) {
204 log.debug("testIsValidFalse() for " + routine.getClass().getName());
205 }
206
207 for (int i = 0; i < invalid.length; i++) {
208 if (log.isDebugEnabled()) {
209 log.debug(" " + i + " Testing Invalid Code=[" + invalid[i] + "]");
210 }
211 assertFalse(routine.isValid(invalid[i]), "invalid[" + i + "]: " + invalid[i]);
212 }
213
214 final String[] invalidCheckDigits = createInvalidCodes(valid);
215 for (int i = 0; i < invalidCheckDigits.length; i++) {
216 if (log.isDebugEnabled()) {
217 log.debug(" " + i + " Testing Invalid Check Digit, Code=[" + invalidCheckDigits[i] + "]");
218 }
219 assertFalse(routine.isValid(invalidCheckDigits[i]), "invalid check digit[" + i + "]: " + invalidCheckDigits[i]);
220 }
221 }
222
223
224
225
226 @Test
227 void testIsValidTrue() {
228 if (log.isDebugEnabled()) {
229 log.debug("testIsValidTrue() for " + routine.getClass().getName());
230 }
231
232 for (int i = 0; i < valid.length; i++) {
233 if (log.isDebugEnabled()) {
234 log.debug(" " + i + " Testing Valid Code=[" + valid[i] + "]");
235 }
236 assertTrue(routine.isValid(valid[i]), "valid[" + i + "]: " + valid[i]);
237 }
238 }
239
240
241
242
243 @Test
244 void testMissingCode() {
245
246 assertFalse(routine.isValid(null), "isValid() Null");
247
248 assertFalse(routine.isValid(""), "isValid() Zero Length");
249
250
251 assertFalse(routine.isValid("9"), "isValid() Length 1");
252
253 Exception e = assertThrows(Exception.class, () -> routine.calculate(null), "calculate() Null");
254 assertEquals(missingMessage, e.getMessage(), "calculate() Null");
255
256 e = assertThrows(Exception.class, () -> routine.calculate(""), "calculate() Zero Length");
257 assertEquals(missingMessage, e.getMessage(), "calculate() Zero Length");
258 }
259
260
261
262
263 @Test
264 void testSerialization() {
265 assumeTrue(routine instanceof Serializable);
266
267 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
268 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
269 oos.writeObject(routine);
270 oos.flush();
271 } catch (final Exception e) {
272 fail(routine.getClass().getName() + " error during serialization: " + e);
273 }
274
275 Object result = null;
276 try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
277 final ObjectInputStream ois = new ObjectInputStream(bais);
278 result = ois.readObject();
279 } catch (final Exception e) {
280 fail(routine.getClass().getName() + " error during deserialization: " + e);
281 }
282 assertNotNull(result);
283 }
284
285
286
287
288 @Test
289 void testZeroSum() {
290 assertFalse(routine.isValid(zeroSum), "isValid() Zero Sum");
291 final Exception e = assertThrows(Exception.class, () -> routine.calculate(zeroSum), "Zero Sum");
292 assertEquals("Invalid code, sum is zero", e.getMessage(), "isValid() Zero Sum");
293 }
294 }