cleaned up JdbcMocks in test sources and moved it to hibernate-testing

This commit is contained in:
Steve Ebersole 2016-01-23 12:55:17 -06:00
parent a5271d6e76
commit 039382e8be
3 changed files with 339 additions and 213 deletions

View File

@ -1,209 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.boot.database.qualfiedTableNaming;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
/**
* @author Steve Ebersole
*/
public class JdbcMocks {
public static Connection createConnection(String databaseName, int majorVersion) {
return createConnection( databaseName, majorVersion, -9999 );
}
public static Connection createConnection(String databaseName, int majorVersion, int minorVersion) {
DatabaseMetaDataHandler metadataHandler = new DatabaseMetaDataHandler( databaseName, majorVersion, minorVersion );
ConnectionHandler connectionHandler = new ConnectionHandler();
DatabaseMetaData metadataProxy = ( DatabaseMetaData ) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class[] {DatabaseMetaData.class},
metadataHandler
);
Connection connectionProxy = ( Connection ) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class[] { Connection.class },
connectionHandler
);
metadataHandler.setConnectionProxy( connectionProxy );
connectionHandler.setMetadataProxy( metadataProxy );
return connectionProxy;
}
private static class ConnectionHandler implements InvocationHandler {
private DatabaseMetaData metadataProxy;
public void setMetadataProxy(DatabaseMetaData metadataProxy) {
this.metadataProxy = metadataProxy;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if ( "getMetaData".equals( methodName ) ) {
return metadataProxy;
}
if ( "toString".equals( methodName ) ) {
return "Connection proxy [@" + hashCode() + "]";
}
if ( "hashCode".equals( methodName ) ) {
return Integer.valueOf( this.hashCode() );
}
if ( "getCatalog".equals( methodName ) ) {
return "DB1";
}
if ( "supportsRefCursors".equals( methodName ) ) {
return false;
}
if ( canThrowSQLException( method ) ) {
throw new SQLException();
}
else {
throw new UnsupportedOperationException();
}
}
}
private static class DatabaseMetaDataHandler implements InvocationHandler {
private final String databaseName;
private final int majorVersion;
private final int minorVersion;
private Connection connectionProxy;
public void setConnectionProxy(Connection connectionProxy) {
this.connectionProxy = connectionProxy;
}
private DatabaseMetaDataHandler(String databaseName, int majorVersion) {
this( databaseName, majorVersion, -9999 );
}
private DatabaseMetaDataHandler(String databaseName, int majorVersion, int minorVersion) {
this.databaseName = databaseName;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if ( "getDatabaseProductName".equals( methodName ) ) {
return databaseName;
}
if ( "getDatabaseMajorVersion".equals( methodName ) ) {
return Integer.valueOf( majorVersion );
}
if ( "getDatabaseMinorVersion".equals( methodName ) ) {
return Integer.valueOf( minorVersion );
}
if ( "getConnection".equals( methodName ) ) {
return connectionProxy;
}
if ( "toString".equals( methodName ) ) {
return "DatabaseMetaData proxy [db-name=" + databaseName + ", version=" + majorVersion + "]";
}
if ( "hashCode".equals( methodName ) ) {
return Integer.valueOf( this.hashCode() );
}
if ( "supportsNamedParameters".equals( methodName ) ) {
return true ;
}
if ( "supportsResultSetType".equals( methodName ) ) {
return true ;
}
if ( "supportsGetGeneratedKeys".equals( methodName ) ) {
return true ;
}
if ( "supportsBatchUpdates".equals( methodName ) ) {
return true ;
}
if ( "dataDefinitionIgnoredInTransactions".equals( methodName ) ) {
return false ;
}
if ( "dataDefinitionCausesTransactionCommit".equals( methodName ) ) {
return false ;
}
if ( "getSQLKeywords".equals( methodName ) ) {
return "after,ansi,append,attach,audit,before,bitmap,boolean,buffered,byte,cache,call,cluster,clustersize,codeset,database,datafiles,dataskip,datetime,dba,dbdate,dbmoney,debug,define,delimiter,deluxe,detach,dirty,distributions,document,each,elif,exclusive,exit,explain,express,expression,extend,extent,file,fillfactor,foreach,format,fraction,fragment,gk,hash,high,hold,hybrid,if,index,init,labeleq,labelge,labelgt,labelle,labellt,let,listing,lock,log,low,matches,maxerrors,medium,mode,modify,money,mounting,new,nvarchar,off,old,operational,optical,optimization,page,pdqpriority,pload,private,raise,range,raw,recordend,recover,referencing,rejectfile,release,remainder,rename,reserve,resolution,resource,resume,return,returning,returns,ridlist,robin,rollforward,round,row,rowids,sameas,samples,schedule,scratch,serial,share,skall,skinhibit,skshow,smallfloat,stability,standard,start,static,statistics,stdev,step,sync,synonym,system,temp,text,timeout,trace,trigger,units,unlock,variance,wait,while,xload,xunload" ;
}
if ( "getSQLStateType".equals( methodName ) ) {
return DatabaseMetaData.sqlStateXOpen ;
}
if ( "locatorsUpdateCopy".equals( methodName ) ) {
return false ;
}
if ( "getTypeInfo".equals( methodName ) ) {
com.sun.rowset.CachedRowSetImpl rowSet = new com.sun.rowset.CachedRowSetImpl();
return rowSet ;
}
if ( "storesLowerCaseIdentifiers".equals( methodName ) ) {
return true ;
}
if ( "storesUpperCaseIdentifiers".equals( methodName ) ) {
return false ;
}
if ( "getCatalogSeparator".equals( methodName ) ) {
return ":" ;
}
if ( "isCatalogAtStart".equals( methodName ) ) {
return true ;
}
if ( canThrowSQLException( method ) ) {
throw new SQLException();
}
else {
throw new UnsupportedOperationException();
}
}
}
private static boolean canThrowSQLException(Method method) {
final Class[] exceptions = method.getExceptionTypes();
for ( Class exceptionType : exceptions ) {
if ( SQLException.class.isAssignableFrom( exceptionType ) ) {
return true;
}
}
return false;
}
}

