Merge branch '2.1.x'

pull/15937/head
Andy Wilkinson 6 years ago
commit db31e42751

@ -805,6 +805,11 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>

@ -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");
* you may not use this file except in compliance with the License.
@ -22,12 +22,12 @@ import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.CassandraContainer;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
@ -43,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class CassandraDataAutoConfigurationIntegrationTests {
@ClassRule
public static CassandraContainer cassandra = new CassandraContainer();
public static CassandraContainer<?> cassandra = new CassandraContainer<>();
private AnnotationConfigApplicationContext context;
@ -51,7 +51,7 @@ public class CassandraDataAutoConfigurationIntegrationTests {
public void setUp() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues
.of("spring.data.cassandra.port=" + cassandra.getMappedPort(),
.of("spring.data.cassandra.port=" + cassandra.getFirstMappedPort(),
"spring.data.cassandra.read-timeout=24000",
"spring.data.cassandra.connect-timeout=10000")
.applyTo(this.context.getEnvironment());
@ -96,7 +96,8 @@ public class CassandraDataAutoConfigurationIntegrationTests {
private void createTestKeyspaceIfNotExists() {
Cluster cluster = Cluster.builder().withoutJMXReporting()
.withPort(cassandra.getMappedPort()).addContactPoint("localhost").build();
.withPort(cassandra.getFirstMappedPort()).addContactPoint("localhost")
.build();
try (Session session = cluster.connect()) {
session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test"
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");

@ -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.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
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 Cassandra.
*
* @author Andy Wilkinson
* @author Madhura Bhave
*/
public class CassandraContainer extends Container {
private static final int PORT = 9042;
public CassandraContainer() {
super("cassandra:3.11.1", PORT,
(container) -> container.waitingFor(new WaitStrategy(container))
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofSeconds(120)));
}
private static final class WaitStrategy extends HostPortWaitStrategy {
private final GenericContainer<?> container;
private WaitStrategy(GenericContainer<?> container) {
this.container = container;
}
@Override
public void waitUntilReady() {
super.waitUntilReady();
try {
Unreliables.retryUntilTrue((int) this.startupTimeout.getSeconds(),
TimeUnit.SECONDS, checkConnection());
}
catch (TimeoutException ex) {
throw new IllegalStateException(ex);
}
}
private Callable<Boolean> checkConnection() {
return () -> {
try (Cluster cluster = Cluster.builder().withoutJMXReporting()
.withPort(this.container.getMappedPort(CassandraContainer.PORT))
.addContactPoint("localhost").build()) {
cluster.connect();
return true;
}
catch (IllegalArgumentException | NoHostAvailableException ex) {
return false;
}
};
}
}
}
Loading…
Cancel
Save