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/07/07 14:52:11 UTC
[camel] 01/07: CAMEL-13732: Converting to boolean should always be
strict
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
commit 39c7aaf9027eff4f687645a3d31f413d23bf75c1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Jul 7 12:19:12 2019 +0200
CAMEL-13732: Converting to boolean should always be strict
---
.../sql/stored/ProducerBatchInvalidTest.java | 75 ++++++++++++++++++++++
.../apache/camel/converter/ObjectConverter.java | 5 +-
.../camel/converter/ObjectConverterTest.java | 22 ++++++-
3 files changed, 98 insertions(+), 4 deletions(-)
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchInvalidTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchInvalidTest.java
new file mode 100644
index 0000000..3926b71
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchInvalidTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.stored;
+
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.PropertyBindingException;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+public class ProducerBatchInvalidTest extends CamelTestSupport {
+
+ private EmbeddedDatabase db;
+
+ @Before
+ public void setUp() throws Exception {
+ db = new EmbeddedDatabaseBuilder()
+ .setType(EmbeddedDatabaseType.DERBY).addScript("sql/storedProcedureTest.sql").build();
+ super.setUp();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ db.shutdown();
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ @Test
+ public void shouldInvalid() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // required for the sql component
+ getContext().getComponent("sql-stored", SqlStoredComponent.class).setDataSource(db);
+
+ // batch option has been configured twice
+ from("direct:query").to("sql-stored:BATCHFN(INTEGER :#num)?batch=true&batch=true").to("mock:query");
+ }
+ });
+ try {
+ context.start();
+ fail("Should throw exception");
+ } catch (FailedToCreateRouteException e) {
+ ResolveEndpointFailedException refe = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+ PropertyBindingException pbe = assertIsInstanceOf(PropertyBindingException.class, refe.getCause());
+ assertEquals("batch", pbe.getPropertyName());
+ }
+ }
+
+}
diff --git a/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java b/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
index a1c63a2..6945f49 100644
--- a/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
+++ b/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
@@ -44,7 +44,10 @@ public final class ObjectConverter {
@Converter
public static boolean toBool(Object value) {
Boolean answer = toBoolean(value);
- return answer != null && answer;
+ if (answer == null) {
+ throw new IllegalArgumentException("Cannot convert type: " + value.getClass().getName() + " to boolean");
+ }
+ return answer;
}
/**
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
index e969278..b533f31 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
@@ -159,9 +159,25 @@ public class ObjectConverterTest extends Assert {
assertTrue(ObjectConverter.toBool("TRUE"));
assertFalse(ObjectConverter.toBool("false"));
assertFalse(ObjectConverter.toBool("FALSE"));
- assertFalse(ObjectConverter.toBool("1"));
- assertFalse(ObjectConverter.toBool(""));
- assertFalse(ObjectConverter.toBool("yes"));
+ // primitive boolean is more strict
+ try {
+ ObjectConverter.toBool("1");
+ fail("Should throw exception");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ ObjectConverter.toBool("");
+ fail("Should throw exception");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ ObjectConverter.toBool("yes");
+ fail("Should throw exception");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
}
}