Skip to content

Commit e0d817d

Browse files
DATAREDIS-425 - Add repository identifying annotation to configuration extension.
1 parent 69db5ad commit e0d817d

File tree

4 files changed

+149
-8
lines changed

4 files changed

+149
-8
lines changed

src/main/java/org/springframework/data/redis/repository/configuration/RedisRepositoryConfigurationExtension.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.springframework.data.redis.repository.configuration;
1717

18+
import java.lang.annotation.Annotation;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
1822
import org.springframework.beans.DirectFieldAccessor;
1923
import org.springframework.beans.MutablePropertyValues;
2024
import org.springframework.beans.factory.config.BeanDefinition;
@@ -26,6 +30,7 @@
2630
import org.springframework.beans.factory.support.RootBeanDefinition;
2731
import org.springframework.core.annotation.AnnotationAttributes;
2832
import org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension;
33+
import org.springframework.data.redis.core.RedisHash;
2934
import org.springframework.data.redis.core.RedisKeyValueAdapter;
3035
import org.springframework.data.redis.core.RedisKeyValueTemplate;
3136
import org.springframework.data.redis.core.convert.CustomConversions;
@@ -205,4 +210,13 @@ private RootBeanDefinition createRedisConverterDefinition() {
205210
return beanDef;
206211
}
207212

213+
/*
214+
* (non-Javadoc)
215+
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingAnnotations()
216+
*/
217+
@Override
218+
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
219+
return Collections.<Class<? extends Annotation>> singleton(RedisHash.class);
220+
}
221+
208222
}

