Skip to content

Commit

Permalink
Refactor tests in spring-batch-core
Browse files Browse the repository at this point in the history
- Remove unused resources
- Fix logging libraries conflicts
- Merge duplicate data source configuration
- Merge scattered DDL scripts
- Replace usage of DataSourceInitializer with Spring JDBC test utilities
  • Loading branch information
fmbenhassine committed Jun 23, 2023
1 parent acb7de0 commit 018fcf4
Show file tree
Hide file tree
Showing 53 changed files with 176 additions and 1,234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import test.jdbc.datasource.DataSourceInitializer;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
Expand Down Expand Up @@ -56,8 +54,6 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
Expand Down Expand Up @@ -121,23 +117,11 @@ public SimpleFlow simpleFlow() {
}

@Bean
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true;hsqldb.tx=mvcc");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}

@Bean
public DataSourceInitializer dataSourceInitializer() {
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource());
dataSourceInitializer.setInitScripts(
new Resource[] { new ClassPathResource("org/springframework/batch/core/schema-drop-hsqldb.sql"),
new ClassPathResource("org/springframework/batch/core/schema-hsqldb.sql") });
return dataSourceInitializer;
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2022 the original author or authors.
* Copyright 2008-2023 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.
Expand Down Expand Up @@ -32,7 +32,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/data-source-context.xml")
@SpringJUnitConfig(locations = "classpath:data-source-context.xml")
public class JdbcCursorItemReaderPreparedStatementIntegrationTests {

JdbcCursorItemReader<Foo> itemReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 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.
Expand All @@ -15,6 +15,7 @@
*/
package org.springframework.batch.core.step;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
Expand All @@ -26,7 +27,6 @@
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
Expand All @@ -45,12 +45,9 @@
*/
@SpringJUnitConfig
// FIXME this test fails when upgrading the batch xsd from 2.2 to 3.0:
// https://github.com/spring-projects/spring-batch/issues/1287
@Disabled("https://github.com/spring-projects/spring-batch/issues/1287")
class RestartInPriorStepTests {

@Autowired
private JobRepository jobRepository;

@Autowired
private JobLauncher jobLauncher;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public FaultTolerantStepFactoryBeanTests() throws Exception {
void setUp() throws Exception {
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder().generateUniqueName(true)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb-extended.sql")
.addScript("/schema-hsqldb-extended.sql")
.build();
JdbcTransactionManager transactionManager = new JdbcTransactionManager(embeddedDatabase);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -43,7 +43,7 @@
/**
* Tests for {@link FaultTolerantStepFactoryBean} with unexpected rollback.
*/
@SpringJUnitConfig(locations = "classpath:/org/springframework/batch/core/repository/dao/data-source-context.xml")
@SpringJUnitConfig(locations = "classpath:data-source-context.xml")
class FaultTolerantStepFactoryBeanUnexpectedRollbackTests {

protected final Log logger = LogFactory.getLog(getClass());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand All @@ -18,8 +18,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
Expand All @@ -29,7 +27,6 @@
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.test.AbstractIntegrationTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

Expand All @@ -39,7 +36,7 @@
*
*/
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/META-INF/batch/footballJob.xml" })
public class FootballJobIntegrationTests extends AbstractIntegrationTests {
public class FootballJobIntegrationTests {

/** Logger */
private final Log logger = LogFactory.getLog(getClass());
Expand All @@ -50,11 +47,6 @@ public class FootballJobIntegrationTests extends AbstractIntegrationTests {
@Autowired
private Job job;

@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

@Test
void testLaunchJob() throws Exception {
JobExecution execution = jobLauncher.run(job,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand All @@ -18,8 +18,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
Expand All @@ -29,10 +27,7 @@
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.test.AbstractIntegrationTests;
import org.springframework.batch.support.DatabaseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
Expand All @@ -41,40 +36,19 @@
*
*/
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/META-INF/batch/footballSkipJob.xml" })
public class FootballJobSkipIntegrationTests extends AbstractIntegrationTests {
public class FootballJobSkipIntegrationTests {

/** Logger */
private final Log logger = LogFactory.getLog(getClass());

private JdbcTemplate jdbcTemplate;

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;

private DatabaseType databaseType;

@Autowired
public void setDataSource(DataSource dataSource) throws Exception {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
databaseType = DatabaseType.fromMetaData(dataSource);
}

@Test
void testLaunchJob() throws Exception {
try {
if (databaseType == DatabaseType.POSTGRES || databaseType == DatabaseType.ORACLE) {
// Extra special test for these platforms (would have failed
// the job with UNKNOWN status in Batch 2.0):
jdbcTemplate.update("SET CONSTRAINTS ALL DEFERRED");
}
}
catch (Exception e) {
// Ignore (wrong platform)
}
JobExecution execution = jobLauncher.run(job,
new JobParametersBuilder().addLong("skip.limit", 0L).toJobParameters());
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand All @@ -22,8 +22,6 @@
import java.util.List;
import java.util.Set;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -34,7 +32,6 @@
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.test.AbstractIntegrationTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
Expand All @@ -46,7 +43,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml" })
class JdbcJobRepositoryTests extends AbstractIntegrationTests {
class JdbcJobRepositoryTests {

private JobSupport job;

Expand All @@ -56,6 +53,7 @@ class JdbcJobRepositoryTests extends AbstractIntegrationTests {

private final List<Serializable> list = new ArrayList<>();

@Autowired
private JdbcTemplate jdbcTemplate;

@Autowired
Expand All @@ -64,16 +62,12 @@ class JdbcJobRepositoryTests extends AbstractIntegrationTests {
/** Logger */
private final Log logger = LogFactory.getLog(getClass());

@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

@BeforeEach
void onSetUpInTransaction() {
job = new JobSupport("test-job");
job.setRestartable(true);
JdbcTestUtils.deleteFromTables(jdbcTemplate, "BATCH_JOB_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION_CONTEXT",
"BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION", "BATCH_JOB_EXECUTION_PARAMS", "BATCH_JOB_INSTANCE");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand All @@ -15,29 +15,20 @@
*/
package org.springframework.batch.core.test.timeout;

import javax.sql.DataSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.test.AbstractIntegrationTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/META-INF/batch/timeoutJob.xml" })
public class TimeoutJobIntegrationTests extends AbstractIntegrationTests {

/** Logger */
@SuppressWarnings("unused")
private final Log logger = LogFactory.getLog(getClass());
public class TimeoutJobIntegrationTests {

@Autowired
private JobLauncher jobLauncher;
Expand All @@ -50,11 +41,6 @@ public class TimeoutJobIntegrationTests extends AbstractIntegrationTests {
@Qualifier("taskletTimeoutJob")
private Job taskletTimeoutJob;

@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

@Test
void testChunkTimeoutShouldFail() throws Exception {
JobExecution execution = jobLauncher.run(chunkTimeoutJob,
Expand Down
Loading

0 comments on commit 018fcf4

Please sign in to comment.