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/03/22 15:10:07 UTC
[camel] branch camel-3.20.x updated: CAMEL-19186: camel-core - Type function in simple language should use enum type instead of toString value
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.20.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.20.x by this push:
new aa6b9ecd2ff CAMEL-19186: camel-core - Type function in simple language should use enum type instead of toString value
aa6b9ecd2ff is described below
commit aa6b9ecd2ff07737aef2427ccae227bda29dc9b8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Mar 22 16:06:49 2023 +0100
CAMEL-19186: camel-core - Type function in simple language should use enum type instead of toString value
---
.../language/simple/SimpleExpressionBuilder.java | 24 +++++++++--
.../apache/camel/converter/EnumConverterTest.java | 47 +++++++++++++++++++++-
.../org/apache/camel/converter/MyTypeEnum.java | 34 ++++++++++++++++
.../apache/camel/language/simple/SimpleTest.java | 14 ++++++-
4 files changed, 113 insertions(+), 6 deletions(-)
diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
index 14a1139d588..0a400ed0709 100644
--- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
+++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
@@ -906,19 +906,37 @@ public final class SimpleExpressionBuilder {
String after = text.substring(pos + 1);
type = classResolver.resolveClass(before);
if (type != null) {
- return ObjectHelper.lookupConstantFieldValue(type, after);
+ // special for enum constants
+ if (type.isEnum()) {
+ Class<Enum<?>> enumClass = (Class<Enum<?>>) type;
+ for (Enum<?> enumValue : enumClass.getEnumConstants()) {
+ if (enumValue.name().equalsIgnoreCase(after)) {
+ return type.cast(enumValue);
+ }
+ }
+ throw CamelExecutionException.wrapCamelExecutionException(exchange,
+ new ClassNotFoundException("Cannot find enum: " + after + " on type: " + type));
+ } else {
+ // we assume it is a field constant
+ Object answer = ObjectHelper.lookupConstantFieldValue(type, after);
+ if (answer != null) {
+ return answer;
+ }
+ }
}
}
throw CamelExecutionException.wrapCamelExecutionException(exchange,
- new ClassNotFoundException("Cannot find type " + text));
+ new ClassNotFoundException("Cannot find type: " + text));
}
@Override
public String toString() {
return "type:" + name;
}
- };
+ }
+
+ ;
}
/**
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java
index e98ec8bda9a..a994ddaddce 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java
@@ -20,10 +20,13 @@ import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.TypeConversionException;
+import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.fail;
public class EnumConverterTest extends ContextTestSupport {
@@ -95,6 +98,48 @@ public class EnumConverterTest extends ContextTestSupport {
assertEquals(MyEnum.GET_USERS_BY_TOPIC, level);
}
+ @Test
+ public void testConvertEnumWithToStringOverride() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+
+ MyTypeEnum type = context.getTypeConverter().mandatoryConvertTo(MyTypeEnum.class, exchange, "TYPE1");
+ assertSame(MyTypeEnum.TYPE1, type);
+
+ type = context.getTypeConverter().mandatoryConvertTo(MyTypeEnum.class, exchange, "TYPE2");
+ assertSame(MyTypeEnum.TYPE2, type);
+
+ type = context.getTypeConverter().mandatoryConvertTo(MyTypeEnum.class, exchange, "TYPE3");
+ assertSame(MyTypeEnum.TYPE3, type);
+ }
+
+ @Test
+ public void testEnumWithToStringOverrideRouteSimple() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .choice()
+ .when(simple("${header.type} == ${type:org.apache.camel.converter.MyTypeEnum.TYPE1}")).to("mock:type1")
+ .when(simple("${header.type} == ${type:org.apache.camel.converter.MyTypeEnum.TYPE2}")).to("mock:type2")
+ .when(simple("${header.type} == ${type:org.apache.camel.converter.MyTypeEnum.TYPE3}")).to("mock:type3")
+ .otherwise().to("mock:other");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:type1").expectedBodiesReceived("C");
+ getMockEndpoint("mock:type2").expectedBodiesReceived("A");
+ getMockEndpoint("mock:type3").expectedBodiesReceived("B");
+
+ template.sendBodyAndHeader("direct:start", "A", "type", MyTypeEnum.TYPE2);
+ template.sendBodyAndHeader("direct:start", "B", "type", MyTypeEnum.TYPE3);
+ template.sendBodyAndHeader("direct:start", "C", "type", MyTypeEnum.TYPE1);
+
+ assertMockEndpointsSatisfied();
+
+ context.stop();
+ }
+
public enum MyEnum {
GET_USERS,
GET_USERS_BY_TOPIC
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/MyTypeEnum.java b/core/camel-core/src/test/java/org/apache/camel/converter/MyTypeEnum.java
new file mode 100644
index 00000000000..13f01cba81a
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/MyTypeEnum.java
@@ -0,0 +1,34 @@
+/*
+ * 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.converter;
+
+public enum MyTypeEnum {
+ TYPE1("This is type 1"),
+ TYPE2("This is type 2"),
+ TYPE3("This is type 3");
+
+ private final String description;
+
+ MyTypeEnum(String s) {
+ this.description = s;
+ }
+
+ @Override
+ public String toString() {
+ return description;
+ }
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 57fcbf88bf3..669425d0786 100644
--- a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1708,8 +1708,18 @@ public class SimpleTest extends LanguageTestSupport {
assertExpression("${type:org.apache.camel.ExchangePattern.InOut}", ExchangePattern.InOut);
// non existing fields
- assertExpression("${type:org.apache.camel.ExchangePattern.}", null);
- assertExpression("${type:org.apache.camel.ExchangePattern.UNKNOWN}", null);
+ try {
+ assertExpression("${type:org.apache.camel.ExchangePattern.}", null);
+ fail("Should throw exception");
+ } catch (Exception e) {
+ assertIsInstanceOf(ClassNotFoundException.class, e.getCause());
+ }
+ try {
+ assertExpression("${type:org.apache.camel.ExchangePattern.UNKNOWN}", null);
+ fail("Should throw exception");
+ } catch (Exception e) {
+ assertIsInstanceOf(ClassNotFoundException.class, e.getCause());
+ }
}
@Test