1 package org.nuiton.validator.bean.list;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import com.google.common.collect.ArrayListMultimap;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.junit.After;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.nuiton.validator.NuitonValidatorScope;
32 import org.nuiton.validator.bean.SimpleBean;
33
34 import java.util.Arrays;
35 import java.util.List;
36
37
38
39
40
41
42
43 public class BeanListValidatorTest {
44
45
46 private static final Log log =
47 LogFactory.getLog(BeanListValidatorTest.class);
48
49 protected BeanListValidator<SimpleBean> validator;
50
51 protected SimpleBean bean;
52
53 protected SimpleBean bean2;
54
55 BeanValidatorListenerImpl fatalListener;
56
57 BeanValidatorListenerImpl errorListener;
58
59 BeanValidatorListenerImpl warningListener;
60
61 BeanValidatorListenerImpl infoListener;
62
63 @Before
64 public void setUp() {
65
66 bean = new SimpleBean();
67 bean2 = new SimpleBean();
68 }
69
70 protected void prepareValidator(String context) {
71
72 validator = BeanListValidator.newValidator(SimpleBean.class, context);
73
74 validator.addBeanListValidatorListener(fatalListener = new BeanValidatorListenerImpl(NuitonValidatorScope.FATAL));
75 validator.addBeanListValidatorListener(errorListener = new BeanValidatorListenerImpl(NuitonValidatorScope.ERROR));
76 validator.addBeanListValidatorListener(warningListener = new BeanValidatorListenerImpl(NuitonValidatorScope.WARNING));
77 validator.addBeanListValidatorListener(infoListener = new BeanValidatorListenerImpl(NuitonValidatorScope.INFO));
78 }
79
80 @After
81 public void tearDown() {
82 bean = null;
83 bean2 = null;
84 if (validator != null) {
85 validator.removeAllBeans();
86 validator = null;
87 }
88 }
89
90 private static final String STRING_VALUE_FATAL = "stringValue.fatal";
91
92 private static final String STRING_VALUE_ERROR = "stringValue.error";
93
94 private static final String STRING_VALUE_WARNING = "stringValue.warning";
95
96 private static final String INT_VALUE_FATAL = "intValue.fatal";
97
98 private static final String INT_VALUE_ERROR = "intValue.error";
99
100 private static final String INT_VALUE_INFO = "intValue.info";
101
102 @Test(expected = IllegalStateException.class)
103 public void testValidateWithBad() {
104
105
106
107 prepareValidator("marchepo");
108 }
109
110 @Test
111 public void addBean() {
112 prepareValidator(null);
113
114 assertMessages(fatalListener, bean);
115 assertMessages(errorListener, bean);
116 assertMessages(warningListener, bean);
117 assertMessages(infoListener, bean);
118
119 validator.addBean(bean);
120
121 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
122 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
123 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
124 assertMessages(infoListener, bean, INT_VALUE_INFO);
125
126 validator.addBean(bean2);
127
128 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
129 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
130 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
131 assertMessages(infoListener, bean, INT_VALUE_INFO);
132
133 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
134 assertMessages(errorListener, bean2, STRING_VALUE_ERROR, INT_VALUE_ERROR);
135 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
136 assertMessages(infoListener, bean2, INT_VALUE_INFO);
137
138 }
139
140 @Test
141 public void addAllBean() {
142 prepareValidator(null);
143
144 assertMessages(fatalListener, bean);
145 assertMessages(errorListener, bean);
146 assertMessages(warningListener, bean);
147 assertMessages(infoListener, bean);
148
149 assertMessages(fatalListener, bean2);
150 assertMessages(errorListener, bean2);
151 assertMessages(warningListener, bean2);
152 assertMessages(infoListener, bean2);
153
154 validator.addAllBeans(Arrays.asList(bean, bean2));
155
156 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
157 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
158 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
159 assertMessages(infoListener, bean, INT_VALUE_INFO);
160
161 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
162 assertMessages(errorListener, bean2, STRING_VALUE_ERROR, INT_VALUE_ERROR);
163 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
164 assertMessages(infoListener, bean2, INT_VALUE_INFO);
165
166 }
167
168 @Test
169 public void removeBean() {
170 prepareValidator(null);
171
172 assertMessages(fatalListener, bean);
173 assertMessages(errorListener, bean);
174 assertMessages(warningListener, bean);
175 assertMessages(infoListener, bean);
176
177 assertMessages(fatalListener, bean2);
178 assertMessages(errorListener, bean2);
179 assertMessages(warningListener, bean2);
180 assertMessages(infoListener, bean2);
181
182 validator.addBean(bean);
183 validator.addBean(bean2);
184
185 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
186 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
187 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
188 assertMessages(infoListener, bean, INT_VALUE_INFO);
189
190 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
191 assertMessages(errorListener, bean2, STRING_VALUE_ERROR, INT_VALUE_ERROR);
192 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
193 assertMessages(infoListener, bean2, INT_VALUE_INFO);
194
195 validator.removeBean(bean);
196
197 assertMessages(fatalListener, bean);
198 assertMessages(errorListener, bean);
199 assertMessages(warningListener, bean);
200 assertMessages(infoListener, bean);
201
202 validator.removeBean(bean2);
203
204 assertMessages(fatalListener, bean2);
205 assertMessages(errorListener, bean2);
206 assertMessages(warningListener, bean2);
207 assertMessages(infoListener, bean2);
208 }
209
210 @Test
211 public void removeAllBeans() {
212 prepareValidator(null);
213
214 assertMessages(fatalListener, bean);
215 assertMessages(errorListener, bean);
216 assertMessages(warningListener, bean);
217 assertMessages(infoListener, bean);
218
219 assertMessages(fatalListener, bean2);
220 assertMessages(errorListener, bean2);
221 assertMessages(warningListener, bean2);
222 assertMessages(infoListener, bean2);
223
224 validator.addBean(bean);
225 validator.addBean(bean2);
226
227 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
228 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
229 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
230 assertMessages(infoListener, bean, INT_VALUE_INFO);
231
232 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
233 assertMessages(errorListener, bean2, STRING_VALUE_ERROR, INT_VALUE_ERROR);
234 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
235 assertMessages(infoListener, bean2, INT_VALUE_INFO);
236
237 validator.removeAllBeans(Arrays.asList(bean, bean2));
238
239 assertMessages(fatalListener, bean);
240 assertMessages(errorListener, bean);
241 assertMessages(warningListener, bean);
242 assertMessages(infoListener, bean);
243
244 assertMessages(fatalListener, bean2);
245 assertMessages(errorListener, bean2);
246 assertMessages(warningListener, bean2);
247 assertMessages(infoListener, bean2);
248 }
249
250 @Test
251 public void removeAllBeans2() {
252 prepareValidator(null);
253
254 assertMessages(fatalListener, bean);
255 assertMessages(errorListener, bean);
256 assertMessages(warningListener, bean);
257 assertMessages(infoListener, bean);
258
259 assertMessages(fatalListener, bean2);
260 assertMessages(errorListener, bean2);
261 assertMessages(warningListener, bean2);
262 assertMessages(infoListener, bean2);
263
264 validator.addBean(bean);
265 validator.addBean(bean2);
266
267 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
268 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
269 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
270 assertMessages(infoListener, bean, INT_VALUE_INFO);
271
272 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
273 assertMessages(errorListener, bean2, STRING_VALUE_ERROR, INT_VALUE_ERROR);
274 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
275 assertMessages(infoListener, bean2, INT_VALUE_INFO);
276
277 validator.removeAllBeans();
278
279 assertMessages(fatalListener, bean);
280 assertMessages(errorListener, bean);
281 assertMessages(warningListener, bean);
282 assertMessages(infoListener, bean);
283
284 assertMessages(fatalListener, bean2);
285 assertMessages(errorListener, bean2);
286 assertMessages(warningListener, bean2);
287 assertMessages(infoListener, bean2);
288 }
289
290 @Test
291 public void validate() {
292
293 prepareValidator(null);
294
295 assertMessages(fatalListener, bean);
296 assertMessages(errorListener, bean);
297 assertMessages(warningListener, bean);
298 assertMessages(infoListener, bean);
299
300 if (log.isDebugEnabled()) {
301 log.debug("- bean 1 ----------------------------------------------");
302 }
303 validator.addBean(bean);
304
305 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
306 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
307 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
308 assertMessages(infoListener, bean, INT_VALUE_INFO);
309
310 if (log.isDebugEnabled()) {
311 log.debug("- bean 1 ----------------------------------------------");
312 }
313 bean.setStringValue("one");
314
315 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
316 assertMessages(errorListener, bean, INT_VALUE_ERROR);
317 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
318 assertMessages(infoListener, bean, INT_VALUE_INFO);
319
320 if (log.isDebugEnabled()) {
321 log.debug("- bean 1 ----------------------------------------------");
322 }
323 bean.setStringValue("oneone");
324
325 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
326 assertMessages(errorListener, bean, INT_VALUE_ERROR);
327 assertMessages(warningListener, bean);
328 assertMessages(infoListener, bean, INT_VALUE_INFO);
329
330 if (log.isDebugEnabled()) {
331 log.debug("- bean 1 ----------------------------------------------");
332 }
333 bean.setIntValue(1);
334
335 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
336 assertMessages(errorListener, bean);
337 assertMessages(warningListener, bean);
338 assertMessages(infoListener, bean, INT_VALUE_INFO);
339
340 if (log.isDebugEnabled()) {
341 log.debug("- bean 1 ----------------------------------------------");
342 }
343 bean.setIntValue(10);
344
345 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
346 assertMessages(errorListener, bean);
347 assertMessages(warningListener, bean);
348 assertMessages(infoListener, bean);
349
350 if (log.isDebugEnabled()) {
351 log.debug("- bean 1 ----------------------------------------------");
352 }
353
354 bean.setStringValue(null);
355 bean.setIntValue(0);
356
357 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
358 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
359 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
360 assertMessages(infoListener, bean, INT_VALUE_INFO);
361
362
363 if (log.isDebugEnabled()) {
364 log.debug("- bean 1 ----------------------------------------------");
365 }
366
367 bean.setStringValue("5");
368 bean.setIntValue(5);
369 assertMessages(fatalListener, bean);
370 assertMessages(errorListener, bean);
371 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
372 assertMessages(infoListener, bean, INT_VALUE_INFO);
373
374 if (log.isDebugEnabled()) {
375 log.debug("- bean 2 ----------------------------------------------");
376 }
377 validator.addBean(bean2);
378
379 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
380 assertMessages(errorListener, bean2, STRING_VALUE_ERROR, INT_VALUE_ERROR);
381 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
382 assertMessages(infoListener, bean2, INT_VALUE_INFO);
383
384 if (log.isDebugEnabled()) {
385 log.debug("- bean 2 ----------------------------------------------");
386 }
387 bean2.setStringValue("one");
388
389 assertMessages(fatalListener, bean2, STRING_VALUE_FATAL, INT_VALUE_FATAL);
390 assertMessages(errorListener, bean2, INT_VALUE_ERROR);
391 assertMessages(warningListener, bean2, STRING_VALUE_WARNING);
392 assertMessages(infoListener, bean2, INT_VALUE_INFO);
393
394 }
395
396 @Test
397 public void convert() {
398
399 prepareValidator(null);
400
401 assertMessages(errorListener, bean);
402 assertMessages(warningListener, bean);
403 assertMessages(infoListener, bean);
404
405 if (log.isDebugEnabled()) {
406 log.debug("-----------------------------------------------");
407 }
408
409 validator.addBean(bean);
410
411 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
412 assertMessages(errorListener, bean, STRING_VALUE_ERROR, INT_VALUE_ERROR);
413 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
414 assertMessages(infoListener, bean, INT_VALUE_INFO);
415
416
417 if (log.isDebugEnabled()) {
418 log.debug("-----------------------------------------------");
419 }
420
421 Object value = validator.convert(bean, "intValue", "abc", Class.class);
422
423 Assert.assertNull(value);
424
425 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
426 assertMessages(errorListener, bean, STRING_VALUE_ERROR, "error.convertor.class");
427 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
428 assertMessages(infoListener, bean, INT_VALUE_INFO);
429
430 if (log.isDebugEnabled()) {
431 log.debug("-----------------------------------------------");
432 }
433 bean.setStringValue("one");
434
435 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
436 assertMessages(errorListener, bean, "error.convertor.class");
437 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
438 assertMessages(infoListener, bean, INT_VALUE_INFO);
439
440 if (log.isDebugEnabled()) {
441 log.debug("-----------------------------------------------");
442 }
443
444 value = validator.convert(bean, "intValue", "3", Integer.class);
445
446 bean.setIntValue((Integer) value);
447
448 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
449 assertMessages(errorListener, bean);
450 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
451 assertMessages(infoListener, bean, INT_VALUE_INFO);
452
453 if (log.isDebugEnabled()) {
454 log.debug("-----------------------------------------------");
455 }
456
457 bean.setIntValue(-1);
458 assertMessages(fatalListener, bean, STRING_VALUE_FATAL, INT_VALUE_FATAL);
459 assertMessages(errorListener, bean, INT_VALUE_ERROR);
460 assertMessages(warningListener, bean, STRING_VALUE_WARNING);
461 assertMessages(infoListener, bean, INT_VALUE_INFO);
462
463 if (log.isDebugEnabled()) {
464 log.debug("-----------------------------------------------");
465 }
466 }
467
468 void assertMessages(BeanValidatorListenerImpl listener,
469 Object bean,
470 String... expected) {
471 List<String> actual = listener.getMessages(bean);
472 Assert.assertEquals(" shoudl have " +
473 Arrays.toString(expected) + " but had " + actual,
474 expected.length, actual.size());
475 for (String m : expected) {
476 Assert.assertEquals("could not find " + m + " in " + actual,
477 true, actual.contains(m));
478 }
479 }
480
481 class BeanValidatorListenerImpl implements BeanListValidatorListener {
482
483 private final NuitonValidatorScope scope;
484
485 private final ArrayListMultimap<Object, String> messages;
486
487 public BeanValidatorListenerImpl(NuitonValidatorScope scope) {
488 this.scope = scope;
489 messages = ArrayListMultimap.create();
490 }
491
492
493 public ArrayListMultimap<Object, String> getMessages() {
494 return messages;
495 }
496
497 public List<String> getMessages(Object bean) {
498 return messages.get(bean);
499 }
500
501
502 @Override
503 public void onFieldChanged(BeanListValidatorEvent event) {
504 if (scope != event.getScope()) {
505 return;
506 }
507 Object bean = event.getBean();
508 String[] messagesToDelete = event.getMessagesToDelete();
509 if (messagesToDelete != null && messagesToDelete.length > 0) {
510 if (log.isDebugEnabled()) {
511 log.debug(scope + "[" + bean + "] messages to delete : " + Arrays.toString(messagesToDelete));
512 }
513 for (String m : messagesToDelete) {
514 messages.remove(bean, m);
515 }
516 }
517 String[] messagesToAdd = event.getMessagesToAdd();
518 if (messagesToAdd != null && messagesToAdd.length > 0) {
519 if (log.isDebugEnabled()) {
520 log.debug(scope + "[" + bean + "] messages to add : " + Arrays.toString(messagesToAdd));
521 }
522 messages.putAll(bean, Arrays.asList(messagesToAdd));
523 }
524 }
525 }
526 }