EMMA Coverage Report (generated Tue May 06 07:28:24 PDT 2008)
[all classes][org.springframework.batch.item.validator]

COVERAGE SUMMARY FOR SOURCE FILE [SpringValidator.java]

nameclass, %method, %block, %line, %
SpringValidator.java100% (1/1)83%  (5/6)95%  (101/106)90%  (19/21)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SpringValidator100% (1/1)83%  (5/6)95%  (101/106)90%  (19/21)
afterPropertiesSet (): void 0%   (0/1)0%   (0/5)0%   (0/2)
SpringValidator (): void 100% (1/1)100% (3/3)100% (1/1)
appendCollection (Collection, StringBuffer): void 100% (1/1)100% (18/18)100% (5/5)
errorsToString (Errors): String 100% (1/1)100% (17/17)100% (4/4)
setValidator (Validator): void 100% (1/1)100% (4/4)100% (2/2)
validate (Object): void 100% (1/1)100% (59/59)100% (7/7)

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 */
16 
17package org.springframework.batch.item.validator;
18 
19import java.util.Collection;
20import java.util.Iterator;
21 
22import org.springframework.beans.factory.InitializingBean;
23import org.springframework.util.Assert;
24import org.springframework.validation.BeanPropertyBindingResult;
25import org.springframework.validation.Errors;
26 
27/**
28 * Adapts the {@link org.springframework.validation.Validator} interface to
29 * {@link org.springframework.batch.item.validator.Validator}.
30 * 
31 * @author Tomas Slanina
32 * @author Robert Kasanicky
33 */
34public class SpringValidator implements Validator, InitializingBean {
35 
36        private org.springframework.validation.Validator validator;
37 
38        /**
39         * @see Validator#validate(Object)
40         */
41        public void validate(Object item) throws ValidationException {
42 
43                if (!validator.supports(item.getClass())) {
44                        throw new ValidationException("Validation failed for " + item + ": " + item.getClass().getName()
45                                        + " class is not supported by validator.");
46                }
47 
48                BeanPropertyBindingResult errors = new BeanPropertyBindingResult(item, "item");
49 
50                validator.validate(item, errors);
51 
52                if (errors.hasErrors()) {
53                        throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors));
54                }
55        }
56 
57        /**
58         * @return string of field errors followed by global errors.
59         */
60        private String errorsToString(Errors errors) {
61                StringBuffer builder = new StringBuffer();
62 
63                appendCollection(errors.getFieldErrors(), builder);
64                appendCollection(errors.getGlobalErrors(), builder);
65 
66                return builder.toString();
67        }
68 
69        /**
70         * Append the string representation of elements of the collection (separated
71         * by new lines) to the given StringBuilder.
72         */
73        private void appendCollection(Collection collection, StringBuffer builder) {
74                for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
75                        builder.append("\n");
76                        builder.append(iterator.next().toString());
77                }
78        }
79 
80        public void setValidator(org.springframework.validation.Validator validator) {
81                this.validator = validator;
82        }
83 
84        public void afterPropertiesSet() throws Exception {
85                Assert.notNull(validator, "validator must be set");
86 
87        }
88}

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