001: package net.sourceforge.squirrel_sql.test;
002:
003: import static java.sql.Types.BIGINT;
004: import static java.sql.Types.BINARY;
005: import static java.sql.Types.BLOB;
006: import static java.sql.Types.CLOB;
007: import static java.sql.Types.DATE;
008: import static java.sql.Types.INTEGER;
009: import static java.sql.Types.LONGVARCHAR;
010: import static java.sql.Types.VARCHAR;
011: import static java.util.Arrays.asList;
012: import static org.easymock.EasyMock.expect;
013: import static org.easymock.EasyMock.expectLastCall;
014: import static org.easymock.EasyMock.isA;
015: import static org.easymock.EasyMock.startsWith;
016: import static org.easymock.classextension.EasyMock.createMock;
017: import static org.easymock.classextension.EasyMock.createNiceMock;
018: import static org.easymock.classextension.EasyMock.replay;
019:
020: import java.beans.PropertyChangeListener;
021: import java.io.File;
022: import java.sql.Connection;
023: import java.sql.DatabaseMetaData;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.sql.Statement;
027: import java.util.ArrayList;
028: import java.util.Arrays;
029: import java.util.List;
030:
031: import javax.swing.Action;
032:
033: import net.sourceforge.squirrel_sql.client.IApplication;
034: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
035: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
036: import net.sourceforge.squirrel_sql.client.gui.db.ISQLAliasExt;
037: import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
038: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
039: import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
040: import net.sourceforge.squirrel_sql.client.session.ISession;
041: import net.sourceforge.squirrel_sql.client.session.action.DeleteSelectedTablesAction;
042: import net.sourceforge.squirrel_sql.client.session.action.EditWhereColsAction;
043: import net.sourceforge.squirrel_sql.client.session.action.FilterObjectsAction;
044: import net.sourceforge.squirrel_sql.client.session.action.RefreshObjectTreeAction;
045: import net.sourceforge.squirrel_sql.client.session.action.RefreshObjectTreeItemAction;
046: import net.sourceforge.squirrel_sql.client.session.action.RefreshSchemaInfoAction;
047: import net.sourceforge.squirrel_sql.client.session.action.SQLFilterAction;
048: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
049: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
050: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
051: import net.sourceforge.squirrel_sql.fw.sql.ForeignKeyInfo;
052: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
053: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
054: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
055: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
056: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
057: import net.sourceforge.squirrel_sql.fw.sql.IndexInfo;
058: import net.sourceforge.squirrel_sql.fw.sql.JDBCTypeMapper;
059: import net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo;
060: import net.sourceforge.squirrel_sql.fw.sql.SQLConnection;
061: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverPropertyCollection;
062: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
063: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
064: import net.sourceforge.squirrel_sql.fw.util.TaskThreadPool;
065: import net.sourceforge.squirrel_sql.plugins.dbcopy.prefs.DBCopyPreferenceBean;
066:
067: import org.easymock.classextension.EasyMock;
068:
069: /**
070: * A utility class for building test objects.
071: *
072: * @author manningr
073: */
074: public class TestUtil {
075:
076: public static ISession getEasyMockSession(String dbName,
077: boolean replay) throws SQLException {
078: ISQLDatabaseMetaData md = getEasyMockSQLMetaData(dbName,
079: "jdbc:oracle");
080: ISession session = getEasyMockSession(md, replay);
081: return session;
082: }
083:
084: /**
085: * Calls replay by default.
086: *
087: * @param dbName
088: * @return
089: * @throws SQLException
090: */
091: public static ISession getEasyMockSession(String dbName)
092: throws SQLException {
093: return getEasyMockSession(dbName, true);
094: }
095:
096: public static ISession getEasyMockSession(ISQLDatabaseMetaData md,
097: boolean replay) {
098: ISession session = null;
099: try {
100: ISQLConnection con = getEasyMockSQLConnection();
101: session = getEasyMockSession(md, con, false);
102: if (replay) {
103: replay(session);
104: }
105: } catch (Exception e) {
106: e.printStackTrace();
107: }
108: return session;
109: }
110:
111: public static ISession getEasyMockSession(ISQLDatabaseMetaData md,
112: ISQLConnection con, boolean replay) {
113: ISession session = createMock(ISession.class);
114: IQueryTokenizer tokenizer = getEasyMockQueryTokenizer();
115: // IMessageHandler messageHandler = getEasyMockMessageHandler();
116:
117: expect(session.getMetaData()).andReturn(md).anyTimes();
118: expect(session.getApplication()).andReturn(
119: getEasyMockApplication()).anyTimes();
120: expect(session.getQueryTokenizer()).andReturn(tokenizer)
121: .anyTimes();
122: // expect(session.getMessageHandler()).andReturn(messageHandler).anyTimes();
123: expect(session.getAlias()).andReturn(getEasyMockSqlAliasExt());
124: expect(session.getIdentifier()).andReturn(
125: getEasyMockIdentifier()).anyTimes();
126: expect(session.getSQLConnection()).andReturn(con).anyTimes();
127: if (replay) {
128: replay(session);
129: }
130: return session;
131: }
132:
133: public static IMessageHandler getEasyMockMessageHandler() {
134: IMessageHandler result = createMock(IMessageHandler.class);
135: result.showErrorMessage(isA(Throwable.class), null);
136: result.showErrorMessage(isA(String.class));
137: result.showMessage(isA(String.class));
138: result.showMessage(isA(Throwable.class), null);
139: result.showWarningMessage(isA(String.class));
140: replay(result);
141: return result;
142: }
143:
144: public static IQueryTokenizer getEasyMockQueryTokenizer() {
145: return getEasyMockQueryTokenizer(";", "--", true, 5);
146: }
147:
148: public static IQueryTokenizer getEasyMockQueryTokenizer(String sep,
149: String solComment, boolean removeMultiLineComment,
150: int queryCount) {
151: IQueryTokenizer tokenizer = createMock(IQueryTokenizer.class);
152: expect(tokenizer.getSQLStatementSeparator()).andReturn(sep)
153: .anyTimes();
154: expect(tokenizer.getLineCommentBegin()).andReturn(solComment)
155: .anyTimes();
156: expect(tokenizer.isRemoveMultiLineComment()).andReturn(
157: removeMultiLineComment).anyTimes();
158: expect(tokenizer.getQueryCount()).andReturn(queryCount)
159: .anyTimes();
160: replay(tokenizer);
161: return tokenizer;
162: }
163:
164: /**
165: * Calls replay by default.
166: *
167: * @param md
168: * @return
169: */
170: public static ISession getEasyMockSession(ISQLDatabaseMetaData md) {
171: return getEasyMockSession(md, true);
172: }
173:
174: public static ISession getEasyMockSession(ISQLDatabaseMetaData md,
175: ResultSet rs) throws SQLException {
176: ISQLConnection con = getEasyMockSQLConnection(rs);
177: ISession session = getEasyMockSession(md, con, false);
178: replay(session);
179: return session;
180: }
181:
182: public static SQLConnection getEasyMockSQLConnection()
183: throws SQLException {
184: SQLConnection result = createMock(SQLConnection.class);
185: result.addPropertyChangeListener(EasyMock
186: .isA(PropertyChangeListener.class));
187: expect(result.getCatalog()).andReturn("TestCatalog").anyTimes();
188: return result;
189: }
190:
191: public static ISQLConnection getEasyMockSQLConnection(ResultSet rs)
192: throws SQLException {
193: if (rs == null) {
194: throw new IllegalArgumentException("rs cannot be null");
195: }
196: Statement stmt = createNiceMock(Statement.class);
197: expect(stmt.executeQuery(startsWith("select"))).andReturn(rs)
198: .anyTimes();
199: replay(stmt);
200:
201: Connection con = createNiceMock(Connection.class);
202: expect(con.createStatement()).andReturn(stmt);
203: expect(
204: con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
205: ResultSet.CONCUR_READ_ONLY)).andReturn(stmt);
206: replay(con);
207:
208: ISQLConnection sqlCon = createNiceMock(ISQLConnection.class);
209: expect(sqlCon.getConnection()).andReturn(con);
210: replay(sqlCon);
211:
212: return sqlCon;
213: }
214:
215: public static ISQLDatabaseMetaData getEasyMockH2SQLMetaData()
216: throws SQLException {
217: ISQLDatabaseMetaData md = createMock(ISQLDatabaseMetaData.class);
218: expect(md.getDatabaseProductName()).andReturn("H2").anyTimes();
219: expect(md.getDatabaseProductVersion()).andReturn(
220: "1.0 (2007-04-29)").anyTimes();
221: expect(md.supportsSchemasInDataManipulation()).andReturn(true)
222: .anyTimes();
223: expect(md.supportsCatalogsInDataManipulation())
224: .andReturn(false).anyTimes();
225: expect(md.getCatalogSeparator()).andReturn(".").anyTimes();
226: expect(md.getIdentifierQuoteString()).andReturn("\"")
227: .anyTimes();
228: expect(md.getURL()).andReturn(
229: "jdbc:h2:tcp://localhost:9094/testDatabase").anyTimes();
230: replay(md);
231: return md;
232: }
233:
234: public static ISQLDatabaseMetaData getEasyMockSQLMetaData(
235: String dbName, String dbURL, DatabaseMetaData md)
236: throws SQLException {
237: ISQLDatabaseMetaData result = getEasyMockSQLMetaData(dbName,
238: dbURL, false, false);
239: expect(result.getJDBCMetaData()).andReturn(md);
240: replay(result);
241: return result;
242: }
243:
244: public static ISQLDatabaseMetaData getEasyMockSQLMetaData(
245: String dbName, String dbURL, boolean nice, boolean replay)
246: throws SQLException {
247: ISQLDatabaseMetaData md = null;
248: if (nice) {
249: md = createNiceMock(ISQLDatabaseMetaData.class);
250: } else {
251: md = createMock(ISQLDatabaseMetaData.class);
252: }
253:
254: expect(md.getDatabaseProductName()).andReturn(dbName)
255: .anyTimes();
256: expect(md.getDatabaseProductVersion()).andReturn("1.0")
257: .anyTimes();
258: expect(md.supportsSchemasInDataManipulation()).andReturn(true)
259: .anyTimes();
260: expect(md.supportsCatalogsInDataManipulation()).andReturn(true)
261: .anyTimes();
262: expect(md.getCatalogSeparator()).andReturn("").anyTimes();
263: expect(md.getIdentifierQuoteString()).andReturn("\"")
264: .anyTimes();
265: expect(md.getURL()).andReturn(dbURL).anyTimes();
266: DatabaseMetaData dbmd = createMock(DatabaseMetaData.class);
267: expect(md.getJDBCMetaData()).andReturn(dbmd).anyTimes();
268: if (replay) {
269: replay(md);
270: }
271: return md;
272: }
273:
274: /**
275: * Calls replay by default. Nice by default.
276: *
277: * @param dbName
278: * @param dbURL
279: * @return
280: * @throws SQLException
281: */
282: public static ISQLDatabaseMetaData getEasyMockSQLMetaData(
283: String dbName, String dbURL) throws SQLException {
284: return getEasyMockSQLMetaData(dbName, dbURL, true, true);
285: }
286:
287: /**
288: * Calls replay by default.
289: *
290: * @param dbName
291: * @param dbURL
292: * @param nice
293: * @return
294: * @throws SQLException
295: */
296: public static ISQLDatabaseMetaData getEasyMockSQLMetaData(
297: String dbName, String dbURL, boolean nice)
298: throws SQLException {
299: return getEasyMockSQLMetaData(dbName, dbURL, nice, true);
300: }
301:
302: public static IApplication getEasyMockApplication(boolean nice) {
303: IApplication result = null;
304: if (nice) {
305: result = createNiceMock(IApplication.class);
306: } else {
307: result = createMock(IApplication.class);
308: }
309: SquirrelResources resoures = getEasyMockSquirrelResources();
310: SessionProperties props = getEasyMockSessionProperties(";",
311: "--", true);
312: SquirrelPreferences prefs = getEasyMockSquirrelPreferences(props);
313: expect(result.getMainFrame()).andReturn(null).anyTimes();
314: expect(result.getResources()).andReturn(resoures).anyTimes();
315: expect(result.getSquirrelPreferences()).andReturn(prefs)
316: .anyTimes();
317: TaskThreadPool mockThreadPool = getEasyMockTaskThreadPool();
318: expect(result.getThreadPool()).andReturn(mockThreadPool)
319: .anyTimes();
320: ActionCollection mockActColl = getEasyMockActionCollection();
321: expect(result.getActionCollection()).andReturn(mockActColl)
322: .anyTimes();
323: replay(result);
324: return result;
325: }
326:
327: public static ActionCollection getEasyMockActionCollection() {
328: ActionCollection result = createMock(ActionCollection.class);
329: result.add(isA(Action.class));
330: expectLastCall().anyTimes();
331: expectActionCollectionGet("refreshSchema",
332: RefreshSchemaInfoAction.class, result);
333: expectActionCollectionGet("refreshObjectTree",
334: RefreshObjectTreeAction.class, result);
335: expectActionCollectionGet("refreshObjectItemTree",
336: RefreshObjectTreeItemAction.class, result);
337: expectActionCollectionGet("editWhereColsAction",
338: EditWhereColsAction.class, result);
339: expectActionCollectionGet("SQLFilterAction",
340: SQLFilterAction.class, result);
341: expectActionCollectionGet("DeleteSelectedTablesAction",
342: DeleteSelectedTablesAction.class, result);
343: expectActionCollectionGet("FilterObjectsAction",
344: FilterObjectsAction.class, result);
345: replay(result);
346: return result;
347: }
348:
349: public static void expectActionCollectionGet(String actionName,
350: Class<? extends Action> actionClass, ActionCollection col) {
351: SquirrelAction action = getEasyMockSquirrelAction(actionName);
352: expect(col.get(actionClass)).andReturn(action).anyTimes();
353: }
354:
355: public static SquirrelAction getEasyMockSquirrelAction(String name) {
356: SquirrelAction result = createMock(SquirrelAction.class);
357: expect(result.getValue(Action.NAME)).andReturn(name).anyTimes();
358: expect(result.getValue(Action.SMALL_ICON)).andReturn(null)
359: .anyTimes();
360: expect(result.getValue(Action.MNEMONIC_KEY)).andReturn(null)
361: .anyTimes();
362: expect(result.getValue(Action.SHORT_DESCRIPTION)).andReturn(
363: null).anyTimes();
364: expect(result.getValue(Action.ACTION_COMMAND_KEY)).andReturn(
365: null).anyTimes();
366: expect(result.getValue(Action.ACCELERATOR_KEY)).andReturn(null)
367: .anyTimes();
368: expect(result.isEnabled()).andReturn(true).anyTimes();
369: expect(result.getKeyStroke()).andReturn(null).anyTimes();
370: result
371: .addPropertyChangeListener(isA(PropertyChangeListener.class));
372: expectLastCall().anyTimes();
373: replay(result);
374: return result;
375: }
376:
377: public static TaskThreadPool getEasyMockTaskThreadPool() {
378: TaskThreadPool result = createMock(TaskThreadPool.class);
379: result.addTask(isA(Runnable.class));
380: expectLastCall().anyTimes();
381: replay(result);
382: return result;
383: }
384:
385: public static IIdentifier getEasyMockIdentifier() {
386: IIdentifier result = createMock(IIdentifier.class);
387: replay(result);
388: return result;
389: }
390:
391: public static ISQLAliasExt getEasyMockSqlAliasExt() {
392: ISQLAliasExt result = createMock(ISQLAliasExt.class);
393: expect(result.getName()).andReturn("TestAlias").anyTimes();
394: IIdentifier id = getEasyMockIdentifier();
395: expect(result.getDriverIdentifier()).andReturn(id).anyTimes();
396: replay(result);
397: return result;
398: }
399:
400: public static IApplication getEasyMockApplication() {
401: return getEasyMockApplication(true);
402: }
403:
404: public static SquirrelResources getEasyMockSquirrelResources() {
405: SquirrelResources resources = EasyMock
406: .createMock(SquirrelResources.class);
407: resources.setupAction(isA(Action.class), EasyMock.anyBoolean());
408: EasyMock.expectLastCall().times(1, 10000);
409: replay(resources);
410: return resources;
411: }
412:
413: public static SessionProperties getEasyMockSessionProperties(
414: String sep, String solComment,
415: boolean removeMultLineComments) {
416: SessionProperties result = createMock(SessionProperties.class);
417: expect(result.getSQLStatementSeparator()).andReturn(sep)
418: .anyTimes();
419: expect(result.getStartOfLineComment()).andReturn(solComment)
420: .anyTimes();
421: expect(result.getRemoveMultiLineComment()).andReturn(
422: removeMultLineComments).anyTimes();
423: expect(result.clone()).andReturn(result).anyTimes();
424: replay(result);
425: return result;
426: }
427:
428: public static TaskThreadPool getThreadPool() {
429: TaskThreadPool result = createMock(TaskThreadPool.class);
430: result.addTask(isA(Runnable.class));
431: replay(result);
432: return result;
433: }
434:
435: public static SquirrelPreferences getEasyMockSquirrelPreferences(
436: SessionProperties props) {
437: SquirrelPreferences prefs = createMock(SquirrelPreferences.class);
438: expect(prefs.getShowColoriconsInToolbar()).andReturn(true)
439: .anyTimes();
440: expect(prefs.getSessionProperties()).andReturn(props)
441: .anyTimes();
442: expect(prefs.getWarnJreJdbcMismatch()).andReturn(false)
443: .anyTimes();
444: replay(prefs);
445: return prefs;
446: }
447:
448: public static ForeignKeyInfo[] getEasyMockForeignKeyInfos(
449: String fkName, String ctab, String ccol, String ptab,
450: String pcol) {
451: ForeignKeyInfo result = createMock(ForeignKeyInfo.class);
452: expect(result.getSimpleName()).andReturn(fkName).anyTimes();
453: expect(result.getForeignKeyColumnName()).andReturn(ccol)
454: .anyTimes();
455: expect(result.getPrimaryKeyColumnName()).andReturn(pcol)
456: .anyTimes();
457: expect(result.getForeignKeyTableName()).andReturn(ctab)
458: .anyTimes();
459: expect(result.getPrimaryKeyTableName()).andReturn(ptab)
460: .anyTimes();
461: expect(result.getDeleteRule()).andReturn(
462: DatabaseMetaData.importedKeyCascade).anyTimes();
463: expect(result.getUpdateRule()).andReturn(
464: DatabaseMetaData.importedKeyCascade).anyTimes();
465: replay(result);
466: return new ForeignKeyInfo[] { result };
467: }
468:
469: public static List<IndexInfo> getEasyMockIndexInfos(
470: String tableName, String columnName) {
471: IndexInfo result = createMock(IndexInfo.class);
472: expect(result.getColumnName()).andReturn(columnName).anyTimes();
473: expect(result.getSimpleName()).andReturn("TestIndex")
474: .anyTimes();
475: expect(result.getOrdinalPosition()).andReturn((short) 1)
476: .anyTimes();
477: expect(result.getTableName()).andReturn(tableName).anyTimes();
478: expect(result.isNonUnique()).andReturn(false).anyTimes();
479: replay(result);
480: return Arrays.asList(new IndexInfo[] { result });
481: }
482:
483: public static PrimaryKeyInfo getEasyMockPrimaryKeyInfo(
484: String catalog, String schemaName, String tableName,
485: String columnName, short keySequence, String pkName,
486: boolean replay) {
487: PrimaryKeyInfo pki = createMock(PrimaryKeyInfo.class);
488: expect(pki.getCatalogName()).andReturn(catalog).anyTimes();
489: expect(pki.getColumnName()).andReturn(columnName).anyTimes();
490: expect(pki.getDatabaseObjectType()).andReturn(
491: DatabaseObjectType.PRIMARY_KEY).anyTimes();
492: expect(pki.getKeySequence()).andReturn(keySequence).anyTimes();
493: expect(pki.getQualifiedColumnName()).andReturn(columnName)
494: .anyTimes();
495: expect(pki.getQualifiedName()).andReturn(pkName).anyTimes();
496: expect(pki.getSchemaName()).andReturn(schemaName).anyTimes();
497: expect(pki.getSimpleName()).andReturn(pkName).anyTimes();
498: expect(pki.getTableName()).andReturn(tableName).anyTimes();
499: if (replay) {
500: replay(pki);
501: }
502: return pki;
503: }
504:
505: /**
506: * Calls replay by default.
507: *
508: * @param catalog
509: * @param schemaName
510: * @param tableName
511: * @param columnName
512: * @param keySequence
513: * @param pkName
514: * @return
515: */
516: public static PrimaryKeyInfo getEasyMockPrimaryKeyInfo(
517: String catalog, String schemaName, String tableName,
518: String columnName, short keySequence, String pkName) {
519: return getEasyMockPrimaryKeyInfo(catalog, schemaName,
520: tableName, columnName, keySequence, pkName, true);
521: }
522:
523: public static TableColumnInfo getEasyMockTableColumn(
524: String catalogName, String schemaName, String tableName,
525: String columnName, int dataType) {
526: String[] columnNames = new String[] { columnName };
527: Integer[] dataTypes = new Integer[] { dataType };
528: TableColumnInfo[] result = getEasyMockTableColumns(catalogName,
529: schemaName, tableName, asList(columnNames),
530: asList(dataTypes));
531: return result[0];
532: }
533:
534: public static TableColumnInfo[] getEasyMockTableColumns(
535: String catalogName, String schemaName, String tableName,
536: List<String> columnNames, List<Integer> dataTypes) {
537: if (columnNames.size() != dataTypes.size()) {
538: throw new IllegalArgumentException(
539: "columnNames.size() != dataTypes.size()");
540: }
541: ArrayList<TableColumnInfo> result = new ArrayList<TableColumnInfo>();
542:
543: int index = 0;
544: for (String columnName : columnNames) {
545: Integer columnDataType = dataTypes.get(index++);
546:
547: TableColumnInfo info = getEasyMockTableColumnInfo(
548: catalogName, schemaName, tableName, columnName,
549: columnDataType, 10, "defval", "remark", 10, 10, 10,
550: true);
551:
552: result.add(info);
553: }
554:
555: return result.toArray(new TableColumnInfo[0]);
556: }
557:
558: public static TableColumnInfo getEasyMockTableColumnInfo(
559: String catalogName, String schemaName, String tableName,
560: String columnName, int dataType, int columnSize,
561: String defaultValue, String remarks, int decimalDigits,
562: int octetLength, int radix, boolean nullable) {
563: TableColumnInfo info = createMock(TableColumnInfo.class);
564: expect(info.getCatalogName()).andReturn(catalogName).anyTimes();
565: expect(info.getSchemaName()).andReturn(schemaName).anyTimes();
566: expect(info.getTableName()).andReturn(tableName).anyTimes();
567: expect(info.getColumnName()).andReturn(columnName).anyTimes();
568: expect(info.getDataType()).andReturn(dataType).anyTimes();
569: expect(info.getTypeName()).andReturn(
570: JDBCTypeMapper.getJdbcTypeName(dataType)).anyTimes();
571: expect(info.getColumnSize()).andReturn(columnSize).anyTimes();
572: expect(info.getDatabaseObjectType()).andReturn(
573: DatabaseObjectType.COLUMN).anyTimes();
574: expect(info.getDefaultValue()).andReturn(defaultValue)
575: .anyTimes();
576: expect(info.getRemarks()).andReturn(remarks).anyTimes();
577: expect(info.getDecimalDigits()).andReturn(decimalDigits)
578: .anyTimes();
579: expect(info.getOctetLength()).andReturn(octetLength).anyTimes();
580: expect(info.getQualifiedName()).andReturn(
581: schemaName + "." + tableName + "." + columnName)
582: .anyTimes();
583: expect(info.getRadix()).andReturn(radix).anyTimes();
584: if (nullable) {
585: expect(info.isNullable()).andReturn("YES").anyTimes();
586: expect(info.isNullAllowed()).andReturn(1).anyTimes();
587: } else {
588: expect(info.isNullable()).andReturn("NO").anyTimes();
589: expect(info.isNullAllowed()).andReturn(0).anyTimes();
590: }
591: replay(info);
592: return info;
593: }
594:
595: /**
596: * Returns a new TableColumnInfo EasyMock based on values from the one
597: * specified, only the column size is the one specified.
598: *
599: * @param info
600: * the existing TableColumnInfo to replicate
601: * @param newSize
602: * the new column size
603: * @return
604: */
605: public static TableColumnInfo setEasyMockTableColumnInfoSize(
606: final TableColumnInfo info, final int newSize) {
607: TableColumnInfo result = getEasyMockTableColumnInfo(info
608: .getCatalogName(), info.getSchemaName(), info
609: .getTableName(), info.getColumnName(), info
610: .getDataType(), newSize, info.getDefaultValue(), info
611: .getRemarks(), info.getDecimalDigits(), info
612: .getOctetLength(), info.getRadix(), info
613: .isNullAllowed() == 1 ? true : false);
614: return result;
615:
616: }
617:
618: /**
619: * Returns a new TableColumnInfo EasyMock based on values from the one
620: * specified, only the column size is the one specified.
621: *
622: * @param info
623: * the existing TableColumnInfo to replicate
624: * @param newSize
625: * the new column size
626: * @return
627: */
628: public static TableColumnInfo setEasyMockTableColumnInfoNullable(
629: final TableColumnInfo info, final boolean nullable) {
630: TableColumnInfo result = getEasyMockTableColumnInfo(info
631: .getCatalogName(), info.getSchemaName(), info
632: .getTableName(), info.getColumnName(), info
633: .getDataType(), info.getColumnSize(), info
634: .getDefaultValue(), info.getRemarks(), info
635: .getDecimalDigits(), info.getOctetLength(), info
636: .getRadix(), nullable);
637: return result;
638:
639: }
640:
641: /**
642: * Returns a new TableColumnInfo EasyMock based on values from the one
643: * specified, only the column data type is the one specified.
644: *
645: * @param info
646: * the existing TableColumnInfo to replicate
647: * @param dataTyoe
648: * the new column data type
649: * @return
650: */
651: public static TableColumnInfo setEasyMockTableColumnInfoType(
652: final TableColumnInfo info, final int dataType) {
653: TableColumnInfo result = getEasyMockTableColumnInfo(info
654: .getCatalogName(), info.getSchemaName(), info
655: .getTableName(), info.getColumnName(), dataType, info
656: .getColumnSize(), info.getDefaultValue(), info
657: .getRemarks(), info.getDecimalDigits(), info
658: .getOctetLength(), info.getRadix(), info
659: .isNullAllowed() == 1 ? true : false);
660: return result;
661:
662: }
663:
664: public static TableColumnInfo getBigintColumnInfo(
665: ISQLDatabaseMetaData md, boolean nullable) {
666: return getTableColumnInfo(md, BIGINT, 20, 10, nullable);
667: }
668:
669: public static TableColumnInfo getBinaryColumnInfo(
670: ISQLDatabaseMetaData md, boolean nullable) {
671: return getTableColumnInfo(md, BINARY, -1, 0, nullable);
672: }
673:
674: public static TableColumnInfo getBlobColumnInfo(
675: ISQLDatabaseMetaData md, boolean nullable) {
676: return getTableColumnInfo(md, BLOB, Integer.MAX_VALUE, 0,
677: nullable);
678: }
679:
680: public static TableColumnInfo getClobColumnInfo(
681: ISQLDatabaseMetaData md, boolean nullable) {
682: return getTableColumnInfo(md, CLOB, Integer.MAX_VALUE, 0,
683: nullable);
684: }
685:
686: public static TableColumnInfo getIntegerColumnInfo(
687: ISQLDatabaseMetaData md, boolean nullable) {
688: return getTableColumnInfo(md, INTEGER, 10, 0, nullable);
689: }
690:
691: public static TableColumnInfo getDateColumnInfo(
692: ISQLDatabaseMetaData md, boolean nullable) {
693: return getTableColumnInfo(md, DATE, 0, 0, nullable);
694: }
695:
696: public static TableColumnInfo getLongVarcharColumnInfo(
697: ISQLDatabaseMetaData md, boolean nullable, int length) {
698: return getTableColumnInfo(md, LONGVARCHAR, length, 0, nullable);
699: }
700:
701: public static TableColumnInfo getVarcharColumnInfo(
702: ISQLDatabaseMetaData md, boolean nullable, int length) {
703: return getTableColumnInfo(md, VARCHAR, length, 0, nullable);
704: }
705:
706: public static TableColumnInfo getTableColumnInfo(
707: ISQLDatabaseMetaData md, int type, int columnSize,
708: int decimalDigits, boolean nullable) {
709: return getTableColumnInfo(md, "TestColumn", type, columnSize,
710: decimalDigits, nullable);
711: }
712:
713: public static TableColumnInfo getTableColumnInfo(
714: ISQLDatabaseMetaData md, String columnName, int type,
715: int columnSize, int decimalDigits, boolean nullable) {
716: int isNullableInt = 0;
717: String isNullableStr = "no";
718:
719: if (nullable) {
720: isNullableInt = 1;
721: isNullableStr = "yes";
722: }
723: TableColumnInfo info = new TableColumnInfo("TestCatalog",
724: "TestSchema", "TestTable", columnName, type,
725: JDBCTypeMapper.getJdbcTypeName(type), // typeName
726: columnSize, // columnSize
727: decimalDigits, // decimalDigits
728: 0, // radix
729: isNullableInt, // isNullAllowable
730: "TestRemark", "0", // defaultValue
731: 0, // octetLength
732: 0, // ordinalPosition
733: isNullableStr, // isNullable
734: md);
735: return info;
736: }
737:
738: public static String findAncestorSquirrelSqlDistDirBase(
739: String dirToFind) {
740: File f = new File("../" + dirToFind);
741: if (f.exists())
742: return "../";
743: f = new File("../../" + dirToFind);
744: if (f.exists())
745: return "../../";
746: f = new File("../../../" + dirToFind);
747: if (f.exists())
748: return "../../../";
749: f = new File("../../../../" + dirToFind);
750: if (f.exists())
751: return "../../../../";
752: f = new File("../../../../../" + dirToFind);
753: if (f.exists())
754: return "../../../../../";
755: return null;
756: }
757:
758: public static SQLAlias getEasyMockSQLAlias(IIdentifier SqlAliasId,
759: IIdentifier SqlDriverId) {
760: SQLAlias mockSqlAlias = createMock(SQLAlias.class);
761: SQLDriverPropertyCollection mockSqlDriverPropCol = createMock(SQLDriverPropertyCollection.class);
762: expect(mockSqlAlias.getIdentifier()).andReturn(SqlAliasId)
763: .anyTimes();
764: expect(mockSqlAlias.getName()).andReturn("TestAliasName")
765: .anyTimes();
766: expect(mockSqlAlias.getDriverIdentifier()).andReturn(
767: SqlDriverId).anyTimes();
768: expect(mockSqlAlias.getUrl()).andReturn("TestUrl").anyTimes();
769: expect(mockSqlAlias.getUserName()).andReturn("TestUserName")
770: .anyTimes();
771: expect(mockSqlAlias.getPassword()).andReturn("TestPassword")
772: .anyTimes();
773: expect(mockSqlAlias.isAutoLogon()).andReturn(true).anyTimes();
774: expect(mockSqlAlias.getUseDriverProperties()).andReturn(true)
775: .anyTimes();
776: expect(mockSqlAlias.getDriverPropertiesClone()).andReturn(
777: mockSqlDriverPropCol).anyTimes();
778: replay(mockSqlAlias);
779: return mockSqlAlias;
780: }
781:
782: public static IDatabaseObjectInfo getEasyMockDatabaseObjectInfo(
783: String catalog, String schema, String simpleName,
784: String qualName, DatabaseObjectType type) {
785: IDatabaseObjectInfo result = EasyMock
786: .createMock(IDatabaseObjectInfo.class);
787: expect(result.getCatalogName()).andReturn(catalog).anyTimes();
788: expect(result.getSchemaName()).andReturn(schema).anyTimes();
789: expect(result.getSimpleName()).andReturn(simpleName).anyTimes();
790: expect(result.getQualifiedName()).andReturn(qualName)
791: .anyTimes();
792: expect(result.getDatabaseObjectType()).andReturn(type)
793: .anyTimes();
794: replay(result);
795: return result;
796: }
797:
798: public static DBCopyPreferenceBean getEasyMockDBCopyPreferenceBean() {
799: DBCopyPreferenceBean result = EasyMock
800: .createMock(DBCopyPreferenceBean.class);
801: expect(result.isCopyForeignKeys()).andReturn(true);
802: expect(result.isCopyPrimaryKeys()).andReturn(true);
803: expect(result.isPruneDuplicateIndexDefs()).andReturn(true);
804: expect(result.isPromptForDialect()).andReturn(false);
805: replay(result);
806: return result;
807: }
808:
809: public static ITableInfo getEasyMockTableInfo(String catalog,
810: String schema, String simpleName, String qualName) {
811: ITableInfo result = EasyMock.createMock(ITableInfo.class);
812: expect(result.getCatalogName()).andReturn(catalog).anyTimes();
813: expect(result.getSchemaName()).andReturn(schema).anyTimes();
814: expect(result.getSimpleName()).andReturn(simpleName).anyTimes();
815: expect(result.getQualifiedName()).andReturn(qualName)
816: .anyTimes();
817: replay(result);
818: return result;
819: }
820: }
|