camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/3] camel git commit: CAMEL-4725: Batch support and other options for Producer.
Date Thu, 14 Jan 2016 16:41:39 GMT
Repository: camel
Updated Branches:
  refs/heads/master f5116063b -> df5944fad


http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
index ac138ad..617d24a 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
@@ -66,14 +66,15 @@ InputParameter InputParameter() :
 {
      String sqlTypeAsStr;
      String name;
-     String valueSrcAsStr;
+     Token valueSrcToken;
 }
 {
-    (sqlTypeAsStr = ParameterSqlType() " " valueSrcAsStr =
+    (sqlTypeAsStr = ParameterSqlType() " " valueSrcToken =
     InputParameterSrc())
     {
         int sqlType = ParseHelper.parseSqlType(sqlTypeAsStr);
-        return new InputParameter(createNextParameterName(),sqlType,valueSrcAsStr,ParseHelper.sqlTypeToJavaType(sqlType,sqlTypeAsStr));
+        return new InputParameter(createNextParameterName(),sqlType,valueSrcToken,ParseHelper.sqlTypeToJavaType(sqlType,
+        sqlTypeAsStr));
     }
 }
 
@@ -112,26 +113,20 @@ String OutHeader():
         return token.toString();
     }
 }
-
-String InputParameterSrc():
+Token InputParameterSrc():
 {
-    String ret;
+    Token ret = null;
 }
 {
-    (ret = SimpleExpression())
+    (ret = <SIMPLE_EXP_TOKEN>)
     {
         return ret;
     }
-}
+    |
 
-String SimpleExpression() :
-{
- Token t = null;
-}
-{
-    (t = <SIMPLE_EXP_TOKEN>)
+    (ret = <PARAMETER_POS_TOKEN>)
     {
-    return t.toString();
+            return ret;
     }
 }
 
@@ -143,12 +138,20 @@ TOKEN: {
     <#LETTER: (["a"-"z","A"-"Z"])>
 }
 
