Skip to content

Commit

Permalink
Integration tests now work with Boot 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
cppwfs committed Sep 11, 2023
1 parent 40566a5 commit 3997a16
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 131 deletions.
15 changes: 15 additions & 0 deletions spring-cloud-task-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

import javax.sql.DataSource;

import org.h2.tools.Server;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskRepository;
Expand All @@ -45,8 +45,6 @@
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
Expand All @@ -59,37 +57,26 @@
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.TestSocketUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { TaskStartTests.TaskLauncherConfiguration.class })
@Testcontainers
public class TaskStartTests {

private final static int WAIT_INTERVAL = 500;

private final static int MAX_WAIT_TIME = 5000;

private final static String DATASOURCE_URL;

private final static String DATASOURCE_USER_NAME = "SA";

private final static String DATASOURCE_USER_PASSWORD = "''";

private final static String DATASOURCE_DRIVER_CLASS_NAME = "org.h2.Driver";

private final static String TASK_NAME = "TASK_LAUNCHER_SINK_TEST";

private static int randomPort;
private static final DockerImageName MARIADB_IMAGE = DockerImageName.parse("mariadb:10.9.3");

static {
randomPort = TestSocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
}
/**
* Provide mariadb test container for tests.
*/
@Container
public static MariaDBContainer<?> mariaDBContainer = new MariaDBContainer<>(MARIADB_IMAGE);

private DataSource dataSource;

Expand All @@ -108,21 +95,23 @@ public void tearDown() {
}
}

@Autowired
public void setDataSource(DataSource dataSource) {
@BeforeEach
public void setup() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(mariaDBContainer.getDriverClassName());
dataSource.setUrl(mariaDBContainer.getJdbcUrl());
dataSource.setUsername(mariaDBContainer.getUsername());
dataSource.setPassword(mariaDBContainer.getPassword());
this.dataSource = dataSource;
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
this.taskExplorer = new SimpleTaskExplorer(factoryBean);
this.taskRepository = new SimpleTaskRepository(factoryBean);
}

