mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Replace custom Neo4j container with Testcontainers version
See gh-15638
This commit is contained in:
parent
a5aceb35d7
commit
316126e8f0
@ -101,6 +101,11 @@
|
|||||||
<artifactId>testcontainers</artifactId>
|
<artifactId>testcontainers</artifactId>
|
||||||
<version>1.10.6</version>
|
<version>1.10.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>neo4j</artifactId>
|
||||||
|
<version>1.10.6</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.vaadin.external.google</groupId>
|
<groupId>com.vaadin.external.google</groupId>
|
||||||
<artifactId>android-json</artifactId>
|
<artifactId>android-json</artifactId>
|
||||||
|
@ -299,5 +299,10 @@
|
|||||||
<artifactId>testcontainers</artifactId>
|
<artifactId>testcontainers</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>neo4j</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2018 the original author or authors.
|
* Copyright 2012-2019 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -20,11 +20,11 @@ import org.junit.ClassRule;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.neo4j.ogm.session.Session;
|
import org.neo4j.ogm.session.Session;
|
||||||
|
import org.testcontainers.containers.Neo4jContainer;
|
||||||
|
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.test.util.TestPropertyValues;
|
||||||
import org.springframework.boot.testsupport.testcontainers.Neo4jContainer;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextInitializer;
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Michael Simons
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(initializers = DataNeo4jTestIntegrationTests.Initializer.class)
|
@ContextConfiguration(initializers = DataNeo4jTestIntegrationTests.Initializer.class)
|
||||||
@ -46,7 +47,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||||||
public class DataNeo4jTestIntegrationTests {
|
public class DataNeo4jTestIntegrationTests {
|
||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static Neo4jContainer neo4j = new Neo4jContainer();
|
public static Neo4jContainer neo4j = new Neo4jContainer().withAdminPassword(null);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Session session;
|
private Session session;
|
||||||
@ -79,8 +80,7 @@ public class DataNeo4jTestIntegrationTests {
|
|||||||
@Override
|
@Override
|
||||||
public void initialize(
|
public void initialize(
|
||||||
ConfigurableApplicationContext configurableApplicationContext) {
|
ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
TestPropertyValues
|
TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl())
|
||||||
.of("spring.data.neo4j.uri=bolt://localhost:" + neo4j.getMappedPort())
|
|
||||||
.applyTo(configurableApplicationContext.getEnvironment());
|
.applyTo(configurableApplicationContext.getEnvironment());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2018 the original author or authors.
|
* Copyright 2012-2019 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -19,10 +19,10 @@ package org.springframework.boot.test.autoconfigure.data.neo4j;
|
|||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.testcontainers.containers.Neo4jContainer;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.test.util.TestPropertyValues;
|
||||||
import org.springframework.boot.testsupport.testcontainers.Neo4jContainer;
|
|
||||||
import org.springframework.context.ApplicationContextInitializer;
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||||
@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
* Integration test with custom include filter for {@link DataNeo4jTest}.
|
* Integration test with custom include filter for {@link DataNeo4jTest}.
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
|
* @author Michael Simons
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(initializers = DataNeo4jTestWithIncludeFilterIntegrationTests.Initializer.class)
|
@ContextConfiguration(initializers = DataNeo4jTestWithIncludeFilterIntegrationTests.Initializer.class)
|
||||||
@ -43,7 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
public class DataNeo4jTestWithIncludeFilterIntegrationTests {
|
public class DataNeo4jTestWithIncludeFilterIntegrationTests {
|
||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static Neo4jContainer neo4j = new Neo4jContainer();
|
public static Neo4jContainer neo4j = new Neo4jContainer().withAdminPassword(null);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ExampleService service;
|
private ExampleService service;
|
||||||
@ -59,8 +60,7 @@ public class DataNeo4jTestWithIncludeFilterIntegrationTests {
|
|||||||
@Override
|
@Override
|
||||||
public void initialize(
|
public void initialize(
|
||||||
ConfigurableApplicationContext configurableApplicationContext) {
|
ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
TestPropertyValues
|
TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl())
|
||||||
.of("spring.data.neo4j.uri=bolt://localhost:" + neo4j.getMappedPort())
|
|
||||||
.applyTo(configurableApplicationContext.getEnvironment());
|
.applyTo(configurableApplicationContext.getEnvironment());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.testsupport.testcontainers;
|
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.neo4j.ogm.config.Configuration;
|
|
||||||
import org.neo4j.ogm.session.SessionFactory;
|
|
||||||
import org.rnorth.ducttape.TimeoutException;
|
|
||||||
import org.rnorth.ducttape.unreliables.Unreliables;
|
|
||||||
import org.testcontainers.containers.GenericContainer;
|
|
||||||
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link GenericContainer} for Neo4J.
|
|
||||||
*
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
* @author Madhura Bhave
|
|
||||||
*/
|
|
||||||
public class Neo4jContainer extends Container {
|
|
||||||
|
|
||||||
private static final int PORT = 7687;
|
|
||||||
|
|
||||||
public Neo4jContainer() {
|
|
||||||
super("neo4j:3.3.1", PORT, (container) -> container
|
|
||||||
.waitingFor(new WaitStrategy(container)).withEnv("NEO4J_AUTH", "none"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final class WaitStrategy extends HostPortWaitStrategy {
|
|
||||||
|
|
||||||
private final GenericContainer<?> container;
|
|
||||||
|
|
||||||
private WaitStrategy(GenericContainer<?> container) {
|
|
||||||
this.container = container;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void waitUntilReady() {
|
|
||||||
super.waitUntilReady();
|
|
||||||
Configuration configuration = new Configuration.Builder()
|
|
||||||
.uri("bolt://localhost:"
|
|
||||||
+ this.container.getMappedPort(Neo4jContainer.PORT))
|
|
||||||
.build();
|
|
||||||
SessionFactory sessionFactory = new SessionFactory(configuration,
|
|
||||||
"org.springframework.boot.test.autoconfigure.data.neo4j");
|
|
||||||
try {
|
|
||||||
Unreliables.retryUntilTrue((int) this.startupTimeout.getSeconds(),
|
|
||||||
TimeUnit.SECONDS, checkConnection(sessionFactory));
|
|
||||||
}
|
|
||||||
catch (TimeoutException ex) {
|
|
||||||
throw new IllegalStateException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Callable<Boolean> checkConnection(SessionFactory sessionFactory) {
|
|
||||||
return () -> {
|
|
||||||
try {
|
|
||||||
sessionFactory.openSession().beginTransaction().close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user