### Question:
DataSourceFactory { public XQDataSource getDataSource() throws Exception { XQueryDataSourceType dataSourceType = config.getDataSourceType(); XQDataSource dataSource = getXQDataSource(dataSourceType, config); if (dataSourceType.connectionPropertiesAreSupported()) { if (config.getHost() != null && config.getHost().length() > 0) { dataSource.setProperty(SERVER_NAME, config.getHost()); } if (config.getPort() != null && config.getPort().length() > 0) { dataSource.setProperty(PORT, config.getPort()); } if (config.getDatabaseName() != null && config.getDatabaseName().length() > 0) { dataSource.setProperty(DATABASE_NAME, config.getDatabaseName()); } } return dataSource; } DataSourceFactory(XQueryRunConfig config); XQDataSource getDataSource(); static final String SERVER_NAME; static final String PORT; static final String DATABASE_NAME; }### Answer:
@Test public void shouldGetDataSourceWithoutSettingProperties() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SAXON); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldSetHostOnDataSourceWhenConnectionPropertiesSupportedAndHostNotEmpty() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getHost()).willReturn(HOST); factory.getDataSource(); verify(dataSource).setProperty(SERVER_NAME, HOST); }
@Test public void shouldNotSetHostOnDataSourceWhenConnectionPropertiesSupportedAndHostEmpty() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getHost()).willReturn(""); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldNotSetHostOnDataSourceWhenConnectionPropertiesSupportedAndHostNotSet() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getHost()).willReturn(null); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldSetPortOnDataSourceWhenConnectionPropertiesSupportedAndPortNotEmpty() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getPort()).willReturn(PORT_VALUE); factory.getDataSource(); verify(dataSource).setProperty(PORT, PORT_VALUE); }
@Test public void shouldNotSetPortOnDataSourceWhenConnectionPropertiesSupportedAndPortEmpty() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getPort()).willReturn(""); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldNotSetPortOnDataSourceWhenConnectionPropertiesSupportedAndPortNotSet() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getPort()).willReturn(null); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldSetDbNameOnDataSourceWhenConnectionPropertiesSupportedAndDbNameNotEmpty() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getDatabaseName()).willReturn(DB_NAME); factory.getDataSource(); verify(dataSource).setProperty(DATABASE_NAME, DB_NAME); }
@Test public void shouldNotSetDbNameOnDataSourceWhenConnectionPropertiesSupportedAndDbNameEmpty() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getDatabaseName()).willReturn(""); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldNotSetDbNameOnDataSourceWhenConnectionPropertiesSupportedAndDbNameNotSet() throws Exception { given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getDatabaseName()).willReturn(null); factory.getDataSource(); verifyNoMoreInteractions(dataSource); }
@Test public void shouldGetDataSourceBasedOnDataSourceType() throws Exception { factory = new DataSourceFactory(config); given(config.getDataSourceType()).willReturn(XQueryDataSourceType.SEDNA); given(config.getHost()).willReturn("host"); given(config.getPort()).willReturn("123"); XQDataSource result = factory.getDataSource(); assertThat(result, is(not(nullValue()))); }