15
15
*/
16
16
package org .springframework .data .neo4j .test ;
17
17
18
+ import io .netty .channel .EventLoopGroup ;
19
+ import io .netty .channel .nio .NioEventLoopGroup ;
20
+ import io .netty .util .concurrent .DefaultThreadFactory ;
18
21
import org .apache .commons .logging .Log ;
19
22
import org .junit .jupiter .api .extension .BeforeAllCallback ;
20
23
import org .junit .jupiter .api .extension .BeforeEachCallback ;
23
26
import org .junit .platform .commons .support .ReflectionSupport ;
24
27
import org .neo4j .driver .AccessMode ;
25
28
import org .neo4j .driver .AuthToken ;
29
+ import org .neo4j .driver .AuthTokenManagers ;
26
30
import org .neo4j .driver .AuthTokens ;
27
31
import org .neo4j .driver .Config ;
28
32
import org .neo4j .driver .Driver ;
29
- import org .neo4j .driver .GraphDatabase ;
30
33
import org .neo4j .driver .Logging ;
31
34
import org .neo4j .driver .Record ;
32
35
import org .neo4j .driver .Session ;
33
36
import org .neo4j .driver .SessionConfig ;
37
+ import org .neo4j .driver .internal .DriverFactory ;
38
+ import org .neo4j .driver .internal .SecuritySettings ;
39
+ import org .neo4j .driver .internal .security .SecurityPlans ;
34
40
import org .springframework .core .log .LogMessage ;
35
41
import org .springframework .lang .Nullable ;
36
42
import org .testcontainers .containers .Neo4jContainer ;
37
43
import org .testcontainers .utility .TestcontainersConfiguration ;
38
44
39
45
import java .lang .reflect .Field ;
40
46
import java .lang .reflect .Modifier ;
47
+ import java .net .URI ;
41
48
import java .util .List ;
42
49
import java .util .Locale ;
43
50
import java .util .Map ;
@@ -82,12 +89,14 @@ public class Neo4jExtension implements BeforeAllCallback, BeforeEachCallback {
82
89
private static final String SYS_PROPERTY_FORCE_CONTAINER_REUSE = "SDN_FORCE_REUSE_OF_CONTAINERS" ;
83
90
private static final Log log = org .apache .commons .logging .LogFactory .getLog (Neo4jExtension .class );
84
91
85
- private static Set <String > COMMUNITY_EDITION_INDICATOR = Set .of ("community" );
92
+ private static final Set <String > COMMUNITY_EDITION_INDICATOR = Set .of ("community" );
93
+ private static final Set <String > COMMERCIAL_EDITION_INDICATOR = Set .of ("commercial" , "enterprise" );
86
94
87
- private static Set < String > COMMERCIAL_EDITION_INDICATOR = Set . of ( "commercial" , "enterprise" );
95
+ private static final EventLoopGroup EVENT_LOOP_GROUP = new NioEventLoopGroup ( new DefaultThreadFactory ( Neo4jExtension . class , true ) );
88
96
89
97
@ Override
90
98
public void beforeAll (ExtensionContext context ) throws Exception {
99
+
91
100
List <Field > injectableFields = ReflectionSupport .findFields (context .getRequiredTestClass (),
92
101
field -> Modifier .isStatic (field .getModifiers ()) && field .getType () == Neo4jConnectionSupport .class ,
93
102
HierarchyTraversalMode .BOTTOM_UP );
@@ -163,6 +172,8 @@ private void checkRequiredFeatures(Neo4jConnectionSupport neo4jConnectionSupport
163
172
*/
164
173
public static final class Neo4jConnectionSupport implements ExtensionContext .Store .CloseableResource {
165
174
175
+ private final DriverFactory driverFactory ;
176
+
166
177
public final String url ;
167
178
168
179
public final AuthToken authToken ;
@@ -182,6 +193,7 @@ public Neo4jConnectionSupport(String url, AuthToken authToken) {
182
193
this .config = Config .builder ().withLogging (Logging .slf4j ())
183
194
.withMaxConnectionPoolSize (Runtime .getRuntime ().availableProcessors ())
184
195
.build ();
196
+ this .driverFactory = new DriverFactory ();
185
197
}
186
198
187
199
/**
@@ -198,14 +210,21 @@ public Driver getDriver() {
198
210
synchronized (this ) {
199
211
driver = this .driverInstance ;
200
212
if (!isUsable (driver )) {
201
- this .driverInstance = GraphDatabase . driver ( url , authToken , config );
213
+ this .driverInstance = createDriverInstance ( );
202
214
driver = this .driverInstance ;
203
215
}
204
216
}
205
217
}
206
218
return driver ;
207
219
}
208
220
221
+ private Driver createDriverInstance () {
222
+ var uri = URI .create (url );
223
+ var settings = new SecuritySettings (config .encrypted (), config .trustStrategy ());
224
+ var securityPlan = SecurityPlans .createSecurityPlan (settings , uri .getScheme ());
225
+ return this .driverFactory .newInstance (uri , AuthTokenManagers .basic (() -> authToken ), config , securityPlan , EVENT_LOOP_GROUP , null );
226
+ }
227
+
209
228
/**
210
229
* A driver is usable if it's not null and can verify its connectivity. This method force closes
211
230
* the bean if the connectivity cannot be verified to avoid having a netty pool dangling around.
0 commit comments