@Test public void testSuccessfulDriverInitInConvertNonGeneric() throws DBDatasourceServiceException { when( ( (IDriverLocator) driverLocatorDialect ).initialize( nativeDriverName ) ).thenReturn( true ); PooledDatasourceHelper.convert( connection, () -> dialectService ); verify( ( (IDriverLocator) driverLocatorDialect ), times( 1 ) ).initialize( nativeDriverName ); }
@Test( expected = DriverNotInitializedException.class ) public void testDriverNotInitialized() throws DBDatasourceServiceException { when( dialectService.getDialect( connection ) ).thenReturn( driverLocatorDialect ); when( ( (IDriverLocator) driverLocatorDialect ).initialize( nativeDriverName ) ).thenReturn( false ); PooledDatasourceHelper.convert( connection, () -> dialectService ); }
@Test public void testThatFailedDriverInitThrowsInConvert() throws DBDatasourceServiceException { when( ( (IDriverLocator) driverLocatorDialect ).initialize( nativeDriverName ) ).thenReturn( false ); try { PooledDatasourceHelper.convert( connection, () -> dialectService ); fail( "Expected exception" ); } catch ( Exception e ) { assertThat( e.getMessage(), containsString( nativeDriverName ) ); } }
@Test public void testSuccessfulDriverInitInConvertGeneric() throws DBDatasourceServiceException { when( databaseType.getShortName() ).thenReturn( "GENERIC" ); when( connection.getAttributes() ).thenReturn( ImmutableMap.of( GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, nativeDriverName ) ); when( connection.getUsername() ).thenReturn( "suzy" ); when( connection.getPassword() ).thenReturn( "password" ); when( ( (IDriverLocator) driverLocatorDialect ).initialize( nativeDriverName ) ).thenReturn( true ); DriverManagerDataSource dataSource = (DriverManagerDataSource) PooledDatasourceHelper.convert( connection, () -> dialectService ); verify( ( (IDriverLocator) driverLocatorDialect ), times( 1 ) ).initialize( nativeDriverName ); assertThat( dataSource.getUrl(), is( jdbcUrl ) ); assertThat( dataSource.getUsername(), is( "suzy" ) ); assertThat( dataSource.getPassword(), is( "password" ) ); }
if ( !( (IDriverLocator) dialect ).initialize( driverClass ) ) { throw new DriverNotInitializedException( Messages.getInstance() .getErrorString( "PooledDatasourceHelper.ERROR_0009_UNABLE_TO_POOL_DATASOURCE_CANT_INITIALIZE",
/** * For dialects which implement IDriverLocator, this method will use the provided * initialize() implementation. For all others, will initialize drivers via the call to * {@link DriverManagerDataSource#setDriverClassName(String)} (which internally uses Class.forName()) * @throws DBDatasourceServiceException */ private static void initDriverClass( DriverManagerDataSource driverManagerDataSource, IDatabaseDialect dialect, String driverClassName, String databaseConnectionName ) throws DBDatasourceServiceException { if ( dialect instanceof IDriverLocator ) { if ( !( (IDriverLocator) dialect ).initialize( driverClassName ) ) { throw new DriverNotInitializedException( Messages.getInstance() .getErrorString( "PooledDatasourceHelper.ERROR_0009_UNABLE_TO_POOL_DATASOURCE_CANT_INITIALIZE", databaseConnectionName, driverClassName ) ); } return; } try { driverManagerDataSource.setDriverClassName( driverClassName ); } catch ( Throwable th ) { throw new DBDatasourceServiceException( Messages.getInstance().getErrorString( "PooledDatasourceHelper.ERROR_0002_DATASOURCE_CREATE_ERROR_NO_CLASSNAME", databaseConnectionName ), th ); } }