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

COVERAGE SUMMARY FOR SOURCE FILE [ScheduledJobParametersFactory.java]

nameclass, %method, %block, %line, %
ScheduledJobParametersFactory.java100% (1/1)100% (4/4)100% (149/149)100% (26/26)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ScheduledJobParametersFactory100% (1/1)100% (4/4)100% (149/149)100% (26/26)
ScheduledJobParametersFactory (): void 100% (1/1)100% (9/9)100% (2/2)
getJobParameters (Properties): JobParameters 100% (1/1)100% (73/73)100% (11/11)
getProperties (JobParameters): Properties 100% (1/1)100% (63/63)100% (11/11)
setDateFormat (DateFormat): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 * Copyright 2006-2008 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.launch.support;
17 
18import java.text.DateFormat;
19import java.text.ParseException;
20import java.text.SimpleDateFormat;
21import java.util.Date;
22import java.util.Map;
23import java.util.Properties;
24import java.util.Map.Entry;
25 
26import org.springframework.batch.core.JobParameter;
27import org.springframework.batch.core.JobParameters;
28import org.springframework.batch.core.JobParametersBuilder;
29import org.springframework.batch.core.converter.JobParametersConverter;
30 
31/**
32 * @author Lucas Ward
33 * 
34 */
35public class ScheduledJobParametersFactory implements JobParametersConverter {
36 
37        public static final String SCHEDULE_DATE_KEY = "schedule.date";
38 
39        private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
40 
41        /*
42         * (non-Javadoc)
43         * 
44         * @see org.springframework.batch.core.runtime.JobParametersFactory#getJobParameters(java.util.Properties)
45         */
46        public JobParameters getJobParameters(Properties props) {
47 
48                if (props == null || props.isEmpty()) {
49                        return new JobParameters();
50                }
51 
52                JobParametersBuilder propertiesBuilder = new JobParametersBuilder();
53 
54                for (Entry<Object, Object> entry : props.entrySet()) {
55                        if (entry.getKey().equals(SCHEDULE_DATE_KEY)) {
56                                Date scheduleDate;
57                                try {
58                                        scheduleDate = dateFormat.parse(entry.getValue().toString());
59                                } catch (ParseException ex) {
60                                        throw new IllegalArgumentException("Date format is invalid: [" + entry.getValue() + "]");
61                                }
62                                propertiesBuilder.addDate(entry.getKey().toString(), scheduleDate);
63                        } else {
64                                propertiesBuilder.addString(entry.getKey().toString(), entry.getValue().toString());
65                        }
66                }
67 
68                return propertiesBuilder.toJobParameters();
69        }
70 
71        /**
72         * Convert schedule date to Date, and assume all other parameters can be represented by their default string value.
73         * 
74         * @see org.springframework.batch.core.converter.JobParametersConverter#getProperties(org.springframework.batch.core.JobParameters)
75         */
76        public Properties getProperties(JobParameters params) {
77 
78                if (params == null || params.isEmpty()) {
79                        return new Properties();
80                }
81 
82                Map<String, JobParameter> parameters = params.getParameters();
83                Properties result = new Properties();
84                for (Entry<String, JobParameter> entry : parameters.entrySet()) {
85                        String key = entry.getKey();
86                        JobParameter jobParameter = entry.getValue();
87                        if (key.equals(SCHEDULE_DATE_KEY)) {
88                                result.setProperty(key, dateFormat.format(jobParameter.getValue()));
89                        } else {
90                                result.setProperty(key, "" + jobParameter.getValue());
91                        }
92                }
93                return result;
94        }
95 
96        /**
97         * Public setter for injecting a date format.
98         * 
99         * @param dateFormat a {@link DateFormat}, defaults to "yyyy/MM/dd"
100         */
101        public void setDateFormat(DateFormat dateFormat) {
102                this.dateFormat = dateFormat;
103        }
104}

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