001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2006 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import static org.easymock.EasyMock.createMock;
022: import static org.easymock.EasyMock.createNiceMock;
023: import static org.easymock.EasyMock.expect;
024: import static org.easymock.EasyMock.replay;
025:
026: import java.sql.Connection;
027: import java.sql.DatabaseMetaData;
028: import java.sql.ResultSet;
029: import java.sql.ResultSetMetaData;
030: import java.sql.SQLException;
031:
032: import net.sourceforge.squirrel_sql.BaseSQuirreLTestCase;
033:
034: /**
035: * Test case for SQLDatabaseMetaData class.
036: *
037: * @author manningr
038: */
039: public class SQLDatabaseMetaDataTest extends BaseSQuirreLTestCase {
040:
041: SQLDatabaseMetaData iut = null;
042:
043: /* Mock Objects */
044: Connection mockConnection = null;
045: ISQLConnection mockSqlConnection = null;
046: DatabaseMetaData mockDatabaseMetaData = null;
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050:
051: mockDatabaseMetaData = createMock(DatabaseMetaData.class);
052: expect(mockDatabaseMetaData.getDatabaseProductName())
053: .andReturn("PostgreSQL");
054: expect(mockDatabaseMetaData.getDatabaseProductVersion())
055: .andReturn("8.1.8");
056:
057: /* Build the table types returned by PostgreSQL */
058: ResultSet mockTableTypeResultSet = buildVarcharResultSet(new String[] {
059: "SYSTEM INDEX", "SYSTEM VIEW", "SYSTEM TABLE",
060: "SYSTEM TOAST INDEX", "SYSTEM TOAST TABLE",
061: "SYSTEM VIEW", "TABLE", "TEMPORARY INDEX",
062: "TEMPORARY TABLE", "VIEW" });
063: expect(mockDatabaseMetaData.getTableTypes()).andReturn(
064: mockTableTypeResultSet);
065:
066: /* The first time that catalogs are asked for, return just one */
067: ResultSet catalogResultSet1 = buildVarcharResultSet(new String[] { "aCatalog" });
068: expect(mockDatabaseMetaData.getCatalogs()).andReturn(
069: catalogResultSet1);
070:
071: /* The second time that catalogs are asked for, return two */
072: ResultSet catalogResultSet2 = buildVarcharResultSet(new String[] {
073: "aCatalog", "aCatalog2" });
074: expect(mockDatabaseMetaData.getCatalogs()).andReturn(
075: catalogResultSet2);
076:
077: /* The first time that schemas are asked for, return just one */
078: ResultSet schemaResultSet1 = buildVarcharResultSet(new String[] { "aSchema" });
079: expect(mockDatabaseMetaData.getSchemas()).andReturn(
080: schemaResultSet1);
081:
082: /* The second time that schemas are asked for, return two */
083: ResultSet schemaResultSet2 = buildVarcharResultSet(new String[] {
084: "aSchema", "aSchema2" });
085: expect(mockDatabaseMetaData.getSchemas()).andReturn(
086: schemaResultSet2);
087: replay(mockDatabaseMetaData);
088:
089: mockConnection = createMock(Connection.class);
090: expect(mockConnection.getMetaData()).andReturn(
091: mockDatabaseMetaData).anyTimes();
092: replay(mockConnection);
093:
094: mockSqlConnection = createMock(ISQLConnection.class);
095: expect(mockSqlConnection.getConnection()).andReturn(
096: mockConnection).anyTimes();
097: replay(mockSqlConnection);
098:
099: iut = new SQLDatabaseMetaData(mockSqlConnection);
100: }
101:
102: public void testGetSchemas() {
103:
104: try {
105: // Check to be sure we get only one schema
106: String[] currentSchemas = iut.getSchemas();
107: assertEquals(1, currentSchemas.length);
108:
109: // Now, check to be sure we get both schemas.
110: currentSchemas = iut.getSchemas();
111: assertEquals(2, currentSchemas.length);
112: } catch (SQLException e) {
113: fail("Unexpected exception: " + e.getMessage());
114: }
115:
116: }
117:
118: public void testGetCatalogs() {
119: try {
120: // Check to be sure we get only one schema
121: String[] currentCatalogs = iut.getCatalogs();
122: assertEquals(1, currentCatalogs.length);
123:
124: // Now, check to be sure we get both catalogs.
125: currentCatalogs = iut.getCatalogs();
126: assertEquals(2, currentCatalogs.length);
127: } catch (SQLException e) {
128: fail("Unexpected exception: " + e.getMessage());
129: }
130: }
131:
132: private ResultSet buildVarcharResultSet(String[] values)
133: throws SQLException {
134: ResultSetMetaData rsmd = createMock(ResultSetMetaData.class);
135: expect(rsmd.getColumnCount()).andReturn(1);
136: expect(rsmd.getColumnType(1)).andReturn(java.sql.Types.VARCHAR)
137: .anyTimes();
138: expect(rsmd.getColumnTypeName(1)).andReturn("varchar")
139: .anyTimes();
140: replay(rsmd);
141: ResultSet rs = createMock(ResultSet.class);
142: expect(rs.getMetaData()).andReturn(rsmd);
143: for (String value : values) {
144: expect(rs.next()).andReturn(true);
145: expect(rs.getString(1)).andReturn(value);
146: expect(rs.wasNull()).andReturn(false);
147: }
148: expect(rs.next()).andReturn(false);
149: rs.close();
150: replay(rs);
151: return rs;
152: }
153:
154: public void testPGGetTableTypes() {
155: try {
156: String[] tableTypes = iut.getTableTypes();
157: for (int i = 0; i < tableTypes.length; i++) {
158: String type = tableTypes[i];
159: assertFalse(
160: "'SYSTEM INDEX' is a type returned from "
161: + "SQLDatabaseMetaData.getTableTypes for PostgreSQL - "
162: + "it should not be.", "SYSTEM INDEX"
163: .equals(type));
164: }
165: } catch (SQLException e) {
166: fail("Unexpected exception: " + e.getMessage());
167: }
168: }
169:
170: /**
171: * Test for bug 1716859 (Can't see data in content tab or row count tab)
172: * SQLServer with a dash in the name needs to be quoted.
173: * @throws SQLException
174: */
175: public void testGetIdentifierQuoteStringMSSQL() throws SQLException {
176: Connection con = createNiceMock(Connection.class);
177: DatabaseMetaData md = createNiceMock(DatabaseMetaData.class);
178: expect(md.getIdentifierQuoteString()).andReturn("foo")
179: .anyTimes();
180: expect(md.getDatabaseProductName()).andReturn("microsoft")
181: .andReturn("sybase").andReturn("adaptive").andReturn(
182: "sql server");
183: expect(con.getMetaData()).andReturn(md).anyTimes();
184: replay(con);
185: replay(md);
186: SQLConnection sqlcon = new SQLConnection(con, null, null);
187: SQLDatabaseMetaData sqlmd = new SQLDatabaseMetaData(sqlcon);
188: try {
189: String quoteString = sqlmd.getIdentifierQuoteString();
190: assertEquals("foo", quoteString);
191: quoteString = sqlmd.getIdentifierQuoteString();
192: assertEquals("foo", quoteString);
193: quoteString = sqlmd.getIdentifierQuoteString();
194: assertEquals("foo", quoteString);
195: quoteString = sqlmd.getIdentifierQuoteString();
196: assertEquals("foo", quoteString);
197:
198: } catch (SQLException e) {
199: fail("Unexpected exception: " + e.getMessage());
200: }
201: }
202:
203: }
|