From 97bcf28a04a286f9e0f5ba37151a38b19ec4b35b Mon Sep 17 00:00:00 2001 From: Kevin Leturc Date: Fri, 9 Jan 2015 00:49:24 +0100 Subject: [PATCH] DATAES-135 - Add a flag in configuration to consider nested repositories --- .../EnableElasticsearchRepositories.java | 9 ++- .../EnableElasticsearchRepositoriesTests.java | 18 +++++- ...eNestedElasticsearchRepositoriesTests.java | 61 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/config/EnableElasticsearchRepositories.java b/src/main/java/org/springframework/data/elasticsearch/repository/config/EnableElasticsearchRepositories.java index c6f2a77d4..e18297dcb 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/config/EnableElasticsearchRepositories.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/config/EnableElasticsearchRepositories.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 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. @@ -29,6 +29,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key; * * @author Rizwan Idrees * @author Mohsin Husen + * @author Kevin Leturc */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @@ -111,4 +112,10 @@ public @interface EnableElasticsearchRepositories { * @return */ String elasticsearchTemplateRef() default "elasticsearchTemplate"; + + /** + * Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the + * repositories infrastructure. + */ + boolean considerNestedRepositories() default false; } diff --git a/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java b/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java index e7101f08a..a5ac78808 100644 --- a/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-15 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. @@ -29,15 +29,18 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.entities.SampleEntity; import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; +import org.springframework.data.repository.Repository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Rizwan Idrees * @author Mohsin Husen + * @author Kevin Leturc */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @@ -51,7 +54,8 @@ public class EnableElasticsearchRepositoriesTests implements ApplicationContextA } @Configuration - @EnableElasticsearchRepositories(basePackages = "org.springframework.data.elasticsearch.repositories.sample") + @EnableElasticsearchRepositories(basePackages = {"org.springframework.data.elasticsearch.repositories.sample", + "org.springframework.data.elasticsearch.config"}) static class Config { @Bean @@ -63,6 +67,11 @@ public class EnableElasticsearchRepositoriesTests implements ApplicationContextA @Autowired private SampleElasticsearchRepository repository; + @Autowired(required = false) + private SampleRepository nestedRepository; + + interface SampleRepository extends Repository {}; + @Test public void bootstrapsRepository() { assertThat(repository, is(notNullValue())); @@ -79,4 +88,9 @@ public class EnableElasticsearchRepositoriesTests implements ApplicationContextA assertThat(beanNamesForType.length, is(1)); assertThat(beanNamesForType[0], is("sampleElasticsearchRepository")); } + + @Test + public void hasNotNestedRepository() { + assertNull(nestedRepository); + } } diff --git a/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java b/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java new file mode 100644 index 000000000..f1c89c3cb --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/config/EnableNestedElasticsearchRepositoriesTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2015 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.elasticsearch.config; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.entities.SampleEntity; +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; +import org.springframework.data.repository.Repository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.elasticsearch.node.NodeBuilder.nodeBuilder; +import static org.junit.Assert.assertNotNull; + +/** + * @author Kevin Leturc + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class EnableNestedElasticsearchRepositoriesTests { + + @Configuration + @EnableElasticsearchRepositories(basePackages = {"org.springframework.data.elasticsearch.repositories.sample", + "org.springframework.data.elasticsearch.config"}, considerNestedRepositories = true) + static class Config { + + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchTemplate(nodeBuilder().local(true).clusterName("testCluster2").node().client()); + } + } + + @Autowired(required = false) + private SampleRepository nestedRepository; + + interface SampleRepository extends Repository {}; + + @Test + public void hasNestedRepository() { + assertNotNull(nestedRepository); + } +}