+TOKEN: {
+    <#SPECIAL: (["#","_","-","'",".","$","{","}","\""])>
+}
+
+TOKEN : {
+    <SIMPLE_EXP_TOKEN: "${"(<LETTER>|<DIGIT> | <SPECIAL> | " ")+ "}">
+}
+
 TOKEN : {
-    <SIMPLE_EXP_TOKEN: "${"(<LETTER>|<DIGIT> | " " | "'" | "." )* "}">
+    <PARAMETER_POS_TOKEN: ":#"(<LETTER>|<DIGIT>|<SPECIAL> )+>
 }
 
 
 TOKEN : {
-    <IDENTIFIER: <LETTER>( <LETTER> | <DIGIT> | ".") *>
+    <IDENTIFIER: ( <LETTER> | <DIGIT> | <SPECIAL> )+ >
 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
new file mode 100644
index 0000000..33a95d5
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/CallableStatementWrapperTest.java
@@ -0,0 +1,99 @@
+/**
+ * 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 java.sql.SQLException;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.sql.stored.template.TemplateParser;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.dao.DataAccessException;
+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;
+
+public class CallableStatementWrapperTest extends CamelTestSupport {
+
+    private TemplateParser templateParser;
+    private EmbeddedDatabase db;
+    private JdbcTemplate jdbcTemplate;
+    private CallableStatementWrapperFactory factory;
+
+    @Before
+    public void setUp() throws Exception {
+        db = new EmbeddedDatabaseBuilder()
+                .setType(EmbeddedDatabaseType.DERBY).addScript("sql/storedProcedureTest.sql").build();
+        jdbcTemplate = new JdbcTemplate(db);
+        templateParser = new TemplateParser();
+        this.factory = new CallableStatementWrapperFactory(jdbcTemplate, templateParser);
+        super.setUp();
+    }
+
+
+    @Test
+    public void shouldExecuteStoredProcedure() throws Exception {
+
+        CallableStatementWrapper wrapper = new CallableStatementWrapper("SUBNUMBERS"
+                + "(INTEGER ${header.v1},INTEGER ${header.v2},OUT INTEGER resultofsub)",
factory);
+
+        final Exchange exchange = createExchangeWithBody(null);
+        exchange.getIn().setHeader("v1", 1);
+        exchange.getIn().setHeader("v2", 2);
+
+        wrapper.call(new WrapperExecuteCallback() {
+            @Override
+            public void execute(StamentWrapper stamentWrapper) throws SQLException, DataAccessException
{
+                stamentWrapper.populateStatement(null, exchange);
+
+                Map resultOfQuery = (Map) stamentWrapper.executeStatement();
+                Assert.assertEquals(Integer.valueOf(-1), ((Map) resultOfQuery).get("resultofsub"));
+            }
+        });
+    }
+
+    @Test
+    public void shouldExecuteNilacidProcedure() throws Exception {
+        CallableStatementWrapper wrapper = new CallableStatementWrapper("NILADIC()", factory);
+
+
+        wrapper.call(new WrapperExecuteCallback() {
+            @Override
+            public void execute(StamentWrapper stamentWrapper) throws SQLException, DataAccessException
{
+
+                stamentWrapper.populateStatement(null, null);
+                Map result = (Map) stamentWrapper.executeStatement();
+                //no output parameter in stored procedure NILADIC()
+                //Spring sets #update-count-1
+                assertNotNull(result.get("#update-count-1"));
+
+            }
+        });
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+        db.shutdown();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
index 36ccc76..ffd92fb 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
@@ -18,9 +18,11 @@ package org.apache.camel.component.sql.stored;
 
 import java.math.BigInteger;
 import java.sql.Types;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.component.sql.stored.template.TemplateStoredProcedureFactory;
+import org.apache.camel.component.sql.stored.template.TemplateParser;
 import org.apache.camel.component.sql.stored.template.ast.InputParameter;
 import org.apache.camel.component.sql.stored.template.ast.OutParameter;
 import org.apache.camel.component.sql.stored.template.ast.ParseRuntimeException;
@@ -31,7 +33,7 @@ import org.junit.Test;
 
 public class ParserTest extends CamelTestSupport {
 
-    TemplateStoredProcedureFactory parser = new TemplateStoredProcedureFactory(null);
+    TemplateParser parser = new TemplateParser();
 
     @Test
     public void shouldParseOk() {
@@ -39,29 +41,29 @@ public class ParserTest extends CamelTestSupport {
                 + "VARCHAR ${property.property1},BIGINT ${header.header2},OUT INTEGER header1)");
 
         Assert.assertEquals("addnumbers", template.getProcedureName());
-        Assert.assertEquals(3, template.getInputParameterList().size());
+        Assert.assertEquals(4, template.getParameterList().size());
 
         Exchange exchange = createExchangeWithBody(null);
         exchange.getIn().setHeader("header1", 1);
         exchange.setProperty("property1", "constant string");
         exchange.getIn().setHeader("header2", BigInteger.valueOf(2));
 
-        InputParameter param1 = template.getInputParameterList().get(0);
+        InputParameter param1 = (InputParameter) template.getParameterList().get(0);
         Assert.assertEquals("_0", param1.getName());
         Assert.assertEquals(Types.INTEGER, param1.getSqlType());
-        Assert.assertEquals(Integer.valueOf(1), param1.getValueExpression().evaluate(exchange,
Integer.class));
+        Assert.assertEquals(Integer.valueOf(1), param1.getValueExtractor().eval(exchange,
null));
 
-        InputParameter param2 = template.getInputParameterList().get(1);
+        InputParameter param2 = (InputParameter) template.getParameterList().get(1);
         Assert.assertEquals("_1", param2.getName());
         Assert.assertEquals(Types.VARCHAR, param2.getSqlType());
-        Assert.assertEquals("constant string", param2.getValueExpression().evaluate(exchange,
String.class));
+        Assert.assertEquals("constant string", param2.getValueExtractor().eval(exchange,
null));
 
-        InputParameter param3 = template.getInputParameterList().get(2);
+        InputParameter param3 = (InputParameter) template.getParameterList().get(2);
         Assert.assertEquals("_2", param3.getName());
         Assert.assertEquals(Types.BIGINT, param3.getSqlType());
-        Assert.assertEquals(BigInteger.valueOf(2), param3.getValueExpression().evaluate(exchange,
BigInteger.class));
+        Assert.assertEquals(2L, param3.getValueExtractor().eval(exchange, null));
 
-        OutParameter sptpOutputNode = template.getOutParameterList().get(0);
+        OutParameter sptpOutputNode = (OutParameter) template.getParameterList().get(3);
         Assert.assertEquals("_3", sptpOutputNode.getName());
         Assert.assertEquals(Types.INTEGER, sptpOutputNode.getSqlType());
         Assert.assertEquals("header1", sptpOutputNode.getOutHeader());
@@ -80,6 +82,23 @@ public class ParserTest extends CamelTestSupport {
                 + "(XML VALUE1 ${header.v1},OUT INTEGER VALUE2 ${header.v2})");
     }
 
+    @Test
+    public void nestedSimpleExpression() {
+        Exchange exchange = createExchangeWithBody(1);
+        exchange.getIn().setHeader("body", "body");
+        Template template = parser.parseTemplate("ADDNUMBERS2(INTEGER ${${header.body}})");
+        assertEquals(1, ((InputParameter) template.getParameterList().get(0)).getValueExtractor().eval(exchange,
null));
+    }
+
+    @Test
+    public void nableIssueSyntax() {
+        Map params = new HashMap<>();
+        params.put("P_STR_IN", "a");
+        Template template = parser.parseTemplate("IBS.\"Z$IMS_INTERFACE_WS\".TEST_STR(VARCHAR
:#P_STR_IN,OUT VARCHAR P_STR_OUT)");
+        assertEquals("a", ((InputParameter) template.getParameterList().get(0)).getValueExtractor().eval(null,
params));
+        assertEquals("IBS.\"Z$IMS_INTERFACE_WS\".TEST_STR", template.getProcedureName());
+    }
+
     @Test(expected = ParseRuntimeException.class)
     public void unmappedTypeShouldFaild() {
         parser.parseTemplate("ADDNUMBERS2"

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchTest.java
new file mode 100644
index 0000000..8a36c5e
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerBatchTest.java
@@ -0,0 +1,95 @@
+/**
+ * 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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+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.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+
+public class ProducerBatchTest 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();
+    }
+
+    @Test
+    public void shouldExecuteBatch() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:query");
+        mock.expectedMessageCount(1);
+
+
+        List<Map> batchParams = new ArrayList<>();
+
+        Map<String, Object> batch1 = new HashMap<>();
+        batchParams.add(batch1);
+
+        batch1.put("num", "1");
+
+
+        Map<String, Object> batch2 = new HashMap<>();
+
+        batch2.put("num", "3");
+        batchParams.add(batch2);
+
+        final long batchfnCallsBefore = TestStoredProcedure.BATCHFN_CALL_COUNTER.get();
+
+        template.requestBody("direct:query", batchParams);
+
+        assertMockEndpointsSatisfied();
+
+        Exchange exchange = mock.getExchanges().get(0);
+
+        assertNotNull(exchange.getIn().getHeader(SqlStoredConstants.SQL_STORED_UPDATE_COUNT));
+        assertEquals(batchfnCallsBefore + 2, TestStoredProcedure.BATCHFN_CALL_COUNTER.get());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // required for the sql component
+                getContext().getComponent("sql-stored", SqlStoredComponent.class).setDataSource(db);
+
+                from("direct:query").to("sql-stored:BATCHFN(INTEGER :#num)?batch=true").to("mock:query");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerTest.java
index 19a8089..1e8bc35 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerTest.java
@@ -62,7 +62,8 @@ public class ProducerTest extends CamelTestSupport {
 
         Exchange exchange = mock.getExchanges().get(0);
 
-        assertEquals(Integer.valueOf(3), exchange.getIn().getHeader("resultofsum"));
+        assertEquals(Integer.valueOf(-1), exchange.getIn().getBody(Map.class).get("resultofsub"));
+        assertNotNull(exchange.getIn().getHeader(SqlStoredConstants.SQL_STORED_UPDATE_COUNT));
     }
 
     @Override
@@ -73,8 +74,8 @@ public class ProducerTest extends CamelTestSupport {
                 // required for the sql component
                 getContext().getComponent("sql-stored", SqlStoredComponent.class).setDataSource(db);
 
-                from("direct:query").to("sql-stored:ADDNUMBERS(INTEGER ${headers.num1},INTEGER
${headers"
-                        + ".num2},OUT INTEGER resultofsum)").to("mock:query");
+                from("direct:query").to("sql-stored:SUBNUMBERS(INTEGER ${headers.num1},INTEGER
${headers"
+                        + ".num2},OUT INTEGER resultofsub)").to("mock:query");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerUseMessageBodyForTemplateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerUseMessageBodyForTemplateTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerUseMessageBodyForTemplateTest.java
new file mode 100644
index 0000000..6a10010
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ProducerUseMessageBodyForTemplateTest.java
@@ -0,0 +1,85 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.sql.SqlConstants;
+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 ProducerUseMessageBodyForTemplateTest 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();
+    }
+
+    @Test
+    public void shouldUseMessageBodyAsQuery() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:query");
+        mock.expectedMessageCount(1);
+
+
+        Map<String, Object> batch1 = new HashMap<>();
+        batch1.put("num1", 3);
+        batch1.put("num2", 1);
+
+
+        template.requestBodyAndHeader("direct:query", "SUBNUMBERS(INTEGER :#num1,INTEGER
:#num2,OUT INTEGER resultofsum)", SqlStoredConstants.SQL_STORED_PARAMETERS, batch1);
+
+        assertMockEndpointsSatisfied();
+
+        Exchange exchange = mock.getExchanges().get(0);
+
+
+        assertEquals(Integer.valueOf(2), exchange.getIn().getBody(Map.class).get("resultofsum"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // required for the sql component
+                getContext().getComponent("sql-stored", SqlStoredComponent.class).setDataSource(db);
+
+                from("direct:query").to("sql-stored:query?useMessageBodyForTemplate=true").to("mock:query");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/SqlStoredDataSourceTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/SqlStoredDataSourceTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/SqlStoredDataSourceTest.java
new file mode 100644
index 0000000..6f9f479
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/SqlStoredDataSourceTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.After;
+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 SqlStoredDataSourceTest extends CamelTestSupport {
+
+    private EmbeddedDatabase db;
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+
+        // START SNIPPET: e2
+        // this is the database we create with some initial data for our unit test
+        db = new EmbeddedDatabaseBuilder()
+                .setType(EmbeddedDatabaseType.DERBY).addScript("sql/storedProcedureTest.sql").build();
+        // END SNIPPET: e2
+
+        jndi.bind("jdbc/myDataSource", db);
+
+        return jndi;
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+        db.shutdown();
+    }
+
+    @Test
+    public void shouldExecuteStoredProcedure() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:query");
+        mock.expectedMessageCount(1);
+
+        template.requestBody("direct:query", "");
+
+        assertMockEndpointsSatisfied();
+
+        Exchange exchange = mock.getExchanges().get(0);
+        assertNotNull(exchange.getIn().getHeader(SqlStoredConstants.SQL_STORED_UPDATE_COUNT));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // required for the sql component
+
+                from("direct:query").to("sql-stored:NILADIC()?dataSource=#jdbc/myDataSource").to("mock:query");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
new file mode 100644
index 0000000..4aee75c
--- /dev/null
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateCacheTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.component.sql.stored.template.TemplateParser;
+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;
+
+public class TemplateCacheTest 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();
+    }
+
+    @Test
+    public void shouldCacheTemplateFunctions() throws InterruptedException {
+        JdbcTemplate jdbcTemplate = new JdbcTemplate(db);
+        CallableStatementWrapperFactory fac = new CallableStatementWrapperFactory(jdbcTemplate,
new TemplateParser());
+
+        BatchCallableStatementCreatorFactory batchFactory1 = fac.getTemplateForBatch("FOO()");
+        BatchCallableStatementCreatorFactory batchFactory2 = fac.getTemplateForBatch("FOO()");
+        assertSame(batchFactory1, batchFactory2);
+
+        TemplateStoredProcedure templateStoredProcedure1 = fac.getTemplateStoredProcedure("FOO()");
+        TemplateStoredProcedure templateStoredProcedure2 = fac.getTemplateStoredProcedure("FOO()");
+        assertSame(templateStoredProcedure1, templateStoredProcedure2);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
deleted file mode 100644
index f538ce5..0000000
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/**
- * 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.Exchange;
-import org.apache.camel.component.sql.stored.template.TemplateStoredProcedure;
-import org.apache.camel.component.sql.stored.template.TemplateStoredProcedureFactory;
-import org.apache.camel.test.junit4.CamelTestSupport;
-import org.junit.After;
-import org.junit.Assert;
-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;
-
-public class TemplateStoredProcedureTest extends CamelTestSupport {
-
-    private TemplateStoredProcedureFactory parser;
-    private EmbeddedDatabase db;
-    private JdbcTemplate jdbcTemplate;
-
-    @Before
-    public void setUp() throws Exception {
-        db = new EmbeddedDatabaseBuilder()
-                .setType(EmbeddedDatabaseType.DERBY).addScript("sql/storedProcedureTest.sql").build();
-        jdbcTemplate = new JdbcTemplate(db);
-        parser = new TemplateStoredProcedureFactory(jdbcTemplate);
-        super.setUp();
-    }
-
-
-    @Test
-    public void shouldExecuteStoredProcedure() {
-        TemplateStoredProcedure sp = new TemplateStoredProcedure(jdbcTemplate, parser.parseTemplate("ADDNUMBERS"
-                + "(INTEGER ${header.v1},INTEGER ${header.v2},OUT INTEGER resultofsum)"));
-
-        Exchange exchange = createExchangeWithBody(null);
-        exchange.getIn().setHeader("v1", 1);
-        exchange.getIn().setHeader("v2", 2);
-
-        sp.execute(exchange);
-
-        Assert.assertEquals(Integer.valueOf(3), exchange.getOut().getHeader("resultofsum"));
-    }
-
-    @Test
-    public void shouldExecuteNilacidProcedure() {
-        TemplateStoredProcedure sp = new TemplateStoredProcedure(jdbcTemplate, parser.parseTemplate("NILADIC()"));
-
-        Exchange exchange = createExchangeWithBody(null);
-        exchange.getIn().setHeader("v1", 1);
-        exchange.getIn().setHeader("v2", 2);
-
-        sp.execute(exchange);
-    }
-
-    @After
-    public void tearDown() throws Exception {
-        super.tearDown();
-        db.shutdown();
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TestStoredProcedure.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TestStoredProcedure.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TestStoredProcedure.java
index 55e2624..fea0d11 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TestStoredProcedure.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TestStoredProcedure.java
@@ -16,21 +16,36 @@
  */
 package org.apache.camel.component.sql.stored;
 
+import java.util.concurrent.atomic.AtomicLong;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class TestStoredProcedure {
 
+    public static final AtomicLong BATCHFN_CALL_COUNTER = new AtomicLong(0);
+
     private static final Logger LOG = LoggerFactory.getLogger(TestStoredProcedure.class);
 
+
     private TestStoredProcedure() {
     }
 
-    public static void addnumbers(int val1, int val2, int[] ret) {
+    public static void subnumbers(int val1, int val2, int[] ret) {
         LOG.info("calling addnumbers:{} + {}", val1, val2);
-        ret[0] = val1 + val2;
+        ret[0] = val1 - val2;
     }
 
+
+    public static void batchfn(String val1) {
+        LOG.info("calling batchfn:{}", val1);
+        if (val1 == null) {
+            throw new IllegalArgumentException("Argument val1 is null!");
+        }
+        BATCHFN_CALL_COUNTER.incrementAndGet();
+    }
+
+
     public static void niladic() {
         LOG.info("nilacid called");
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/df5944fa/components/camel-sql/src/test/resources/sql/storedProcedureTest.sql
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/resources/sql/storedProcedureTest.sql b/components/camel-sql/src/test/resources/sql/storedProcedureTest.sql
index 807768e..108c2e1 100644
--- a/components/camel-sql/src/test/resources/sql/storedProcedureTest.sql
+++ b/components/camel-sql/src/test/resources/sql/storedProcedureTest.sql
@@ -15,14 +15,20 @@
 -- limitations under the License.
 -- ------------------------------------------------------------------------
 
-CREATE PROCEDURE ADDNUMBERS(VALUE1 INTEGER, VALUE2 INTEGER,OUT RESULT INTEGER)
+CREATE PROCEDURE SUBNUMBERS(VALUE1 INTEGER, VALUE2 INTEGER,OUT RESULT INTEGER)
  PARAMETER STYLE JAVA
  LANGUAGE JAVA
  EXTERNAL NAME
-'org.apache.camel.component.sql.stored.TestStoredProcedure.addnumbers';
+'org.apache.camel.component.sql.stored.TestStoredProcedure.subnumbers';
 
 CREATE PROCEDURE NILADIC()
  PARAMETER STYLE JAVA
  LANGUAGE JAVA
  EXTERNAL NAME
-'org.apache.camel.component.sql.stored.TestStoredProcedure.niladic';
\ No newline at end of file
+'org.apache.camel.component.sql.stored.TestStoredProcedure.niladic';
+
+CREATE PROCEDURE BATCHFN(VALUE1 CHAR(10))
+ PARAMETER STYLE JAVA
+ LANGUAGE JAVA
+ EXTERNAL NAME
+'org.apache.camel.component.sql.stored.TestStoredProcedure.batchfn';


Mime
View raw message