001 /**
002 * Copyright 2006-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.sql;
017
018 import java.io.File;
019 import java.util.Properties;
020
021 import org.apache.maven.plugin.MojoExecutionException;
022 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
023 import org.apache.maven.settings.Server;
024 import org.apache.maven.settings.Settings;
025 import org.apache.maven.shared.filtering.MavenFileFilter;
026
027 /**
028 * Unit test for simple SqlExecMojo.
029 */
030 public class SqlExecMojoTest extends AbstractMojoTestCase {
031 private SqlExecMojo mojo;
032
033 private Properties p;
034
035 @Override
036 public void setUp() throws Exception {
037 super.setUp();
038 p = new Properties();
039 p.load(getClass().getResourceAsStream("/test.properties"));
040
041 mojo = new SqlExecMojo();
042
043 // populate parameters
044 mojo.setDriver(p.getProperty("driver"));
045 mojo.setUsername(p.getProperty("user"));
046 mojo.setPassword(p.getProperty("password"));
047 mojo.setUrl(p.getProperty("url"));
048 mojo.setDriverProperties(p.getProperty("driverProperties"));
049
050 MavenFileFilter filter = (MavenFileFilter) lookup("org.apache.maven.shared.filtering.MavenFileFilter",
051 "default");
052 mojo.setFileFilter(filter);
053
054 }
055
056 /**
057 * No error when there is no input
058 */
059 public void testNoCommandMojo() throws MojoExecutionException {
060 mojo.execute();
061
062 assertEquals(0, mojo.getSuccessfulStatements());
063 }
064
065 public void testCreateCommandMojo() throws MojoExecutionException {
066 String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
067 mojo.addText(command);
068 mojo.execute();
069
070 assertEquals(1, mojo.getSuccessfulStatements());
071 }
072
073 public void testDropCommandMojo() throws MojoExecutionException {
074 String command = "drop table PERSON";
075 mojo.addText(command);
076 mojo.execute();
077 assertEquals(1, mojo.getSuccessfulStatements());
078 }
079
080 public void testFileSetMojo() throws MojoExecutionException {
081
082 Fileset ds = new Fileset();
083 ds.setBasedir("src/test");
084 ds.setIncludes(new String[] { "**/create*.sql" });
085 ds.scan();
086 assert (ds.getIncludedFiles().length == 1);
087
088 mojo.setFileset(ds);
089
090 mojo.execute();
091
092 assertEquals(3, mojo.getSuccessfulStatements());
093
094 }
095
096 public void testFileArrayMojo() throws MojoExecutionException {
097 File[] srcFiles = new File[1];
098 srcFiles[0] = new File("src/test/data/drop-test-tables.sql");
099
100 mojo.setSrcFiles(srcFiles);
101 mojo.execute();
102
103 assertEquals(3, mojo.getSuccessfulStatements());
104
105 }
106
107 /**
108 * Ensure srcFiles always execute first
109 *
110 */
111 public void testAllMojo() throws MojoExecutionException {
112
113 String command = "create table PERSON2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
114 mojo.addText(command);
115
116 File[] srcFiles = new File[1];
117 srcFiles[0] = new File("src/test/data/create-test-tables.sql");
118 mojo.setSrcFiles(srcFiles);
119
120 Fileset ds = new Fileset();
121 ds.setBasedir("src/test");
122 ds.setIncludes(new String[] { "**/drop*.sql" });
123 ds.scan();
124 mojo.setFileset(ds);
125 mojo.execute();
126
127 assertEquals(7, mojo.getSuccessfulStatements());
128 }
129
130 public void testOrderFile() throws MojoExecutionException {
131 Fileset ds = new Fileset();
132 ds.setBasedir("src/test");
133 ds.setIncludes(new String[] { "**/drop*.sql", "**/create*.sql" });
134 ds.scan();
135 mojo.setFileset(ds);
136
137 mojo.setOrderFile(Order.ASCENDING.name());
138 mojo.execute();
139
140 assertEquals(6, mojo.getSuccessfulStatements());
141
142 try {
143 mojo.setOrderFile(Order.DESCENDING.name());
144 mojo.execute();
145 fail("Execution is not aborted on error.");
146 } catch (MojoExecutionException e) {
147 }
148 }
149
150 public void testOnErrorContinueMojo() throws MojoExecutionException {
151 String command = "create table BOGUS"; // bad syntax
152 mojo.addText(command);
153 mojo.setOnError("continue");
154 mojo.execute();
155 assertEquals(0, mojo.getSuccessfulStatements());
156 }
157
158 public void testOnErrorAbortMojo() throws MojoExecutionException {
159 String command = "create table BOGUS"; // bad syntax
160 mojo.addText(command);
161
162 try {
163 mojo.execute();
164 fail("Execution is not aborted on error.");
165
166 } catch (MojoExecutionException e) {
167
168 }
169
170 assertEquals(0, mojo.getSuccessfulStatements());
171 }
172
173 public void testOnErrorAbortAfterMojo() throws MojoExecutionException {
174 String commands = "create table BOGUS"; // bad syntax
175
176 mojo.addText(commands);
177
178 File[] srcFiles = new File[1];
179 srcFiles[0] = new File("src/test/data/invalid-syntax.sql");
180
181 assertTrue(srcFiles[0].exists());
182
183 mojo.setSrcFiles(srcFiles);
184 mojo.setOnError("abortAfter");
185
186 try {
187 mojo.execute();
188 fail("Execution is not aborted on error.");
189
190 } catch (MojoExecutionException e) {
191 // expected
192 }
193
194 assertEquals(0, mojo.getSuccessfulStatements());
195 assertEquals(2, mojo.getTotalStatements());
196 }
197
198 public void testDefaultUsernamePassword() throws MojoExecutionException {
199
200 Settings settings = new Settings();
201 Server server = new Server();
202 settings.addServer(server);
203
204 mojo.setSettings(settings);
205
206 // force a lookup of username
207 mojo.setUsername(null);
208 mojo.setPassword(null);
209
210 mojo.execute();
211
212 assertEquals("", mojo.getUsername());
213 assertEquals("", mojo.getPassword());
214
215 }
216
217 public void testUsernamePasswordLookup() throws MojoExecutionException {
218
219 Settings settings = new Settings();
220 Server server = new Server();
221 server.setId("somekey");
222 server.setUsername("username");
223 server.setPassword("password");
224 settings.addServer(server);
225
226 mojo.setSettings(settings);
227
228 // force a lookup of username
229 mojo.setSettingsKey("somekey");
230 mojo.setUsername(null);
231 mojo.setPassword(null);
232
233 mojo.execute();
234
235 assertEquals("username", mojo.getUsername());
236 assertEquals("password", mojo.getPassword());
237
238 }
239
240 public void testBadDriver() {
241 mojo.setDriver("bad-driver");
242 try {
243 mojo.execute();
244
245 fail("Bad driver is not detected");
246 } catch (MojoExecutionException e) {
247
248 }
249 }
250
251 public void testBadUrl() {
252 mojo.setUrl("bad-url");
253 try {
254 mojo.execute();
255
256 fail("Bad URL is not detected");
257 } catch (MojoExecutionException e) {
258
259 }
260 }
261
262 public void testBadFile() {
263 File[] srcFiles = new File[1];
264 srcFiles[0] = new File("a-every-bogus-file-that-does-not-exist");
265
266 mojo.setSrcFiles(srcFiles);
267 try {
268 mojo.execute();
269
270 fail("Bad files is not detected");
271 } catch (MojoExecutionException e) {
272
273 }
274 }
275
276 public void testOnError() {
277 mojo.setOnError("AbOrT");
278 assertEquals(SqlExecMojo.ON_ERROR_ABORT, mojo.getOnError());
279 mojo.setOnError("cOnTiNuE");
280 assertEquals(SqlExecMojo.ON_ERROR_CONTINUE, mojo.getOnError());
281 try {
282 mojo.setOnError("bad");
283 fail(IllegalArgumentException.class.getName() + " was not thrown.");
284 } catch (IllegalArgumentException e) {
285 // expected
286 }
287 try {
288 mojo.setOnError(null);
289 fail(IllegalArgumentException.class.getName() + " was not thrown.");
290 } catch (IllegalArgumentException e) {
291 // expected
292 }
293 }
294
295 public void testSkip() throws MojoExecutionException {
296 String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
297 mojo.addText(command);
298 mojo.setSkip(true);
299 mojo.execute();
300
301 // no command was executed due to skip is on
302 assertEquals(0, mojo.getSuccessfulStatements());
303 }
304
305 public void testDriverProperties() throws MojoExecutionException {
306 Properties driverProperties = this.mojo.getDriverProperties();
307 assertEquals(2, driverProperties.size());
308 assertEquals("value1", driverProperties.get("key1"));
309 assertEquals("value2", driverProperties.get("key2"));
310
311 mojo.setDriverProperties("key1=value1,key2");
312 try {
313 driverProperties = this.mojo.getDriverProperties();
314 } catch (MojoExecutionException e) {
315 }
316
317 }
318
319 public void testBlockMode() throws MojoExecutionException {
320 String command = "create table BLOCKTABLE ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
321 mojo.addText(command);
322 mojo.setEnableBlockMode(true);
323 mojo.execute();
324 assertEquals(1, mojo.getSuccessfulStatements());
325
326 mojo.setSqlCommand("");
327 mojo.getTransactions().clear();
328 command = "drop table BLOCKTABLE";
329 mojo.addText(command);
330 mojo.execute();
331 assertEquals(1, mojo.getSuccessfulStatements());
332 }
333
334 public void testKeepFormat() throws MojoExecutionException {
335 // Normally a line starting in -- would be ignored, but with keepformat
336 // mode
337 // on it will not.
338 String command = "--create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
339 mojo.addText(command);
340 mojo.setKeepFormat(true);
341
342 try {
343 mojo.execute();
344 fail("-- at the start of the SQL command is ignored.");
345 } catch (MojoExecutionException e) {
346 }
347
348 assertEquals(0, mojo.getSuccessfulStatements());
349
350 }
351
352 public void testBadDelimiter() throws Exception {
353 String command = "create table SEPARATOR ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar):"
354 + "create table SEPARATOR2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
355
356 mojo.addText(command);
357 mojo.setDelimiter(":");
358
359 try {
360 mojo.execute();
361 fail("Expected parser error.");
362 } catch (MojoExecutionException e) {
363 }
364 }
365
366 public void testGoodDelimiter() throws Exception {
367 String command = "create table SEPARATOR ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)\n:\n"
368 + "create table SEPARATOR2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
369
370 mojo.addText(command);
371 mojo.setDelimiter(":");
372
373 mojo.execute();
374
375 assertEquals(2, mojo.getSuccessfulStatements());
376 }
377
378 public void testBadDelimiterType() throws Exception {
379 String command = "create table BADDELIMTYPE ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)" + "\n:"
380 + "create table BADDELIMTYPE2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
381
382 mojo.addText(command);
383 mojo.setDelimiter(":");
384 mojo.setDelimiterType(DelimiterType.ROW);
385
386 try {
387 mojo.execute();
388 fail("Expected parser error.");
389 } catch (MojoExecutionException e) {
390 }
391 }
392
393 public void testGoodDelimiterType() throws Exception {
394 String command = "create table GOODDELIMTYPE ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)"
395 + "\n: \n" + "create table GOODDELIMTYPE2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
396
397 mojo.addText(command);
398 mojo.setDelimiter(":");
399 mojo.setDelimiterType(DelimiterType.ROW);
400
401 mojo.execute();
402 assertEquals(2, mojo.getSuccessfulStatements());
403 }
404
405 @SuppressWarnings("deprecation")
406 public void testOutputFile() throws Exception {
407 String command = "create table GOODDELIMTYPE3 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)"
408 + "\n: \n" + "create table GOODDELIMTYPE4 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
409
410 mojo.addText(command);
411 mojo.setDelimiter(":");
412 mojo.setDelimiterType(DelimiterType.ROW);
413
414 String basedir = System.getProperty("basedir", ".");
415 File outputFile = new File(basedir, "target/sql.out");
416 outputFile.delete();
417 mojo.setOutputFile(outputFile);
418 mojo.setPrintResutlSet(true);
419
420 mojo.execute();
421
422 assertTrue("Output file: " + outputFile + " not found.", outputFile.exists());
423
424 assertTrue("Unexpected empty output file. ", outputFile.length() > 0);
425
426 // makesure we can remote the file, it is not locked
427 // assertTrue( outputFile.delete() );
428
429 }
430 }