Skip to content

Commit 6e51738

Browse files
committed
Add assertion that a serializer was set in the JdbcExecutionContextDao
Resolves BATCH-2779
1 parent cd22987 commit 6e51738

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2018 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.
@@ -52,6 +52,7 @@
5252
* @author Thomas Risberg
5353
* @author Michael Minella
5454
* @author David Turanski
55+
* @author Mahmoud Ben Hassine
5556
*/
5657
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {
5758

@@ -87,6 +88,7 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
8788
* @param serializer
8889
*/
8990
public void setSerializer(ExecutionContextSerializer serializer) {
91+
Assert.notNull(serializer, "Serializer must not be null");
9092
this.serializer = serializer;
9193
}
9294

@@ -208,6 +210,7 @@ public void setLobHandler(LobHandler lobHandler) {
208210
@Override
209211
public void afterPropertiesSet() throws Exception {
210212
super.afterPropertiesSet();
213+
Assert.state(serializer != null, "ExecutionContextSerializer is required");
211214
}
212215

213216
/**

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2014 the original author or authors.
2+
* Copyright 2008-2018 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,14 +15,45 @@
1515
*/
1616
package org.springframework.batch.core.repository.dao;
1717

18+
import org.junit.Assert;
19+
import org.junit.Test;
1820
import org.junit.runner.RunWith;
21+
22+
import org.springframework.jdbc.core.JdbcOperations;
1923
import org.springframework.test.context.ContextConfiguration;
2024
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2125

26+
import static org.mockito.Mockito.mock;
27+
2228
@RunWith(SpringJUnit4ClassRunner.class)
2329
@ContextConfiguration(locations = {"sql-dao-test.xml"})
2430
public class JdbcExecutionContextDaoTests extends AbstractExecutionContextDaoTests {
2531

32+
@Test
33+
public void testNoSerializer() {
34+
try {
35+
JdbcExecutionContextDao jdbcExecutionContextDao = new JdbcExecutionContextDao();
36+
jdbcExecutionContextDao.setJdbcTemplate(mock(JdbcOperations.class));
37+
jdbcExecutionContextDao.afterPropertiesSet();
38+
} catch (Exception e) {
39+
Assert.assertTrue(e instanceof IllegalStateException);
40+
Assert.assertEquals("ExecutionContextSerializer is required", e.getMessage());
41+
}
42+
}
43+
44+
@Test
45+
public void testNullSerializer() {
46+
try {
47+
JdbcExecutionContextDao jdbcExecutionContextDao = new JdbcExecutionContextDao();
48+
jdbcExecutionContextDao.setJdbcTemplate(mock(JdbcOperations.class));
49+
jdbcExecutionContextDao.setSerializer(null);
50+
jdbcExecutionContextDao.afterPropertiesSet();
51+
} catch (Exception e) {
52+
Assert.assertTrue(e instanceof IllegalArgumentException);
53+
Assert.assertEquals("Serializer must not be null", e.getMessage());
54+
}
55+
}
56+
2657
@Override
2758
protected JobInstanceDao getJobInstanceDao() {
2859
return applicationContext.getBean("jobInstanceDao", JobInstanceDao.class);

0 commit comments

Comments
 (0)