1 package org.codehaus.mojo.sql;
2
3
4
5
6
7
8
9
10
11
12
13
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
27
28 public class SqlExecMojoTest extends AbstractMojoTestCase {
29 private SqlExecMojo mojo;
30
31 private Properties p;
32
33 @Override
34 public void setUp() throws Exception {
35 super.setUp();
36 p = new Properties();
37 p.load(getClass().getResourceAsStream("/test.properties"));
38
39 mojo = new SqlExecMojo();
40
41
42 mojo.setDriver(p.getProperty("driver"));
43 mojo.setUsername(p.getProperty("user"));
44 mojo.setPassword(p.getProperty("password"));
45 mojo.setUrl(p.getProperty("url"));
46 mojo.setDriverProperties(p.getProperty("driverProperties"));
47
48 MavenFileFilter filter = (MavenFileFilter) lookup("org.apache.maven.shared.filtering.MavenFileFilter",
49 "default");
50 mojo.setFileFilter(filter);
51
52 }
53
54
55
56
57 public void testNoCommandMojo() throws MojoExecutionException {
58 mojo.execute();
59
60 assertEquals(0, mojo.getSuccessfulStatements());
61 }
62
63 public void testCreateCommandMojo() throws MojoExecutionException {
64 String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)";
65 mojo.addText(command);
66 mojo.execute();
67
68 assertEquals(1, mojo.getSuccessfulStatements());
69 }
70
71 public void testDropCommandMojo() throws MojoExecutionException {
72 String command = "drop table PERSON";
73 mojo.addText(command);
74 mojo.execute();
75 assertEquals(1, mojo.getSuccessfulStatements());
76 }
77
78 public void testFileSetMojo() throws MojoExecutionException {
79
80 Fileset ds = new Fileset();
81 ds.setBasedir("src/test");
82 ds.setIncludes(new String[] { "**/create*.sql" });
83 ds.scan();
84 assert (ds.getIncludedFiles().length == 1);
85
86 mojo.setFileset(ds);
87
88 mojo.execute();
89
90 assertEquals(3, mojo.getSuccessfulStatements());
91
92 }
93
94 public void testFileArrayMojo() throws MojoExecutionException {
95 File[] srcFiles = new File[1];
96 srcFiles[0] = new File("src/test/data/drop-test-tables.sql");
97
98 mojo.setSrcFiles(srcFiles);
99 mojo.execute();
100
101 assertEquals(3, mojo.getSuccessfulStatements());
102
103 }
104
105
106
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";
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";
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";
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
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
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
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
284 }
285 try {
286 mojo.setOnError(null);
287 fail(IllegalArgumentException.class.getName() + " was not thrown.");
288 } catch (IllegalArgumentException e) {
289
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
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
334
335
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
425
426
427 }
428 }