EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.core.step.item]

COVERAGE SUMMARY FOR SOURCE FILE [TransactionInterceptorValidator.java]

nameclass, %method, %block, %line, %
TransactionInterceptorValidator.java100% (1/1)100% (3/3)91%  (62/68)90%  (19/21)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TransactionInterceptorValidator100% (1/1)100% (3/3)91%  (62/68)90%  (19/21)
countTransactionInterceptors (Object): int 100% (1/1)86%  (36/42)86%  (12/14)
TransactionInterceptorValidator (int): void 100% (1/1)100% (11/11)100% (4/4)
validate (Object): void 100% (1/1)100% (15/15)100% (3/3)

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.core.step.item;
17 
18import org.apache.commons.logging.Log;
19import org.apache.commons.logging.LogFactory;
20import org.springframework.aop.Advisor;
21import org.springframework.aop.framework.Advised;
22import org.springframework.batch.item.validator.ValidationException;
23import org.springframework.batch.item.validator.Validator;
24import org.springframework.transaction.interceptor.TransactionInterceptor;
25import org.springframework.util.Assert;
26 
27/**
28 * Simple validator for internal use only (package private to make it testable).
29 * Asserts that its argument has no more than the specified number of
30 * transaction interceptors in its advice chain.
31 * 
32 * @author Dave Syer
33 * 
34 */
35class TransactionInterceptorValidator implements Validator {
36 
37        protected Log logger = LogFactory.getLog(getClass());
38 
39        private final int maxCount;
40 
41        /**
42         * @param maxCount
43         */
44        public TransactionInterceptorValidator(int maxCount) {
45                super();
46                this.maxCount = maxCount;
47        }
48 
49        /**
50         * Assert that the object passed in has no more than the maximum number of
51         * transaction interceptors in its advice chain.
52         * 
53         * @see org.springframework.batch.item.validator.Validator#validate(java.lang.Object)
54         */
55        public void validate(Object value) throws ValidationException {
56                Assert.notNull(value, "JobRepository must be provided");
57                Assert.state(countTransactionInterceptors(value) <= maxCount,
58                                "JobRepository has more than one transaction interceptor.  "
59                                                + "Do not declare a separate transaction advice if using the JobRepositoryFactoryBean.");
60        }
61 
62        /**
63         * @param object an Object, possibly advised
64         * @return the number of transaction interceptors in the advice chain
65         */
66        private int countTransactionInterceptors(Object object) {
67                int count = 0;
68                Object target = object;
69                while (target instanceof Advised) {
70                        Advised advised = (Advised) target;
71                        Advisor[] interceptors = advised.getAdvisors();
72                        for (int i = 0; i < interceptors.length; i++) {
73                                if (interceptors[i].getAdvice() instanceof TransactionInterceptor) {
74                                        count++;
75                                }
76                        }
77                        try {
78                                target = advised.getTargetSource().getTarget();
79                        }
80                        catch (Exception e) {
81                                logger.warn("Target could not be obtained from advised instance.", e);
82                        }
83                }
84                return count;
85        }
86 
87}

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