001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil
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.util;
023:
024: import java.io.PrintStream;
025: import java.io.PrintWriter;
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.IOException;
029:
030: import java.sql.Connection;
031: import java.sql.DriverManager;
032: import java.sql.SQLException;
033: import java.sql.SQLWarning;
034: import java.sql.Statement;
035: import java.sql.PreparedStatement;
036: import java.sql.ResultSet;
037: import java.sql.ResultSetMetaData;
038: import java.sql.Types;
039:
040: import java.util.Properties;
041: import java.util.Enumeration;
042: import java.util.Vector;
043:
044: import org.apache.derby.impl.tools.ij.ijException;
045:
046: import org.apache.derby.tools.JDBCDisplayUtil;
047:
048: /**
049: Show common format for Network Server and Embedded Exceptions
050: **/
051:
052: public class JDBCTestDisplayUtil extends JDBCDisplayUtil {
053:
054: /**
055: Show common format for Network Server and Embedded Exceptions
056: @param out PrintStream to write to
057: @param e Throwable to print
058: */
059:
060: static public void ShowCommonSQLException(PrintStream out,
061: Throwable e) {
062: if (e == null)
063: return;
064:
065: if (e instanceof SQLException) {
066: SQLException se = (SQLException) e;
067: if (isDataConversionException(se))
068: out.println("Data Conversion SQLException");
069: else if (isResultSetClosedException(se))
070: out.println("Result Set Closed Exception");
071: else if (isNullSQLStringException(se))
072: out.println("Null SQL String Exception");
073: else if (isInvalidParameterException(se))
074: out.println("Invalid Parameter SQL Exception");
075: else if (isValidOnScrollCursorsException(se))
076: out
077: .println("Method Only Valid On Scroll Cursors SQL Exception");
078: else if (isInvalidMethodReturnException(se))
079: out
080: .println("Invalid Method Returning a ResultSet or Row Count SQL Exception");
081: else if (isTableDoesNotExistException(se))
082: out.println("Table Does Not Exist SQL Exception");
083: else if (isReturnsInvalidResultSetException(se))
084: out
085: .println("Invalid Method Returning ResultSet SQL Exception");
086: else
087: ShowSQLException(out, se);
088: } else
089: ShowException(out, e);
090: }
091:
092: static private boolean isDataConversionException(SQLException se) {
093: if ((se.getMessage() != null && se.getMessage().indexOf(
094: "Invalid data conversion") >= 0)
095: || (se.getSQLState() != null && (se.getSQLState()
096: .equals("22018")
097: || se.getSQLState().equals("22005") || se
098: .getSQLState().equals("22007"))))
099: return true;
100: return false;
101: }
102:
103: static private boolean isResultSetClosedException(SQLException se) {
104: if ((se.getMessage() != null && se.getMessage().indexOf(
105: "Invalid operation: result set closed") >= 0)
106: || (se.getSQLState() != null && (se.getSQLState()
107: .equals("XCL16"))))
108: return true;
109: return false;
110: }
111:
112: static private boolean isNullSQLStringException(SQLException se) {
113: if ((se.getMessage() != null && se.getMessage().indexOf(
114: "Null SQL string passed.") >= 0)
115: || (se.getSQLState() != null && (se.getSQLState()
116: .equals("XJ067"))))
117: return true;
118: return false;
119: }
120:
121: static private boolean isInvalidParameterException(SQLException se) {
122: if ((se.getMessage() != null && se.getMessage().indexOf(
123: "Invalid parameter value") >= 0)
124: || (se.getMessage().indexOf("Invalid fetch size") >= 0)
125: || (se.getMessage().indexOf("Invalid fetch direction") >= 0)
126: || (se.getSQLState() != null && (se.getSQLState()
127: .equals("XJ066"))))
128: return true;
129: return false;
130: }
131:
132: static private boolean isValidOnScrollCursorsException(
133: SQLException se) {
134: if ((se.getMessage() != null && se.getMessage().indexOf(
135: "' method is only allowed on scroll cursors.") >= 0)
136: || (se.getSQLState() != null && (se.getSQLState()
137: .equals("XJ061"))))
138: return true;
139: return false;
140: }
141:
142: static private boolean isInvalidMethodReturnException(
143: SQLException se) {
144: if (((se.getMessage() != null && se.getMessage().indexOf(
145: "executeQuery method cannot be used for update.") >= 0) || se
146: .getMessage()
147: .indexOf(
148: "executeUpdate method cannot be used for query.") >= 0)
149: || (se.getSQLState() != null && (se.getSQLState()
150: .equals("X0Y78") || se.getSQLState().equals(
151: "X0Y79"))))
152: return true;
153: return false;
154: }
155:
156: static private boolean isTableDoesNotExistException(SQLException se) {
157: if (se.getSQLState() != null
158: && se.getSQLState().equals("42X05"))
159: return true;
160: return false;
161: }
162:
163: static private boolean isReturnsInvalidResultSetException(
164: SQLException se) {
165: if ((se.getMessage() != null && se
166: .getMessage()
167: .indexOf(
168: "cannot be called with a statement that returns a ResultSet.") >= 0)
169: || (se.getSQLState() != null && (se.getSQLState()
170: .equals("X0Y79"))))
171: return true;
172: return false;
173: }
174: }
|