Some JDBC tests polishing and optimization
**Cherry-pick to master** Conflicts: build.gradle spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-derby-context.xml
This commit is contained in:
@@ -479,8 +479,6 @@ project('spring-integration-jpa') {
|
||||
}
|
||||
|
||||
testCompile "com.h2database:h2:$h2Version"
|
||||
testCompile "org.hsqldb:hsqldb:$hsqldbVersion"
|
||||
testCompile "org.apache.derby:derby:$derbyVersion"
|
||||
|
||||
testCompile "org.hibernate:hibernate-entitymanager:$hibernateVersion"
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -23,7 +23,8 @@ import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
@@ -40,29 +41,35 @@ import org.springframework.messaging.support.GenericMessage;
|
||||
*/
|
||||
public class JdbcMessageHandlerIntegrationTests {
|
||||
|
||||
private EmbeddedDatabase embeddedDatabase;
|
||||
private static EmbeddedDatabase embeddedDatabase;
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private static JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||
builder.setType(EmbeddedDatabaseType.HSQL).addScript(
|
||||
"classpath:org/springframework/integration/jdbc/messageHandlerIntegrationTest.sql");
|
||||
this.embeddedDatabase = builder.build();
|
||||
this.jdbcTemplate = new JdbcTemplate(this.embeddedDatabase);
|
||||
embeddedDatabase = builder.build();
|
||||
jdbcTemplate = new JdbcTemplate(embeddedDatabase);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
embeddedDatabase.shutdown();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.embeddedDatabase.shutdown();
|
||||
public void cleanup() {
|
||||
jdbcTemplate.execute("DELETE FROM FOOS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleStaticInsert() {
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate, "insert into foos (id, status, name) values (1, 0, 'foo')");
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,
|
||||
"insert into foos (id, status, name) values (1, 0, 'foo')");
|
||||
handler.afterPropertiesSet();
|
||||
Message<String> message = new GenericMessage<String>("foo");
|
||||
Message<String> message = new GenericMessage<>("foo");
|
||||
handler.handleMessage(message);
|
||||
Map<String, Object> map = jdbcTemplate.queryForMap("SELECT * FROM FOOS WHERE ID=?", 1);
|
||||
assertEquals("Wrong id", "1", map.get("ID"));
|
||||
@@ -72,9 +79,10 @@ public class JdbcMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void testSimpleDynamicInsert() {
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate, "insert into foos (id, status, name) values (1, 0, :payload)");
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,
|
||||
"insert into foos (id, status, name) values (1, 0, :payload)");
|
||||
handler.afterPropertiesSet();
|
||||
Message<String> message = new GenericMessage<String>("foo");
|
||||
Message<String> message = new GenericMessage<>("foo");
|
||||
handler.handleMessage(message);
|
||||
Map<String, Object> map = jdbcTemplate.queryForMap("SELECT * FROM FOOS WHERE ID=?", 1);
|
||||
assertEquals("Wrong name", "foo", map.get("NAME"));
|
||||
@@ -82,14 +90,15 @@ public class JdbcMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void testInsertWithMessagePreparedStatementSetter() {
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate, "insert into foos (id, status, name) values (1, 0, ?)");
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,
|
||||
"insert into foos (id, status, name) values (1, 0, ?)");
|
||||
final AtomicBoolean setterInvoked = new AtomicBoolean();
|
||||
handler.setPreparedStatementSetter((ps, requestMessage) -> {
|
||||
ps.setObject(1, requestMessage.getPayload());
|
||||
setterInvoked.set(true);
|
||||
});
|
||||
handler.afterPropertiesSet();
|
||||
Message<String> message = new GenericMessage<String>("foo");
|
||||
Message<String> message = new GenericMessage<>("foo");
|
||||
handler.handleMessage(message);
|
||||
Map<String, Object> map = jdbcTemplate.queryForMap("SELECT * FROM FOOS WHERE ID=?", 1);
|
||||
assertEquals("Wrong name", "foo", map.get("NAME"));
|
||||
@@ -98,9 +107,10 @@ public class JdbcMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void testIdHeaderDynamicInsert() {
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate, "insert into foos (id, status, name) values (:headers[idAsString], 0, :payload)");
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,
|
||||
"insert into foos (id, status, name) values (:headers[idAsString], 0, :payload)");
|
||||
handler.afterPropertiesSet();
|
||||
Message<String> message = new GenericMessage<String>("foo");
|
||||
Message<String> message = new GenericMessage<>("foo");
|
||||
String id = message.getHeaders().getId().toString();
|
||||
message = MessageBuilder.fromMessage(message)
|
||||
.setHeader("idAsString", message.getHeaders().getId().toString())
|
||||
@@ -113,7 +123,8 @@ public class JdbcMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void testDottedHeaderDynamicInsert() {
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate, "insert into foos (id, status, name) values (:headers[business.id], 0, :payload)");
|
||||
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,
|
||||
"insert into foos (id, status, name) values (:headers[business.id], 0, :payload)");
|
||||
handler.afterPropertiesSet();
|
||||
Message<String> message = MessageBuilder.withPayload("foo").setHeader("business.id", "FOO").build();
|
||||
handler.handleMessage(message);
|
||||
|
||||
@@ -20,7 +20,9 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
@@ -39,6 +41,18 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
*/
|
||||
public class JdbcOutboundGatewayTests {
|
||||
|
||||
private static EmbeddedDatabase dataSource;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
dataSource = new EmbeddedDatabaseBuilder().build();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void teardown() {
|
||||
dataSource.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetMaxRowsPerPollWithoutSelectQuery() {
|
||||
EmbeddedDatabase dataSource = new EmbeddedDatabaseBuilder().build();
|
||||
@@ -61,7 +75,6 @@ public class JdbcOutboundGatewayTests {
|
||||
|
||||
@Test
|
||||
public void testConstructorWithNullJdbcOperations() {
|
||||
|
||||
JdbcOperations jdbcOperations = null;
|
||||
|
||||
try {
|
||||
@@ -77,8 +90,6 @@ public class JdbcOutboundGatewayTests {
|
||||
|
||||
@Test
|
||||
public void testConstructorWithEmptyAndNullQueries() {
|
||||
EmbeddedDatabase dataSource = new EmbeddedDatabaseBuilder().build();
|
||||
|
||||
final String selectQuery = " ";
|
||||
final String updateQuery = null;
|
||||
|
||||
@@ -90,14 +101,10 @@ public class JdbcOutboundGatewayTests {
|
||||
catch (IllegalArgumentException e) {
|
||||
Assert.assertEquals("The 'updateQuery' and the 'selectQuery' must not both be null or empty.", e.getMessage());
|
||||
}
|
||||
|
||||
dataSource.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetMaxRowsPerPoll() {
|
||||
EmbeddedDatabase dataSource = new EmbeddedDatabaseBuilder().build();
|
||||
|
||||
JdbcOutboundGateway jdbcOutboundGateway = new JdbcOutboundGateway(dataSource, "select * from DOES_NOT_EXIST");
|
||||
|
||||
try {
|
||||
@@ -108,8 +115,6 @@ public class JdbcOutboundGatewayTests {
|
||||
catch (IllegalArgumentException e) {
|
||||
assertEquals("MaxRowsPerPoll must not be null.", e.getMessage());
|
||||
}
|
||||
|
||||
dataSource.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2017 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,10 +27,9 @@ import java.sql.Types;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
@@ -45,36 +44,38 @@ import org.springframework.messaging.Message;
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
|
||||
private static Log logger = LogFactory.getLog(JdbcPollingChannelAdapterIntegrationTests.class);
|
||||
private static EmbeddedDatabase embeddedDatabase;
|
||||
|
||||
private EmbeddedDatabase embeddedDatabase;
|
||||
private static JdbcTemplate jdbcTemplate;
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||
builder
|
||||
.setType(EmbeddedDatabaseType.DERBY)
|
||||
.addScript(
|
||||
"classpath:org/springframework/integration/jdbc/pollingChannelAdapterIntegrationTest.sql");
|
||||
this.embeddedDatabase = builder.build();
|
||||
this.jdbcTemplate = new JdbcTemplate(this.embeddedDatabase);
|
||||
builder.setType(EmbeddedDatabaseType.DERBY)
|
||||
.addScript("classpath:org/springframework/integration/jdbc/pollingChannelAdapterIntegrationTest.sql");
|
||||
embeddedDatabase = builder.build();
|
||||
jdbcTemplate = new JdbcTemplate(embeddedDatabase);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
embeddedDatabase.shutdown();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.embeddedDatabase.shutdown();
|
||||
public void cleanup() {
|
||||
jdbcTemplate.execute("DELETE FROM item");
|
||||
jdbcTemplate.execute("DELETE FROM copy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimplePollForListOfMapsNoUpdate() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item");
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase, "select * from item");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
Message<Object> message = adapter.receive();
|
||||
Object payload = message.getPayload();
|
||||
assertTrue("Wrong payload type", payload instanceof List<?>);
|
||||
@@ -84,13 +85,11 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
Map<?, ?> row = (Map<?, ?>) rows.get(0);
|
||||
assertEquals("Wrong id", 1, row.get("id"));
|
||||
assertEquals("Wrong status", 2, row.get("status"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParameterizedPollForListOfMapsNoUpdate() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase,
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
|
||||
"select * from item where status=:status");
|
||||
adapter.setSelectSqlParameterSource(new SqlParameterSource() {
|
||||
|
||||
@@ -113,8 +112,9 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
public int getSqlType(String name) {
|
||||
return Types.INTEGER;
|
||||
}
|
||||
|
||||
});
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
Message<Object> message = adapter.receive();
|
||||
Object payload = message.getPayload();
|
||||
assertTrue("Wrong payload type", payload instanceof List<?>);
|
||||
@@ -124,15 +124,13 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
Map<?, ?> row = (Map<?, ?>) rows.get(0);
|
||||
assertEquals("Wrong id", 1, row.get("id"));
|
||||
assertEquals("Wrong status", 2, row.get("status"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimplePollForListWithRowMapperNoUpdate() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase, "select * from item");
|
||||
adapter.setRowMapper(new ItemRowMapper());
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
Message<Object> message = adapter.receive();
|
||||
Object payload = message.getPayload();
|
||||
List<?> rows = (List<?>) payload;
|
||||
@@ -141,21 +139,19 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
Item item = (Item) rows.get(0);
|
||||
assertEquals("Wrong id", 1, item.getId());
|
||||
assertEquals("Wrong status", 2, item.getStatus());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimplePollForListWithRowMapperAndOneUpdate() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item where status=2");
|
||||
adapter
|
||||
.setUpdateSql("update item set status = 10 where id in (:id)");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
|
||||
"select * from item where status=2");
|
||||
adapter.setUpdateSql("update item set status = 10 where id in (:id)");
|
||||
adapter.setRowMapper(new ItemRowMapper());
|
||||
adapter.setBeanFactory(mock(BeanFactory.class));
|
||||
adapter.afterPropertiesSet();
|
||||
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
this.jdbcTemplate.update("insert into item values(2,2)");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
jdbcTemplate.update("insert into item values(2,2)");
|
||||
|
||||
Message<Object> message = adapter.receive();
|
||||
Object payload = message.getPayload();
|
||||
@@ -166,32 +162,25 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
assertEquals("Wrong id", 1, item.getId());
|
||||
assertEquals("Wrong status", 2, item.getStatus());
|
||||
|
||||
int countOfStatusTwo = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 2", 0,
|
||||
countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 10", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 10", 2,
|
||||
countOfStatusTen);
|
||||
int countOfStatusTwo = jdbcTemplate.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 2", 0, countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = jdbcTemplate.queryForObject("select count(*) from item where status = 10", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 10", 2, countOfStatusTen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimplePollForListWithRowMapperAndUpdatePerRow() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item where status=2");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
|
||||
"select * from item where status=2");
|
||||
adapter.setUpdateSql("update item set status = 10 where id = :id");
|
||||
adapter.setUpdatePerRow(true);
|
||||
adapter.setRowMapper(new ItemRowMapper());
|
||||
adapter.setBeanFactory(mock(BeanFactory.class));
|
||||
adapter.afterPropertiesSet();
|
||||
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
this.jdbcTemplate.update("insert into item values(2,2)");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
jdbcTemplate.update("insert into item values(2,2)");
|
||||
|
||||
Message<Object> message = adapter.receive();
|
||||
Object payload = message.getPayload();
|
||||
@@ -202,24 +191,17 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
assertEquals("Wrong id", 1, item.getId());
|
||||
assertEquals("Wrong status", 2, item.getStatus());
|
||||
|
||||
int countOfStatusTwo = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 2", 0,
|
||||
countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 10", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 10", 2,
|
||||
countOfStatusTen);
|
||||
int countOfStatusTwo = jdbcTemplate.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 2", 0, countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = jdbcTemplate.queryForObject("select count(*) from item where status = 10", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 10", 2, countOfStatusTen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimplePollForListWithRowMapperAndInsertPerRowAndMaxRows() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item where id not in (select id from copy)");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
|
||||
"select * from item where id not in (select id from copy)");
|
||||
adapter.setUpdateSql("insert into copy values(:id,10)");
|
||||
adapter.setUpdatePerRow(true);
|
||||
adapter.setMaxRowsPerPoll(1);
|
||||
@@ -227,38 +209,29 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
adapter.setBeanFactory(mock(BeanFactory.class));
|
||||
adapter.afterPropertiesSet();
|
||||
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
this.jdbcTemplate.update("insert into item values(2,2)");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
jdbcTemplate.update("insert into item values(2,2)");
|
||||
|
||||
logger.debug(adapter.receive());
|
||||
Message<Object> message = adapter.receive();
|
||||
Object payload = message.getPayload();
|
||||
List<?> rows = (List<?>) payload;
|
||||
assertEquals("Wrong number of elements", 1, rows.size());
|
||||
assertTrue("Wrong payload type", rows.get(0) instanceof Item);
|
||||
Item item = (Item) rows.get(0);
|
||||
logger.debug(item);
|
||||
assertEquals("Wrong id", 2, item.getId());
|
||||
assertEquals("Wrong id", 1, item.getId());
|
||||
assertEquals("Wrong status", 2, item.getStatus());
|
||||
|
||||
int countOfStatusTwo = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 2", 2,
|
||||
countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from copy where status = 10", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 10", 2,
|
||||
countOfStatusTen);
|
||||
int countOfStatusTwo = jdbcTemplate.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 2", 2, countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = jdbcTemplate.queryForObject("select count(*) from copy where status = 10", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 10", 1, countOfStatusTen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimplePollForListWithRowMapperAndUpdatePerRowWithMaxRows() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item where status=2");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
|
||||
"select * from item where status=2");
|
||||
adapter.setUpdateSql("update item set status = 10 where id = :id");
|
||||
adapter.setUpdatePerRow(true);
|
||||
adapter.setMaxRowsPerPoll(1);
|
||||
@@ -266,8 +239,8 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
adapter.setBeanFactory(mock(BeanFactory.class));
|
||||
adapter.afterPropertiesSet();
|
||||
|
||||
this.jdbcTemplate.update("insert into item values(1,2)");
|
||||
this.jdbcTemplate.update("insert into item values(2,2)");
|
||||
jdbcTemplate.update("insert into item values(1,2)");
|
||||
jdbcTemplate.update("insert into item values(2,2)");
|
||||
|
||||
adapter.receive();
|
||||
Message<Object> message = adapter.receive();
|
||||
@@ -279,27 +252,18 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
assertEquals("Wrong id", 2, item.getId());
|
||||
assertEquals("Wrong status", 2, item.getStatus());
|
||||
|
||||
int countOfStatusTwo = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 2", 0,
|
||||
countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = this.jdbcTemplate
|
||||
.queryForObject("select count(*) from item where status = 10", Integer.class);
|
||||
assertEquals(
|
||||
"Status not updated incorect number of rows with status 10", 2,
|
||||
countOfStatusTen);
|
||||
int countOfStatusTwo = jdbcTemplate.queryForObject("select count(*) from item where status = 2", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 2", 0, countOfStatusTwo);
|
||||
|
||||
int countOfStatusTen = jdbcTemplate.queryForObject("select count(*) from item where status = 10", Integer.class);
|
||||
assertEquals("Status not updated incorrect number of rows with status 10", 2, countOfStatusTen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyPoll() {
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(
|
||||
this.embeddedDatabase, "select * from item");
|
||||
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase, "select * from item");
|
||||
Message<Object> message = adapter.receive();
|
||||
assertNull("Message received when no rows in table", message);
|
||||
|
||||
}
|
||||
|
||||
private static class Item {
|
||||
@@ -309,7 +273,7 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
private int status;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
@@ -317,7 +281,7 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
return this.status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
@@ -326,8 +290,9 @@ public class JdbcPollingChannelAdapterIntegrationTests {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Item [id=" + id + ", status=" + status + "]";
|
||||
return "Item [id=" + this.id + ", status=" + this.status + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ItemRowMapper implements RowMapper<Item> {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -25,7 +25,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
@@ -42,31 +43,35 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
/**
|
||||
* @author Gunnar Hillert
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class StoredProcMessageHandlerDerbyIntegrationTests {
|
||||
|
||||
private EmbeddedDatabase embeddedDatabase;
|
||||
private static EmbeddedDatabase embeddedDatabase;
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private static JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Before
|
||||
public void setUp() throws SQLException {
|
||||
@BeforeClass
|
||||
public static void setUp() throws SQLException {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||
builder.setType(EmbeddedDatabaseType.DERBY);
|
||||
builder.addScript("classpath:derby-stored-procedures.sql");
|
||||
this.embeddedDatabase = builder.build();
|
||||
this.jdbcTemplate = new JdbcTemplate(this.embeddedDatabase);
|
||||
embeddedDatabase = builder.build();
|
||||
jdbcTemplate = new JdbcTemplate(embeddedDatabase);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
embeddedDatabase.shutdown();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.embeddedDatabase.shutdown();
|
||||
public void cleanup() {
|
||||
jdbcTemplate.execute("DELETE FROM USERS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDerbyStoredProcedureInsertWithDefaultSqlSource() {
|
||||
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(this.embeddedDatabase);
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
|
||||
StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
|
||||
|
||||
storedProcExecutor.setStoredProcedureName("CREATE_USER");
|
||||
@@ -84,13 +89,11 @@ public class StoredProcMessageHandlerDerbyIntegrationTests {
|
||||
assertEquals("Wrong username", "username", map.get("USERNAME"));
|
||||
assertEquals("Wrong password", "password", map.get("PASSWORD"));
|
||||
assertEquals("Wrong email", "email", map.get("EMAIL"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDerbyStoredProcInsertWithDefaultSqlSourceAndDynamicProcName() throws Exception {
|
||||
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(this.embeddedDatabase);
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
|
||||
StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
|
||||
|
||||
final ExpressionFactoryBean efb = new ExpressionFactoryBean("headers['stored_procedure_name']");
|
||||
@@ -113,13 +116,11 @@ public class StoredProcMessageHandlerDerbyIntegrationTests {
|
||||
assertEquals("Wrong username", "username", map.get("USERNAME"));
|
||||
assertEquals("Wrong password", "password", map.get("PASSWORD"));
|
||||
assertEquals("Wrong email", "email", map.get("EMAIL"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDerbyStoredProcInsertWithDefaultSqlSourceAndSpelProcName() throws Exception {
|
||||
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(this.embeddedDatabase);
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
|
||||
StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
|
||||
ExpressionFactoryBean efb = new ExpressionFactoryBean("headers.headerWithProcedureName");
|
||||
efb.afterPropertiesSet();
|
||||
@@ -142,13 +143,11 @@ public class StoredProcMessageHandlerDerbyIntegrationTests {
|
||||
assertEquals("Wrong username", "username", map.get("USERNAME"));
|
||||
assertEquals("Wrong password", "password", map.get("PASSWORD"));
|
||||
assertEquals("Wrong email", "email", map.get("EMAIL"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDerbyStoredProcedureInsertWithExpression() {
|
||||
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(this.embeddedDatabase);
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
|
||||
StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
|
||||
|
||||
storedProcExecutor.setStoredProcedureName("CREATE_USER");
|
||||
@@ -156,7 +155,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests {
|
||||
final List<ProcedureParameter> procedureParameters = new ArrayList<ProcedureParameter>();
|
||||
procedureParameters.add(new ProcedureParameter("username", null, "payload.username.toUpperCase()"));
|
||||
procedureParameters.add(new ProcedureParameter("password", null, "payload.password.toUpperCase()"));
|
||||
procedureParameters.add(new ProcedureParameter("email", null, "payload.email.toUpperCase()"));
|
||||
procedureParameters.add(new ProcedureParameter("email", null, "payload.email.toUpperCase()"));
|
||||
|
||||
storedProcExecutor.setProcedureParameters(procedureParameters);
|
||||
storedProcExecutor.setBeanFactory(mock(BeanFactory.class));
|
||||
@@ -168,20 +167,16 @@ public class StoredProcMessageHandlerDerbyIntegrationTests {
|
||||
MessageBuilder<User> message = MessageBuilder.withPayload(new User("Eric.Cartman", "c4rtm4n", "eric@cartman.com"));
|
||||
messageHandler.handleMessage(message.build());
|
||||
|
||||
|
||||
|
||||
Map<String, Object> map = jdbcTemplate.queryForMap("SELECT * FROM USERS WHERE USERNAME=?", "ERIC.CARTMAN");
|
||||
|
||||
assertEquals("Wrong username", "ERIC.CARTMAN", map.get("USERNAME"));
|
||||
assertEquals("Wrong password", "C4RTM4N", map.get("PASSWORD"));
|
||||
assertEquals("Wrong email", "ERIC@CARTMAN.COM", map.get("EMAIL"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDerbyStoredProcedureInsertWithHeaderExpression() {
|
||||
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(this.embeddedDatabase);
|
||||
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase);
|
||||
StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor);
|
||||
|
||||
storedProcExecutor.setStoredProcedureName("CREATE_USER");
|
||||
|
||||
@@ -12,12 +12,7 @@
|
||||
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
|
||||
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
|
||||
<property name="url" value="jdbc:derby:memory:myDB;create=true" />
|
||||
<property name="username" value="sa" />
|
||||
<property name="password" value="sa" />
|
||||
</bean>
|
||||
<jdbc:embedded-database id="dataSource" type="DERBY" />
|
||||
|
||||
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
|
||||
<jdbc:script location="${int.drop.script}" />
|
||||
|
||||
@@ -8,14 +8,7 @@
|
||||
|
||||
<import resource="classpath:org/springframework/integration/jdbc/store/channel/DataSource-common-context.xml" />
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
|
||||
destroy-method="close">
|
||||
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
|
||||
<property name="initialSize" value="10" />
|
||||
<property name="url" value="jdbc:derby:memory:integration;create=true" />
|
||||
<property name="username" value="int" />
|
||||
<property name="password" value="int" />
|
||||
</bean>
|
||||
<jdbc:embedded-database id="dataSource" type="DERBY" />
|
||||
|
||||
<!-- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
|
||||
destroy-method="close"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
|
||||
|
||||
@@ -5,12 +5,7 @@
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
|
||||
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
|
||||
<property name="url" value="jdbc:derby:memory:myDB;create=true" />
|
||||
<property name="username" value="sa" />
|
||||
<property name="password" value="sa" />
|
||||
</bean>
|
||||
<jdbc:embedded-database id="dataSource" type="DERBY" />
|
||||
|
||||
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS" >
|
||||
<jdbc:script location="classpath:derby-stored-procedures-drops.sql"/>
|
||||
|
||||
Reference in New Issue
Block a user