From derby-commits-return-13648-apmail-db-derby-commits-archive=db.apache.org@db.apache.org Mon Jan 03 18:49:38 2011 Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 66818 invoked from network); 3 Jan 2011 18:49:38 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Jan 2011 18:49:38 -0000 Received: (qmail 86264 invoked by uid 500); 3 Jan 2011 18:49:38 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 86210 invoked by uid 500); 3 Jan 2011 18:49:38 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 86203 invoked by uid 99); 3 Jan 2011 18:49:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Jan 2011 18:49:37 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Jan 2011 18:49:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E44252388978; Mon, 3 Jan 2011 18:49:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1054706 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ engine/ engine/org/apache/derby/iapi/jdbc/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ Date: Mon, 03 Jan 2011 18:49:08 -0000 To: derby-commits@db.apache.org From: rhillegas@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110103184908.E44252388978@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: rhillegas Date: Mon Jan 3 18:49:08 2011 New Revision: 1054706 URL: http://svn.apache.org/viewvc?rev=1054706&view=rev Log: DERBY-4869: Add JDBC 4.1 overloads to CallableStatement.getObject(). Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41.java (with props) db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41Test.java (with props) Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement.java db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement40.java db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalCallableStatement40.java db/derby/code/trunk/java/engine/build.xml db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredCallableStatement40.java db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement40.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ResultSetTest.java Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement.java (original) +++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement.java Mon Jan 3 18:49:08 2011 @@ -1201,7 +1201,7 @@ public class CallableStatement extends P } } - private SQLException jdbcMethodNotImplemented() throws SQLException + protected SQLException jdbcMethodNotImplemented() throws SQLException { try { Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement40.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement40.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement40.java (original) +++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CallableStatement40.java Mon Jan 3 18:49:08 2011 @@ -23,12 +23,16 @@ package org.apache.derby.client.am; import java.io.InputStream; import java.io.Reader; +import java.math.BigDecimal; import java.sql.Blob; import java.sql.Clob; +import java.sql.Date; import java.sql.NClob; import java.sql.RowId; import java.sql.SQLException; import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; import org.apache.derby.client.ClientPooledConnection; import org.apache.derby.client.am.SqlException; import org.apache.derby.client.am.ClientMessageId; @@ -320,4 +324,64 @@ public class CallableStatement40 extends throws SQLException { throw SQLExceptionFactory.notImplemented ("setCharacterStream(String,Reader,long)"); } + + //////////////////////////////////////////////////////////////////// + // + // INTRODUCED BY JDBC 4.1 IN JAVA 7 + // + //////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") + public T getObject( int parameterIndex, Class type ) + throws SQLException + { + // checkForClosedStatement() should be called by all of the + // more specific methods to which we forward this call + + if ( type == null ) + { + throw mismatchException( "NULL", parameterIndex ); + } + + try { + if ( String.class.equals( type ) ) { return (T) getString( parameterIndex ); } + else if ( BigDecimal.class.equals( type ) ) { return (T) getBigDecimal( parameterIndex ); } + else if ( Boolean.class.equals( type ) ) { return (T) Boolean.valueOf( getBoolean(parameterIndex ) ); } + else if ( Byte.class.equals( type ) ) { return (T) Byte.valueOf( getByte( parameterIndex ) ); } + else if ( Short.class.equals( type ) ) { return (T) Short.valueOf( getShort( parameterIndex ) ); } + else if ( Integer.class.equals( type ) ) { return (T) Integer.valueOf( getInt( parameterIndex ) ); } + else if ( Long.class.equals( type ) ) { return (T) Long.valueOf( getLong( parameterIndex ) ); } + else if ( Float.class.equals( type ) ) { return (T) Float.valueOf( getFloat( parameterIndex ) ); } + else if ( Double.class.equals( type ) ) { return (T) Double.valueOf( getDouble( parameterIndex ) ); } + else if ( Date.class.equals( type ) ) { return (T) getDate( parameterIndex ); } + else if ( Time.class.equals( type ) ) { return (T) getTime( parameterIndex ); } + else if ( Timestamp.class.equals( type ) ) { return (T) getTimestamp( parameterIndex ); } + else if ( Blob.class.equals( type ) ) { return (T) getBlob( parameterIndex ); } + else if ( Clob.class.equals( type ) ) { return (T) getClob( parameterIndex ); } + else if ( type.isArray() && type.getComponentType().equals( byte.class ) ) { return (T) getBytes( parameterIndex ); } + else + { + return type.cast( getObject( parameterIndex ) ); + } + } + catch (ClassCastException e) {} + + throw mismatchException( type.getName(), parameterIndex ); + } + private SQLException mismatchException( String targetTypeName, int parameterIndex ) + throws SQLException + { + String sourceTypeName = getParameterMetaData().getParameterTypeName( parameterIndex ); + ClientMessageId cmi = new ClientMessageId( SQLState.LANG_DATA_TYPE_GET_MISMATCH ); + SqlException se = new SqlException( agent_.logWriter_, cmi, targetTypeName, sourceTypeName ); + + return se.getSQLException(); + } + + public T getObject(String parameterName, Class type) + throws SQLException + { + throw jdbcMethodNotImplemented(); + } + } Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalCallableStatement40.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalCallableStatement40.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalCallableStatement40.java (original) +++ db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalCallableStatement40.java Mon Jan 3 18:49:08 2011 @@ -348,4 +348,23 @@ public class LogicalCallableStatement40 throws SQLException { getPhysCs().setNClob(arg0, arg1); } + + //////////////////////////////////////////////////////////////////// + // + // INTRODUCED BY JDBC 4.1 IN JAVA 7 + // + //////////////////////////////////////////////////////////////////// + + public T getObject( int parameterIndex, Class type ) + throws SQLException + { + return ((CallableStatement40)getPhysCs()).getObject( parameterIndex, type ); + } + + public T getObject(String parameterName, Class type) + throws SQLException + { + return ((CallableStatement40)getPhysCs()).getObject( parameterName, type ); + } + } Modified: db/derby/code/trunk/java/engine/build.xml URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/build.xml?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/build.xml (original) +++ db/derby/code/trunk/java/engine/build.xml Mon Jan 3 18:49:08 2011 @@ -55,9 +55,9 @@ - + Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredCallableStatement40.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredCallableStatement40.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredCallableStatement40.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredCallableStatement40.java Mon Jan 3 18:49:08 2011 @@ -31,6 +31,7 @@ import java.sql.SQLException; import java.sql.SQLXML; import org.apache.derby.iapi.reference.SQLState; import org.apache.derby.impl.jdbc.Util; +import org.apache.derby.impl.jdbc.EmbedCallableStatement40; public class BrokeredCallableStatement40 extends BrokeredCallableStatement30{ @@ -441,4 +442,22 @@ public class BrokeredCallableStatement40 throws SQLException { getCallableStatement().setCharacterStream(parameterName,x,length); } + + //////////////////////////////////////////////////////////////////// + // + // INTRODUCED BY JDBC 4.1 IN JAVA 7 + // + //////////////////////////////////////////////////////////////////// + + public T getObject( int parameterIndex, Class type ) + throws SQLException + { + return ((EmbedCallableStatement40) getCallableStatement()).getObject( parameterIndex, type ); + } + + public T getObject(String parameterName, Class type) + throws SQLException + { + return ((EmbedCallableStatement40) getCallableStatement()).getObject( parameterName, type ); + } } Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement40.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement40.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement40.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement40.java Mon Jan 3 18:49:08 2011 @@ -23,13 +23,17 @@ package org.apache.derby.impl.jdbc; import java.io.InputStream; import java.io.Reader; +import java.math.BigDecimal; import java.sql.Blob; import java.sql.Clob; +import java.sql.Date; import java.sql.NClob; import java.sql.ParameterMetaData; import java.sql.RowId; import java.sql.SQLException; import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; import org.apache.derby.iapi.reference.SQLState; @@ -323,4 +327,62 @@ public class EmbedCallableStatement40 ex throws SQLException { throw Util.notImplemented(); } + + //////////////////////////////////////////////////////////////////// + // + // INTRODUCED BY JDBC 4.1 IN JAVA 7 + // + //////////////////////////////////////////////////////////////////// + + @SuppressWarnings("unchecked") + public T getObject( int parameterIndex, Class type ) + throws SQLException + { + checkStatus(); + + if ( type == null ) + { + throw mismatchException( "NULL", parameterIndex ); + } + + try { + if ( String.class.equals( type ) ) { return (T) getString( parameterIndex ); } + else if ( BigDecimal.class.equals( type ) ) { return (T) getBigDecimal( parameterIndex ); } + else if ( Boolean.class.equals( type ) ) { return (T) Boolean.valueOf( getBoolean(parameterIndex ) ); } + else if ( Byte.class.equals( type ) ) { return (T) Byte.valueOf( getByte( parameterIndex ) ); } + else if ( Short.class.equals( type ) ) { return (T) Short.valueOf( getShort( parameterIndex ) ); } + else if ( Integer.class.equals( type ) ) { return (T) Integer.valueOf( getInt( parameterIndex ) ); } + else if ( Long.class.equals( type ) ) { return (T) Long.valueOf( getLong( parameterIndex ) ); } + else if ( Float.class.equals( type ) ) { return (T) Float.valueOf( getFloat( parameterIndex ) ); } + else if ( Double.class.equals( type ) ) { return (T) Double.valueOf( getDouble( parameterIndex ) ); } + else if ( Date.class.equals( type ) ) { return (T) getDate( parameterIndex ); } + else if ( Time.class.equals( type ) ) { return (T) getTime( parameterIndex ); } + else if ( Timestamp.class.equals( type ) ) { return (T) getTimestamp( parameterIndex ); } + else if ( Blob.class.equals( type ) ) { return (T) getBlob( parameterIndex ); } + else if ( Clob.class.equals( type ) ) { return (T) getClob( parameterIndex ); } + else if ( type.isArray() && type.getComponentType().equals( byte.class ) ) { return (T) getBytes( parameterIndex ); } + else + { + return type.cast( getObject( parameterIndex ) ); + } + } + catch (ClassCastException e) {} + + throw mismatchException( type.getName(), parameterIndex ); + } + private SQLException mismatchException( String targetTypeName, int parameterIndex ) + throws SQLException + { + String sourceTypeName = getParameterMetaData().getParameterTypeName( parameterIndex ); + SQLException se = newSQLException( SQLState.LANG_DATA_TYPE_GET_MISMATCH, targetTypeName, sourceTypeName ); + + return se; + } + + public T getObject(String parameterName, Class type) + throws SQLException + { + throw Util.notImplemented(); + } + } Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java Mon Jan 3 18:49:08 2011 @@ -1,6 +1,6 @@ /* - Derby - Class CallableStatementTest + Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.CallableStatementTest Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -23,11 +23,16 @@ package org.apache.derbyTesting.function import junit.framework.*; +import org.apache.derby.iapi.types.HarmonySerialBlob; +import org.apache.derby.iapi.types.HarmonySerialClob; + import org.apache.derbyTesting.junit.BaseJDBCTestCase; import org.apache.derbyTesting.junit.TestConfiguration; + import java.io.IOException; import java.io.Reader; +import java.math.BigDecimal; import java.sql.*; import java.lang.reflect.Method; import java.util.Vector; @@ -35,10 +40,8 @@ import java.util.Vector; /** * Tests of the java.sql.CallableStatement JDBC40 API. */ -public class CallableStatementTest - extends BaseJDBCTestCase { - - +public class CallableStatementTest extends Wrapper41Test +{ /** Default callable statement used by the tests. */ private CallableStatement cStmt = null; @@ -581,6 +584,152 @@ public class CallableStatementTest } /** + * Test the JDBC 4.1 extensions. + */ + public void testJDBC4_1() throws Exception + { + Connection conn = getConnection(); + + vetDataTypeCount( conn ); + + PreparedStatement ps = prepareStatement + ( + conn, + "create procedure allTypesProc\n" + + "(\n" + + " out bigintCol bigint,\n" + + " out blobCol blob,\n" + + " out booleanCol boolean,\n" + + " out charCol char(1),\n" + + " out charForBitDataCol char(1) for bit data,\n" + + " out clobCol clob,\n" + + " out dateCol date,\n" + + " out doubleCol double,\n" + + " out floatCol float,\n" + + " out intCol int,\n" + + " out longVarcharCol long varchar,\n" + + " out longVarcharForBitDataCol long varchar for bit data,\n" + + " out numericCol numeric,\n" + + " out realCol real,\n" + + " out smallintCol smallint,\n" + + " out timeCol time,\n" + + " out timestampCol timestamp,\n" + + " out varcharCol varchar( 2 ),\n" + + " out varcharForBitDataCol varchar( 2 ) for bit data\n" + + ")\n" + + "language java\n" + + "parameter style java\n" + + "no sql\n" + + "external name 'org.apache.derbyTesting.functionTests.tests.jdbc4.CallableStatementTest.allTypesProc'\n" + ); + ps.execute(); + ps.close(); + + CallableStatement cs = prepareCall + ( + conn, + "call allTypesProc( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" + ); + int param = 1; + cs.registerOutParameter( param++, Types.BIGINT ); + cs.registerOutParameter( param++, Types.BLOB ); + cs.registerOutParameter( param++, Types.BOOLEAN ); + cs.registerOutParameter( param++, Types.CHAR ); + cs.registerOutParameter( param++, Types.BINARY ); + cs.registerOutParameter( param++, Types.CLOB ); + cs.registerOutParameter( param++, Types.DATE ); + cs.registerOutParameter( param++, Types.DOUBLE ); + cs.registerOutParameter( param++, Types.FLOAT ); + cs.registerOutParameter( param++, Types.INTEGER ); + cs.registerOutParameter( param++, Types.LONGVARCHAR ); + cs.registerOutParameter( param++, Types.LONGVARBINARY ); + cs.registerOutParameter( param++, Types.NUMERIC ); + cs.registerOutParameter( param++, Types.REAL ); + cs.registerOutParameter( param++, Types.SMALLINT ); + cs.registerOutParameter( param++, Types.TIME ); + cs.registerOutParameter( param++, Types.TIMESTAMP ); + cs.registerOutParameter( param++, Types.VARCHAR ); + cs.registerOutParameter( param++, Types.VARBINARY ); + cs.execute(); + examineJDBC4_1extensions( new Wrapper41( cs ) ); + cs.close(); + + ps = prepareStatement( conn, "drop procedure allTypesProc" ); + ps.execute(); + ps.close(); + } + private void vetDataTypeCount( Connection conn ) throws Exception + { + ResultSet rs = conn.getMetaData().getTypeInfo(); + int actualTypeCount = 0; + while ( rs.next() ) { actualTypeCount++; } + rs.close(); + + // + // If this assertion fails, that means that another data type has been added + // to Derby. You need to add that datatype to the allTypesProc procedure created + // by testJDBC4_1() and you need to add a verification case to examineJDBC4_1extensions(). + // + assertEquals( 22, actualTypeCount ); + } + + /** + *

