@Before public void init() { EmbeddedDatabaseConnection.override = null; EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.initialize:false", "spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt()); }
@Test public void bindInvalidHost() { this.context.register(SamplePropertyValidationApplication.class); EnvironmentTestUtils.addEnvironment(this.context, "sample.host:xxxxxx", "sample.port:9090"); this.thrown.expect(BeanCreationException.class); this.thrown.expectMessage("xxxxxx"); this.context.refresh(); }
private AnnotationConfigApplicationContext doLoad(Class<?>[] configs, String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(configs); applicationContext.register(RabbitAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(applicationContext, environment); applicationContext.refresh(); return applicationContext; }
@Test public void bindValidProperties() { this.context.register(SamplePropertyValidationApplication.class); EnvironmentTestUtils.addEnvironment( this.context, "sample.host:192.168.0.1", "sample.port:9090"); this.context.refresh(); SampleProperties properties = this.context.getBean(SampleProperties.class); assertThat(properties.getHost()).isEqualTo("192.168.0.1"); assertThat(properties.getPort()).isEqualTo(Integer.valueOf(9090)); }
@Test public void validatorOnlyCalledOnSupportedClass() { this.context.register(SamplePropertyValidationApplication.class); this.context.register(ServerProperties.class); // our validator will not apply EnvironmentTestUtils.addEnvironment( this.context, "sample.host:192.168.0.1", "sample.port:9090"); this.context.refresh(); SampleProperties properties = this.context.getBean(SampleProperties.class); assertThat(properties.getHost()).isEqualTo("192.168.0.1"); assertThat(properties.getPort()).isEqualTo(Integer.valueOf(9090)); }
@Test public void testDataSourceInitialized() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true"); this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue(); assertThat(dataSource).isNotNull(); JdbcOperations template = new JdbcTemplate(dataSource); assertThat(template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)).isEqualTo(1); }
@Test public void testTwoDataSources() throws Exception { EnvironmentTestUtils.addEnvironment( this.context, "datasource.one.url=jdbc:hsqldb:mem:/one", "datasource.one.driverClassName=org.hsqldb.Driver", "datasource.two.url=jdbc:hsqldb:mem:/two", "datasource.two.driverClassName=org.hsqldb.Driver"); this.context.register( TwoDataSources.class, DataSourceInitializer.class, PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class); this.context.refresh(); assertThat(this.context.getBeanNamesForType(DataSource.class).length).isEqualTo(2); }
@Test public void testDataSourceInitializedWithExplicitScript() throws Exception { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql")); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue(); assertThat(dataSource).isNotNull(); JdbcOperations template = new JdbcTemplate(dataSource); assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1); }
@Test public void testDataSourceInitializedWithDataCredentials() { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.initialize:true", "spring.datasource.sqlScriptEncoding:UTF-8", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "encoding-schema.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "encoding-data.sql"), "spring.datasource.data-username:admin", "spring.datasource.data-password:admin"); try { this.context.refresh(); fail("User does not exist"); } catch (Exception ex) { assertThat(ex).isInstanceOf(UnsatisfiedDependencyException.class); } }