|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.data.jpa; |
| 18 | + |
| 19 | +import javax.persistence.EntityManagerFactory; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 24 | +import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; |
| 25 | +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
| 26 | +import org.springframework.boot.autoconfigure.data.alt.elasticsearch.CityElasticsearchDbRepository; |
| 27 | +import org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository; |
| 28 | +import org.springframework.boot.autoconfigure.data.alt.mongo.CityMongoDbRepository; |
| 29 | +import org.springframework.boot.autoconfigure.data.jpa.city.City; |
| 30 | +import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository; |
| 31 | +import org.springframework.boot.autoconfigure.data.jpa.country.Country; |
| 32 | +import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; |
| 33 | +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; |
| 34 | +import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; |
| 35 | +import org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration; |
| 36 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.context.annotation.ComponentScan.Filter; |
| 39 | +import org.springframework.context.annotation.Configuration; |
| 40 | +import org.springframework.context.annotation.FilterType; |
| 41 | +import org.springframework.context.annotation.Import; |
| 42 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
| 43 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 44 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 45 | +import org.springframework.scheduling.annotation.EnableScheduling; |
| 46 | +import org.springframework.transaction.PlatformTransactionManager; |
| 47 | + |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | + |
| 50 | +/** |
| 51 | + * Base class for {@link JpaRepositoriesAutoConfiguration} tests. |
| 52 | + * |
| 53 | + * @author Dave Syer |
| 54 | + * @author Oliver Gierke |
| 55 | + * @author Scott Frederick |
| 56 | + * @author Stefano Cordio |
| 57 | + */ |
| 58 | +abstract class AbstractJpaRepositoriesAutoConfigurationTests { |
| 59 | + |
| 60 | + final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 61 | + .withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class, |
| 62 | + JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class)) |
| 63 | + .withUserConfiguration(EmbeddedDataSourceConfiguration.class); |
| 64 | + |
| 65 | + @Test |
| 66 | + void testDefaultRepositoryConfiguration() { |
| 67 | + this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { |
| 68 | + assertThat(context).hasSingleBean(CityRepository.class); |
| 69 | + assertThat(context).hasSingleBean(PlatformTransactionManager.class); |
| 70 | + assertThat(context).hasSingleBean(EntityManagerFactory.class); |
| 71 | + assertThat(context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull(); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testOverrideRepositoryConfiguration() { |
| 77 | + this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { |
| 78 | + assertThat(context).hasSingleBean(CityJpaRepository.class); |
| 79 | + assertThat(context).hasSingleBean(PlatformTransactionManager.class); |
| 80 | + assertThat(context).hasSingleBean(EntityManagerFactory.class); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositories() { |
| 86 | + this.contextRunner.withUserConfiguration(SortOfInvalidCustomConfiguration.class) |
| 87 | + .run((context) -> assertThat(context).doesNotHaveBean(CityRepository.class)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void whenBootstrapModeIsLazyWithMultipleAsyncExecutorBootstrapExecutorIsConfigured() { |
| 92 | + this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class) |
| 93 | + .withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class, |
| 94 | + TaskSchedulingAutoConfiguration.class)) |
| 95 | + .withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=lazy") |
| 96 | + .run((context) -> assertThat( |
| 97 | + context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()) |
| 98 | + .isEqualTo(context.getBean("applicationTaskExecutor"))); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void whenBootstrapModeIsLazyWithSingleAsyncExecutorBootstrapExecutorIsConfigured() { |
| 103 | + this.contextRunner.withUserConfiguration(SingleAsyncTaskExecutorConfiguration.class) |
| 104 | + .withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=lazy") |
| 105 | + .run((context) -> assertThat( |
| 106 | + context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()) |
| 107 | + .isEqualTo(context.getBean("testAsyncTaskExecutor"))); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void whenBootstrapModeIsDeferredBootstrapExecutorIsConfigured() { |
| 112 | + this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class) |
| 113 | + .withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class, |
| 114 | + TaskSchedulingAutoConfiguration.class)) |
| 115 | + .withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=deferred") |
| 116 | + .run((context) -> assertThat( |
| 117 | + context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()) |
| 118 | + .isEqualTo(context.getBean("applicationTaskExecutor"))); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void whenBootstrapModeIsDefaultBootstrapExecutorIsNotConfigured() { |
| 123 | + this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class) |
| 124 | + .withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class, |
| 125 | + TaskSchedulingAutoConfiguration.class)) |
| 126 | + .withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=default").run((context) -> assertThat( |
| 127 | + context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void bootstrapModeIsDefaultByDefault() { |
| 132 | + this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class) |
| 133 | + .withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class, |
| 134 | + TaskSchedulingAutoConfiguration.class)) |
| 135 | + .run((context) -> assertThat( |
| 136 | + context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull()); |
| 137 | + } |
| 138 | + |
| 139 | + @Configuration(proxyBeanMethods = false) |
| 140 | + @EnableScheduling |
| 141 | + @Import(TestConfiguration.class) |
| 142 | + static class MultipleAsyncTaskExecutorConfiguration { |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + @Configuration(proxyBeanMethods = false) |
| 147 | + @Import(TestConfiguration.class) |
| 148 | + static class SingleAsyncTaskExecutorConfiguration { |
| 149 | + |
| 150 | + @Bean |
| 151 | + SimpleAsyncTaskExecutor testAsyncTaskExecutor() { |
| 152 | + return new SimpleAsyncTaskExecutor(); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + @Configuration(proxyBeanMethods = false) |
| 158 | + @TestAutoConfigurationPackage(City.class) |
| 159 | + static class TestConfiguration { |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + @Configuration(proxyBeanMethods = false) |
| 164 | + @EnableJpaRepositories( |
| 165 | + basePackageClasses = org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository.class, |
| 166 | + excludeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, value = CityMongoDbRepository.class), |
| 167 | + @Filter(type = FilterType.ASSIGNABLE_TYPE, value = CityElasticsearchDbRepository.class) }) |
| 168 | + @TestAutoConfigurationPackage(City.class) |
| 169 | + static class CustomConfiguration { |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + @Configuration(proxyBeanMethods = false) |
| 174 | + // To not find any repositories |
| 175 | + @EnableJpaRepositories("foo.bar") |
| 176 | + @TestAutoConfigurationPackage(City.class) |
| 177 | + static class SortOfInvalidCustomConfiguration { |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + @Configuration(proxyBeanMethods = false) |
| 182 | + @TestAutoConfigurationPackage(Country.class) |
| 183 | + static class RevisionRepositoryConfiguration { |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments