EMMA Coverage Report (generated Fri Aug 21 15:59:46 BST 2009)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [MapJobExecutionDao.java]

nameclass, %method, %block, %line, %
MapJobExecutionDao.java100% (2/2)100% (13/13)98%  (255/261)95%  (56/59)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MapJobExecutionDao$1100% (1/1)100% (2/2)93%  (26/28)88%  (7/8)
compare (JobExecution, JobExecution): int 100% (1/1)91%  (20/22)83%  (5/6)
MapJobExecutionDao$1 (MapJobExecutionDao): void 100% (1/1)100% (6/6)100% (2/2)
     
class MapJobExecutionDao100% (1/1)100% (11/11)98%  (229/233)96%  (50/52)
getLastJobExecution (JobInstance): JobExecution 100% (1/1)91%  (32/35)78%  (7/9)
saveJobExecution (JobExecution): void 100% (1/1)96%  (25/26)99%  (6/6)
<static initializer> 100% (1/1)100% (5/5)100% (3/3)
MapJobExecutionDao (): void 100% (1/1)100% (3/3)100% (1/1)
clear (): void 100% (1/1)100% (3/3)100% (2/2)
copy (JobExecution): JobExecution 100% (1/1)100% (7/7)100% (2/2)
findJobExecutions (JobInstance): List 100% (1/1)100% (34/34)100% (6/6)
findRunningJobExecutions (String): Set 100% (1/1)100% (33/33)100% (6/6)
getJobExecution (Long): JobExecution 100% (1/1)100% (6/6)100% (1/1)
synchronizeStatus (JobExecution): void 100% (1/1)100% (21/21)100% (5/5)
updateJobExecution (JobExecution): void 100% (1/1)100% (60/60)100% (12/12)

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.core.repository.dao;
18 
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.HashSet;
23import java.util.List;
24import java.util.Map;
25import java.util.Set;
26 
27import org.apache.commons.lang.SerializationUtils;
28import org.springframework.batch.core.JobExecution;
29import org.springframework.batch.core.JobInstance;
30import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
31import org.springframework.dao.OptimisticLockingFailureException;
32import org.springframework.util.Assert;
33 
34/**
35 * In-memory implementation of {@link JobExecutionDao}.
36 */
37public class MapJobExecutionDao implements JobExecutionDao {
38 
39        private static Map<Long, JobExecution> executionsById = TransactionAwareProxyFactory.createTransactionalMap();
40 
41        private static long currentId = 0;
42 
43        public static void clear() {
44                executionsById.clear();
45        }
46 
47        private static JobExecution copy(JobExecution original) {
48                JobExecution copy = (JobExecution) SerializationUtils.deserialize(SerializationUtils.serialize(original));
49                return copy;
50        }
51 
52        public void saveJobExecution(JobExecution jobExecution) {
53                Assert.isTrue(jobExecution.getId() == null);
54                Long newId = currentId++;
55                jobExecution.setId(newId);
56                jobExecution.incrementVersion();
57                executionsById.put(newId, copy(jobExecution));
58        }
59 
60        public List<JobExecution> findJobExecutions(JobInstance jobInstance) {
61                List<JobExecution> executions = new ArrayList<JobExecution>();
62                for (JobExecution exec : executionsById.values()) {
63                        if (exec.getJobInstance().equals(jobInstance)) {
64                                executions.add(copy(exec));
65                        }
66                }
67                Collections.sort(executions, new Comparator<JobExecution>() {
68 
69                        public int compare(JobExecution e1, JobExecution e2) {
70                                long result = (e1.getId() - e2.getId());
71                                if (result > 0) {
72                                        return -1;
73                                }
74                                else if (result < 0) {
75                                        return 1;
76                                }
77                                else {
78                                        return 0;
79                                }
80                        }
81                });
82                return executions;
83        }
84 
85        public void updateJobExecution(JobExecution jobExecution) {
86                Long id = jobExecution.getId();
87                Assert.notNull(id, "JobExecution is expected to have an id (should be saved already)");
88                JobExecution persistedExecution = executionsById.get(id);
89                Assert.notNull(persistedExecution, "JobExecution must already be saved");
90 
91                synchronized (jobExecution) {
92                        if (!persistedExecution.getVersion().equals(jobExecution.getVersion())) {
93                                throw new OptimisticLockingFailureException("Attempt to update step execution id=" + id
94                                                + " with wrong version (" + jobExecution.getVersion() + "), where current version is "
95                                                + persistedExecution.getVersion());
96                        }
97                        jobExecution.incrementVersion();
98                        executionsById.put(id, copy(jobExecution));
99                }
100        }
101 
102        public JobExecution getLastJobExecution(JobInstance jobInstance) {
103                JobExecution lastExec = null;
104                for (JobExecution exec : executionsById.values()) {
105                        if (!exec.getJobInstance().equals(jobInstance)) {
106                                continue;
107                        }
108                        if (lastExec == null) {
109                                lastExec = exec;
110                        }
111                        if (lastExec.getCreateTime().before(exec.getCreateTime())) {
112                                lastExec = exec;
113                        }
114                }
115                return copy(lastExec);
116        }
117 
118        /*
119         * (non-Javadoc)
120         * 
121         * @seeorg.springframework.batch.core.repository.dao.JobExecutionDao#
122         * findRunningJobExecutions(java.lang.String)
123         */
124        public Set<JobExecution> findRunningJobExecutions(String jobName) {
125                Set<JobExecution> result = new HashSet<JobExecution>();
126                for (JobExecution exec : executionsById.values()) {
127                        if (!exec.getJobInstance().getJobName().equals(jobName) || !exec.isRunning()) {
128                                continue;
129                        }
130                        result.add(copy(exec));
131                }
132                return result;
133        }
134 
135        /*
136         * (non-Javadoc)
137         * 
138         * @see
139         * org.springframework.batch.core.repository.dao.JobExecutionDao#getJobExecution
140         * (java.lang.Long)
141         */
142        public JobExecution getJobExecution(Long executionId) {
143                return copy(executionsById.get(executionId));
144        }
145 
146        public void synchronizeStatus(JobExecution jobExecution) {
147                JobExecution saved = getJobExecution(jobExecution.getId());
148                if (saved.getVersion().intValue() != jobExecution.getVersion().intValue()) {
149                        jobExecution.upgradeStatus(saved.getStatus());
150                        jobExecution.setVersion(saved.getVersion());
151                }
152        }
153}

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