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 2021/08/13 14:49:25 UTC

[camel] branch main updated: CAMEL-16864: camel-core - Endpoints that are mis configured in options that are supposed to be a registry lookup should fail when using generated configurers.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 45d8dad  CAMEL-16864: camel-core - Endpoints that are mis configured in options that are supposed to be a registry lookup should fail when using generated configurers.
45d8dad is described below

commit 45d8dadce2bf9bc4b557e212fce3e3910cd1bf03
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Aug 13 16:46:08 2021 +0200

    CAMEL-16864: camel-core - Endpoints that are mis configured in options that are supposed to be a registry lookup should fail when using generated configurers.
---
 .../sql/SqlEndpointMisconfigureDataSourceTest.java | 90 ++++++++++++++++++++++
 .../component/PropertyConfigurerSupport.java       | 12 ++-
 2 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlEndpointMisconfigureDataSourceTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlEndpointMisconfigureDataSourceTest.java
new file mode 100644
index 0000000..6997f6a
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlEndpointMisconfigureDataSourceTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.sql;
+
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.PropertyBindingException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+public class SqlEndpointMisconfigureDataSourceTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+
+    @Test
+    public void testFail() throws Exception {
+        context.getRegistry().bind("myDataSource", db);
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    from("direct:start")
+                            .to("sql:foo?dataSource=myDataSource")
+                            .to("mock:result");
+                }
+            });
+            fail("Should throw exception");
+        } catch (FailedToCreateRouteException e) {
+            PropertyBindingException pbe = (PropertyBindingException) e.getCause().getCause();
+            assertEquals("dataSource", pbe.getPropertyName());
+            assertEquals("myDataSource", pbe.getValue());
+        }
+    }
+
+    @Test
+    public void testOk() throws Exception {
+        context.getRegistry().bind("myDataSource", db);
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("sql:foo?dataSource=#myDataSource")
+                        .to("mock:result");
+            }
+        });
+        context.start();
+    }
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        db = new EmbeddedDatabaseBuilder()
+                .setName(getClass().getSimpleName())
+                .setType(EmbeddedDatabaseType.DERBY)
+                .addScript("sql/createAndPopulateDatabase.sql").build();
+
+        super.setUp();
+    }
+
+    @Override
+    @AfterEach
+    public void tearDown() throws Exception {
+        super.tearDown();
+
+        db.shutdown();
+    }
+
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java
index 9437d16..d5db368 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/PropertyConfigurerSupport.java
@@ -20,6 +20,8 @@ import java.util.List;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.support.EndpointHelper;
 import org.apache.camel.util.TimeUtils;
 
@@ -88,7 +90,15 @@ public abstract class PropertyConfigurerSupport {
             }
         }
 
-        return camelContext.getTypeConverter().convertTo(type, value);
+        if (value != null) {
+            try {
+                return camelContext.getTypeConverter().mandatoryConvertTo(type, value);
+            } catch (NoTypeConversionAvailableException e) {
+                throw RuntimeCamelException.wrapRuntimeCamelException(e);
+            }
+        } else {
+            return null;
+        }
     }
 
 }