diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryConfigDefinitionParser.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryConfigDefinitionParser.java new file mode 100644 index 000000000..38611907e --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryConfigDefinitionParser.java @@ -0,0 +1,51 @@ +package org.springframework.data.document.mongodb.repository.config; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.data.document.mongodb.repository.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration; +import org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser; +import org.w3c.dom.Element; + + +/** + * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} to create + * Mongo DB repositories from classpath scanning or manual definition. + * + * @author Oliver Gierke + */ +public class MongoRepositoryConfigDefinitionParser + extends + AbstractRepositoryConfigDefinitionParser { + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.config. + * AbstractRepositoryConfigDefinitionParser + * #getGlobalRepositoryConfigInformation(org.w3c.dom.Element) + */ + @Override + protected SimpleMongoRepositoryConfiguration getGlobalRepositoryConfigInformation( + Element element) { + + return new SimpleMongoRepositoryConfiguration(element); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.config. + * AbstractRepositoryConfigDefinitionParser + * #postProcessBeanDefinition(org.springframework + * .data.repository.config.SingleRepositoryConfigInformation, + * org.springframework.beans.factory.support.BeanDefinitionBuilder, + * java.lang.Object) + */ + @Override + protected void postProcessBeanDefinition( + MongoRepositoryConfiguration context, + BeanDefinitionBuilder builder, Object beanSource) { + + builder.addPropertyReference("template", context.getMongoTemplateRef()); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryNamespaceHandler.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryNamespaceHandler.java new file mode 100644 index 000000000..62067bd60 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/MongoRepositoryNamespaceHandler.java @@ -0,0 +1,24 @@ +package org.springframework.data.document.mongodb.repository.config; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + + +/** + * {@link org.springframework.beans.factory.xml.NamespaceHandler} for Mongo DB + * based repositories. + * + * @author Oliver Gierke + */ +public class MongoRepositoryNamespaceHandler extends NamespaceHandlerSupport { + + /* + * (non-Javadoc) + * + * @see org.springframework.beans.factory.xml.NamespaceHandler#init() + */ + public void init() { + + registerBeanDefinitionParser("repositories", + new MongoRepositoryConfigDefinitionParser()); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/SimpleMongoRepositoryConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/SimpleMongoRepositoryConfiguration.java new file mode 100644 index 000000000..6b7bdb876 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/config/SimpleMongoRepositoryConfiguration.java @@ -0,0 +1,181 @@ +package org.springframework.data.document.mongodb.repository.config; + +import org.springframework.data.document.mongodb.MongoTemplate; +import org.springframework.data.document.mongodb.repository.MongoRepository; +import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean; +import org.springframework.data.repository.config.AutomaticRepositoryConfigInformation; +import org.springframework.data.repository.config.ManualRepositoryConfigInformation; +import org.springframework.data.repository.config.RepositoryConfig; +import org.springframework.data.repository.config.SingleRepositoryConfigInformation; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + + +/** + * {@link RepositoryConfig} implementation to create + * {@link MongoRepositoryConfiguration} instances for both automatic and manual + * configuration. + * + * @author Oliver Gierke + */ +public class SimpleMongoRepositoryConfiguration + extends + RepositoryConfig { + + private static final String MONGO_TEMPLATE_REF = "mongo-template-ref"; + private static final String DEFAULT_MONGO_TEMPLATE_REF = "mongoTemplate"; + + + /** + * Creates a new {@link SimpleMongoRepositoryConfiguration} for the given + * {@link Element}. + * + * @param repositoriesElement + * @param defaultRepositoryFactoryBeanClassName + */ + protected SimpleMongoRepositoryConfiguration(Element repositoriesElement) { + + super(repositoriesElement, MongoRepositoryFactoryBean.class.getName()); + } + + + /** + * Returns the bean name of the {@link MongoTemplate} to be referenced. + * + * @return + */ + public String getMongoTemplateRef() { + + String templateRef = getSource().getAttribute(MONGO_TEMPLATE_REF); + return StringUtils.hasText(templateRef) ? templateRef + : DEFAULT_MONGO_TEMPLATE_REF; + } + + + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.GlobalRepositoryConfigInformation + * #getAutoconfigRepositoryInformation(java.lang.String) + */ + public MongoRepositoryConfiguration getAutoconfigRepositoryInformation( + String interfaceName) { + + return new AutomaticMongoRepositoryConfiguration(interfaceName, this); + } + + + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.GlobalRepositoryConfigInformation + * #getRepositoryBaseInterface() + */ + public Class getRepositoryBaseInterface() { + + return MongoRepository.class; + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.config.RepositoryConfig# + * createSingleRepositoryConfigInformationFor(org.w3c.dom.Element) + */ + @Override + protected MongoRepositoryConfiguration createSingleRepositoryConfigInformationFor( + Element element) { + + return new ManualMongoRepositoryConfiguration(element, this); + } + + /** + * Simple interface for configuration values specific to Mongo repositories. + * + * @author Oliver Gierke + */ + public static interface MongoRepositoryConfiguration + extends + SingleRepositoryConfigInformation { + + String getMongoTemplateRef(); + } + + /** + * Implements manual lookup of the additional attributes. + * + * @author Oliver Gierke + */ + private static class ManualMongoRepositoryConfiguration + extends + ManualRepositoryConfigInformation + implements MongoRepositoryConfiguration { + + /** + * Creates a new {@link ManualMongoRepositoryConfiguration} for the + * given {@link Element} and parent. + * + * @param element + * @param parent + */ + public ManualMongoRepositoryConfiguration(Element element, + SimpleMongoRepositoryConfiguration parent) { + + super(element, parent); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.document.mongodb.repository.config. + * SimpleMongoRepositoryConfiguration + * .MongoRepositoryConfiguration#getMongoTemplateRef() + */ + public String getMongoTemplateRef() { + + return getAttribute(MONGO_TEMPLATE_REF); + } + } + + /** + * Implements the lookup of the additional attributes during automatic + * configuration. + * + * @author Oliver Gierke + */ + private static class AutomaticMongoRepositoryConfiguration + extends + AutomaticRepositoryConfigInformation + implements MongoRepositoryConfiguration { + + /** + * Creates a new {@link AutomaticMongoRepositoryConfiguration} for the + * given interface and parent. + * + * @param interfaceName + * @param parent + */ + public AutomaticMongoRepositoryConfiguration(String interfaceName, + SimpleMongoRepositoryConfiguration parent) { + + super(interfaceName, parent); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.document.mongodb.repository.config. + * SimpleMongoRepositoryConfiguration + * .MongoRepositoryConfiguration#getMongoTemplateRef() + */ + public String getMongoTemplateRef() { + + return getParent().getMongoTemplateRef(); + } + } +} diff --git a/spring-data-mongodb/src/main/resources/META-INF/spring.handlers b/spring-data-mongodb/src/main/resources/META-INF/spring.handlers new file mode 100644 index 000000000..d13e19090 --- /dev/null +++ b/spring-data-mongodb/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/schema/data/mongo=org.springframework.data.document.mongodb.repository.config.MongoRepositoryNamespaceHandler diff --git a/spring-data-mongodb/src/main/resources/META-INF/spring.schemas b/spring-data-mongodb/src/main/resources/META-INF/spring.schemas new file mode 100644 index 000000000..4eac173e7 --- /dev/null +++ b/spring-data-mongodb/src/main/resources/META-INF/spring.schemas @@ -0,0 +1,2 @@ +http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd +http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd diff --git a/spring-data-mongodb/src/main/resources/META-INF/spring.tooling b/spring-data-mongodb/src/main/resources/META-INF/spring.tooling new file mode 100644 index 000000000..6dcd5320e --- /dev/null +++ b/spring-data-mongodb/src/main/resources/META-INF/spring.tooling @@ -0,0 +1,4 @@ +# Tooling related information for the Mongo DB namespace +http\://www.springframework.org/schema/data/mongo@name=Mongo Namespace +http\://www.springframework.org/schema/data/mongo@prefix=mongo +http\://www.springframework.org/schema/data/mongo@icon=org/springframework/jdbc/config/spring-jdbc.gif diff --git a/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd b/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd new file mode 100644 index 000000000..5781a02bc --- /dev/null +++ b/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/repository/config/spring-mongo-1.0.xsd @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + The reference to a MongoTemplate. Will default to 'mongoTemplate'. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java new file mode 100644 index 000000000..e8615d559 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java @@ -0,0 +1,83 @@ +package org.springframework.data.document.mongodb.repository; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort.Direction; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Base class for tests for {@link PersonRepository}. + * + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +public abstract class AbstractPersonRepositoryIntegrationTests { + + @Autowired + protected PersonRepository repository; + Person dave, carter, boyd,stefan,leroi; + + @Before + public void setUp() { + + repository.deleteAll(); + + dave = new Person("Dave", "Matthews"); + carter = new Person("Carter", "Beauford"); + boyd = new Person("Boyd", "Tinsley"); + stefan = new Person("Stefan", "Lessard"); + leroi = new Person("Leroi", "Moore"); + + repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi)); + } + + @Test + public void findsPersonsByLastname() throws Exception { + + List result = repository.findByLastname("Beauford"); + assertThat(result.size(), is(1)); + assertThat(result, hasItem(carter)); + } + + @Test + public void findsPersonsByFirstnameLike() throws Exception { + + List result = repository.findByFirstnameLike("Bo*"); + assertThat(result.size(), is(1)); + assertThat(result, hasItem(boyd)); + } + + @Test + public void findsPagedPersons() throws Exception { + + Page result = + repository.findAll(new PageRequest(1, 2, Direction.ASC, + "lastname")); + assertThat(result.isFirstPage(), is(false)); + assertThat(result.isLastPage(), is(false)); + assertThat(result, hasItems(dave, leroi)); + } + + @Test + public void executesPagedFinderCorrectly() throws Exception { + + Page page = + repository.findByLastnameLike("*a*", new PageRequest(0, 2, + Direction.ASC, "lastname")); + assertThat(page.isFirstPage(), is(true)); + assertThat(page.isLastPage(), is(false)); + assertThat(page.getNumberOfElements(), is(2)); + assertThat(page, hasItems(carter, stefan)); + } + +} \ No newline at end of file diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests.java index 361c65308..5a1c718bc 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests.java @@ -15,21 +15,7 @@ */ package org.springframework.data.document.mongodb.repository; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Sort.Direction; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** @@ -37,70 +23,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * * @author Oliver Gierke */ -@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration -public class PersonRepositoryIntegrationTests { - - @Autowired - PersonRepository repository; - - Person dave, carter, boyd, stefan, leroi; - - - @Before - public void setUp() { - - repository.deleteAll(); - - dave = new Person("Dave", "Matthews"); - carter = new Person("Carter", "Beauford"); - boyd = new Person("Boyd", "Tinsley"); - stefan = new Person("Stefan", "Lessard"); - leroi = new Person("Leroi", "Moore"); - - repository.save(Arrays.asList(dave, carter, boyd, stefan, leroi)); - } - - - @Test - public void findsPersonsByLastname() throws Exception { - - List result = repository.findByLastname("Beauford"); - assertThat(result.size(), is(1)); - assertThat(result, hasItem(carter)); - } - - - @Test - public void findsPersonsByFirstnameLike() throws Exception { - - List result = repository.findByFirstnameLike("Bo*"); - assertThat(result.size(), is(1)); - assertThat(result, hasItem(boyd)); - } - - - @Test - public void findsPagedPersons() throws Exception { - - Page result = - repository.findAll(new PageRequest(1, 2, Direction.ASC, - "lastname")); - assertThat(result.isFirstPage(), is(false)); - assertThat(result.isLastPage(), is(false)); - assertThat(result, hasItems(dave, leroi)); - } - - - @Test - public void executesPagedFinderCorrectly() throws Exception { - - Page page = - repository.findByLastnameLike("*a*", new PageRequest(0, 2, - Direction.ASC, "lastname")); - assertThat(page.isFirstPage(), is(true)); - assertThat(page.isLastPage(), is(false)); - assertThat(page.getNumberOfElements(), is(2)); - assertThat(page, hasItems(carter, stefan)); - } +public class PersonRepositoryIntegrationTests extends + AbstractPersonRepositoryIntegrationTests { } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests.java new file mode 100644 index 000000000..0cca67cef --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests.java @@ -0,0 +1,17 @@ +package org.springframework.data.document.mongodb.repository.config; + +import org.springframework.data.document.mongodb.repository.AbstractPersonRepositoryIntegrationTests; +import org.springframework.test.context.ContextConfiguration; + + +/** + * Test class using the namespace configuration to set up the repository + * instance. + * + * @author Oliver Gierke + */ +@ContextConfiguration +public class MongoNamespaceIntegrationTests extends + AbstractPersonRepositoryIntegrationTests { + +} diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml index 462d3b2e2..45d609dee 100644 --- a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml @@ -6,7 +6,7 @@ - + diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml new file mode 100644 index 000000000..6df1f15c5 --- /dev/null +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests-context.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/infrastructure.xml b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/infrastructure.xml index e216cb973..c34a0a5f8 100644 --- a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/infrastructure.xml +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/infrastructure.xml @@ -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"> - +