From c5233cbd9fb9adc63e66e4a1c3cb0cd569d6a270 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 2 Feb 2011 13:28:36 -0500 Subject: [PATCH] INTSAMPLES-19 Add Gateway Example --- basic/testing-examples/pom.xml | 2 +- basic/testing-examples/readme.txt | 5 ++ .../samples/testing/gateway/VoidGateway.java | 30 ++++++++ .../06-filter/integration-context.xml | 2 +- .../07-gateway/integration-context.xml | 20 ++++++ .../testing/filter/PetFilterTests.java | 10 +-- .../testing/gateway/GatewayTests-context.xml | 19 ++++++ .../samples/testing/gateway/GatewayTests.java | 68 +++++++++++++++++++ 8 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/gateway/VoidGateway.java create mode 100644 basic/testing-examples/src/main/resources/META-INF/spring/integration/07-gateway/integration-context.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests-context.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests.java diff --git a/basic/testing-examples/pom.xml b/basic/testing-examples/pom.xml index 80fbda9c..409874a0 100644 --- a/basic/testing-examples/pom.xml +++ b/basic/testing-examples/pom.xml @@ -16,7 +16,7 @@ org.springframework.integration - spring-integration-core + spring-integration-file ${spring.integration.version} diff --git a/basic/testing-examples/readme.txt b/basic/testing-examples/readme.txt index 2804a2c9..2df7e719 100644 --- a/basic/testing-examples/readme.txt +++ b/basic/testing-examples/readme.txt @@ -60,4 +60,9 @@ This test case shows both unit testing a custom filter class as well as testing it within a Spring Integration flow fragment. +...gateway.GatewayTests.java + +This test case shows how to verify that a has been configured +to work as expected, including @Header parameters on the gateway method +and elements in the configuration. diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/gateway/VoidGateway.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/gateway/VoidGateway.java new file mode 100644 index 00000000..228f87c7 --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/gateway/VoidGateway.java @@ -0,0 +1,30 @@ +/* + * Copyright 2002-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.testing.gateway; + +import org.springframework.integration.annotation.Header; +import org.springframework.integration.file.FileHeaders; + +/** + * @author Gary Russell + * @since 2.0.2 + * + */ +public interface VoidGateway { + + public void process(String thing, @Header(FileHeaders.FILENAME) String fileName); + +} diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/06-filter/integration-context.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/06-filter/integration-context.xml index 4ae2fea6..e35be1af 100644 --- a/basic/testing-examples/src/main/resources/META-INF/spring/integration/06-filter/integration-context.xml +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/06-filter/integration-context.xml @@ -13,7 +13,7 @@ output-channel="outputChannel"> - + diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/07-gateway/integration-context.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/07-gateway/integration-context.xml new file mode 100644 index 00000000..e71c952a --- /dev/null +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/07-gateway/integration-context.xml @@ -0,0 +1,20 @@ + + + + + +
+ + + + + + diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/filter/PetFilterTests.java b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/filter/PetFilterTests.java index 8e0bd4a3..9d88f17d 100644 --- a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/filter/PetFilterTests.java +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/filter/PetFilterTests.java @@ -34,11 +34,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * - * Shows how to test a router; the router is configured to route to direct - * channel names. The configuration would - * be a fragment of a larger flow. Since the output channels are direct, - * they have no subscribers outside the context of a larger flow. So, - * in this test case, we bridge them to {@link QueueChannel}s to + * Shows how to test a filter. + * The filter has direct input and output channels. The filter configuration would + * be a fragment of a larger flow. Since the output channel is direct, + * it has no subscribers outside the context of a larger flow. So, + * in this test case, we bridge it to a {@link QueueChannel} to * facilitate easy testing. * * @author Gary Russell diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests-context.xml b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests-context.xml new file mode 100644 index 00000000..7e32ef12 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests-context.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests.java b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests.java new file mode 100644 index 00000000..81ecf81d --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/gateway/GatewayTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.testing.gateway; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.Message; +import org.springframework.integration.MessageHeaders; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.file.FileHeaders; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * + * Shows how to test a gateway to ensure the message injected + * into the Spring Integration flow is what we expected. + * The gateway uses a direct input channel. The configuration would + * be a fragment of a larger flow. Since the input channel is direct, + * it has no subscribers outside the context of a larger flow. So, + * in this test case, we bridge it to a {@link QueueChannel} to + * facilitate easy testing. + * + * @author Gary Russell + * @since 2.0.2 + * + */ +@ContextConfiguration // default context name is -context.xml +@RunWith(SpringJUnit4ClassRunner.class) +public class GatewayTests { + + @Autowired + QueueChannel testChannel; + + @Autowired + VoidGateway gateway; + + @Test + public void testTrueHeader() { + String payload = "XXXABCXXX"; + String fileName = "abc.txt"; + gateway.process(payload, fileName); + Message inMessage = testChannel.receive(0); + assertNotNull("Expected a message", inMessage); + assertEquals(payload, inMessage.getPayload()); + MessageHeaders headers = inMessage.getHeaders(); + assertEquals(fileName, headers.get(FileHeaders.FILENAME)); + assertEquals("abc", headers.get("configuredHeader")); + } +}