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 |
|
|
|
|
| 0% |
Uncovered Elements: 220 (220) |
Complexity: 39 |
Complexity Density: 0.2 |
|
28 |
|
public class SqlExecMojoTest extends AbstractMojoTestCase { |
29 |
|
private SqlExecMojo mojo; |
30 |
|
|
31 |
|
private Properties p; |
32 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
33 |
0
|
@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 |
|
|
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 |
|
|
56 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
4
-
|
|
57 |
0
|
public void testNoCommandMojo() throws MojoExecutionException {... |
58 |
0
|
mojo.execute(); |
59 |
|
|
60 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
61 |
|
} |
62 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4
-
|
|
63 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4
-
|
|
71 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
78 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
94 |
0
|
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 |
|
|
107 |
|
|
108 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
4
-
|
|
109 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
4
-
|
|
128 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
148 |
0
|
public void testOnErrorContinueMojo() throws MojoExecutionException {... |
149 |
0
|
String command = "create table BOGUS"; |
150 |
0
|
mojo.addText(command); |
151 |
0
|
mojo.setOnError("continue"); |
152 |
0
|
mojo.execute(); |
153 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
154 |
|
} |
155 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
156 |
0
|
public void testOnErrorAbortMojo() throws MojoExecutionException {... |
157 |
0
|
String command = "create table BOGUS"; |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
4
-
|
|
171 |
0
|
public void testOnErrorAbortAfterMojo() throws MojoExecutionException {... |
172 |
0
|
String commands = "create table BOGUS"; |
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 |
|
|
190 |
|
} |
191 |
|
|
192 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
193 |
0
|
assertEquals(2, mojo.getTotalStatements()); |
194 |
|
} |
195 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
4
-
|
|
196 |
0
|
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
4
-
|
|
215 |
0
|
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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
4
-
|
|
238 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
4
-
|
|
249 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
260 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.3 |
4
-
|
|
274 |
0
|
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 |
|
|
284 |
|
} |
285 |
0
|
try { |
286 |
0
|
mojo.setOnError(null); |
287 |
0
|
fail(IllegalArgumentException.class.getName() + " was not thrown."); |
288 |
|
} catch (IllegalArgumentException e) { |
289 |
|
|
290 |
|
} |
291 |
|
} |
292 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
293 |
0
|
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 |
|
|
300 |
0
|
assertEquals(0, mojo.getSuccessfulStatements()); |
301 |
|
} |
302 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
303 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
4
-
|
|
317 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
332 |
0
|
public void testKeepFormat() throws MojoExecutionException {... |
333 |
|
|
334 |
|
|
335 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
350 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
364 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
4
-
|
|
376 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
4
-
|
|
391 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
4
-
|
|
403 |
0
|
@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 |
|
|
425 |
|
|
426 |
|
|
427 |
|
} |
428 |
|
} |