|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import javax.persistence.EntityManagerFactory;
|
20 | 20 |
|
21 |
| -import org.junit.After; |
22 | 21 | import org.junit.Test;
|
23 | 22 |
|
24 |
| -import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
25 | 24 | import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
26 | 25 | import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
| 26 | +import org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository; |
27 | 27 | import org.springframework.boot.autoconfigure.data.alt.mongo.CityMongoDbRepository;
|
28 | 28 | import org.springframework.boot.autoconfigure.data.alt.solr.CitySolrRepository;
|
29 | 29 | import org.springframework.boot.autoconfigure.data.jpa.city.City;
|
30 | 30 | import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository;
|
31 | 31 | import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
32 | 32 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
33 |
| -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 33 | +import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; |
| 34 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
34 | 35 | import org.springframework.context.annotation.ComponentScan.Filter;
|
35 | 36 | import org.springframework.context.annotation.Configuration;
|
36 | 37 | import org.springframework.context.annotation.FilterType;
|
37 | 38 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
| 39 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
38 | 40 | import org.springframework.transaction.PlatformTransactionManager;
|
39 | 41 |
|
40 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
|
47 | 49 | */
|
48 | 50 | public class JpaRepositoriesAutoConfigurationTests {
|
49 | 51 |
|
50 |
| - private AnnotationConfigApplicationContext context; |
51 |
| - |
52 |
| - @After |
53 |
| - public void close() { |
54 |
| - this.context.close(); |
55 |
| - } |
| 52 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 53 | + .withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class, |
| 54 | + JpaRepositoriesAutoConfiguration.class, |
| 55 | + PropertyPlaceholderAutoConfiguration.class, |
| 56 | + TaskExecutionAutoConfiguration.class)) |
| 57 | + .withUserConfiguration(EmbeddedDataSourceConfiguration.class); |
56 | 58 |
|
57 | 59 | @Test
|
58 | 60 | public void testDefaultRepositoryConfiguration() {
|
59 |
| - prepareApplicationContext(TestConfiguration.class); |
60 |
| - |
61 |
| - assertThat(this.context.getBean(CityRepository.class)).isNotNull(); |
62 |
| - assertThat(this.context.getBean(PlatformTransactionManager.class)).isNotNull(); |
63 |
| - assertThat(this.context.getBean(EntityManagerFactory.class)).isNotNull(); |
| 61 | + this.contextRunner.withUserConfiguration(TestConfiguration.class) |
| 62 | + .run((context) -> { |
| 63 | + assertThat(context).hasSingleBean(CityRepository.class); |
| 64 | + assertThat(context).hasSingleBean(PlatformTransactionManager.class); |
| 65 | + assertThat(context).hasSingleBean(EntityManagerFactory.class); |
| 66 | + assertThat( |
| 67 | + context.getBean(LocalContainerEntityManagerFactoryBean.class) |
| 68 | + .getBootstrapExecutor()).isNull(); |
| 69 | + }); |
64 | 70 | }
|
65 | 71 |
|
66 | 72 | @Test
|
67 | 73 | public void testOverrideRepositoryConfiguration() {
|
68 |
| - prepareApplicationContext(CustomConfiguration.class); |
69 |
| - assertThat(this.context.getBean( |
70 |
| - org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository.class)) |
71 |
| - .isNotNull(); |
72 |
| - assertThat(this.context.getBean(PlatformTransactionManager.class)).isNotNull(); |
73 |
| - assertThat(this.context.getBean(EntityManagerFactory.class)).isNotNull(); |
| 74 | + this.contextRunner.withUserConfiguration(CustomConfiguration.class) |
| 75 | + .run((context) -> { |
| 76 | + assertThat(context).hasSingleBean(CityJpaRepository.class); |
| 77 | + assertThat(context).hasSingleBean(PlatformTransactionManager.class); |
| 78 | + assertThat(context).hasSingleBean(EntityManagerFactory.class); |
| 79 | + }); |
74 | 80 | }
|
75 | 81 |
|
76 |
| - @Test(expected = NoSuchBeanDefinitionException.class) |
| 82 | + @Test |
77 | 83 | public void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositories() {
|
78 |
| - prepareApplicationContext(SortOfInvalidCustomConfiguration.class); |
| 84 | + this.contextRunner.withUserConfiguration(SortOfInvalidCustomConfiguration.class) |
| 85 | + .run((context) -> assertThat(context) |
| 86 | + .doesNotHaveBean(CityRepository.class)); |
| 87 | + } |
79 | 88 |
|
80 |
| - this.context.getBean(CityRepository.class); |
| 89 | + @Test |
| 90 | + public void whenBootstrappingModeIsLazyBoostrapExecutorIsConfigured() { |
| 91 | + this.contextRunner.withUserConfiguration(TestConfiguration.class) |
| 92 | + .withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=lazy") |
| 93 | + .run((context) -> assertThat( |
| 94 | + context.getBean(LocalContainerEntityManagerFactoryBean.class) |
| 95 | + .getBootstrapExecutor()).isNotNull()); |
81 | 96 | }
|
82 | 97 |
|
83 |
| - private void prepareApplicationContext(Class<?>... configurationClasses) { |
84 |
| - this.context = new AnnotationConfigApplicationContext(); |
85 |
| - this.context.register(configurationClasses); |
86 |
| - this.context.register(EmbeddedDataSourceConfiguration.class, |
87 |
| - HibernateJpaAutoConfiguration.class, |
88 |
| - JpaRepositoriesAutoConfiguration.class, |
89 |
| - PropertyPlaceholderAutoConfiguration.class); |
90 |
| - this.context.refresh(); |
| 98 | + @Test |
| 99 | + public void whenBootstrappingModeIsDeferredBoostrapExecutorIsConfigured() { |
| 100 | + this.contextRunner.withUserConfiguration(TestConfiguration.class) |
| 101 | + .withPropertyValues( |
| 102 | + "spring.data.jpa.repositories.bootstrap-mode=deferred") |
| 103 | + .run((context) -> assertThat( |
| 104 | + context.getBean(LocalContainerEntityManagerFactoryBean.class) |
| 105 | + .getBootstrapExecutor()).isNotNull()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void whenBootstrappingModeIsDefaultBoostrapExecutorIsNotConfigured() { |
| 110 | + this.contextRunner.withUserConfiguration(TestConfiguration.class) |
| 111 | + .withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=default") |
| 112 | + .run((context) -> assertThat( |
| 113 | + context.getBean(LocalContainerEntityManagerFactoryBean.class) |
| 114 | + .getBootstrapExecutor()).isNull()); |
91 | 115 | }
|
92 | 116 |
|
93 | 117 | @Configuration
|
|
0 commit comments