View Javadoc

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