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