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