From b4dae0ea09e4d6dcf9660ec48b1f40d94fbccf7e Mon Sep 17 00:00:00 2001 From: Caleb Cushing Date: Wed, 24 Aug 2016 18:29:01 -0500 Subject: [PATCH 1/3] allow bolt driver autoconfiguration for neo4j gh-6690 Signed-off-by: Caleb Cushing --- .../autoconfigure/data/neo4j/Neo4jProperties.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java index f85b56b83a9c..7bb97ecd3549 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java @@ -45,6 +45,10 @@ public class Neo4jProperties implements ApplicationContextAware { static final String DEFAULT_HTTP_URI = "http://localhost:7474"; + static final String BOLT_DRIVER = "org.neo4j.ogm.drivers.bolt.driver.BoltDriver"; + + static final String DEFAULT_BOLT_URI = "bolt://localhost:7687"; + /** * URI used by the driver. Auto-detected by default. */ @@ -151,6 +155,9 @@ private String deduceDriverFromUri() { if ("http".equals(scheme)) { return HTTP_DRIVER; } + if ("bolt".equals(scheme)) { + return BOLT_DRIVER; + } throw new IllegalArgumentException( "Could not deduce driver to use based on URI '" + uri + "'"); } @@ -166,6 +173,11 @@ private void configureDriverWithDefaults(DriverConfiguration driverConfiguration driverConfiguration.setDriverClassName(EMBEDDED_DRIVER); return; } + if (ClassUtils.isPresent(BOLT_DRIVER, this.classLoader)) { + driverConfiguration.setDriverClassName(BOLT_DRIVER); + driverConfiguration.setURI( DEFAULT_BOLT_URI ); + return; + } driverConfiguration.setDriverClassName(HTTP_DRIVER); driverConfiguration.setURI(DEFAULT_HTTP_URI); } From 6644d359b516e4720c902ebff97678ab60257d88 Mon Sep 17 00:00:00 2001 From: Caleb Cushing Date: Wed, 24 Aug 2016 19:49:07 -0500 Subject: [PATCH 2/3] update tests to scale gh-6690 Signed-off-by: Caleb Cushing --- .../data/neo4j/Neo4jPropertiesTests.java | 64 ++++++++++++++----- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java index 5e876435121f..e854dc12b3dd 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java @@ -18,6 +18,8 @@ import java.net.URL; import java.net.URLClassLoader; +import java.util.EnumSet; +import java.util.Set; import com.hazelcast.util.Base64; import org.junit.After; @@ -29,7 +31,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; - import static org.assertj.core.api.Assertions.assertThat; /** @@ -50,14 +51,14 @@ public void close() { @Test public void defaultUseEmbeddedInMemoryIfAvailable() { - Neo4jProperties properties = load(true); + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED )); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, null); } @Test public void defaultUseHttpDriverIfEmbeddedDriverIsNotAvailable() { - Neo4jProperties properties = load(false); + Neo4jProperties properties = load(EnumSet.noneOf( AvailableDriver.class )); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, Neo4jProperties.DEFAULT_HTTP_URI); @@ -65,7 +66,7 @@ public void defaultUseHttpDriverIfEmbeddedDriverIsNotAvailable() { @Test public void httpUriUseHttpServer() { - Neo4jProperties properties = load(true, + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), "spring.data.neo4j.uri=http://localhost:7474"); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, "http://localhost:7474"); @@ -73,7 +74,7 @@ public void httpUriUseHttpServer() { @Test public void fileUriUseEmbeddedServer() { - Neo4jProperties properties = load(true, + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), "spring.data.neo4j.uri=file://var/tmp/graph.db"); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, @@ -82,7 +83,7 @@ public void fileUriUseEmbeddedServer() { @Test public void credentialsAreSet() { - Neo4jProperties properties = load(true, + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), "spring.data.neo4j.uri=http://localhost:7474", "spring.data.neo4j.username=user", "spring.data.neo4j.password=secret"); Configuration configuration = properties.createConfiguration(); @@ -92,7 +93,7 @@ public void credentialsAreSet() { @Test public void credentialsAreSetFromUri() { - Neo4jProperties properties = load(true, + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), "spring.data.neo4j.uri=http://user:secret@my-server:7474"); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, @@ -102,7 +103,7 @@ public void credentialsAreSetFromUri() { @Test public void embeddedModeDisabledUseHttpUri() { - Neo4jProperties properties = load(true, + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), "spring.data.neo4j.embedded.enabled=false"); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, @@ -111,7 +112,7 @@ public void embeddedModeDisabledUseHttpUri() { @Test public void embeddedModeWithRelativeLocation() { - Neo4jProperties properties = load(true, + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), "spring.data.neo4j.uri=target/neo4j/my.db"); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, @@ -143,20 +144,15 @@ private static void assertCredentials(Configuration actual, String username, } } - public Neo4jProperties load(final boolean embeddedAvailable, String... environment) { + public Neo4jProperties load( final Set availableDrivers, String... environment) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.setClassLoader(new URLClassLoader(new URL[0], getClass().getClassLoader()) { @Override protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - if (name.equals(Neo4jProperties.EMBEDDED_DRIVER)) { - if (embeddedAvailable) { - return TestEmbeddedDriver.class; - } - else { - throw new ClassNotFoundException(); - } + if ( AvailableDriver.shouldLoad( availableDrivers, name )) { + return AvailableDriver.loadClass( name ); } return super.loadClass(name, resolve); } @@ -175,8 +171,42 @@ static class TestConfiguration { } + private enum AvailableDriver { + EMBEDDED( Neo4jProperties.EMBEDDED_DRIVER, TestEmbeddedDriver.class ), + BOLT( Neo4jProperties.BOLT_DRIVER, TestBoltDriver.class ); + + private final String driverName; + private final Class driverStub; + + AvailableDriver( final String driverName, final Class driverStub ) { + this.driverName = driverName; + this.driverStub = driverStub; + } + + public static boolean shouldLoad( Set availableDrivers, String name ) { + for ( AvailableDriver driver : availableDrivers ) { + if ( driver.driverName.equals( name ) ) { + return true; + } + } + return false; + } + + public static Class loadClass( String className ) throws ClassNotFoundException { + for ( AvailableDriver driver : AvailableDriver.values() ) { + if ( driver.driverName.equals( className ) ) { + return driver.driverStub; + } + } + throw new ClassNotFoundException(); + } + } + private static class TestEmbeddedDriver { } + private static class TestBoltDriver { + } + } From c16054b67bba36b939da0ef0640ce70f571d8e74 Mon Sep 17 00:00:00 2001 From: Caleb Cushing Date: Wed, 24 Aug 2016 20:11:28 -0500 Subject: [PATCH 3/3] add happy path tests gh-6690 Signed-off-by: Caleb Cushing --- .../data/neo4j/Neo4jPropertiesTests.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java index e854dc12b3dd..1c27d972af04 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jPropertiesTests.java @@ -57,7 +57,14 @@ public void defaultUseEmbeddedInMemoryIfAvailable() { } @Test - public void defaultUseHttpDriverIfEmbeddedDriverIsNotAvailable() { + public void defaultUseBoltIfAvailableAndEmbeddedNot() { + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.BOLT )); + Configuration configuration = properties.createConfiguration(); + assertDriver(configuration, Neo4jProperties.BOLT_DRIVER, "bolt://localhost:7687"); + } + + @Test + public void defaultUseHttpIfEmbeddedAndBoltIsNotAvailable() { Neo4jProperties properties = load(EnumSet.noneOf( AvailableDriver.class )); Configuration configuration = properties.createConfiguration(); assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, @@ -72,6 +79,14 @@ public void httpUriUseHttpServer() { assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, "http://localhost:7474"); } + @Test + public void boltUriUseBolt() { + Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED, AvailableDriver.BOLT ), + "spring.data.neo4j.uri=bolt://localhost:7687"); + Configuration configuration = properties.createConfiguration(); + assertDriver(configuration, Neo4jProperties.BOLT_DRIVER, "bolt://localhost:7687"); + } + @Test public void fileUriUseEmbeddedServer() { Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ),