public BaseDatasourceService() { cacheManager = getCacheManager(); // if no cache manager implementation is available we'll use the simple one }
@Before public void setUp(){ mockConnection = mock(IDatabaseConnection.class); // Set it up - this is a NATIVE connection when( mockConnection.getAccessType()).thenReturn( DatabaseAccessType.NATIVE ); when( mockConnection.getDatabaseName()).thenReturn( dsName ); DataSource mockDs = mock(DataSource.class); IDatasourceMgmtService mockMgmtService = mock(IDatasourceMgmtService.class); spyService = spy(service ); try { when( mockMgmtService.getDatasourceByName( dsName ) ).thenReturn( mockConnection ); } catch ( DatasourceMgmtServiceException e ) { e.printStackTrace(); } try { doReturn(mockDs).when( spyService ).resolveDatabaseConnection( mockConnection ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } doReturn( mockMgmtService ).when( spyService ).getDatasourceMgmtService(); service.clearCache(); }
@Test public void testCachedPentahoConnectionServices( ){ try { // Now make sure that the JNDI service method gets called the first time, // but the second time it should retrieve from cache. spyService.getDataSource( dsName ); spyService.getDataSource( dsName ); verify(spyService, Mockito.times( 1 )).resolveDatabaseConnection( mockConnection ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }
@Test public void testPentahoConnectionServices( ){ try { // Now make sure that the resolve.. service method gets called; if not, // fail the test spyService.getDataSource( dsName ); verify(spyService).resolveDatabaseConnection( mockConnection ); verify(spyService, Mockito.never()).getJndiDataSource( dsName ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }
@Test public void testCacheClearinginDynamicConnectionServices( ) { try { when( mockConnection.isUsingConnectionPool() ).thenReturn( true ); spyService.getDataSource( dsName ); spyService.clearCache(); spyService.getDataSource( dsName ); verify( pooledSpyService, Mockito.times( 2 ) ).resolveDatabaseConnection( mockConnection ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }
@Before public void setUp(){ IDatabaseConnection mockConnection = mock(IDatabaseConnection.class); // Set it up - this is a JNDI connection when( mockConnection.getAccessType()).thenReturn( DatabaseAccessType.JNDI ); when( mockConnection.getDatabaseName()).thenReturn( dsName ); DataSource mockDs = mock(DataSource.class); IDatasourceMgmtService mockMgmtService = mock(IDatasourceMgmtService.class); spyService = spy(service ); try { when( mockMgmtService.getDatasourceByName( dsName ) ).thenReturn( mockConnection ); } catch ( DatasourceMgmtServiceException e ) { e.printStackTrace(); } try { doReturn(mockDs).when( spyService ).getJndiDataSource( dsName ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } doReturn( mockMgmtService ).when( spyService ).getDatasourceMgmtService(); service.clearCache(); }
@Test public void testCachedJndiServices( ){ try { // Now make sure that the JNDI service method gets called the first time, // but the second time it should retrieve from cache. spyService.getDataSource( dsName ); spyService.getDataSource( dsName ); verify(spyService, Mockito.times( 1 )).getJndiDataSource( dsName ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }
public DataSource getDataSource( String dsName ) throws DBDatasourceServiceException { DataSource dataSource = null; if ( cacheManager != null ) { if ( !cacheManager.cacheEnabled( IDBDatasourceService.JDBC_DATASOURCE ) ) { cacheManager.addCacheRegion( IDBDatasourceService.JDBC_DATASOURCE ); } Object foundDs = cacheManager.getFromRegionCache( IDBDatasourceService.JDBC_DATASOURCE, dsName ); if ( foundDs != null ) { dataSource = (DataSource) foundDs; } else { dataSource = retrieve( dsName ); } } return dataSource; }
@Before public void setUp() { mockConnection = mock( IDatabaseConnection.class ); // Set it up - this is a NATIVE connection when( mockConnection.getAccessType() ).thenReturn( DatabaseAccessType.NATIVE ); when( mockConnection.getDatabaseName() ).thenReturn( dsName ); DataSource mockDs = mock( DataSource.class ); IDatasourceMgmtService mockMgmtService = mock( IDatasourceMgmtService.class ); DynamicallyPooledOrJndiDatasourceService dynamic = new DynamicallyPooledOrJndiDatasourceService(); dynamic.setNonPooledDatasourceService( nonPooledSpyService ); dynamic.setPooledDatasourceService( pooledSpyService ); spyService = spy(dynamic ); try { when( mockMgmtService.getDatasourceByName( dsName ) ).thenReturn( mockConnection ); } catch ( DatasourceMgmtServiceException e ) { e.printStackTrace(); } try { doReturn( mockDs ).when( nonPooledSpyService ).resolveDatabaseConnection( mockConnection ); doReturn( mockDs ).when( pooledSpyService ).resolveDatabaseConnection( mockConnection ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } doReturn( mockMgmtService ).when( nonPooledSpyService ).getDatasourceMgmtService(); doReturn( mockMgmtService ).when( pooledSpyService ).getDatasourceMgmtService(); doReturn( mockMgmtService ).when( spyService ).getDatasourceMgmtService(); spyService.clearCache(); }
@Test public void testUsePoolingConnectionServices( ) { try { when( mockConnection.isUsingConnectionPool() ).thenReturn( true ); spyService.getDataSource( dsName ); verify( pooledSpyService ).resolveDatabaseConnection( mockConnection ); verify( nonPooledSpyService, Mockito.never() ).resolveDatabaseConnection( mockConnection ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }
@Test public void testJndiServices( ){ try { // Now make sure that the JNDI service method gets called; if not, // fail the test spyService.getDataSource( dsName ); verify(spyService).getJndiDataSource( dsName ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }
@Test public void testUseNonPoolingConnectionServices( ) { try { when( mockConnection.isUsingConnectionPool() ).thenReturn( false ); spyService.getDataSource( dsName ); verify( nonPooledSpyService ).resolveDatabaseConnection( mockConnection ); verify( pooledSpyService, Mockito.never() ).resolveDatabaseConnection( mockConnection ); } catch ( DBDatasourceServiceException e ) { e.printStackTrace(); } }