@BeforeEach
public void setup() {
this.properties = new HashMap<>();
this.properties.put("spring.datasource.url", DATASOURCE_URL);
this.properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
this.properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
this.properties.put("spring.datasource.driverClassName", DATASOURCE_DRIVER_CLASS_NAME);
this.properties.put("spring.datasource.url", mariaDBContainer.getJdbcUrl());
this.properties.put("spring.datasource.username", mariaDBContainer.getUsername());
this.properties.put("spring.datasource.password", mariaDBContainer.getPassword());
this.properties.put("spring.datasource.driverClassName", mariaDBContainer.getDriverClassName());
this.properties.put("spring.application.name", TASK_NAME);
this.properties.put("spring.cloud.task.initialize-enabled", "false");

Expand All @@ -147,7 +136,7 @@ public void setup() {

initializer.setDataSource(this.dataSource);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
databasePopulator.addScript(new ClassPathResource("/org/springframework/cloud/task/schema-mariadb.sql"));
initializer.setDatabasePopulator(databasePopulator);
initializer.afterPropertiesSet();
}
Expand Down Expand Up @@ -260,6 +249,11 @@ private SpringApplication getTaskApplication(Integer executionId) {
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
myMap.put("spring.cloud.task.executionid", executionId);
myMap.put("spring.datasource.url", mariaDBContainer.getJdbcUrl());
myMap.put("spring.datasource.username", mariaDBContainer.getUsername());
myMap.put("spring.datasource.password", mariaDBContainer.getPassword());
myMap.put("spring.datasource.driverClassName", mariaDBContainer.getDriverClassName());

propertySources.addFirst(new MapPropertySource("EnvrionmentTestPropsource", myMap));
myapp.setEnvironment(environment);
return myapp;
Expand Down Expand Up @@ -296,37 +290,4 @@ private void enableLock(String lockKey) {
taskLockInsert.execute(taskLockParams);
}

@Configuration
public static class TaskLauncherConfiguration {

private static Server defaultServer;

@Bean
public Server initH2TCPServer() {
Server server = null;
try {
if (defaultServer == null) {
server = Server.createTcpServer("-ifNotExists", "-tcp", "-tcpAllowOthers", "-tcpPort",
String.valueOf(randomPort)).start();
defaultServer = server;
}
}
catch (SQLException e) {
throw new IllegalStateException(e);
}
return defaultServer;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DATASOURCE_DRIVER_CLASS_NAME);
dataSource.setUrl(DATASOURCE_URL);
dataSource.setUsername(DATASOURCE_USER_NAME);
dataSource.setPassword(DATASOURCE_USER_PASSWORD);
return dataSource;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,61 @@
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.h2.tools.Server;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.task.executionid.TaskStartApplication;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.TestSocketUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { TaskInitializerTests.TaskLauncherConfiguration.class })
@Testcontainers
public class TaskInitializerTests {

private final static int WAIT_INTERVAL = 500;

private final static int MAX_WAIT_TIME = 5000;

private final static String URL = "maven://io.spring.cloud:" + "timestamp-task:jar:1.1.0.RELEASE";

private final static String DATASOURCE_URL;

private final static String DATASOURCE_USER_NAME = "SA";

private final static String DATASOURCE_USER_PASSWORD = "''";

private final static String DATASOURCE_DRIVER_CLASS_NAME = "org.h2.Driver";

private final static String TASK_NAME = "TASK_LAUNCHER_SINK_TEST";

private static int randomPort;

static {
randomPort = TestSocketUtils.findAvailableTcpPort();
DATASOURCE_URL = "jdbc:h2:tcp://localhost:" + randomPort + "/mem:dataflow;DB_CLOSE_DELAY=-1;"
+ "DB_CLOSE_ON_EXIT=FALSE";
}

private DataSource dataSource;

private TaskExplorer taskExplorer;

private ConfigurableApplicationContext applicationContext;

private static final DockerImageName MARIADB_IMAGE = DockerImageName.parse("mariadb:10.9.3");

/**
* Provide mariadb test container for tests.
*/
@Container
public static MariaDBContainer<?> mariaDBContainer = new MariaDBContainer<>(MARIADB_IMAGE);

@AfterEach
public void tearDown() {
if (this.applicationContext != null && this.applicationContext.isActive()) {
Expand All @@ -100,6 +90,14 @@ public void setDataSource(DataSource dataSource) {

@BeforeEach
public void setup() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(mariaDBContainer.getDriverClassName());
dataSource.setUrl(mariaDBContainer.getJdbcUrl());
dataSource.setUsername(mariaDBContainer.getUsername());
dataSource.setPassword(mariaDBContainer.getPassword());
this.dataSource = dataSource;
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
this.taskExplorer = new SimpleTaskExplorer(factoryBean);

JdbcTemplate template = new JdbcTemplate(this.dataSource);
template.execute("DROP TABLE IF EXISTS TASK_TASK_BATCH");
Expand All @@ -121,7 +119,7 @@ public void setup() {

@Test
public void testNotInitialized() throws Exception {
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
SpringApplication myapp = getTaskApplication();
String[] properties = { "--spring.cloud.task.initialize-enabled=false" };
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> {
this.applicationContext = myapp.run(properties);
Expand All @@ -130,7 +128,7 @@ public void testNotInitialized() throws Exception {

@Test
public void testWithInitialized() throws Exception {
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
SpringApplication myapp = getTaskApplication();
String[] properties = { "--spring.cloud.task.initialize-enabled=true" };
this.applicationContext = myapp.run(properties);
assertThat(waitForDBToBePopulated()).isTrue();
Expand All @@ -144,7 +142,7 @@ public void testWithInitialized() throws Exception {

@Test
public void testNotInitializedOriginalProperty() throws Exception {
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
SpringApplication myapp = getTaskApplication();
String[] properties = { "--spring.cloud.task.initialize.enable=false" };
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> {
this.applicationContext = myapp.run(properties);
Expand All @@ -153,7 +151,7 @@ public void testNotInitializedOriginalProperty() throws Exception {

@Test
public void testWithInitializedOriginalProperty() throws Exception {
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
SpringApplication myapp = getTaskApplication();
String[] properties = { "--spring.cloud.task.initialize.enable=true" };
this.applicationContext = myapp.run(properties);
assertThat(waitForDBToBePopulated()).isTrue();
Expand Down Expand Up @@ -186,37 +184,19 @@ private boolean waitForDBToBePopulated() throws Exception {
return isDbPopulated;
}

@Configuration
public static class TaskLauncherConfiguration {

private static Server defaultServer;

@Bean
public Server initH2TCPServer() {
Server server = null;
try {
if (defaultServer == null) {
server = Server.createTcpServer("-ifNotExists", "-tcp", "-tcpAllowOthers", "-tcpPort",
String.valueOf(randomPort)).start();
defaultServer = server;
}
}
catch (SQLException e) {
throw new IllegalStateException(e);
}
return defaultServer;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DATASOURCE_DRIVER_CLASS_NAME);
dataSource.setUrl(DATASOURCE_URL);
dataSource.setUsername(DATASOURCE_USER_NAME);
dataSource.setPassword(DATASOURCE_USER_PASSWORD);
return dataSource;
}

private SpringApplication getTaskApplication() {
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
Map<String, Object> myMap = new HashMap<>();
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
myMap.put("spring.datasource.url", mariaDBContainer.getJdbcUrl());
myMap.put("spring.datasource.username", mariaDBContainer.getUsername());
myMap.put("spring.datasource.password", mariaDBContainer.getPassword());
myMap.put("spring.datasource.driverClassName", mariaDBContainer.getDriverClassName());

propertySources.addFirst(new MapPropertySource("EnvrionmentTestPropsource", myMap));
myapp.setEnvironment(environment);
return myapp;
}

}
Loading

0 comments on commit 3997a16

Please sign in to comment.