001: package org.dbbrowser.db.engine.queryengine;
002:
003: import infrastructure.logging.Log;
004: import java.io.IOException;
005: import java.sql.Connection;
006: import java.sql.SQLException;
007: import java.util.Iterator;
008: import java.util.List;
009: import junit.framework.TestCase;
010: import org.dbbrowser.db.engine.exception.DBEngineException;
011: import org.dbbrowser.db.engine.model.DBTable;
012: import org.dbbrowser.db.engine.queryengine.MySQLDBQueryEngine;
013: import org.dbbrowser.drivermanager.ConnectionInfo;
014: import org.dbbrowser.drivermanager.ConnectionInfoSerializer;
015: import org.dbbrowser.drivermanager.DBBrowserDriverManager;
016: import org.dbbrowser.drivermanager.DriverManagerException;
017: import org.dbbrowser.ui.UIControllerForQueries;
018:
019: public class MySQLDBQueryEngineTest extends TestCase {
020: private MySQLDBQueryEngine mySQLDBQueryEngine = null;
021: private static final String SCHEMA_NAME = "hww";
022: private static final String TABLE_NAME = "customer";
023:
024: public MySQLDBQueryEngineTest(String name) {
025: super (name);
026: }
027:
028: public void setUp() {
029: try {
030: Log
031: .getInstance()
032: .debugMessage(
033: "*** Setting up connection for mySQL database *** ",
034: this .getClass().getName());
035:
036: //Get the connection info
037: List listOfConnectionInfo = ConnectionInfoSerializer
038: .deserialize();
039:
040: //Get the first mysql connection info
041: Iterator i = listOfConnectionInfo.iterator();
042: ConnectionInfo ci = null;
043: while (i.hasNext()) {
044: ConnectionInfo connectionInfo = (ConnectionInfo) i
045: .next();
046: if (connectionInfo.getDBMSType().equals(
047: UIControllerForQueries.MYSQL_DBMS)) {
048: ci = connectionInfo;
049: break;
050: }
051: }
052:
053: //Get the connection if connection info is not null
054: if (ci != null) {
055: Connection conn = DBBrowserDriverManager.getInstance()
056: .getConnection(ci, null);
057:
058: //Setup the query engine
059: mySQLDBQueryEngine = new MySQLDBQueryEngine(conn
060: .createStatement());
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 testListSchemas() {
095: try {
096: Log.getInstance().debugMessage(
097: "*** testListSchemas mySQL database *** ",
098: this .getClass().getName());
099:
100: //Get the list of table spaces
101: List listOfSchemas = mySQLDBQueryEngine.listSchemas();
102:
103: assertTrue("Schema " + SCHEMA_NAME + " found",
104: listOfSchemas.contains(SCHEMA_NAME));
105: } catch (DBEngineException exc) {
106: Log.getInstance().fatalMessage(
107: "*** DBEngineException *** " + exc.getMessage(),
108: this .getClass().getName());
109: fail(exc.getMessage());
110: }
111: }
112:
113: public void testListIndexes() {
114: try {
115: Log.getInstance().debugMessage(
116: "*** testListIndexes mySQL database *** ",
117: this .getClass().getName());
118:
119: //Get the list of table spaces
120: DBTable tableForIndexes = mySQLDBQueryEngine.listIndexes();
121:
122: assertTrue("MySQL database has "
123: + tableForIndexes.getListOfColumnInfos().size()
124: + " columns, should be 13", tableForIndexes
125: .getListOfColumnInfos().size() == 13);
126: } catch (DBEngineException exc) {
127: Log.getInstance().fatalMessage(
128: "*** DBEngineException *** " + exc.getMessage(),
129: this .getClass().getName());
130: fail(exc.getMessage());
131: }
132: }
133:
134: public void testListTablesInSchema() {
135: try {
136: Log
137: .getInstance()
138: .debugMessage(
139: "*** testListTablesInSchema for mySQL database *** ",
140: this .getClass().getName());
141:
142: //Get the list of tables
143: List listOfTables = mySQLDBQueryEngine
144: .listTablesInSchema(SCHEMA_NAME);
145:
146: assertTrue("Table " + TABLE_NAME + " found in schema "
147: + SCHEMA_NAME, listOfTables.contains(TABLE_NAME));
148: } catch (DBEngineException exc) {
149: Log.getInstance().fatalMessage(
150: "*** DBEngineException *** " + exc.getMessage(),
151: this .getClass().getName());
152: fail(exc.getMessage());
153: }
154: }
155:
156: public void testGetAllDataInATable() {
157: try {
158: Log
159: .getInstance()
160: .debugMessage(
161: "*** testGetAllDataInATable for mySQL database *** ",
162: this .getClass().getName());
163:
164: //Get the data in the form of a dbtable
165: DBTable dbTable = mySQLDBQueryEngine.getAllDataInATable(
166: SCHEMA_NAME, TABLE_NAME, new Integer(0),
167: new Integer(100));
168: } catch (DBEngineException exc) {
169: Log.getInstance().fatalMessage(
170: "*** DBEngineException *** " + exc.getMessage(),
171: this .getClass().getName());
172: fail(exc.getMessage());
173: }
174: }
175:
176: public void testGetPrimaryKeyColumnNames() {
177: try {
178: Log
179: .getInstance()
180: .debugMessage(
181: "*** testGetPrimaryKeyColumnNames for MySQL DBMS *** ",
182: this .getClass().getName());
183:
184: //Get the primary keys for the table
185: List listOfPrimaryKeyColumnNames = mySQLDBQueryEngine
186: .getPrimaryKeyColumnNames(SCHEMA_NAME, TABLE_NAME);
187: assertTrue("List of primary key in table " + TABLE_NAME
188: + "should be 1",
189: listOfPrimaryKeyColumnNames.size() == 1);
190: } catch (DBEngineException exc) {
191: Log.getInstance().fatalMessage(
192: "*** DBEngineException *** " + exc.getMessage(),
193: this .getClass().getName());
194: fail(exc.getMessage());
195: }
196: }
197:
198: public void testListColumnsInATable() {
199: try {
200: Log.getInstance().debugMessage(
201: "*** testListColumnsInATable for MySQL DBMS *** ",
202: this .getClass().getName());
203:
204: //Get the list of columns in the table
205: List listColumnInfos = mySQLDBQueryEngine
206: .listColumnsInATable(SCHEMA_NAME, TABLE_NAME);
207: assertTrue("List of columns in table " + TABLE_NAME
208: + "should be 13", listColumnInfos.size() == 13);
209: } catch (DBEngineException exc) {
210: Log.getInstance().fatalMessage(
211: "*** DBEngineException *** " + exc.getMessage(),
212: this.getClass().getName());
213: fail(exc.getMessage());
214: }
215: }
216: }
|