View File

@ -18,17 +18,14 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport; import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.mapping.Table;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister; import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.hibernate.tool.schema.internal.StandardTableExporter;
import org.hibernate.testing.jdbc.JdbcMocks;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/** /**
* @author Steve Ebersole * @author Steve Ebersole

View File

@ -0,0 +1,338 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.testing.jdbc;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.rowset.RowSetProvider;
import org.hibernate.engine.jdbc.env.spi.AnsiSqlKeywords;
import org.hibernate.internal.util.StringHelper;
/**
* @author Steve Ebersole
*/
@SuppressWarnings({"UnnecessaryBoxing", "unused"})
public class JdbcMocks {
public static class Options {
private String databaseProductName;
private int databaseMajorVersion = -9999;
private int databaseMinorVersion = -9999;
private String catalogName = "db1";
private boolean supportsRefCursors = false;
private boolean supportsNamedParameters = true;
private boolean supportsResultSetType = true;
private boolean supportsGetGeneratedKeys = true;
private boolean supportsBatchUpdates = true;
private boolean dataDefinitionIgnoredInTransactions = false;
private boolean dataDefinitionCausesTransactionCommit = false;
private String sqlKeywords = StringHelper.join( ",", AnsiSqlKeywords.INSTANCE.sql2003() );
private int sqlStateType = DatabaseMetaData.sqlStateXOpen;
private boolean locatorsUpdateCopy = false;
private boolean storesLowerCaseIdentifiers = true;
private boolean storesUpperCaseIdentifiers = false;
private String catalogSeparator = ":";
private boolean isCatalogAtStart = true;
}
public static class ConnectionBuilder {
private final Options options;
public ConnectionBuilder() {
this( new Options() );
}
public ConnectionBuilder(Options options) {
this.options = options;
}
public Connection buildConnection() {
ConnectionHandler connectionHandler = new ConnectionHandler( options );
return (Connection) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class[] { Connection.class },
connectionHandler
);
}
public ConnectionBuilder setDatabaseProductName(String databaseProductName) {
this.options.databaseProductName = databaseProductName;
return this;
}
public ConnectionBuilder setDatabaseMajorVersion(int databaseMajorVersion) {
this.options.databaseMajorVersion = databaseMajorVersion;
return this;
}
public ConnectionBuilder setDatabaseMinorVersion(int databaseMinorVersion) {
this.options.databaseMinorVersion = databaseMinorVersion;
return this;
}
public ConnectionBuilder setCatalogName(String catalogName) {
this.options.catalogName = catalogName;
return this;
}
public ConnectionBuilder setSupportsRefCursors(boolean supportsRefCursors) {
this.options.supportsRefCursors = supportsRefCursors;
return this;
}
public ConnectionBuilder setSupportsNamedParameters(boolean supportsNamedParameters) {
this.options.supportsNamedParameters = supportsNamedParameters;
return this;
}
public ConnectionBuilder setSupportsResultSetType(boolean supportsResultSetType) {
this.options.supportsResultSetType = supportsResultSetType;
return this;
}
public ConnectionBuilder setSupportsGetGeneratedKeys(boolean supportsGetGeneratedKeys) {
this.options.supportsGetGeneratedKeys = supportsGetGeneratedKeys;
return this;
}
public ConnectionBuilder setSupportsBatchUpdates(boolean supportsBatchUpdates) {
this.options.supportsBatchUpdates = supportsBatchUpdates;
return this;
}
public ConnectionBuilder setDataDefinitionIgnoredInTransactions(boolean dataDefinitionIgnoredInTransactions) {
this.options.dataDefinitionIgnoredInTransactions = dataDefinitionIgnoredInTransactions;
return this;
}
public ConnectionBuilder setDataDefinitionCausesTransactionCommit(boolean dataDefinitionCausesTransactionCommit) {
this.options.dataDefinitionCausesTransactionCommit = dataDefinitionCausesTransactionCommit;
return this;
}
public ConnectionBuilder setSqlKeywords(String sqlKeywords) {
this.options.sqlKeywords = sqlKeywords;
return this;
}
public ConnectionBuilder setSqlStateType(int sqlStateType) {
this.options.sqlStateType = sqlStateType;
return this;
}
public ConnectionBuilder setLocatorsUpdateCopy(boolean locatorsUpdateCopy) {
this.options.locatorsUpdateCopy = locatorsUpdateCopy;
return this;
}
public ConnectionBuilder setStoresLowerCaseIdentifiers(boolean storesLowerCaseIdentifiers) {
this.options.storesLowerCaseIdentifiers = storesLowerCaseIdentifiers;
return this;
}
public ConnectionBuilder setStoresUpperCaseIdentifiers(boolean storesUpperCaseIdentifiers) {
this.options.storesUpperCaseIdentifiers = storesUpperCaseIdentifiers;
return this;
}
public ConnectionBuilder setCatalogSeparator(String catalogSeparator) {
this.options.catalogSeparator = catalogSeparator;
return this;
}
public ConnectionBuilder setCatalogAtStart(boolean catalogAtStart) {
this.options.isCatalogAtStart = catalogAtStart;
return this;
}
}
public static Connection createConnection(String databaseName, int majorVersion) {
return new ConnectionBuilder()
.setDatabaseProductName( databaseName )
.setDatabaseMajorVersion( majorVersion )
.buildConnection();
}
public static Connection createConnection(String databaseName, int majorVersion, int minorVersion) {
return new ConnectionBuilder()
.setDatabaseProductName( databaseName )
.setDatabaseMajorVersion( majorVersion )
.setDatabaseMinorVersion( minorVersion )
.buildConnection();
}
private static class ConnectionHandler implements InvocationHandler {
private final Options options;
private DatabaseMetaData metadataProxy;
public ConnectionHandler(Options options) {
this.options = options;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if ( "getMetaData".equals( methodName ) ) {
return getMetadataProxy( (Connection) proxy );
}
if ( "toString".equals( methodName ) ) {
return "Connection proxy [@" + hashCode() + "]";
}
if ( "hashCode".equals( methodName ) ) {
return Integer.valueOf( this.hashCode() );
}
if ( "getCatalog".equals( methodName ) ) {
return options.catalogName;
}
if ( "supportsRefCursors".equals( methodName ) ) {
return options.supportsRefCursors;
}
if ( canThrowSQLException( method ) ) {
throw new SQLException();
}
else {
throw new UnsupportedOperationException();
}
}
private DatabaseMetaData getMetadataProxy(Connection connectionProxy) {
if ( metadataProxy == null ) {
// we need to make it
final DatabaseMetaDataHandler metadataHandler = new DatabaseMetaDataHandler( options, connectionProxy );
metadataProxy = (DatabaseMetaData) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class[] {DatabaseMetaData.class},
metadataHandler
);
}
return metadataProxy;
}
}
private static class DatabaseMetaDataHandler implements InvocationHandler {
private final Options options;
private final Connection connectionProxy;
public DatabaseMetaDataHandler(Options options, Connection connectionProxy) {
this.options = options;
this.connectionProxy = connectionProxy;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if ( "getDatabaseProductName".equals( methodName ) ) {
return options.databaseProductName;
}
if ( "getDatabaseMajorVersion".equals( methodName ) ) {
return Integer.valueOf( options.databaseMajorVersion );
}
if ( "getDatabaseMinorVersion".equals( methodName ) ) {
return Integer.valueOf( options.databaseMinorVersion );
}
if ( "getConnection".equals( methodName ) ) {
return connectionProxy;
}
if ( "toString".equals( methodName ) ) {
return "DatabaseMetaData proxy [db-name=" + options.databaseProductName + ", version=" + options.databaseMajorVersion + "]";
}
if ( "hashCode".equals( methodName ) ) {
return Integer.valueOf( this.hashCode() );
}
if ( "supportsNamedParameters".equals( methodName ) ) {
return options.supportsNamedParameters;
}
if ( "supportsResultSetType".equals( methodName ) ) {
return options.supportsResultSetType;
}
if ( "supportsGetGeneratedKeys".equals( methodName ) ) {
return options.supportsGetGeneratedKeys;
}
if ( "supportsBatchUpdates".equals( methodName ) ) {
return options.supportsBatchUpdates;
}
if ( "dataDefinitionIgnoredInTransactions".equals( methodName ) ) {
return options.dataDefinitionIgnoredInTransactions;
}
if ( "dataDefinitionCausesTransactionCommit".equals( methodName ) ) {
return options.dataDefinitionCausesTransactionCommit;
}
if ( "getSQLKeywords".equals( methodName ) ) {
return options.sqlKeywords;
}
if ( "getSQLStateType".equals( methodName ) ) {
return options.sqlStateType;
}
if ( "locatorsUpdateCopy".equals( methodName ) ) {
return options.locatorsUpdateCopy;
}
if ( "getTypeInfo".equals( methodName ) ) {
return RowSetProvider.newFactory().createCachedRowSet();
}
if ( "storesLowerCaseIdentifiers".equals( methodName ) ) {
return options.storesLowerCaseIdentifiers;
}
if ( "storesUpperCaseIdentifiers".equals( methodName ) ) {
return options.storesUpperCaseIdentifiers;
}
if ( "getCatalogSeparator".equals( methodName ) ) {
return options.catalogSeparator;
}
if ( "isCatalogAtStart".equals( methodName ) ) {
return options.isCatalogAtStart;
}
if ( canThrowSQLException( method ) ) {
throw new SQLException();
}
else {
throw new UnsupportedOperationException();
}
}
}
private static boolean canThrowSQLException(Method method) {
final Class[] exceptions = method.getExceptionTypes();
for ( Class exceptionType : exceptions ) {
if ( SQLException.class.isAssignableFrom( exceptionType ) ) {
return true;
}
}
return false;
}
}