NonPooledOrJndiDatasourceService extends BaseDatasourceService { @Override protected DataSource retrieve( String dsName ) throws DBDatasourceServiceException { DataSource ds = null; requestedDatasourceName = dsName; try { IDatasourceMgmtService datasourceMgmtSvc = getDatasourceMgmtService(); IDatabaseConnection databaseConnection = datasourceMgmtSvc.getDatasourceByName( dsName ); if ( databaseConnection != null && !databaseConnection.getAccessType().equals( DatabaseAccessType.JNDI ) ) { ds = resolveDatabaseConnection( databaseConnection ); } else { try { ds = getJndiDataSource( dsName ); } catch ( DBDatasourceServiceException e ) { } } if ( ds == null && databaseConnection != null ) { ds = getJndiDataSource( databaseConnection.getDatabaseName() ); } if ( ds != null ) { cacheManager.putInRegionCache( IDBDatasourceService.JDBC_DATASOURCE, dsName, ds ); } } catch ( DatasourceMgmtServiceException daoe ) { log.debug( Messages.getInstance().getErrorString( "DatasourceService.DEBUG_0001_UNABLE_TO_FIND_DATASOURCE_IN_REPOSITORY", daoe.getLocalizedMessage() ), daoe ); try { return getJndiDataSource( dsName ); } catch ( DBDatasourceServiceException dse ) { throw new DBDatasourceServiceException( Messages.getInstance().getErrorString( "DatasourceService.ERROR_0003_UNABLE_TO_GET_JNDI_DATASOURCE" ), dse ); } } return ds; }    }
@Test public void testRetrieveJNDIConnection_1() throws Exception { when( mgmtService.getDatasourceByName( testName ) ).thenReturn( connection ); when( connection.getAccessType() ).thenReturn( DatabaseAccessType.JNDI ); service.retrieve( testName ); verify( service ).getJndiDataSource( testName ); verify( cacheManager ).putInRegionCache( IDBDatasourceService.JDBC_DATASOURCE, testName, jndiDataSource ); }
@Test public void testRetrieveJNDIConnection_2() throws Exception { when( mgmtService.getDatasourceByName( testName ) ).thenReturn( null ); service.retrieve( testName ); verify( service ).getJndiDataSource( testName ); verify( cacheManager ).putInRegionCache( IDBDatasourceService.JDBC_DATASOURCE, testName, jndiDataSource ); }
@Test public void testRetrieveJNDIConnection_3() throws Exception { service = spy( getPreparedService( mgmtService, cacheManager, null, null ) ); when( mgmtService.getDatasourceByName( testName ) ).thenReturn( connection ); when( connection.getAccessType() ).thenReturn( DatabaseAccessType.JNDI ); service.retrieve( testName ); verify( service, times( 2 ) ).getJndiDataSource( anyString() ); verify( cacheManager, never() ).putInRegionCache( IDBDatasourceService.JDBC_DATASOURCE, testName, jndiDataSource ); }
@Test public void testRetrieveJNDIConnection_4() throws Exception { service = spy( getPreparedService( mgmtService, cacheManager, null, null ) ); when( mgmtService.getDatasourceByName( testName ) ).thenReturn( connection ); when( service.getJndiDataSource( testName ) ).thenThrow( DBDatasourceServiceException.class ).thenCallRealMethod(); when( connection.getAccessType() ).thenReturn( DatabaseAccessType.JNDI ); service.retrieve( testName ); verify( service, times( 2 ) ).getJndiDataSource( anyString() ); verify( cacheManager, never() ).putInRegionCache( IDBDatasourceService.JDBC_DATASOURCE, testName, jndiDataSource ); }