Improvements to repositories.

- MongoRepositoryFactory now validates id type for supported types
- use MongoOperations.find(…) in SimpleMongoRepository.findById(…)
- added integration test for findById(…)
- added integration test for MongoTemplate
This commit is contained in:
Oliver Gierke
2011-01-05 20:01:40 +01:00
parent 8abd7df7ef
commit cb6bed43ab
10 changed files with 157 additions and 20 deletions

View File

@@ -19,6 +19,7 @@ import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@@ -27,7 +28,9 @@ import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.support.RepositoryFactorySupport;
import org.springframework.data.repository.support.RepositorySupport;
import org.springframework.data.repository.util.ClassUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -36,7 +39,7 @@ import org.springframework.util.Assert;
* @author Oliver Gierke
*/
public class MongoRepositoryFactoryBean extends
RepositoryFactoryBeanSupport<Repository<?, ?>> {
RepositoryFactoryBeanSupport<MongoRepository<?, ?>> {
private MongoOperations operations;
@@ -62,7 +65,7 @@ public class MongoRepositoryFactoryBean extends
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
return new MongoRepositoryFactory();
return new MongoRepositoryFactory(operations);
}
@@ -85,9 +88,22 @@ public class MongoRepositoryFactoryBean extends
*
* @author Oliver Gierke
*/
private class MongoRepositoryFactory extends RepositoryFactorySupport {
public static class MongoRepositoryFactory extends RepositoryFactorySupport {
@Override
private final MongoOperations operations;
/**
* Creates a new {@link MongoRepositoryFactory} fwith the given {@link MongoOperations}.
*
* @param operations
*/
public MongoRepositoryFactory(MongoOperations operations) {
this.operations = operations;
}
@Override
protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository(
Class<T> domainClass, Class<?> repositoryInterface) {
@@ -121,5 +137,20 @@ public class MongoRepositoryFactoryBean extends
return new MongoQuery(new QueryMethod(method), operations);
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryFactorySupport#validate(java.lang.Class, java.lang.Object)
*/
@Override
protected void validate(Class<? extends Repository<?, ?>> repositoryInterface, Object customImplementation) {
Class<?> idClass = ClassUtils.getIdClass(repositoryInterface);
if (!MongoPropertyDescriptor.SUPPORTED_ID_CLASSES.contains(idClass)) {
throw new IllegalArgumentException(String.format("Unsupported id class! Only %s are supported!",
StringUtils.collectionToCommaDelimitedString(MongoPropertyDescriptor.SUPPORTED_ID_CLASSES)));
}
super.validate(repositoryInterface, customImplementation);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2010-2011 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.
@@ -105,11 +105,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
*/
public T findById(ID id) {
List<T> result =
operations.query(operations.getDefaultCollectionName(),
QueryBuilder.start("_id").get(), getDomainClass());
return result.isEmpty() ? null : result.get(0);
return operations.find(getDomainClass(), id);
}

View File

@@ -69,6 +69,10 @@ public abstract class MongoOperationsUnitTests {
public <T> T convertObjectId(ObjectId id, Class<T> targetType) {
return null;
}
public ObjectId convertObjectId(Object id) {
return null;
}
};
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2011 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.document.mongodb;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration test for {@link MongoTemplate}.
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
public class MongoTemplateTests {
@Autowired
MongoTemplate template;
@Before
public void setUp() {
template.dropCollection(template.getDefaultCollectionName());
}
@Test
public void insertsSimpleEntityCorrectly() throws Exception {
Person person = new Person("Oliver");
template.insert(person);
Person reference = template.find(Person.class, person.getId());
assertThat(reference, is(person));
}
}

View File

@@ -41,6 +41,12 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi));
}
@Test
public void findsPersonById() throws Exception {
assertThat(repository.findById(dave.getId()), is(dave));
}
@Test
public void findsPersonsByLastname() throws Exception {

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2011 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.document.mongodb.repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.User;
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean.MongoRepositoryFactory;
/**
* Unit test for {@link MongoRepositoryFactory}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class MongoRepositoryFactoryUnitTests {
@Mock
MongoOperations operations;
@Test(expected = IllegalArgumentException.class)
public void rejectsInvalidIdType() throws Exception {
MongoRepositoryFactory factory = new MongoRepositoryFactory(operations);
factory.getRepository(SampleRepository.class);
}
private interface SampleRepository extends MongoRepository<User, Long> {
}
}

View File

@@ -26,7 +26,7 @@ import org.springframework.data.domain.Pageable;
*
* @author Oliver Gierke
*/
public interface PersonRepository extends MongoRepository<Person, Long> {
public interface PersonRepository extends MongoRepository<Person, String> {
/**
* Returns all {@link Person}s with the given lastname.

View File

@@ -3,13 +3,13 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mongo" class="org.springframework.data.document.mongodb.MongoFactoryBean">
<property name="host" value="localhost" />
<property name="port" value="27017" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg>
<bean class="com.mongodb.Mongo">
<constructor-arg value="localhost" />
<constructor-arg value="27017" />
</bean>
</constructor-arg>
<constructor-arg ref="mongo" />
<constructor-arg value="database" />
<property name="defaultCollectionName" value="springdata" />
</bean>

View File

@@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="infrastructure.xml" />
<import resource="classpath:infrastructure.xml" />
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="operations" ref="mongoTemplate" />

View File

@@ -7,7 +7,7 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.0.xsd">
<import resource="../infrastructure.xml" />
<import resource="classpath:infrastructure.xml" />
<mongo:repositories base-package="org.springframework.data.**.repository">
<repository:exclude-filter type="regex" expression=".*MongoRepository" />