Skip to content

Commit 715cbcd

Browse files
committed
Add test to verify Map adapter customization.
See #358.
1 parent 0e33b03 commit 715cbcd

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/test/java/org/springframework/data/map/repository/config/MapRepositoriesConfigurationExtensionIntegrationTests.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package org.springframework.data.map.repository.config;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import lombok.Data;
1922

2023
import java.util.Arrays;
2124
import java.util.concurrent.ConcurrentHashMap;
@@ -27,8 +30,14 @@
2730
import org.springframework.context.ConfigurableApplicationContext;
2831
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2932
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.ComponentScan;
3034
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.context.annotation.FilterType;
36+
import org.springframework.data.annotation.Id;
37+
import org.springframework.data.keyvalue.core.KeyValueAdapter;
38+
import org.springframework.data.keyvalue.core.KeyValueOperations;
3139
import org.springframework.data.keyvalue.core.KeyValueTemplate;
40+
import org.springframework.data.keyvalue.repository.KeyValueRepository;
3241
import org.springframework.data.map.MapKeyValueAdapter;
3342
import org.springframework.test.util.ReflectionTestUtils;
3443

@@ -37,6 +46,7 @@
3746
*
3847
* @author Oliver Gierke
3948
* @author Christoph Strobl
49+
* @author Mark Paluch
4050
*/
4151
class MapRepositoriesConfigurationExtensionIntegrationTests {
4252

@@ -51,7 +61,7 @@ void registersDefaultTemplateIfReferenceNotCustomized() {
5161
}
5262

5363
@Test // DATAKV-86
54-
void doesNotRegisterDefaulttemplateIfReferenceIsCustomized() {
64+
void doesNotRegisterDefaultTemplateIfReferenceIsCustomized() {
5565

5666
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
5767
ConfigWithCustomTemplateReference.class);
@@ -61,6 +71,19 @@ void doesNotRegisterDefaulttemplateIfReferenceIsCustomized() {
6171
context.close();
6272
}
6373

74+
@Test // GH-358
75+
void shouldUseCustomAdapter() {
76+
77+
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
78+
ConfigWithOverriddenTemplateReference.class);
79+
80+
PersonRepository repository = context.getBean(PersonRepository.class);
81+
82+
assertThatThrownBy(() -> repository.findById("foo")).hasRootCauseInstanceOf(IllegalStateException.class).hasMessageContaining("Mock!");
83+
84+
context.close();
85+
}
86+
6487
@Test // DATAKV-87
6588
void considersMapTypeConfiguredOnAnnotation() {
6689
assertKeyValueTemplateWithAdapterFor(ConcurrentSkipListMap.class,
@@ -90,6 +113,28 @@ static class Config {}
90113
@EnableMapRepositories(keyValueTemplateRef = "foo")
91114
static class ConfigWithCustomTemplateReference {}
92115

116+
@Configuration
117+
@EnableMapRepositories(considerNestedRepositories = true,
118+
includeFilters = @ComponentScan.Filter(value = PersonRepository.class, type = FilterType.ASSIGNABLE_TYPE))
119+
static class ConfigWithOverriddenTemplateReference {
120+
121+
@Bean
122+
public KeyValueOperations mapKeyValueTemplate() {
123+
return new KeyValueTemplate(keyValueAdapter());
124+
}
125+
126+
@Bean
127+
public KeyValueAdapter keyValueAdapter() {
128+
129+
KeyValueAdapter mock = mock(KeyValueAdapter.class);
130+
131+
when(mock.get(any(), anyString(), any())).thenThrow(new IllegalStateException("Mock!"));
132+
133+
return mock;
134+
}
135+
136+
}
137+
93138
@Configuration
94139
@EnableMapRepositories(mapType = ConcurrentSkipListMap.class)
95140
static class ConfigWithCustomizedMapType {}
@@ -103,4 +148,14 @@ public KeyValueTemplate mapKeyValueTemplate() {
103148
return new KeyValueTemplate(new MapKeyValueAdapter());
104149
}
105150
}
151+
152+
interface PersonRepository extends KeyValueRepository<Person, String> {
153+
154+
}
155+
156+
@Data
157+
static class Person {
158+
@Id String id;
159+
String name;
160+
}
106161
}

0 commit comments

Comments
 (0)