From be0e24aea55ed053ad89859c583799487ebd9283 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 27 Apr 2017 16:19:02 -0400 Subject: [PATCH] Fix JmsMockTests for bad Mockito usage https://build.spring.io/browse/INTSAMPLES-NIGHTLY-2074 Upgrade to SK-2.0 M1 and SIK-3.0 M1 Fix JPA sample adding `SEQUENCE HIBERNATE_SEQUENCE` DDL into the `schema.sql` https://build.spring.io/browse/INTSAMPLES-NIGHTLY-2078/ The latest Hibernate version doesn't create such a DB entity if we use `hibernate.ddl-auto: none` Increase timeouts in the JMS and TCP tests --- .../advance/testing/jms/JmsMockTests.java | 62 ++++++++++++------- .../spring/integration/aggregation.xml | 10 +-- .../integration/outboundChannelAdapter.xml | 6 +- .../spring/integration/outboundGateway.xml | 6 +- .../samples/jms/AggregatorDemoTest.java | 10 +-- .../samples/jms/ChannelAdapterDemoTest.java | 7 ++- .../samples/jms/GatewayDemoTest.java | 7 ++- basic/jpa/src/main/resources/data.sql | 2 - basic/jpa/src/main/resources/schema.sql | 4 +- build.gradle | 12 ++-- .../TcpClientServerDemoTest.java | 10 ++- 11 files changed, 79 insertions(+), 57 deletions(-) diff --git a/advanced/advanced-testing-examples/src/test/java/org/springframework/integration/samples/advance/testing/jms/JmsMockTests.java b/advanced/advanced-testing-examples/src/test/java/org/springframework/integration/samples/advance/testing/jms/JmsMockTests.java index 309a0c9f..63ed056e 100644 --- a/advanced/advanced-testing-examples/src/test/java/org/springframework/integration/samples/advance/testing/jms/JmsMockTests.java +++ b/advanced/advanced-testing-examples/src/test/java/org/springframework/integration/samples/advance/testing/jms/JmsMockTests.java @@ -18,21 +18,25 @@ package org.springframework.integration.samples.advance.testing.jms; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import javax.jms.JMSException; import javax.jms.TextMessage; import org.apache.log4j.Logger; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -62,6 +66,8 @@ public class JmsMockTests { private static final Logger LOGGER = Logger.getLogger(JmsMockTests.class); + private final AtomicReference testMessageHolder = new AtomicReference<>(); + @Autowired private JmsTemplate mockJmsTemplate; @@ -80,6 +86,25 @@ public class JmsMockTests { @Qualifier("invalidMessageChannel") private SubscribableChannel invalidMessageChannel; + + @Before + public void setup() throws JMSException { + TextMessage message = mock(TextMessage.class); + when(this.mockJmsTemplate.getMessageConverter()).thenReturn(new SimpleMessageConverter()); + when(this.mockJmsTemplate.receiveSelected(anyString())).thenReturn(message); + + + given(message.getText()) + .willAnswer(new Answer() { + + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return testMessageHolder.get(); + } + + }); + } + /** * This test verifies that a message received on a polling JMS inbound channel adapter is * routed to the designated channel and that the message payload is as expected @@ -92,13 +117,13 @@ public class JmsMockTests { public void testReceiveMessage() throws JMSException, InterruptedException, IOException { String msg = "hello"; - boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, outputChannel,new CountDownHandler() { + boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, outputChannel, new CountDownHandler() { - @Override - protected void verifyMessage(Message message) { - assertEquals("hello",message.getPayload()); - } - } + @Override + protected void verifyMessage(Message message) { + assertEquals("hello", message.getPayload()); + } + } ); assertTrue("message not sent to expected output channel", sent); } @@ -116,12 +141,12 @@ public class JmsMockTests { String msg = "whoops"; boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, invalidMessageChannel, new CountDownHandler() { - @Override - protected void verifyMessage(Message message) { - assertEquals("invalid payload",message.getPayload()); - } + @Override + protected void verifyMessage(Message message) { + assertEquals("invalid payload", message.getPayload()); + } - } + } ); assertTrue("message not sent to expected output channel", sent); } @@ -168,16 +193,10 @@ public class JmsMockTests { * is also a mock. */ - TextMessage message = mock(TextMessage.class); - when(this.mockJmsTemplate.getMessageConverter()).thenReturn(new SimpleMessageConverter()); - when(this.mockJmsTemplate.receiveSelected(anyString())).thenReturn(message); - - String text = (String) obj; - + this.testMessageHolder.set((String) obj); CountDownLatch latch = new CountDownLatch(1); handler.setLatch(latch); - doReturn(text).when(message).getText(); expectedOutputChannel.subscribe(handler); @@ -192,6 +211,7 @@ public class JmsMockTests { return latchCountedToZero; } + /* * A MessageHandler that uses a CountDownLatch to synchronize with the calling thread */ @@ -199,7 +219,7 @@ public class JmsMockTests { CountDownLatch latch; - public final void setLatch(CountDownLatch latch){ + public final void setLatch(CountDownLatch latch) { this.latch = latch; } @@ -219,4 +239,4 @@ public class JmsMockTests { } -} +} \ No newline at end of file diff --git a/basic/jms/src/main/resources/META-INF/spring/integration/aggregation.xml b/basic/jms/src/main/resources/META-INF/spring/integration/aggregation.xml index 683dab76..dacedf41 100644 --- a/basic/jms/src/main/resources/META-INF/spring/integration/aggregation.xml +++ b/basic/jms/src/main/resources/META-INF/spring/integration/aggregation.xml @@ -6,8 +6,6 @@ xmlns:int-stream="http://www.springframework.org/schema/integration/stream" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/jms @@ -15,11 +13,9 @@ http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd"> - + - - - + @@ -32,7 +28,7 @@ - + - + - + diff --git a/basic/jms/src/main/resources/META-INF/spring/integration/outboundGateway.xml b/basic/jms/src/main/resources/META-INF/spring/integration/outboundGateway.xml index 858c6b9a..d76a0f0a 100644 --- a/basic/jms/src/main/resources/META-INF/spring/integration/outboundGateway.xml +++ b/basic/jms/src/main/resources/META-INF/spring/integration/outboundGateway.xml @@ -15,11 +15,11 @@ http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd"> - + - + - diff --git a/basic/jms/src/test/java/org/springframework/integration/samples/jms/AggregatorDemoTest.java b/basic/jms/src/test/java/org/springframework/integration/samples/jms/AggregatorDemoTest.java index 3b3fa575..e430abf7 100644 --- a/basic/jms/src/test/java/org/springframework/integration/samples/jms/AggregatorDemoTest.java +++ b/basic/jms/src/test/java/org/springframework/integration/samples/jms/AggregatorDemoTest.java @@ -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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.samples.jms; import java.util.List; @@ -29,6 +30,7 @@ import org.springframework.messaging.MessageChannel; /** * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan */ public class AggregatorDemoTest { @@ -44,14 +46,14 @@ public class AggregatorDemoTest { final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesGatewayDemo); - final MessageChannel stdinToJmsoutChannel = applicationContext.getBean("stdinToJmsoutChannel", MessageChannel.class); + final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class); - stdinToJmsoutChannel.send(MessageBuilder.withPayload("jms test").build()); + stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build()); final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class); @SuppressWarnings("unchecked") - Message> reply = (Message>) queueChannel.receive(20000); + Message> reply = (Message>) queueChannel.receive(600000); Assert.assertNotNull(reply); List out = reply.getPayload(); diff --git a/basic/jms/src/test/java/org/springframework/integration/samples/jms/ChannelAdapterDemoTest.java b/basic/jms/src/test/java/org/springframework/integration/samples/jms/ChannelAdapterDemoTest.java index 4bc9aa47..b835c818 100644 --- a/basic/jms/src/test/java/org/springframework/integration/samples/jms/ChannelAdapterDemoTest.java +++ b/basic/jms/src/test/java/org/springframework/integration/samples/jms/ChannelAdapterDemoTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.samples.jms; import org.junit.Assert; @@ -42,9 +43,9 @@ public class ChannelAdapterDemoTest { final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesChannelAdapterDemo); - final MessageChannel stdinToJmsoutChannel = applicationContext.getBean("stdinToJmsoutChannel", MessageChannel.class); + final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class); - stdinToJmsoutChannel.send(MessageBuilder.withPayload("jms test").build()); + stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build()); final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class); diff --git a/basic/jms/src/test/java/org/springframework/integration/samples/jms/GatewayDemoTest.java b/basic/jms/src/test/java/org/springframework/integration/samples/jms/GatewayDemoTest.java index 1ab84670..27707667 100644 --- a/basic/jms/src/test/java/org/springframework/integration/samples/jms/GatewayDemoTest.java +++ b/basic/jms/src/test/java/org/springframework/integration/samples/jms/GatewayDemoTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.samples.jms; import org.junit.Assert; @@ -42,9 +43,9 @@ public class GatewayDemoTest { final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesGatewayDemo); - final MessageChannel stdinToJmsoutChannel = applicationContext.getBean("stdinToJmsoutChannel", MessageChannel.class); + final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class); - stdinToJmsoutChannel.send(MessageBuilder.withPayload("jms test").build()); + stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build()); final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class); diff --git a/basic/jpa/src/main/resources/data.sql b/basic/jpa/src/main/resources/data.sql index ff37c67c..465bc0df 100644 --- a/basic/jpa/src/main/resources/data.sql +++ b/basic/jpa/src/main/resources/data.sql @@ -1,3 +1 @@ insert into PEOPLE(id, name, CREATED_DATE_TIME) values ('1001', 'Cartman', NOW()); - -INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0); diff --git a/basic/jpa/src/main/resources/schema.sql b/basic/jpa/src/main/resources/schema.sql index 1ec4cc38..45bab574 100644 --- a/basic/jpa/src/main/resources/schema.sql +++ b/basic/jpa/src/main/resources/schema.sql @@ -4,6 +4,6 @@ DROP TABLE if EXISTS SEQUENCE; CREATE TABLE OPENJPA_SEQUENCE_TABLE (ID TINYINT NOT NULL, SEQUENCE_VALUE BIGINT, PRIMARY KEY (ID)); -CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50), SEQ_COUNT DECIMAL(15)); - CREATE TABLE PEOPLE (id BIGINT generated by default as identity, name VARCHAR(255), CREATED_DATE_TIME TIMESTAMP, PRIMARY KEY (id)); + +CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 INCREMENT BY 1; \ No newline at end of file diff --git a/build.gradle b/build.gradle index b64ecdaf..1c452900 100644 --- a/build.gradle +++ b/build.gradle @@ -183,16 +183,16 @@ subprojects { subproject -> commonsPoolVersion = '1.5.4' c3p0Version = '0.9.1.2' derbyVersion = '10.10.1.1' - eclipseLinkVersion = '2.6.2' + eclipseLinkVersion = '2.6.4' hamcrestVersion = '1.3' - hibernateVersion = '5.2.4.Final' - hibernateValidatorVersion = '5.2.4.Final' + hibernateVersion = '5.2.10.Final' + hibernateValidatorVersion = '5.4.1.Final' ftpServerVersion = '1.1.0' flexjsonVersion = '2.0' guavaVersion = '16.0.1' groovyVersion = '2.3.0' hsqldbVersion = '2.3.2' - h2Version = '1.3.175' + h2Version = '1.4.194' jacksonVersion = '2.3.2' jasyptVersion = '1.7' javaxInjectVersion = '1' @@ -215,9 +215,9 @@ subprojects { subproject -> subethasmtpVersion = '1.2' slf4jVersion = '1.7.11' springIntegrationVersion = '5.0.0.BUILD-SNAPSHOT' - springIntegrationKafkaVersion = '3.0.0.BUILD-SNAPSHOT' + springIntegrationKafkaVersion = '3.0.0.M1' springIntegrationSplunkVersion = '1.1.0.RELEASE' - springKafkaVersion = '2.0.0.BUILD-SNAPSHOT' + springKafkaVersion = '2.0.0.M1' springVersion = '5.0.0.BUILD-SNAPSHOT' springSecurityVersion = '4.2.2.RELEASE' springWebFlowVersion = '2.3.3.RELEASE' diff --git a/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java b/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java index bf9ebe73..eed7adf6 100644 --- a/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java +++ b/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.samples.tcpclientserver; import static org.hamcrest.Matchers.containsString; @@ -53,7 +54,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @since 2.1 * */ -@ContextConfiguration(loader=CustomTestContextLoader.class, locations={"/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml"}) +@ContextConfiguration(loader = CustomTestContextLoader.class, + locations = { "/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml" }) @RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext public class TcpClientServerDemoTest { @@ -84,15 +86,17 @@ public class TcpClientServerDemoTest { results.add(i); final int j = i; executor.execute(new Runnable() { + @Override public void run() { String result = gw.send(j + "Hello world!"); // first 3 bytes is correlationid assertEquals(j + "Hello world!:echo", result); results.remove(j); latch.countDown(); - }}); + } + }); } - assertTrue(latch.await(10, TimeUnit.SECONDS)); + assertTrue(latch.await(20, TimeUnit.SECONDS)); assertEquals(0, results.size()); }