001: /*
002: * Copyright (C) 2007 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.fw.dialects;
020:
021: import static org.easymock.EasyMock.expect;
022: import static org.easymock.classextension.EasyMock.createMock;
023: import static org.easymock.classextension.EasyMock.replay;
024:
025: import static org.junit.Assert.assertEquals;
026: import static org.junit.Assert.assertTrue;
027:
028: import java.io.File;
029: import java.sql.DatabaseMetaData;
030: import java.sql.SQLException;
031: import java.sql.Types;
032: import java.util.ArrayList;
033: import java.util.Arrays;
034: import java.util.List;
035:
036: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
037: import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
038: import net.sourceforge.squirrel_sql.client.session.ISession;
039: import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
040: import net.sourceforge.squirrel_sql.fw.sql.ForeignKeyInfo;
041: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
042: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
043: import net.sourceforge.squirrel_sql.fw.sql.IndexInfo;
044: import net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo;
045: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
046: import net.sourceforge.squirrel_sql.fw.sql.TableInfo;
047: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferencesManager;
048: import net.sourceforge.squirrel_sql.test.TestUtil;
049:
050: import org.junit.After;
051: import org.junit.Before;
052: import org.junit.Test;
053:
054: public class DialectUtilsTest extends BaseSQuirreLJUnit4TestCase {
055:
056: private final static String catalog = "testCatalog";
057:
058: private final static String schema = "testSchema";
059:
060: private final static String table = "testTable";
061:
062: private final static String pkCol = "id";
063:
064: private final static String pkName = "PKtestTable";
065:
066: private final static List<IndexInfo> noIndexList = new ArrayList<IndexInfo>();
067:
068: private final static ForeignKeyInfo[] noFKArray = new ForeignKeyInfo[0];
069:
070: private final static PrimaryKeyInfo[] noPrimaryKey = new PrimaryKeyInfo[0];
071:
072: CreateScriptPreferences prefs = null;
073:
074: ISQLDatabaseMetaData mockMetaData;
075:
076: ISession mockSession;
077:
078: ITableInfo childTableInfo;
079:
080: ITableInfo parentTableInfo;
081:
082: List<ITableInfo> oneTableList = new ArrayList<ITableInfo>();
083:
084: List<ITableInfo> twoTableList = new ArrayList<ITableInfo>();
085:
086: IPlugin mockPlugin;
087:
088: PrimaryKeyInfo mockPrimaryKeyInfo;
089:
090: PrimaryKeyInfo[] pkInfos;
091:
092: ForeignKeyInfo[] fkinfos;
093:
094: IndexInfo mockIndexInfo;
095:
096: List<IndexInfo> mockIndexInfos;
097:
098: @Before
099: public void setUp() throws Exception {
100:
101: prefs = new CreateScriptPreferences();
102: prefs.setIncludeExternalReferences(true);
103:
104: mockMetaData = TestUtil.getEasyMockSQLMetaData("oracle",
105: "jdbc:oracle:thin", false, false);
106:
107: mockPrimaryKeyInfo = TestUtil.getEasyMockPrimaryKeyInfo(
108: catalog, schema, table, pkCol, (short) 1, pkName, true);
109:
110: pkInfos = new PrimaryKeyInfo[] { mockPrimaryKeyInfo };
111:
112: mockSession = TestUtil.getEasyMockSession(mockMetaData, false);
113: mockPlugin = createMock(IPlugin.class);
114:
115: String name = new ApplicationFiles()
116: .getPluginsUserSettingsDirectory()
117: + File.separator + "sqlscript" + File.separator;
118:
119: File folder = new File(name);
120:
121: List<String> columnNames = Arrays.asList(new String[] { pkCol,
122: "fkcol", "data" });
123: List<Integer> dataTypes = Arrays.asList(new Integer[] {
124: Types.INTEGER, Types.VARCHAR, Types.VARCHAR });
125:
126: List<String> parentColumnNames = Arrays.asList(new String[] {
127: pkCol, "startTime" });
128: List<Integer> parentDataTypes = Arrays.asList(new Integer[] {
129: Types.INTEGER, Types.DATE });
130:
131: TableColumnInfo[] childColInfos = TestUtil
132: .getEasyMockTableColumns(catalog, schema, table,
133: columnNames, dataTypes);
134:
135: TableColumnInfo[] parentColInfos = TestUtil
136: .getEasyMockTableColumns(catalog, schema, table,
137: parentColumnNames, parentDataTypes);
138:
139: ISQLDatabaseMetaData tableMockMetaData = TestUtil
140: .getEasyMockSQLMetaData("oracle", "jdbc:oracle:thin",
141: false, true);
142:
143: childTableInfo = new TableInfo("testCatalog", "testSchema",
144: "childTable", "TABLE", "a comment", tableMockMetaData);
145:
146: parentTableInfo = new TableInfo("testCatalog", "testSchema",
147: "parentTable", "TABLE", "a comment", tableMockMetaData);
148:
149: oneTableList.add(childTableInfo);
150:
151: twoTableList.add(childTableInfo);
152: twoTableList.add(parentTableInfo);
153:
154: mockIndexInfos = TestUtil.getEasyMockIndexInfos("testTable",
155: "data1");
156:
157: fkinfos = TestUtil.getEasyMockForeignKeyInfos("ChildTable_FK",
158: "childTable", "fkcol", "parentTable", "id");
159:
160: expect(mockMetaData.getPrimaryKey(childTableInfo)).andReturn(
161: pkInfos).anyTimes();
162: expect(mockMetaData.getPrimaryKey(parentTableInfo)).andReturn(
163: noPrimaryKey).anyTimes();
164:
165: expect(mockMetaData.getColumnInfo(childTableInfo)).andReturn(
166: childColInfos).anyTimes();
167: expect(mockMetaData.getColumnInfo(parentTableInfo)).andReturn(
168: parentColInfos).anyTimes();
169:
170: expect(mockMetaData.getImportedKeysInfo(childTableInfo))
171: .andReturn(fkinfos).anyTimes();
172: expect(mockMetaData.getImportedKeysInfo(parentTableInfo))
173: .andReturn(noFKArray).anyTimes();
174:
175: expect(mockMetaData.getIndexInfo(childTableInfo)).andReturn(
176: mockIndexInfos).anyTimes();
177: expect(mockMetaData.getIndexInfo(parentTableInfo)).andReturn(
178: noIndexList).anyTimes();
179:
180: expect(mockPlugin.getPluginUserSettingsFolder()).andReturn(
181: folder).anyTimes();
182: expect(mockPlugin.getDescriptiveName()).andReturn(
183: "EasyMock SQLScript Plugin").anyTimes();
184: expect(mockPlugin.getVersion()).andReturn("1.0").anyTimes();
185:
186: replayMocks();
187:
188: SQLScriptPreferencesManager.initialize(mockPlugin);
189:
190: }
191:
192: @After
193: public void tearDown() throws Exception {
194: mockMetaData = null;
195: mockSession = null;
196: childTableInfo = null;
197: mockPlugin = null;
198: }
199:
200: private void replayMocks() {
201: replay(mockPlugin);
202: replay(mockSession);
203: replay(mockMetaData);
204: }
205:
206: // Tests
207:
208: @Test
209: public void testGetTableSource() throws SQLException {
210: Object[] dbNames = DialectFactory.getDbNames();
211: for (Object dbName : dbNames) {
212: HibernateDialect dialect = DialectFactory.getDialect(dbName
213: .toString());
214: checkGetTableSource(dialect, oneTableList, mockMetaData,
215: prefs, false, 3);
216: }
217: }
218:
219: @Test
220: public void testConstraintsAfterTable() throws SQLException {
221: prefs.setConstraintsAtEnd(false);
222: checkGetTableSource(new HSQLDialect(), twoTableList,
223: mockMetaData, prefs, false, 4);
224: }
225:
226: @Test
227: public void testGetTableSourceDeleteAction() throws SQLException {
228: prefs.setDeleteRefAction(true);
229: prefs.setDeleteAction(DatabaseMetaData.importedKeyCascade);
230: List<String> sqls = checkGetTableSource(new HSQLDialect(),
231: twoTableList, mockMetaData, prefs, false, 4);
232: checkAction(sqls, "ALTER TABLE", " ON DELETE CASCADE");
233:
234: prefs.setDeleteAction(DatabaseMetaData.importedKeyRestrict);
235: sqls = checkGetTableSource(new HSQLDialect(), twoTableList,
236: mockMetaData, prefs, false, 4);
237: checkAction(sqls, "ALTER TABLE", " ON DELETE NO ACTION");
238:
239: prefs.setDeleteAction(DatabaseMetaData.importedKeyNoAction);
240: sqls = checkGetTableSource(new HSQLDialect(), twoTableList,
241: mockMetaData, prefs, false, 4);
242: checkAction(sqls, "ALTER TABLE", " ON DELETE NO ACTION");
243:
244: prefs.setDeleteAction(DatabaseMetaData.importedKeySetNull);
245: sqls = checkGetTableSource(new HSQLDialect(), twoTableList,
246: mockMetaData, prefs, false, 4);
247: checkAction(sqls, "ALTER TABLE", " ON DELETE SET NULL");
248:
249: prefs.setDeleteAction(DatabaseMetaData.importedKeySetDefault);
250: sqls = checkGetTableSource(new HSQLDialect(), twoTableList,
251: mockMetaData, prefs, false, 4);
252: checkAction(sqls, "ALTER TABLE", " ON DELETE SET DEFAULT");
253:
254: }
255:
256: @Test
257: public void testIsJdbcOdbc() throws SQLException {
258: checkGetTableSource(new HSQLDialect(), twoTableList,
259: mockMetaData, prefs, true, 2);
260: }
261:
262: private List<String> checkGetTableSource(HibernateDialect d,
263: List<ITableInfo> tableList, ISQLDatabaseMetaData md,
264: CreateScriptPreferences scriptPrefs, boolean isJdbcOdbc,
265: int sqlCount) throws SQLException {
266:
267: List<String> createSQLs = DialectUtils.getCreateTableSQL(
268: tableList, md, d, scriptPrefs, isJdbcOdbc);
269: assertEquals("SQL Statement Count", sqlCount, createSQLs.size());
270: for (String sql : createSQLs) {
271: //System.out.println("SQL: \n"+sql);
272: assertEquals("sql.length() <= 0", true, sql.length() > 0);
273: }
274: return createSQLs;
275: }
276:
277: private void checkAction(List<String> sqls, String prefix,
278: String actionClause) {
279: for (String sql : sqls) {
280: if (sql.startsWith(prefix)) {
281: int idx = sql.indexOf(actionClause);
282: assertTrue("idx == -1: actionClause(" + actionClause
283: + ") not found in sql: " + sql, idx != -1);
284: }
285: }
286: }
287: }
|