camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] git commit: CAMEL-6561: Fixed sql producer to support json like structure for # parameters when only 1 parameter in use.
Date Wed, 07 Aug 2013 10:05:23 GMT
CAMEL-6561: Fixed sql producer to support json like structure for # parameters when only 1
parameter in use.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/554e8e18
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/554e8e18
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/554e8e18

Branch: refs/heads/camel-2.11.x
Commit: 554e8e189a20afec3a781ce1757e1d82013d014b
Parents: 9639e77
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Aug 7 12:04:36 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Aug 7 12:04:58 2013 +0200

----------------------------------------------------------------------
 .../sql/DefaultSqlPrepareStatementStrategy.java | 22 ++++--
 .../component/sql/SqlProducerJSONTest.java      | 83 ++++++++++++++++++++
 .../camel-sql/src/test/resources/servlet.json   | 28 +++++++
 .../sql/createAndPopulateDatabase2.sql          | 23 ++++++
 4 files changed, 148 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/554e8e18/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
index f8719a2..ddb854a 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/DefaultSqlPrepareStatementStrategy.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.sql;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -125,15 +126,20 @@ public class DefaultSqlPrepareStatementStrategy implements SqlPrepareStatementSt
 
 
         } else {
-            // is the body a String
-            if (value instanceof String) {
-                // if the body is a String then honor quotes etc.
-                String[] tokens = StringQuoteHelper.splitSafeQuote((String)value, separator,
true);
-                List<String> list = Arrays.asList(tokens);
-                return list.iterator();
+            // if only 1 parameter and the body is a String then use body as is
+            if (expectedParams == 1 && value instanceof String) {
+                return Collections.singletonList(value).iterator();
             } else {
-                // just use a regular iterator
-                return exchange.getContext().getTypeConverter().convertTo(Iterator.class,
value);
+                // is the body a String
+                if (value instanceof String) {
+                    // if the body is a String then honor quotes etc.
+                    String[] tokens = StringQuoteHelper.splitSafeQuote((String)value, separator,
true);
+                    List<String> list = Arrays.asList(tokens);
+                    return list.iterator();
+                } else {
+                    // just use a regular iterator
+                    return exchange.getContext().getTypeConverter().convertTo(Iterator.class,
value);
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/554e8e18/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java
new file mode 100755
index 0000000..8c8ef3a
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlProducerJSONTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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 java.io.File;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+/**
+ * @version 
+ */
+public class SqlProducerJSONTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+    private JdbcTemplate jdbcTemplate;
+
+    @Before
+    public void setUp() throws Exception {
+        db = new EmbeddedDatabaseBuilder()
+            .setType(EmbeddedDatabaseType.DERBY).addScript("sql/createAndPopulateDatabase2.sql").build();
+
+        jdbcTemplate = new JdbcTemplate(db);
+
+        super.setUp();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+        
+        db.shutdown();
+    }
+
+    @Test
+    public void testJSON() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        String json = context.getTypeConverter().convertTo(String.class, new File("src/test/resources/servlet.json"));
+
+        template.sendBody("direct:start", json);
+
+        mock.assertIsSatisfied();
+
+        assertEquals(4, jdbcTemplate.queryForInt("select count(*) from projects"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                getContext().getComponent("sql", SqlComponent.class).setDataSource(db);
+
+                from("direct:start")
+                    .to("sql:insert into projects (id, project, license, description) values
(4, 'Food, Inc', 'ASF', #)")
+                    .to("mock:result");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/554e8e18/components/camel-sql/src/test/resources/servlet.json
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/resources/servlet.json b/components/camel-sql/src/test/resources/servlet.json
new file mode 100644
index 0000000..b07fba0
--- /dev/null
+++ b/components/camel-sql/src/test/resources/servlet.json
@@ -0,0 +1,28 @@
+{"web-app": {
+  "servlet": [
+    {
+      "servlet-name": "fooServlet",
+      "servlet-class": "com.foo.FooServlet",
+      "init-param": {
+        "configGlossary:staticPath": "/content/static",
+        "useJSP": false,
+        "cachePackageTagsTrack": 200,
+        "cachePackageTagsStore": 200,
+        "cachePackageTagsRefresh": 60,
+        "cacheTemplatesTrack": 100,
+        "cacheTemplatesStore": 50,
+        "cacheTemplatesRefresh": 15,
+        "cachePagesTrack": 200,
+        "cachePagesStore": 100,
+        "cachePagesRefresh": 10,
+        "cachePagesDirtyRead": 10,
+        "dataStoreLogLevel": "debug",
+        "maxUrlLength": 500
+      }
+    }
+  ],
+  "servlet-mapping": {
+    "fooSerlvet": "/"
+  }
+}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/554e8e18/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql b/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql
new file mode 100644
index 0000000..db01686
--- /dev/null
+++ b/components/camel-sql/src/test/resources/sql/createAndPopulateDatabase2.sql
@@ -0,0 +1,23 @@
+-- ------------------------------------------------------------------------
+-- 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.
+-- ------------------------------------------------------------------------
+
+-- START SNIPPET: e1
+create table projects (id integer primary key, project varchar(10), license varchar(5), description
varchar(1000) default null);
+insert into projects values (1, 'Camel', 'ASF', '');
+insert into projects values (2, 'AMQ', 'ASF', '');
+insert into projects values (3, 'Linux', 'XXX', '');
+-- END SNIPPET: e1
\ No newline at end of file


Mime
View raw message