From 8e7785d8b592f6ee1d130f301f0fd11d72e85f9a Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 9 Sep 2020 14:18:35 -0400 Subject: [PATCH] Some JDBC tests clean up; use H2 mostly --- build.gradle | 2 +- .../AggregatorIntegrationTests-context.xml | 14 ++---- ...ocMessageHandlerDerbyIntegrationTests.java | 50 +++++++++++-------- ...tboundGatewayWithSpelIntegrationTests.java | 41 ++++++--------- ...hSpringContextIntegrationTests-context.xml | 44 +++++++--------- ...ewayWithSpringContextIntegrationTests.java | 22 ++++---- .../JdbcLockRegistryDifferentClientTests.java | 4 +- .../lock/JdbcLockRegistryTests-context.xml | 14 ++---- .../JdbcMetadataStoreTests-context.xml | 14 ++---- ...geStoreChannelIntegrationTests-context.xml | 14 ++---- ...annelOnePollerIntegrationTests-context.xml | 14 ++---- 11 files changed, 97 insertions(+), 136 deletions(-) diff --git a/build.gradle b/build.gradle index df816be948..a932445472 100644 --- a/build.gradle +++ b/build.gradle @@ -106,7 +106,7 @@ ext { springVersion = project.hasProperty('springVersion') ? project.springVersion : '5.3.0-SNAPSHOT' springWsVersion = '3.0.9.RELEASE' tomcatVersion = "9.0.37" - xstreamVersion = '1.4.12' + xstreamVersion = '1.4.13' javaProjects = subprojects - project(':spring-integration-bom') } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml index 9cc16743db..4a19d15797 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml @@ -4,7 +4,6 @@ xmlns="http://www.springframework.org/schema/integration" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc" xmlns:jdbc="http://www.springframework.org/schema/jdbc" - xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration @@ -12,16 +11,13 @@ http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/integration/jdbc - https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd - http://www.springframework.org/schema/context - https://www.springframework.org/schema/context/spring-context.xsd"> + https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd"> - + - - - - + + + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java index f78d7463c5..3831addb3f 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -19,15 +19,14 @@ package org.springframework.integration.jdbc; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; @@ -39,6 +38,8 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.GenericMessage; /** * @author Gunnar Hillert @@ -51,24 +52,26 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { private static JdbcTemplate jdbcTemplate; - @BeforeClass - public static void setUp() throws SQLException { - EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); - builder.setType(EmbeddedDatabaseType.DERBY); - builder.addScript("classpath:derby-stored-procedures.sql"); - embeddedDatabase = builder.build(); + @BeforeAll + public static void setUp() { + embeddedDatabase = + new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.DERBY) + .addScript("classpath:derby-stored-procedures.sql") + .build(); jdbcTemplate = new JdbcTemplate(embeddedDatabase); } - @AfterClass + @AfterAll public static void tearDown() { embeddedDatabase.shutdown(); } - @After + @AfterEach public void cleanup() { jdbcTemplate.execute("DELETE FROM USERS"); } + @Test public void testDerbyStoredProcedureInsertWithDefaultSqlSource() { StoredProcExecutor storedProcExecutor = new StoredProcExecutor(embeddedDatabase); @@ -152,7 +155,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { storedProcExecutor.setStoredProcedureName("CREATE_USER"); - final List procedureParameters = new ArrayList(); + final List procedureParameters = new ArrayList<>(); 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()")); @@ -164,8 +167,8 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { messageHandler.setBeanFactory(mock(BeanFactory.class)); messageHandler.afterPropertiesSet(); - MessageBuilder message = MessageBuilder.withPayload(new User("Eric.Cartman", "c4rtm4n", "eric@cartman.com")); - messageHandler.handleMessage(message.build()); + Message message = new GenericMessage<>(new User("Eric.Cartman", "c4rtm4n", "eric@cartman.com")); + messageHandler.handleMessage(message); Map map = jdbcTemplate.queryForMap("SELECT * FROM USERS WHERE USERNAME=?", "ERIC.CARTMAN"); @@ -181,8 +184,9 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { storedProcExecutor.setStoredProcedureName("CREATE_USER"); - final List procedureParameters = new ArrayList(); - procedureParameters.add(new ProcedureParameter("USERNAME", null, "headers[business_id] + '_' + payload.username")); + final List procedureParameters = new ArrayList<>(); + procedureParameters.add(new ProcedureParameter("USERNAME", null, + "headers[business_id] + '_' + payload.username")); procedureParameters.add(new ProcedureParameter("password", "static_password", null)); procedureParameters.add(new ProcedureParameter("email", "static_email", null)); @@ -193,9 +197,11 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { messageHandler.setBeanFactory(mock(BeanFactory.class)); messageHandler.afterPropertiesSet(); - MessageBuilder message = MessageBuilder.withPayload(new User("Eric.Cartman", "c4rtm4n", "eric@cartman.com")); - message.setHeader("business_id", "1234"); - messageHandler.handleMessage(message.build()); + Message message = + MessageBuilder.withPayload(new User("Eric.Cartman", "c4rtm4n", "eric@cartman.com")) + .setHeader("business_id", "1234") + .build(); + messageHandler.handleMessage(message); Map map = jdbcTemplate.queryForMap("SELECT * FROM USERS WHERE USERNAME=?", "1234_Eric.Cartman"); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpelIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpelIntegrationTests.java index f1099f292f..a8c7da7606 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpelIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpelIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -17,7 +17,7 @@ package org.springframework.integration.jdbc; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import java.sql.CallableStatement; import java.util.Collection; @@ -26,8 +26,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -50,17 +49,15 @@ import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.ErrorMessage; import org.springframework.messaging.support.GenericMessage; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.transaction.annotation.Transactional; /** * @author Gunnar Hillert * @author Artem Bilan */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) -@DirtiesContext // close at the end after class +@SpringJUnitConfig +@DirtiesContext public class StoredProcOutboundGatewayWithSpelIntegrationTests { @Autowired @@ -90,9 +87,7 @@ public class StoredProcOutboundGatewayWithSpelIntegrationTests { SqlReturnType clobSqlReturnType; @Test - @DirtiesContext - public void executeStoredProcedureWithMessageHeader() throws Exception { - + public void executeStoredProcedureWithMessageHeader() { User user1 = new User("First User", "my first password", "email1"); User user2 = new User("Second User", "my second password", "email2"); @@ -122,9 +117,7 @@ public class StoredProcOutboundGatewayWithSpelIntegrationTests { } @Test - @DirtiesContext - public void testWithMissingMessageHeader() throws Exception { - + public void testWithMissingMessageHeader() { User user1 = new User("First User", "my first password", "email1"); Message user1Message = MessageBuilder.withPayload(user1).build(); @@ -153,7 +146,7 @@ public class StoredProcOutboundGatewayWithSpelIntegrationTests { this.jdbcTemplate.update("INSERT INTO json_message VALUES (?,?)", messageId, jsonMessage); - this.getMessageChannel.send(new GenericMessage(messageId)); + this.getMessageChannel.send(new GenericMessage<>(messageId)); Message resultMessage = this.output2Channel.receive(10000); assertThat(resultMessage).isNotNull(); Object resultPayload = resultMessage.getPayload(); @@ -163,31 +156,27 @@ public class StoredProcOutboundGatewayWithSpelIntegrationTests { assertThat(message.getPayload()).isEqualTo(testMessage.getPayload()); assertThat(message.getHeaders().get("FOO")).isEqualTo(testMessage.getHeaders().get("FOO")); Mockito.verify(clobSqlReturnType).getTypeValue(Mockito.any(CallableStatement.class), - Mockito.eq(2), Mockito.eq(JdbcTypesEnum.CLOB.getCode()), Mockito.eq((String) null)); + Mockito.eq(2), Mockito.eq(JdbcTypesEnum.CLOB.getCode()), Mockito.eq(null)); } @Test public void testNoIllegalArgumentButRequiresReplyException() { - try { - this.getMessageChannel.send(new GenericMessage("foo")); - fail("ReplyRequiredException expected"); - } - catch (Exception e) { - assertThat(e).isInstanceOf(ReplyRequiredException.class); - } + assertThatExceptionOfType(ReplyRequiredException.class) + .isThrownBy(() -> this.getMessageChannel.send(new GenericMessage<>("foo"))); } static class Counter { private final AtomicInteger count = new AtomicInteger(); - public Integer next() throws InterruptedException { + public Integer next() { if (count.get() > 2) { //prevent message overload return null; } return count.incrementAndGet(); } + } /** @@ -197,7 +186,7 @@ public class StoredProcOutboundGatewayWithSpelIntegrationTests { */ static class Consumer { - private volatile BlockingQueue>> messages = new LinkedBlockingQueue>>(); + private volatile BlockingQueue>> messages = new LinkedBlockingQueue<>(); @ServiceActivator public void receive(Message> message) { diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests-context.xml index 3eff14a4f1..4af7e5f2aa 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests-context.xml @@ -1,44 +1,37 @@ + http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> + service-interface="org.springframework.integration.jdbc.storedproc.CreateUser"/> - + - + - + - + @@ -50,24 +43,25 @@ - - - - + + + + + class="org.springframework.integration.endpoint.EventDrivenConsumer"> - + - - + + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests.java index e45a36af05..f5412a304f 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcOutboundGatewayWithSpringContextIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -26,8 +26,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.AbstractApplicationContext; @@ -36,14 +35,13 @@ import org.springframework.integration.jdbc.storedproc.CreateUser; import org.springframework.integration.jdbc.storedproc.User; import org.springframework.messaging.Message; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author Gunnar Hillert + * @author Artem Bilan */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig @DirtiesContext // close at the end after class public class StoredProcOutboundGatewayWithSpringContextIntegrationTests { @@ -58,10 +56,9 @@ public class StoredProcOutboundGatewayWithSpringContextIntegrationTests { @Test public void test() throws Exception { - createUser.createUser(new User("myUsername", "myPassword", "myEmail")); - List>> received = new ArrayList>>(); + List>> received = new ArrayList<>(); received.add(consumer.poll(2000)); @@ -81,19 +78,20 @@ public class StoredProcOutboundGatewayWithSpringContextIntegrationTests { private final AtomicInteger count = new AtomicInteger(); - public Integer next() throws InterruptedException { + public Integer next() { if (count.get() > 2) { //prevent message overload return null; } return Integer.valueOf(count.incrementAndGet()); } + } static class Consumer { - private final BlockingQueue>> messages = new LinkedBlockingQueue>>(); + private final BlockingQueue>> messages = new LinkedBlockingQueue<>(); @ServiceActivator public void receive(Message> message) { @@ -103,5 +101,7 @@ public class StoredProcOutboundGatewayWithSpringContextIntegrationTests { Message> poll(long timeoutInMillis) throws InterruptedException { return messages.poll(timeoutInMillis, TimeUnit.MILLISECONDS); } + } + } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryDifferentClientTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryDifferentClientTests.java index c5c5aa1a6a..7961797ff9 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryDifferentClientTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryDifferentClientTests.java @@ -58,7 +58,7 @@ import org.springframework.util.StopWatch; @DirtiesContext public class JdbcLockRegistryDifferentClientTests { - private static Log logger = LogFactory.getLog(JdbcLockRegistryDifferentClientTests.class); + private static final Log LOGGER = LogFactory.getLog(JdbcLockRegistryDifferentClientTests.class); @Autowired private JdbcLockRegistry registry; @@ -218,7 +218,7 @@ public class JdbcLockRegistryDifferentClientTests { }; tasks.add(task); } - logger.info("Starting: " + i); + LOGGER.info("Starting: " + i); pool.invokeAll(tasks); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryTests-context.xml index 2a74b532f0..4d4511c095 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryTests-context.xml @@ -2,24 +2,18 @@ - + - - + + + - - diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml index f687de36a0..6f9f2eb54c 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml @@ -2,22 +2,16 @@ + http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> - - + + + - - diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml index 998fcb34c1..bd4044ea2b 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml @@ -6,18 +6,17 @@ xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd - http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> + http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> - - + + + @@ -56,11 +55,6 @@ - - diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml index aaa44ad009..66c5d3a2fa 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml @@ -5,18 +5,17 @@ xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd - http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> + http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> - - + + + - -