http://git-wip-us.apache.org/repos/asf/cayenne/blob/5aedf54e/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessorTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessorTest.java b/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessorTest.java new file mode 100644 index 0000000..f21e0f3 --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessorTest.java @@ -0,0 +1,234 @@ +/***************************************************************** + * 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.cayenne.velocity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.sql.Types; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.cayenne.CayenneDataObject; +import org.apache.cayenne.DataObject; +import org.apache.cayenne.ObjectId; +import org.apache.cayenne.access.jdbc.ParameterBinding; +import org.apache.cayenne.access.jdbc.SQLStatement; +import org.junit.Before; +import org.junit.Test; + +public class VelocitySQLTemplateProcessorTest { + + private VelocitySQLTemplateProcessor processor; + + @Before + public void before() { + processor = new VelocitySQLTemplateProcessor(); + } + + @Test + public void testProcessTemplateUnchanged1() throws Exception { + String sqlTemplate = "SELECT * FROM ME"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals(sqlTemplate, compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + } + + @Test + public void testProcessTemplateUnchanged2() throws Exception { + String sqlTemplate = "SELECT a.b as XYZ FROM $SYSTEM_TABLE"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals(sqlTemplate, compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + } + + @Test + public void testProcessTemplateSimpleDynamicContent() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE $a"; + + Map<String, Object> map = Collections.<String, Object> singletonMap("a", "VALUE_OF_A"); + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE VALUE_OF_A", compiled.getSql()); + + // bindings are not populated, since no "bind" macro is used. + assertEquals(0, compiled.getBindings().length); + } + + @Test + public void testProcessTemplateBind() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE " + + "COLUMN1 = #bind($a 'VARCHAR') AND COLUMN2 = #bind($b 'INTEGER')"; + Map<String, Object> map = Collections.<String, Object> singletonMap("a", "VALUE_OF_A"); + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN1 = ? AND COLUMN2 = ?", compiled.getSql()); + assertEquals(2, compiled.getBindings().length); + assertBindingValue("VALUE_OF_A", compiled.getBindings()[0]); + assertBindingValue(null, compiled.getBindings()[1]); + } + + @Test + public void testProcessTemplateBindGuessVarchar() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($a)"; + Map<String, Object> map = Collections.<String, Object> singletonMap("a", "VALUE_OF_A"); + + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals(1, compiled.getBindings().length); + assertBindingType(Types.VARCHAR, compiled.getBindings()[0]); + } + + @Test + public void testProcessTemplateBindGuessInteger() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($a)"; + Map<String, Object> map = Collections.<String, Object> singletonMap("a", 4); + + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals(1, compiled.getBindings().length); + assertBindingType(Types.INTEGER, compiled.getBindings()[0]); + } + + @Test + public void testProcessTemplateBindEqual() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE COLUMN #bindEqual($a 'VARCHAR')"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT * FROM ME WHERE COLUMN IS NULL", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + + Map<String, Object> map = Collections.<String, Object> singletonMap("a", "VALUE_OF_A"); + + compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN = ?", compiled.getSql()); + assertEquals(1, compiled.getBindings().length); + assertBindingValue("VALUE_OF_A", compiled.getBindings()[0]); + } + + @Test + public void testProcessTemplateBindNotEqual() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE COLUMN #bindNotEqual($a 'VARCHAR')"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT * FROM ME WHERE COLUMN IS NOT NULL", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + + Map<String, Object> map = Collections.<String, Object> singletonMap("a", "VALUE_OF_A"); + + compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN <> ?", compiled.getSql()); + assertEquals(1, compiled.getBindings().length); + assertBindingValue("VALUE_OF_A", compiled.getBindings()[0]); + } + + @Test + public void testProcessTemplateID() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($helper.cayenneExp($a, 'db:ID_COLUMN'))"; + + DataObject dataObject = new CayenneDataObject(); + dataObject.setObjectId(new ObjectId("T", "ID_COLUMN", 5)); + + Map<String, Object> map = Collections.<String, Object> singletonMap("a", dataObject); + + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN1 = ?", compiled.getSql()); + assertEquals(1, compiled.getBindings().length); + assertBindingValue(new Integer(5), compiled.getBindings()[0]); + } + + @Test + public void testProcessTemplateNotEqualID() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE " + + "COLUMN1 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN1')) " + + "AND COLUMN2 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN2'))"; + + Map<String, Object> idMap = new HashMap<String, Object>(); + idMap.put("ID_COLUMN1", new Integer(3)); + idMap.put("ID_COLUMN2", "aaa"); + ObjectId id = new ObjectId("T", idMap); + DataObject dataObject = new CayenneDataObject(); + dataObject.setObjectId(id); + + Map<String, Object> map = Collections.<String, Object> singletonMap("a", dataObject); + + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN1 <> ? AND COLUMN2 <> ?", compiled.getSql()); + assertEquals(2, compiled.getBindings().length); + assertBindingValue(new Integer(3), compiled.getBindings()[0]); + assertBindingValue("aaa", compiled.getBindings()[1]); + } + + @Test + public void testProcessTemplateConditions() throws Exception { + String sqlTemplate = "SELECT * FROM ME #if($a) WHERE COLUMN1 > #bind($a)#end"; + + Map<String, Object> map = Collections.<String, Object> singletonMap("a", "VALUE_OF_A"); + + SQLStatement compiled = processor.processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN1 > ?", compiled.getSql()); + assertEquals(1, compiled.getBindings().length); + assertBindingValue("VALUE_OF_A", compiled.getBindings()[0]); + + compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT * FROM ME ", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + } + + @Test + public void testProcessTemplateBindCollection() throws Exception { + String sqlTemplate = "SELECT * FROM ME WHERE COLUMN IN (#bind($list 'VARCHAR'))"; + + Map<String, Object> map = Collections.<String, Object> singletonMap("list", Arrays.asList("a", "b", "c")); + SQLStatement compiled = new VelocitySQLTemplateProcessor().processTemplate(sqlTemplate, map); + + assertEquals("SELECT * FROM ME WHERE COLUMN IN (?,?,?)", compiled.getSql()); + assertEquals(3, compiled.getBindings().length); + + compiled = processor.processTemplate(sqlTemplate, map); + assertBindingValue("a", compiled.getBindings()[0]); + assertBindingValue("b", compiled.getBindings()[1]); + assertBindingValue("c", compiled.getBindings()[2]); + } + + private void assertBindingValue(Object expectedValue, Object binding) { + assertTrue("Not a binding!", binding instanceof ParameterBinding); + assertEquals(expectedValue, ((ParameterBinding) binding).getValue()); + } + + private void assertBindingType(int expectedType, Object binding) { + assertTrue("Not a binding!", binding instanceof ParameterBinding); + assertEquals(expectedType, ((ParameterBinding) binding).getJdbcType()); + } +}
http://git-wip-us.apache.org/repos/asf/cayenne/blob/5aedf54e/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_ChainTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_ChainTest.java b/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_ChainTest.java new file mode 100644 index 0000000..6bbc271 --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_ChainTest.java @@ -0,0 +1,184 @@ +/***************************************************************** + * 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.cayenne.velocity; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.cayenne.access.jdbc.SQLStatement; +import org.junit.Before; +import org.junit.Test; + +public class VelocitySQLTemplateProcessor_ChainTest { + + private VelocitySQLTemplateProcessor processor; + + @Before + public void before() { + processor = new VelocitySQLTemplateProcessor(); + } + + @Test + public void testProcessTemplateNoChunks() throws Exception { + // whatever is inside the chain, it should render as empty if there + // is no chunks... + + SQLStatement compiled = processor.processTemplate("#chain(' AND ') #end", + Collections.<String, Object> emptyMap()); + assertEquals("", compiled.getSql()); + + compiled = processor.processTemplate("#chain(' AND ') garbage #end", Collections.<String, Object> emptyMap()); + assertEquals("", compiled.getSql()); + + compiled = processor.processTemplate("#chain(' AND ' 'PREFIX') #end", Collections.<String, Object> emptyMap()); + + assertEquals("", compiled.getSql()); + compiled = processor.processTemplate("#chain(' AND ' 'PREFIX') garbage #end", + Collections.<String, Object> emptyMap()); + + assertEquals("", compiled.getSql()); + } + + @Test + public void testProcessTemplateFullChain() throws Exception { + String template = "#chain(' OR ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", "[A]"); + map.put("b", "[B]"); + map.put("c", "[C]"); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("[A] OR [B] OR [C]", compiled.getSql()); + } + + @Test + public void testProcessTemplateFullChainAndPrefix() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", "[A]"); + map.put("b", "[B]"); + map.put("c", "[C]"); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE [A] OR [B] OR [C]", compiled.getSql()); + } + + @Test + public void testProcessTemplatePartialChainMiddle() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", "[A]"); + map.put("c", "[C]"); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE [A] OR [C]", compiled.getSql()); + } + + @Test + public void testProcessTemplatePartialChainStart() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("b", "[B]"); + map.put("c", "[C]"); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE [B] OR [C]", compiled.getSql()); + } + + @Test + public void testProcessTemplatePartialChainEnd() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", "[A]"); + map.put("b", "[B]"); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE [A] OR [B]", compiled.getSql()); + } + + @Test + public void testProcessTemplateChainWithGarbage() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + " some other stuff" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", "[A]"); + map.put("c", "[C]"); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE [A] some other stuff OR [C]", compiled.getSql()); + } + + @Test + public void testProcessTemplateChainUnconditionalChunks() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk()C1#end" + "#chunk()C2#end" + "#chunk()C3#end" + "#end"; + + SQLStatement compiled = processor.processTemplate(template, Collections.<String, Object> emptyMap()); + assertEquals("WHERE C1 OR C2 OR C3", compiled.getSql()); + } + + @Test + public void testProcessTemplateEmptyChain() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + + "#end"; + + SQLStatement compiled = processor.processTemplate(template, Collections.<String, Object> emptyMap()); + assertEquals("", compiled.getSql()); + } + + @Test + public void testProcessTemplateWithFalseOrZero1() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)[A]#end" + "#chunk($b)[B]#end" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", false); + map.put("b", 0); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE [A] OR [B]", compiled.getSql()); + } + + @Test + public void testProcessTemplateWithFalseOrZero2() throws Exception { + String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + + "#end"; + + Map<String, Object> map = new HashMap<String, Object>(); + map.put("a", false); + map.put("b", 0); + + SQLStatement compiled = processor.processTemplate(template, map); + assertEquals("WHERE false OR 0", compiled.getSql()); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/5aedf54e/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_SelectTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_SelectTest.java b/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_SelectTest.java new file mode 100644 index 0000000..74a4d22 --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/velocity/VelocitySQLTemplateProcessor_SelectTest.java @@ -0,0 +1,109 @@ +/***************************************************************** + * 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.cayenne.velocity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Collections; + +import org.apache.cayenne.access.jdbc.ColumnDescriptor; +import org.apache.cayenne.access.jdbc.SQLStatement; +import org.junit.Before; +import org.junit.Test; + +public class VelocitySQLTemplateProcessor_SelectTest { + + private VelocitySQLTemplateProcessor processor; + + @Before + public void before() { + processor = new VelocitySQLTemplateProcessor(); + } + + @Test + public void testProcessTemplateUnchanged() throws Exception { + String sqlTemplate = "SELECT * FROM ME"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals(sqlTemplate, compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + assertEquals(0, compiled.getResultColumns().length); + } + + @Test + public void testProcessSelectTemplate1() throws Exception { + String sqlTemplate = "SELECT #result('A') FROM ME"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT A FROM ME", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + assertEquals(1, compiled.getResultColumns().length); + assertEquals("A", compiled.getResultColumns()[0].getName()); + assertNull(compiled.getResultColumns()[0].getJavaClass()); + } + + @Test + public void testProcessSelectTemplate2() throws Exception { + String sqlTemplate = "SELECT #result('A' 'String') FROM ME"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT A FROM ME", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + + assertEquals(1, compiled.getResultColumns().length); + assertEquals("A", compiled.getResultColumns()[0].getName()); + assertEquals("java.lang.String", compiled.getResultColumns()[0].getJavaClass()); + } + + @Test + public void testProcessSelectTemplate3() throws Exception { + String sqlTemplate = "SELECT #result('A' 'String' 'B') FROM ME"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT A AS B FROM ME", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + + assertEquals(1, compiled.getResultColumns().length); + ColumnDescriptor column = compiled.getResultColumns()[0]; + assertEquals("A", column.getName()); + assertEquals("B", column.getDataRowKey()); + assertEquals("java.lang.String", column.getJavaClass()); + } + + @Test + public void testProcessSelectTemplate4() throws Exception { + String sqlTemplate = "SELECT #result('A'), #result('B'), #result('C') FROM ME"; + + SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object> emptyMap()); + + assertEquals("SELECT A, B, C FROM ME", compiled.getSql()); + assertEquals(0, compiled.getBindings().length); + + assertEquals(3, compiled.getResultColumns().length); + assertEquals("A", compiled.getResultColumns()[0].getName()); + assertEquals("B", compiled.getResultColumns()[1].getName()); + assertEquals("C", compiled.getResultColumns()[2].getName()); + } +}
