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 2010/05/21 08:44:18 UTC
svn commit: r946905 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/component/mock/MockEndpoint.java
test/java/org/apache/camel/component/mock/MockAsBeanTest.java
Author: davsclaus
Date: Fri May 21 06:44:18 2010
New Revision: 946905
URL: http://svn.apache.org/viewvc?rev=946905&view=rev
Log:
CAMEL-2061: Allow a MockEndpoint to act as a bean which can be leveraged in unit testing
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.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=946905&r1=946904&r2=946905&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 Fri May 21 06:44:18 2010
@@ -35,6 +35,7 @@ import org.apache.camel.Consumer;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
+import org.apache.camel.Handler;
import org.apache.camel.Message;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
@@ -221,6 +222,21 @@ public class MockEndpoint extends Defaul
// -------------------------------------------------------------------------
/**
+ * Handles the incoming exchange.
+ * <p/>
+ * This method turns this mock endpoint into a bean which you can use
+ * in the Camel routes, which allows you to inject MockEndpoint as beans
+ * in your routes and use the features of the mock to control the bean.
+ *
+ * @param exchange the exchange
+ * @throws Exception can be thrown
+ */
+ @Handler
+ public void handle(Exchange exchange) throws Exception {
+ onExchange(exchange);
+ }
+
+ /**
* Set the processor that will be invoked when the index
* message is received.
*/
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java?rev=946905&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java Fri May 21 06:44:18 2010
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.mock;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class MockAsBeanTest extends ContextTestSupport {
+
+ private MockEndpoint bean = new MockEndpoint();
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("foo", bean);
+ return jndi;
+ }
+
+ public void testMockAsBean() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+
+ bean.whenAnyExchangeReceived(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String in = exchange.getIn().getBody(String.class);
+ exchange.getIn().setBody("Bye " + in);
+ }
+ });
+
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").beanRef("foo").to("mock:result");
+ }
+ };
+ }
+
+
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockAsBeanTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date