Clover Coverage Report - SQL Maven Plugin 1.7-SNAPSHOT
Coverage timestamp: Wed Dec 31 1969 19:00:00 EST
192   429   39   7.38
2   289   0.2   26
26     1.5  
1    
 
  SqlExecMojoTest       Line # 29 192 0% 39 220 0% 0.0
 
No Tests
 
1    package org.codehaus.mojo.sql;
2   
3    /*
4    * Copyright 2006 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7    * in compliance with the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10    *
11    * Unless required by applicable law or agreed to in writing, software distributed under the License
12    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13    * or implied. See the License for the specific language governing permissions and limitations under
14    * the License.
15    */
16   
17    import java.io.File;
18    import java.util.Properties;
19   
20    import org.apache.maven.plugin.MojoExecutionException;
21    import org.apache.maven.plugin.testing.AbstractMojoTestCase;
22    import org.apache.maven.settings.Server;
23    import org.apache.maven.settings.Settings;
24    import org.apache.maven.shared.filtering.MavenFileFilter;
25   
26    /**
27    * Unit test for simple SqlExecMojo.
28    */
 
29    public class SqlExecMojoTest extends AbstractMojoTestCase {
30    private SqlExecMojo mojo;
31   
32    private Properties p;
33   
 
34  0 toggle @Override
35    public void setUp() throws Exception {
36  0 super.setUp();
37  0 p = new Properties();
38  0 p.load(getClass().getResourceAsStream("/test.properties"));
39   
40  0 mojo = new SqlExecMojo();
41   
42    // populate parameters
43  0 mojo.setDriver(p.getProperty("driver"));
44  0 mojo.setUsername(p.getProperty("user"));
45  0 mojo.setPassword(p.getProperty("password"));
46  0 mojo.setUrl(p.getProperty("url"));
47  0 mojo.setDriverProperties(p.getProperty("driverProperties"));
48   
49  0 MavenFileFilter filter = (MavenFileFilter) lookup("org.apache.maven.shared.filtering.MavenFileFilter",
50    "default");
51  0 mojo.setFileFilter(filter);
52   
53    }
54   
55    /**
56    * No error when there is no input
57    */
 
58  0 toggle public void testNoCommandMojo() throws MojoExecutionException {
59  0 mojo.execute();
60   
61  0 assertEquals(0, mojo.getSuccessfulStatements());
62    }
63   
 
64  0 toggle public void testCreateCommandMojo() throws MojoExecutionException {
65  0 String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
66  0 mojo.addText(command);
67  0 mojo.execute();
68   
69  0 assertEquals(1, mojo.getSuccessfulStatements());
70    }
71   
 
72  0 toggle public void testDropCommandMojo() throws MojoExecutionException {
73  0 String command = "drop table PERSON";
74  0 mojo.addText(command);
75  0 mojo.execute();
76  0 assertEquals(1, mojo.getSuccessfulStatements());
77    }
78   
 
79  0 toggle public void testFileSetMojo() throws MojoExecutionException {
80   
81  0 Fileset ds = new Fileset();
82  0 ds.setBasedir("src/test");
83  0 ds.setIncludes(new String[] { "**/create*.sql" });
84  0 ds.scan();
85  0 assert (ds.getIncludedFiles().length == 1);
86   
87  0 mojo.setFileset(ds);
88   
89  0 mojo.execute();
90   
91  0 assertEquals(3, mojo.getSuccessfulStatements());
92   
93    }
94   
 
95  0 toggle public void testFileArrayMojo() throws MojoExecutionException {
96  0 File[] srcFiles = new File[1];
97  0 srcFiles[0] = new File("src/test/data/drop-test-tables.sql");
98   
99  0 mojo.setSrcFiles(srcFiles);
100  0 mojo.execute();
101   
102  0 assertEquals(3, mojo.getSuccessfulStatements());
103   
104    }
105   
106    /**
107    * Ensure srcFiles always execute first
108    *
109    */
 
110  0 toggle public void testAllMojo() throws MojoExecutionException {
111   
112  0 String command = "create table PERSON2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
113  0 mojo.addText(command);
114   
115  0 File[] srcFiles = new File[1];
116  0 srcFiles[0] = new File("src/test/data/create-test-tables.sql");
117  0 mojo.setSrcFiles(srcFiles);
118   
119  0 Fileset ds = new Fileset();
120  0 ds.setBasedir("src/test");
121  0 ds.setIncludes(new String[] { "**/drop*.sql" });
122  0 ds.scan();
123  0 mojo.setFileset(ds);
124  0 mojo.execute();
125   
126  0 assertEquals(7, mojo.getSuccessfulStatements());
127    }
128   
 
129  0 toggle public void testOrderFile() throws MojoExecutionException {
130  0 Fileset ds = new Fileset();
131  0 ds.setBasedir("src/test");
132  0 ds.setIncludes(new String[] { "**/drop*.sql", "**/create*.sql" });
133  0 ds.scan();
134  0 mojo.setFileset(ds);
135   
136  0 mojo.setOrderFile(SqlExecMojo.FILE_SORTING_ASC);
137  0 mojo.execute();
138   
139  0 assertEquals(6, mojo.getSuccessfulStatements());
140   
141  0 try {
142  0 mojo.setOrderFile(SqlExecMojo.FILE_SORTING_DSC);
143  0 mojo.execute();
144  0 fail("Execution is not aborted on error.");
145    } catch (MojoExecutionException e) {
146    }
147    }
148   
 
149  0 toggle public void testOnErrorContinueMojo() throws MojoExecutionException {
150  0 String command = "create table BOGUS"; // bad syntax
151  0 mojo.addText(command);
152  0 mojo.setOnError("continue");
153  0 mojo.execute();
154  0 assertEquals(0, mojo.getSuccessfulStatements());
155    }
156   
 
157  0 toggle public void testOnErrorAbortMojo() throws MojoExecutionException {
158  0 String command = "create table BOGUS"; // bad syntax
159  0 mojo.addText(command);
160   
161  0 try {
162  0 mojo.execute();
163  0 fail("Execution is not aborted on error.");
164   
165    } catch (MojoExecutionException e) {
166   
167    }
168   
169  0 assertEquals(0, mojo.getSuccessfulStatements());
170    }
171   
 
172  0 toggle public void testOnErrorAbortAfterMojo() throws MojoExecutionException {
173  0 String commands = "create table BOGUS"; // bad syntax
174   
175  0 mojo.addText(commands);
176   
177  0 File[] srcFiles = new File[1];
178  0 srcFiles[0] = new File("src/test/data/invalid-syntax.sql");
179   
180  0 assertTrue(srcFiles[0].exists());
181   
182  0 mojo.setSrcFiles(srcFiles);
183  0 mojo.setOnError("abortAfter");
184   
185  0 try {
186  0 mojo.execute();
187  0 fail("Execution is not aborted on error.");
188   
189    } catch (MojoExecutionException e) {
190    // expected
191    }
192   
193  0 assertEquals(0, mojo.getSuccessfulStatements());
194  0 assertEquals(2, mojo.getTotalStatements());
195    }
196   
 
197  0 toggle public void testDefaultUsernamePassword() throws MojoExecutionException {
198   
199  0 Settings settings = new Settings();
200  0 Server server = new Server();
201  0 settings.addServer(server);
202   
203  0 mojo.setSettings(settings);
204   
205    // force a lookup of username
206  0 mojo.setUsername(null);
207  0 mojo.setPassword(null);
208   
209  0 mojo.execute();
210   
211  0 assertEquals("", mojo.getUsername());
212  0 assertEquals("", mojo.getPassword());
213   
214    }
215   
 
216  0 toggle public void testUsernamePasswordLookup() throws MojoExecutionException {
217   
218  0 Settings settings = new Settings();
219  0 Server server = new Server();
220  0 server.setId("somekey");
221  0 server.setUsername("username");
222  0 server.setPassword("password");
223  0 settings.addServer(server);
224   
225  0 mojo.setSettings(settings);
226   
227    // force a lookup of username
228  0 mojo.setSettingsKey("somekey");
229  0 mojo.setUsername(null);
230  0 mojo.setPassword(null);
231   
232  0 mojo.execute();
233   
234  0 assertEquals("username", mojo.getUsername());
235  0 assertEquals("password", mojo.getPassword());
236   
237    }
238   
 
239  0 toggle public void testBadDriver() {
240  0 mojo.setDriver("bad-driver");
241  0 try {
242  0 mojo.execute();
243   
244  0 fail("Bad driver is not detected");
245    } catch (MojoExecutionException e) {
246   
247    }
248    }
249   
 
250  0 toggle public void testBadUrl() {
251  0 mojo.setUrl("bad-url");
252  0 try {
253  0 mojo.execute();
254   
255  0 fail("Bad URL is not detected");
256    } catch (MojoExecutionException e) {
257   
258    }
259    }
260   
 
261  0 toggle public void testBadFile() {
262  0 File[] srcFiles = new File[1];
263  0 srcFiles[0] = new File("a-every-bogus-file-that-does-not-exist");
264   
265  0 mojo.setSrcFiles(srcFiles);
266  0 try {
267  0 mojo.execute();
268   
269  0 fail("Bad files is not detected");
270    } catch (MojoExecutionException e) {
271   
272    }
273    }
274   
 
275  0 toggle public void testOnError() {
276  0 mojo.setOnError("AbOrT");
277  0 assertEquals(SqlExecMojo.ON_ERROR_ABORT, mojo.getOnError());
278  0 mojo.setOnError("cOnTiNuE");
279  0 assertEquals(SqlExecMojo.ON_ERROR_CONTINUE, mojo.getOnError());
280  0 try {
281  0 mojo.setOnError("bad");
282  0 fail(IllegalArgumentException.class.getName() + " was not thrown.");
283    } catch (IllegalArgumentException e) {
284    // expected
285    }
286  0 try {
287  0 mojo.setOnError(null);
288  0 fail(IllegalArgumentException.class.getName() + " was not thrown.");
289    } catch (IllegalArgumentException e) {
290    // expected
291    }
292    }
293   
 
294  0 toggle public void testSkip() throws MojoExecutionException {
295  0 String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
296  0 mojo.addText(command);
297  0 mojo.setSkip(true);
298  0 mojo.execute();
299   
300    // no command was executed due to skip is on
301  0 assertEquals(0, mojo.getSuccessfulStatements());
302    }
303   
 
304  0 toggle public void testDriverProperties() throws MojoExecutionException {
305  0 Properties driverProperties = this.mojo.getDriverProperties();
306  0 assertEquals(2, driverProperties.size());
307  0 assertEquals("value1", driverProperties.get("key1"));
308  0 assertEquals("value2", driverProperties.get("key2"));
309   
310  0 mojo.setDriverProperties("key1=value1,key2");
311  0 try {
312  0 driverProperties = this.mojo.getDriverProperties();
313    } catch (MojoExecutionException e) {
314    }
315   
316    }
317   
 
318  0 toggle public void testBlockMode() throws MojoExecutionException {
319  0 String command = "create table BLOCKTABLE ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
320  0 mojo.addText(command);
321  0 mojo.setEnableBlockMode(true);
322  0 mojo.execute();
323  0 assertEquals(1, mojo.getSuccessfulStatements());
324   
325  0 mojo.setSqlCommand("");
326  0 mojo.getTransactions().clear();
327  0 command = "drop table BLOCKTABLE";
328  0 mojo.addText(command);
329  0 mojo.execute();
330  0 assertEquals(1, mojo.getSuccessfulStatements());
331    }
332   
 
333  0 toggle public void testKeepFormat() throws MojoExecutionException {
334    // Normally a line starting in -- would be ignored, but with keepformat
335    // mode
336    // on it will not.
337  0 String command = "--create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
338  0 mojo.addText(command);
339  0 mojo.setKeepFormat(true);
340   
341  0 try {
342  0 mojo.execute();
343  0 fail("-- at the start of the SQL command is ignored.");
344    } catch (MojoExecutionException e) {
345    }
346   
347  0 assertEquals(0, mojo.getSuccessfulStatements());
348   
349    }
350   
 
351  0 toggle public void testBadDelimiter() throws Exception {
352  0 String command = "create table SEPARATOR ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar):"
353    + "create table SEPARATOR2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
354   
355  0 mojo.addText(command);
356  0 mojo.setDelimiter(":");
357   
358  0 try {
359  0 mojo.execute();
360  0 fail("Expected parser error.");
361    } catch (MojoExecutionException e) {
362    }
363    }
364   
 
365  0 toggle public void testGoodDelimiter() throws Exception {
366  0 String command = "create table SEPARATOR ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)\n:\n"
367    + "create table SEPARATOR2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
368   
369  0 mojo.addText(command);
370  0 mojo.setDelimiter(":");
371   
372  0 mojo.execute();
373   
374  0 assertEquals(2, mojo.getSuccessfulStatements());
375    }
376   
 
377  0 toggle public void testBadDelimiterType() throws Exception {
378  0 String command = "create table BADDELIMTYPE ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)" + "\n:"
379    + "create table BADDELIMTYPE2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
380   
381  0 mojo.addText(command);
382  0 mojo.setDelimiter(":");
383  0 mojo.setDelimiterType(DelimiterType.ROW);
384   
385  0 try {
386  0 mojo.execute();
387  0 fail("Expected parser error.");
388    } catch (MojoExecutionException e) {
389    }
390    }
391   
 
392  0 toggle public void testGoodDelimiterType() throws Exception {
393  0 String command = "create table GOODDELIMTYPE ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)"
394    + "\n: \n" + "create table GOODDELIMTYPE2 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
395   
396  0 mojo.addText(command);
397  0 mojo.setDelimiter(":");
398  0 mojo.setDelimiterType(DelimiterType.ROW);
399   
400  0 mojo.execute();
401  0 assertEquals(2, mojo.getSuccessfulStatements());
402    }
403   
 
404  0 toggle @SuppressWarnings("deprecation")
405    public void testOutputFile() throws Exception {
406  0 String command = "create table GOODDELIMTYPE3 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)"
407    + "\n: \n" + "create table GOODDELIMTYPE4 ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
408   
409  0 mojo.addText(command);
410  0 mojo.setDelimiter(":");
411  0 mojo.setDelimiterType(DelimiterType.ROW);
412   
413  0 String basedir = System.getProperty("basedir", ".");
414  0 File outputFile = new File(basedir, "target/sql.out");
415  0 outputFile.delete();
416  0 mojo.setOutputFile(outputFile);
417  0 mojo.setPrintResutlSet(true);
418   
419  0 mojo.execute();
420   
421  0 assertTrue("Output file: " + outputFile + " not found.", outputFile.exists());
422   
423  0 assertTrue("Unexpected empty output file. ", outputFile.length() > 0);
424   
425    // makesure we can remote the file, it is not locked
426    // assertTrue( outputFile.delete() );
427   
428    }
429    }