PooledDatasourceHelper { public static DataSource convert( IDatabaseConnection databaseConnection ) throws DBDatasourceServiceException { return convert( databaseConnection, () -> PentahoSystem.get( IDatabaseDialectService.class, PentahoSessionHolder.getSession() ) ); }  static PoolingDataSource setupPooledDataSource( IDatabaseConnection databaseConnection ); static DataSource convert( IDatabaseConnection databaseConnection ); static DataSource getJndiDataSource( final String dsName );  }
@Test public void testCreateDatasourceNoDialect() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.postgresql.Driver" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql: con.setAttributes( attrs ); try { PooledDatasourceHelper.convert( con, () -> dialectService ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
@Test public void testCreateDatasourceNoClassName() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setDatabaseType( databaseTypeHelper.getDatabaseTypeByShortName( "GENERIC" ) ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql: con.setAttributes( attrs ); try { PooledDatasourceHelper.convert( con, () -> dialectService ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
@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 testSuccessfulDriverInitInConvertNonGeneric() throws DBDatasourceServiceException { when( ( (IDriverLocator) driverLocatorDialect ).initialize( nativeDriverName ) ).thenReturn( true ); PooledDatasourceHelper.convert( connection, () -> dialectService ); verify( ( (IDriverLocator) driverLocatorDialect ), times( 1 ) ).initialize( 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" ) ); }
@Test public void testDialectWithoutLocatorAndDriverNotPresent() throws DBDatasourceServiceException { when( dialectService.getDialect( connection ) ).thenReturn( plainDialect ); try { PooledDatasourceHelper.convert( connection, () -> dialectService ); fail( "Expected exception, driver class should not be present." ); } catch ( Exception e ) { assertThat( e, instanceOf( DBDatasourceServiceException.class ) ); assertThat( e.getCause().getMessage(), containsString( nativeDriverName ) ); } }
@Test public void testDialectWithNoDriverSpecified() throws DBDatasourceServiceException { when( dialectService.getDialect( connection ) ).thenReturn( driverLocatorDialect ); when( driverLocatorDialect.getNativeDriver() ).thenReturn( "" ); try { PooledDatasourceHelper.convert( connection, () -> dialectService ); fail( "Expected exception, driver class not specified in dialect." ); } catch ( Exception e ) { assertThat( e, instanceOf( DBDatasourceServiceException.class ) ); } }
@Test public void testNoDialectService() throws DBDatasourceServiceException { try { PooledDatasourceHelper.convert( connection, () -> null ); fail( "Expected an exception. No dialect service." ); } catch ( Exception e ) { assertThat( e, instanceOf( DBDatasourceServiceException.class ) ); } }
@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 ); }