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 2019/06/25 12:18:52 UTC
[camel] branch master updated: Fixed bug in property binding
support with #class: as prefix
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 1438b50 Fixed bug in property binding support with #class: as prefix
1438b50 is described below
commit 1438b5033902c4d5456a3e72a6de3786f45b3490
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jun 25 14:18:34 2019 +0200
Fixed bug in property binding support with #class: as prefix
---
.../main/java/org/apache/camel/CamelContext.java | 5 +++
.../camel/support/PropertyBindingSupportTest.java | 45 ++++++++++++++++++++++
.../camel/support/PropertyBindingSupport.java | 5 ++-
3 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
index 325cd06..8a84349 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
@@ -701,6 +701,11 @@ public interface CamelContext extends StatefulService, RuntimeConfiguration {
Injector getInjector();
/**
+ * Sets the injector to use
+ */
+ void setInjector(Injector injector);
+
+ /**
* Returns the lifecycle strategies used to handle lifecycle notifications
*
* @return the lifecycle strategies
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 f92edb0..ba1b5f4 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,7 +22,9 @@ import java.util.Properties;
import org.apache.camel.CamelContext;
import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.PropertyBindingException;
+import org.apache.camel.spi.Injector;
import org.junit.Test;
/**
@@ -222,6 +224,49 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
}
}
+ @Test
+ public void testDoesNotExistClass() throws Exception {
+ Foo foo = new Foo();
+
+ PropertyBindingSupport.bindProperty(context, foo, "name", "James");
+ try {
+ PropertyBindingSupport.bindProperty(context, foo, "bar.work", "#class:org.apache.camel.support.DoesNotExist");
+ fail("Should throw exception");
+ } catch (PropertyBindingException e) {
+ assertIsInstanceOf(ClassNotFoundException.class, e.getCause());
+ }
+ }
+
+ @Test
+ public void testNullInjectorClass() throws Exception {
+ Foo foo = new Foo();
+
+ context.setInjector(new Injector() {
+ @Override
+ public <T> T newInstance(Class<T> type) {
+ return null;
+ }
+
+ @Override
+ public <T> T newInstance(Class<T> type, boolean postProcessBean) {
+ return null;
+ }
+
+ @Override
+ public boolean supportsAutoWiring() {
+ return false;
+ }
+ });
+
+ PropertyBindingSupport.bindProperty(context, foo, "name", "James");
+ try {
+ PropertyBindingSupport.bindProperty(context, foo, "bar.work", "#class:org.apache.camel.support.Company");
+ fail("Should throw exception");
+ } catch (PropertyBindingException e) {
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ }
+ }
+
public static class Foo {
private String name;
private Bar bar = new Bar();
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 fd20f95..40752a0 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
@@ -489,10 +489,13 @@ public final class PropertyBindingSupport {
if (reference && value instanceof String) {
if (value.toString().startsWith("#class:")) {
// its a new class to be created
- String className = value.toString().substring(6);
+ String className = value.toString().substring(7);
Class<?> type = context.getClassResolver().resolveMandatoryClass(className);
if (type != null) {
value = context.getInjector().newInstance(type);
+ if (value == null) {
+ throw new IllegalArgumentException("Cannot create instance of class: " + className);
+ }
}
} else if (value.toString().startsWith("#type:")) {
// its reference by type, so lookup the actual value and use it if there is only one instance in the registry