EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.item.validator]

COVERAGE SUMMARY FOR SOURCE FILE [ValidatingItemProcessor.java]

nameclass, %method, %block, %line, %
ValidatingItemProcessor.java100% (1/1)50%  (3/6)65%  (28/43)66%  (12.6/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ValidatingItemProcessor100% (1/1)50%  (3/6)65%  (28/43)66%  (12.6/19)
ValidatingItemProcessor (): void 0%   (0/1)0%   (0/6)0%   (0/3)
afterPropertiesSet (): void 0%   (0/1)0%   (0/5)0%   (0/2)
setValidator (Validator): void 0%   (0/1)0%   (0/4)0%   (0/2)
ValidatingItemProcessor (Validator): void 100% (1/1)100% (9/9)100% (4/4)
process (Object): Object 100% (1/1)100% (15/15)100% (7/7)
setFilter (boolean): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 * Copyright 2006-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.batch.item.validator;
17 
18import org.springframework.batch.item.ItemProcessor;
19import org.springframework.beans.factory.InitializingBean;
20import org.springframework.util.Assert;
21 
22/**
23 * Simple implementation of {@link ItemProcessor} that validates input and
24 * returns it without modifications. Should the given {@link Validator} throw a
25 * {@link ValidationException} this processor will re-throw it to indicate the
26 * item should be skipped, unless {@link #setFilter(boolean)} is set to
27 * <code>true</code>, in which case <code>null</code> will be returned to
28 * indicate the item should be filtered.
29 * 
30 * @author Robert Kasanicky
31 */
32public class ValidatingItemProcessor<T> implements ItemProcessor<T, T>, InitializingBean {
33 
34        private Validator<? super T> validator;
35 
36        private boolean filter = false;
37 
38        /**
39         * Default constructor
40         */
41        public ValidatingItemProcessor() {
42        }
43 
44        /**
45         * Creates a ValidatingItemProcessor based on the given Validator.
46         */
47        public ValidatingItemProcessor(Validator<? super T> validator) {
48                this.validator = validator;
49        }
50 
51        /**
52         * Set the validator used to validate each item.
53         * 
54         * @param validator
55         */
56        public void setValidator(Validator<? super T> validator) {
57                this.validator = validator;
58        }
59 
60        /**
61         * Should the processor filter invalid records instead of skipping them?
62         * 
63         * @param filter
64         */
65        public void setFilter(boolean filter) {
66                this.filter = filter;
67        }
68 
69        /**
70         * Validate the item and return it unmodified
71         * 
72         * @return the input item
73         * @throws ValidationException if validation fails
74         */
75    @Override
76        public T process(T item) throws ValidationException {
77                try {
78                        validator.validate(item);
79                }
80                catch (ValidationException e) {
81                        if (filter) {
82                                return null; // filter the item
83                        }
84                        else {
85                                throw e; // skip the item
86                        }
87                }
88                return item;
89        }
90 
91    @Override
92        public void afterPropertiesSet() throws Exception {
93                Assert.notNull(validator, "Validator must not be null.");
94        }
95 
96}

[all classes][org.springframework.batch.item.validator]
EMMA 2.0.5312 (C) Vladimir Roubtsov