|
30 | 30 | import org.junit.Rule;
|
31 | 31 | import org.junit.Test;
|
32 | 32 | import org.junit.rules.ExpectedException;
|
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.context.ConfigurableApplicationContext; |
| 35 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.context.annotation.ComponentScan; |
| 38 | +import org.springframework.context.annotation.ComponentScan.Filter; |
| 39 | +import org.springframework.context.annotation.FilterType; |
33 | 40 | import org.springframework.data.jpa.domain.sample.Category;
|
34 | 41 | import org.springframework.data.jpa.domain.sample.User;
|
35 | 42 | import org.springframework.data.jpa.repository.JpaContext;
|
| 43 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
36 | 44 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
37 | 45 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
38 | 46 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
| 47 | +import org.springframework.stereotype.Component; |
39 | 48 |
|
40 | 49 | /**
|
41 | 50 | * Integration tests for {@link DefaultJpaContext}.
|
@@ -100,14 +109,58 @@ public void rejectsRequestForTypeManagedByMultipleEntityManagers() {
|
100 | 109 | jpaContext.getEntityManagerByManagedType(User.class);
|
101 | 110 | }
|
102 | 111 |
|
103 |
| - private static final EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) { |
| 112 | + /** |
| 113 | + * @see DATAJPA-813 |
| 114 | + */ |
| 115 | + @Test |
| 116 | + public void bootstrapsDefaultJpaContextInSpringContainer() { |
| 117 | + |
| 118 | + ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class); |
| 119 | + ApplicationComponent component = context.getBean(ApplicationComponent.class); |
| 120 | + |
| 121 | + assertThat(component.context, is(notNullValue())); |
| 122 | + |
| 123 | + context.close(); |
| 124 | + } |
| 125 | + |
| 126 | + private static final LocalContainerEntityManagerFactoryBean createEntityManagerFactoryBean( |
| 127 | + String persistenceUnitName) { |
104 | 128 |
|
105 | 129 | LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
|
106 | 130 | factoryBean.setPersistenceProvider(new HibernatePersistence());
|
107 | 131 | factoryBean.setDataSource(new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build());
|
108 | 132 | factoryBean.setPersistenceUnitName(persistenceUnitName);
|
| 133 | + |
| 134 | + return factoryBean; |
| 135 | + } |
| 136 | + |
| 137 | + private static final EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) { |
| 138 | + |
| 139 | + LocalContainerEntityManagerFactoryBean factoryBean = createEntityManagerFactoryBean(persistenceUnitName); |
109 | 140 | factoryBean.afterPropertiesSet();
|
110 | 141 |
|
111 | 142 | return factoryBean.getObject();
|
112 | 143 | }
|
| 144 | + |
| 145 | + @EnableJpaRepositories |
| 146 | + @ComponentScan(includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApplicationComponent.class) , |
| 147 | + useDefaultFilters = false) |
| 148 | + static class Config { |
| 149 | + |
| 150 | + @Bean |
| 151 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
| 152 | + return createEntityManagerFactoryBean("spring-data-jpa"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Component |
| 157 | + static class ApplicationComponent { |
| 158 | + |
| 159 | + JpaContext context; |
| 160 | + |
| 161 | + @Autowired |
| 162 | + public ApplicationComponent(JpaContext context) { |
| 163 | + this.context = context; |
| 164 | + } |
| 165 | + } |
113 | 166 | }
|
0 commit comments