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 java.lang.reflect.Method;
import org.springframework.data.document.mongodb.MongoOperations; 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.Repository;
import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key; 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.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.support.RepositoryFactorySupport; import org.springframework.data.repository.support.RepositoryFactorySupport;
import org.springframework.data.repository.support.RepositorySupport; import org.springframework.data.repository.support.RepositorySupport;
import org.springframework.data.repository.util.ClassUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
@@ -36,7 +39,7 @@ import org.springframework.util.Assert;
* @author Oliver Gierke * @author Oliver Gierke
*/ */
public class MongoRepositoryFactoryBean extends public class MongoRepositoryFactoryBean extends
RepositoryFactoryBeanSupport<Repository<?, ?>> { RepositoryFactoryBeanSupport<MongoRepository<?, ?>> {
private MongoOperations operations; private MongoOperations operations;
@@ -62,7 +65,7 @@ public class MongoRepositoryFactoryBean extends
@Override @Override
protected RepositoryFactorySupport createRepositoryFactory() { protected RepositoryFactorySupport createRepositoryFactory() {
return new MongoRepositoryFactory(); return new MongoRepositoryFactory(operations);
} }
@@ -85,7 +88,20 @@ public class MongoRepositoryFactoryBean extends
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
private class MongoRepositoryFactory extends RepositoryFactorySupport { public static class MongoRepositoryFactory extends RepositoryFactorySupport {
private final MongoOperations operations;
/**
* Creates a new {@link MongoRepositoryFactory} fwith the given {@link MongoOperations}.
*
* @param operations
*/
public MongoRepositoryFactory(MongoOperations operations) {
this.operations = operations;
}
@Override @Override
protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository( protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository(
@@ -121,5 +137,20 @@ public class MongoRepositoryFactoryBean extends
return new MongoQuery(new QueryMethod(method), operations); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { public T findById(ID id) {
List<T> result = return operations.find(getDomainClass(), id);
operations.query(operations.getDefaultCollectionName(),
QueryBuilder.start("_id").get(), getDomainClass());
return result.isEmpty() ? null : result.get(0);
} }

View File

@@ -69,6 +69,10 @@ public abstract class MongoOperationsUnitTests {
public <T> T convertObjectId(ObjectId id, Class<T> targetType) { public <T> T convertObjectId(ObjectId id, Class<T> targetType) {
return null; 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)); repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi));
} }
@Test
public void findsPersonById() throws Exception {
assertThat(repository.findById(dave.getId()), is(dave));
}
@Test @Test
public void findsPersonsByLastname() throws Exception { 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 * @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. * Returns all {@link Person}s with the given lastname.

View File

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

View File

@@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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"> <bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="operations" ref="mongoTemplate" /> <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/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"> 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"> <mongo:repositories base-package="org.springframework.data.**.repository">
<repository:exclude-filter type="regex" expression=".*MongoRepository" /> <repository:exclude-filter type="regex" expression=".*MongoRepository" />