2 * Copyright 2013 F.B.A. Scheper.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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. *
16 package org.bitbucket.fbascheper.tutorial.envers.util;
18 import org.springframework.core.io.Resource;
19 import org.springframework.jdbc.core.JdbcTemplate;
20 import org.springframework.test.jdbc.JdbcTestUtils;
22 import javax.sql.DataSource;
25 * A initialization script runner.
27 * @author Erik-Berndt Scheper
30 public interface InitScriptRunner {
33 * Run the database creation script.
35 public void runScript();
38 * Return the datasource used to run the script
40 * @return the datasource
42 public DataSource getDataSource();
46 * Default implementation for the runner of the creation script.
48 public static class InitScriptRunnerImpl implements InitScriptRunner {
50 private final DataSource dataSource;
51 private final Resource dbDropScriptLocation;
52 private final Resource dbCreateScriptLocation;
56 * Initializing constructor.
58 * @param dataSource the raw {@link javax.sql.DataSource} to return
59 * @param dbDropScriptLocation location of the create DB drop script to run
60 * @param dbCreateScriptLocation location of the create DB create script to run
62 public InitScriptRunnerImpl(DataSource dataSource,
63 Resource dbDropScriptLocation,
64 Resource dbCreateScriptLocation) {
65 this.dataSource = dataSource;
66 this.dbDropScriptLocation = dbDropScriptLocation;
67 this.dbCreateScriptLocation = dbCreateScriptLocation;
71 public void runScript() {
73 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
74 JdbcTestUtils.executeSqlScript(jdbcTemplate, dbDropScriptLocation, false);
75 JdbcTestUtils.executeSqlScript(jdbcTemplate, dbCreateScriptLocation, false);
80 public DataSource getDataSource() {
82 return this.dataSource;