HHH-2412 - Support for JDBC4
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17770 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
cbdce5873a
commit
518cf52dfd
|
@ -30,5 +30,5 @@ package org.hibernate.engine.jdbc;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface NClobImplementer {
|
||||
public interface NClobImplementer extends ClobImplementer {
|
||||
}
|
||||
|
|
|
@ -43,18 +43,6 @@
|
|||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>8.4-701.jdbc4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -70,24 +70,6 @@
|
|||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>8.4-701.jdbc4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.1.0.7.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -21,15 +21,20 @@
|
|||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.engine.jdbc.jdbc4;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.Blob;
|
||||
import java.sql.Clob;
|
||||
import java.sql.NClob;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
@ -50,197 +55,100 @@ import org.hibernate.engine.jdbc.WrappedClob;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public class JdbcSupportTest extends TestCase {
|
||||
private interface Envionment {
|
||||
public String getDriver();
|
||||
public String getUrl();
|
||||
public String getUser();
|
||||
public String getPass();
|
||||
|
||||
public void verifyCreator(LobCreator lobCreator);
|
||||
public void verifyBlob(Blob blob);
|
||||
public void verifyClob(Clob clob);
|
||||
public void verifyNClob(Clob nclob);
|
||||
}
|
||||
|
||||
private abstract class ContextualEnvironment implements Envionment {
|
||||
public void verifyCreator(LobCreator lobCreator) {
|
||||
assertTrue( lobCreator instanceof ContextualLobCreator );
|
||||
}
|
||||
|
||||
public void verifyBlob(Blob blob) {
|
||||
assertFalse( blob instanceof BlobImplementer );
|
||||
}
|
||||
|
||||
public void verifyClob(Clob clob) {
|
||||
assertFalse( clob instanceof ClobImplementer );
|
||||
}
|
||||
|
||||
public void verifyNClob(Clob nclob) {
|
||||
assertFalse( nclob instanceof NClobImplementer );
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class NonContextualEnvironment implements Envionment {
|
||||
public void verifyCreator(LobCreator lobCreator) {
|
||||
assertTrue( lobCreator instanceof NonContextualLobCreator );
|
||||
}
|
||||
|
||||
public void verifyBlob(Blob blob) {
|
||||
assertTrue( blob instanceof BlobImplementer );
|
||||
}
|
||||
|
||||
public void verifyClob(Clob clob) {
|
||||
assertTrue( clob instanceof ClobImplementer );
|
||||
}
|
||||
|
||||
public void verifyNClob(Clob nclob) {
|
||||
assertTrue( nclob instanceof NClobImplementer );
|
||||
}
|
||||
}
|
||||
|
||||
private Envionment POSTGRESQL = new NonContextualEnvironment() {
|
||||
public String getDriver() {
|
||||
return "org.postgresql.Driver";
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return "jdbc:postgresql://vmg03.mw.lab.eng.bos.redhat.com:5432:platformae";
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return "sebersole";
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return "sebersole";
|
||||
}
|
||||
};
|
||||
|
||||
private Envionment MYSQL = new ContextualEnvironment() {
|
||||
public String getDriver() {
|
||||
return "com.mysql.jdbc.Driver";
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return "jdbc:mysql://vmg02.mw.lab.eng.bos.redhat.com/sebersole";
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return "sebersole";
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return "sebersole";
|
||||
}
|
||||
};
|
||||
|
||||
private Envionment ORACLE9i = new ContextualEnvironment() {
|
||||
public String getDriver() {
|
||||
return "oracle.jdbc.driver.OracleDriver";
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return "jdbc:oracle:thin:@dev20.qa.atl.jboss.com:1521:qa";
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return "sebersole";
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return "sebersole";
|
||||
}
|
||||
};
|
||||
|
||||
private Envionment ORACLE10g = new ContextualEnvironment() {
|
||||
public String getDriver() {
|
||||
return "oracle.jdbc.driver.OracleDriver";
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return "jdbc:oracle:thin:@vmg05.mw.lab.eng.bos.redhat.com:1521:qaora10";
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return "sebersole";
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return "sebersole";
|
||||
}
|
||||
};
|
||||
|
||||
private Envionment ORACLE11g = new ContextualEnvironment() {
|
||||
public String getDriver() {
|
||||
return "oracle.jdbc.driver.OracleDriver";
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return "jdbc:oracle:thin:@dev04.qa.atl2.redhat.com:1521:qaora11";
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return "sebersole";
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return "sebersole";
|
||||
}
|
||||
};
|
||||
|
||||
private Envionment ORACLE_RAC = new ContextualEnvironment() {
|
||||
public String getDriver() {
|
||||
return "oracle.jdbc.driver.OracleDriver";
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=vmg24-vip.mw.lab.eng.bos.redhat.com)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=vmg25-vip.mw.lab.eng.bos.redhat.com)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=qarac.jboss))";
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return "sebersole";
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return "sebersole";
|
||||
}
|
||||
};
|
||||
|
||||
private Envionment envionment = ORACLE11g;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Class.forName( envionment.getDriver() );
|
||||
}
|
||||
|
||||
public void testConnectedLobCreator() throws SQLException {
|
||||
final Connection connection = DriverManager.getConnection( envionment.getUrl(), envionment.getUser(), envionment.getPass() );
|
||||
final Connection connection = createConnectionProxy(
|
||||
new JdbcLobBuilder() {
|
||||
public Blob createBlob() {
|
||||
return new JdbcBlob();
|
||||
}
|
||||
|
||||
public Clob createClob() {
|
||||
return new JdbcClob();
|
||||
}
|
||||
|
||||
public NClob createNClob() {
|
||||
return new JdbcNClob();
|
||||
}
|
||||
}
|
||||
);
|
||||
final LobCreationContext lobCreationContext = new LobCreationContext() {
|
||||
public Object execute(Callback callback) {
|
||||
try {
|
||||
return callback.executeOnConnection( connection );
|
||||
}
|
||||
catch ( SQLException e ) {
|
||||
throw new RuntimeException( "Unexpected sql exception", e );
|
||||
throw new RuntimeException( "Unexpected SQLException", e );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
LobCreator lobCreator = JdbcSupportLoader.loadJdbcSupport( connection ).getLobCreator( lobCreationContext );
|
||||
envionment.verifyCreator( lobCreator );
|
||||
assertTrue( lobCreator instanceof ContextualLobCreator );
|
||||
|
||||
Blob blob = lobCreator.createBlob( new byte[] {} );
|
||||
envionment.verifyBlob( blob );
|
||||
assertTrue( blob instanceof JdbcBlob );
|
||||
blob = lobCreator.wrap( blob );
|
||||
assertTrue( blob instanceof WrappedBlob );
|
||||
|
||||
Clob clob = lobCreator.createClob( "Hi" );
|
||||
envionment.verifyClob( clob );
|
||||
assertTrue( clob instanceof JdbcClob );
|
||||
clob = lobCreator.wrap( clob );
|
||||
assertTrue( clob instanceof WrappedClob );
|
||||
|
||||
Clob nclob = lobCreator.createNClob( "Hi" );
|
||||
envionment.verifyNClob( nclob );
|
||||
assertTrue( NClob.class.isInstance( nclob ) );
|
||||
assertTrue( nclob instanceof JdbcNClob );
|
||||
nclob = lobCreator.wrap( nclob );
|
||||
assertTrue( nclob instanceof WrappedClob );
|
||||
|
||||
blob.free();
|
||||
clob.free();
|
||||
nclob.free();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
public void testConnectedLobCreatorWithUnSupportedCreations() throws SQLException {
|
||||
final Connection connection = createConnectionProxy(
|
||||
new JdbcLobBuilder() {
|
||||
public Blob createBlob() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Clob createClob() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public NClob createNClob() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
);
|
||||
final LobCreationContext lobCreationContext = new LobCreationContext() {
|
||||
public Object execute(Callback callback) {
|
||||
try {
|
||||
return callback.executeOnConnection( connection );
|
||||
}
|
||||
catch ( SQLException e ) {
|
||||
throw new RuntimeException( "Unexpected SQLException", e );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
LobCreator lobCreator = JdbcSupportLoader.loadJdbcSupport( connection ).getLobCreator( lobCreationContext );
|
||||
assertTrue( lobCreator instanceof NonContextualLobCreator );
|
||||
|
||||
Blob blob = lobCreator.createBlob( new byte[] {} );
|
||||
assertTrue( blob instanceof BlobImplementer );
|
||||
blob = lobCreator.wrap( blob );
|
||||
assertTrue( blob instanceof WrappedBlob );
|
||||
|
||||
Clob clob = lobCreator.createClob( "Hi" );
|
||||
assertTrue( clob instanceof ClobImplementer );
|
||||
clob = lobCreator.wrap( clob );
|
||||
assertTrue( clob instanceof WrappedClob );
|
||||
|
||||
Clob nclob = lobCreator.createNClob( "Hi" );
|
||||
assertTrue( nclob instanceof ClobImplementer );
|
||||
assertTrue( nclob instanceof NClobImplementer );
|
||||
nclob = lobCreator.wrap( nclob );
|
||||
assertTrue( nclob instanceof WrappedClob );
|
||||
|
||||
|
@ -273,4 +181,141 @@ public class JdbcSupportTest extends TestCase {
|
|||
clob.free();
|
||||
nclob.free();
|
||||
}
|
||||
|
||||
private interface JdbcLobBuilder {
|
||||
public Blob createBlob();
|
||||
public Clob createClob();
|
||||
public NClob createNClob();
|
||||
}
|
||||
|
||||
private class ConnectionProxyHandler implements InvocationHandler {
|
||||
private final JdbcLobBuilder lobBuilder;
|
||||
|
||||
private ConnectionProxyHandler(JdbcLobBuilder lobBuilder) {
|
||||
this.lobBuilder = lobBuilder;
|
||||
}
|
||||
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
// the only methods we are interested in are the LOB creation methods...
|
||||
if ( args == null || args.length == 0 ) {
|
||||
final String methodName = method.getName();
|
||||
if ( "createBlob".equals( methodName ) ) {
|
||||
return lobBuilder.createBlob();
|
||||
}
|
||||
else if ( "createClob".equals( methodName ) ) {
|
||||
return lobBuilder.createClob();
|
||||
}
|
||||
else if ( "createNClob".equals( methodName ) ) {
|
||||
return lobBuilder.createNClob();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Class[] PROXY_TYPES = new Class[] { Connection.class };
|
||||
|
||||
private Connection createConnectionProxy(JdbcLobBuilder jdbcLobBuilder) {
|
||||
ConnectionProxyHandler handler = new ConnectionProxyHandler( jdbcLobBuilder );
|
||||
return ( Connection ) Proxy.newProxyInstance( getClass().getClassLoader(), PROXY_TYPES, handler );
|
||||
}
|
||||
|
||||
private class JdbcBlob implements Blob {
|
||||
public long length() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public byte[] getBytes(long pos, int length) throws SQLException {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
public InputStream getBinaryStream() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public long position(byte[] pattern, long start) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long position(Blob pattern, long start) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int setBytes(long pos, byte[] bytes) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public OutputStream setBinaryStream(long pos) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void truncate(long len) throws SQLException {
|
||||
}
|
||||
|
||||
public void free() throws SQLException {
|
||||
}
|
||||
|
||||
public InputStream getBinaryStream(long pos, long length) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class JdbcClob implements Clob {
|
||||
public long length() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getSubString(long pos, int length) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Reader getCharacterStream() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public InputStream getAsciiStream() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public long position(String searchstr, long start) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long position(Clob searchstr, long start) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int setString(long pos, String str) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int setString(long pos, String str, int offset, int len) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public OutputStream setAsciiStream(long pos) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Writer setCharacterStream(long pos) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void truncate(long len) throws SQLException {
|
||||
}
|
||||
|
||||
public void free() throws SQLException {
|
||||
}
|
||||
|
||||
public Reader getCharacterStream(long pos, long length) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class JdbcNClob extends JdbcClob implements NClob {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue