You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2008/02/15 13:17:17 UTC

svn commit: r628037 - in /activemq/camel/trunk: ./ camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/util/ components/camel-spring/ components/camel-spring/src/main/java/org/apache/camel/component/test/...

Author: jstrachan
Date: Fri Feb 15 04:17:16 2008
New Revision: 628037

URL: http://svn.apache.org/viewvc?rev=628037&view=rev
Log:
added support for the new test endpoint for http://issues.apache.org/activemq/browse/CAMEL-327

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java   (with props)
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestComponent.java   (with props)
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java   (with props)
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/package.html
      - copied, changed from r627805, activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/event/package.html
    activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/test
      - copied, changed from r627805, activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/validator
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java   (with props)
    activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/
    activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    activemq/camel/trunk/components/camel-spring/pom.xml
    activemq/camel/trunk/components/camel-spring/src/test/resources/log4j.properties
    activemq/camel/trunk/pom.xml

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?rev=628037&r1=628036&r2=628037&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java Fri Feb 15 04:17:16 2008
@@ -46,7 +46,7 @@
         this.endpoint = endpoint;
     }
 
-    protected void poll() throws Exception {
+    protected synchronized void poll() throws Exception {
         int rc = pollFileOrDirectory(endpoint.getFile(), isRecursive());
         if( rc == 0 && generateEmptyExchangeWhenIdle ) {
             final FileExchange exchange = endpoint.createExchange((File)null);

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java?rev=628037&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java Fri Feb 15 04:17:16 2008
@@ -0,0 +1,79 @@
+/**
+ *
+ * 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.util;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.PollingConsumer;
+import org.apache.camel.Processor;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Some helper methods for working with {@link Endpoint} instances
+ *
+ * @version $Revision: 1.1 $
+ */
+public class EndpointHelper {
+    private static final transient Log LOG = LogFactory.getLog(EndpointHelper.class);
+
+    /**
+     * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
+     * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
+     * down the consumer and throws any exceptions thrown.
+     *
+     * @param endpoint
+     * @param processor
+     */
+    public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
+        PollingConsumer consumer = endpoint.createPollingConsumer();
+        try {
+            consumer.start();
+
+            while (true) {
+                Exchange exchange = consumer.receive(timeout);
+                if (exchange == null) {
+                    break;
+                }
+                else {
+                    processor.process(exchange);
+                }
+            }
+        }
+        finally {
+            try {
+                consumer.stop();
+            }
+            catch (Exception e) {
+                LOG.warn("Failed to stop PollingConsumer: " + e, e);
+            }
+        }
+    }
+
+    /**
+     * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
+     * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
+     * down the consumer and throws any exceptions thrown.
+     *
+     * @param endpoint
+     * @param processor
+     */
+    public static void pollEndpoint(Endpoint endpoint, Processor processor) throws Exception {
+        pollEndpoint(endpoint, processor, 1000L);
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/components/camel-spring/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/pom.xml?rev=628037&r1=628036&r2=628037&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/pom.xml (original)
+++ activemq/camel/trunk/components/camel-spring/pom.xml Fri Feb 15 04:17:16 2008
@@ -65,6 +65,10 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-test</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
       <type>test-jar</type>

Added: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestComponent.java?rev=628037&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestComponent.java (added)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestComponent.java Fri Feb 15 04:17:16 2008
@@ -0,0 +1,50 @@
+/**
+ *
+ * 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.test;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultComponent;
+import org.apache.camel.util.CamelContextHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.UnsafeUriCharactersEncoder;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class TestComponent extends DefaultComponent<Exchange> {
+    public Endpoint<Exchange> createEndpoint(String uri) throws Exception {
+        // lets not use the normal parameter handling so that all parameters are sent to the nested endpoint
+
+        ObjectHelper.notNull(getCamelContext(), "camelContext");
+        URI u = new URI(UnsafeUriCharactersEncoder.encode(uri));
+        String path = u.getSchemeSpecificPart();
+
+        return createEndpoint(uri, path, new HashMap());
+    }
+
+    @Override
+    protected Endpoint<Exchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
+        Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(getCamelContext(), remaining);
+        return new TestEndpoint(uri, this, endpoint);
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestComponent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java?rev=628037&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java (added)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java Fri Feb 15 04:17:16 2008
@@ -0,0 +1,74 @@
+/**
+ *
+ * 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.test;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.Component;
+import org.apache.camel.Service;
+import org.apache.camel.Endpoint;
+import org.apache.camel.PollingConsumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Exchange;
+import org.apache.camel.util.EndpointHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * A <a href="http://activemq.apache.org/camel/test.html">Test Endpoint</a> is a
+ * <a href="http://activemq.apache.org/camel/mock.html">Mock Endpoint</a> for testing but it will
+ * pull all messages from the nested endpoint and use those as expected message body assertions.
+ * 
+ * @version $Revision: 1.1 $
+ */
+public class TestEndpoint extends MockEndpoint implements Service {
+    private static final transient Log LOG = LogFactory.getLog(TestEndpoint.class);
+    private final Endpoint expectedMessageEndpoint;
+    private long timeout = 2000L;
+
+    public TestEndpoint(String endpointUri, Component component, Endpoint expectedMessageEndpoint) {
+        super(endpointUri, component);
+        this.expectedMessageEndpoint = expectedMessageEndpoint;
+    }
+
+    public void start() throws Exception {
+        LOG.debug("Consuming expected messages from: " + expectedMessageEndpoint);
+        final List expectedBodies = new ArrayList();
+        EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                Object body = getInBody(exchange);
+                expectedBodies.add(body);
+            }
+        }, timeout);
+
+        LOG.debug("Received: " + expectedBodies.size() + " expected message(s) from: " + expectedMessageEndpoint);
+        expectedBodiesReceived(expectedBodies);
+    }
+
+    public void stop() throws Exception {
+    }
+
+    /**
+     * This method allows us to convert or cooerce the expected message body into some other type
+     */
+    protected Object getInBody(Exchange exchange) {
+        return exchange.getIn().getBody();
+    }
+}

Propchange: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/package.html (from r627805, activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/event/package.html)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/package.html?p2=activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/package.html&p1=activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/event/package.html&r1=627805&r2=628037&rev=628037&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/event/package.html (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/package.html Fri Feb 15 04:17:16 2008
@@ -19,7 +19,7 @@
 </head>
 <body>
 
-An <a href="http://activemq.apache.org/camel/event.html">Event Endpoint</a> for working with Spring ApplicationEvents
+A <a href="http://activemq.apache.org/camel/test.html">Test Endpoint</a> for Pattern Based Testing
 
 </body>
 </html>

Copied: activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/test (from r627805, activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/validator)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/test?p2=activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/test&p1=activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/validator&r1=627805&r2=628037&rev=628037&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/validator (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/component/test Fri Feb 15 04:17:16 2008
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-class=org.apache.camel.component.validator.ValidatorComponent
\ No newline at end of file
+class=org.apache.camel.component.test.TestComponent
\ No newline at end of file

Added: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java?rev=628037&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java Fri Feb 15 04:17:16 2008
@@ -0,0 +1,62 @@
+/**
+ *
+ * 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.test;
+
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.CamelContextHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+@ContextConfiguration
+public class TestEndpointTest extends AbstractJUnit38SpringContextTests {
+    private static final transient Log LOG = LogFactory.getLog(TestEndpointTest.class);
+
+    @Autowired
+    protected CamelContext camelContext;
+
+    @EndpointInject(uri = "test:file://src/test/data?noop=true")
+    protected TestEndpoint endpoint;
+
+    public void testMocksAreValid() throws Exception {
+        assertNotNull(camelContext);
+        assertNotNull(endpoint);
+
+        MockEndpoint.assertIsSatisfied(camelContext);
+
+        // lets show the endpoints in the test
+        List<MockEndpoint> list = CamelContextHelper.getSingletonEndpoints(camelContext, MockEndpoint.class);
+        LOG.debug("Found endpoints: " + list);
+
+        // lets dump the messages sent to our test endpoint
+        List<Exchange> exchanges = endpoint.getReceivedExchanges();
+        for (Exchange exchange : exchanges) {
+            LOG.debug("Received: " + exchange);
+        }
+    }
+}

Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/components/camel-spring/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/log4j.properties?rev=628037&r1=628036&r2=628037&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/log4j.properties (original)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/log4j.properties Fri Feb 15 04:17:16 2008
@@ -21,6 +21,9 @@
 log4j.rootLogger=INFO, out
 
 log4j.logger.org.apache.camel=WARN
+log4j.logger.org.apache.camel.component.test=DEBUG
+
+
 log4j.logger.org.springframework=WARN
 #log4j.logger.org.apache.camel=DEBUG
 

Added: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml?rev=628037&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml Fri Feb 15 04:17:16 2008
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+       http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
+    ">
+
+  <!-- START SNIPPET: example -->
+  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
+    <route>
+      <from uri="file://src/test/data?noop=true"/>
+        <to uri="test:file://src/test/data?noop=true"/>
+    </route>
+  </camelContext>
+  <!-- END SNIPPET: example -->
+
+</beans>

Propchange: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/pom.xml?rev=628037&r1=628036&r2=628037&view=diff
==============================================================================
--- activemq/camel/trunk/pom.xml (original)
+++ activemq/camel/trunk/pom.xml Fri Feb 15 04:17:16 2008
@@ -596,6 +596,12 @@
           </exclusion>
         </exclusions>
       </dependency>
+      <dependency>
+        <groupId>org.springframework</groupId>
+        <artifactId>spring-test</artifactId>
+        <version>${spring-version}</version>
+      </dependency>
+
 
       <!-- optional mina dependencies -->
       <dependency>