src/test/java/org/springframework/data/redis/repository/RedisRepositoryIntegrationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import org.junit.runner.RunWith;
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.ComponentScan;
3536
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.context.annotation.FilterType;
3638
import org.springframework.data.annotation.Id;
3739
import org.springframework.data.annotation.Reference;
3840
import org.springframework.data.domain.Page;
@@ -61,7 +63,8 @@ public class RedisRepositoryIntegrationTests {
6163

6264
@Configuration
6365
@EnableRedisRepositories(considerNestedRepositories = true, indexConfiguration = MyIndexConfiguration.class,
64-
keyspaceConfiguration = MyKeyspaceConfiguration.class)
66+
keyspaceConfiguration = MyKeyspaceConfiguration.class,
67+
includeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*PersonRepository") })
6568
static class Config {
6669

6770
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.repository.configuration;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.util.Collection;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.springframework.core.env.Environment;
25+
import org.springframework.core.env.StandardEnvironment;
26+
import org.springframework.core.io.ResourceLoader;
27+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
28+
import org.springframework.core.type.StandardAnnotationMetadata;
29+
import org.springframework.data.annotation.Id;
30+
import org.springframework.data.keyvalue.repository.KeyValueRepository;
31+
import org.springframework.data.redis.core.RedisHash;
32+
import org.springframework.data.repository.Repository;
33+
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
34+
import org.springframework.data.repository.config.RepositoryConfiguration;
35+
import org.springframework.data.repository.config.RepositoryConfigurationSource;
36+
37+
/**
38+
* @author Christoph Strobl
39+
*/
40+
public class RedisRepositoryConfigurationExtensionUnitTests {
41+
42+
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true);
43+
ResourceLoader loader = new PathMatchingResourcePatternResolver();
44+
Environment environment = new StandardEnvironment();
45+
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
46+
EnableRedisRepositories.class, loader, environment);
47+
48+
RedisRepositoryConfigurationExtension extension;
49+
50+
@Before
51+
public void setUp() {
52+
extension = new RedisRepositoryConfigurationExtension();
53+
}
54+
55+
/**
56+
* @see DATAREDIS-425
57+
*/
58+
@Test
59+
public void isStrictMatchIfDomainTypeIsAnnotatedWithDocument() {
60+
assertHasRepo(SampleRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
61+
}
62+
63+
/**
64+
* @see DATAREDIS-425
65+
*/
66+
@Test
67+
public void isStrictMatchIfRepositoryExtendsStoreSpecificBase() {
68+
assertHasRepo(StoreRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
69+
}
70+
71+
/**
72+
* @see DATAREDIS-425
73+
*/
74+
@Test
75+
public void isNotStrictMatchIfDomainTypeIsNotAnnotatedWithDocument() {
76+
77+
assertDoesNotHaveRepo(UnannotatedRepository.class,
78+
extension.getRepositoryConfigurations(configurationSource, loader, true));
79+
}
80+
81+
private static void assertDoesNotHaveRepo(Class<?> repositoryInterface,
82+
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) {
83+
84+
try {
85+
86+
assertHasRepo(repositoryInterface, configs);
87+
fail("Expected not to find config for repository interface ".concat(repositoryInterface.getName()));
88+
} catch (AssertionError error) {
89+
// repo not there. we're fine.
90+
}
91+
}
92+
93+
private static void assertHasRepo(Class<?> repositoryInterface,
94+
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) {
95+
96+
for (RepositoryConfiguration<?> config : configs) {
97+
if (config.getRepositoryInterface().equals(repositoryInterface.getName())) {
98+
return;
99+
}
100+
}
101+
102+
fail("Expected to find config for repository interface ".concat(repositoryInterface.getName()).concat(" but got ")
103+
.concat(configs.toString()));
104+
}
105+
106+
@EnableRedisRepositories(considerNestedRepositories = true)
107+
static class Config {
108+
109+
}
110+
111+
@RedisHash
112+
static class Sample {
113+
@Id String id;
114+
}
115+
116+
interface SampleRepository extends Repository<Sample, Long> {}
117+
118+
interface UnannotatedRepository extends Repository<Object, Long> {}
119+
120+
interface StoreRepository extends KeyValueRepository<Object, Long> {}
121+
}

src/test/java/org/springframework/data/redis/repository/configuration/RedisRepositoryConfigurationUnitTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
import org.springframework.beans.factory.annotation.Autowired;
2929
import org.springframework.context.ApplicationContext;
3030
import org.springframework.context.annotation.Bean;
31-
import org.springframework.data.annotation.Id;
31+
import org.springframework.context.annotation.ComponentScan;
32+
import org.springframework.context.annotation.FilterType;
3233
import org.springframework.data.redis.connection.RedisConnection;
3334
import org.springframework.data.redis.connection.RedisConnectionFactory;
3435
import org.springframework.data.redis.core.RedisHash;
@@ -67,7 +68,8 @@ public class RedisRepositoryConfigurationUnitTests {
6768
@ContextConfiguration(classes = { ContextWithCustomReferenceResolver.Config.class })
6869
public static class ContextWithCustomReferenceResolver {
6970

70-
@EnableRedisRepositories(considerNestedRepositories = true)
71+
@EnableRedisRepositories(considerNestedRepositories = true,
72+
includeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = { ".*ContextSampleRepository" }) })
7173
static class Config {
7274

7375
@Bean
@@ -104,7 +106,8 @@ public void shouldPickUpReferenceResolver() {
104106
@ContextConfiguration(classes = { ContextWithoutCustomization.Config.class })
105107
public static class ContextWithoutCustomization {
106108

107-
@EnableRedisRepositories(considerNestedRepositories = true)
109+
@EnableRedisRepositories(considerNestedRepositories = true,
110+
includeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = { ".*ContextSampleRepository" }) })
108111
static class Config {
109112

110113
@Bean
@@ -120,7 +123,7 @@ static class Config {
120123
*/
121124
@Test
122125
public void shouldInitWithDefaults() {
123-
assertThat(ctx.getBean(SampleRepository.class), is(notNullValue()));
126+
assertThat(ctx.getBean(ContextSampleRepository.class), is(notNullValue()));
124127
}
125128

126129
/**
@@ -129,7 +132,7 @@ public void shouldInitWithDefaults() {
129132
@Test
130133
public void shouldRegisterDefaultBeans() {
131134

132-
assertThat(ctx.getBean(SampleRepository.class), is(notNullValue()));
135+
assertThat(ctx.getBean(ContextSampleRepository.class), is(notNullValue()));
133136
assertThat(ctx.getBean("redisKeyValueAdapter"), is(notNullValue()));
134137
assertThat(ctx.getBean("redisCustomConversions"), is(notNullValue()));
135138
assertThat(ctx.getBean("redisReferenceResolver"), is(notNullValue()));
@@ -138,8 +141,8 @@ public void shouldRegisterDefaultBeans() {
138141

139142
@RedisHash
140143
static class Sample {
141-
@Id String id;
144+
String id;
142145
}
143146

144-
interface SampleRepository extends Repository<Sample, Long> {}
147+
interface ContextSampleRepository extends Repository<Sample, Long> {}
145148
}

0 commit comments

Comments
 (0)