activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r530414 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/mock/ main/resources/META-INF/services/org/apache/camel/component/ test/java/org/apache/camel/ test/java/org/apache/camel/processor/
Date Thu, 19 Apr 2007 13:50:11 GMT
Author: jstrachan
Date: Thu Apr 19 06:50:08 2007
New Revision: 530414

URL: http://svn.apache.org/viewvc?view=rev&rev=530414
Log:
added a mock endpoint which can be used for easier testing in a JMock sense; where expectations
can be put on endpoints, then a route run and the assertions then applied

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockComponent.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/mock
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockComponent.java?view=auto&rev=530414
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockComponent.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockComponent.java
Thu Apr 19 06:50:08 2007
@@ -0,0 +1,37 @@
+/**
+ *
+ * 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.Exchange;
+import org.apache.camel.Endpoint;
+import org.apache.camel.impl.DefaultComponent;
+
+import java.util.Map;
+
+/**
+ * A factory of {@link MockEndpoint} instances
+ *
+ * @version $Revision: 1.1 $
+ */
+public class MockComponent extends DefaultComponent<Exchange> {
+
+    @Override
+    protected Endpoint<Exchange> createEndpoint(String uri, String remaining, Map parameters)
throws Exception {
+        return new MockEndpoint(uri, this);
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockComponent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?view=auto&rev=530414
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
Thu Apr 19 06:50:08 2007
@@ -0,0 +1,141 @@
+/**
+ *
+ * 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.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A Mock endpoint which provides a literate, fluent API for testing routes using
+ * a <a href="http://jmock.org/">JMock style</a> API.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class MockEndpoint extends DefaultEndpoint<Exchange> {
+    private int receivedCounter;
+    private int expectedCount = -1;
+    private Map<Integer, Processor<Exchange>> processors = new HashMap<Integer,
Processor<Exchange>>();
+    private List<Exchange> exchangesReceived = new ArrayList<Exchange>();
+    private List<Throwable> failures = new ArrayList<Throwable>();
+    private CountDownLatch latch = new CountDownLatch(1);
+
+    public MockEndpoint(String endpointUri, Component component) {
+        super(endpointUri, component);
+    }
+
+    public Exchange createExchange() {
+        return new DefaultExchange(getContext());
+    }
+
+    public Consumer<Exchange> createConsumer(Processor<Exchange> processor) throws
Exception {
+        throw new UnsupportedOperationException("You cannot consume from this endpoint");
+    }
+
+    public Producer<Exchange> createProducer() throws Exception {
+        return new DefaultProducer<Exchange>(this) {
+            public void process(Exchange exchange) {
+                onExchange(exchange);
+            }
+        };
+    }
+
+    // Testing API
+    //-------------------------------------------------------------------------
+    public void assertIsSatisfied() throws InterruptedException {
+        if (latch != null) {
+            // now lets wait for the results
+            latch.await(10, TimeUnit.SECONDS);
+        }
+        else if (expectedCount == 0) {
+            // lets wait a little bit just in case
+            Thread.sleep(1000);
+        }
+
+        if (expectedCount >= 0) {
+            assertEquals("Expected message count", expectedCount, receivedCounter);
+        }
+    }
+
+    protected void assertEquals(String message, Object expectedValue, Object actualValue)
{
+        if (!ObjectHelper.equals(expectedValue, actualValue)) {
+            throw new AssertionError(message + ". Expected: <" + expectedValue + ">
but was: <" + actualValue + ">");
+        }
+    }
+
+    public void reset() {
+        receivedCounter = 0;
+    }
+
+    public void expectedMessageCount(int expectedCount) {
+        this.expectedCount = expectedCount;
+        if (expectedCount <= 0) {
+            latch = null;
+        }
+        else {
+            latch = new CountDownLatch(expectedCount);
+        }
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public List<Throwable> getFailures() {
+        return failures;
+    }
+
+    public int getReceivedCounter() {
+        return receivedCounter;
+    }
+
+    public List<Exchange> getExchangesReceived() {
+        return exchangesReceived;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+    protected synchronized void onExchange(Exchange exchange) {
+        try {
+            exchangesReceived.add(exchange);
+
+            if (latch != null) {
+                latch.countDown();
+            }
+
+            Processor<Exchange> processor = processors.get(++receivedCounter);
+            if (processor != null) {
+                processor.process(exchange);
+            }
+        }
+        catch (Exception e) {
+            failures.add(e);
+        }
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/mock
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/mock?view=auto&rev=530414
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/mock
(added)
+++ activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/mock
Thu Apr 19 06:50:08 2007
@@ -0,0 +1 @@
+class=org.apache.camel.component.mock.MockComponent
\ No newline at end of file

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java?view=diff&rev=530414&r1=530413&r2=530414
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java Thu Apr
19 06:50:08 2007
@@ -84,4 +84,15 @@
         assertEquals("Predicate: " + expression + " on Exchange: " + exchange, expected,
value);
         return value;
     }
+
+    /**
+     * Resolves an endpoint and asserts that it is found
+     */
+    protected Endpoint<Exchange> resolveMandatoryEndpoint(CamelContext context, String
uri) {
+        Endpoint endpoint = context.resolveEndpoint(uri);
+
+        assertNotNull("No endpoint found for URI: " + uri, endpoint);
+
+        return endpoint;
+    }
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java?view=auto&rev=530414
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java
Thu Apr 19 06:50:08 2007
@@ -0,0 +1,99 @@
+/**
+ *
+ * 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.processor;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Message;
+import org.apache.camel.Endpoint;
+import org.apache.camel.TestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.util.ProducerCache;
+import org.apache.camel.impl.DefaultCamelContext;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class FilterTest extends TestSupport {
+    protected CamelContext context = new DefaultCamelContext();
+    protected Endpoint<Exchange> startEndpoint;
+    protected MockEndpoint resultEndpoint;
+    protected ProducerCache<Exchange> client = new ProducerCache<Exchange>();
+
+    public void testSendMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(1);
+
+        sendMessage("bar", "matched");
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    public void testSendNotMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(0);
+
+        sendMessage("notMatchedHeaderValue", "notMatched");
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        context.addRoutes(createRouteBuilder());
+
+        startEndpoint = resolveMandatoryEndpoint(context, "queue:a");
+        resultEndpoint = (MockEndpoint)resolveMandatoryEndpoint(context, "mock:result");
+
+        context.start();
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder<Exchange>() {
+            public void configure() {
+                from("queue:a").filter(header("foo").isEqualTo("bar")).to("mock:result");
+            }
+        };
+    }
+
+    protected void sendMessage(final Object headerValue, final Object body) throws Exception
{
+        client.send(startEndpoint, new Processor<Exchange>() {
+            public void process(Exchange exchange) {
+                // now lets fire in a message
+                Message in = exchange.getIn();
+                in.setBody(body);
+                in.setHeader("foo", headerValue);
+            }
+        });
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        client.stop();
+        context.stop();
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message