001: /*
002: * DefaultStatementRunnerTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql;
013:
014: import junit.framework.TestCase;
015: import workbench.TestUtil;
016: import workbench.db.ConnectionMgr;
017: import workbench.db.WbConnection;
018: import workbench.sql.commands.DdlCommand;
019: import workbench.sql.commands.UpdatingCommand;
020: import workbench.sql.wbcommands.WbCopy;
021: import workbench.sql.wbcommands.WbDefineVar;
022: import workbench.sql.wbcommands.WbFeedback;
023: import workbench.sql.wbcommands.WbInclude;
024:
025: /**
026: *
027: * @author support@sql-workbench.net
028: */
029: public class DefaultStatementRunnerTest extends TestCase {
030: private TestUtil util;
031:
032: public DefaultStatementRunnerTest(String testName) {
033: super (testName);
034: util = new TestUtil(testName);
035: }
036:
037: public void testWbCommands() throws Exception {
038: try {
039: util.prepareEnvironment();
040: DefaultStatementRunner runner = util
041: .createConnectedStatementRunner();
042: WbConnection con = runner.getConnection();
043:
044: runner.setVerboseLogging(true);
045:
046: String sql = "--comment\n\nwbfeedback off";
047: SqlCommand command = runner.cmdMapper.getCommandToUse(sql);
048: assertTrue(command instanceof WbFeedback);
049: runner.runStatement(sql, -1, -1);
050:
051: boolean verbose = runner.getVerboseLogging();
052: assertEquals("Feedback not executed", false, verbose);
053: } catch (Exception e) {
054: e.printStackTrace();
055: } finally {
056: ConnectionMgr.getInstance().disconnectAll();
057: }
058: }
059:
060: /**
061: * Test of getCommandToUse method, of class workbench.sql.DefaultStatementRunner.
062: */
063: public void testCommands() throws Exception {
064: String sql = "\n\ninsert into bla (col) values (1)";
065: DefaultStatementRunner runner = new DefaultStatementRunner();
066: SqlCommand command = runner.cmdMapper.getCommandToUse(sql);
067: assertSame(command, UpdatingCommand.INSERT);
068:
069: sql = "--do something\nupdate bla set col = value";
070: command = runner.cmdMapper.getCommandToUse(sql);
071: assertSame(command, UpdatingCommand.UPDATE);
072: assertEquals(true, command.isUpdatingCommand());
073:
074: sql = " delete from bla";
075: command = runner.cmdMapper.getCommandToUse(sql);
076: assertSame(command, UpdatingCommand.DELETE);
077: assertEquals(true, command.isUpdatingCommand());
078:
079: sql = " create table bla (col integer);";
080: command = runner.cmdMapper.getCommandToUse(sql);
081: assertSame(command, DdlCommand.CREATE);
082: assertEquals(true, command.isUpdatingCommand());
083:
084: sql = "-- comment\n\n\ncreate view bla as select * from blub;";
085: command = runner.cmdMapper.getCommandToUse(sql);
086: assertSame(command, DdlCommand.CREATE);
087:
088: sql = "-- comment\n\n\ncreate \nor \nreplace \nview bla as select * from blub;";
089: command = runner.cmdMapper.getCommandToUse(sql);
090: assertSame(command, DdlCommand.CREATE);
091:
092: sql = "-- comment\n\n\ncreate trigger bla;";
093: command = runner.cmdMapper.getCommandToUse(sql);
094: assertSame(command, DdlCommand.CREATE);
095:
096: sql = " drop table bla (col integer);";
097: command = runner.cmdMapper.getCommandToUse(sql);
098: assertSame(command, DdlCommand.DROP);
099: assertEquals(true, command.isUpdatingCommand());
100:
101: sql = "/* this is \n a comment \n*/\n-- comment\nalter table bla drop constraint xyz;";
102: command = runner.cmdMapper.getCommandToUse(sql);
103: assertSame(command, DdlCommand.ALTER);
104: assertEquals(true, command.isUpdatingCommand());
105:
106: boolean isDrop = ((DdlCommand) command).isDropCommand(sql);
107: assertEquals(true, isDrop);
108:
109: sql = " -- comment\n wbvardefine x=42;";
110: command = runner.cmdMapper.getCommandToUse(sql);
111: assertSame(command, WbDefineVar.DEFINE_LONG);
112: assertEquals(false, command.isUpdatingCommand());
113:
114: sql = "-- bla\nwbvardef x=42;";
115: command = runner.cmdMapper.getCommandToUse(sql);
116: assertSame(command, WbDefineVar.DEFINE_SHORT);
117:
118: sql = " -- comment\nwbcopy -sourceprofile=x";
119: command = runner.cmdMapper.getCommandToUse(sql);
120: assertTrue(command instanceof WbCopy);
121:
122: sql = "@file.sql";
123: command = runner.cmdMapper.getCommandToUse(sql);
124: assertTrue(command instanceof WbInclude);
125:
126: sql = "-- run the second script\n/* bla blub */\nwbinclude -file=file.sql";
127: command = runner.cmdMapper.getCommandToUse(sql);
128: assertTrue(command instanceof WbInclude);
129: }
130: }
|