001: /*
002: * Copyright (C) 2006 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.session;
020:
021: import java.sql.Connection;
022: import java.sql.DriverManager;
023: import java.sql.SQLException;
024:
025: import javax.swing.Action;
026: import javax.swing.JComponent;
027:
028: import net.sourceforge.squirrel_sql.client.IApplication;
029: import net.sourceforge.squirrel_sql.client.MockApplication;
030: import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
031: import net.sourceforge.squirrel_sql.client.gui.db.ISQLAliasExt;
032: import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
033: import net.sourceforge.squirrel_sql.client.gui.session.BaseSessionInternalFrame;
034: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
035: import net.sourceforge.squirrel_sql.client.gui.session.SessionPanel;
036: import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
037: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
038: import net.sourceforge.squirrel_sql.client.session.mainpanel.IMainPanelTab;
039: import net.sourceforge.squirrel_sql.client.session.parser.IParserEventsProcessor;
040: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
041: import net.sourceforge.squirrel_sql.client.session.schemainfo.SchemaInfo;
042: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
043: import net.sourceforge.squirrel_sql.fw.id.UidIdentifier;
044: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
045: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
046: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
047: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
048: import net.sourceforge.squirrel_sql.fw.sql.MockSQLDriver;
049: import net.sourceforge.squirrel_sql.fw.sql.QueryTokenizer;
050: import net.sourceforge.squirrel_sql.fw.sql.SQLConnection;
051: import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
052: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
053: import net.sourceforge.squirrel_sql.fw.util.MockMessageHandler;
054: import net.sourceforge.squirrel_sql.mo.sql.MockDatabaseMetaData;
055:
056: import com.mockobjects.sql.MockConnection2;
057:
058: public class MockSession implements ISession {
059:
060: ISQLAliasExt sqlAlias = null;
061: ISQLDriver sqlDriver = null;
062: SQLConnection con = null;
063: MockDatabaseMetaData mdata = null;
064: MockApplication app = null;
065: SessionProperties props = null;
066: IMessageHandler messageHandler = null;
067: SchemaInfo schemaInfo = null;
068: SessionPanel sessionPanel = null;
069: SquirrelPreferences prefs = null;
070: UidIdentifier id = null;
071: boolean closed;
072:
073: // These tell the Dialect test runner where tables that is creates can be
074: // found.
075: private String defaultCatalog = "";
076: private String defaultSchema = "";
077:
078: public MockSession() {
079: init(true);
080: }
081:
082: public MockSession(String className, String jdbcUrl, String u,
083: String p) throws Exception {
084: System.out.println("Attempting to load class=" + className);
085: Class.forName(className);
086: System.out.println("Getting connection for url=" + jdbcUrl);
087: Connection c = DriverManager.getConnection(jdbcUrl, u, p);
088: sqlDriver = new MockSQLDriver(className, jdbcUrl);
089: con = new SQLConnection(c, null, sqlDriver);
090: init(false);
091: sqlAlias.setUrl(jdbcUrl);
092: sqlAlias.setUserName(u);
093: sqlAlias.setPassword(p);
094: sqlDriver.setDriverClassName(className);
095: }
096:
097: private void init(boolean initConnection) {
098: if (initConnection) {
099: //MockConnection2 mockCon = getMockConnection();
100: con = new SQLConnection(getMockConnection(), null,
101: sqlDriver);
102: }
103: id = new UidIdentifier();
104: messageHandler = new MockMessageHandler();
105: props = new SessionProperties();
106: props.setLoadSchemasCatalogs(false);
107: app = new MockApplication();
108: app.getMockSessionManager().setSession(this );
109: sqlAlias = new SQLAlias(new UidIdentifier());
110: schemaInfo = new SchemaInfo(app);
111: schemaInfo.initialLoad(this );
112: prefs = app.getSquirrelPreferences();
113: try {
114: UIFactory.initialize(prefs, app);
115: } catch (Throwable e) {
116:
117: }
118: // If we are connecting to a database, then this is fine. However, when
119: // using MockObjects, this is problematic since there are many
120: // unimplemented methods in the MockObjects implementation that are
121: // required by this.
122: if (!initConnection) {
123: sessionPanel = new SessionPanel(this );
124: }
125: }
126:
127: private MockConnection2 getMockConnection() {
128: MockConnection2 result = new MockConnection2();
129: sqlDriver = new MockSQLDriver("JUnitTestClassName",
130: "JUnitJDBCURL");
131: mdata = new MockDatabaseMetaData();
132: mdata.setupDriverName("junit");
133: result.setupMetaData(mdata);
134: return result;
135: }
136:
137: /**
138: * @see net.sourceforge.squirrel_sql.client.session.ISession#getExceptionFormatter()
139: */
140: public ExceptionFormatter getExceptionFormatter() {
141: // TODO Auto-generated method stub
142: System.err
143: .println("MockSession.getExceptionFormatter: stub not yet implemented");
144: return null;
145: }
146:
147: /**
148: * @see net.sourceforge.squirrel_sql.client.session.ISession#setExceptionFormatter(net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter)
149: */
150: public void setExceptionFormatter(ExceptionFormatter formatter) {
151: // TODO Auto-generated method stub
152: System.err
153: .println("MockSession.setExceptionFormatter: stub not yet implemented");
154: }
155:
156: public boolean isClosed() {
157: return closed;
158: }
159:
160: public IApplication getApplication() {
161: return app;
162: }
163:
164: public ISQLConnection getSQLConnection() {
165: return con;
166: }
167:
168: public ISQLDriver getDriver() {
169: return sqlDriver;
170: }
171:
172: public ISQLAliasExt getAlias() {
173: return sqlAlias;
174: }
175:
176: public SessionProperties getProperties() {
177: return props;
178: }
179:
180: public void commit() {
181: try {
182: con.commit();
183: } catch (Exception e) {
184: e.printStackTrace();
185: }
186: }
187:
188: public void rollback() {
189: try {
190: con.rollback();
191: } catch (Exception e) {
192: e.printStackTrace();
193: }
194: }
195:
196: public void close() throws SQLException {
197: if (!closed) {
198: con.close();
199: }
200: }
201:
202: public void closeSQLConnection() throws SQLException {
203: con.close();
204: }
205:
206: public void setSessionInternalFrame(SessionInternalFrame sif) {
207: // TODO Auto-generated method stub
208: System.err
209: .println("MockSession.setSessionInternalFrame: stub not yet implemented");
210: }
211:
212: public void reconnect() {
213: // TODO Auto-generated method stub
214: System.err
215: .println("MockSession.reconnect: stub not yet implemented");
216: }
217:
218: public Object getPluginObject(IPlugin plugin, String key) {
219: // TODO Auto-generated method stub
220: System.err
221: .println("MockSession.getPluginObject: stub not yet implemented");
222: return null;
223: }
224:
225: public Object putPluginObject(IPlugin plugin, String key, Object obj) {
226: // TODO Auto-generated method stub
227: System.err
228: .println("MockSession.putPluginObject: stub not yet implemented");
229: return null;
230: }
231:
232: public void removePluginObject(IPlugin plugin, String key) {
233: // TODO Auto-generated method stub
234: System.err
235: .println("MockSession.removePluginObject: stub not yet implemented");
236: }
237:
238: public void setMessageHandler(IMessageHandler handler) {
239: messageHandler = handler;
240: }
241:
242: public IMessageHandler getMessageHandler() {
243: return messageHandler;
244: }
245:
246: public SessionPanel getSessionSheet() {
247: return sessionPanel;
248: }
249:
250: public SessionInternalFrame getSessionInternalFrame() {
251: // TODO Auto-generated method stub
252: System.err
253: .println("MockSession.getSessionInternalFrame: stub not yet implemented");
254: return null;
255: }
256:
257: /* (non-Javadoc)
258: * @see net.sourceforge.squirrel_sql.client.session.ISession#getSchemaInfo()
259: */
260: public SchemaInfo getSchemaInfo() {
261: return schemaInfo;
262: }
263:
264: public void selectMainTab(int tabIndex)
265: throws IllegalArgumentException {
266: // TODO Auto-generated method stub
267: System.err
268: .println("MockSession.selectMainTab: stub not yet implemented");
269: }
270:
271: public int addMainTab(IMainPanelTab tab) {
272: // TODO Auto-generated method stub
273: System.err
274: .println("MockSession.addMainTab: stub not yet implemented");
275: return 0;
276: }
277:
278: public void addToStatusBar(JComponent comp) {
279: // TODO Auto-generated method stub
280: System.err
281: .println("MockSession.addToStatusBar: stub not yet implemented");
282: }
283:
284: public void removeFromStatusBar(JComponent comp) {
285: // TODO Auto-generated method stub
286: System.err
287: .println("MockSession.removeFromStatusBar: stub not yet implemented");
288: }
289:
290: public String getTitle() {
291: // TODO Auto-generated method stub
292: System.err
293: .println("MockSession.getTitle: stub not yet implemented");
294: return null;
295: }
296:
297: public String getDatabaseProductName() {
298: String result = null;
299: try {
300: result = con.getSQLMetaData().getDatabaseProductName();
301: } catch (SQLException e) {
302: e.printStackTrace();
303: }
304: return result;
305: }
306:
307: public void addToToolbar(Action action) {
308: // TODO Auto-generated method stub
309: System.err
310: .println("MockSession.addToToolbar: stub not yet implemented");
311: }
312:
313: public void addSeparatorToToolbar() {
314: // TODO Auto-generated method stub
315: System.err
316: .println("MockSession.addSeparatorToToolbar: stub not yet implemented");
317: }
318:
319: public IParserEventsProcessor getParserEventsProcessor(
320: IIdentifier sqlEntryPanelIdentifier) {
321: // TODO Auto-generated method stub
322: System.err
323: .println("MockSession.getParserEventsProcessor: stub not yet implemented");
324: return null;
325: }
326:
327: public void setActiveSessionWindow(
328: BaseSessionInternalFrame activeActiveSessionWindow) {
329: // TODO Auto-generated method stub
330: System.err
331: .println("MockSession.setActiveSessionWindow: stub not yet implemented");
332: }
333:
334: public BaseSessionInternalFrame getActiveSessionWindow() {
335: // TODO Auto-generated method stub
336: System.err
337: .println("MockSession.getActiveSessionWindow: stub not yet implemented");
338: return null;
339: }
340:
341: public ISQLPanelAPI getSQLPanelAPIOfActiveSessionWindow() {
342: // TODO Auto-generated method stub
343: System.err
344: .println("MockSession.getSQLPanelAPIOfActiveSessionWindow: stub not yet implemented");
345: return null;
346: }
347:
348: public IObjectTreeAPI getObjectTreeAPIOfActiveSessionWindow() {
349: // TODO Auto-generated method stub
350: System.err
351: .println("MockSession.getObjectTreeAPIOfActiveSessionWindow: stub not yet implemented");
352: return null;
353: }
354:
355: public boolean isfinishedLoading() {
356: // TODO Auto-generated method stub
357: System.err
358: .println("MockSession.isfinishedLoading: stub not yet implemented");
359: return true;
360: }
361:
362: public void setPluginsfinishedLoading(boolean _finishedLoading) {
363: // TODO Auto-generated method stub
364: System.err
365: .println("MockSession.setPluginsfinishedLoading: stub not yet implemented");
366: }
367:
368: public boolean confirmClose() {
369: // TODO Auto-generated method stub
370: System.err
371: .println("MockSession.confirmClose: stub not yet implemented");
372: return false;
373: }
374:
375: public IIdentifier getIdentifier() {
376: return id;
377: }
378:
379: public MockDatabaseMetaData getMockDatabaseMetaData() {
380: return mdata;
381: }
382:
383: /**
384: * @param defaultCatalog the defaultCatalog to set
385: */
386: public void setDefaultCatalog(String defaultCatalog) {
387: this .defaultCatalog = defaultCatalog;
388: }
389:
390: /**
391: * @return the defaultCatalog
392: */
393: public String getDefaultCatalog() {
394: return defaultCatalog;
395: }
396:
397: /**
398: * @param defaultSchema the defaultSchema to set
399: */
400: public void setDefaultSchema(String defaultSchema) {
401: this .defaultSchema = defaultSchema;
402: }
403:
404: /**
405: * @return the defaultSchema
406: */
407: public String getDefaultSchema() {
408: return defaultSchema;
409: }
410:
411: public IQueryTokenizer getQueryTokenizer() {
412: return new QueryTokenizer(";", "--", true);
413: }
414:
415: public void setQueryTokenizer(IQueryTokenizer tokenizer) {
416: // TODO Auto-generated method stub
417: }
418:
419: /* (non-Javadoc)
420: * @see net.sourceforge.squirrel_sql.client.session.ISession#getMetaData()
421: */
422: public ISQLDatabaseMetaData getMetaData() {
423: return con.getSQLMetaData();
424: }
425:
426: /**
427: * @see net.sourceforge.squirrel_sql.client.session.ISession#showErrorMessage(java.lang.String)
428: */
429: public void showErrorMessage(String msg) {
430: // TODO Auto-generated method stub
431: System.err
432: .println("MockSession.showErrorMessage: stub not yet implemented");
433: }
434:
435: /**
436: * @see net.sourceforge.squirrel_sql.client.session.ISession#showErrorMessage(java.lang.Throwable)
437: */
438: public void showErrorMessage(Throwable th) {
439: // TODO Auto-generated method stub
440: System.err
441: .println("MockSession.showErrorMessage: stub not yet implemented");
442: }
443:
444: /**
445: * @see net.sourceforge.squirrel_sql.client.session.ISession#showMessage(java.lang.String)
446: */
447: public void showMessage(String msg) {
448: // TODO Auto-generated method stub
449: System.err
450: .println("MockSession.showMessage: stub not yet implemented");
451: }
452:
453: /**
454: * @see net.sourceforge.squirrel_sql.client.session.ISession#showMessage(java.lang.Throwable)
455: */
456: public void showMessage(Throwable th) {
457: // TODO Auto-generated method stub
458: System.err
459: .println("MockSession.showMessage: stub not yet implemented");
460: }
461:
462: /**
463: * @see net.sourceforge.squirrel_sql.client.session.ISession#showWarningMessage(java.lang.String)
464: */
465: public void showWarningMessage(String msg) {
466: // TODO Auto-generated method stub
467: System.err
468: .println("MockSession.showWarningMessage: stub not yet implemented");
469: }
470:
471: public String formatException(Throwable th) {
472: // TODO Auto-generated method stub
473: System.err
474: .println("MockSession.format: stub not yet implemented");
475: return null;
476: }
477:
478: }
|