Filled out tests, added matchers to TestUtils

This commit is contained in:
Iwein Fuld
2009-03-24 10:36:28 +00:00
parent 7b32947f4e
commit c756aff3af
3 changed files with 142 additions and 9 deletions

View File

@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:si="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/http
http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd">
http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd">
<si:channel id="requests">
<si:queue capacity="1"/>
</si:channel>
<inbound-gateway id="inboundGateway" request-channel="requests"/>
<si:publish-subscribe-channel id="requests"/>
<si:bridge output-channel="responses" input-channel="requests"/>
<si:channel id="responses"/>
<inbound-gateway id="inboundGateway" request-channel="requests"
reply-channel="responses" />
</beans:beans>

View File

@@ -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"));
}
}

View File

@@ -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> T getPropertyValue(Object root, String propertyPath,
Class<T> type) {
Object value = getPropertyValue(root, propertyPath);
Assert.isAssignable(type, value.getClass());
return (T) value;
}
@SuppressWarnings("unchecked")
public static MessageHandler handlerExpecting(
final Matcher<Message> expectation) {
return new MessageHandler() {
public void handleMessage(Message<?> message) {
assertThat(message, expectation);
}
};
}
public static Matcher<Message<?>> messageWithPayload(final Object payload) {
return new BaseMatcher<Message<?>>() {
public boolean matches(Object candidate) {
return ((Message<?>) candidate).getPayload().equals(payload);
}
public void describeTo(Description description) {
description.appendText("A message with payload: " + payload);
}
};
}
}