Integration-Test for envers really generates the SQL
[hibernate4-maven-plugin] / src / it / hibernate4-maven-plugin-envers-sample / src / test / java / org / bitbucket / fbascheper / tutorial / envers / util / InitScriptRunner.java
1 /**
2  * Copyright 2013 F.B.A. Scheper.
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 package org.bitbucket.fbascheper.tutorial.envers.util;
17
18 import org.springframework.core.io.Resource;
19 import org.springframework.jdbc.core.JdbcTemplate;
20 import org.springframework.test.jdbc.JdbcTestUtils;
21
22 import javax.sql.DataSource;
23
24 /**
25  * A initialization script runner.
26  *
27  * @author Erik-Berndt Scheper
28  * @since 11-09-2013
29  */
30 public interface InitScriptRunner {
31
32     /**
33      * Run the database creation script.
34      */
35     public void runScript();
36
37     /**
38      * Return the datasource used to run the script
39      *
40      * @return the datasource
41      */
42     public DataSource getDataSource();
43
44
45     /**
46      * Default implementation for the runner of the creation script.
47      */
48     public static class InitScriptRunnerImpl implements InitScriptRunner {
49
50         private final DataSource dataSource;
51         private final Resource dbDropScriptLocation;
52         private final Resource dbCreateScriptLocation;
53
54
55         /**
56          * Initializing constructor.
57          *
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
61          */
62         public InitScriptRunnerImpl(DataSource dataSource,
63                                     Resource dbDropScriptLocation,
64                                     Resource dbCreateScriptLocation) {
65             this.dataSource = dataSource;
66             this.dbDropScriptLocation = dbDropScriptLocation;
67             this.dbCreateScriptLocation = dbCreateScriptLocation;
68         }
69
70         @Override
71         public void runScript() {
72
73             JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
74             try {
75                 JdbcTestUtils.executeSqlScript(jdbcTemplate, dbDropScriptLocation, false);
76             }
77             catch (Exception e) {}
78             try {
79             JdbcTestUtils.executeSqlScript(jdbcTemplate, dbCreateScriptLocation, false);
80             }
81             catch (Exception e) {}
82
83         }
84
85         @Override
86         public DataSource getDataSource() {
87
88             return this.dataSource;
89
90         }
91     }
92
93 }