diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java index 2e51f759f4..adba2a3863 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDao.java @@ -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 @@ * @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 void setLobHandler(LobHandler lobHandler) { @Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); + Assert.state(serializer != null, "ExecutionContextSerializer is required"); } /** diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDaoTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDaoTests.java index 2843fcfcf2..2bd77b35fc 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDaoTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcExecutionContextDaoTests.java @@ -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);