View Javadoc
1   /**
2    *    Copyright 2009-2015 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  package org.apache.ibatis;
17  
18  import org.apache.ibatis.datasource.pooled.PooledDataSource;
19  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
20  import org.apache.ibatis.io.Resources;
21  import org.apache.ibatis.jdbc.ScriptRunner;
22  
23  import javax.sql.DataSource;
24  import java.io.IOException;
25  import java.io.Reader;
26  import java.sql.Connection;
27  import java.sql.SQLException;
28  import java.util.Properties;
29  
30  public abstract class BaseDataTest {
31  
32    public static final String BLOG_PROPERTIES = "org/apache/ibatis/databases/blog/blog-derby.properties";
33    public static final String BLOG_DDL = "org/apache/ibatis/databases/blog/blog-derby-schema.sql";
34    public static final String BLOG_DATA = "org/apache/ibatis/databases/blog/blog-derby-dataload.sql";
35  
36    public static final String JPETSTORE_PROPERTIES = "org/apache/ibatis/databases/jpetstore/jpetstore-hsqldb.properties";
37    public static final String JPETSTORE_DDL = "org/apache/ibatis/databases/jpetstore/jpetstore-hsqldb-schema.sql";
38    public static final String JPETSTORE_DATA = "org/apache/ibatis/databases/jpetstore/jpetstore-hsqldb-dataload.sql";
39  
40    public static UnpooledDataSource createUnpooledDataSource(String resource) throws IOException {
41      Properties props = Resources.getResourceAsProperties(resource);
42      UnpooledDataSource ds = new UnpooledDataSource();
43      ds.setDriver(props.getProperty("driver"));
44      ds.setUrl(props.getProperty("url"));
45      ds.setUsername(props.getProperty("username"));
46      ds.setPassword(props.getProperty("password"));
47      return ds;
48    }
49  
50    public static PooledDataSource createPooledDataSource(String resource) throws IOException {
51      Properties props = Resources.getResourceAsProperties(resource);
52      PooledDataSource ds = new PooledDataSource();
53      ds.setDriver(props.getProperty("driver"));
54      ds.setUrl(props.getProperty("url"));
55      ds.setUsername(props.getProperty("username"));
56      ds.setPassword(props.getProperty("password"));
57      return ds;
58    }
59  
60    public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
61      Connection connection = ds.getConnection();
62      try {
63        ScriptRunner runner = new ScriptRunner(connection);
64        runner.setAutoCommit(true);
65        runner.setStopOnError(false);
66        runner.setLogWriter(null);
67        runner.setErrorLogWriter(null);
68        runScript(runner, resource);
69      } finally {
70        connection.close();
71      }
72    }
73  
74    public static void runScript(ScriptRunner runner, String resource) throws IOException, SQLException {
75      Reader reader = Resources.getResourceAsReader(resource);
76      try {
77        runner.runScript(reader);
78      } finally {
79        reader.close();
80      }
81    }
82  
83    public static DataSource createBlogDataSource() throws IOException, SQLException {
84      DataSource ds = createUnpooledDataSource(BLOG_PROPERTIES);
85      runScript(ds, BLOG_DDL);
86      runScript(ds, BLOG_DATA);
87      return ds;
88    }
89  
90    public static DataSource createJPetstoreDataSource() throws IOException, SQLException {
91      DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
92      runScript(ds, JPETSTORE_DDL);
93      runScript(ds, JPETSTORE_DATA);
94      return ds;
95    }
96  }