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 007 * in compliance with 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 012 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 013 * or implied. See the License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 017 import java.io.File; 018 import java.util.Properties; 019 020 import org.apache.maven.plugin.MojoExecutionException; 021 import org.apache.maven.plugin.testing.AbstractMojoTestCase; 022 import org.apache.maven.settings.Server; 023 import org.apache.maven.settings.Settings; 024 import org.apache.maven.shared.filtering.MavenFileFilter; 025 026 /** 027 * Unit test for simple SqlExecMojo. 028 */ 029 public class SqlExecMojoTest extends AbstractMojoTestCase { 030 private SqlExecMojo mojo; 031 032 private Properties p; 033 034 @Override 035 public void setUp() throws Exception { 036 super.setUp(); 037 p = new Properties(); 038 p.load(getClass().getResourceAsStream("/test.properties")); 039 040 mojo = new SqlExecMojo(); 041 042 // populate parameters 043 mojo.setDriver(p.getProperty("driver")); 044 mojo.setUsername(p.getProperty("user")); 045 mojo.setPassword(p.getProperty("password")); 046 mojo.setUrl(p.getProperty("url")); 047 mojo.setDriverProperties(p.getProperty("driverProperties")); 048 049 MavenFileFilter filter = (MavenFileFilter) lookup("org.apache.maven.shared.filtering.MavenFileFilter", 050 "default"); 051 mojo.setFileFilter(filter); 052 053 } 054 055 /** 056 * No error when there is no input 057 */ 058 public void testNoCommandMojo() throws MojoExecutionException { 059 mojo.execute(); 060 061 assertEquals(0, mojo.getSuccessfulStatements()); 062 } 063 064 public void testCreateCommandMojo() throws MojoExecutionException { 065 String command = "create table PERSON ( PERSON_ID integer, FIRSTNAME varchar, LASTNAME varchar)"; 066 mojo.addText(command); 067 mojo.execute(); 068 069 assertEquals(1, mojo.getSuccessfulStatements()); 070 } 071 072 public void testDropCommandMojo() throws MojoExecutionException { 073 String command = "drop table PERSON"; 074 mojo.addText(command); 075 mojo.execute(); 076 assertEquals(1, mojo.getSuccessfulStatements()); 077 } 078 079 public void testFileSetMojo() throws MojoExecutionException { 080 081 Fileset ds = new Fileset(); 082 ds.setBasedir("src/test"); 083 ds.setIncludes(new String[] { "**/create*.sql" }); 084 ds.scan(); 085 assert (ds.getIncludedFiles().length == 1); 086 087 mojo.setFileset(ds); 088 089 mojo.execute(); 090 091 assertEquals(3, mojo.getSuccessfulStatements()); 092 093 } 094 095 public void testFileArrayMojo() throws MojoExecutionException { 096 File[] srcFiles = new File[1]; 097 srcFiles[0] = new File("src/test/data/drop-test-tables.sql"); 098 099 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 }