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.jdbc;
17  
18  import org.apache.ibatis.BaseDataTest;
19  import org.apache.ibatis.datasource.pooled.PooledDataSource;
20  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
21  import org.apache.ibatis.io.Resources;
22  import static org.junit.Assert.*;
23  
24  import org.junit.Ignore;
25  import org.junit.Test;
26  
27  import javax.sql.DataSource;
28  import java.io.IOException;
29  import java.io.PrintWriter;
30  import java.io.Reader;
31  import java.io.StringReader;
32  import java.io.StringWriter;
33  import java.sql.Connection;
34  import java.sql.SQLException;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Properties;
38  
39  public class ScriptRunnerTest extends BaseDataTest {
40  
41    @Test
42    @Ignore("This fails with HSQLDB 2.0 due to the create index statements in the schema script")
43    public void shouldRunScriptsBySendingFullScriptAtOnce() throws Exception {
44      DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
45      Connection conn = ds.getConnection();
46      ScriptRunner runner = new ScriptRunner(conn);
47      runner.setSendFullScript(true);
48      runner.setAutoCommit(true);
49      runner.setStopOnError(false);
50      runner.setErrorLogWriter(null);
51      runner.setLogWriter(null);
52      runJPetStoreScripts(runner);
53      assertProductsTableExistsAndLoaded();
54    }
55  
56    @Test
57    public void shouldRunScriptsUsingConnection() throws Exception {
58      DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
59      Connection conn = ds.getConnection();
60      ScriptRunner runner = new ScriptRunner(conn);
61      runner.setAutoCommit(true);
62      runner.setStopOnError(false);
63      runner.setErrorLogWriter(null);
64      runner.setLogWriter(null);
65      runJPetStoreScripts(runner);
66      assertProductsTableExistsAndLoaded();
67    }
68  
69    @Test
70    public void shouldRunScriptsUsingProperties() throws Exception {
71      Properties props = Resources.getResourceAsProperties(JPETSTORE_PROPERTIES);
72      DataSource dataSource = new UnpooledDataSource(
73          props.getProperty("driver"),
74          props.getProperty("url"),
75          props.getProperty("username"),
76          props.getProperty("password"));
77      ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
78      runner.setAutoCommit(true);
79      runner.setStopOnError(false);
80      runner.setErrorLogWriter(null);
81      runner.setLogWriter(null);
82      runJPetStoreScripts(runner);
83      assertProductsTableExistsAndLoaded();
84    }
85  
86    @Test
87    public void shouldReturnWarningIfEndOfLineTerminatorNotFound() throws Exception {
88      DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
89      Connection conn = ds.getConnection();
90      ScriptRunner runner = new ScriptRunner(conn);
91      runner.setAutoCommit(true);
92      runner.setStopOnError(false);
93      runner.setErrorLogWriter(null);
94      runner.setLogWriter(null);
95  
96      String resource = "org/apache/ibatis/jdbc/ScriptMissingEOLTerminator.sql";
97      Reader reader = Resources.getResourceAsReader(resource);
98  
99      try {
100       runner.runScript(reader);
101       fail("Expected script runner to fail due to missing end of line terminator.");
102     } catch (Exception e) {
103       assertTrue(e.getMessage().contains("end-of-line terminator"));
104     }
105   }
106 
107   @Test
108   public void commentAferStatementDelimiterShouldNotCauseRunnerFail() throws Exception {
109     DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
110     Connection conn = ds.getConnection();
111     ScriptRunner runner = new ScriptRunner(conn);
112     runner.setAutoCommit(true);
113     runner.setStopOnError(true);
114     runner.setErrorLogWriter(null);
115     runner.setLogWriter(null);
116     runJPetStoreScripts(runner);
117 
118     String resource = "org/apache/ibatis/jdbc/ScriptCommentAfterEOLTerminator.sql";
119     Reader reader = Resources.getResourceAsReader(resource);
120 
121     try {
122       runner.runScript(reader);
123     } catch (Exception e) {
124       fail(e.getMessage());
125     }
126   }
127 
128   @Test
129   public void shouldReturnWarningIfNotTheCurrentDelimiterUsed() throws Exception {
130     DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
131     Connection conn = ds.getConnection();
132     ScriptRunner runner = new ScriptRunner(conn);
133     runner.setAutoCommit(false);
134     runner.setStopOnError(true);
135     runner.setErrorLogWriter(null);
136     runner.setLogWriter(null);
137 
138     String resource = "org/apache/ibatis/jdbc/ScriptChangingDelimiterMissingDelimiter.sql";
139     Reader reader = Resources.getResourceAsReader(resource);
140 
141     try {
142       runner.runScript(reader);
143       fail("Expected script runner to fail due to the usage of invalid delimiter.");
144     } catch (Exception e) {
145       assertTrue(e.getMessage().contains("end-of-line terminator"));
146     }
147   }
148 
149   @Test
150   public void changingDelimiterShouldNotCauseRunnerFail() throws Exception {
151     DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
152     Connection conn = ds.getConnection();
153     ScriptRunner runner = new ScriptRunner(conn);
154     runner.setAutoCommit(false);
155     runner.setStopOnError(true);
156     runner.setErrorLogWriter(null);
157     runner.setLogWriter(null);
158     runJPetStoreScripts(runner);
159 
160     String resource = "org/apache/ibatis/jdbc/ScriptChangingDelimiter.sql";
161     Reader reader = Resources.getResourceAsReader(resource);
162 
163     try {
164       runner.runScript(reader);
165     } catch (Exception e) {
166       fail(e.getMessage());
167     }
168   }
169 
170   @Test
171   public void testLogging() throws Exception {
172     DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
173     Connection conn = ds.getConnection();
174     ScriptRunner runner = new ScriptRunner(conn);
175     runner.setAutoCommit(true);
176     runner.setStopOnError(false);
177     runner.setErrorLogWriter(null);
178     runner.setSendFullScript(false);
179     StringWriter sw = new StringWriter();
180     PrintWriter logWriter = new PrintWriter(sw);
181     runner.setLogWriter(logWriter);
182 
183     Reader reader = new StringReader("select userid from account where userid = 'j2ee';");
184     runner.runScript(reader);
185 
186     assertEquals(
187             "select userid from account where userid = 'j2ee'" + System.getProperty("line.separator")
188                     + System.getProperty("line.separator") + "USERID\t" + System.getProperty("line.separator")
189                     + "j2ee\t" + System.getProperty("line.separator"), sw.toString());
190   }
191 
192   @Test
193   public void testLoggingFullScipt() throws Exception {
194     DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
195     Connection conn = ds.getConnection();
196     ScriptRunner runner = new ScriptRunner(conn);
197     runner.setAutoCommit(true);
198     runner.setStopOnError(false);
199     runner.setErrorLogWriter(null);
200     runner.setSendFullScript(true);
201     StringWriter sw = new StringWriter();
202     PrintWriter logWriter = new PrintWriter(sw);
203     runner.setLogWriter(logWriter);
204 
205     Reader reader = new StringReader("select userid from account where userid = 'j2ee';");
206     runner.runScript(reader);
207 
208     assertEquals(
209             "select userid from account where userid = 'j2ee';" + System.getProperty("line.separator")
210                     + System.getProperty("line.separator") + "USERID\t" + System.getProperty("line.separator")
211                     + "j2ee\t" + System.getProperty("line.separator"), sw.toString());
212   }
213 
214   private void runJPetStoreScripts(ScriptRunner runner) throws IOException, SQLException {
215     runScript(runner, JPETSTORE_DDL);
216     runScript(runner, JPETSTORE_DATA);
217   }
218 
219   private void assertProductsTableExistsAndLoaded() throws IOException, SQLException {
220     PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
221     try {
222       Connection conn = ds.getConnection();
223       SqlRunner executor = new SqlRunner(conn);
224       List<Map<String, Object>> products = executor.selectAll("SELECT * FROM PRODUCT");
225       assertEquals(16, products.size());
226     } finally {
227       ds.forceCloseAll();
228     }
229   }
230 
231 }