PentahoPmdConnectionProvider extends PmdConnectionProvider { public Connection createConnection( final DatabaseMeta databaseMeta, final String username, final String password ) throws ReportDataFactoryException { try { final String realUser = ( databaseMeta.getUsername() == null ) ? username : databaseMeta.getUsername(); final String realPassword = ( databaseMeta.getPassword() == null ) ? password : databaseMeta.getPassword(); if ( databaseMeta.getAccessType() == DatabaseMeta.TYPE_ACCESS_JNDI ) { final String jndiName = databaseMeta.getDatabaseName(); if ( jndiName != null ) { try { final PentahoJndiDatasourceConnectionProvider connectionProvider = getJndiProvider(); connectionProvider.setJndiName( jndiName ); return connectionProvider.createConnection( realUser, realPassword ); } catch ( Exception e ) { CommonUtil.checkStyleIgnore(); } } } if ( databaseMeta.isUsingConnectionPool() ) { final String name = databaseMeta.getName(); if ( name != null ) { try { final PentahoPoolDataSourceConnectionProvider connectionProvider = getPoolProvider(); connectionProvider.setName( name ); return connectionProvider.createConnection( realUser, realPassword ); } catch ( Exception e ) { CommonUtil.checkStyleIgnore(); } } } } catch ( Exception e ) { throw new ReportDataFactoryException( Messages.getInstance().getString( "ReportPlugin.unableToCreateConnection" ), e ); } return super.createConnection( databaseMeta, username, password ); } PentahoPmdConnectionProvider(); IMetadataDomainRepository getMetadataDomainRepository( final String domain,
      final ResourceManager resourceManager, final ResourceKey contextKey, final String xmiFile ); Connection createConnection( final DatabaseMeta databaseMeta, final String username, final String password );  }
@Test public void testCreateConnection() throws SQLException { PentahoJndiDatasourceConnectionProvider jndicp = Mockito.mock( PentahoJndiDatasourceConnectionProvider.class ); PentahoPoolDataSourceConnectionProvider poolcp = Mockito.mock( PentahoPoolDataSourceConnectionProvider.class ); PentahoPmdConnectionProvider ppcp = Mockito.spy( new PentahoPmdConnectionProvider() ); when( ppcp.getJndiProvider() ).thenReturn( jndicp ); when( ppcp.getPoolProvider() ).thenReturn( poolcp ); when( jndicp.createConnection( any(), any() ) ).thenReturn( (Connection) Mockito.mock( Connection.class ) ); when( poolcp.createConnection( any(), any() ) ).thenReturn( (Connection) Mockito.mock( Connection.class ) ); String username = "user"; String password = "pass"; DatabaseMeta databaseMeta = mock( DatabaseMeta.class ); when( databaseMeta.getAccessType() ).thenReturn( DatabaseMeta.TYPE_ACCESS_JNDI ); when( databaseMeta.getDatabaseName() ).thenReturn( "test" ); when( databaseMeta.isUsingConnectionPool() ).thenReturn( false ); try { Connection conn = ppcp.createConnection( databaseMeta, username, password ); Assert.assertFalse( "JNDI connection", conn == null ); } catch ( ReportDataFactoryException e ) { Assert.fail(); } when( databaseMeta.getAccessType() ).thenReturn( DatabaseMeta.TYPE_ACCESS_NATIVE ); when( databaseMeta.getName() ).thenReturn( "test" ); when( databaseMeta.isUsingConnectionPool() ).thenReturn( true ); try { Connection conn = ppcp.createConnection( databaseMeta, username, password ); Assert.assertFalse( "Pool connection", conn == null ); } catch ( ReportDataFactoryException e ) { Assert.fail(); } }