Skip to content

Commit 018fcf4

Browse files
committed
Refactor tests in spring-batch-core
- 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
1 parent acb7de0 commit 018fcf4

File tree

53 files changed

+176
-1234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+176
-1234
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerIntegrationTests.java

+5-21
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020

2121
import javax.sql.DataSource;
2222

23-
import org.apache.commons.dbcp2.BasicDataSource;
2423
import org.junit.jupiter.api.Assertions;
2524
import org.junit.jupiter.api.Test;
26-
import test.jdbc.datasource.DataSourceInitializer;
2725

2826
import org.springframework.batch.core.BatchStatus;
2927
import org.springframework.batch.core.Job;
@@ -56,8 +54,6 @@
5654
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5755
import org.springframework.context.annotation.Bean;
5856
import org.springframework.context.annotation.Configuration;
59-
import org.springframework.core.io.ClassPathResource;
60-
import org.springframework.core.io.Resource;
6157
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
6258
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
6359
import org.springframework.jdbc.support.JdbcTransactionManager;
@@ -121,23 +117,11 @@ public SimpleFlow simpleFlow() {
121117
}
122118

123119
@Bean
124-
public BasicDataSource dataSource() {
125-
BasicDataSource dataSource = new BasicDataSource();
126-
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
127-
dataSource.setUrl("jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true;hsqldb.tx=mvcc");
128-
dataSource.setUsername("sa");
129-
dataSource.setPassword("");
130-
return dataSource;
131-
}
132-
133-
@Bean
134-
public DataSourceInitializer dataSourceInitializer() {
135-
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
136-
dataSourceInitializer.setDataSource(dataSource());
137-
dataSourceInitializer.setInitScripts(
138-
new Resource[] { new ClassPathResource("org/springframework/batch/core/schema-drop-hsqldb.sql"),
139-
new ClassPathResource("org/springframework/batch/core/schema-hsqldb.sql") });
140-
return dataSourceInitializer;
120+
public DataSource dataSource() {
121+
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
122+
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
123+
.generateUniqueName(true)
124+
.build();
141125
}
142126

143127
@Bean

spring-batch-core/src/test/java/org/springframework/batch/core/resource/JdbcCursorItemReaderPreparedStatementIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2022 the original author or authors.
2+
* Copyright 2008-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232
import static org.junit.jupiter.api.Assertions.assertEquals;
3333
import static org.junit.jupiter.api.Assertions.assertNull;
3434

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

3838
JdbcCursorItemReader<Foo> itemReader;

spring-batch-core/src/test/java/org/springframework/batch/core/step/RestartInPriorStepTests.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.step;
1717

18+
import org.junit.jupiter.api.Disabled;
1819
import org.junit.jupiter.api.Test;
1920
import org.springframework.batch.core.BatchStatus;
2021
import org.springframework.batch.core.ExitStatus;
@@ -26,7 +27,6 @@
2627
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
2728
import org.springframework.batch.core.job.flow.JobExecutionDecider;
2829
import org.springframework.batch.core.launch.JobLauncher;
29-
import org.springframework.batch.core.repository.JobRepository;
3030
import org.springframework.batch.core.scope.context.ChunkContext;
3131
import org.springframework.batch.core.step.tasklet.Tasklet;
3232
import org.springframework.batch.repeat.RepeatStatus;
@@ -45,12 +45,9 @@
4545
*/
4646
@SpringJUnitConfig
4747
// FIXME this test fails when upgrading the batch xsd from 2.2 to 3.0:
48-
// https://github.com/spring-projects/spring-batch/issues/1287
48+
@Disabled("https://github.com/spring-projects/spring-batch/issues/1287")
4949
class RestartInPriorStepTests {
5050

51-
@Autowired
52-
private JobRepository jobRepository;
53-
5451
@Autowired
5552
private JobLauncher jobLauncher;
5653

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public FaultTolerantStepFactoryBeanTests() throws Exception {
110110
void setUp() throws Exception {
111111
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder().generateUniqueName(true)
112112
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
113-
.addScript("/org/springframework/batch/core/schema-hsqldb-extended.sql")
113+
.addScript("/schema-hsqldb-extended.sql")
114114
.build();
115115
JdbcTransactionManager transactionManager = new JdbcTransactionManager(embeddedDatabase);
116116

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanUnexpectedRollbackTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 the original author or authors.
2+
* Copyright 2010-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@
4343
/**
4444
* Tests for {@link FaultTolerantStepFactoryBean} with unexpected rollback.
4545
*/
46-
@SpringJUnitConfig(locations = "classpath:/org/springframework/batch/core/repository/dao/data-source-context.xml")
46+
@SpringJUnitConfig(locations = "classpath:data-source-context.xml")
4747
class FaultTolerantStepFactoryBeanUnexpectedRollbackTests {
4848

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

spring-batch-core/src/test/java/org/springframework/batch/core/test/AbstractIntegrationTests.java

-41
This file was deleted.

spring-batch-core/src/test/java/org/springframework/batch/core/test/football/FootballJobIntegrationTests.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@
1818

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

21-
import javax.sql.DataSource;
22-
2321
import org.apache.commons.logging.Log;
2422
import org.apache.commons.logging.LogFactory;
2523
import org.junit.jupiter.api.Test;
@@ -29,7 +27,6 @@
2927
import org.springframework.batch.core.JobParametersBuilder;
3028
import org.springframework.batch.core.StepExecution;
3129
import org.springframework.batch.core.launch.JobLauncher;
32-
import org.springframework.batch.core.test.AbstractIntegrationTests;
3330
import org.springframework.beans.factory.annotation.Autowired;
3431
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3532

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

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

53-
@Autowired
54-
public void setDataSource(DataSource dataSource) {
55-
this.dataSource = dataSource;
56-
}
57-
5850
@Test
5951
void testLaunchJob() throws Exception {
6052
JobExecution execution = jobLauncher.run(job,

spring-batch-core/src/test/java/org/springframework/batch/core/test/football/FootballJobSkipIntegrationTests.java

+2-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@
1818

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

21-
import javax.sql.DataSource;
22-
2321
import org.apache.commons.logging.Log;
2422
import org.apache.commons.logging.LogFactory;
2523
import org.junit.jupiter.api.Test;
@@ -29,10 +27,7 @@
2927
import org.springframework.batch.core.JobParametersBuilder;
3028
import org.springframework.batch.core.StepExecution;
3129
import org.springframework.batch.core.launch.JobLauncher;
32-
import org.springframework.batch.core.test.AbstractIntegrationTests;
33-
import org.springframework.batch.support.DatabaseType;
3430
import org.springframework.beans.factory.annotation.Autowired;
35-
import org.springframework.jdbc.core.JdbcTemplate;
3631
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3732

3833
/**
@@ -41,40 +36,19 @@
4136
*
4237
*/
4338
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/META-INF/batch/footballSkipJob.xml" })
44-
public class FootballJobSkipIntegrationTests extends AbstractIntegrationTests {
39+
public class FootballJobSkipIntegrationTests {
4540

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

49-
private JdbcTemplate jdbcTemplate;
50-
5144
@Autowired
5245
private JobLauncher jobLauncher;
5346

5447
@Autowired
5548
private Job job;
5649

57-
private DatabaseType databaseType;
58-
59-
@Autowired
60-
public void setDataSource(DataSource dataSource) throws Exception {
61-
this.dataSource = dataSource;
62-
this.jdbcTemplate = new JdbcTemplate(dataSource);
63-
databaseType = DatabaseType.fromMetaData(dataSource);
64-
}
65-
6650
@Test
6751
void testLaunchJob() throws Exception {
68-
try {
69-
if (databaseType == DatabaseType.POSTGRES || databaseType == DatabaseType.ORACLE) {
70-
// Extra special test for these platforms (would have failed
71-
// the job with UNKNOWN status in Batch 2.0):
72-
jdbcTemplate.update("SET CONSTRAINTS ALL DEFERRED");
73-
}
74-
}
75-
catch (Exception e) {
76-
// Ignore (wrong platform)
77-
}
7852
JobExecution execution = jobLauncher.run(job,
7953
new JobParametersBuilder().addLong("skip.limit", 0L).toJobParameters());
8054
assertEquals(BatchStatus.COMPLETED, execution.getStatus());

spring-batch-core/src/test/java/org/springframework/batch/core/test/repository/JdbcJobRepositoryTests.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,8 +22,6 @@
2222
import java.util.List;
2323
import java.util.Set;
2424

25-
import javax.sql.DataSource;
26-
2725
import org.apache.commons.logging.Log;
2826
import org.apache.commons.logging.LogFactory;
2927
import org.junit.jupiter.api.BeforeEach;
@@ -34,7 +32,6 @@
3432
import org.springframework.batch.core.JobParameters;
3533
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
3634
import org.springframework.batch.core.repository.JobRepository;
37-
import org.springframework.batch.core.test.AbstractIntegrationTests;
3835
import org.springframework.beans.factory.annotation.Autowired;
3936
import org.springframework.jdbc.core.JdbcTemplate;
4037
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -46,7 +43,7 @@
4643
import static org.junit.jupiter.api.Assertions.assertTrue;
4744

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

5148
private JobSupport job;
5249

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

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

56+
@Autowired
5957
private JdbcTemplate jdbcTemplate;
6058

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

67-
@Autowired
68-
public void setDataSource(DataSource dataSource) {
69-
this.dataSource = dataSource;
70-
this.jdbcTemplate = new JdbcTemplate(dataSource);
71-
}
72-
7365
@BeforeEach
7466
void onSetUpInTransaction() {
7567
job = new JobSupport("test-job");
7668
job.setRestartable(true);
69+
JdbcTestUtils.deleteFromTables(jdbcTemplate, "BATCH_JOB_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION_CONTEXT",
70+
"BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION", "BATCH_JOB_EXECUTION_PARAMS", "BATCH_JOB_INSTANCE");
7771
}
7872

7973
@Test

spring-batch-core/src/test/java/org/springframework/batch/core/test/timeout/TimeoutJobIntegrationTests.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,29 +15,20 @@
1515
*/
1616
package org.springframework.batch.core.test.timeout;
1717

18-
import javax.sql.DataSource;
19-
2018
import static org.junit.jupiter.api.Assertions.assertEquals;
2119

22-
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
2420
import org.junit.jupiter.api.Test;
2521
import org.springframework.batch.core.BatchStatus;
2622
import org.springframework.batch.core.Job;
2723
import org.springframework.batch.core.JobExecution;
2824
import org.springframework.batch.core.JobParametersBuilder;
2925
import org.springframework.batch.core.launch.JobLauncher;
30-
import org.springframework.batch.core.test.AbstractIntegrationTests;
3126
import org.springframework.beans.factory.annotation.Autowired;
3227
import org.springframework.beans.factory.annotation.Qualifier;
3328
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3429

3530
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/META-INF/batch/timeoutJob.xml" })
36-
public class TimeoutJobIntegrationTests extends AbstractIntegrationTests {
37-
38-
/** Logger */
39-
@SuppressWarnings("unused")
40-
private final Log logger = LogFactory.getLog(getClass());
31+
public class TimeoutJobIntegrationTests {
4132

4233
@Autowired
4334
private JobLauncher jobLauncher;
@@ -50,11 +41,6 @@ public class TimeoutJobIntegrationTests extends AbstractIntegrationTests {
5041
@Qualifier("taskletTimeoutJob")
5142
private Job taskletTimeoutJob;
5243

53-
@Autowired
54-
public void setDataSource(DataSource dataSource) {
55-
this.dataSource = dataSource;
56-
}
57-
5844
@Test
5945
void testChunkTimeoutShouldFail() throws Exception {
6046
JobExecution execution = jobLauncher.run(chunkTimeoutJob,

0 commit comments

Comments
 (0)