DATACMNS-166 - Improved API in Repositories / RepositoryFactoryInformation.
Renamed methods in RepositoryMetadata to be consistent with EntityMetadata. Changed return type of RepositoryMetadata.getIdType() to Class<? extends Serializable>. Changed RepositoryFactoryInformation interface to expose RepositoryInformation rather than just the repository interface. Added exposing the QueryMethod instances as well. Adapter Repositories wrapper and it's clients accordingly.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,13 +33,13 @@ public class AbstractEntityInformationUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullDomainClass() throws Exception {
|
||||
|
||||
new DummyAbstractEntityInformation(null);
|
||||
new DummyEntityInformation<Object>(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersEntityNewIfGetIdReturnsNull() throws Exception {
|
||||
|
||||
EntityInformation<Object, Serializable> metadata = new DummyAbstractEntityInformation(Object.class);
|
||||
EntityInformation<Object, Serializable> metadata = new DummyEntityInformation<Object>(Object.class);
|
||||
assertThat(metadata.isNew(null), is(true));
|
||||
assertThat(metadata.isNew(new Object()), is(false));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -111,11 +111,11 @@ public class AbstractRepositoryMetadataUnitTests {
|
||||
super(repositoryInterface);
|
||||
}
|
||||
|
||||
public Class<?> getIdClass() {
|
||||
public Class<? extends Serializable> getIdType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Class<?> getDomainClass() {
|
||||
public Class<?> getDomainType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,8 +34,8 @@ public class AnnotationRepositoryMetadataUnitTests {
|
||||
public void handlesRepositoryProxyAnnotationCorrectly() {
|
||||
|
||||
RepositoryMetadata metadata = new AnnotationRepositoryMetadata(AnnotatedRepository.class);
|
||||
assertEquals(User.class, metadata.getDomainClass());
|
||||
assertEquals(Integer.class, metadata.getIdClass());
|
||||
assertEquals(User.class, metadata.getDomainType());
|
||||
assertEquals(Integer.class, metadata.getIdType());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -54,24 +54,24 @@ public class DefaultRepositoryMetadataUnitTests {
|
||||
public void looksUpDomainClassCorrectly() throws Exception {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
|
||||
assertEquals(User.class, metadata.getDomainClass());
|
||||
assertEquals(User.class, metadata.getDomainType());
|
||||
|
||||
metadata = new DefaultRepositoryMetadata(SomeDao.class);
|
||||
assertEquals(User.class, metadata.getDomainClass());
|
||||
assertEquals(User.class, metadata.getDomainType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsDomainClassOnExtensionOfDaoInterface() throws Exception {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(ExtensionOfUserCustomExtendedDao.class);
|
||||
assertEquals(User.class, metadata.getDomainClass());
|
||||
assertEquals(User.class, metadata.getDomainType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsParameterizedEntitiesCorrectly() {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(GenericEntityRepository.class);
|
||||
assertEquals(GenericEntity.class, metadata.getDomainClass());
|
||||
assertEquals(GenericEntity.class, metadata.getDomainType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,7 +79,7 @@ public class DefaultRepositoryMetadataUnitTests {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
|
||||
|
||||
assertEquals(Integer.class, metadata.getIdClass());
|
||||
assertEquals(Integer.class, metadata.getIdType());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
@@ -22,14 +22,14 @@ import java.io.Serializable;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class DummyAbstractEntityInformation<T> extends AbstractEntityInformation<T, Serializable> {
|
||||
public class DummyEntityInformation<T> extends AbstractEntityInformation<T, Serializable> {
|
||||
|
||||
/**
|
||||
* Creates a new {@link DummyAbstractEntityInformation} for the given domain class.
|
||||
* Creates a new {@link DummyEntityInformation} for the given domain class.
|
||||
*
|
||||
* @param domainClass
|
||||
*/
|
||||
public DummyAbstractEntityInformation(Class<T> domainClass) {
|
||||
public DummyEntityInformation(Class<T> domainClass) {
|
||||
super(domainClass);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,9 @@ import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.core.support.DummyAbstractEntityInformation;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.support.DummyEntityInformation;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
|
||||
|
||||
@@ -48,9 +50,11 @@ public class DomainClassConverterIntegrationTests {
|
||||
|
||||
@Mock
|
||||
@SuppressWarnings("rawtypes")
|
||||
static RepositoryFactoryBeanSupport factory;
|
||||
RepositoryFactoryBeanSupport factory;
|
||||
@Mock
|
||||
static PersonRepository repository;
|
||||
PersonRepository repository;
|
||||
@Mock
|
||||
RepositoryInformation information;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@@ -66,11 +70,16 @@ public class DomainClassConverterIntegrationTests {
|
||||
beanFactory.registerBeanDefinition("postProcessor", new RootBeanDefinition(PredictingProcessor.class));
|
||||
beanFactory.registerBeanDefinition("repoFactory", new RootBeanDefinition(RepositoryFactoryBeanSupport.class));
|
||||
|
||||
DummyAbstractEntityInformation<Person> entityInformation = new DummyAbstractEntityInformation<Person>(Person.class);
|
||||
when(information.getRepositoryInterface()).thenReturn((Class) PersonRepository.class);
|
||||
when(information.getDomainType()).thenReturn((Class) Person.class);
|
||||
when(information.getIdType()).thenReturn((Class) Serializable.class);
|
||||
|
||||
EntityInformation<Person, Serializable> entityInformation = new DummyEntityInformation<Person>(Person.class);
|
||||
|
||||
when(factory.getObject()).thenReturn(repository);
|
||||
when(factory.getObjectType()).thenReturn(PersonRepository.class);
|
||||
when(factory.getEntityInformation()).thenReturn(entityInformation);
|
||||
when(factory.getRepositoryInterface()).thenReturn(PersonRepository.class);
|
||||
when(factory.getRepositoryInformation()).thenReturn(information);
|
||||
|
||||
GenericApplicationContext context = new GenericApplicationContext(beanFactory);
|
||||
assertThat(context.getBeansOfType(RepositoryFactoryInformation.class).values().size(), is(1));
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -35,6 +36,8 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.support.DummyEntityInformation;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
|
||||
|
||||
/**
|
||||
@@ -63,14 +66,15 @@ public class DomainClassConverterUnitTests {
|
||||
@Mock
|
||||
DefaultConversionService service;
|
||||
@Mock
|
||||
EntityInformation<User, Long> information;
|
||||
@Mock
|
||||
RepositoryFactoryInformation<User, Long> provider;
|
||||
RepositoryFactoryInformation<User, Serializable> provider;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void setUp() {
|
||||
|
||||
EntityInformation<User, Serializable> information = new DummyEntityInformation<User>(User.class);
|
||||
RepositoryInformation repositoryInformation = new DummyRepositoryInformation(UserRepository.class);
|
||||
|
||||
converter = new DomainClassConverter(service);
|
||||
providers = new HashMap<String, RepositoryFactoryInformation>();
|
||||
|
||||
@@ -78,9 +82,7 @@ public class DomainClassConverterUnitTests {
|
||||
targetDescriptor = TypeDescriptor.valueOf(User.class);
|
||||
|
||||
when(provider.getEntityInformation()).thenReturn(information);
|
||||
when(provider.getRepositoryInterface()).thenReturn((Class) UserRepository.class);
|
||||
when(information.getJavaType()).thenReturn(User.class);
|
||||
when(information.getIdType()).thenReturn(Long.class);
|
||||
when(provider.getRepositoryInformation()).thenReturn(repositoryInformation);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -155,7 +157,7 @@ public class DomainClassConverterUnitTests {
|
||||
}
|
||||
|
||||
private void configureContextToReturnBeans(ApplicationContext context, UserRepository repository,
|
||||
RepositoryFactoryInformation<User, Long> provider) {
|
||||
RepositoryFactoryInformation<User, Serializable> provider) {
|
||||
|
||||
Map<String, UserRepository> map = getBeanAsMap(repository);
|
||||
when(context.getBeansOfType(UserRepository.class)).thenReturn(map);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2011 the original author or authors.
|
||||
* Copyright 2008-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,6 +34,8 @@ import org.springframework.beans.PropertyEditorRegistry;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.support.DummyEntityInformation;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
|
||||
|
||||
/**
|
||||
@@ -52,23 +54,23 @@ public class DomainClassPropertyEditorRegistrarUnitTests {
|
||||
@Mock
|
||||
EntityRepository repository;
|
||||
@Mock
|
||||
EntityInformation<Entity, Long> information;
|
||||
@Mock
|
||||
RepositoryFactoryInformation<Entity, Long> provider;
|
||||
RepositoryFactoryInformation<Entity, Serializable> provider;
|
||||
|
||||
DomainClassPropertyEditor<Entity, Long> reference;
|
||||
DomainClassPropertyEditor<Entity, Serializable> reference;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void setup() {
|
||||
|
||||
when(information.getJavaType()).thenReturn(Entity.class);
|
||||
when(provider.getEntityInformation()).thenReturn(information);
|
||||
when(provider.getRepositoryInterface()).thenReturn((Class) EntityRepository.class);
|
||||
EntityInformation<Entity, Serializable> entityInformation = new DummyEntityInformation<Entity>(Entity.class);
|
||||
RepositoryInformation repositoryInformation = new DummyRepositoryInformation(EntityRepository.class);
|
||||
|
||||
when(provider.getEntityInformation()).thenReturn(entityInformation);
|
||||
when(provider.getRepositoryInformation()).thenReturn(repositoryInformation);
|
||||
|
||||
Map<String, EntityRepository> map = getBeanAsMap(repository);
|
||||
when(context.getBeansOfType(EntityRepository.class)).thenReturn(map);
|
||||
|
||||
reference = new DomainClassPropertyEditor<Entity, Long>(repository, information, registry);
|
||||
reference = new DomainClassPropertyEditor<Entity, Serializable>(repository, entityInformation, registry);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,7 +111,7 @@ public class DomainClassPropertyEditorRegistrarUnitTests {
|
||||
|
||||
}
|
||||
|
||||
private static interface EntityRepository extends CrudRepository<Entity, Long> {
|
||||
private static interface EntityRepository extends CrudRepository<Entity, Serializable> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
|
||||
public final class DummyRepositoryInformation implements RepositoryInformation {
|
||||
|
||||
private final RepositoryMetadata metadata;
|
||||
|
||||
public DummyRepositoryInformation(Class<?> repositoryInterface) {
|
||||
this.metadata = new DefaultRepositoryMetadata(repositoryInterface);
|
||||
}
|
||||
|
||||
public Class<? extends Serializable> getIdType() {
|
||||
return metadata.getIdType();
|
||||
}
|
||||
|
||||
public Class<?> getDomainType() {
|
||||
return metadata.getDomainType();
|
||||
}
|
||||
|
||||
public Class<?> getRepositoryInterface() {
|
||||
return metadata.getRepositoryInterface();
|
||||
}
|
||||
|
||||
public Class<?> getReturnedDomainClass(Method method) {
|
||||
return getDomainType();
|
||||
}
|
||||
|
||||
public Class<?> getRepositoryBaseClass() {
|
||||
return getRepositoryInterface();
|
||||
}
|
||||
|
||||
public boolean hasCustomMethod() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isCustomMethod(Method method) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Iterable<Method> getQueryMethods() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
public Method getTargetClassMethod(Method method) {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,9 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -34,9 +36,12 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DummyEntityInformation;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Repositories}.
|
||||
@@ -112,32 +117,17 @@ public class RepositoriesUnitTests {
|
||||
this.repositoryMetadata = new DefaultRepositoryMetadata(repositoryInterface);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public EntityInformation<T, S> getEntityInformation() {
|
||||
|
||||
return new EntityInformation<T, S>() {
|
||||
|
||||
public Class<T> getJavaType() {
|
||||
return (Class<T>) repositoryMetadata.getDomainClass();
|
||||
}
|
||||
|
||||
public boolean isNew(T entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public S getId(T entity) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Class<S> getIdType() {
|
||||
return (Class<S>) repositoryMetadata.getIdClass();
|
||||
}
|
||||
};
|
||||
return (EntityInformation) new DummyEntityInformation(repositoryMetadata.getDomainType());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<? extends Repository<T, S>> getRepositoryInterface() {
|
||||
return (Class<? extends Repository<T, S>>) repositoryMetadata.getRepositoryInterface();
|
||||
public RepositoryInformation getRepositoryInformation() {
|
||||
return new DummyRepositoryInformation(repositoryMetadata.getRepositoryInterface());
|
||||
}
|
||||
|
||||
public List<QueryMethod> getQueryMethods() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user