001: package org.dbbrowser.db.engine.rawsqlengine;
002:
003: import junit.framework.TestCase;
004: import org.dbbrowser.db.engine.exception.DBEngineException;
005: import org.dbbrowser.db.engine.model.DBTable;
006: import org.dbbrowser.db.engine.rawsqlengine.GenericRawSQLEngine;
007: import org.dbbrowser.drivermanager.ConnectionInfoSerializer;
008: import org.dbbrowser.drivermanager.ConnectionInfo;
009: import org.dbbrowser.drivermanager.DBBrowserDriverManager;
010: import org.dbbrowser.drivermanager.DriverManagerException;
011: import infrastructure.logging.Log;
012: import java.util.List;
013: import java.util.Iterator;
014: import java.sql.Connection;
015: import java.sql.SQLException;
016: import java.io.IOException;
017:
018: public class MySQLGenericRawSQLEngineAutoCommitOffTest extends TestCase {
019: private GenericRawSQLEngine genericRawSQLEngine = null;
020: private Connection conn = null;
021:
022: public MySQLGenericRawSQLEngineAutoCommitOffTest(String name) {
023: super (name);
024: }
025:
026: public void setUp() {
027: try {
028: Log
029: .getInstance()
030: .debugMessage(
031: "*** Setting up connection for MySQL database *** ",
032: this .getClass().getName());
033:
034: //Get the connection info
035: List listOfConnectionInfo = ConnectionInfoSerializer
036: .deserialize();
037:
038: //Get the Oracle connection info
039: Iterator i = listOfConnectionInfo.iterator();
040: ConnectionInfo ci = null;
041: while (i.hasNext()) {
042: ConnectionInfo connectionInfo = (ConnectionInfo) i
043: .next();
044: if (connectionInfo.getName().equals("HWW on MySQL")) {
045: ci = connectionInfo;
046: break;
047: }
048: }
049:
050: //Get the connection if connection info is not null
051: if (ci != null) {
052: this .conn = DBBrowserDriverManager.getInstance()
053: .getConnection(ci, null);
054:
055: //Setup the update engine
056: genericRawSQLEngine = new GenericRawSQLEngine(conn
057: .createStatement());
058:
059: //Set autocommit off
060: conn.setAutoCommit(false);
061: } else {
062: Log
063: .getInstance()
064: .fatalMessage(
065: "*** No Connection info found for MySQL database *** ",
066: this .getClass().getName());
067: fail("*** No Connection info found for MySQL database *** ");
068: }
069: } catch (ClassNotFoundException exc) {
070: Log.getInstance().fatalMessage(
071: "*** ClassNotFoundException *** "
072: + exc.getMessage(),
073: this .getClass().getName());
074: fail(exc.getMessage());
075: } catch (IOException exc) {
076: Log.getInstance().fatalMessage(
077: "*** IOException *** " + exc.getMessage(),
078: this .getClass().getName());
079: fail(exc.getMessage());
080: } catch (DriverManagerException exc) {
081: Log.getInstance().fatalMessage(
082: "*** DriverManagerException *** "
083: + exc.getMessage(),
084: this .getClass().getName());
085: fail(exc.getMessage());
086: } catch (SQLException exc) {
087: Log.getInstance().fatalMessage(
088: "*** SQLException *** " + exc.getMessage(),
089: this .getClass().getName());
090: fail(exc.getMessage());
091: }
092: }
093:
094: public void testRunRawSelectSQLStatement() {
095: try {
096: Log
097: .getInstance()
098: .debugMessage(
099: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawSelectSQLStatement *** ",
100: this .getClass().getName());
101:
102: DBTable dbTable = this .genericRawSQLEngine
103: .runRawSQL("select count(*) from hww.customer");
104: assertTrue("Number of rows should be 1", dbTable
105: .getNumberOfRowsInTable().intValue() == 1);
106: } catch (DBEngineException exc) {
107: Log.getInstance().fatalMessage(
108: "*** DBEngineException *** " + exc.getMessage(),
109: this .getClass().getName());
110: fail(exc.getMessage());
111: }
112: }
113:
114: public void testRunRawInsertSQLStatement() {
115: try {
116: Log
117: .getInstance()
118: .debugMessage(
119: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawInsertSQLStatement *** ",
120: this .getClass().getName());
121:
122: String sql = "insert into hww.customer (pk_customerid, last_updated_at, last_updated_by "
123: + ") values ( 3, '2005-10-21', 'JUnit')";
124: DBTable dbTable = this .genericRawSQLEngine.runRawSQL(sql);
125: assertTrue("DBTable should be null", dbTable == null);
126: this .conn.commit();
127: } catch (DBEngineException exc) {
128: Log.getInstance().fatalMessage(
129: "*** DBEngineException *** " + exc.getMessage(),
130: this .getClass().getName());
131: fail(exc.getMessage());
132: } catch (SQLException exc) {
133: Log.getInstance().fatalMessage(
134: "*** SQLException *** " + exc.getMessage(),
135: this .getClass().getName());
136: fail(exc.getMessage());
137: }
138: }
139:
140: public void testRunRawUpdateSQLStatement() {
141: try {
142: Log
143: .getInstance()
144: .debugMessage(
145: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawUpdateSQLStatement *** ",
146: this .getClass().getName());
147:
148: DBTable dbTable = this .genericRawSQLEngine
149: .runRawSQL("update hww.customer set firstname = 'JUnit' where pk_customerid=3");
150: assertTrue(
151: "MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawUpdateSQLStatement should be null",
152: dbTable == null);
153: this .conn.commit();
154: } catch (DBEngineException exc) {
155: Log.getInstance().fatalMessage(
156: "*** DBEngineException *** " + exc.getMessage(),
157: this .getClass().getName());
158: fail(exc.getMessage());
159: } catch (SQLException exc) {
160: Log.getInstance().fatalMessage(
161: "*** SQLException *** " + exc.getMessage(),
162: this .getClass().getName());
163: fail(exc.getMessage());
164: }
165: }
166:
167: public void testRunRawDeleteSQLStatement() {
168: try {
169: Log
170: .getInstance()
171: .debugMessage(
172: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawDeleteSQLStatement *** ",
173: this .getClass().getName());
174:
175: DBTable dbTable = this .genericRawSQLEngine
176: .runRawSQL("delete from hww.customer where pk_customerid=3");
177: assertTrue(
178: "MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawDeleteSQLStatement should be null",
179: dbTable == null);
180: this .conn.commit();
181: } catch (DBEngineException exc) {
182: Log.getInstance().fatalMessage(
183: "*** DBEngineException *** " + exc.getMessage(),
184: this .getClass().getName());
185: fail(exc.getMessage());
186: } catch (SQLException exc) {
187: Log.getInstance().fatalMessage(
188: "*** SQLException *** " + exc.getMessage(),
189: this .getClass().getName());
190: fail(exc.getMessage());
191: }
192: }
193:
194: public void testRunRawAddColumnSQLStatement() {
195: try {
196: Log
197: .getInstance()
198: .debugMessage(
199: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawAddColumnSQLStatement *** ",
200: this .getClass().getName());
201:
202: DBTable dbTable = this .genericRawSQLEngine
203: .runRawSQL("alter table hww.customer add test_column varchar(10)");
204: assertTrue(
205: "MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawAlterTableSQLStatement should be null",
206: dbTable == null);
207: this .conn.commit();
208: } catch (DBEngineException exc) {
209: Log.getInstance().fatalMessage(
210: "*** DBEngineException *** " + exc.getMessage(),
211: this .getClass().getName());
212: fail(exc.getMessage());
213: } catch (SQLException exc) {
214: Log.getInstance().fatalMessage(
215: "*** SQLException *** " + exc.getMessage(),
216: this .getClass().getName());
217: fail(exc.getMessage());
218: }
219: }
220:
221: public void testRunRawDropColumnSQLStatement() {
222: try {
223: Log
224: .getInstance()
225: .debugMessage(
226: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawDropColumnSQLStatement *** ",
227: this .getClass().getName());
228:
229: DBTable dbTable = this .genericRawSQLEngine
230: .runRawSQL("alter table hww.customer drop column test_column");
231: assertTrue(
232: "MySQLGenericRawSQLEngineAutoCommitOffTest.testRunRawAlterTableSQLStatement should be null",
233: dbTable == null);
234: this .conn.commit();
235: } catch (DBEngineException exc) {
236: Log.getInstance().fatalMessage(
237: "*** DBEngineException *** " + exc.getMessage(),
238: this .getClass().getName());
239: fail(exc.getMessage());
240: } catch (SQLException exc) {
241: Log.getInstance().fatalMessage(
242: "*** SQLException *** " + exc.getMessage(),
243: this .getClass().getName());
244: fail(exc.getMessage());
245: }
246: }
247:
248: public void testStripComments() {
249: Log
250: .getInstance()
251: .debugMessage(
252: "*** MySQLGenericRawSQLEngineAutoCommitOffTest.testStripComments *** ",
253: this .getClass().getName());
254:
255: String sql = this .genericRawSQLEngine
256: .stripComments("alter table CLAIM_EXPERTS /*Test comments*/drop column test_column");
257: assertTrue(
258: "GenericRawSQLEngineTest.testStripComments sql is incorrect",
259: "alter table CLAIM_EXPERTS drop column test_column"
260: .equals(sql));
261: }
262: }
|