Skip to content

Add assertion that a serializer was set in the JdbcExecutionContextDao #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -52,6 +52,7 @@
* @author Thomas Risberg
* @author Michael Minella
* @author David Turanski
* @author Mahmoud Ben Hassine
*/
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -208,6 +210,7 @@ public void setLobHandler(LobHandler lobHandler) {
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Assert.state(serializer != null, "ExecutionContextSerializer is required");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand Down