Skip to content

Commit

Permalink
inject jdbc template to dao tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvard Fonsell committed Oct 14, 2014
1 parent f0c3c39 commit ed29d0a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -24,6 +25,9 @@ public abstract class BaseDaoTest extends BaseNflowTest {
@Inject
protected DataSource ds;

@Inject
protected JdbcTemplate jdbc;

@After
public void truncateDb() {
ResourceDatabasePopulator populator = populator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import javax.inject.Inject;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
Expand All @@ -33,14 +31,8 @@ public class ExecutorDaoTest extends BaseDaoTest {
@Inject
ExecutorDao dao;

private JdbcTemplate jdbcTemplate;
private final DateTime started = now().minusDays(1);

@Before
public void setup() {
jdbcTemplate = new JdbcTemplate(ds);
}

@Test
public void tickCausesDeadNodeRecoveryPeriodically() {
DateTime firstNextUpdate = dao.getMaxWaitUntil();
Expand All @@ -59,10 +51,10 @@ public void recoverWorkflowInstancesFromDeadNodesSetsExecutorIdToNullAndInsertsA

dao.recoverWorkflowInstancesFromDeadNodes();

Integer executorId = jdbcTemplate.queryForObject("select executor_id from nflow_workflow where id = ?", Integer.class, id);
Integer executorId = jdbc.queryForObject("select executor_id from nflow_workflow where id = ?", Integer.class, id);
assertThat(executorId, is(nullValue()));

List<WorkflowInstanceAction> actions = jdbcTemplate.query("select * from nflow_workflow_action where workflow_id = ?",
List<WorkflowInstanceAction> actions = jdbc.query("select * from nflow_workflow_action where workflow_id = ?",
new WorkflowInstanceActionRowMapper(Collections.<Integer,Map<String, String>>emptyMap()), id);
assertThat(actions.size(), is(1));
WorkflowInstanceAction workflowInstanceAction = actions.get(0);
Expand All @@ -72,7 +64,7 @@ public void recoverWorkflowInstancesFromDeadNodesSetsExecutorIdToNullAndInsertsA

private int insertWorkflowInstance(final int crashedExecutorId) {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
jdbc.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(
Expand All @@ -90,7 +82,7 @@ public PreparedStatement createPreparedStatement(Connection connection) throws S
}

private void insertCrashedExecutor(int crashedExecutorId) {
jdbcTemplate.update(
jdbc.update(
"insert into nflow_executor (id, host, pid, executor_group, started, active, expires) values (?, ?, ?, ?, ?, ?, ?)",
crashedExecutorId, "localhost", 666, dao.getExecutorGroup(), started.toDate(), started.plusSeconds(1).toDate(), started
.plusHours(1).toDate());
Expand Down

0 comments on commit ed29d0a

Please sign in to comment.