From bfb4f0c4aa37a83ba9788619480eaeb1d19cadd6 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 1 Feb 2011 17:05:20 -0500 Subject: [PATCH] INTSAMPLES-19 First Set of Sample Testing Techniques --- basic/testing-examples/.classpath | 10 ++ basic/testing-examples/.gitignore | 1 + basic/testing-examples/.project | 35 +++++ basic/testing-examples/.springBeans | 13 ++ basic/testing-examples/pom.xml | 86 ++++++++++++ basic/testing-examples/readme.txt | 44 ++++++ .../aggregator/CommaDelimitedAggregator.java | 47 +++++++ .../samples/testing/externalgateway/Main.java | 44 ++++++ .../testing/externalgateway/Traffic.java | 41 ++++++ .../externalgateway/TrafficHttpConverter.java | 90 ++++++++++++ .../testing/externalgateway/Weather.java | 59 ++++++++ .../externalgateway/WeatherAndTraffic.java | 27 ++++ .../externalgateway/WeatherMarshaller.java | 131 ++++++++++++++++++ .../splitter/CommaDelimitedSplitter.java | 46 ++++++ .../01-chain/integration-context.xml | 22 +++ .../02-splitter/integration-context.xml | 19 +++ .../03-aggregator/integration-context.xml | 32 +++++ .../main-integration-context.xml | 45 ++++++ .../04-externalgateway/real-gateways.xml | 47 +++++++ .../src/main/resources/log4j.xml | 32 +++++ .../CommaDelimitedAggregatorTests-context.xml | 19 +++ .../CommaDelimitedAggregatorTests.java | 124 +++++++++++++++++ .../testing/chain/SpelChainTests-context.xml | 19 +++ .../samples/testing/chain/SpelChainTests.java | 81 +++++++++++ ...ternalGatewaySubstitutionTests-context.xml | 25 ++++ .../ExternalGatewaySubstitutionTests.java | 58 ++++++++ .../CommaDelimitedSplitterTests-context.xml | 19 +++ .../splitter/CommaDelimitedSplitterTests.java | 131 ++++++++++++++++++ 28 files changed, 1347 insertions(+) create mode 100644 basic/testing-examples/.classpath create mode 100644 basic/testing-examples/.gitignore create mode 100644 basic/testing-examples/.project create mode 100644 basic/testing-examples/.springBeans create mode 100644 basic/testing-examples/pom.xml create mode 100644 basic/testing-examples/readme.txt create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregator.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Main.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Traffic.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/TrafficHttpConverter.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Weather.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherAndTraffic.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherMarshaller.java create mode 100644 basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitter.java create mode 100644 basic/testing-examples/src/main/resources/META-INF/spring/integration/01-chain/integration-context.xml create mode 100644 basic/testing-examples/src/main/resources/META-INF/spring/integration/02-splitter/integration-context.xml create mode 100644 basic/testing-examples/src/main/resources/META-INF/spring/integration/03-aggregator/integration-context.xml create mode 100644 basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/main-integration-context.xml create mode 100644 basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/real-gateways.xml create mode 100644 basic/testing-examples/src/main/resources/log4j.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests-context.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests.java create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests-context.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests.java create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests-context.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests.java create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests-context.xml create mode 100644 basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests.java diff --git a/basic/testing-examples/.classpath b/basic/testing-examples/.classpath new file mode 100644 index 00000000..dde3100b --- /dev/null +++ b/basic/testing-examples/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/basic/testing-examples/.gitignore b/basic/testing-examples/.gitignore new file mode 100644 index 00000000..ea8c4bf7 --- /dev/null +++ b/basic/testing-examples/.gitignore @@ -0,0 +1 @@ +/target diff --git a/basic/testing-examples/.project b/basic/testing-examples/.project new file mode 100644 index 00000000..7f1a9d1c --- /dev/null +++ b/basic/testing-examples/.project @@ -0,0 +1,35 @@ + + + testing-examples + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/basic/testing-examples/.springBeans b/basic/testing-examples/.springBeans new file mode 100644 index 00000000..ca9c1817 --- /dev/null +++ b/basic/testing-examples/.springBeans @@ -0,0 +1,13 @@ + + + 1 + + + + + + + + + + diff --git a/basic/testing-examples/pom.xml b/basic/testing-examples/pom.xml new file mode 100644 index 00000000..3af46cd6 --- /dev/null +++ b/basic/testing-examples/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + org.springframework.integration.samples + testing-examples + 2.0.0 + Spring Integration Testing Examples + jar + + 2.0.2.BUILD-SNAPSHOT + 3.0.5.RELEASE + 1.2.16 + 4.7 + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + org.springframework.integration + spring-integration-ws + ${spring.integration.version} + + + org.springframework.integration + spring-integration-http + ${spring.integration.version} + + + log4j + log4j + ${log4j.version} + + + + junit + junit + ${junit.version} + + + org.springframework + spring-test + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + -Xlint:all + true + false + + + + + + + repository.springframework.maven.release + Spring Framework Maven Release Repository + http://maven.springframework.org/release + + + repository.springframework.maven.milestone + Spring Framework Maven Milestone Repository + http://maven.springframework.org/milestone + + + repository.springframework.maven.snapshot + Spring Framework Maven Snapshot Repository + http://maven.springframework.org/snapshot + + + \ No newline at end of file diff --git a/basic/testing-examples/readme.txt b/basic/testing-examples/readme.txt new file mode 100644 index 00000000..5cc30c3a --- /dev/null +++ b/basic/testing-examples/readme.txt @@ -0,0 +1,44 @@ +A series of test cases that show techniques to test Spring Integration applications. + + +Examples +======== + +...spelchain.SpelChainTests.java + +This test case shows how to test a fragment of a larger integration flow. +In this example, we have a simple chain containing a header enricher and +a transformer. It uses Direct channels. The test case shows how to +bridge the output channel to a queue channel so we can retrieve the +output message after the test executes and verify the appropriate +actions were applied to the input message. + + +...splitter.CommaDelimitedSplitterTests.java + +This test case shows both unit testing a custom splitter class as well +as testing it within a Spring Integration flow fragment. + + +...aggregator.CommaDelimitedAggregatorTests.java + +This test case shows both unit testing a custom aggregator class as well +as testing it within a Spring Integration flow fragment. + + +...aggregator.ExternalGatewaySubstitutionTests.java + +This example is a little more complex. The application is based on a +travel demo presented at SpringOne in 2010. You do not need to fully understand +the application for the purposes of this demonstration. Suffice it to +say that a zip code is sent to two outbound gateways (one web service, one http) and +the weather and traffic for that zip code are aggregated together. You +can run the "real" application by executing the Main class in src/main/java +and entering a valid zip code. + +In many cases, when integrating with external services, we can't rely on +those services being available while we test. Consequently, we need a +mechanism to stub out those services so we can run the remainder of the +flow in a repeatable manner without need for connectivity to the services. +This example uses dummy service activators in place of the real outbound +gateways. \ No newline at end of file diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregator.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregator.java new file mode 100644 index 00000000..89279538 --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregator.java @@ -0,0 +1,47 @@ +/* + * 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.aggregator; + +import java.util.List; + +import org.springframework.integration.annotation.Aggregator; + +/** + * + * Aggregates messages into a comma-delimited list. + * + * @author Gary Russell + * @since 2.0.2 + * + */ +public class CommaDelimitedAggregator { + + @Aggregator + public String aggregate(List bits) { + StringBuilder sb = new StringBuilder(); + for (String bit : bits) { + sb.append(bit).append(","); + } + // remove final comma, if any + if (sb.length() > 0) { + sb.setLength(sb.length() - 1); + } + if (sb.length() < 1) { + return null; + } + return sb.toString(); + } +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Main.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Main.java new file mode 100644 index 00000000..7a7610d1 --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Main.java @@ -0,0 +1,44 @@ +/* + * 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.externalgateway; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author Gary Russell + * @since 2.0.2 + * + */ +public class Main { + + public static void main(String[] args) throws Exception { + AbstractApplicationContext context = new ClassPathXmlApplicationContext( + "META-INF/spring/integration/04-externalgateway/*.xml"); + System.out.println("Please enter zip"); + + BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); + String zip = console.readLine().trim(); + WeatherAndTraffic weatherAndTraffic = context.getBean("wat", WeatherAndTraffic.class); + String[] result = weatherAndTraffic.getByZip(zip); + System.out.println(result[0] + "\r\n" + result[1] + "\r\n"); + context.close(); + System.exit(0); + } +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Traffic.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Traffic.java new file mode 100644 index 00000000..c2367c84 --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Traffic.java @@ -0,0 +1,41 @@ +/* + * Copyright 2010 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.externalgateway; + +import java.util.HashMap; +import java.util.Map; +/** + * + * @author Oleg Zhurakousky + * @author Mark Fisher + * @since SpringOne2GX - 2010, Chicago + */ +public class Traffic { + + private Map incidents = new HashMap(); + + public void addIncident(String title, String description){ + incidents.put(title, description); + } + + public Map getIncidents(){ + return incidents; + } + + public String toString(){ + return "Traffic: {" + incidents.keySet().toString() + "}"; + } +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/TrafficHttpConverter.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/TrafficHttpConverter.java new file mode 100644 index 00000000..9e41426c --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/TrafficHttpConverter.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010 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.externalgateway; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import javax.xml.parsers.DocumentBuilderFactory; + +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConversionException; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.HttpMessageNotWritableException; +import org.springframework.xml.xpath.XPathExpression; +import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +/** + * + * @author Oleg Zhurakousky + * @author Mark Fisher + * @since SpringOne2GX - 2010, Chicago + */ +public class TrafficHttpConverter implements HttpMessageConverter { + private List supportedMediaTypes = Collections.emptyList(); + + @Override + public boolean canRead(Class clazz, MediaType mediaType) { + return Traffic.class.equals(clazz); + } + + @Override + public boolean canWrite(Class clazz, MediaType mediaType) { + return false; + } + + @Override + public List getSupportedMediaTypes() { + return supportedMediaTypes; + } + + @SuppressWarnings("unchecked") + @Override + public Traffic read(Class clazz, HttpInputMessage inputMessage) throws IOException, + HttpMessageNotReadableException { + Traffic traffic = new Traffic(); + try { + Document document = + DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputMessage.getBody()); + + XPathExpression titlesXp = XPathExpressionFactory.createXPathExpression("/ResultSet/Result[@type='incident']/Title"); + List titles = titlesXp.evaluateAsNodeList(document); + XPathExpression descXp = XPathExpressionFactory.createXPathExpression("/ResultSet/Result[@type='incident']/Description"); + List description = descXp.evaluateAsNodeList(document); + int counter = 0; + for (Node node : titles) { + traffic.addIncident(((Element)node).getTextContent(), ((Element)description.get(counter++)).getTextContent()); + } + } catch (Exception e) { + throw new HttpMessageConversionException("Failed to convert response to: " + clazz, e); + } + return traffic; + } + + @Override + public void write(Traffic t, MediaType contentType, + HttpOutputMessage outputMessage) throws IOException, + HttpMessageNotWritableException { + // for now this converter is only used for converting response + } + +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Weather.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Weather.java new file mode 100644 index 00000000..e4a346c8 --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/Weather.java @@ -0,0 +1,59 @@ +/* + * Copyright 2010 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.externalgateway; +/** + * + * @author Oleg Zhurakousky + * @author Mark Fisher + * @since SpringOne2GX - 2010, Chicago + */ +public class Weather { + + private String city; + private String state; + private String temperature; + private String description; + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getCity() { + return city; + } + public void setCity(String city) { + this.city = city; + } + public String getState() { + return state; + } + public void setState(String state) { + this.state = state; + } + public String getTemperature() { + return temperature; + } + public void setTemperature(String temperature) { + this.temperature = temperature; + } + + public String toString(){ + return description + " in " + city + + ", " + state + "; Temperature " + temperature + ""; + } +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherAndTraffic.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherAndTraffic.java new file mode 100644 index 00000000..ff62562b --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherAndTraffic.java @@ -0,0 +1,27 @@ +/* + * 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.externalgateway; + +/** + * @author Gary Russell + * @since 2.0.2 + * + */ +public interface WeatherAndTraffic { + + public String[] getByZip(String zip); + +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherMarshaller.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherMarshaller.java new file mode 100644 index 00000000..a84ee058 --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/externalgateway/WeatherMarshaller.java @@ -0,0 +1,131 @@ +/* + * Copyright 2010 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.externalgateway; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.oxm.Marshaller; +import org.springframework.oxm.MarshallingFailureException; +import org.springframework.oxm.Unmarshaller; +import org.springframework.oxm.XmlMappingException; +import org.springframework.xml.transform.StringResult; +import org.springframework.xml.transform.StringSource; +import org.springframework.xml.xpath.XPathExpressionFactory; +import org.w3c.dom.Document; + +import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; +/** + * + * @author Oleg Zhurakousky + * @author Mark Fisher + * @since SpringOne2GX - 2010, Chicago + */ +public class WeatherMarshaller implements Marshaller, Unmarshaller, InitializingBean { + private Map namespacePrefixes = new HashMap(); + private String xpathPrefix = "/p:GetCityWeatherByZIPResponse/p:GetCityWeatherByZIPResult/"; + + @Override + public Object unmarshal(Source source) throws IOException, XmlMappingException { + + //this.writeXml(((DOMSource)source).getNode().getOwnerDocument()); + DOMResult result = null; + try { + Transformer transformer = new TransformerFactoryImpl().newTransformer(); + result = new DOMResult(); + transformer.transform(source, result); + } catch (Exception e) { + throw new MarshallingFailureException("Failed to unmarshal SOAP Response", e); + } + Weather weather = new Weather(); + String expression = xpathPrefix + "p:City"; + String city = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); + weather.setCity(city); + expression = xpathPrefix + "p:State"; + String state = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); + weather.setState(state); + expression = xpathPrefix + "p:Temperature"; + String temperature = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); + weather.setTemperature(temperature); + expression = xpathPrefix + "p:Description"; + String description = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); + weather.setDescription(description); + return weather; + } + + @Override + public boolean supports(Class clazz) { + System.out.println("Suppors"); + return false; + } + + @Override + public void marshal(Object zip, Result result) throws IOException, + XmlMappingException { + String xmlString = "" + + " " + zip + "" + + ""; + try { + Transformer transformer = new TransformerFactoryImpl().newTransformer(); + transformer.transform(new StringSource(xmlString), result); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static final void writeXml(Document document) { + Transformer transformer = createIndentingTransformer(); + + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + + try { + StringResult streamResult = new StringResult(); + transformer.transform(new DOMSource(document), streamResult); + } catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + public static final Transformer createIndentingTransformer() { + Transformer xformer; + try { + xformer = TransformerFactory.newInstance().newTransformer(); + xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + xformer.setOutputProperty(OutputKeys.INDENT, "yes"); + xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2)); + } catch (Exception ex) { + throw new IllegalStateException(ex); + } + xformer.setOutputProperty(OutputKeys.INDENT, "yes"); + xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + return xformer; + } + + @Override + public void afterPropertiesSet() throws Exception { + namespacePrefixes.put("p", "http://ws.cdyne.com/WeatherWS/"); + } +} diff --git a/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitter.java b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitter.java new file mode 100644 index 00000000..d7a2c8ac --- /dev/null +++ b/basic/testing-examples/src/main/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitter.java @@ -0,0 +1,46 @@ +/* + * 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.splitter; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.integration.annotation.Splitter; + +/** + * Splits a message containing a comma-delimited list into + * a list of strings. Empty elements are dropped. + * + * @author Gary Russell + * @since 2.0.2 + * + */ +public class CommaDelimitedSplitter { + + @Splitter + public List split(String input) { + String[] splits = input.split(","); + List list = new ArrayList(); + for (String split : splits) { + String trimmed = split.trim(); + if (trimmed.length() > 0) { + list.add(trimmed); + } + } + return list; + } + +} diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/01-chain/integration-context.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/01-chain/integration-context.xml new file mode 100644 index 00000000..0bee0ac6 --- /dev/null +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/01-chain/integration-context.xml @@ -0,0 +1,22 @@ + + + + + + + +
+ + + + + + + diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/02-splitter/integration-context.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/02-splitter/integration-context.xml new file mode 100644 index 00000000..811c3868 --- /dev/null +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/02-splitter/integration-context.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/03-aggregator/integration-context.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/03-aggregator/integration-context.xml new file mode 100644 index 00000000..35ed7c27 --- /dev/null +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/03-aggregator/integration-context.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/main-integration-context.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/main-integration-context.xml new file mode 100644 index 00000000..f475f75d --- /dev/null +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/main-integration-context.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/real-gateways.xml b/basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/real-gateways.xml new file mode 100644 index 00000000..cf229cf3 --- /dev/null +++ b/basic/testing-examples/src/main/resources/META-INF/spring/integration/04-externalgateway/real-gateways.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/testing-examples/src/main/resources/log4j.xml b/basic/testing-examples/src/main/resources/log4j.xml new file mode 100644 index 00000000..1c6e74d9 --- /dev/null +++ b/basic/testing-examples/src/main/resources/log4j.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests-context.xml b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests-context.xml new file mode 100644 index 00000000..7a471ee2 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests-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/aggregator/CommaDelimitedAggregatorTests.java b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests.java new file mode 100644 index 00000000..bf936673 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/aggregator/CommaDelimitedAggregatorTests.java @@ -0,0 +1,124 @@ +/* + * 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.aggregator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; + +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.MessageChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.samples.testing.splitter.CommaDelimitedSplitter; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * + * Shows how to test a custom aggregator. Unit test for the class and + * tests for the integration subflow. + * The subflow has direct input and output channels. The flow 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 + * @since 2.0.2 + * + */ +@ContextConfiguration // default context name is -context.xml +@RunWith(SpringJUnit4ClassRunner.class) +public class CommaDelimitedAggregatorTests { + + @Autowired + MessageChannel inputChannel; + + @Autowired + QueueChannel testChannel; + + @Test + public void unitTestClass3() { + List splits = new CommaDelimitedSplitter().split(" a , b, c "); + String out = new CommaDelimitedAggregator().aggregate(splits); + assertEquals("a,b,c", out); + } + + @Test + public void unitTestClass2() { + List splits = new CommaDelimitedSplitter().split(" a ,, c "); + String out = new CommaDelimitedAggregator().aggregate(splits); + assertEquals("a,c", out); + } + + @Test + public void unitTestClass0() { + List splits = new CommaDelimitedSplitter().split(",,, ,, ,, ,,"); + String out = new CommaDelimitedAggregator().aggregate(splits); + assertNull(out); + } + + @Test + public void testOne() { + inputChannel.send(MessageBuilder.withPayload(" a ").build()); + Message outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("A", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNull("Only one message expected", outMessage); + } + + @Test + public void testTwo() { + inputChannel.send(MessageBuilder.withPayload(" a ,z ").build()); + Message outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("A,Z", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNull("Only one message expected", outMessage); + } + + @Test + public void testSkipEmpty() { + inputChannel.send(MessageBuilder.withPayload(" a ,,z ").build()); + Message outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("A,Z", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNull("Only one message expected", outMessage); + } + + @Test + public void testNone() { + inputChannel.send(MessageBuilder.withPayload(" ,, ,,, ,,,,, ,,,,,,, ").build()); + Message outMessage = testChannel.receive(0); + assertNull("No messages expected", outMessage); + } + + @Test + public void testEmpty() { + inputChannel.send(MessageBuilder.withPayload("").build()); + Message outMessage = testChannel.receive(0); + assertNull("No messages expected", outMessage); + } +} diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests-context.xml b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests-context.xml new file mode 100644 index 00000000..d9e868b9 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests-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/chain/SpelChainTests.java b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests.java new file mode 100644 index 00000000..726502c9 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/chain/SpelChainTests.java @@ -0,0 +1,81 @@ +/* + * 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.chain; + +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.MessageChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * + * Shows how to test a chain of endpoints that use SpEL expressions. + * The chain has direct input and output channels. The chain 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 + * @since 2.0.2 + * + */ +@ContextConfiguration // default context name is -context.xml +@RunWith(SpringJUnit4ClassRunner.class) +public class SpelChainTests { + + @Autowired + MessageChannel inputChannel; + + @Autowired + QueueChannel testChannel; + + @Test + public void testTrueHeader() { + String payload = "XXXABCXXX"; + Message message = MessageBuilder.withPayload(payload).build(); + inputChannel.send(message); + Message outMessage = testChannel.receive(0); + assertNotNull("Expected an output message", outMessage); + Object myHeader = outMessage.getHeaders().get("myHeader"); + assertNotNull("Expecter myHeader header", myHeader); + assertEquals("Expected myHeader==true", Boolean.TRUE, myHeader); + assertEquals("Expected lower case message", payload.toLowerCase(), outMessage.getPayload()); + } + + @Test + public void testFalseHeader() { + String payload = "XXXDEFXXX"; + Message message = MessageBuilder.withPayload(payload).build(); + inputChannel.send(message); + Message outMessage = testChannel.receive(0); + assertNotNull("Expected an output message", outMessage); + Object myHeader = outMessage.getHeaders().get("myHeader"); + assertNotNull("Expecter myHeader header", myHeader); + assertEquals("Expected myHeader==true", Boolean.FALSE, myHeader); + assertEquals("Expected lower case message", payload.toLowerCase(), outMessage.getPayload()); + } + +} diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests-context.xml b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests-context.xml new file mode 100644 index 00000000..6d0f0c67 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests-context.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests.java b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests.java new file mode 100644 index 00000000..bf945071 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/externalgateway/ExternalGatewaySubstitutionTests.java @@ -0,0 +1,58 @@ +/* + * 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.externalgateway; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * Shows how to stub-out external outbound-gateways with service activators + * enabling isolated and repeatable testing. + * + * @author Gary Russell + * @since 2.0.2 + * + */ +@ContextConfiguration // default context name is -context.xml +@RunWith(SpringJUnit4ClassRunner.class) +public class ExternalGatewaySubstitutionTests { + + @Autowired + WeatherAndTraffic weatherAndTraffic; + + @Test + public void doTest() { + String[] results = weatherAndTraffic.getByZip("12345"); + assertEquals(2, results.length); + List list = new ArrayList(); + list.add(results[0]); + list.add(results[1]); + Collections.sort(list); + assertEquals("Dummy traffic for zip:12345", list.get(0)); + assertEquals("Dummy weather for zip:12345", list.get(1)); + } + +} diff --git a/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests-context.xml b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests-context.xml new file mode 100644 index 00000000..aed28805 --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests-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/splitter/CommaDelimitedSplitterTests.java b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests.java new file mode 100644 index 00000000..a66a262b --- /dev/null +++ b/basic/testing-examples/src/test/java/org/springframework/integration/samples/testing/splitter/CommaDelimitedSplitterTests.java @@ -0,0 +1,131 @@ +/* + * 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.splitter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; + +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.MessageChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * + * Shows how to test a custom splitter. Unit test for the class and + * tests for the integration subflow. + * The splitter has direct input and output channels. The splitter 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 + * @since 2.0.2 + * + */ +@ContextConfiguration // default context name is -context.xml +@RunWith(SpringJUnit4ClassRunner.class) +public class CommaDelimitedSplitterTests { + + @Autowired + MessageChannel inputChannel; + + @Autowired + QueueChannel testChannel; + + @Test + public void unitTestClass3() { + List splits = new CommaDelimitedSplitter().split(" a , b, c "); + assertEquals("Expected 3 splits", 3, splits.size()); + assertEquals("a", splits.get(0)); + assertEquals("b", splits.get(1)); + assertEquals("c", splits.get(2)); + } + + @Test + public void unitTestClass2() { + List splits = new CommaDelimitedSplitter().split(" a ,, c "); + assertEquals("Expected 2 splits", 2, splits.size()); + assertEquals("a", splits.get(0)); + assertEquals("c", splits.get(1)); + } + + @Test + public void unitTestClass0() { + List splits = new CommaDelimitedSplitter().split(",,, ,, ,, ,,"); + assertEquals("Expected 0 splits", 0, splits.size()); + } + + @Test + public void testOne() { + inputChannel.send(MessageBuilder.withPayload(" a ").build()); + Message outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("a", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNull("Only one message expected", outMessage); + } + + @Test + public void testTwo() { + inputChannel.send(MessageBuilder.withPayload(" a ,z ").build()); + Message outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("a", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("z", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNull("Only two messages expected", outMessage); + } + + @Test + public void testSkipEmpty() { + inputChannel.send(MessageBuilder.withPayload(" a ,,z ").build()); + Message outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("a", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNotNull(outMessage); + assertEquals("z", outMessage.getPayload()); + outMessage = testChannel.receive(0); + assertNull("Only two messages expected", outMessage); + } + + @Test + public void testNone() { + inputChannel.send(MessageBuilder.withPayload(" ,, ,,, ,,,,, ,,,,,,, ").build()); + Message outMessage = testChannel.receive(0); + assertNull("No messages expected", outMessage); + } + + @Test + public void testEmpty() { + inputChannel.send(MessageBuilder.withPayload("").build()); + Message outMessage = testChannel.receive(0); + assertNull("No messages expected", outMessage); + } +}