+ * Procedure used by jdbc 4.1 tests. + *

+ */ + public static void allTypesProc + ( + long[] bigintarg, + Blob[] blobarg, + boolean[] booleanarg, + String[] chararg, + byte[][] charforbitdataarg, + Clob[] clobarg, + Date[] datearg, + double[] doublearg, + double[] floatarg, + int[] intarg, + String[] longvarchararg, + byte[][] longvarcharforbitdataarg, + BigDecimal[] numericarg, + float[] realarg, + short[] smallintarg, + Time[] timearg, + Timestamp[] timestamparg, + String[] varchararg, + byte[][] varcharforbitdataarg + ) + throws Exception + { + String stringValue = "a"; + byte[] binaryValue = new byte[] { (byte) 0xde }; + byte intValue = (byte) 1; + float floatValue = 1.0F; + String lobValue = "abc"; + + bigintarg[0] = intValue; + blobarg[0] = new HarmonySerialBlob( lobValue.getBytes( "UTF-8" )); + booleanarg[0] = true; + chararg[0] = stringValue; + charforbitdataarg[0] = binaryValue; + clobarg[0] = new HarmonySerialClob( lobValue ); + datearg[0]= new Date( 761990400000L ); + doublearg[0] = floatValue; + floatarg[0] = floatValue; + intarg[0] = intValue; + longvarchararg[0] = stringValue; + longvarcharforbitdataarg[0] = binaryValue; + numericarg[0] = new BigDecimal( "1.0" ); + realarg[0] = floatValue; + smallintarg[0] = intValue; + timearg[0] = new Time( 83342000L ); + timestamparg[0] = new Timestamp( -229527385766L ); + varchararg[0] = stringValue; + varcharforbitdataarg[0] = binaryValue; + } + + /** * Return suite with all tests of the class. */ public static Test suite() { Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ResultSetTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ResultSetTest.java?rev=1054706&r1=1054705&r2=1054706&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ResultSetTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ResultSetTest.java Mon Jan 3 18:49:08 2011 @@ -1,6 +1,6 @@ /* - Derby - Class ResultSetTest + Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.ResultSetTest Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -50,9 +50,6 @@ import junit.framework.Test; import junit.framework.TestSuite; import junit.extensions.TestSetup; -import org.apache.derby.impl.jdbc.EmbedResultSet40; -import org.apache.derby.client.net.NetResultSet40; - import org.apache.derbyTesting.junit.BaseJDBCTestCase; import org.apache.derbyTesting.junit.BaseJDBCTestSetup; import org.apache.derbyTesting.junit.TestConfiguration; @@ -65,50 +62,8 @@ import org.apache.derbyTesting.junit.Tes * This test also makes use of a TestSetup wrapper to perform one-time * setup and teardown for the whole suite. */ -public class ResultSetTest - extends BaseJDBCTestCase +public class ResultSetTest extends Wrapper41Test { - // - // Wrapper class for testing JDBC 4.1 functionality. - // - // This wrapper can be removed after Java 7 goes GA and we are allowed - // to use the Java 7 compiler to build derbyTesting.jar. - // - public static final class Wrapper41 - { - private EmbedResultSet40 _embedded; - private NetResultSet40 _netclient; - - public Wrapper41( ResultSet rs ) throws Exception - { - if ( rs instanceof EmbedResultSet40 ) { _embedded = (EmbedResultSet40) rs; } - else if ( rs instanceof NetResultSet40 ) { _netclient = (NetResultSet40) rs; } - else { throw nothingWrapped(); } - } - - public T getObject( int columnIndex, Class type ) throws SQLException - { - if ( _embedded != null ) { return _embedded.getObject( columnIndex, type ); } - else if ( _netclient != null ) { return _netclient.getObject( columnIndex, type ); } - else { throw nothingWrapped(); } - } - public T getObject( String columnName, Class type ) - throws SQLException - { - if ( _embedded != null ) { return _embedded.getObject( columnName, type ); } - else if ( _netclient != null ) { return _netclient.getObject( columnName, type ); } - else { throw nothingWrapped(); } - } - - private SQLException nothingWrapped() { return new SQLException( "Nothing wrapped!" ); } - } - - private static final String UNSUPPORTED_COERCION = "22005"; - private static final String BAD_FORMAT = "22018"; - private static final String BAD_DATETIME = "22007"; - - private static final String VARIABLE_STRING = "XXXXX"; - private static final byte[] BYTES1 = { 0x65, 0x66, 0x67, 0x68, 0x69, 0x69, 0x68, 0x67, 0x66, 0x65 @@ -137,8 +92,6 @@ public class ResultSetTest /** Default row identifier used by the tests. */ private int key = -1; - private Class byteArrayClass; - /** * Create test with given name. * @@ -2120,7 +2073,7 @@ public class ResultSetTest ps = prepareStatement( conn, "select * from allTypes" ); ResultSet rs = ps.executeQuery(); rs.next(); - examineJDBC4_1extensions( rs ); + examineJDBC4_1extensions( new Wrapper41( rs ) ); rs.close(); ps.close(); @@ -2145,518 +2098,6 @@ public class ResultSetTest // assertEquals( 22, actualTypeCount ); } - private void examineJDBC4_1extensions( ResultSet rs ) throws Exception - { - Wrapper41 wrapper = new Wrapper41( rs ); - - println( "Vetting a " + rs.getClass().getName() ); - - byteArrayClass = Class.forName( "[B" ); - - vetWrappedNull( wrapper ); - vetWrappedInteger( wrapper, 1, "BIGINTCOL" ); - vetWrappedBlob( wrapper ); - vetWrappedBoolean( wrapper ); - vetWrappedString( wrapper, 4, "CHARCOL" ); - vetWrappedBinary( wrapper, 5, "CHARFORBITDATACOL" ); - vetWrappedClob( wrapper ); - vetWrappedDate( wrapper ); - vetWrappedFloatingPoint( wrapper, 8, "DOUBLECOL" ); - vetWrappedFloatingPoint( wrapper, 9, "FLOATCOL" ); - vetWrappedInteger( wrapper, 10, "INTCOL" ); - vetWrappedString( wrapper, 11, "LONGVARCHARCOL" ); - vetWrappedBinary( wrapper, 12, "LONGVARCHARFORBITDATACOL" ); - vetWrappedInteger( wrapper, 13, "NUMERICCOL" ); - vetWrappedFloatingPoint( wrapper, 14, "REALCOL" ); - vetWrappedInteger( wrapper, 15, "SMALLINTCOL" ); - vetWrappedTime( wrapper ); - vetWrappedTimestamp( wrapper ); - vetWrappedString( wrapper, 18, "VARCHARCOL" ); - vetWrappedBinary( wrapper, 19, "VARCHARFORBITDATACOL" ); - } - @SuppressWarnings("unchecked") - private void vetWrappedNull( Wrapper41 wrapper ) throws Exception - { - try { - wrapper.getObject( 1, (Class) null ); - fail( "Did not expect to get a result for a null class type." ); - } - catch (SQLException e) - { - assertSQLState( "Null type", UNSUPPORTED_COERCION, e ); - } - try { - wrapper.getObject( "BIGINTCOL", (Class) null ); - fail( "Did not expect to get a result for a null class type." ); - } - catch (SQLException e) - { - assertSQLState( "Null type", UNSUPPORTED_COERCION, e ); - } - } - private void vetWrappedInteger( Wrapper41 wrapper, int colID, String colName ) throws Exception - { - vetWrapperOK - ( - wrapper, - colID, - colName, - "1", - new Class[] { String.class, BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, Number.class, Object.class } - ); - vetWrapperOK - ( - wrapper, - colID, - colName, - "1.0", - new Class[] { Float.class, Double.class } - ); - vetWrapperOK - ( - wrapper, - colID, - colName, - "true", - new Class[] { Boolean.class } - ); - - vetNoWrapper - ( - wrapper, - colID, - colName, - new Class[] { Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, byteArrayClass, getClass() } - ); - } - private void vetWrappedBlob( Wrapper41 wrapper ) throws Exception - { - vetWrapperOK - ( - wrapper, - 2, - "BLOBCOL", - "abc", - new Class[] { Blob.class, Object.class, byteArrayClass, String.class, } - ); - - vetNoWrapper - ( - wrapper, - 2, - "BLOBCOL", - new Class[] - { - BigDecimal.class, Boolean.class, - Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, - Date.class, Time.class, Timestamp.class - } - ); - - // - // We don't try to get a Clob value because we have already gotten a LOB value. - // Trying to open another LOB stream raises an error. Using a random class type - // also takes us down that code path, so we don't verify against getClass() either. - // - } - private void vetWrappedBoolean( Wrapper41 wrapper ) throws Exception - { - vetWrapperOK - ( - wrapper, - 3, - "BOOLEANCOL", - "true", - new Class[] { String.class, Boolean.class, Object.class } - ); - vetWrapperOK - ( - wrapper, - 3, - "BOOLEANCOL", - "1", - new Class[] { BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class } - ); - vetWrapperOK - ( - wrapper, - 3, - "BOOLEANCOL", - "1.0", - new Class[] { Float.class, Double.class } - ); - - vetNoWrapper - ( - wrapper, - 3, - "BOOLEANCOL", - new Class[] { Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, byteArrayClass, getClass() } - ); - } - private void vetWrappedString( Wrapper41 wrapper, int colID, String colName ) throws Exception - { - vetWrapperOK - ( - wrapper, - colID, - colName, - "a", - new Class[] { String.class, Object.class } - ); - - vetWrapperOK - ( - wrapper, - colID, - colName, - "true", - new Class[] { Boolean.class } - ); - - vetCoercionError - ( - wrapper, - colID, - colName, - new Class[] - { - BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, - Float.class, Double.class, - }, - BAD_FORMAT - ); - - vetCoercionError - ( - wrapper, - colID, - colName, - new Class[] - { - Date.class, Time.class, Timestamp.class - }, - BAD_DATETIME - ); - - vetNoWrapper - ( - wrapper, - colID, - colName, - new Class[] - { - Blob.class, Clob.class, byteArrayClass, getClass() - } - ); - } - private void vetWrappedBinary( Wrapper41 wrapper, int colID, String colName ) throws Exception - { - vetWrapperOK - ( - wrapper, - colID, - colName, - "de", - new Class[] { String.class } - ); - vetWrapperOK - ( - wrapper, - colID, - colName, - "\ufffd", - new Class[] { byteArrayClass, Object.class } - ); - - vetNoWrapper - ( - wrapper, - colID, - colName, - new Class[] - { - Boolean.class, BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, - Float.class, Double.class, - Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, getClass() - } - ); - } - private void vetWrappedClob( Wrapper41 wrapper ) throws Exception - { - vetWrapperOK - ( - wrapper, - 6, - "CLOBCOL", - "abc", - new Class[] { String.class, Clob.class, Object.class } - ); - - vetNoWrapper - ( - wrapper, - 6, - "CLOBCOL", - new Class[] - { - Boolean.class, - BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, - Float.class, Double.class, - Date.class, Time.class, Timestamp.class, - byteArrayClass - } - ); - - // - // We don't test getting a BLOB because we are only allowed one attempt - // to get a LOB from the column. Using a random class type - // also takes us down that code path, so we don't verify against getClass() either. - // - } - private void vetWrappedDate( Wrapper41 wrapper ) throws Exception - { - vetWrapperOK - ( - wrapper, - 7, - "DATECOL", - "1994-02-23", - new Class[] { String.class, Date.class, Object.class } - ); - vetWrapperOK - ( - wrapper, - 7, - "DATECOL", - "1994-02-23 00:00:00.0", - new Class[] { Timestamp.class } - ); - - vetNoWrapper - ( - wrapper, - 7, - "DATECOL", - new Class[] - { - Boolean.class, - BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, - Float.class, Double.class, - Time.class, - Blob.class, Clob.class, byteArrayClass, getClass() - } - ); - } - private void vetWrappedFloatingPoint( Wrapper41 wrapper, int colID, String colName ) throws Exception - { - vetWrapperOK - ( - wrapper, - colID, - colName, - "1.0", - new Class[] { String.class, Float.class, Double.class, BigDecimal.class, Number.class, Object.class } - ); - vetWrapperOK - ( - wrapper, - colID, - colName, - "1", - new Class[] { Byte.class, Short.class, Integer.class, Long.class } - ); - vetWrapperOK - ( - wrapper, - colID, - colName, - "true", - new Class[] { Boolean.class } - ); - - vetNoWrapper - ( - wrapper, - colID, - colName, - new Class[] { Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, byteArrayClass, getClass() } - ); - } - private void vetWrappedTime( Wrapper41 wrapper ) throws Exception - { - vetWrapperOK - ( - wrapper, - 16, - "TIMECOL", - "15:09:02", - new Class[] { String.class, Time.class, Object.class } - ); - vetWrapperOK - ( - wrapper, - 16, - "TIMECOL", - VARIABLE_STRING, - new Class[] { Timestamp.class } - ); - - vetNoWrapper - ( - wrapper, - 16, - "TIMECOL", - new Class[] - { - Boolean.class, - BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, - Float.class, Double.class, - Date.class, - Blob.class, Clob.class, byteArrayClass, getClass() - } - ); - } - private void vetWrappedTimestamp( Wrapper41 wrapper ) throws Exception - { - vetWrapperOK - ( - wrapper, - 17, - "TIMESTAMPCOL", - "1962-09-23 03:23:34.234", - new Class[] { String.class, Timestamp.class, Object.class } - ); - vetWrapperOK - ( - wrapper, - 17, - "TIMESTAMPCOL", - "03:23:34", - new Class[] { Time.class } - ); - vetWrapperOK - ( - wrapper, - 17, - "TIMESTAMPCOL", - "1962-09-23", - new Class[] { Date.class } - ); - - vetNoWrapper - ( - wrapper, - 17, - "TIMESTAMPCOL", - new Class[] - { - Boolean.class, - BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, - Float.class, Double.class, - Blob.class, Clob.class, byteArrayClass, getClass() - } - ); - } - - @SuppressWarnings("unchecked") - private void vetWrapperOK - ( Wrapper41 wrapper, int colID, String colName, String expectedValue, Class[] supportedCoercions ) - throws Exception - { - int coercionCount = supportedCoercions.length; - for ( int i = 0; i < coercionCount; i++ ) - { - Class candidate = supportedCoercions[ i ]; - vetCandidate( candidate, expectedValue, wrapper.getObject( colID, candidate ) ); - - // you can only retrieve a LOB once - if ( (candidate == Blob.class) || (candidate == Clob.class) ) { return; } - - vetCandidate( candidate, expectedValue, wrapper.getObject( colName, candidate ) ); - } - } - @SuppressWarnings("unchecked") - private void vetCandidate( Class candidate, String expectedValue, Object actualValue ) - throws Exception - { - assertTrue( candidate.getName(), candidate.isAssignableFrom( actualValue.getClass( ) ) ); - - if ( VARIABLE_STRING.equals( expectedValue ) ) { return; } - - String actualString; - if ( actualValue instanceof Blob ) - { - Blob blob = (Blob) actualValue; - actualString = squeezeString( blob.getBytes( 1L, (int) blob.length() ) ); - } - else if ( actualValue instanceof byte[] ) - { - actualString = squeezeString( (byte[]) actualValue ); - } - else if ( actualValue instanceof Clob ) - { - Clob clob = (Clob) actualValue; - actualString = clob.getSubString( 1L, (int) clob.length() ); - } - else { actualString = actualValue.toString(); } - - assertEquals( candidate.getName(), expectedValue, actualString ); - } - private String squeezeString( byte[] bytes ) throws Exception - { - - String result = new String( bytes, "UTF-8" ); - - return result; - } - private void vetNoWrapper - ( Wrapper41 wrapper, int colID, String colName, Class[] unsupportedCoercions ) - throws Exception - { - vetCoercionError( wrapper, colID, colName, unsupportedCoercions, UNSUPPORTED_COERCION ); - } - @SuppressWarnings("unchecked") - private void vetCoercionError - ( Wrapper41 wrapper, int colID, String colName, Class[] unsupportedCoercions, String expectedSQLState ) - throws Exception - { - int coercionCount = unsupportedCoercions.length; - for ( int i = 0; i < coercionCount; i++ ) - { - Class candidate = unsupportedCoercions[ i ]; - - try { - wrapper.getObject( colID, candidate ); - fail( "Did not expect to get a " + candidate.getName() ); - } - catch (SQLException e) - { - assertSQLState( candidate.getName(), expectedSQLState, e ); - } - - // you can only retrieve a LOB once - if ( (candidate == Blob.class) || (candidate == Clob.class) ) { return; } - - try { - wrapper.getObject( colName, candidate ); - fail( "Did not expect to get a " + candidate.getName() ); - } - catch (SQLException e) - { - assertSQLState( candidate.getName(), expectedSQLState, e ); - } - } - } - private PreparedStatement prepareStatement( Connection conn, String text ) - throws Exception - { - println( text ); - - PreparedStatement ps = conn.prepareStatement( text ); - - return ps; - } } Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41.java?rev=1054706&view=auto ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41.java (added) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41.java Mon Jan 3 18:49:08 2011 @@ -0,0 +1,124 @@ +/* + + Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.Wrapper41 + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to you under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + */ + +package org.apache.derbyTesting.functionTests.tests.jdbc4; + +import java.sql.SQLException; + +import org.apache.derby.impl.jdbc.EmbedResultSet40; +import org.apache.derby.client.net.NetResultSet40; +import org.apache.derby.impl.jdbc.EmbedCallableStatement40; +import org.apache.derby.client.am.CallableStatement40; +import org.apache.derby.iapi.jdbc.BrokeredCallableStatement40; +import org.apache.derby.client.am.LogicalCallableStatement40; + +/** + * A wrapper around the getObject() overloads added by JDBC 4.1. + * We can eliminate this class after Java 7 goes GA and we are allowed + * to use the Java 7 compiler to build our released versions of derbyTesting.jar. + */ +public class Wrapper41 +{ + /////////////////////////////////////////////////////////////////////// + // + // STATE + // + /////////////////////////////////////////////////////////////////////// + + private EmbedResultSet40 _embedded; + private NetResultSet40 _netclient; + private EmbedCallableStatement40 _embedCallableStatement; + private CallableStatement40 _callableStatement; + private BrokeredCallableStatement40 _brokeredCallableStatement; + private LogicalCallableStatement40 _logicalCallableStatement; + + /////////////////////////////////////////////////////////////////////// + // + // CONSTRUCTORS + // + /////////////////////////////////////////////////////////////////////// + + public Wrapper41( Object wrapped ) throws Exception + { + if ( wrapped instanceof EmbedResultSet40 ) { _embedded = (EmbedResultSet40) wrapped; } + else if ( wrapped instanceof NetResultSet40 ) { _netclient = (NetResultSet40) wrapped; } + else if ( wrapped instanceof EmbedCallableStatement40 ) { _embedCallableStatement = (EmbedCallableStatement40) wrapped; } + else if ( wrapped instanceof CallableStatement40 ) { _callableStatement = (CallableStatement40) wrapped; } + else if ( wrapped instanceof BrokeredCallableStatement40 ) { _brokeredCallableStatement = (BrokeredCallableStatement40) wrapped; } + else if ( wrapped instanceof LogicalCallableStatement40 ) { _logicalCallableStatement = (LogicalCallableStatement40) wrapped; } + else { throw nothingWrapped(); } + } + + /////////////////////////////////////////////////////////////////////// + // + // JDBC 4.1 BEHAVIOR + // + /////////////////////////////////////////////////////////////////////// + + public T getObject( int columnIndex, Class type ) throws SQLException + { + if ( _embedded != null ) { return _embedded.getObject( columnIndex, type ); } + else if ( _netclient != null ) { return _netclient.getObject( columnIndex, type ); } + else if ( _embedCallableStatement != null ) { return _embedCallableStatement.getObject( columnIndex, type ); } + else if ( _callableStatement != null ) { return _callableStatement.getObject( columnIndex, type ); } + else if ( _brokeredCallableStatement != null ) { return _brokeredCallableStatement.getObject( columnIndex, type ); } + else if ( _logicalCallableStatement != null ) { return _logicalCallableStatement.getObject( columnIndex, type ); } + else { throw nothingWrapped(); } + } + public T getObject( String columnName, Class type ) + throws SQLException + { + if ( _embedded != null ) { return _embedded.getObject( columnName, type ); } + else if ( _netclient != null ) { return _netclient.getObject( columnName, type ); } + else if ( _embedCallableStatement != null ) { return _embedCallableStatement.getObject( columnName, type ); } + else if ( _callableStatement != null ) { return _callableStatement.getObject( columnName, type ); } + else if ( _brokeredCallableStatement != null ) { return _brokeredCallableStatement.getObject( columnName, type ); } + else if ( _logicalCallableStatement != null ) { return _logicalCallableStatement.getObject( columnName, type ); } + else { throw nothingWrapped(); } + } + + /////////////////////////////////////////////////////////////////////// + // + // OTHER PUBLIC BEHAVIOR + // + /////////////////////////////////////////////////////////////////////// + + public Object getWrappedObject() throws SQLException + { + if ( _embedded != null ) { return _embedded; } + else if ( _netclient != null ) { return _netclient; } + else if ( _embedCallableStatement != null ) { return _embedCallableStatement; } + else if ( _callableStatement != null ) { return _callableStatement; } + else if ( _brokeredCallableStatement != null ) { return _brokeredCallableStatement; } + else if ( _logicalCallableStatement != null ) { return _logicalCallableStatement; } + else { throw nothingWrapped(); } + } + + /////////////////////////////////////////////////////////////////////// + // + // MINIONS + // + /////////////////////////////////////////////////////////////////////// + + private SQLException nothingWrapped() { return new SQLException( "Nothing wrapped!" ); } + +} + Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41.java ------------------------------------------------------------------------------ svn:eol-style = native Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41Test.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41Test.java?rev=1054706&view=auto ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41Test.java (added) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41Test.java Mon Jan 3 18:49:08 2011 @@ -0,0 +1,627 @@ +/* + + Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.Wrapper41Test + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to you under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + */ + +package org.apache.derbyTesting.functionTests.tests.jdbc4; + +import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.CallableStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.NClob; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import junit.framework.*; + +import org.apache.derbyTesting.junit.BaseJDBCTestCase; +import org.apache.derbyTesting.junit.TestConfiguration; + +/** + *

+ * Machinery shared by the JDBC 4.1 tests for ResultSets and CallableStatements. + *

+ */ +public class Wrapper41Test extends BaseJDBCTestCase +{ + /////////////////////////////////////////////////////////////////////// + // + // CONSTANTS + // + /////////////////////////////////////////////////////////////////////// + + private static final String UNSUPPORTED_COERCION = "22005"; + private static final String BAD_FORMAT = "22018"; + private static final String BAD_DATETIME = "22007"; + + private static final String VARIABLE_STRING = "XXXXX"; + + /////////////////////////////////////////////////////////////////////// + // + // STATE + // + /////////////////////////////////////////////////////////////////////// + + private Class byteArrayClass; + + /////////////////////////////////////////////////////////////////////// + // + // CONSTRUCTOR + // + /////////////////////////////////////////////////////////////////////// + + /** + * Create test with given name. + * + * @param name name of the test. + */ + public Wrapper41Test(String name) { super(name); } + + /////////////////////////////////////////////////////////////////////// + // + // BEHAVIOR + // + /////////////////////////////////////////////////////////////////////// + + protected void examineJDBC4_1extensions( Wrapper41 wrapper ) throws Exception + { + println( "Vetting a " + wrapper.getWrappedObject().getClass().getName() ); + + byteArrayClass = Class.forName( "[B" ); + + vetWrappedNull( wrapper ); + vetWrappedInteger( wrapper, 1, "BIGINTCOL" ); + vetWrappedBlob( wrapper ); + vetWrappedBoolean( wrapper ); + vetWrappedString( wrapper, 4, "CHARCOL" ); + vetWrappedBinary( wrapper, 5, "CHARFORBITDATACOL" ); + vetWrappedClob( wrapper ); + vetWrappedDate( wrapper ); + vetWrappedFloatingPoint( wrapper, 8, "DOUBLECOL" ); + vetWrappedFloatingPoint( wrapper, 9, "FLOATCOL" ); + vetWrappedInteger( wrapper, 10, "INTCOL" ); + vetWrappedString( wrapper, 11, "LONGVARCHARCOL" ); + vetWrappedBinary( wrapper, 12, "LONGVARCHARFORBITDATACOL" ); + vetWrappedInteger( wrapper, 13, "NUMERICCOL" ); + vetWrappedFloatingPoint( wrapper, 14, "REALCOL" ); + vetWrappedInteger( wrapper, 15, "SMALLINTCOL" ); + vetWrappedTime( wrapper ); + vetWrappedTimestamp( wrapper ); + vetWrappedString( wrapper, 18, "VARCHARCOL" ); + vetWrappedBinary( wrapper, 19, "VARCHARFORBITDATACOL" ); + } + @SuppressWarnings("unchecked") + private void vetWrappedNull( Wrapper41 wrapper ) throws Exception + { + try { + wrapper.getObject( 1, (Class) null ); + fail( "Did not expect to get a result for a null class type." ); + } + catch (SQLException e) + { + assertSQLState( "Null type", UNSUPPORTED_COERCION, e ); + } + + // String overloads not implemented for CallableStatements + if ( wrapper.getWrappedObject() instanceof CallableStatement ) { return; } + + try { + wrapper.getObject( "BIGINTCOL", (Class) null ); + fail( "Did not expect to get a result for a null class type." ); + } + catch (SQLException e) + { + assertSQLState( "Null type", UNSUPPORTED_COERCION, e ); + } + } + private void vetWrappedInteger( Wrapper41 wrapper, int colID, String colName ) throws Exception + { + vetWrapperOK + ( + wrapper, + colID, + colName, + "1", + new Class[] { String.class, BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, Number.class, Object.class } + ); + vetWrapperOK + ( + wrapper, + colID, + colName, + "1.0", + new Class[] { Float.class, Double.class } + ); + vetWrapperOK + ( + wrapper, + colID, + colName, + "true", + new Class[] { Boolean.class } + ); + + vetNoWrapper + ( + wrapper, + colID, + colName, + new Class[] { Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, byteArrayClass, getClass() } + ); + } + private void vetWrappedBlob( Wrapper41 wrapper ) throws Exception + { + vetWrapperOK + ( + wrapper, + 2, + "BLOBCOL", + "abc", + new Class[] { Blob.class, Object.class, byteArrayClass, String.class, } + ); + + vetNoWrapper + ( + wrapper, + 2, + "BLOBCOL", + new Class[] + { + BigDecimal.class, Boolean.class, + Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, + Date.class, Time.class, Timestamp.class + } + ); + + // + // We don't try to get a Clob value because we have already gotten a LOB value. + // Trying to open another LOB stream raises an error. Using a random class type + // also takes us down that code path, so we don't verify against getClass() either. + // + } + private void vetWrappedBoolean( Wrapper41 wrapper ) throws Exception + { + vetWrapperOK + ( + wrapper, + 3, + "BOOLEANCOL", + "true", + new Class[] { String.class, Boolean.class, Object.class } + ); + vetWrapperOK + ( + wrapper, + 3, + "BOOLEANCOL", + "1", + new Class[] { BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class } + ); + vetWrapperOK + ( + wrapper, + 3, + "BOOLEANCOL", + "1.0", + new Class[] { Float.class, Double.class } + ); + + vetNoWrapper + ( + wrapper, + 3, + "BOOLEANCOL", + new Class[] { Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, byteArrayClass, getClass() } + ); + } + private void vetWrappedString( Wrapper41 wrapper, int colID, String colName ) throws Exception + { + vetWrapperOK + ( + wrapper, + colID, + colName, + "a", + new Class[] { String.class, Object.class } + ); + + vetWrapperOK + ( + wrapper, + colID, + colName, + "true", + new Class[] { Boolean.class } + ); + + vetCoercionError + ( + wrapper, + colID, + colName, + new Class[] + { + BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, + Float.class, Double.class, + }, + BAD_FORMAT + ); + + vetCoercionError + ( + wrapper, + colID, + colName, + new Class[] + { + Date.class, Time.class, Timestamp.class + }, + BAD_DATETIME + ); + + vetNoWrapper + ( + wrapper, + colID, + colName, + new Class[] + { + Blob.class, Clob.class, byteArrayClass, getClass() + } + ); + } + private void vetWrappedBinary( Wrapper41 wrapper, int colID, String colName ) throws Exception + { + vetWrapperOK + ( + wrapper, + colID, + colName, + "de", + new Class[] { String.class } + ); + vetWrapperOK + ( + wrapper, + colID, + colName, + "\ufffd", + new Class[] { byteArrayClass, Object.class } + ); + + vetNoWrapper + ( + wrapper, + colID, + colName, + new Class[] + { + Boolean.class, BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, + Float.class, Double.class, + Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, getClass() + } + ); + } + private void vetWrappedClob( Wrapper41 wrapper ) throws Exception + { + vetWrapperOK + ( + wrapper, + 6, + "CLOBCOL", + "abc", + new Class[] { String.class, Clob.class, Object.class } + ); + + vetNoWrapper + ( + wrapper, + 6, + "CLOBCOL", + new Class[] + { + Boolean.class, + BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, + Float.class, Double.class, + Date.class, Time.class, Timestamp.class, + byteArrayClass + } + ); + + // + // We don't test getting a BLOB because we are only allowed one attempt + // to get a LOB from the column. Using a random class type + // also takes us down that code path, so we don't verify against getClass() either. + // + } + private void vetWrappedDate( Wrapper41 wrapper ) throws Exception + { + vetWrapperOK + ( + wrapper, + 7, + "DATECOL", + "1994-02-23", + new Class[] { String.class, Date.class, Object.class } + ); + vetWrapperOK + ( + wrapper, + 7, + "DATECOL", + "1994-02-23 00:00:00.0", + new Class[] { Timestamp.class } + ); + + vetNoWrapper + ( + wrapper, + 7, + "DATECOL", + new Class[] + { + Boolean.class, + BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, + Float.class, Double.class, + Time.class, + Blob.class, Clob.class, byteArrayClass, getClass() + } + ); + } + private void vetWrappedFloatingPoint( Wrapper41 wrapper, int colID, String colName ) throws Exception + { + vetWrapperOK + ( + wrapper, + colID, + colName, + "1.0", + new Class[] { String.class, Float.class, Double.class, BigDecimal.class, Number.class, Object.class } + ); + vetWrapperOK + ( + wrapper, + colID, + colName, + "1", + new Class[] { Byte.class, Short.class, Integer.class, Long.class } + ); + vetWrapperOK + ( + wrapper, + colID, + colName, + "true", + new Class[] { Boolean.class } + ); + + vetNoWrapper + ( + wrapper, + colID, + colName, + new Class[] { Date.class, Time.class, Timestamp.class, Blob.class, Clob.class, byteArrayClass, getClass() } + ); + } + private void vetWrappedTime( Wrapper41 wrapper ) throws Exception + { + vetWrapperOK + ( + wrapper, + 16, + "TIMECOL", + "15:09:02", + new Class[] { String.class, Time.class, Object.class } + ); + vetWrapperOK + ( + wrapper, + 16, + "TIMECOL", + VARIABLE_STRING, + new Class[] { Timestamp.class } + ); + + vetNoWrapper + ( + wrapper, + 16, + "TIMECOL", + new Class[] + { + Boolean.class, + BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, + Float.class, Double.class, + Date.class, + Blob.class, Clob.class, byteArrayClass, getClass() + } + ); + } + private void vetWrappedTimestamp( Wrapper41 wrapper ) throws Exception + { + vetWrapperOK + ( + wrapper, + 17, + "TIMESTAMPCOL", + "1962-09-23 03:23:34.234", + new Class[] { String.class, Timestamp.class, Object.class } + ); + vetWrapperOK + ( + wrapper, + 17, + "TIMESTAMPCOL", + "03:23:34", + new Class[] { Time.class } + ); + vetWrapperOK + ( + wrapper, + 17, + "TIMESTAMPCOL", + "1962-09-23", + new Class[] { Date.class } + ); + + vetNoWrapper + ( + wrapper, + 17, + "TIMESTAMPCOL", + new Class[] + { + Boolean.class, + BigDecimal.class, Byte.class, Short.class, Integer.class, Long.class, + Float.class, Double.class, + Blob.class, Clob.class, byteArrayClass, getClass() + } + ); + } + + @SuppressWarnings("unchecked") + private void vetWrapperOK + ( Wrapper41 wrapper, int colID, String colName, String expectedValue, Class[] supportedCoercions ) + throws Exception + { + int coercionCount = supportedCoercions.length; + for ( int i = 0; i < coercionCount; i++ ) + { + Class candidate = supportedCoercions[ i ]; + vetCandidate( candidate, expectedValue, wrapper.getObject( colID, candidate ) ); + + // you can only retrieve a LOB once + if ( (candidate == Blob.class) || (candidate == Clob.class) ) { return; } + + // String overloads not implemented for CallableStatement + if ( !(wrapper.getWrappedObject() instanceof CallableStatement) ) + { vetCandidate( candidate, expectedValue, wrapper.getObject( colName, candidate ) ); } + } + } + @SuppressWarnings("unchecked") + private void vetCandidate( Class candidate, String expectedValue, Object actualValue ) + throws Exception + { + assertTrue( candidate.getName(), candidate.isAssignableFrom( actualValue.getClass( ) ) ); + + if ( VARIABLE_STRING.equals( expectedValue ) ) { return; } + + String actualString; + if ( actualValue instanceof Blob ) + { + Blob blob = (Blob) actualValue; + actualString = squeezeString( blob.getBytes( 1L, (int) blob.length() ) ); + } + else if ( actualValue instanceof byte[] ) + { + actualString = squeezeString( (byte[]) actualValue ); + } + else if ( actualValue instanceof Clob ) + { + Clob clob = (Clob) actualValue; + actualString = clob.getSubString( 1L, (int) clob.length() ); + } + else { actualString = actualValue.toString(); } + + assertEquals( candidate.getName(), expectedValue, actualString ); + } + private String squeezeString( byte[] bytes ) throws Exception + { + + String result = new String( bytes, "UTF-8" ); + + return result; + } + private void vetNoWrapper + ( Wrapper41 wrapper, int colID, String colName, Class[] unsupportedCoercions ) + throws Exception + { + vetCoercionError( wrapper, colID, colName, unsupportedCoercions, UNSUPPORTED_COERCION ); + } + @SuppressWarnings("unchecked") + private void vetCoercionError + ( Wrapper41 wrapper, int colID, String colName, Class[] unsupportedCoercions, String expectedSQLState ) + throws Exception + { + int coercionCount = unsupportedCoercions.length; + for ( int i = 0; i < coercionCount; i++ ) + { + Class candidate = unsupportedCoercions[ i ]; + + try { + wrapper.getObject( colID, candidate ); + fail( "Did not expect to get a " + candidate.getName() ); + } + catch (SQLException e) + { + assertSQLState( candidate.getName(), expectedSQLState, e ); + } + + // you can only retrieve a LOB once + if ( (candidate == Blob.class) || (candidate == Clob.class) ) { return; } + + // String overloads not implemented for CallableStatements + if ( wrapper.getWrappedObject() instanceof CallableStatement ) { return; } + + try { + wrapper.getObject( colName, candidate ); + fail( "Did not expect to get a " + candidate.getName() ); + } + catch (SQLException e) + { + assertSQLState( candidate.getName(), expectedSQLState, e ); + } + } + } + + /////////////////////////////////////////////////////////////////////// + // + // MINIONS + // + /////////////////////////////////////////////////////////////////////// + + protected PreparedStatement prepareStatement( Connection conn, String text ) + throws Exception + { + println( text ); + + PreparedStatement ps = conn.prepareStatement( text ); + + return ps; + } + + protected CallableStatement prepareCall( Connection conn, String text ) + throws Exception + { + println( text ); + + CallableStatement cs = conn.prepareCall( text ); + + return cs; + } + +} Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/Wrapper41Test.java ------------------------------------------------------------------------------ svn:eol-style = native