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 2017/10/03 10:21:24 UTC

[2/2] camel git commit: CAMEL-11866: File consumer should report exceptions from fileName expression such as a bean not found exception.

CAMEL-11866: File consumer should report exceptions from fileName expression such as a bean not found exception.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a51e93ef
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a51e93ef
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a51e93ef

Branch: refs/heads/camel-2.19.x
Commit: a51e93ef9856d8f104369316b6439c322e7ca2c1
Parents: 80544f4
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Oct 3 12:20:44 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Oct 3 12:21:15 2017 +0200

----------------------------------------------------------------------
 .../component/file/GenericFileConsumer.java     |   4 +
 ...onsumerFileExpressionThrowExceptionTest.java | 112 +++++++++++++++++++
 camel-core/src/test/resources/log4j2.properties |   4 +-
 3 files changed, 118 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a51e93ef/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index 9ee398c..868148c 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -31,6 +31,7 @@ import org.apache.camel.Processor;
 import org.apache.camel.ShutdownRunningTask;
 import org.apache.camel.impl.ScheduledBatchPollingConsumer;
 import org.apache.camel.util.CastUtils;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StopWatch;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.TimeUtils;
@@ -704,6 +705,9 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
             // create a dummy exchange as Exchange is needed for expression evaluation
             Exchange dummy = endpoint.createExchange();
             fileExpressionResult = endpoint.getFileName().evaluate(dummy, String.class);
+            if (dummy.getException() != null) {
+                throw ObjectHelper.wrapRuntimeCamelException(dummy.getException());
+            }
         }
         return fileExpressionResult;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a51e93ef/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileExpressionThrowExceptionTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileExpressionThrowExceptionTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileExpressionThrowExceptionTest.java
new file mode 100644
index 0000000..2ec7d8e
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileExpressionThrowExceptionTest.java
@@ -0,0 +1,112 @@
+/**
+ * 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.file;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.bean.MethodNotFoundException;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.spi.PollingConsumerPollStrategy;
+
+import static org.awaitility.Awaitility.await;
+
+/**
+ * Unit test for expression option for file consumer.
+ */
+public class FileConsumerFileExpressionThrowExceptionTest extends ContextTestSupport {
+
+    private static volatile String event = "";
+    private static volatile Exception rollbackCause;
+
+    private static final CountDownLatch LATCH = new CountDownLatch(1);
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/filelanguage");
+        super.setUp();
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("counter", new MyGuidGenerator());
+        jndi.bind("myPoll", new MyPollStrategy());
+        return jndi;
+    }
+
+    public void testConsumeExpressionThrowException() throws Exception {
+        template.sendBodyAndHeader("file://target/filelanguage/bean", "Bye World", Exchange.FILE_NAME, "123.txt");
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/filelanguage/bean/"
+                      + "?pollStrategy=#myPoll&initialDelay=0&delay=10&fileName=${bean:counter?method=doNotExistMethod}.txt&delete=true").to("mock:result");
+                    // specify a method name that does not exists
+            }
+        });
+
+        context.start();
+
+        await().atMost(2, TimeUnit.SECONDS).until(() -> LATCH.getCount() == 0);
+
+        // and we should rollback X number of times
+        assertTrue(event.startsWith("rollback"));
+
+        assertNotNull(rollbackCause);
+
+        MethodNotFoundException e = assertIsInstanceOf(MethodNotFoundException.class, rollbackCause.getCause());
+        assertNotNull(e);
+        assertEquals("doNotExistMethod", e.getMethodName());
+    }
+
+    public class MyGuidGenerator {
+        public String next() {
+            return "123";
+        }
+    }
+
+    private static class MyPollStrategy implements PollingConsumerPollStrategy {
+
+        public boolean begin(Consumer consumer, Endpoint endpoint) {
+            return true;
+        }
+
+        public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
+            event += "commit";
+        }
+
+        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception cause) throws Exception {
+            event += "rollback";
+            rollbackCause = cause;
+            LATCH.countDown();
+            return false;
+        }
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/a51e93ef/camel-core/src/test/resources/log4j2.properties
----------------------------------------------------------------------
diff --git a/camel-core/src/test/resources/log4j2.properties b/camel-core/src/test/resources/log4j2.properties
index 6481a17..8dc0756 100644
--- a/camel-core/src/test/resources/log4j2.properties
+++ b/camel-core/src/test/resources/log4j2.properties
@@ -43,5 +43,5 @@ rootLogger.level = INFO
 rootLogger.appenderRef.file.ref = file
 #rootLogger.appenderRef.file.ref = console
 
-#logger.camel-core.name = org.apache.camel.impl
-#logger.camel-core.level = DEBUG
+#logger.camel-core.name = org.apache.camel
+#logger.camel-core.level = TRACE