DATAJPA-564 - Extracted SPI into Spring Data Commons.

This commit is contained in:
Oliver Gierke
2014-07-05 22:56:07 +02:00
parent 1a89612f25
commit 841bb22906
26 changed files with 51 additions and 486 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.jpa.repository;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@@ -34,13 +35,14 @@ import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.jpa.repository.sample.SampleEvaluationContextExtension;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.data.jpa.repository.sample.UserRepositoryImpl;
import org.springframework.data.jpa.repository.support.EvaluationContextExtension;
import org.springframework.data.jpa.repository.support.ExtensibleEvaluationContextProvider;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
import org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider;
import org.springframework.data.repository.query.spi.EvaluationContextExtension;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@@ -58,7 +60,8 @@ public class JavaConfigUserRepositoryTests extends UserRepositoryTests {
static class Config {
@PersistenceContext EntityManager entityManager;
@Autowired BeanFactory beanFactory;
@Autowired ApplicationContext applicationContext;
@Autowired List<EvaluationContextExtension> extensions;
@Bean
public EvaluationContextExtension sampleEvaluationContextExtension() {
@@ -68,16 +71,17 @@ public class JavaConfigUserRepositoryTests extends UserRepositoryTests {
@Bean
public UserRepository userRepository() throws Exception {
ExtensibleEvaluationContextProvider evaluationContextProvider = new ExtensibleEvaluationContextProvider();
evaluationContextProvider.setApplicationContext((ApplicationContext) beanFactory);
ExtensionAwareEvaluationContextProvider evaluationContextProvider = new ExtensionAwareEvaluationContextProvider(
extensions);
evaluationContextProvider.setApplicationContext(applicationContext);
JpaRepositoryFactoryBean<UserRepository, User, Integer> factory = new JpaRepositoryFactoryBean<UserRepository, User, Integer>();
factory.setEntityManager(entityManager);
factory.setBeanFactory(beanFactory);
factory.setBeanFactory(applicationContext);
factory.setRepositoryInterface(UserRepository.class);
factory.setCustomImplementation(new UserRepositoryImpl());
factory.setNamedQueries(namedQueries());
factory.setExpressionEvaluationContextProvider(evaluationContextProvider);
factory.setEvaluationContextProvider(evaluationContextProvider);
factory.afterPropertiesSet();
return factory.getObject();

View File

@@ -58,8 +58,8 @@ import org.springframework.data.jpa.domain.sample.Address;
import org.springframework.data.jpa.domain.sample.Role;
import org.springframework.data.jpa.domain.sample.SpecialUser;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.SampleSecurity.SampleSecurityContextHolder;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.data.jpa.repository.sample.SampleSecurity.SampleSecurityContextHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

View File

@@ -36,12 +36,12 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.StandardEvaluationContextProvider;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
/**
@@ -74,7 +74,7 @@ public class JpaQueryLookupStrategyUnitTests {
public void invalidAnnotatedQueryCausesException() throws Exception {
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, Key.CREATE_IF_NOT_FOUND, extractor,
StandardEvaluationContextProvider.INSTANCE);
DefaultEvaluationContextProvider.INSTANCE);
Method method = UserRepository.class.getMethod("findByFoo", String.class);
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
@@ -96,7 +96,7 @@ public class JpaQueryLookupStrategyUnitTests {
public void sholdThrowMorePreciseExceptionIfTryingToUsePaginationInNativeQueries() throws Exception {
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, Key.CREATE_IF_NOT_FOUND, extractor,
StandardEvaluationContextProvider.INSTANCE);
DefaultEvaluationContextProvider.INSTANCE);
Method method = UserRepository.class.getMethod("findByInvalidNativeQuery", String.class, Pageable.class);
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);

View File

@@ -44,9 +44,9 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.data.jpa.repository.support.DefaultJpaEntityMetadata;
import org.springframework.data.jpa.repository.support.JpaEntityMetadata;
import org.springframework.data.jpa.repository.support.StandardEvaluationContextProvider;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
/**
* Unit test for {@link SimpleJpaQuery}.
@@ -97,7 +97,7 @@ public class SimpleJpaQueryUnitTests {
when(em.createQuery("foo", Long.class)).thenReturn(query);
SimpleJpaQuery jpaQuery = new SimpleJpaQuery(method, em, "select u from User u",
StandardEvaluationContextProvider.INSTANCE);
DefaultEvaluationContextProvider.INSTANCE);
assertThat(jpaQuery.createCountQuery(new Object[] {}), is(query));
}
@@ -114,7 +114,7 @@ public class SimpleJpaQueryUnitTests {
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
AbstractJpaQuery jpaQuery = new SimpleJpaQuery(queryMethod, em, "select u from User u",
StandardEvaluationContextProvider.INSTANCE);
DefaultEvaluationContextProvider.INSTANCE);
jpaQuery.createCountQuery(new Object[] { new PageRequest(1, 10) });
verify(query, times(0)).setFirstResult(anyInt());
@@ -128,7 +128,7 @@ public class SimpleJpaQueryUnitTests {
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class);
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
AbstractJpaQuery jpaQuery = JpaQueryFactory.INSTANCE.fromQueryAnnotation(queryMethod, em,
StandardEvaluationContextProvider.INSTANCE);
DefaultEvaluationContextProvider.INSTANCE);
assertThat(jpaQuery instanceof NativeJpaQuery, is(true));
@@ -210,7 +210,7 @@ public class SimpleJpaQueryUnitTests {
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
return JpaQueryFactory.INSTANCE.fromQueryAnnotation(queryMethod, em,
StandardEvaluationContextProvider.INSTANCE);
DefaultEvaluationContextProvider.INSTANCE);
}
interface SampleRepository {

View File

@@ -13,24 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository;
package org.springframework.data.jpa.repository.sample;
import java.util.Collections;
import java.util.Map;
import org.springframework.data.jpa.repository.SampleSecurity.SampleSecurityContextHolder;
import org.springframework.data.jpa.repository.support.DefaultEvaluationContextExtension;
import org.springframework.data.jpa.repository.support.EvaluationContextExtension;
import org.springframework.data.jpa.repository.sample.SampleSecurity.SampleSecurityContextHolder;
import org.springframework.data.repository.query.spi.EvaluationContextExtension;
import org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport;
/**
* A sample implementation of a custom {@link EvaluationContextExtension}.
*
* @author Thomas Darimont
*/
public class SampleEvaluationContextExtension extends DefaultEvaluationContextExtension {
public class SampleEvaluationContextExtension extends EvaluationContextExtensionSupport {
@Override
public String getScope() {
public String getExtensionId() {
return "security";
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository;
package org.springframework.data.jpa.repository.sample;
/**
* Minimalistic thread-scoped security context analogous to Spring Security for testing.

View File

@@ -56,7 +56,7 @@ public class JpaRepositoryFactoryUnitTests {
public void setUp() {
// Setup standard factory configuration
factory = new JpaRepositoryFactory(entityManager, StandardEvaluationContextProvider.INSTANCE) {
factory = new JpaRepositoryFactory(entityManager) {
@Override
@SuppressWarnings("unchecked")