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