Revision: 6096
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6096&view=rev
Author: manningr
Date: 2010-12-31 20:04:42 +0000 (Fri, 31 Dec 2010)
Log Message:
-----------
Handle edge case of null Throwable. New test to verify behavior.
Modified Paths:
--------------
trunk/sql12/plugins/db2/src/main/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatter.java
Added Paths:
-----------
trunk/sql12/plugins/db2/src/test/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatterTest.java
Modified: trunk/sql12/plugins/db2/src/main/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatter.java
===================================================================
--- trunk/sql12/plugins/db2/src/main/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatter.java 2010-12-31 20:03:35 UTC (rev 6095)
+++ trunk/sql12/plugins/db2/src/main/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatter.java 2010-12-31 20:04:42 UTC (rev 6096)
@@ -1,4 +1,5 @@
package net.sourceforge.squirrel_sql.plugins.db2;
+
/*
* Copyright (C) 2007 Christoph Schmitz
*
@@ -19,85 +20,84 @@
import java.lang.reflect.Method;
import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
+import net.sourceforge.squirrel_sql.fw.util.Utilities;
/**
- * Formats an exception of the new DB2 JCC driver, where the human-readable
- * error message needs to be obtained from a DB2SqlCa object.
+ * Formats an exception of the new DB2 JCC driver, where the human-readable error message needs to be obtained
+ * from a DB2SqlCa object. Invokes DB2 specific methods via reflection in order to avoid the need for the
+ * proprietary DB2 class files for compilation.
*
- * Invokes DB2 specific methods via reflection in order to avoid the need for
- * the proprietary DB2 class files for compilation.
- *
* @author Christoph Schmitz <sch...@us...>
*/
-public class DB2JCCExceptionFormatter implements ExceptionFormatter {
+public class DB2JCCExceptionFormatter implements ExceptionFormatter
+{
- /*
- * As the JCC driver code is obfuscated, we do not check the full class
- * name, but resort to checking a prefix and suffix instead.
- *
- * In my version, the full class name is "com.ibm.db2.jcc.c.SqlException"
- */
+ /*
+ * As the JCC driver code is obfuscated, we do not check the full class
+ * name, but resort to checking a prefix and suffix instead.
+ *
+ * In my version, the full class name is "com.ibm.db2.jcc.c.SqlException"
+ */
- // Prefix for the JCC SqlException class name
- private static final String JCC_EXCEPTION_PREFIX = "com.ibm.db2.jcc";
+ // Prefix for the JCC SqlException class name
+ private static final String JCC_EXCEPTION_PREFIX = "com.ibm.db2.jcc";
- // Class name for the JCC SqlException class
- private static final String JCC_EXCEPTION_CLASS = "SqlException";
-
- // Names of the various methods we need to invoke
- private static final String METHOD_GET_SQLCA = "getSqlca";
+ // Class name for the JCC SqlException class
+ private static final String JCC_EXCEPTION_CLASS = "SqlException";
- private static final String METHOD_GET_SQL_STATE = "getSqlState";
+ // Names of the various methods we need to invoke
+ private static final String METHOD_GET_SQLCA = "getSqlca";
- private static final String METHOD_GET_SQL_CODE = "getSqlCode";
+ private static final String METHOD_GET_SQL_STATE = "getSqlState";
- private static final String METHOD_GET_MESSAGE = "getMessage";
+ private static final String METHOD_GET_SQL_CODE = "getSqlCode";
- /**
- * Checks if this {@link Throwable} is a DB2 JCC SqlException
- * (com.ibm.db2.jcc.*.SqlException) by testing for the proper prefix and
- * suffix of the class name
- *
- * @see net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter#formatsException(Throwable)
- */
- public boolean formatsException(Throwable t) {
- if (t == null) {
- return false;
- } else {
- String className = t.getClass().getName();
- return className.startsWith(JCC_EXCEPTION_PREFIX)
- && className.endsWith(JCC_EXCEPTION_CLASS);
- }
- }
+ private static final String METHOD_GET_MESSAGE = "getMessage";
- /**
- * @see net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter#format(Throwable)
- */
- public String format(Throwable t) throws Exception {
- StringBuilder builder = new StringBuilder();
- // DB2Sqlca sqlca = ((DB2Diagnosable) t).getSqlca();
- Method getSqlca = t.getClass().getMethod(METHOD_GET_SQLCA,
- (Class[]) null);
- Object sqlca = getSqlca.invoke(t, (Object[]) null);
+ /**
+ * Checks if this {@link Throwable} is a DB2 JCC SqlException (com.ibm.db2.jcc.*.SqlException) by testing
+ * for the proper prefix and suffix of the class name
+ *
+ * @see net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter#formatsException(Throwable)
+ */
+ public boolean formatsException(Throwable t)
+ {
+ if (t == null)
+ {
+ return false;
+ }
+ else
+ {
+ String className = t.getClass().getName();
+ return className.startsWith(JCC_EXCEPTION_PREFIX) && className.endsWith(JCC_EXCEPTION_CLASS);
+ }
+ }
- // String msg = sqlca.getMessage();
- Method getMessage = sqlca.getClass().getMethod(METHOD_GET_MESSAGE,
- (Class[]) null);
- String msg = getMessage.invoke(sqlca, (Object[]) null).toString();
+ /**
+ * @see net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter#format(Throwable)
+ */
+ public String format(Throwable t) throws Exception
+ {
+ Utilities.checkNull("format", "t", t);
- // int sqlCode = sqlca.getSqlCode();
- Method getSqlCode = sqlca.getClass().getMethod(METHOD_GET_SQL_CODE,
- (Class[]) null);
- int sqlCode = (Integer) getSqlCode.invoke(sqlca, (Object[]) null);
+ StringBuilder builder = new StringBuilder();
+ // DB2Sqlca sqlca = ((DB2Diagnosable) t).getSqlca();
+ Method getSqlca = t.getClass().getMethod(METHOD_GET_SQLCA, (Class[]) null);
+ Object sqlca = getSqlca.invoke(t, (Object[]) null);
- // int sqlstate = sqlca.getSqlState();
- Method getSqlState = sqlca.getClass().getMethod(
- METHOD_GET_SQL_STATE, (Class[]) null);
- String sqlState = getSqlState.invoke(sqlca, (Object[]) null)
- .toString();
+ // String msg = sqlca.getMessage();
+ Method getMessage = sqlca.getClass().getMethod(METHOD_GET_MESSAGE, (Class[]) null);
+ String msg = getMessage.invoke(sqlca, (Object[]) null).toString();
- builder.append(msg).append(" SQL Code: ").append(sqlCode).append(
- ", SQL State: ").append(sqlState);
- return builder.toString();
- }
+ // int sqlCode = sqlca.getSqlCode();
+ Method getSqlCode = sqlca.getClass().getMethod(METHOD_GET_SQL_CODE, (Class[]) null);
+ int sqlCode = (Integer) getSqlCode.invoke(sqlca, (Object[]) null);
+
+ // int sqlstate = sqlca.getSqlState();
+ Method getSqlState = sqlca.getClass().getMethod(METHOD_GET_SQL_STATE, (Class[]) null);
+ String sqlState = getSqlState.invoke(sqlca, (Object[]) null).toString();
+
+ builder.append(msg).append(" SQL Code: ").append(sqlCode).append(", SQL State: ").append(sqlState);
+ return builder.toString();
+ }
}
\ No newline at end of file
Added: trunk/sql12/plugins/db2/src/test/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatterTest.java
===================================================================
--- trunk/sql12/plugins/db2/src/test/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatterTest.java (rev 0)
+++ trunk/sql12/plugins/db2/src/test/java/net/sourceforge/squirrel_sql/plugins/db2/DB2JCCExceptionFormatterTest.java 2010-12-31 20:04:42 UTC (rev 6096)
@@ -0,0 +1,35 @@
+package net.sourceforge.squirrel_sql.plugins.db2;
+
+import net.sourceforge.squirrel_sql.fw.util.AbstractExceptionFormatterTest;
+import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
+
+
+/*
+ * Copyright (C) 2010 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+public class DB2JCCExceptionFormatterTest extends AbstractExceptionFormatterTest
+{
+
+ @Override
+ protected ExceptionFormatter getExceptionFormatterToTest()
+ {
+ return new DB2JCCExceptionFormatter();
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|