001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.dbMetaDataJdbc30
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.DatabaseMetaData;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.SQLException;
029: import java.sql.Statement;
030:
031: import org.apache.derby.iapi.reference.JDBC30Translation;
032: import org.apache.derby.iapi.services.info.JVMInfo;
033:
034: import org.apache.derby.tools.ij;
035: import org.apache.derbyTesting.functionTests.util.TestUtil;
036:
037: /**
038: * Test of database meta-data for new methods in jdbc 30. This program simply calls
039: * each of the new meta-data methods in jdbc30, one by one, and prints the results.
040: *
041: * @author mamta
042: */
043:
044: public class dbMetaDataJdbc30 {
045:
046: public static void main(String[] args) {
047: DatabaseMetaData met;
048: Connection con;
049: Statement s;
050:
051: System.out.println("Test dbMetaDataJdbc30 starting");
052:
053: try {
054: // use the ij utility to read the property file and
055: // make the initial connection.
056: ij.getPropertyArg(args);
057: con = ij.startJBMS();
058:
059: con.setAutoCommit(true); // make sure it is true
060:
061: s = con.createStatement();
062:
063: met = con.getMetaData();
064:
065: System.out.println("JDBC Driver '" + met.getDriverName()
066: + "', version " + met.getDriverMajorVersion() + "."
067: + met.getDriverMinorVersion() + " ("
068: + met.getDriverVersion() + ")");
069:
070: boolean pass = false;
071: try {
072: pass = TestUtil.compareURL(met.getURL());
073: } catch (NoSuchMethodError msme) {
074: // DatabaseMetaData.getURL not present - correct for JSR169
075: if (!TestUtil.HAVE_DRIVER_CLASS)
076: pass = true;
077: } catch (Throwable err) {
078: System.out.println("%%getURL() gave the exception: "
079: + err);
080: }
081:
082: if (pass)
083: System.out
084: .println("DatabaseMetaData.getURL test passed");
085: else
086: System.out
087: .println("FAIL: DatabaseMetaData.getURL test failed");
088:
089: System.out.println();
090: System.out.println("supportsSavepoints() : "
091: + met.supportsSavepoints());
092:
093: System.out.println();
094: System.out.println("supportsNamedParameters() : "
095: + met.supportsNamedParameters());
096:
097: System.out.println();
098: System.out.println("supportsMultipleOpenResults() : "
099: + met.supportsMultipleOpenResults());
100:
101: System.out.println();
102: System.out.println("supportsGetGeneratedKeys() : "
103: + met.supportsGetGeneratedKeys());
104:
105: System.out.println();
106: System.out
107: .println("supportsResultSetHoldability(HOLD_CURSORS_OVER_COMMIT) : "
108: + met
109: .supportsResultSetHoldability(JDBC30Translation.HOLD_CURSORS_OVER_COMMIT));
110:
111: System.out.println();
112: System.out
113: .println("supportsResultSetHoldability(CLOSE_CURSORS_AT_COMMIT) : "
114: + met
115: .supportsResultSetHoldability(JDBC30Translation.CLOSE_CURSORS_AT_COMMIT));
116:
117: System.out.println();
118: checkJDBCVersion(met);
119:
120: System.out.println();
121: System.out.println("getSQLStateType() : "
122: + met.getSQLStateType());
123:
124: System.out.println();
125: System.out.println("getResultSetHoldability() : "
126: + met.getResultSetHoldability());
127:
128: System.out.println();
129: System.out.println("getDatabaseMajorVersion() : "
130: + met.getDatabaseMajorVersion());
131:
132: System.out.println();
133: System.out.println("getDatabaseMinorVersion() : "
134: + met.getDatabaseMinorVersion());
135:
136: System.out.println();
137: System.out.println("supportsStatementPooling() : "
138: + met.supportsStatementPooling());
139:
140: System.out.println("getMaxColumnNameLength() = "
141: + met.getMaxColumnNameLength());
142: System.out.println("getMaxCursorNameLength() = "
143: + met.getMaxCursorNameLength());
144: System.out.println("getMaxSchemaNameLength() = "
145: + met.getMaxSchemaNameLength());
146: System.out.println("getMaxProcedureNameLength() = "
147: + met.getMaxProcedureNameLength());
148: System.out.println("getMaxTableNameLength() = "
149: + met.getMaxTableNameLength());
150: System.out.println("getMaxUserNameLength() = "
151: + met.getMaxUserNameLength());
152:
153: //following will give not implemented exceptions.
154: // JCC will return an empty result set, so either a
155: // result set with no rows or an exception will pass
156: System.out.println();
157: System.out.println("getSuperTypes() with null :");
158: checkEmptyRS(met.getSuperTypes(null, null, null));
159:
160: System.out.println();
161: System.out.println("getSuperTables() with null :");
162: checkEmptyRS(met.getSuperTables(null, null, null));
163:
164: System.out.println();
165: System.out.println("getAttributes() with null :");
166:
167: checkEmptyRS(met.getAttributes(null, null, null, null));
168:
169: System.out.println();
170: System.out.println("locatorsUpdateCopy(): ");
171: System.out.println("Returned: " + met.locatorsUpdateCopy());
172:
173: s.close();
174:
175: con.close();
176:
177: } catch (SQLException e) {
178: dumpSQLExceptions(e);
179: } catch (Throwable e) {
180: System.out.println("FAIL -- unexpected exception:");
181: e.printStackTrace(System.out);
182: }
183:
184: System.out.println("Test dbMetaDataJdbc30 finished");
185: }
186:
187: static private void dumpSQLExceptions(SQLException se) {
188: System.out.println("FAIL -- unexpected exception");
189: while (se != null) {
190: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
191: se.printStackTrace(System.out);
192: se = se.getNextException();
193: }
194: }
195:
196: public static void dumpRS(ResultSet s) throws SQLException {
197: ResultSetMetaData rsmd = s.getMetaData();
198:
199: // Get the number of columns in the result set
200: int numCols = rsmd.getColumnCount();
201:
202: if (numCols <= 0) {
203: System.out.println("(no columns!)");
204: return;
205: }
206:
207: // Display column headings
208: for (int i = 1; i <= numCols; i++) {
209: if (i > 1)
210: System.out.print(",");
211: System.out.print(rsmd.getColumnLabel(i));
212: }
213: System.out.println();
214:
215: // Display data, fetching until end of the result set
216: while (s.next()) {
217: // Loop through each column, getting the
218: // column data and displaying
219: for (int i = 1; i <= numCols; i++) {
220: if (i > 1)
221: System.out.print(",");
222: System.out.print(s.getString(i));
223: }
224: System.out.println();
225: }
226: s.close();
227: }
228:
229: /**
230: * Check whether <code>getJDBCMajorVersion()</code> and
231: * <code>getJDBCMinorVersion()</code> return the expected version numbers.
232: * @param met the <code>DatabaseMetaData</code> object to test
233: * @exception SQLException if a database error occurs
234: */
235: private static void checkJDBCVersion(DatabaseMetaData met)
236: throws SQLException {
237: final int major, minor;
238: if (TestUtil.isJCCFramework()) {
239: major = 3;
240: minor = 0;
241: } else if (JVMInfo.JDK_ID < JVMInfo.J2SE_16) {
242: major = 3;
243: minor = 0;
244: } else {
245: major = 4;
246: minor = 0;
247: }
248: System.out
249: .print("getJDBCMajorVersion()/getJDBCMinorVersion() : ");
250: int maj = met.getJDBCMajorVersion();
251: int min = met.getJDBCMinorVersion();
252: if (major == maj && minor == min) {
253: System.out.println("AS EXPECTED");
254: } else {
255: System.out.println("GOT " + maj + "." + min + ", EXPECTED "
256: + major + "." + minor);
257: }
258: }
259:
260: /**
261: * In order to be JDBC compliant, all metadata calls must return valid
262: * results, even if it's an empty result set. It should be considered
263: * a failure if we throw an exception
264: */
265: public static void checkEmptyRS(ResultSet rs) {
266: boolean passed = false;
267:
268: try {
269: if (rs != null) {
270: int numrows = 0;
271: while (rs.next())
272: numrows++;
273: // Zero rows is what we want.
274: if (numrows == 0)
275: passed = true;
276: }
277: } catch (SQLException e) {
278: System.out.println("Unexpected SQL Exception"
279: + e.getMessage());
280: e.printStackTrace();
281: } finally {
282: if (passed)
283: System.out.println("EXPECTED: Empty ResultSet");
284: else
285: System.out
286: .println("FAIL: Should have gotten empty ResultSet");
287: }
288: }
289: }
|