EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.test]

COVERAGE SUMMARY FOR SOURCE FILE [DataSourceInitializer.java]

nameclass, %method, %block, %line, %
DataSourceInitializer.java100% (2/2)72%  (13/18)60%  (177/295)65%  (38.8/60)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DataSourceInitializer$1100% (1/1)100% (2/2)55%  (49/89)62%  (10/16)
doInTransaction (TransactionStatus): Object 100% (1/1)50%  (40/80)60%  (9/15)
DataSourceInitializer$1 (DataSourceInitializer, Resource): void 100% (1/1)100% (9/9)100% (1/1)
     
class DataSourceInitializer100% (1/1)69%  (11/16)62%  (128/206)66%  (29.8/45)
access$200 (DataSourceInitializer): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
access$300 (): Log 0%   (0/1)0%   (0/2)0%   (0/1)
main (String []): void 0%   (0/1)0%   (0/16)0%   (0/2)
setDestroyScripts (Resource []): void 0%   (0/1)0%   (0/4)0%   (0/2)
setIgnoreFailedDrop (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
destroy (): void 100% (1/1)8%   (4/53)10%  (1/10)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
DataSourceInitializer (): void 100% (1/1)100% (9/9)100% (3/3)
access$000 (DataSourceInitializer): DataSource 100% (1/1)100% (3/3)100% (1/1)
access$100 (DataSourceInitializer, List): String 100% (1/1)100% (4/4)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (6/6)100% (3/3)
doExecuteScript (Resource): void 100% (1/1)100% (24/24)100% (5/5)
initialize (): void 100% (1/1)100% (29/29)100% (8/8)
setDataSource (DataSource): void 100% (1/1)100% (4/4)100% (2/2)
setInitScripts (Resource []): void 100% (1/1)100% (4/4)100% (2/2)
stripComments (List): String 100% (1/1)100% (37/37)100% (5/5)

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.test;
18 
19import java.io.IOException;
20import java.util.List;
21 
22import javax.sql.DataSource;
23 
24import org.apache.commons.io.IOUtils;
25import org.apache.commons.logging.Log;
26import org.apache.commons.logging.LogFactory;
27import org.springframework.beans.factory.BeanInitializationException;
28import org.springframework.beans.factory.DisposableBean;
29import org.springframework.beans.factory.InitializingBean;
30import org.springframework.context.support.ClassPathXmlApplicationContext;
31import org.springframework.core.io.Resource;
32import org.springframework.dao.DataAccessException;
33import org.springframework.jdbc.core.JdbcTemplate;
34import org.springframework.jdbc.datasource.DataSourceTransactionManager;
35import org.springframework.transaction.TransactionStatus;
36import org.springframework.transaction.support.TransactionCallback;
37import org.springframework.transaction.support.TransactionTemplate;
38import org.springframework.util.Assert;
39import org.springframework.util.ClassUtils;
40import org.springframework.util.StringUtils;
41 
42/**
43 * Wrapper for a {@link DataSource} that can run scripts on start up and shut
44 * down.  Us as a bean definition <br/><br/>
45 * 
46 * Run this class to initialize a database in a running server process.
47 * Make sure the server is running first by launching the "hsql-server" from the
48 * <code>hsql.server</code> project. Then you can right click in Eclipse and
49 * Run As -&gt; Java Application. Do the same any time you want to wipe the
50 * database and start again.
51 * 
52 * @author Dave Syer
53 * 
54 */
55public class DataSourceInitializer implements InitializingBean, DisposableBean {
56 
57        private static final Log logger = LogFactory.getLog(DataSourceInitializer.class);
58 
59        private Resource[] initScripts;
60 
61        private Resource[] destroyScripts;
62 
63        private DataSource dataSource;
64 
65        private boolean ignoreFailedDrop = true;
66 
67        private boolean initialized = false;
68 
69        /**
70         * Main method as convenient entry point.
71         * 
72         * @param args
73         */
74        public static void main(String... args) {
75                new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(DataSourceInitializer.class,
76                                DataSourceInitializer.class.getSimpleName() + "-context.xml"));
77        }
78 
79        public void destroy() {
80                if (destroyScripts==null) return;
81                for (int i = 0; i < destroyScripts.length; i++) {
82                        Resource destroyScript = destroyScripts[i];
83                        try {
84                                doExecuteScript(destroyScript);
85                        }
86                        catch (Exception e) {
87                                if (logger.isDebugEnabled()) {
88                                        logger.warn("Could not execute destroy script [" + destroyScript + "]", e);
89                                }
90                                else {
91                                        logger.warn("Could not execute destroy script [" + destroyScript + "]");
92                                }
93                        }
94                }
95        }
96 
97        public void afterPropertiesSet() throws Exception {
98                Assert.notNull(dataSource);
99                initialize();
100        }
101 
102        private void initialize() {
103                if (!initialized) {
104                        destroy();
105                        if (initScripts != null) {
106                                for (int i = 0; i < initScripts.length; i++) {
107                                        Resource initScript = initScripts[i];
108                                        doExecuteScript(initScript);
109                                }
110                        }
111                        initialized = true;
112                }
113        }
114 
115        private void doExecuteScript(final Resource scriptResource) {
116                if (scriptResource == null || !scriptResource.exists())
117                        return;
118                TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
119                transactionTemplate.execute(new TransactionCallback() {
120 
121                        @SuppressWarnings("unchecked")
122                        public Object doInTransaction(TransactionStatus status) {
123                                JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
124                                String[] scripts;
125                                try {
126                                        scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource
127                                                        .getInputStream())), ";");
128                                }
129                                catch (IOException e) {
130                                        throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
131                                }
132                                for (int i = 0; i < scripts.length; i++) {
133                                        String script = scripts[i].trim();
134                                        if (StringUtils.hasText(script)) {
135                                                try {
136                                                        jdbcTemplate.execute(script);
137                                                }
138                                                catch (DataAccessException e) {
139                                                        if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
140                                                                logger.debug("DROP script failed (ignoring): " + script);
141                                                        }
142                                                        else {
143                                                                throw e;
144                                                        }
145                                                }
146                                        }
147                                }
148                                return null;
149                        }
150 
151                });
152 
153        }
154 
155        private String stripComments(List<String> list) {
156                StringBuffer buffer = new StringBuffer();
157                for (String line : list) {
158                        if (!line.startsWith("//") && !line.startsWith("--")) {
159                                buffer.append(line + "\n");
160                        }
161                }
162                return buffer.toString();
163        }
164 
165        public void setInitScripts(Resource[] initScripts) {
166                this.initScripts = initScripts;
167        }
168 
169        public void setDestroyScripts(Resource[] destroyScripts) {
170                this.destroyScripts = destroyScripts;
171        }
172 
173        public void setDataSource(DataSource dataSource) {
174                this.dataSource = dataSource;
175        }
176 
177        public void setIgnoreFailedDrop(boolean ignoreFailedDrop) {
178                this.ignoreFailedDrop = ignoreFailedDrop;
179        }
180 
181}

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