|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.orm.jpa;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URL; |
| 21 | +import java.net.URLClassLoader; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Enumeration; |
| 24 | +import java.util.List; |
19 | 25 | import java.util.Map;
|
| 26 | +import java.util.Vector; |
20 | 27 |
|
| 28 | +import javax.persistence.EntityManager; |
| 29 | +import javax.persistence.EntityManagerFactory; |
21 | 30 | import javax.transaction.Synchronization;
|
22 | 31 | import javax.transaction.SystemException;
|
23 | 32 | import javax.transaction.Transaction;
|
|
30 | 39 | import org.junit.rules.ExpectedException;
|
31 | 40 |
|
32 | 41 | import org.springframework.beans.factory.BeanCreationException;
|
| 42 | +import org.springframework.beans.factory.annotation.Autowired; |
| 43 | +import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; |
33 | 44 | import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
|
34 | 45 | import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
| 46 | +import org.springframework.boot.autoconfigure.orm.jpa.test.City; |
35 | 47 | import org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration;
|
36 | 48 | import org.springframework.boot.orm.jpa.hibernate.SpringJtaPlatform;
|
| 49 | +import org.springframework.context.annotation.Configuration; |
37 | 50 | import org.springframework.orm.jpa.JpaTransactionManager;
|
38 | 51 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
39 | 52 |
|
@@ -77,6 +90,17 @@ public void testDataScript() throws Exception {
|
77 | 90 | load("spring.datasource.data:classpath:/city.sql");
|
78 | 91 | }
|
79 | 92 |
|
| 93 | + @Test |
| 94 | + public void testDataScriptRunsEarly() { |
| 95 | + load(new Class<?>[] { TestInitializedJpaConfiguration.class }, null, |
| 96 | + new HideDataScriptClassLoader(), |
| 97 | + "spring.jpa.show-sql=true", |
| 98 | + "spring.jpa.hibernate.ddl-auto:create-drop", |
| 99 | + "spring.datasource.data:classpath:/city.sql"); |
| 100 | + assertThat(this.context.getBean( |
| 101 | + TestInitializedJpaConfiguration.class).called).isTrue(); |
| 102 | + } |
| 103 | + |
80 | 104 | @Test
|
81 | 105 | public void testFlywayPlusValidation() throws Exception {
|
82 | 106 | load(new Class<?>[0], new Class<?>[] { FlywayAutoConfiguration.class },
|
@@ -125,6 +149,25 @@ public void testCustomJpaTransactionManagerUsingProperties() throws Exception {
|
125 | 149 | assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
|
126 | 150 | }
|
127 | 151 |
|
| 152 | + @Configuration |
| 153 | + @TestAutoConfigurationPackage(City.class) |
| 154 | + static class TestInitializedJpaConfiguration { |
| 155 | + |
| 156 | + private boolean called; |
| 157 | + |
| 158 | + @Autowired |
| 159 | + public void validateDataSourceIsInitialized( |
| 160 | + EntityManagerFactory entityManagerFactory) { |
| 161 | + // Inject the entity manager to validate it is initialized at the injection point |
| 162 | + EntityManager entityManager = entityManagerFactory.createEntityManager(); |
| 163 | + City city = entityManager.find(City.class, 2000L); |
| 164 | + assertThat(city).isNotNull(); |
| 165 | + assertThat(city.getName()).isEqualTo("Washington"); |
| 166 | + this.called = true; |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
128 | 171 | public static class TestJtaPlatform implements JtaPlatform {
|
129 | 172 |
|
130 | 173 | @Override
|
@@ -159,4 +202,23 @@ public int getCurrentStatus() throws SystemException {
|
159 | 202 |
|
160 | 203 | }
|
161 | 204 |
|
| 205 | + private static class HideDataScriptClassLoader extends URLClassLoader { |
| 206 | + |
| 207 | + private static final List<String> HIDDEN_RESOURCES = |
| 208 | + Arrays.asList("schema-all.sql", "schema.sql"); |
| 209 | + |
| 210 | + HideDataScriptClassLoader() { |
| 211 | + super(new URL[0], HideDataScriptClassLoader.class.getClassLoader()); |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | + @Override |
| 216 | + public Enumeration<URL> getResources(String name) throws IOException { |
| 217 | + if (HIDDEN_RESOURCES.contains(name)) { |
| 218 | + return new Vector().elements(); |
| 219 | + } |
| 220 | + return super.getResources(name); |
| 221 | + } |
| 222 | + } |
| 223 | + |
162 | 224 | }
|
0 commit comments