类org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration源码实例Demo

下面列出了怎么用org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: spring-cloud-task   文件: TaskEventTests.java
@Test
public void testDefaultConfiguration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					EmbeddedDataSourceConfiguration.class,
					TaskEventAutoConfiguration.class,
					PropertyPlaceholderAutoConfiguration.class,
					TestSupportBinderAutoConfiguration.class,
					SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class,
					BindingServiceConfiguration.class))
			.withUserConfiguration(TaskEventsConfiguration.class)
			.withPropertyValues("spring.cloud.task.closecontext_enabled=false",
					"spring.main.web-environment=false");
	applicationContextRunner.run((context) -> {
		assertThat(context.getBean("taskEventListener")).isNotNull();
		assertThat(
				context.getBean(TaskEventAutoConfiguration.TaskEventChannels.class))
						.isNotNull();
	});
}
 
@Test
public void testConfiguration() {

	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					PropertyPlaceholderAutoConfiguration.class,
					SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class,
					EmbeddedDataSourceConfiguration.class))
			.withPropertyValues("spring.cloud.task.singleInstanceEnabled=true");
	applicationContextRunner.run((context) -> {
		SingleInstanceTaskListener singleInstanceTaskListener = context
				.getBean(SingleInstanceTaskListener.class);

		assertThat(singleInstanceTaskListener)
				.as("singleInstanceTaskListener should not be null").isNotNull();

		assertThat(SingleInstanceTaskListener.class)
				.isEqualTo(singleInstanceTaskListener.getClass());
	});
}
 
@Test
public void testRepositoryNotInitialized() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					EmbeddedDataSourceConfiguration.class,
					PropertyPlaceholderAutoConfiguration.class,
					SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
			.withUserConfiguration(TaskLifecycleListenerConfiguration.class)
			.withPropertyValues("spring.cloud.task.tablePrefix=foobarless");

	verifyExceptionThrownDefaultExecutable(ApplicationContextException.class,
			"Failed to start " + "bean 'taskLifecycleListener'; nested exception is "
					+ "org.springframework.dao.DataAccessResourceFailureException: "
					+ "Could not obtain sequence value; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: "
					+ "Syntax error in SQL statement \"SELECT FOOBARLESSSEQ.NEXTVAL FROM[*] DUAL\"; "
					+ "expected \"identifier\"; SQL statement:\n"
					+ "select foobarlessSEQ.nextval from dual [42001-200]",
			applicationContextRunner);
}
 
/**
 * Verify that the verifyEnvironment method skips DataSource Proxy Beans when
 * determining the number of available dataSources.
 */
@Test
public void testWithDataSourceProxy() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					EmbeddedDataSourceConfiguration.class,
					PropertyPlaceholderAutoConfiguration.class,
					SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
			.withUserConfiguration(DataSourceProxyConfiguration.class);
	applicationContextRunner.run((context) -> {
		assertThat(context.getBeanNamesForType(DataSource.class).length).isEqualTo(2);
		SimpleTaskAutoConfiguration taskConfiguration = context
				.getBean(SimpleTaskAutoConfiguration.class);
		assertThat(taskConfiguration).isNotNull();
		assertThat(taskConfiguration.taskExplorer()).isNotNull();
	});
}
 
private void setupContextForGraph(String[] args) {
	this.applicationContext = SpringApplication.run(new Class[]{ComposedRunnerVisitorConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			BatchAutoConfiguration.class,
			TaskBatchAutoConfiguration.class,
			SimpleTaskAutoConfiguration.class}, args);
}
 
private void setupContextForGraph(String[] args) {
	this.applicationContext = SpringApplication.run(new Class[]{ComposedRunnerVisitorConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			BatchAutoConfiguration.class,
			TaskBatchAutoConfiguration.class,
			SimpleTaskAutoConfiguration.class}, args);
}
 
@Test
public void testDefaultContext() {
	this.context = new AnnotationConfigApplicationContext();
	this.context.register(TestConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class);
	this.context.refresh();
	assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
			.queryForList("select * from TASK_EXECUTION").size()).isEqualTo(0);
}
 
@Test
public void testNoTaskConfiguration() {
	this.context = new AnnotationConfigApplicationContext();
	this.context.register(EmptyConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class);
	this.context.refresh();
	assertThat(this.context.getBeanNamesForType(SimpleTaskRepository.class).length)
			.isEqualTo(0);
}
 
@Test(expected = BeanCreationException.class)
public void testMultipleDataSourcesContext() {
	this.context = new AnnotationConfigApplicationContext();
	this.context.register(SimpleTaskAutoConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class);
	DataSource dataSource = mock(DataSource.class);
	this.context.getBeanFactory().registerSingleton("mockDataSource", dataSource);
	this.context.refresh();
}
 
private void initializeJdbcExplorerTest() {
	this.context = new AnnotationConfigApplicationContext();
	this.context.register(TestConfiguration.class,
			EmbeddedDataSourceConfiguration.class,
			PropertyPlaceholderAutoConfiguration.class);
	this.context.refresh();

	this.context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
			AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
}
 
@Test
public void testDefaultContext() throws Exception {
	AnnotationConfigApplicationContext localContext = new AnnotationConfigApplicationContext();
	localContext.register(EmbeddedDataSourceConfiguration.class,
			EntityManagerConfiguration.class);
	localContext.refresh();
	DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(
			this.dataSource, TaskProperties.DEFAULT_TABLE_PREFIX, localContext);
	assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName())
			.isEqualTo("org.springframework.orm.jpa.JpaTransactionManager");
}
 
@Test
public void testRepositoryInitialized() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					EmbeddedDataSourceConfiguration.class,
					PropertyPlaceholderAutoConfiguration.class,
					SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
			.withUserConfiguration(TaskLifecycleListenerConfiguration.class);
	applicationContextRunner.run((context) -> {
		TaskExplorer taskExplorer = context.getBean(TaskExplorer.class);
		assertThat(taskExplorer.getTaskExecutionCount()).isEqualTo(1L);
	});
}
 
 同包方法