Use JdbcTestUtils where appropriate in tests

This commit is contained in:
Mahmoud Ben Hassine
2021-08-26 22:12:34 +02:00
parent 3742931c4c
commit de9ce2d0cc
30 changed files with 154 additions and 125 deletions

View File

@@ -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");

View File

@@ -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

View File

@@ -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"));
}
}

View File

@@ -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<String>() {
@@ -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.

View File

@@ -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<String> 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"));

View File

@@ -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

View File

@@ -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<String>() {
@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<String> 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.

View File

@@ -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<String> 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<String> 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<String> 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<String> 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<String> 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.