ViewStoreJdbcDataSourceProvider { public synchronized DataSource getDataSource() { if (datasource == null) { datasource = lookupDataSource(); } return datasource; }  synchronized DataSource getDataSource();  }
@Test public void shouldLookUpTheDataSourceUsingTheCorrectJndiName() throws Exception { final String dataSourceName = "java:/DS.framework"; final DataSource dataSource = mock(DataSource.class); when(viewStoreDataSourceNameProvider.getDataSourceName()).thenReturn(dataSourceName); when(initialContext.lookup(dataSourceName)).thenReturn(dataSource); assertThat(viewStoreJdbcDataSourceProvider.getDataSource(), is(dataSource)); }
@Test public void shouldThrowExceptionIfTheLookupOfTheDataSourceFails() throws Exception { final NamingException namingException = new NamingException(); final String dataSourceName = "java:/DS.framework"; when(viewStoreDataSourceNameProvider.getDataSourceName()).thenReturn(dataSourceName); when(initialContext.lookup(dataSourceName)).thenThrow(namingException); try { viewStoreJdbcDataSourceProvider.getDataSource(); fail(); } catch (final JdbcRepositoryException expected) { assertThat(expected.getCause(), is(namingException)); assertThat(expected.getMessage(), is("Failed to lookup ViewStore DataSource using JNDI name 'java:/DS.framework'")); } }
@Test public void shouldOnlyLookupTheDataSourceOnce() throws Exception { final String dataSourceName = "java:/DS.framework"; final DataSource dataSource = mock(DataSource.class); when(viewStoreDataSourceNameProvider.getDataSourceName()).thenReturn(dataSourceName); when(initialContext.lookup(dataSourceName)).thenReturn(dataSource); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); viewStoreJdbcDataSourceProvider.getDataSource(); verify(viewStoreDataSourceNameProvider, times(1)).getDataSourceName(); }