16
16
package org .springframework .data .map .repository .config ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
+ import static org .mockito .Mockito .*;
20
+
21
+ import lombok .Data ;
19
22
20
23
import java .util .Arrays ;
21
24
import java .util .concurrent .ConcurrentHashMap ;
27
30
import org .springframework .context .ConfigurableApplicationContext ;
28
31
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
29
32
import org .springframework .context .annotation .Bean ;
33
+ import org .springframework .context .annotation .ComponentScan ;
30
34
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 ;
31
39
import org .springframework .data .keyvalue .core .KeyValueTemplate ;
40
+ import org .springframework .data .keyvalue .repository .KeyValueRepository ;
32
41
import org .springframework .data .map .MapKeyValueAdapter ;
33
42
import org .springframework .test .util .ReflectionTestUtils ;
34
43
37
46
*
38
47
* @author Oliver Gierke
39
48
* @author Christoph Strobl
49
+ * @author Mark Paluch
40
50
*/
41
51
class MapRepositoriesConfigurationExtensionIntegrationTests {
42
52
@@ -51,7 +61,7 @@ void registersDefaultTemplateIfReferenceNotCustomized() {
51
61
}
52
62
53
63
@ Test // DATAKV-86
54
- void doesNotRegisterDefaulttemplateIfReferenceIsCustomized () {
64
+ void doesNotRegisterDefaultTemplateIfReferenceIsCustomized () {
55
65
56
66
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (
57
67
ConfigWithCustomTemplateReference .class );
@@ -61,6 +71,19 @@ void doesNotRegisterDefaulttemplateIfReferenceIsCustomized() {
61
71
context .close ();
62
72
}
63
73
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
+
64
87
@ Test // DATAKV-87
65
88
void considersMapTypeConfiguredOnAnnotation () {
66
89
assertKeyValueTemplateWithAdapterFor (ConcurrentSkipListMap .class ,
@@ -90,6 +113,28 @@ static class Config {}
90
113
@ EnableMapRepositories (keyValueTemplateRef = "foo" )
91
114
static class ConfigWithCustomTemplateReference {}
92
115
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
+
93
138
@ Configuration
94
139
@ EnableMapRepositories (mapType = ConcurrentSkipListMap .class )
95
140
static class ConfigWithCustomizedMapType {}
@@ -103,4 +148,14 @@ public KeyValueTemplate mapKeyValueTemplate() {
103
148
return new KeyValueTemplate (new MapKeyValueAdapter ());
104
149
}
105
150
}
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
+ }
106
161
}
0 commit comments