29
29
import org .springframework .batch .core .explore .JobExplorer ;
30
30
import org .springframework .batch .core .launch .JobLauncher ;
31
31
import org .springframework .batch .core .repository .JobRepository ;
32
+ import org .springframework .batch .core .repository .dao .JdbcExecutionContextDao ;
33
+ import org .springframework .batch .core .repository .dao .JdbcJobExecutionDao ;
32
34
import org .springframework .batch .core .repository .dao .JdbcJobInstanceDao ;
35
+ import org .springframework .batch .core .repository .dao .JdbcStepExecutionDao ;
36
+ import org .springframework .beans .factory .BeanCreationException ;
33
37
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
34
38
import org .springframework .context .annotation .Bean ;
35
39
import org .springframework .context .annotation .Configuration ;
49
53
class BatchRegistrarTests {
50
54
51
55
@ Test
52
- @ DisplayName ("When no datasource is provided, then an IllegalStateException should be thrown" )
56
+ @ DisplayName ("When no datasource is provided, then an BeanCreationException should be thrown" )
53
57
void testMissingDataSource () {
54
- Assertions .assertThrows (IllegalStateException .class , new Executable () {
58
+ Assertions .assertThrows (BeanCreationException .class , new Executable () {
55
59
@ Override
56
60
public void execute () throws Throwable {
57
61
new AnnotationConfigApplicationContext (JobConfigurationWithoutDataSource .class );
@@ -60,9 +64,9 @@ public void execute() throws Throwable {
60
64
}
61
65
62
66
@ Test
63
- @ DisplayName ("When no transaction manager is provided, then an IllegalStateException should be thrown" )
67
+ @ DisplayName ("When no transaction manager is provided, then an BeanCreationException should be thrown" )
64
68
void testMissingTransactionManager () {
65
- Assertions .assertThrows (IllegalStateException .class , new Executable () {
69
+ Assertions .assertThrows (BeanCreationException .class , new Executable () {
66
70
@ Override
67
71
public void execute () throws Throwable {
68
72
new AnnotationConfigApplicationContext (JobConfigurationWithoutTransactionManager .class );
@@ -71,7 +75,7 @@ public void execute() throws Throwable {
71
75
}
72
76
73
77
@ Test
74
- @ DisplayName ("When cusotm beans are provided, then no new ones should be created " )
78
+ @ DisplayName ("When cusotm beans are provided, then default ones should not be used " )
75
79
void testConfigurationWithUserDefinedBeans () {
76
80
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
77
81
JobConfigurationWithUserDefinedInfrastrucutreBeans .class );
@@ -98,7 +102,58 @@ void testDataSourceAndTransactionManagerSetup() {
98
102
DataSource dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
99
103
Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
100
104
101
- // TODO assert on other DAOs
105
+ JdbcJobExecutionDao jobExecutionDao = (JdbcJobExecutionDao ) ReflectionTestUtils .getField (jobRepository ,
106
+ "jobExecutionDao" );
107
+ jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (jobExecutionDao , "jdbcTemplate" );
108
+ dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
109
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
110
+
111
+ JdbcStepExecutionDao stepExecutionDao = (JdbcStepExecutionDao ) ReflectionTestUtils .getField (jobRepository ,
112
+ "stepExecutionDao" );
113
+ jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (stepExecutionDao , "jdbcTemplate" );
114
+ dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
115
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
116
+
117
+ JdbcExecutionContextDao executionContextDao = (JdbcExecutionContextDao ) ReflectionTestUtils
118
+ .getField (jobRepository , "ecDao" );
119
+ jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (executionContextDao , "jdbcTemplate" );
120
+ dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
121
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
122
+
123
+ PlatformTransactionManager transactionManager = getTransactionManagerSetOnJobRepository (jobRepository );
124
+ Assertions .assertEquals (context .getBean (JdbcTransactionManager .class ), transactionManager );
125
+ }
126
+
127
+ @ Test
128
+ @ DisplayName ("When custom bean names are provided, then corresponding beans should be used to configure infrastructure beans" )
129
+ void testConfigurationWithCustonBeanNames () {
130
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
131
+ JobConfigurationWithCustomBeanNames .class );
132
+
133
+ JobRepository jobRepository = context .getBean (JobRepository .class );
134
+ JdbcJobInstanceDao jobInstanceDao = (JdbcJobInstanceDao ) ReflectionTestUtils .getField (jobRepository ,
135
+ "jobInstanceDao" );
136
+ JdbcTemplate jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (jobInstanceDao , "jdbcTemplate" );
137
+ DataSource dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
138
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
139
+
140
+ JdbcJobExecutionDao jobExecutionDao = (JdbcJobExecutionDao ) ReflectionTestUtils .getField (jobRepository ,
141
+ "jobExecutionDao" );
142
+ jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (jobExecutionDao , "jdbcTemplate" );
143
+ dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
144
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
145
+
146
+ JdbcStepExecutionDao stepExecutionDao = (JdbcStepExecutionDao ) ReflectionTestUtils .getField (jobRepository ,
147
+ "stepExecutionDao" );
148
+ jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (stepExecutionDao , "jdbcTemplate" );
149
+ dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
150
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
151
+
152
+ JdbcExecutionContextDao executionContextDao = (JdbcExecutionContextDao ) ReflectionTestUtils
153
+ .getField (jobRepository , "ecDao" );
154
+ jdbcTemplate = (JdbcTemplate ) ReflectionTestUtils .getField (executionContextDao , "jdbcTemplate" );
155
+ dataSource = (DataSource ) ReflectionTestUtils .getField (jdbcTemplate , "dataSource" );
156
+ Assertions .assertEquals (context .getBean (DataSource .class ), dataSource );
102
157
103
158
PlatformTransactionManager transactionManager = getTransactionManagerSetOnJobRepository (jobRepository );
104
159
Assertions .assertEquals (context .getBean (JdbcTransactionManager .class ), transactionManager );
@@ -172,6 +227,23 @@ public JdbcTransactionManager transactionManager(DataSource dataSource) {
172
227
173
228
}
174
229
230
+ @ Configuration
231
+ @ EnableBatchProcessing (dataSourceRef = "batchDataSource" , transactionManagerRef = "batchTransactionManager" )
232
+ public static class JobConfigurationWithCustomBeanNames {
233
+
234
+ @ Bean
235
+ public DataSource batchDataSource () {
236
+ return new EmbeddedDatabaseBuilder ().setType (EmbeddedDatabaseType .HSQL )
237
+ .addScript ("/org/springframework/batch/core/schema-hsqldb.sql" ).generateUniqueName (true ).build ();
238
+ }
239
+
240
+ @ Bean
241
+ public JdbcTransactionManager batchTransactionManager (DataSource dataSource ) {
242
+ return new JdbcTransactionManager (dataSource );
243
+ }
244
+
245
+ }
246
+
175
247
private PlatformTransactionManager getTransactionManagerSetOnJobRepository (JobRepository jobRepository ) {
176
248
Advised target = (Advised ) jobRepository ; // proxy created by
177
249
// AbstractJobRepositoryFactoryBean
0 commit comments