View Javadoc

1   /**
2    * Copyright 2006-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.codehaus.mojo.sql;
17  
18  import java.io.File;
19  import java.util.Properties;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
23  import org.apache.maven.settings.Server;
24  import org.apache.maven.settings.Settings;
25  import org.apache.maven.shared.filtering.MavenFileFilter;
26  
27  /**
28   * Unit test for simple SqlExecMojo.
29   */
30  public class SqlExecMojoTest extends AbstractMojoTestCase {
31      private SqlExecMojo mojo;
32  
33      private Properties p;
34  
35      @Override
36      public void setUp() throws Exception {
37          super.setUp();
38          p = new Properties();
39          p.load(getClass().getResourceAsStream("/test.properties"));
40  
41          mojo = new SqlExecMojo();
42  
43          // populate parameters
44          mojo.setDriver(p.getProperty("driver"));
45          mojo.setUsername(p.getProperty("user"));
46          mojo.setPassword(p.getProperty("password"));
47          mojo.setUrl(p.getProperty("url"));
48          mojo.setDriverProperties(p.getProperty("driverProperties"));
49  
50          MavenFileFilter filter = (MavenFileFilter) lookup("org.apache.maven.shared.filtering.MavenFileFilter",
51                  "default");
52          mojo.setFileFilter(filter);
53  
54      }
55  
56      /**
57       * No error when there is no input
58       */
59      public void testNoCommandMojo() throws MojoExecutionException {
60          mojo.execute();
61  
62          assertEquals(0, mojo.getSuccessfulStatements());
63      }
64  
65      public void testCreateCommandMojo() throws MojoExecutionException {
66          String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
67          mojo.addText(command);
68          mojo.execute();
69  
70          assertEquals(1, mojo.getSuccessfulStatements());
71      }
72  
73      public void testDropCommandMojo() throws MojoExecutionException {
74          String command = "drop table PERSON";
75          mojo.addText(command);
76          mojo.execute();
77          assertEquals(1, mojo.getSuccessfulStatements());
78      }
79  
80      public void testFileSetMojo() throws MojoExecutionException {
81  
82          Fileset ds = new Fileset();
83          ds.setBasedir("src/test");
84          ds.setIncludes(new String[] { "**/create*.sql" });
85          ds.scan();
86          assert (ds.getIncludedFiles().length == 1);
87  
88          mojo.setFileset(ds);
89  
90          mojo.execute();
91  
92          assertEquals(3, mojo.getSuccessfulStatements());
93  
94      }
95  
96      public void testFileArrayMojo() throws MojoExecutionException {
97          File[] srcFiles = new File[1];
98          srcFiles[0] = new File("src/test/data/drop-test-tables.sql");
99  
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 }