INTSAMPLES-26 Use Payload and Header Matchers in Tests

This commit is contained in:
Gary Russell
2011-07-26 17:40:38 +05:30
parent 94ea8d35ea
commit 37ae292151
9 changed files with 35 additions and 32 deletions

View File

@@ -3,7 +3,7 @@
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/JVM 1.6.0 (MacOS X Default)"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -29,6 +29,11 @@
<artifactId>spring-integration-http</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-test</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>

View File

@@ -42,22 +42,18 @@ import org.w3c.dom.Node;
public class TrafficHttpConverter implements HttpMessageConverter<Traffic> {
private List<MediaType> 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<MediaType> getSupportedMediaTypes() {
return supportedMediaTypes;
}
@Override
public Traffic read(Class<? extends Traffic> clazz, HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException {
Traffic traffic = new Traffic();
@@ -79,7 +75,6 @@ public class TrafficHttpConverter implements HttpMessageConverter<Traffic> {
return traffic;
}
@Override
public void write(Traffic t, MediaType contentType,
HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {

View File

@@ -48,7 +48,6 @@ public class WeatherMarshaller implements Marshaller, Unmarshaller, Initializing
private Map<String, String> namespacePrefixes = new HashMap<String, String>();
private String xpathPrefix = "/p:GetCityWeatherByZIPResponse/p:GetCityWeatherByZIPResult/";
@Override
public Object unmarshal(Source source) throws IOException, XmlMappingException {
//this.writeXml(((DOMSource)source).getNode().getOwnerDocument());
@@ -76,13 +75,11 @@ public class WeatherMarshaller implements Marshaller, Unmarshaller, Initializing
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 = "<weat:GetCityWeatherByZIP xmlns:weat=\"http://ws.cdyne.com/WeatherWS/\">" +
@@ -124,7 +121,6 @@ public class WeatherMarshaller implements Marshaller, Unmarshaller, Initializing
return xformer;
}
@Override
public void afterPropertiesSet() throws Exception {
namespacePrefixes.put("p", "http://ws.cdyne.com/WeatherWS/");
}

View File

@@ -18,6 +18,8 @@ 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 static org.junit.Assert.assertThat;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
import java.util.List;
@@ -83,7 +85,7 @@ public class CommaDelimitedAggregatorTests {
inputChannel.send(MessageBuilder.withPayload(" a ").build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("A", outMessage.getPayload());
assertThat(outMessage, hasPayload("A"));
outMessage = testChannel.receive(0);
assertNull("Only one message expected", outMessage);
}
@@ -93,7 +95,7 @@ public class CommaDelimitedAggregatorTests {
inputChannel.send(MessageBuilder.withPayload(" a ,z ").build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("A,Z", outMessage.getPayload());
assertThat(outMessage, hasPayload("A,Z"));
outMessage = testChannel.receive(0);
assertNull("Only one message expected", outMessage);
}
@@ -103,7 +105,7 @@ public class CommaDelimitedAggregatorTests {
inputChannel.send(MessageBuilder.withPayload(" a ,,z ").build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("A,Z", outMessage.getPayload());
assertThat(outMessage, hasPayload("A,Z"));
outMessage = testChannel.receive(0);
assertNull("Only one message expected", outMessage);
}

View File

@@ -15,11 +15,12 @@
*/
package org.springframework.integration.samples.testing.filter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -100,7 +101,7 @@ public class PetFilterTests {
inputChannel.send(message);
Message<?> outMessage = testChannel.receive(0);
assertNotNull("Expected an output message", outMessage);
assertEquals(payload, outMessage.getPayload());
assertThat(outMessage, hasPayload(payload));
}
@Test
@@ -121,7 +122,7 @@ public class PetFilterTests {
assertNull("Expected no output message", outMessage);
outMessage = testDiscardChannel2.receive(0);
assertNotNull("Expected discard message", outMessage);
assertEquals(payload, message.getPayload());
assertThat(outMessage, hasPayload(payload));
}
@Test
@@ -131,7 +132,7 @@ public class PetFilterTests {
inputChannel2.send(message);
Message<?> outMessage = testChannel.receive(0);
assertNotNull("Expected an output message", outMessage);
assertEquals(payload, outMessage.getPayload());
assertThat(outMessage, hasPayload(payload));
outMessage = testDiscardChannel2.receive(0);
assertNull("Expected no discard message", outMessage);
}
@@ -145,6 +146,6 @@ public class PetFilterTests {
assertNull("Expected no output message", outMessage);
outMessage = testDiscardChannel2.receive(0);
assertNotNull("Expected discard message", outMessage);
assertEquals(payload, message.getPayload());
assertThat(outMessage, hasPayload(payload));
}
}

View File

@@ -15,14 +15,15 @@
*/
package org.springframework.integration.samples.testing.gateway;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.springframework.integration.test.matcher.HeaderMatcher.hasHeader;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
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;
@@ -60,9 +61,8 @@ public class GatewayTests {
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"));
assertThat(inMessage, hasPayload(payload));
assertThat(inMessage, hasHeader("configuredHeader", "abc"));
assertThat(inMessage, hasHeader(FileHeaders.FILENAME, fileName));
}
}

View File

@@ -17,6 +17,8 @@ package org.springframework.integration.samples.testing.router;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -82,7 +84,7 @@ public class PetRouterTests {
inputChannel.send(message);
Message<?> outMessage = testFelineChannel.receive(0);
assertNotNull("Expected an output message", outMessage);
assertEquals(payload, outMessage.getPayload());
assertThat(outMessage, hasPayload(payload));
}
@Test
@@ -92,7 +94,7 @@ public class PetRouterTests {
inputChannel.send(message);
Message<?> outMessage = testCanineChannel.receive(0);
assertNotNull("Expected an output message", outMessage);
assertEquals(payload, outMessage.getPayload());
assertThat(outMessage, hasPayload(payload));
}
@Test
@@ -102,6 +104,6 @@ public class PetRouterTests {
inputChannel.send(message);
Message<?> outMessage = testUnknownPetTypeChannel.receive(0);
assertNotNull("Expected an output message", outMessage);
assertEquals(payload, outMessage.getPayload());
assertThat(outMessage, hasPayload(payload));
}
}

View File

@@ -18,6 +18,8 @@ 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 static org.junit.Assert.assertThat;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
import java.util.List;
@@ -84,7 +86,7 @@ public class CommaDelimitedSplitterTests {
inputChannel.send(MessageBuilder.withPayload(" a ").build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("a", outMessage.getPayload());
assertThat(outMessage, hasPayload("a"));
outMessage = testChannel.receive(0);
assertNull("Only one message expected", outMessage);
}
@@ -94,10 +96,10 @@ public class CommaDelimitedSplitterTests {
inputChannel.send(MessageBuilder.withPayload(" a ,z ").build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("a", outMessage.getPayload());
assertThat(outMessage, hasPayload("a"));
outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("z", outMessage.getPayload());
assertThat(outMessage, hasPayload("z"));
outMessage = testChannel.receive(0);
assertNull("Only two messages expected", outMessage);
}
@@ -107,10 +109,10 @@ public class CommaDelimitedSplitterTests {
inputChannel.send(MessageBuilder.withPayload(" a ,,z ").build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("a", outMessage.getPayload());
assertThat(outMessage, hasPayload("a"));
outMessage = testChannel.receive(0);
assertNotNull(outMessage);
assertEquals("z", outMessage.getPayload());
assertThat(outMessage, hasPayload("z"));
outMessage = testChannel.receive(0);
assertNull("Only two messages expected", outMessage);
}