16
16
17
17
package org .springframework .boot .autoconfigure .data .neo4j ;
18
18
19
+ import java .util .function .Predicate ;
20
+
21
+ import com .github .benmanes .caffeine .cache .Caffeine ;
22
+ import org .assertj .core .api .Condition ;
19
23
import org .junit .Test ;
20
24
import org .neo4j .ogm .session .Session ;
21
25
import org .neo4j .ogm .session .SessionFactory ;
29
33
import org .springframework .boot .autoconfigure .data .neo4j .country .Country ;
30
34
import org .springframework .boot .autoconfigure .domain .EntityScan ;
31
35
import org .springframework .boot .autoconfigure .transaction .TransactionAutoConfiguration ;
36
+ import org .springframework .boot .test .context .FilteredClassLoader ;
37
+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
32
38
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
39
+ import org .springframework .context .ConfigurableApplicationContext ;
33
40
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
34
41
import org .springframework .context .annotation .Bean ;
35
42
import org .springframework .context .annotation .Configuration ;
43
+ import org .springframework .data .neo4j .annotation .EnableBookmarkManagement ;
44
+ import org .springframework .data .neo4j .bookmark .BookmarkManager ;
36
45
import org .springframework .data .neo4j .mapping .Neo4jMappingContext ;
37
46
import org .springframework .data .neo4j .transaction .Neo4jTransactionManager ;
38
47
import org .springframework .data .neo4j .web .support .OpenSessionInViewInterceptor ;
48
+ import org .springframework .web .context .WebApplicationContext ;
39
49
40
50
import static org .assertj .core .api .Assertions .assertThat ;
41
51
import static org .mockito .ArgumentMatchers .any ;
51
61
* @author Vince Bickers
52
62
* @author Andy Wilkinson
53
63
* @author Kazuki Shimizu
64
+ * @author Michael Simons
54
65
*/
55
66
public class Neo4jDataAutoConfigurationTests {
56
67
@@ -69,6 +80,7 @@ public void defaultConfiguration() {
69
80
assertThat (context ).hasSingleBean (SessionFactory .class );
70
81
assertThat (context ).hasSingleBean (Neo4jTransactionManager .class );
71
82
assertThat (context ).hasSingleBean (OpenSessionInViewInterceptor .class );
83
+ assertThat (context ).doesNotHaveBean (BookmarkManager .class );
72
84
});
73
85
}
74
86
@@ -146,6 +158,41 @@ public void eventListenersAreAutoRegistered() {
146
158
});
147
159
}
148
160
161
+ @ Test
162
+ public void providesARequestScopedBookmarkManangerIfNecessaryAndPossible () {
163
+ Predicate <ConfigurableApplicationContext > hasRequestScopedBookmarkManager = (
164
+ context ) -> context .getBeanFactory () //
165
+ .getBeanDefinition ("scopedTarget."
166
+ + Neo4jBookmarkManagementConfiguration .BOOKMARK_MANAGER_BEAN_NAME ) //
167
+ .getScope () //
168
+ .equals (WebApplicationContext .SCOPE_REQUEST );
169
+
170
+ this .contextRunner
171
+ .withUserConfiguration (BookmarkManagementEnabledConfiguration .class )
172
+ .run ((context ) -> assertThat (context )
173
+ .satisfies (new Condition <>(hasRequestScopedBookmarkManager ,
174
+ "hasRequestScopedBookmarkManager" )));
175
+ }
176
+
177
+ @ Test
178
+ public void providesASingletonScopedBookmarkManangerIfNecessaryAndPossible () {
179
+ new ApplicationContextRunner ()
180
+ .withUserConfiguration (TestConfiguration .class ,
181
+ BookmarkManagementEnabledConfiguration .class )
182
+ .withConfiguration (AutoConfigurations .of (Neo4jDataAutoConfiguration .class ,
183
+ TransactionAutoConfiguration .class ))
184
+ .run ((context ) -> assertThat (context )
185
+ .hasSingleBean (BookmarkManager .class ));
186
+ }
187
+
188
+ @ Test
189
+ public void doesNotProvideABookmarkManagerIfNotPossible () {
190
+ this .contextRunner .withClassLoader (new FilteredClassLoader (Caffeine .class ))
191
+ .withUserConfiguration (BookmarkManagementEnabledConfiguration .class )
192
+ .run ((context ) -> assertThat (context )
193
+ .doesNotHaveBean (BookmarkManager .class ));
194
+ }
195
+
149
196
private static void assertDomainTypesDiscovered (Neo4jMappingContext mappingContext ,
150
197
Class <?>... types ) {
151
198
for (Class <?> type : types ) {
@@ -180,6 +227,12 @@ public org.neo4j.ogm.config.Configuration myConfiguration() {
180
227
181
228
}
182
229
230
+ @ Configuration
231
+ @ EnableBookmarkManagement
232
+ static class BookmarkManagementEnabledConfiguration {
233
+
234
+ }
235
+
183
236
@ Configuration
184
237
static class EventListenerConfiguration {
185
238
0 commit comments