]> source.dussan.org Git - iciql.git/commitdiff
Fix unit tests for column name escaping
authorJames Moger <james.moger@gitblit.com>
Wed, 22 Oct 2014 16:52:53 +0000 (12:52 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 23 Oct 2014 02:29:05 +0000 (22:29 -0400)
src/test/java/com/iciql/test/OneOfTest.java
src/test/java/com/iciql/test/RuntimeQueryTest.java
src/test/java/com/iciql/test/SamplesTest.java
src/test/java/com/iciql/test/StackableConditionsTest.java

index 2ca7cc74d4d3a99cfd435aa0d1f9dd2c945a6da8..e534262344b2221c4d28d7aaec0f629485f1f694 100644 (file)
@@ -1,11 +1,11 @@
 /*
  * Copyright (c) 2009-2014, Architector Inc., Japan
  * All rights reserved.
- * 
+ *
  * Licensed 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
 package com.iciql.test;
 
 import static org.junit.Assert.assertEquals;
+
 import java.util.ArrayList;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+
 import com.iciql.Db;
 import com.iciql.test.models.Customer;
 import com.iciql.test.models.PrimitivesModel;
@@ -43,65 +46,75 @@ public class OneOfTest {
        @SuppressWarnings("serial")
        @Test
        public void oneOfTest() {
+               String PrimitivesTest = db.getDialect().prepareTableName(null, "PrimitivesTest");
+               String Customer = db.getDialect().prepareTableName(null, "Customer");
+               String myInteger = db.getDialect().prepareColumnName("myInteger");
+               String customerId = db.getDialect().prepareColumnName("customerId");
+
                PrimitivesModel p = new PrimitivesModel();
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s IN(0)", PrimitivesTest, myInteger),
                                db.from(p)
                                                .where(p.myInteger).oneOf(0)
-                                               .toSQL(),
-                               "SELECT * FROM PrimitivesTest WHERE myInteger IN(0)");
+                                               .toSQL());
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s IN(0, 1)", PrimitivesTest, myInteger),
                                db.from(p)
                                                .where(p.myInteger).oneOf(0, 1)
-                                               .toSQL(),
-                               "SELECT * FROM PrimitivesTest WHERE myInteger IN(0, 1)");
+                                               .toSQL());
                Customer c = new Customer();
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s IN('a')", Customer, customerId),
                                db.from(c)
                                                .where(c.customerId).oneOf(new ArrayList<String>() {{
                                                        this.add("a");
                                                }})
-                                               .toSQL(),
-                               "SELECT * FROM Customer WHERE customerId IN('a')");
+                                               .toSQL());
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s IN('a', 'b')", Customer, customerId),
                                db.from(c)
                                                .where(c.customerId).oneOf(new ArrayList<String>() {{
                                                        this.add("a");
                                                        this.add("b");
                                                }})
-                                               .toSQL(),
-                               "SELECT * FROM Customer WHERE customerId IN('a', 'b')");
+                                               .toSQL());
        }
 
        @SuppressWarnings("serial")
        @Test
        public void noneOfTest() {
+               String PrimitivesTest = db.getDialect().prepareTableName(null, "PrimitivesTest");
+               String Customer = db.getDialect().prepareTableName(null, "Customer");
+               String myInteger = db.getDialect().prepareColumnName("myInteger");
+               String customerId = db.getDialect().prepareColumnName("customerId");
+
                PrimitivesModel p = new PrimitivesModel();
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s NOT IN(0)", PrimitivesTest, myInteger),
                                db.from(p)
                                                .where(p.myInteger).noneOf(0)
-                                               .toSQL(),
-                               "SELECT * FROM PrimitivesTest WHERE myInteger NOT IN(0)");
+                                               .toSQL());
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s NOT IN(0, 1)", PrimitivesTest, myInteger),
                                db.from(p)
                                                .where(p.myInteger).noneOf(0, 1)
-                                               .toSQL(),
-                               "SELECT * FROM PrimitivesTest WHERE myInteger NOT IN(0, 1)");
+                                               .toSQL());
                Customer c = new Customer();
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s NOT IN('a')", Customer, customerId),
                                db.from(c)
                                                .where(c.customerId).noneOf(new ArrayList<String>() {{
                                                        this.add("a");
                                                }})
-                                               .toSQL(),
-                               "SELECT * FROM Customer WHERE customerId NOT IN('a')");
+                                               .toSQL());
                assertEquals(
+                               String.format("SELECT * FROM %s WHERE %s NOT IN('a', 'b')", Customer, customerId),
                                db.from(c)
                                                .where(c.customerId).noneOf(new ArrayList<String>() {{
                                                        this.add("a");
                                                        this.add("b");
                                                }})
-                                               .toSQL(),
-                               "SELECT * FROM Customer WHERE customerId NOT IN('a', 'b')");
+                                               .toSQL());
        }
 
 }
index c23527f641746a30be26ecfdc451872134eac3ca..8220d7fccda066a9cff31850ff5e73f27e7cfb0b 100644 (file)
@@ -42,7 +42,7 @@ public class RuntimeQueryTest {
        @Test\r
        public void testParameters() {\r
                Db db = IciqlSuite.openNewDb();\r
-               \r
+\r
                // do not test non-H2 databases because dialects will get in the way\r
                // e.g. column quoting, etc\r
                Assume.assumeTrue(IciqlSuite.isH2(db));\r
@@ -50,10 +50,10 @@ public class RuntimeQueryTest {
                Product p = new Product();\r
                String q1 = db.from(p).where(p.unitsInStock).isParameter().and(p.productName).likeParameter().orderBy(p.productId).toSQL();\r
                String q2 = db.from(p).where(p.unitsInStock).lessThan(100).and(p.productName).like("test").or(p.productName).likeParameter().orderBy(p.productId).toSQL();\r
-               \r
+\r
                StaticQueries.StaticModel1 m1 = new StaticQueries.StaticModel1();\r
                String q3 = db.from(m1).where(m1.myTree).is(Tree.MAPLE).and(m1.myTree).isParameter().toSQL();\r
-               \r
+\r
                StaticQueries.StaticModel2 m2 = new StaticQueries.StaticModel2();\r
                String q4 = db.from(m2).where(m2.myTree).is(Tree.MAPLE).and(m2.myTree).isParameter().toSQL();\r
 \r
@@ -64,7 +64,7 @@ public class RuntimeQueryTest {
                java.sql.Date aDate = new java.sql.Date(now);\r
                java.sql.Time aTime = new java.sql.Time(now);\r
                java.sql.Timestamp aTimestamp = new java.sql.Timestamp(now);\r
-               \r
+\r
                String q6 = db.from(m1).where(m1.myDate).is(aDate).and(m1.myDate).isParameter().toSQL();\r
                String q7 = db.from(m1).where(m1.myTime).is(aTime).and(m1.myTime).isParameter().toSQL();\r
                String q8 = db.from(m1).where(m1.myTimestamp).is(aTimestamp).and(m1.myTimestamp).isParameter().toSQL();\r
@@ -72,7 +72,7 @@ public class RuntimeQueryTest {
                db.close();\r
                assertEquals("SELECT * FROM Product WHERE unitsInStock = ? AND productName LIKE ?  ORDER BY productId", q1);\r
                assertEquals("SELECT * FROM Product WHERE unitsInStock < 100 AND productName LIKE 'test' OR productName LIKE ?  ORDER BY productId", q2);\r
-               \r
+\r
                assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTree = 'MAPLE' AND myTree = ?", q3);\r
                assertEquals("SELECT * FROM StaticQueryTest2 WHERE myTree = 50 AND myTree = ?", q4);\r
                assertEquals("SELECT * FROM StaticQueryTest3 WHERE myTree = 4 AND myTree = ?", q5);\r
@@ -82,72 +82,77 @@ public class RuntimeQueryTest {
                assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTime = '" + new SimpleDateFormat("HH:mm:ss").format(refDate) + "' AND myTime = ?", q7);\r
                assertEquals("SELECT * FROM StaticQueryTest1 WHERE myTimestamp = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refDate) + "' AND myTimestamp = ?", q8);\r
        }\r
-       \r
+\r
        @Test\r
        public void testRuntimeSet() {\r
                Db db = IciqlSuite.openNewDb();\r
-               \r
+\r
                // do not test non-H2 databases because dialects will get in the way\r
                // e.g. column quoting, etc\r
                Assume.assumeTrue(IciqlSuite.isH2(db));\r
-               \r
+\r
                StaticQueries.StaticModel1 m = new StaticQueries.StaticModel1();\r
                String q = db.from(m).set(m.myTimestamp).toParameter().where(m.id).isParameter().toSQL();\r
                db.close();\r
-               \r
+\r
                assertEquals("UPDATE StaticQueryTest1 SET myTimestamp = ? WHERE id = ?", q);\r
        }\r
-       \r
+\r
        @Test\r
        public void testRuntimeSelectWildcards() {\r
                Db db = IciqlSuite.openNewDb();\r
-               \r
+\r
                // do not test non-H2 databases because dialects will get in the way\r
                // e.g. column quoting, etc\r
                Assume.assumeTrue(IciqlSuite.isH2(db));\r
-               \r
+\r
                StaticQueries.StaticModel1 m1 = new StaticQueries.StaticModel1();\r
                StaticQueries.StaticModel2 m2 = new StaticQueries.StaticModel2();\r
                StaticQueries.StaticModel2 m3 = new StaticQueries.StaticModel2();\r
-               \r
+\r
                int t0 = Utils.AS_COUNTER.get() + 1;\r
                int t1 = t0 + 1;\r
-               \r
-               QueryWhere<?> where = db.from(m1).innerJoin(m2).on(m1.id).is(m2.id).where(m2.myTree).is(Tree.MAPLE); \r
+\r
+               QueryWhere<?> where = db.from(m1).innerJoin(m2).on(m1.id).is(m2.id).where(m2.myTree).is(Tree.MAPLE);\r
                String q1 = where.toSQL(false);\r
                String q2 = where.toSQL(true);\r
                String q3 = where.toSQL(false, m1);\r
                String q4 = where.toSQL(true, m1);\r
                String q5 = where.toSQL(false, m2);\r
                String q6 = where.toSQL(true, m2);\r
-               \r
+\r
                // test unused alias\r
                String q7 = where.toSQL(true, m3);\r
-               \r
+\r
                db.close();\r
-               \r
+\r
                assertEquals(MessageFormat.format("SELECT * FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q1);\r
                assertEquals(MessageFormat.format("SELECT DISTINCT * FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q2);\r
-               \r
+\r
                assertEquals(MessageFormat.format("SELECT T{0,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q3);\r
                assertEquals(MessageFormat.format("SELECT DISTINCT T{0,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q4);\r
-               \r
+\r
                assertEquals(MessageFormat.format("SELECT T{1,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q5);\r
                assertEquals(MessageFormat.format("SELECT DISTINCT T{1,number,0}.* FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q6);\r
-               \r
+\r
                assertEquals(MessageFormat.format("SELECT DISTINCT * FROM StaticQueryTest1 AS T{0,number,0} INNER JOIN StaticQueryTest2 AS T{1,number,0} ON T{0,number,0}.id = T{1,number,0}.id  WHERE T{1,number,0}.myTree = 50", t0, t1), q7);\r
        }\r
-       \r
+\r
        @Test\r
        public void testRuntimeQuery() {\r
                Db db = IciqlSuite.openNewDb();\r
                db.insertAll(Product.getList());\r
 \r
+               String unitsInStock = db.getDialect().prepareColumnName("unitsInStock");\r
+               String productName = db.getDialect().prepareColumnName("productName");\r
+               String productId = db.getDialect().prepareColumnName("productId");\r
+\r
                Product p = new Product();\r
-               List<Product> products = db.from(p).where("unitsInStock=?", 120).orderBy(p.productId).select();\r
+               List<Product> products = db.from(p).where(unitsInStock + "=?", 120).orderBy(p.productId).select();\r
                assertEquals(1, products.size());\r
 \r
-               products = db.from(p).where("unitsInStock=? and productName like ? order by productId", 0, "Chef%")\r
+               products = db.from(p).where(String.format("%s=? and productName like ? order by productId",\r
+                               unitsInStock, productName, productId), 0, "Chef%")\r
                                .select();\r
                assertEquals(1, products.size());\r
 \r
@@ -159,14 +164,19 @@ public class RuntimeQueryTest {
                Db db = IciqlSuite.openNewDb();\r
                db.insertAll(Product.getList());\r
 \r
+               String product = db.getDialect().prepareTableName(null, "Product");\r
+               String unitsInStock = db.getDialect().prepareColumnName("unitsInStock");\r
+\r
                // test plain statement\r
                List<Product> products = db.executeQuery(Product.class,\r
-                               "select * from product where unitsInStock=120");\r
+                               String.format("select * from %s where %s=120",\r
+                                               product, unitsInStock));\r
                assertEquals(1, products.size());\r
                assertEquals("Condiments", products.get(0).category);\r
 \r
                // test prepared statement\r
-               products = db.executeQuery(Product.class, "select * from product where unitsInStock=?", 120);\r
+               products = db.executeQuery(Product.class, String.format("select * from %s where %s=?",\r
+                               product, unitsInStock), 120);\r
                assertEquals(1, products.size());\r
                assertEquals("Condiments", products.get(0).category);\r
 \r
@@ -178,8 +188,12 @@ public class RuntimeQueryTest {
                Db db = IciqlSuite.openNewDb();\r
                db.insertAll(Product.getList());\r
 \r
+               String product = db.getDialect().prepareTableName(null, "Product");\r
+               String unitsInStock = db.getDialect().prepareColumnName("unitsInStock");\r
+\r
                // test plain statement\r
-               ResultSet rs = db.executeQuery("select * from product where unitsInStock=120");\r
+               ResultSet rs = db.executeQuery(String.format("select * from %s where %s=120",\r
+                               product, unitsInStock));\r
                List<Product> products = db.buildObjects(Product.class, rs);\r
                JdbcUtils.closeSilently(rs, true);\r
 \r
@@ -187,7 +201,8 @@ public class RuntimeQueryTest {
                assertEquals("Condiments", products.get(0).category);\r
 \r
                // test prepared statement\r
-               rs = db.executeQuery("select * from product where unitsInStock=?", 120);\r
+               rs = db.executeQuery(String.format("select * from %s where %s=?",\r
+                               product, unitsInStock), 120);\r
                products = db.buildObjects(Product.class, rs);\r
                JdbcUtils.closeSilently(rs, true);\r
 \r
index 49a64f5dee8f4a36748a1ce8ceb5bfc4702353c1..df6d219deaef6e183cbdf51bc53dceac78cb6613 100644 (file)
@@ -40,6 +40,7 @@ import org.junit.Test;
 import com.iciql.Db;\r
 import com.iciql.Filter;\r
 import com.iciql.Iciql.IQColumn;\r
+import com.iciql.IciqlException;\r
 import com.iciql.test.models.ComplexObject;\r
 import com.iciql.test.models.Customer;\r
 import com.iciql.test.models.Order;\r
@@ -84,6 +85,10 @@ public class SamplesTest {
 \r
        @Test\r
        public void testReverseColumns() {\r
+               try {\r
+                       db.executeUpdate("DROP TABLE TestReverse");\r
+               } catch (IciqlException e) {\r
+               }\r
                db.executeUpdate("create table TestReverse(id int, name varchar(10), additional varchar(10))");\r
                TestReverse t = new TestReverse();\r
                t.id = 10;\r
@@ -92,7 +97,7 @@ public class SamplesTest {
                TestReverse check = db.from(new TestReverse()).selectFirst();\r
                assertEquals(t.name, check.name);\r
                assertEquals(t.id, check.id);\r
-               db.executeUpdate("DROP TABLE testreverse");\r
+               db.executeUpdate("DROP TABLE TestReverse");\r
        }\r
 \r
        @Test\r
@@ -207,6 +212,7 @@ public class SamplesTest {
                public Integer orderId;\r
                public BigDecimal total;\r
 \r
+               @Override\r
                public String toString() {\r
                        return customerId + ":" + orderId + ":" + total;\r
                }\r
@@ -360,6 +366,7 @@ public class SamplesTest {
                final ComplexObject co = new ComplexObject();\r
 \r
                String sql = db.from(co).where(new Filter() {\r
+                       @Override\r
                        public boolean where() {\r
                                return co.id == x && co.name.equals(name) && co.name.equals("hello");\r
                        }\r
@@ -374,6 +381,7 @@ public class SamplesTest {
                assertEquals(sb.toString(), sql);\r
 \r
                long count = db.from(co).where(new Filter() {\r
+                       @Override\r
                        public boolean where() {\r
                                return co.id == x && co.name.equals(name) && co.name.equals("hello");\r
                        }\r
@@ -412,6 +420,7 @@ public class SamplesTest {
                public String category;\r
                public Long productCount;\r
 \r
+               @Override\r
                public String toString() {\r
                        return category + ":" + productCount;\r
                }\r
index 15ae295e99d4b726d921922eaa4fb87b3ad8ccce..f812339a0226185b3ae19a70c0d3b213a0844fbc 100644 (file)
@@ -71,30 +71,42 @@ public class StackableConditionsTest {
        @SuppressWarnings("serial")
        @Test
        public void andOrTest() {
+               String Customer = db.getDialect().prepareTableName(null,  "Customer");
+               String customerId = db.getDialect().prepareColumnName("customerId");
+               String region = db.getDialect().prepareColumnName("region");
+
                assertEquals(
                                search(null, (String[]) null),
-                               "SELECT * FROM Customer WHERE (true)");
+                               String.format("SELECT * FROM %s WHERE (true)",
+                                               Customer));
                assertEquals(
                                search(null, new String[0]),
-                               "SELECT * FROM Customer WHERE (true) AND ( (false) )");
+                               String.format("SELECT * FROM %s WHERE (true) AND ( (false) )",
+                                               Customer));
                assertEquals(
                                search(null, "0001"),
-                               "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' )");
+                               String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' )",
+                                               Customer, customerId));
                assertEquals(
                                search(null, "0001", "0002"),
-                               "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' OR customerId = '0002' )");
+                               String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' OR %s = '0002' )",
+                                               Customer, customerId, customerId));
                assertEquals(
                                search(Region.JP, (String[]) null),
-                               "SELECT * FROM Customer WHERE (true) AND region = 'JP'");
+                               String.format("SELECT * FROM %s WHERE (true) AND %s = 'JP'",
+                                               Customer, region));
                assertEquals(
                                search(Region.JP, new String[0]),
-                               "SELECT * FROM Customer WHERE (true) AND ( (false) ) AND region = 'JP'");
+                               String.format("SELECT * FROM %s WHERE (true) AND ( (false) ) AND %s = 'JP'",
+                                               Customer, region));
                assertEquals(
                                search(Region.JP, "0001"),
-                               "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' ) AND region = 'JP'");
+                               String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' ) AND %s = 'JP'",
+                                               Customer, customerId, region));
                assertEquals(
                                search(Region.JP, "0001", "0002"),
-                               "SELECT * FROM Customer WHERE (true) AND ( (false) OR customerId = '0001' OR customerId = '0002' ) AND region = 'JP'");
+                               String.format("SELECT * FROM %s WHERE (true) AND ( (false) OR %s = '0001' OR %s = '0002' ) AND %s = 'JP'",
+                                               Customer, customerId, customerId, region));
        }
 
        @Test
@@ -144,6 +156,10 @@ public class StackableConditionsTest {
 
        @Test
        public void fluentTest() {
+               String Customer = db.getDialect().prepareTableName(null,  "Customer");
+               String customerId = db.getDialect().prepareColumnName("customerId");
+               String region = db.getDialect().prepareColumnName("region");
+
                final Customer model = new Customer();
                assertEquals(
                                db.from(model).where(new And<Customer>(db, model) {{
@@ -153,7 +169,8 @@ public class StackableConditionsTest {
                                                or(model.region).is("LA");
                                        }});
                                }}).toSQL(),
-                               "SELECT * FROM Customer WHERE (true) AND customerId = '0001' AND ( (false) OR region = 'CA' OR region = 'LA' )");
+                               String.format("SELECT * FROM %s WHERE (true) AND %s = '0001' AND ( (false) OR %s = 'CA' OR %s = 'LA' )",
+                                               Customer, customerId, region, region));
                assertEquals(
                                db.from(model).where(new Or<Customer>(db, model) {{
                                        or(model.customerId).is("0001");
@@ -162,7 +179,8 @@ public class StackableConditionsTest {
                                                and(model.region).is("LA");
                                        }});
                                }}).toSQL(),
-                               "SELECT * FROM Customer WHERE (false) OR customerId = '0001' OR ( (true) AND customerId = '0002' AND region = 'LA' )");
+                               String.format("SELECT * FROM %s WHERE (false) OR %s = '0001' OR ( (true) AND %s = '0002' AND %s = 'LA' )",
+                                               Customer, customerId, customerId, region));
                assertEquals(
                                db.from(model)
                                                .where(model.customerId).isNotNull()
@@ -172,7 +190,8 @@ public class StackableConditionsTest {
                                                }})
                                                .and(model.region).isNotNull()
                                                .toSQL(),
-                               "SELECT * FROM Customer WHERE customerId IS NOT NULL AND ( (false) OR region = 'LA' OR region = 'CA' ) AND region IS NOT NULL");
+                               String.format("SELECT * FROM %s WHERE %s IS NOT NULL AND ( (false) OR %s = 'LA' OR %s = 'CA' ) AND %s IS NOT NULL",
+                                               Customer, customerId, region, region, region));
        }
 
 }