1 |
|
package org.codehaus.mojo.sql; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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 |
|
|
29 |
|
|
|
|
| 0% |
Uncovered Elements: 220 (220) |
Complexity: 39 |
Complexity Density: 0.2 |
|
30 |
|
public class SqlExecMojoTest extends AbstractMojoTestCase { |
31 |
|
private SqlExecMojo mojo; |
32 |
|
|
33 |
|
private Properties p; |
34 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
35 |
0
|
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 |
|
|
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 |
|
|
57 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
4
-
|
|
58 |
0
|
public void testNoCommandMojo() throws MojoExecutionException {... |
59 |
0
|
mojo.execute(); |
60 |
|
|
61 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
62 |
|
} |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4
-
|
|
64 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4
-
|
|
72 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
79 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
95 |
0
|
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 |
|
|
108 |
|
|
109 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
4
-
|
|
110 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
4
-
|
|
129 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
149 |
0
|
public void testOnErrorContinueMojo() throws MojoExecutionException {... |
150 |
0
|
String command = "create table BOGUS"; |
151 |
0
|
mojo.addText(command); |
152 |
0
|
mojo.setOnError("continue"); |
153 |
0
|
mojo.execute(); |
154 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
155 |
|
} |
156 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
157 |
0
|
public void testOnErrorAbortMojo() throws MojoExecutionException {... |
158 |
0
|
String command = "create table BOGUS"; |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
4
-
|
|
172 |
0
|
public void testOnErrorAbortAfterMojo() throws MojoExecutionException {... |
173 |
0
|
String commands = "create table BOGUS"; |
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 |
|
|
191 |
|
} |
192 |
|
|
193 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
194 |
0
|
assertEquals(2, mojo.getTotalStatements()); |
195 |
|
} |
196 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
4
-
|
|
197 |
0
|
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
4
-
|
|
216 |
0
|
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
4
-
|
|
239 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
4
-
|
|
250 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
261 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.3 |
4
-
|
|
275 |
0
|
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 |
|
|
285 |
|
} |
286 |
0
|
try { |
287 |
0
|
mojo.setOnError(null); |
288 |
0
|
fail(IllegalArgumentException.class.getName() + " was not thrown."); |
289 |
|
} catch (IllegalArgumentException e) { |
290 |
|
|
291 |
|
} |
292 |
|
} |
293 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
294 |
0
|
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 |
|
|
301 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
302 |
|
} |
303 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
304 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
4
-
|
|
318 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
333 |
0
|
public void testKeepFormat() throws MojoExecutionException {... |
334 |
|
|
335 |
|
|
336 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
351 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
365 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
377 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
4
-
|
|
392 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
4
-
|
|
404 |
0
|
@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 |
|
|
426 |
|
|
427 |
|
|
428 |
|
} |
429 |
|
} |