From c756aff3af58d2869290f0076389a5dceb34a80c Mon Sep 17 00:00:00 2001 From: Iwein Fuld Date: Tue, 24 Mar 2009 10:36:28 +0000 Subject: [PATCH] Filled out tests, added matchers to TestUtils --- .../HttpInboundGatewayParserTests-context.xml | 18 ++-- .../config/HttpInboundGatewayParserTests.java | 41 ++++++++- .../integration/util/TestUtils.java | 92 +++++++++++++++++++ 3 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 org.springframework.integration.http/src/test/java/org/springframework/integration/util/TestUtils.java diff --git a/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests-context.xml b/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests-context.xml index 4dcbaca75e..7c8b2216ab 100644 --- a/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests-context.xml +++ b/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests-context.xml @@ -1,19 +1,21 @@ + http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd"> - - - - - + + + + + + + diff --git a/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests.java b/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests.java index 9398cf9c05..30f4bc17f2 100644 --- a/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests.java +++ b/org.springframework.integration.http/src/test/java/org/springframework/integration/http/config/HttpInboundGatewayParserTests.java @@ -16,21 +16,60 @@ package org.springframework.integration.http.config; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.springframework.integration.util.TestUtils.*; + +import javax.servlet.http.HttpServletResponse; + import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.channel.SubscribableChannel; +import org.springframework.integration.core.Message; +import org.springframework.integration.core.MessageChannel; +import org.springframework.integration.http.HttpInboundEndpoint; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Mark Fisher + * @author Iwein Fuld */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class HttpInboundGatewayParserTests { + @Autowired + private HttpInboundEndpoint gateway; + + @Qualifier("responses") + @Autowired + MessageChannel responses; + + @Qualifier("requests") + @Autowired + SubscribableChannel requests; + @Test public void checkConfig() { + assertNotNull(gateway); + assertThat((Boolean) getPropertyValue(gateway, "expectReply"), is(true)); + } + + @Test(timeout=1000) + public void checkFlow() throws Exception { + requests.subscribe(handlerExpecting(any(Message.class))); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setMethod("GET"); + request.setParameter("foo", "bar"); + MockHttpServletResponse response = new MockHttpServletResponse(); + gateway.handleRequest(request, response); + assertThat(response.getStatus(), is(HttpServletResponse.SC_OK)); + assertThat(response.getContentType(), is("application/x-java-serialized-object")); } - } diff --git a/org.springframework.integration.http/src/test/java/org/springframework/integration/util/TestUtils.java b/org.springframework.integration.http/src/test/java/org/springframework/integration/util/TestUtils.java new file mode 100644 index 0000000000..e180864fbe --- /dev/null +++ b/org.springframework.integration.http/src/test/java/org/springframework/integration/util/TestUtils.java @@ -0,0 +1,92 @@ +/* + * Copyright 2002-2008 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.util; + +import static org.junit.Assert.assertThat; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.integration.core.Message; +import org.springframework.integration.message.MessageHandler; +import org.springframework.util.Assert; + +/** + * This is a utitily class for tests with convenience methods needed in this + * project + * + * Since there cannot be inter project dependencies on test code this utility + * class is copied around. This practice should be deprecated when there is a + * test project. + * + * @author Mark Fisher + * @author Iwein Fuld + */ +public abstract class TestUtils { + + public static Object getPropertyValue(Object root, String propertyPath) { + Object value = null; + DirectFieldAccessor accessor = new DirectFieldAccessor(root); + String[] tokens = propertyPath.split("\\."); + for (int i = 0; i < tokens.length; i++) { + value = accessor.getPropertyValue(tokens[i]); + if (value != null) { + accessor = new DirectFieldAccessor(value); + } + else if (i == tokens.length - 1) { + return null; + } + else { + throw new IllegalArgumentException("intermediate property '" + + tokens[i] + "' is null"); + } + } + return value; + } + + @SuppressWarnings("unchecked") + public static T getPropertyValue(Object root, String propertyPath, + Class type) { + Object value = getPropertyValue(root, propertyPath); + Assert.isAssignable(type, value.getClass()); + return (T) value; + } + + @SuppressWarnings("unchecked") + public static MessageHandler handlerExpecting( + final Matcher expectation) { + return new MessageHandler() { + public void handleMessage(Message message) { + assertThat(message, expectation); + } + }; + } + + public static Matcher> messageWithPayload(final Object payload) { + return new BaseMatcher>() { + + public boolean matches(Object candidate) { + return ((Message) candidate).getPayload().equals(payload); + } + + public void describeTo(Description description) { + description.appendText("A message with payload: " + payload); + } + }; + } +}