parent
							
								
									bb7b789cd9
								
							
						
					
					
						commit
						47c568a24d
					
				|  | @ -47,8 +47,6 @@ public class Neo4jProperties implements ApplicationContextAware { | ||||||
| 
 | 
 | ||||||
| 	static final String BOLT_DRIVER = "org.neo4j.ogm.drivers.bolt.driver.BoltDriver"; | 	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. | 	 * URI used by the driver. Auto-detected by default. | ||||||
| 	 */ | 	 */ | ||||||
|  | @ -173,11 +171,6 @@ public class Neo4jProperties implements ApplicationContextAware { | ||||||
| 			driverConfiguration.setDriverClassName(EMBEDDED_DRIVER); | 			driverConfiguration.setDriverClassName(EMBEDDED_DRIVER); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 		if (ClassUtils.isPresent(BOLT_DRIVER, this.classLoader)) { |  | ||||||
| 			driverConfiguration.setDriverClassName(BOLT_DRIVER); |  | ||||||
| 			driverConfiguration.setURI( DEFAULT_BOLT_URI ); |  | ||||||
| 			return; |  | ||||||
| 		} |  | ||||||
| 		driverConfiguration.setDriverClassName(HTTP_DRIVER); | 		driverConfiguration.setDriverClassName(HTTP_DRIVER); | ||||||
| 		driverConfiguration.setURI(DEFAULT_HTTP_URI); | 		driverConfiguration.setURI(DEFAULT_HTTP_URI); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -18,8 +18,6 @@ package org.springframework.boot.autoconfigure.data.neo4j; | ||||||
| 
 | 
 | ||||||
| import java.net.URL; | import java.net.URL; | ||||||
| import java.net.URLClassLoader; | import java.net.URLClassLoader; | ||||||
| import java.util.EnumSet; |  | ||||||
| import java.util.Set; |  | ||||||
| 
 | 
 | ||||||
| import com.hazelcast.util.Base64; | import com.hazelcast.util.Base64; | ||||||
| import org.junit.After; | import org.junit.After; | ||||||
|  | @ -31,6 +29,7 @@ import org.neo4j.ogm.config.DriverConfiguration; | ||||||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||||||
| import org.springframework.boot.test.util.EnvironmentTestUtils; | import org.springframework.boot.test.util.EnvironmentTestUtils; | ||||||
| import org.springframework.context.annotation.AnnotationConfigApplicationContext; | import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||||||
|  | 
 | ||||||
| import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | @ -51,21 +50,14 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void defaultUseEmbeddedInMemoryIfAvailable() { | 	public void defaultUseEmbeddedInMemoryIfAvailable() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED )); | 		Neo4jProperties properties = load(true); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, null); | 		assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, null); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void defaultUseBoltIfAvailableAndEmbeddedNot() { | 	public void defaultUseHttpDriverIfEmbeddedDriverIsNotAvailable() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.BOLT )); | 		Neo4jProperties properties = load(false); | ||||||
| 		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(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, | 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, | ||||||
| 				Neo4jProperties.DEFAULT_HTTP_URI); | 				Neo4jProperties.DEFAULT_HTTP_URI); | ||||||
|  | @ -73,15 +65,15 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void httpUriUseHttpServer() { | 	public void httpUriUseHttpServer() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.uri=http://localhost:7474"); | 				"spring.data.neo4j.uri=http://localhost:7474"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, "http://localhost:7474"); | 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, "http://localhost:7474"); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void boltUriUseBolt() { | 	public void boltUriUseBoltDriver() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED, AvailableDriver.BOLT ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.uri=bolt://localhost:7687"); | 				"spring.data.neo4j.uri=bolt://localhost:7687"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.BOLT_DRIVER, "bolt://localhost:7687"); | 		assertDriver(configuration, Neo4jProperties.BOLT_DRIVER, "bolt://localhost:7687"); | ||||||
|  | @ -89,7 +81,7 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void fileUriUseEmbeddedServer() { | 	public void fileUriUseEmbeddedServer() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.uri=file://var/tmp/graph.db"); | 				"spring.data.neo4j.uri=file://var/tmp/graph.db"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, | 		assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, | ||||||
|  | @ -98,7 +90,7 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void credentialsAreSet() { | 	public void credentialsAreSet() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.uri=http://localhost:7474", | 				"spring.data.neo4j.uri=http://localhost:7474", | ||||||
| 				"spring.data.neo4j.username=user", "spring.data.neo4j.password=secret"); | 				"spring.data.neo4j.username=user", "spring.data.neo4j.password=secret"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
|  | @ -108,7 +100,7 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void credentialsAreSetFromUri() { | 	public void credentialsAreSetFromUri() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.uri=http://user:secret@my-server:7474"); | 				"spring.data.neo4j.uri=http://user:secret@my-server:7474"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, | 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, | ||||||
|  | @ -118,7 +110,7 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void embeddedModeDisabledUseHttpUri() { | 	public void embeddedModeDisabledUseHttpUri() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.embedded.enabled=false"); | 				"spring.data.neo4j.embedded.enabled=false"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, | 		assertDriver(configuration, Neo4jProperties.HTTP_DRIVER, | ||||||
|  | @ -127,7 +119,7 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void embeddedModeWithRelativeLocation() { | 	public void embeddedModeWithRelativeLocation() { | ||||||
| 		Neo4jProperties properties = load(EnumSet.of( AvailableDriver.EMBEDDED ), | 		Neo4jProperties properties = load(true, | ||||||
| 				"spring.data.neo4j.uri=target/neo4j/my.db"); | 				"spring.data.neo4j.uri=target/neo4j/my.db"); | ||||||
| 		Configuration configuration = properties.createConfiguration(); | 		Configuration configuration = properties.createConfiguration(); | ||||||
| 		assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, | 		assertDriver(configuration, Neo4jProperties.EMBEDDED_DRIVER, | ||||||
|  | @ -159,15 +151,20 @@ public class Neo4jPropertiesTests { | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public Neo4jProperties load( final Set<AvailableDriver> availableDrivers, String... environment) { | 	public Neo4jProperties load(final boolean embeddedAvailable, String... environment) { | ||||||
| 		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); | 		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); | ||||||
| 		ctx.setClassLoader(new URLClassLoader(new URL[0], getClass().getClassLoader()) { | 		ctx.setClassLoader(new URLClassLoader(new URL[0], getClass().getClassLoader()) { | ||||||
| 
 | 
 | ||||||
| 			@Override | 			@Override | ||||||
| 			protected Class<?> loadClass(String name, boolean resolve) | 			protected Class<?> loadClass(String name, boolean resolve) | ||||||
| 					throws ClassNotFoundException { | 					throws ClassNotFoundException { | ||||||
| 				if ( AvailableDriver.shouldLoad( availableDrivers, name )) { | 				if (name.equals(Neo4jProperties.EMBEDDED_DRIVER)) { | ||||||
| 					return AvailableDriver.loadClass( name ); | 					if (embeddedAvailable) { | ||||||
|  | 						return TestEmbeddedDriver.class; | ||||||
|  | 					} | ||||||
|  | 					else { | ||||||
|  | 						throw new ClassNotFoundException(); | ||||||
|  | 					} | ||||||
| 				} | 				} | ||||||
| 				return super.loadClass(name, resolve); | 				return super.loadClass(name, resolve); | ||||||
| 			} | 			} | ||||||
|  | @ -186,42 +183,8 @@ public class Neo4jPropertiesTests { | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	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<AvailableDriver> 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 TestEmbeddedDriver { | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static class TestBoltDriver { |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue