1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator.routines.checkdigit;
18
19 import java.io.BufferedReader;
20 import java.io.InputStreamReader;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.junit.Assert;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 public class IBANCheckDigitTest extends AbstractCheckDigitTest {
32
33 public IBANCheckDigitTest() {
34 checkDigitLth = 2;
35 }
36
37
38
39
40
41
42
43 @Override
44 protected String checkDigit(final String code) {
45 if (code == null || code.length() <= checkDigitLth) {
46 return "";
47 }
48 return code.substring(2, 4);
49 }
50
51
52
53
54
55
56
57 @Override
58 protected String[] createInvalidCodes(final String[] codes) {
59 final List<String> list = new ArrayList<>();
60
61
62 for (final String code2 : codes) {
63 final String code = removeCheckDigit(code2);
64 final String check = checkDigit(code2);
65 for (int j = 2; j <= 98; j++) {
66 final String curr = j > 9 ? "" + j : "0" + j;
67 if (!curr.equals(check)) {
68 list.add(code.substring(0, 2) + curr + code.substring(4));
69 }
70 }
71 }
72
73 return list.toArray(new String[0]);
74 }
75
76
77
78
79
80
81
82 @Override
83 protected String removeCheckDigit(final String code) {
84 return code.substring(0, 2) + "00" + code.substring(4);
85 }
86
87
88
89
90 @BeforeEach
91 protected void setUp() {
92 routine = IBANCheckDigit.IBAN_CHECK_DIGIT;
93 valid = new String[] { "AD1200012030200359100100",
94 "AE070331234567890123456",
95 "AL47212110090000000235698741",
96 "AT611904300234573201",
97 "AZ21NABZ00000000137010001944",
98 "BA391290079401028494",
99 "BE62510007547061",
100 "BE68539007547034",
101 "BG80BNBG96611020345678",
102 "BH67BMAG00001299123456",
103 "BI4210000100010000332045181",
104 "BR1800000000141455123924100C2",
105 "BY13NBRB3600900000002Z00AB00",
106 "CH3900700115201849173",
107 "CH9300762011623852957",
108 "CR05015202001026284066",
109 "CY17002001280000001200527600",
110 "CZ6508000000192000145399",
111 "DE89370400440532013000",
112 "DJ2110002010010409943020008",
113 "DK5000400440116243",
114 "DO28BAGR00000001212453611324",
115 "EE382200221020145685",
116 "ES8023100001180000012345",
117 "FI2112345600000785",
118 "FO6264600001631634",
119 "FR1420041010050500013M02606",
120 "GB29NWBK60161331926819",
121 "GI75NWBK000000007099453",
122 "GL8964710001000206",
123 "GR1601101250000000012300695",
124 "GT82TRAJ01020000001210029690",
125 "HR1210010051863000160",
126 "HU42117730161111101800000000",
127 "IE29AIBK93115212345678",
128 "IL620108000000099999999",
129 "IQ98NBIQ850123456789012",
130 "IS140159260076545510730339",
131 "IT60X0542811101000000123456",
132 "JO94CBJO0010000000000131000302",
133 "KW81CBKU0000000000001234560101",
134 "KZ86125KZT5004100100",
135 "LB62099900000001001901229114",
136 "LC55HEMM000100010012001200023015",
137 "LI21088100002324013AA",
138 "LT121000011101001000",
139 "LU280019400644750000",
140 "LV80BANK0000435195001",
141 "MC5811222000010123456789030",
142 "MD24AG000225100013104168",
143 "ME25505000012345678951",
144 "MK07250120000058984",
145 "MR1300020001010000123456753",
146 "MT84MALT011000012345MTLCAST001S",
147 "MU17BOMM0101101030300200000MUR",
148 "NL39RABO0300065264",
149 "NL91ABNA0417164300",
150 "NO9386011117947",
151 "PK36SCBL0000001123456702",
152 "PL27114020040000300201355387",
153 "PL60102010260000042270201111",
154 "PS92PALS000000000400123456702",
155 "PT50000201231234567890154",
156 "QA58DOHB00001234567890ABCDEFG",
157 "RO49AAAA1B31007593840000",
158 "RS35260005601001611379",
159 "RU0204452560040702810412345678901",
160 "SA0380000000608010167519",
161 "SC18SSCB11010000000000001497USD",
162 "SD8811123456789012",
163 "SE3550000000054910000003",
164 "SD2129010501234001",
165 "SI56191000000123438",
166 "SK3112000000198742637541",
167 "SM86U0322509800000000270100",
168 "ST68000100010051845310112",
169 "SV62CENR00000000000000700025",
170 "TL380080012345678910157",
171 "TN5910006035183598478831",
172 "TR330006100519786457841326",
173 "UA213223130000026007233566001",
174 "VA59001123000012345678",
175 "VG96VPVG0000012345678901",
176 "XK051212012345678906",
177
178
179
180
181
182
183
184 "AA0200000000053", "AA9700000000089", "AA9800000000071", "ZZ02ZZZZZZZZZZZZZZZZZZZZZZZZZ04", "ZZ97ZZZZZZZZZZZZZZZZZZZZZZZZZ40",
185 "ZZ98ZZZZZZZZZZZZZZZZZZZZZZZZZ22", };
186
187
188
189
190 invalid = new String[] { "510007+47061BE63", "IE01AIBK93118702569045", "AA0000000000089", "AA9900000000053", };
191 zeroSum = null;
192 missingMessage = "Invalid Code length=0";
193
194 }
195
196 @Test
197 public void testOther() throws Exception {
198 try (BufferedReader rdr = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("IBANtests.txt"), "ASCII"))) {
199 String line;
200 while ((line = rdr.readLine()) != null) {
201 if (!line.startsWith("#") && !line.isEmpty()) {
202 if (line.startsWith("-")) {
203 line = line.substring(1);
204 Assert.assertFalse(line, routine.isValid(line.replace(" ", "")));
205 } else {
206 Assert.assertTrue(line, routine.isValid(line.replace(" ", "")));
207 }
208 }
209 }
210 }
211 }
212
213
214
215
216 @Override
217 @Test
218 public void testZeroSum() {
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 }
234 }