@Test public void testDefaultRepositoryConfiguration() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, SpringDataWebAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(CityRepository.class)).isNotNull(); assertThat(this.context.getBean(PageableHandlerMethodArgumentResolver.class)) .isNotNull(); assertThat(this.context.getBean(FormattingConversionService.class) .canConvert(Long.class, City.class)).isTrue(); }
@Test public void testUsingJpa() throws Exception { this.context = new AnnotationConfigApplicationContext(); // The order is very important here: DataSource -> Hibernate -> Batch this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); PlatformTransactionManager transactionManager = this.context .getBean(PlatformTransactionManager.class); // It's a lazy proxy, but it does render its target if you ask for toString(): assertThat(transactionManager.toString().contains("JpaTransactionManager")) .isTrue(); assertThat(this.context.getBean(EntityManagerFactory.class)).isNotNull(); // Ensure the JobRepository can be used (no problem with isolation level) assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())).isNull(); }
@Test public void testRenamePrefix() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.name:batchtest", "spring.batch.schema:classpath:batch/custom-schema-hsql.sql", "spring.batch.tablePrefix:PREFIX_"); this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty(); JobExplorer jobExplorer = this.context.getBean(JobExplorer.class); assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty(); JobRepository jobRepository = this.context.getBean(JobRepository.class); assertThat(jobRepository.getLastJobExecution("test", new JobParameters())) .isNull(); }
private void setupContextForGraph(String graph) { String[] ARGS = new String[] {CLOSE_CONTEXT_ARG, TASK_NAME_ARG, "--graph=" + graph}; this.applicationContext = SpringApplication.run(new Object[] {ComposedRunnerVisitorConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class}, ARGS); }
@Test public void dataSourceHealthIndicator() { this.context.register(EmbeddedDataSourceConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(DataSourceHealthIndicator.class); }
@Test public void testOpenEntityManagerInViewInterceptorNotRegisteredWhenExplicitlyOff() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); EnvironmentTestUtils.addEnvironment(context, "spring.jpa.open_in_view:false"); context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class, getAutoConfigureClass()); context.refresh(); assertThat(getInterceptorBeans(context).length).isEqualTo(0); context.close(); }
@Test public void healthEndpoint() { load(EmbeddedDataSourceConfiguration.class, EndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); HealthEndpoint bean = this.context.getBean(HealthEndpoint.class); assertThat(bean).isNotNull(); Health result = bean.invoke(); assertThat(result).isNotNull(); assertThat(result.getDetails().containsKey("db")).isTrue(); }
@Test public void testFlywayEndpoint() { this.context = new AnnotationConfigApplicationContext(); this.context.register(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, EndpointAutoConfiguration.class); this.context.refresh(); FlywayEndpoint endpoint = this.context.getBean(FlywayEndpoint.class); assertThat(endpoint).isNotNull(); assertThat(endpoint.invoke()).hasSize(1); }
@Test @SuppressWarnings("unchecked") public void testOverrideParameters() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "liquibase.parameters.foo:bar"); this.context.register(EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); Map<String, String> parameters = (Map<String, String>) ReflectionTestUtils .getField(liquibase, "parameters"); assertThat(parameters.containsKey("foo")).isTrue(); assertThat(parameters.get("foo")).isEqualTo("bar"); }
@Test public void defaultConfig() { load(Arrays.asList(EmbeddedDataSourceConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class), "spring.session.store-type=jdbc"); JdbcOperationsSessionRepository repository = validateSessionRepository( JdbcOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName")) .isEqualTo("SPRING_SESSION"); assertThat(this.context.getBean(JdbcOperations.class) .queryForList("select * from SPRING_SESSION")).isEmpty(); }
@Test public void disableDatabaseInitializer() { load(Arrays.asList(EmbeddedDataSourceConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class), "spring.session.store-type=jdbc", "spring.session.jdbc.initializer.enabled=false"); JdbcOperationsSessionRepository repository = validateSessionRepository( JdbcOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName")) .isEqualTo("SPRING_SESSION"); this.thrown.expect(BadSqlGrammarException.class); assertThat(this.context.getBean(JdbcOperations.class) .queryForList("select * from SPRING_SESSION")).isEmpty(); }
@Test public void customTableName() { load(Arrays.asList(EmbeddedDataSourceConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class), "spring.session.store-type=jdbc", "spring.session.jdbc.table-name=FOO_BAR", "spring.session.jdbc.schema=classpath:session/custom-schema-h2.sql"); JdbcOperationsSessionRepository repository = validateSessionRepository( JdbcOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName")) .isEqualTo("FOO_BAR"); assertThat(this.context.getBean(JdbcOperations.class) .queryForList("select * from FOO_BAR")).isEmpty(); }
private void prepareApplicationContext(Class<?>... configurationClasses) { this.context = new AnnotationConfigApplicationContext(); this.context.register(configurationClasses); this.context.register(EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); }
@Test(expected = IllegalArgumentException.class) public void persistOfNullThrowsIllegalArgumentExceptionWithoutExceptionTranslation() { this.context = new AnnotationConfigApplicationContext( EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, TestConfiguration.class); this.context.getBean(TestRepository.class).doSomething(); }
@Test(expected = InvalidDataAccessApiUsageException.class) public void persistOfNullThrowsInvalidDataAccessApiUsageExceptionWithExceptionTranslation() { this.context = new AnnotationConfigApplicationContext( EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, TestConfiguration.class, PersistenceExceptionTranslationAutoConfiguration.class); this.context.getBean(TestRepository.class).doSomething(); }
@Test public void createDataSource() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.url:jdbc:hsqldb:mem:flywaytest", "flyway.user:sa"); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(flyway.getDataSource()).isNotNull(); }
@Test public void flywayDataSource() throws Exception { registerAndRefresh(FlywayDataSourceConfiguration.class, EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(flyway.getDataSource()).isNotNull(); }
@Test public void defaultFlyway() throws Exception { registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(flyway.getLocations()).containsExactly("classpath:db/migration"); }
@Test public void overrideLocations() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.locations:classpath:db/changelog,classpath:db/migration"); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(flyway.getLocations()).containsExactly("classpath:db/changelog", "classpath:db/migration"); }
@Test public void testOpenEntityManagerInViewInterceptorNotRegisteredWhenFilterPresent() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(TestFilterConfiguration.class, EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class, getAutoConfigureClass()); context.refresh(); assertThat(getInterceptorBeans(context).length).isEqualTo(0); context.close(); }
@Test public void overrideSchemas() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.schemas:public"); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(Arrays.asList(flyway.getSchemas()).toString()).isEqualTo("[public]"); }
@Test public void changeLogDoesNotExist() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.locations:file:no-such-dir"); this.thrown.expect(BeanCreationException.class); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); }
@Test public void checkLocationsAllMissing() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.locations:classpath:db/missing1,classpath:db/migration2", "flyway.check-location:true"); this.thrown.expect(BeanCreationException.class); this.thrown.expectMessage("Cannot find migrations location in"); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); }
@Test public void checkLocationsAllExist() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.locations:classpath:db/changelog,classpath:db/migration", "flyway.check-location:true"); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); }
@Test public void customFlywayMigrationStrategy() throws Exception { registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, MockFlywayMigrationStrategy.class); assertThat(this.context.getBean(Flyway.class)).isNotNull(); this.context.getBean(MockFlywayMigrationStrategy.class).assertCalled(); }
@Test public void customFlywayMigrationInitializer() throws Exception { registerAndRefresh(CustomFlywayMigrationInitializer.class, EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); assertThat(this.context.getBean(Flyway.class)).isNotNull(); FlywayMigrationInitializer initializer = this.context .getBean(FlywayMigrationInitializer.class); assertThat(initializer.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE); }
@Test public void overrideBaselineVersionString() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "flyway.baseline-version=0"); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(flyway.getBaselineVersion()) .isEqualTo(MigrationVersion.fromVersion("0")); }
@Test public void overrideBaselineVersionNumber() throws Exception { Map<String, Object> source = Collections .<String, Object>singletonMap("flyway.baseline-version", 1); this.context.getEnvironment().getPropertySources() .addLast(new MapPropertySource("flyway", source)); registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Flyway flyway = this.context.getBean(Flyway.class); assertThat(flyway.getBaselineVersion()) .isEqualTo(MigrationVersion.fromVersion("1")); }
public void load(Class<?> config) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); if (config != null) { ctx.register(config); } ctx.register(EmbeddedDataSourceConfiguration.class, DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class); ctx.register(HazelcastJpaDependencyAutoConfiguration.class); ctx.refresh(); this.context = ctx; }
@Test public void testDefaultContext() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); assertThat(this.context.getBean(JobExplorer.class)).isNotNull(); assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty(); }
@Test public void testNoBatchConfiguration() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(EmptyConfiguration.class, BatchAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBeanNamesForType(JobLauncher.class).length) .isEqualTo(0); assertThat(this.context.getBeanNamesForType(JobRepository.class).length) .isEqualTo(0); }
@Test public void testDefinesAndLaunchesJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(JobConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())).isNotNull(); }
@Test public void testDefinesAndLaunchesNamedJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteRegisteredJob"); this.context.register(NamedJobConfigurationWithRegisteredJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); JobRepository repository = this.context.getBean(JobRepository.class); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertThat(repository.getLastJobExecution("discreteRegisteredJob", new JobParameters())).isNotNull(); }
@Test public void testDefinesAndLaunchesLocalJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteLocalJob"); this.context.register(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertThat(this.context.getBean(JobRepository.class) .getLastJobExecution("discreteLocalJob", new JobParameters())) .isNotNull(); }
@Test public void testDisableLaunchesJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.enabled:false"); this.context.register(JobConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); assertThat(this.context.getBeanNamesForType(CommandLineRunner.class).length) .isEqualTo(0); }
@Test public void testDisableSchemaLoader() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.name:batchtest", "spring.batch.initializer.enabled:false"); this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); this.expected.expect(BadSqlGrammarException.class); new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from BATCH_JOB_EXECUTION"); }
@Test public void testDefaultSpringLiquibase() throws Exception { this.context.register(EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); assertThat(liquibase.getChangeLog()) .isEqualTo("classpath:/db/changelog/db.changelog-master.yaml"); assertThat(liquibase.getContexts()).isNull(); assertThat(liquibase.getDefaultSchema()).isNull(); assertThat(liquibase.isDropFirst()).isFalse(); }