From de9ce2d0cc863b9bcf3438de30f8593bea6827e2 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Thu, 26 Aug 2021 22:12:34 +0200 Subject: [PATCH] Use JdbcTestUtils where appropriate in tests --- .../launch/JobLauncherIntegrationTests.java | 7 ++++--- .../partition/RestartIntegrationTests.java | 9 +++++---- .../partition/VanillaIntegrationTests.java | 9 +++++---- .../repository/JdbcJobRepositoryTests.java | 13 +++++++------ ...lerantStepFactoryBeanIntegrationTests.java | 6 +++--- ...epFactoryBeanRollbackIntegrationTests.java | 6 +++--- .../batch/config/DatasourceTests.java | 5 +++-- .../JdbcPagingQueryIntegrationTests.java | 5 ++--- .../SqliteMaxValueIncrementerTests.java | 6 ++++-- .../batch/jms/ExternalRetryInBatchTests.java | 9 +++++---- .../batch/repeat/jms/AsynchronousTests.java | 9 +++++---- .../batch/repeat/jms/SynchronousTests.java | 11 ++++++----- .../batch/retry/jms/ExternalRetryTests.java | 9 +++++---- .../batch/retry/jms/SynchronousTests.java | 19 ++++++++++--------- ...positeItemWriterSampleFunctionalTests.java | 11 ++++++----- .../CustomerFilterJobFunctionalTests.java | 13 +++++++------ .../sample/FootballJobFunctionalTests.java | 11 +++++------ .../HibernateFailureJobFunctionalTests.java | 11 ++++++----- .../batch/sample/JobStepFunctionalTests.java | 10 ++++++---- .../batch/sample/MailJobFunctionalTests.java | 8 +++++--- .../batch/sample/RestartFunctionalTests.java | 14 ++++++++------ .../sample/SkipSampleFunctionalTests.java | 19 +++++++++---------- .../batch/sample/TradeJobFunctionalTests.java | 9 +++++---- .../sample/common/StagingItemReaderTests.java | 7 ++++--- .../sample/common/StagingItemWriterTests.java | 9 +++++---- .../JdbcPlayerDaoIntegrationTests.java | 8 +++++--- .../JdbcPlayerSummaryDaoIntegrationTests.java | 8 +++++--- .../TwoJobInstancesPagingFunctionalTests.java | 10 ++++++---- .../batch/test/AbstractSampleJobTests.java | 3 ++- .../batch/test/SampleStepTests.java | 5 +++-- 30 files changed, 154 insertions(+), 125 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/JobLauncherIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/JobLauncherIntegrationTests.java index c0910b9f9..0ece416e2 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/JobLauncherIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/JobLauncherIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2014 the original author or authors. + * Copyright 2009-2021 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. @@ -32,6 +32,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @@ -53,13 +54,13 @@ public class JobLauncherIntegrationTests { @Test public void testLaunchAndRelaunch() throws Exception { - int before = jdbcTemplate.queryForObject("select count(*) from BATCH_JOB_INSTANCE", Integer.class); + int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_INSTANCE"); JobExecution jobExecution = launch(true,0); launch(false, jobExecution.getId()); launch(false, jobExecution.getId()); - int after = jdbcTemplate.queryForObject("select count(*) from BATCH_JOB_INSTANCE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_INSTANCE"); assertEquals(before+1, after); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/partition/RestartIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/partition/RestartIntegrationTests.java index 0d40ab6fd..4a51db655 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/partition/RestartIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/partition/RestartIntegrationTests.java @@ -34,6 +34,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; /** * @author Dave Syer @@ -75,8 +76,8 @@ public class RestartIntegrationTests { ExampleItemReader.fail = true; JobParameters jobParameters = new JobParametersBuilder().addString("restart", "yes").toJobParameters(); - int beforeManager = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME='step1:manager'", Integer.class); - int beforePartition = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME like 'step1:partition%'", Integer.class); + int beforeManager = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME='step1:manager'"); + int beforePartition = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME like 'step1:partition%'"); ExampleItemWriter.clear(); JobExecution execution = jobLauncher.run(job, jobParameters); @@ -89,8 +90,8 @@ public class RestartIntegrationTests { // Only 4 because the others were processed in the first attempt assertEquals(4, ExampleItemWriter.getItems().size()); - int afterManager = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME='step1:manager'", Integer.class); - int afterPartition = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME like 'step1:partition%'", Integer.class); + int afterManager = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME='step1:manager'"); + int afterPartition = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME like 'step1:partition%'"); // Two attempts assertEquals(2, afterManager-beforeManager); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/partition/VanillaIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/partition/VanillaIntegrationTests.java index f062dcc0a..1b2c805a7 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/partition/VanillaIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/partition/VanillaIntegrationTests.java @@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; /** * @author Dave Syer @@ -59,11 +60,11 @@ public class VanillaIntegrationTests { @Test public void testLaunchJob() throws Exception { - int beforeManager = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME='step1:manager'", Integer.class); - int beforePartition = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME like 'step1:partition%'", Integer.class); + int beforeManager = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME='step1:manager'"); + int beforePartition = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME like 'step1:partition%'"); assertNotNull(jobLauncher.run(job, new JobParameters())); - int afterManager = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME='step1:manager'", Integer.class); - int afterPartition = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STEP_EXECUTION where STEP_NAME like 'step1:partition%'", Integer.class); + int afterManager = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME='step1:manager'"); + int afterPartition = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "BATCH_STEP_EXECUTION", "STEP_NAME like 'step1:partition%'"); assertEquals(1, afterManager-beforeManager); // Should be same as grid size in step splitter assertEquals(2, afterPartition-beforePartition); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/test/repository/JdbcJobRepositoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/test/repository/JdbcJobRepositoryTests.java index d82c4120f..1314d9f3d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/test/repository/JdbcJobRepositoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/test/repository/JdbcJobRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2019 the original author or authors. + * Copyright 2006-2021 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. @@ -40,6 +40,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -84,7 +85,7 @@ public class JdbcJobRepositoryTests extends AbstractIntegrationTests { job.setName("foo"); int before = 0; JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters()); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_INSTANCE");; assertEquals(before + 1, after); assertNotNull(execution.getId()); } @@ -96,7 +97,7 @@ public class JdbcJobRepositoryTests extends AbstractIntegrationTests { JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters()); execution.getExecutionContext().put("foo", "bar"); repository.updateExecutionContext(execution); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_EXECUTION_CONTEXT", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION_CONTEXT"); assertEquals(before + 1, after); assertNotNull(execution.getId()); JobExecution last = repository.getLastJobExecution(job.getName(), new JobParameters()); @@ -126,7 +127,7 @@ public class JdbcJobRepositoryTests extends AbstractIntegrationTests { assertNotNull(execution); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_INSTANCE"); assertNotNull(execution.getId()); assertEquals(before + 1, after); @@ -147,7 +148,7 @@ public class JdbcJobRepositoryTests extends AbstractIntegrationTests { repository.update(execution); execution.setStatus(BatchStatus.FAILED); - int before = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE", Integer.class); + int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_INSTANCE"); assertEquals(1, before); long t0 = System.currentTimeMillis(); @@ -160,7 +161,7 @@ public class JdbcJobRepositoryTests extends AbstractIntegrationTests { } long t1 = System.currentTimeMillis(); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_INSTANCE"); assertNotNull(execution.getId()); assertEquals(before, after); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanIntegrationTests.java index 249c8861b..e507875be 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 the original author or authors. + * Copyright 2010-2021 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. @@ -226,7 +226,7 @@ public class FaultTolerantStepFactoryBeanIntegrationTests { public void clear() { written.clear(); - jdbcTemplate.update("DELETE FROM ERROR_LOG where STEP_NAME='written'"); + JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "ERROR_LOG", "STEP_NAME='written'"); } @Override @@ -272,7 +272,7 @@ public class FaultTolerantStepFactoryBeanIntegrationTests { public void clear() { processed.clear(); - jdbcTemplate.update("DELETE FROM ERROR_LOG where STEP_NAME='processed'"); + JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "ERROR_LOG", "STEP_NAME='processed'"); } @Nullable diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanRollbackIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanRollbackIntegrationTests.java index 6052705ec..d26daf362 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanRollbackIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/test/step/FaultTolerantStepFactoryBeanRollbackIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 the original author or authors. + * Copyright 2010-2021 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. @@ -251,7 +251,7 @@ public class FaultTolerantStepFactoryBeanRollbackIntegrationTests { public void clear() { written.clear(); - jdbcTemplate.update("DELETE FROM ERROR_LOG where STEP_NAME='written'"); + JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "ERROR_LOG", "STEP_NAME='written'"); } @Override @@ -304,7 +304,7 @@ public class FaultTolerantStepFactoryBeanRollbackIntegrationTests { public void clear() { processed.clear(); - jdbcTemplate.update("DELETE FROM ERROR_LOG where STEP_NAME='processed'"); + JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "ERROR_LOG", "STEP_NAME='processed'"); } @Nullable diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/config/DatasourceTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/config/DatasourceTests.java index b42c4116e..8bc20bf20 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/config/DatasourceTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/config/DatasourceTests.java @@ -22,6 +22,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.ContextConfiguration; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.annotation.Transactional; import org.junit.runner.RunWith; import org.junit.Test; @@ -36,8 +37,8 @@ public class DatasourceTests { @Transactional @Test public void testTemplate() throws Exception { System.err.println(System.getProperty("java.class.path")); - jdbcTemplate.execute("delete from T_BARS"); - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); jdbcTemplate.update("INSERT into T_BARS (id,name,foo_date) values (?,?,null)", 0, "foo"); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java index 8ff6aa8e0..6db954573 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingQueryIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2012 the original author or authors. + * Copyright 2006-2021 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. @@ -48,6 +48,7 @@ import static org.junit.Assert.assertTrue; /** * @author Dave Syer * @author Michael Minella + * @author Mahmoud Ben Hassine * @since 2.1 */ @RunWith(SpringJUnit4ClassRunner.class) @@ -74,7 +75,6 @@ public class JdbcPagingQueryIntegrationTests { String[] names = {"Foo", "Bar", "Baz", "Foo", "Bar", "Baz", "Foo", "Bar", "Baz"}; String[] codes = {"A", "B", "A", "B", "B", "B", "A", "B", "A"}; JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_FOOS"); -// jdbcTemplate.update("DELETE from T_FOOS"); for(int i = 0; i < names.length; i++) { jdbcTemplate.update("INSERT into T_FOOS (ID,NAME, CODE, VALUE) values (?, ?, ?, ?)", maxId, names[i], codes[i], i); maxId++; @@ -85,7 +85,6 @@ public class JdbcPagingQueryIntegrationTests { @After public void destroy() { JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_FOOS"); -// jdbcTemplate.update("DELETE from T_FOOS"); } @Test diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/support/SqliteMaxValueIncrementerTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/support/SqliteMaxValueIncrementerTests.java index 6fb46ce26..e252fce61 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/support/SqliteMaxValueIncrementerTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/support/SqliteMaxValueIncrementerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2021 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. @@ -24,9 +24,11 @@ import org.junit.BeforeClass; import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.SimpleDriverDataSource; +import org.springframework.test.jdbc.JdbcTestUtils; /** * @author Luke Taylor + * @author Mahmoud Ben Hassine */ public class SqliteMaxValueIncrementerTests { static String dbFile; @@ -59,6 +61,6 @@ public class SqliteMaxValueIncrementerTests { assertEquals(1, mvi.getNextKey()); assertEquals(2, mvi.getNextKey()); assertEquals(3, mvi.getNextKey()); - assertEquals(1, template.queryForObject("select count(*) from max_value", Integer.class).intValue()); + assertEquals(1, JdbcTestUtils.countRowsInTable(template, "max_value")); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/jms/ExternalRetryInBatchTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/jms/ExternalRetryInBatchTests.java index 2e5a9c3d8..095e65501 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/jms/ExternalRetryInBatchTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/jms/ExternalRetryInBatchTests.java @@ -39,6 +39,7 @@ import org.springframework.retry.support.DefaultRetryState; import org.springframework.retry.support.RetryTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; @@ -73,7 +74,7 @@ public class ExternalRetryInBatchTests { @Before public void onSetUp() throws Exception { getMessages(); // drain queue - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); jmsTemplate.convertAndSend("queue", "foo"); jmsTemplate.convertAndSend("queue", "bar"); provider = new ItemReader() { @@ -91,11 +92,11 @@ public class ExternalRetryInBatchTests { @After public void onTearDown() throws Exception { getMessages(); // drain queue - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); } private void assertInitialState() { - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); } @@ -189,7 +190,7 @@ public class ExternalRetryInBatchTests { assertEquals(2, recovered.size()); // The database portion committed once... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); // ... and so did the message session. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java index 4c6f6aeb2..7e93a29c8 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/AsynchronousTests.java @@ -36,6 +36,7 @@ import org.springframework.jms.listener.SessionAwareMessageListener; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -63,7 +64,7 @@ public class AsynchronousTests { foo = (String) jmsTemplate.receiveAndConvert("queue"); count++; } - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); // Queue is now drained... assertNull(foo); @@ -90,7 +91,7 @@ public class AsynchronousTests { private volatile List list = new ArrayList<>(); private void assertInitialState() { - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); } @@ -122,7 +123,7 @@ public class AsynchronousTests { String foo = (String) jmsTemplate.receiveAndConvert("queue"); assertEquals(null, foo); - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(2, count); } @@ -169,7 +170,7 @@ public class AsynchronousTests { msgs.add(text); } - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); assertTrue("Foo not on queue", msgs.contains("foo")); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java index 5e80e9995..b986068c7 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/jms/SynchronousTests.java @@ -45,6 +45,7 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionCallback; @@ -84,13 +85,13 @@ public class SynchronousTests implements ApplicationContextAware { foo = (String) jmsTemplate.receiveAndConvert("queue"); count++; } - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); jmsTemplate.convertAndSend("queue", "foo"); jmsTemplate.convertAndSend("queue", "bar"); } private void assertInitialState() { - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); } @@ -110,7 +111,7 @@ public class SynchronousTests implements ApplicationContextAware { } }); - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(2, count); assertTrue(list.contains("foo")); @@ -154,7 +155,7 @@ public class SynchronousTests implements ApplicationContextAware { } // The database portion rolled back... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); // ... and so did the message session. The rollback should have restored @@ -221,7 +222,7 @@ public class SynchronousTests implements ApplicationContextAware { } // The database portion committed... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(2, count); // ...but the JMS session rolled back, so the message is still there diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/ExternalRetryTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/ExternalRetryTests.java index 87df8dbca..74b441768 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/ExternalRetryTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/ExternalRetryTests.java @@ -32,6 +32,7 @@ import org.springframework.retry.support.DefaultRetryState; import org.springframework.retry.support.RetryTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; @@ -64,7 +65,7 @@ public class ExternalRetryTests { @Before public void onSetUp() throws Exception { getMessages(); // drain queue - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); jmsTemplate.convertAndSend("queue", "foo"); provider = new ItemReader() { @Nullable @@ -79,7 +80,7 @@ public class ExternalRetryTests { } private void assertInitialState() { - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); } @@ -167,7 +168,7 @@ public class ExternalRetryTests { List msgs = getMessages(); // The database portion committed once... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(1, count); // ... and so did the message session. @@ -235,7 +236,7 @@ public class ExternalRetryTests { assertEquals(1, recovered.size()); // The database portion committed once... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); // ... and so did the message session. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/SynchronousTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/SynchronousTests.java index b052fc9a9..67af4bab1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/SynchronousTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/jms/SynchronousTests.java @@ -30,6 +30,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.Transactional; @@ -61,7 +62,7 @@ public class SynchronousTests { @BeforeTransaction public void onSetUpBeforeTransaction() throws Exception { - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); jmsTemplate.convertAndSend("queue", "foo"); jmsTemplate.convertAndSend("queue", "foo"); final String text = (String) jmsTemplate.receiveAndConvert("queue"); @@ -81,11 +82,11 @@ public class SynchronousTests { foo = (String) jmsTemplate.receiveAndConvert("queue"); count++; } - jdbcTemplate.execute("delete from T_BARS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "T_BARS"); } private void assertInitialState() { - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); } @@ -140,7 +141,7 @@ public class SynchronousTests { List msgs = getMessages(); // The database portion committed once... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(1, count); // ... and so did the message session. @@ -193,7 +194,7 @@ public class SynchronousTests { List msgs = getMessages(); // The database portion committed once... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(1, count); // ... and so did the message session. @@ -252,7 +253,7 @@ public class SynchronousTests { } // The nested database transaction has committed... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(1, count); // force rollback... @@ -267,7 +268,7 @@ public class SynchronousTests { List msgs = getMessages(); // The database portion rolled back... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); // ... and so did the message session. @@ -316,7 +317,7 @@ public class SynchronousTests { List msgs = getMessages(); // The database portion committed once... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(1, count); // ... and so did the message session. @@ -375,7 +376,7 @@ public class SynchronousTests { List msgs = getMessages(); // The database portion rolled back... - int count = jdbcTemplate.queryForObject("select count(*) from T_BARS", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_BARS"); assertEquals(0, count); // ... and so did the message session. diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java index 0b8299c69..461a369f2 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2014 the original author or authors. + * Copyright 2008-2021 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. @@ -36,6 +36,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import static org.junit.Assert.assertEquals; @@ -49,7 +50,7 @@ public class CompositeItemWriterSampleFunctionalTests { + "Trade: [isin=UK21341EAH44,quantity=214,price=34.11,customer=customer4]" + "Trade: [isin=UK21341EAH45,quantity=215,price=35.11,customer=customer5]"; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @@ -61,8 +62,8 @@ public class CompositeItemWriterSampleFunctionalTests { @Test public void testJobLaunch() throws Exception { - jdbcTemplate.update("DELETE from TRADE"); - int before = jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE", Integer.class); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); + int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); jobLauncherTestUtils.launchJob(); @@ -83,7 +84,7 @@ public class CompositeItemWriterSampleFunctionalTests { } }; - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); assertEquals(before + 5, after); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java index fc657be9d..ab0a67370 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -39,6 +39,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/customerFilterJob.xml", "/job-runner-context.xml" }) @@ -46,7 +47,7 @@ public class CustomerFilterJobFunctionalTests { private static final String GET_CUSTOMERS = "select NAME, CREDIT from CUSTOMER order by NAME"; private List customers; private int activeRow = 0; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; private Map credits = new HashMap<>(); @Autowired @@ -59,8 +60,8 @@ public class CustomerFilterJobFunctionalTests { @Before public void onSetUp() throws Exception { - jdbcTemplate.update("delete from TRADE"); - jdbcTemplate.update("delete from CUSTOMER where ID > 4"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); + JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "CUSTOMER", "ID > 4"); jdbcTemplate.update("update CUSTOMER set credit=100000"); List> list = jdbcTemplate.queryForList("select name, CREDIT from CUSTOMER"); @@ -72,8 +73,8 @@ public class CustomerFilterJobFunctionalTests { @After public void tearDown() throws Exception { - jdbcTemplate.update("delete from TRADE"); - jdbcTemplate.update("delete from CUSTOMER where ID > 4"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); + JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "CUSTOMER", "ID > 4"); } @Test diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java index fdbbeaef9..a8fa318c5 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2014 the original author or authors. + * Copyright 2007-2021 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. @@ -27,13 +27,14 @@ import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/footballJob.xml", "/job-runner-context.xml" }) public class FootballJobFunctionalTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { @@ -42,13 +43,11 @@ public class FootballJobFunctionalTests { @Test public void testLaunchJob() throws Exception { - jdbcTemplate.update("DELETE FROM PLAYERS"); - jdbcTemplate.update("DELETE FROM GAMES"); - jdbcTemplate.update("DELETE FROM PLAYER_SUMMARY"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY"); jobLauncherTestUtils.launchJob(); - int count = jdbcTemplate.queryForObject("SELECT COUNT(*) from PLAYER_SUMMARY", Integer.class); + int count = JdbcTestUtils.countRowsInTable(jdbcTemplate, "PLAYER_SUMMARY"); assertTrue(count > 0); } } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java index 2955d8d25..9f357f6ab 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2014 the original author or authors. + * Copyright 2007-2021 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. @@ -43,6 +43,7 @@ import org.springframework.jdbc.core.RowMapper; import org.springframework.orm.hibernate5.HibernateJdbcException; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; @@ -53,13 +54,13 @@ import org.springframework.transaction.support.TransactionTemplate; * expected value. * * @author Dave Syer + * @author Mahmoud Ben Hassine */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/hibernate-context.xml", "/jobs/hibernateJob.xml", "/job-runner-context.xml" }) public class HibernateFailureJobFunctionalTests { private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseProcessor.FIXED_AMOUNT; - private static final String DELETE_CUSTOMERS = "DELETE FROM CUSTOMER"; private static final String ALL_CUSTOMERS = "select * from CUSTOMER order by ID"; private static final String CREDIT_COLUMN = "CREDIT"; private static String[] customers = { "INSERT INTO CUSTOMER (id, version, name, credit) VALUES (1, 0, 'customer1', 100000)", @@ -71,7 +72,7 @@ public class HibernateFailureJobFunctionalTests { @Autowired private HibernateCreditDao writer; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; private PlatformTransactionManager transactionManager; private List creditsBeforeUpdate; @@ -108,7 +109,7 @@ public class HibernateFailureJobFunctionalTests { throw e; } - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) from CUSTOMER", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "CUSTOMER"); assertEquals(4, after); validatePostConditions(); @@ -141,7 +142,7 @@ public class HibernateFailureJobFunctionalTests { @Override public Void doInTransaction(TransactionStatus status) { - jdbcTemplate.update(DELETE_CUSTOMERS); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "CUSTOMER"); for (String customer : customers) { jdbcTemplate.update(customer); } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobStepFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobStepFunctionalTests.java index 8a279619e..16ad682c2 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobStepFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobStepFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -30,18 +30,20 @@ import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; /** * Sample using a step to launch a job. * * @author Dave Syer + * @author Mahmoud Ben Hassine */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class JobStepFunctionalTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { @@ -50,13 +52,13 @@ public class JobStepFunctionalTests { @Test public void testJobLaunch() throws Exception { - jdbcTemplate.update("DELETE FROM TRADE"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); jobLauncherTestUtils.launchJob(new DefaultJobParametersConverter() .getJobParameters(PropertiesConverter .stringToProperties("run.id(long)=1,parameter=true,run.date=20070122,input.file=classpath:data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt"))); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM TRADE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); assertEquals(5, after); } } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java index 8dfc4ff39..c184d7282 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2012 the original author or authors. + * Copyright 2006-2021 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. @@ -38,10 +38,12 @@ import org.springframework.mail.MailMessage; import org.springframework.mail.SimpleMailMessage; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; /** * @author Dan Garrette * @author Dave Syer + * @author Mahmoud Ben Hassine * * @Since 2.1 */ @@ -67,7 +69,7 @@ public class MailJobFunctionalTests { private static final Object[] USER8 = new Object[] { 8, "Martin Van Buren", email }; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @@ -92,7 +94,7 @@ public class MailJobFunctionalTests { @After public void after() throws Exception { - jdbcTemplate.update("drop table USERS"); + JdbcTestUtils.dropTables(jdbcTemplate, "USERS"); } @Test diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java index 63f384ad7..e3cf8cc96 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -33,18 +33,20 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.test.jdbc.JdbcTestUtils; /** * Simple restart scenario. * * @author Robert Kasanicky * @author Dave Syer + * @author Mahmoud Ben Hassine */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/restartSample.xml", "/job-runner-context.xml" }) public class RestartFunctionalTests { - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @@ -56,7 +58,7 @@ public class RestartFunctionalTests { @BeforeTransaction public void onTearDown() throws Exception { - jdbcTemplate.update("DELETE FROM TRADE"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); } /** @@ -70,7 +72,7 @@ public class RestartFunctionalTests { */ @Test public void testLaunchJob() throws Exception { - int before = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM TRADE", Integer.class); + int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); JobExecution jobExecution = runJobForRestartTest(); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); @@ -83,14 +85,14 @@ public class RestartFunctionalTests { throw new RuntimeException(ex); } - int medium = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM TRADE", Integer.class); + int medium = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); // assert based on commit interval = 2 assertEquals(before + 2, medium); jobExecution = runJobForRestartTest(); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM TRADE", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); assertEquals(before + 5, after); } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java index 888298ba3..046ab9c60 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java @@ -70,7 +70,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = { "/skipSample-job-launcher-context.xml" }) public class SkipSampleFunctionalTests { - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private JobExplorer jobExplorer; @@ -89,12 +89,11 @@ public class SkipSampleFunctionalTests { @Before public void setUp() { - jdbcTemplate.update("DELETE from TRADE"); - jdbcTemplate.update("DELETE from CUSTOMER"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE", "CUSTOMER"); for (int i = 1; i < 10; i++) { jdbcTemplate.update("INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (" + incrementer.nextIntValue() + ", 0, 'customer" + i + "', 100000)"); } - jdbcTemplate.update("DELETE from ERROR_LOG"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "ERROR_LOG"); } /** @@ -263,18 +262,18 @@ public class SkipSampleFunctionalTests { private void validateLaunchWithSkips(JobExecution jobExecution) { // Step1: 9 input records, 1 skipped in read, 1 skipped in write => // 7 written to output - assertEquals(7, JdbcTestUtils.countRowsInTable((JdbcTemplate) jdbcTemplate, "TRADE")); + assertEquals(7, JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE")); // Step2: 7 input records, 1 skipped on process, 1 on write => 5 written // to output // System.err.println(jdbcTemplate.queryForList("SELECT * FROM TRADE")); - assertEquals(5, jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE where VERSION=?", Integer.class, 1).intValue()); + assertEquals(5, JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "TRADE", "VERSION=1")); // 1 record skipped in processing second step assertEquals(1, SkipCheckingListener.getProcessSkips()); // Both steps contained skips - assertEquals(2, JdbcTestUtils.countRowsInTable((JdbcTemplate) jdbcTemplate, "ERROR_LOG")); + assertEquals(2, JdbcTestUtils.countRowsInTable(jdbcTemplate, "ERROR_LOG")); assertEquals("2 records were skipped!", jdbcTemplate.queryForObject( "SELECT MESSAGE from ERROR_LOG where JOB_NAME = ? and STEP_NAME = ?", String.class, "skipJob", "step1")); @@ -293,13 +292,13 @@ public class SkipSampleFunctionalTests { private void validateLaunchWithoutSkips(JobExecution jobExecution) { // Step1: 5 input records => 5 written to output - assertEquals(5, JdbcTestUtils.countRowsInTable((JdbcTemplate) jdbcTemplate, "TRADE")); + assertEquals(5, JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE")); // Step2: 5 input records => 5 written to output - assertEquals(5, jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE where VERSION=?", Integer.class, 1).intValue()); + assertEquals(5, JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "TRADE", "VERSION=1")); // Neither step contained skips - assertEquals(0, JdbcTestUtils.countRowsInTable((JdbcTemplate) jdbcTemplate, "ERROR_LOG")); + assertEquals(0, JdbcTestUtils.countRowsInTable(jdbcTemplate, "ERROR_LOG")); assertEquals(new BigDecimal("270.75"), jobExecution.getExecutionContext().get(TradeWriter.TOTAL_AMOUNT_KEY)); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java index 0e426bd5f..c77c80e43 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -41,6 +41,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/tradeJob.xml", @@ -52,7 +53,7 @@ public class TradeJobFunctionalTests { private List customers; private List trades; private int activeRow = 0; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; private Map credits = new HashMap<>(); @Autowired @@ -65,7 +66,7 @@ public class TradeJobFunctionalTests { @Before public void onSetUp() throws Exception { - jdbcTemplate.update("delete from TRADE"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); List> list = jdbcTemplate.queryForList("select NAME, CREDIT from CUSTOMER"); for (Map map : list) { @@ -75,7 +76,7 @@ public class TradeJobFunctionalTests { @After public void tearDown() throws Exception { - jdbcTemplate.update("delete from TRADE"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); } @Test diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemReaderTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemReaderTests.java index 6afee1135..2dcce23f9 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemReaderTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2014 the original author or authors. + * Copyright 2008-2021 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. @@ -35,6 +35,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; @@ -45,7 +46,7 @@ import org.springframework.transaction.support.TransactionTemplate; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration() public class StagingItemReaderTests { - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private PlatformTransactionManager transactionManager; @@ -75,7 +76,7 @@ public class StagingItemReaderTests { @AfterTransaction public void onTearDownAfterTransaction() throws Exception { reader.destroy(); - jdbcTemplate.update("DELETE FROM BATCH_STAGING"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "BATCH_STAGING"); } @Transactional diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemWriterTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemWriterTests.java index 649d0d25f..5932439ca 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemWriterTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/StagingItemWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -33,12 +33,13 @@ import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class StagingItemWriterTests { - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private StagingItemWriter writer; @@ -58,9 +59,9 @@ public class StagingItemWriterTests { @Transactional @Test public void testProcessInsertsNewItem() throws Exception { - int before = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STAGING", Integer.class); + int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING"); writer.write(Collections.singletonList("FOO")); - int after = jdbcTemplate.queryForObject("SELECT COUNT(*) from BATCH_STAGING", Integer.class); + int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING"); assertEquals(before + 1, after); } } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerDaoIntegrationTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerDaoIntegrationTests.java index 253ecba66..5fde3faf4 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerDaoIntegrationTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerDaoIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -32,10 +32,12 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.annotation.Transactional; /** * @author Lucas Ward + * @author Mahmoud Ben Hassine * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -44,7 +46,7 @@ public class JdbcPlayerDaoIntegrationTests { private JdbcPlayerDao playerDao; private Player player; private static final String GET_PLAYER = "SELECT * from PLAYERS"; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired public void init(DataSource dataSource) { @@ -63,7 +65,7 @@ public class JdbcPlayerDaoIntegrationTests { @Before public void onSetUpInTransaction() throws Exception { - jdbcTemplate.execute("delete from PLAYERS"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS"); } @Test diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerSummaryDaoIntegrationTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerSummaryDaoIntegrationTests.java index f313bd680..38c8e3d6f 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerSummaryDaoIntegrationTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/football/internal/JdbcPlayerSummaryDaoIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -30,10 +30,12 @@ import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.annotation.Transactional; /** * @author Lucas Ward + * @author Mahmoud Ben Hassine * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -41,7 +43,7 @@ import org.springframework.transaction.annotation.Transactional; public class JdbcPlayerSummaryDaoIntegrationTests { private JdbcPlayerSummaryDao playerSummaryDao; private PlayerSummary summary; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired public void init(DataSource dataSource) { @@ -66,7 +68,7 @@ public class JdbcPlayerSummaryDaoIntegrationTests { @Before public void onSetUpInTransaction() throws Exception { - jdbcTemplate.execute("delete from PLAYER_SUMMARY"); + JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYER_SUMMARY"); } @Test diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java index e261267a0..a1276bc7a 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2014 the original author or authors. + * Copyright 2006-2021 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. @@ -36,9 +36,11 @@ import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; /** * @author Dave Syer + * @author Mahmoud Ben Hassine * @since 2.0 */ @RunWith(SpringJUnit4ClassRunner.class) @@ -51,7 +53,7 @@ public class TwoJobInstancesPagingFunctionalTests { @Autowired private Job job; - private JdbcOperations jdbcTemplate; + private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { @@ -60,11 +62,11 @@ public class TwoJobInstancesPagingFunctionalTests { @Test public void testLaunchJobTwice() throws Exception { - int first = jdbcTemplate.queryForObject("select count(0) from CUSTOMER where credit>1000", Integer.class); + int first = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "CUSTOMER", "credit>1000"); JobExecution jobExecution = launcher.run(this.job, getJobParameters(1000.)); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); assertEquals(first, jobExecution.getStepExecutions().iterator().next().getWriteCount()); - int second = jdbcTemplate.queryForObject("select count(0) from CUSTOMER where credit>1000000", Integer.class); + int second = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "CUSTOMER", "credit>1000000"); assertNotSame("The number of records above the threshold did not change", first, second); jobExecution = launcher.run(this.job, getJobParameters(1000000.)); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java index 3baa37e0c..80a04910b 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java @@ -29,6 +29,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.annotation.Repeat; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.jdbc.JdbcTestUtils; /** * This is an abstract test class. @@ -61,7 +62,7 @@ public abstract class AbstractSampleJobTests { @After public void tearDown() { - this.jdbcTemplate.update("drop table TESTS"); + JdbcTestUtils.dropTables(this.jdbcTemplate, "TESTS"); } @Test diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SampleStepTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SampleStepTests.java index c5ce02aae..087caa7e2 100755 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SampleStepTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SampleStepTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2014 the original author or authors. + * Copyright 2008-2021 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. @@ -32,6 +32,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.JdbcTestUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/sample-steps.xml" }) @@ -58,7 +59,7 @@ public class SampleStepTests implements ApplicationContextAware { @After public void tearDown() { - this.jdbcTemplate.update("drop table TESTS"); + JdbcTestUtils.dropTables(this.jdbcTemplate, "TESTS"); } @Test