Add assertion that a serializer was set in the JdbcExecutionContextDao

Resolves BATCH-2779
This commit is contained in:
Mahmoud Ben Hassine
2018-12-11 19:16:16 +01:00
committed by Michael Minella
parent 2a578ae663
commit 7ca11f2f99
2 changed files with 36 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2018 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.
@@ -52,6 +52,7 @@ import org.springframework.util.Assert;
* @author Thomas Risberg
* @author Michael Minella
* @author David Turanski
* @author Mahmoud Ben Hassine
*/
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {
@@ -87,6 +88,7 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
* @param serializer {@link ExecutionContextSerializer} instance to use.
*/
public void setSerializer(ExecutionContextSerializer serializer) {
Assert.notNull(serializer, "Serializer must not be null");
this.serializer = serializer;
}
@@ -208,6 +210,7 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.state(serializer != null, "ExecutionContextSerializer is required");
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2018 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.
@@ -15,14 +15,45 @@
*/
package org.springframework.batch.core.repository.dao;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.mockito.Mockito.mock;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"sql-dao-test.xml"})
public class JdbcExecutionContextDaoTests extends AbstractExecutionContextDaoTests {
@Test
public void testNoSerializer() {
try {
JdbcExecutionContextDao jdbcExecutionContextDao = new JdbcExecutionContextDao();
jdbcExecutionContextDao.setJdbcTemplate(mock(JdbcOperations.class));
jdbcExecutionContextDao.afterPropertiesSet();
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalStateException);
Assert.assertEquals("ExecutionContextSerializer is required", e.getMessage());
}
}
@Test
public void testNullSerializer() {
try {
JdbcExecutionContextDao jdbcExecutionContextDao = new JdbcExecutionContextDao();
jdbcExecutionContextDao.setJdbcTemplate(mock(JdbcOperations.class));
jdbcExecutionContextDao.setSerializer(null);
jdbcExecutionContextDao.afterPropertiesSet();
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
Assert.assertEquals("Serializer must not be null", e.getMessage());
}
}
@Override
protected JobInstanceDao getJobInstanceDao() {
return applicationContext.getBean("jobInstanceDao", JobInstanceDao.class);