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 2023/08/28 08:04:59 UTC

[camel] 02/02: CAMEL-19792: camel-core - PropertyBinding - Constructor args for beans should be mandatory lookup

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

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

commit ed957ac83e40bc22028ebd4f37b12010549c2b3c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 28 10:04:45 2023 +0200

    CAMEL-19792: camel-core - PropertyBinding - Constructor args for beans should be mandatory lookup
---
 .../camel/support/PropertyBindingSupportTest.java  | 26 ++++++++++++++++++++++
 .../camel/support/PropertyBindingSupport.java      |  2 +-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index 1c7293db519..569c10cb938 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -22,9 +22,11 @@ import java.util.Properties;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.PropertyBindingException;
 import org.apache.camel.spi.Injector;
 import org.apache.camel.spi.PropertiesComponent;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.*;
@@ -466,6 +468,30 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
         assertFalse(foo.getAnimal().isDangerous());
     }
 
+    @Test
+    public void testNestedClassConstructorParameterMandatoryBean() throws Exception {
+        Foo foo = new Foo();
+
+        PropertyBindingSupport.build().bind(context, foo, "name", "James");
+        try {
+            PropertyBindingSupport.build().bind(context, foo, "animal",
+                    "#class:org.apache.camel.support.Animal('#bean:myName', false)");
+            fail("Should have thrown exception");
+        } catch (PropertyBindingException e) {
+            NoSuchBeanException nsb = assertIsInstanceOf(NoSuchBeanException.class, e.getCause());
+            assertEquals("myName", nsb.getName());
+        }
+
+        // add bean and try again
+        context.getRegistry().bind("myName", "Acme");
+        PropertyBindingSupport.build().bind(context, foo, "animal",
+                "#class:org.apache.camel.support.Animal('#bean:myName', false)");
+
+        assertEquals("James", foo.getName());
+        assertEquals("Acme", foo.getAnimal().getName());
+        assertFalse(foo.getAnimal().isDangerous());
+    }
+
     @Test
     public void testNestedClassFactoryParameterOneParameter() throws Exception {
         Foo foo = new Foo();
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 67d0a8179f4..222e3525e3c 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -1603,7 +1603,7 @@ public final class PropertyBindingSupport {
             }
         } else if (strval.startsWith("#bean:")) {
             String key = strval.substring(6);
-            answer = camelContext.getRegistry().lookupByName(key);
+            answer = CamelContextHelper.mandatoryLookup(camelContext, key);
         } else if (strval.startsWith("#valueAs(")) {
             String text = strval.substring(8);
             String typeName = StringHelper.between(text, "(", ")");