You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/06/29 11:39:36 UTC
svn commit: r1141025 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/component/mock/MockEndpoint.java
test/java/org/apache/camel/component/mock/MockEndpointTest.java
Author: davsclaus
Date: Wed Jun 29 09:39:36 2011
New Revision: 1141025
URL: http://svn.apache.org/viewvc?rev=1141025&view=rev
Log:
CAMEL-4159: Mock endpoint improved type coercion for expected bodies.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?rev=1141025&r1=1141024&r2=1141025&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java Wed Jun 29 09:39:36 2011
@@ -488,15 +488,7 @@ public class MockEndpoint extends Defaul
}
Object actualValue = exchange.getIn().getHeader(key);
- if (actualValue instanceof Expression) {
- actualValue = ((Expression)actualValue).evaluate(exchange, expectedValue.getClass());
- } else if (actualValue instanceof Predicate) {
- actualValue = ((Predicate)actualValue).matches(exchange);
- } else if (expectedValue != null) {
- actualValue = getCamelContext().getTypeConverter().convertTo(expectedValue.getClass(), actualValue);
- assertTrue("There is no type conversion possible from " + actualValue.getClass().getName()
- + " to " + expectedValue.getClass().getName(), actualValue != null);
- }
+ actualValue = extractActualValue(exchange, actualValue, expectedValue);
assertEquals("Header with name " + key + " for message: " + i, expectedValue, actualValue);
}
@@ -536,15 +528,7 @@ public class MockEndpoint extends Defaul
}
Object actualValue = exchange.getProperty(key);
- if (actualValue instanceof Expression) {
- actualValue = ((Expression)actualValue).evaluate(exchange, expectedValue.getClass());
- } else if (actualValue instanceof Predicate) {
- actualValue = ((Predicate)actualValue).matches(exchange);
- } else if (expectedValue != null) {
- actualValue = getCamelContext().getTypeConverter().convertTo(expectedValue.getClass(), actualValue);
- assertTrue("There is no type conversion possible from " + actualValue.getClass().getName()
- + " to " + expectedValue.getClass().getName(), actualValue != null);
- }
+ actualValue = extractActualValue(exchange, actualValue, expectedValue);
assertEquals("Property with name " + key + " for message: " + i, expectedValue, actualValue);
}
@@ -574,14 +558,32 @@ public class MockEndpoint extends Defaul
if (i < actualBodyValues.size()) {
actualBody = actualBodyValues.get(i);
}
+ actualBody = extractActualValue(exchange, actualBody, expectedBody);
- // TODO: coerce types before assertEquals
assertEquals("Body of message: " + i, expectedBody, actualBody);
}
}
});
}
+ private Object extractActualValue(Exchange exchange, Object actualValue, Object expectedValue) {
+ if (actualValue == null) {
+ return null;
+ }
+
+ if (actualValue instanceof Expression) {
+ actualValue = ((Expression)actualValue).evaluate(exchange, expectedValue != null ? expectedValue.getClass() : Object.class);
+ } else if (actualValue instanceof Predicate) {
+ actualValue = ((Predicate)actualValue).matches(exchange);
+ } else if (expectedValue != null) {
+ String from = actualValue.getClass().getName();
+ String to = expectedValue.getClass().getName();
+ actualValue = getCamelContext().getTypeConverter().convertTo(expectedValue.getClass(), actualValue);
+ assertTrue("There is no type conversion possible from " + from + " to " + to, actualValue != null);
+ }
+ return actualValue;
+ }
+
/**
* Sets an expectation that the given predicates matches the received messages by this endpoint
*/
@@ -1080,7 +1082,12 @@ public class MockEndpoint extends Defaul
if (expectedBodyValues.size() > index) {
Object expectedBody = expectedBodyValues.get(index);
if (expectedBody != null) {
- actualBody = in.getBody(expectedBody.getClass());
+ // prefer to convert body early, for example when using files
+ // we need to read the content at this time
+ Object body = in.getBody(expectedBody.getClass());
+ if (body != null) {
+ actualBody = body;
+ }
}
actualBodyValues.add(actualBody);
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java?rev=1141025&r1=1141024&r2=1141025&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java Wed Jun 29 09:39:36 2011
@@ -26,6 +26,7 @@ import org.apache.camel.ContextTestSuppo
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
+import org.apache.camel.builder.ExpressionBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.xml.XPathBuilder;
import org.apache.camel.impl.JndiRegistry;
@@ -190,6 +191,15 @@ public class MockEndpointTest extends Co
resultEndpoint.assertIsSatisfied();
}
+ public void testExpressionExpectationOfProperty() throws InterruptedException {
+ MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
+ resultEndpoint.reset();
+
+ resultEndpoint.expectedPropertyReceived("number", 123);
+ template.sendBodyAndProperty("direct:a", "<foo><id>123</id></foo>", "number", XPathBuilder.xpath("/foo/id", Integer.class));
+ resultEndpoint.assertIsSatisfied();
+ }
+
public void testAscending() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectsAscending().body();
@@ -769,6 +779,27 @@ public class MockEndpointTest extends Co
mock.assertIsNotSatisfied();
}
+ public void testExpectedBodyTypeCoerce() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived(987);
+
+ // start with 0 (zero) to have it converted to the number and match 987
+ template.sendBody("direct:a", "0987");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testExpectedBodyExpression() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived(987);
+
+ // start with 0 (zero) to have it converted to the number and match 987
+ // and since its an expression it would be evaluated first as well
+ template.sendBody("direct:a", ExpressionBuilder.constantExpression("0987"));
+
+ assertMockEndpointsSatisfied();
+ }
+
protected void sendMessages(int... counters) {
for (int counter : counters) {
template.sendBodyAndHeader("direct:a", createTestMessage(counter), "counter", counter);