You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuntimeQueryTest.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2011 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql.test;
  17. import static org.junit.Assert.assertEquals;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.List;
  21. import org.junit.Test;
  22. import com.iciql.Db;
  23. import com.iciql.test.models.Product;
  24. import com.iciql.util.JdbcUtils;
  25. /**
  26. * Tests the runtime dynamic query function.
  27. */
  28. public class RuntimeQueryTest {
  29. @Test
  30. public void testRuntimeQuery() {
  31. Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
  32. db.insertAll(Product.getList());
  33. Product p = new Product();
  34. List<Product> products = db.from(p).where("unitsInStock=?", 120).orderBy(p.productId).select();
  35. assertEquals(1, products.size());
  36. products = db.from(p).where("unitsInStock=? and productName like ? order by productId", 0, "Chef%")
  37. .select();
  38. assertEquals(1, products.size());
  39. db.close();
  40. }
  41. @Test
  42. public void testExecuteQuery() throws SQLException {
  43. Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
  44. db.insertAll(Product.getList());
  45. // test plain statement
  46. List<Product> products = db.executeQuery(Product.class, "select * from product where unitsInStock=120");
  47. assertEquals(1, products.size());
  48. assertEquals("Condiments", products.get(0).category);
  49. // test prepared statement
  50. products = db.executeQuery(Product.class, "select * from product where unitsInStock=?", 120);
  51. assertEquals(1, products.size());
  52. assertEquals("Condiments", products.get(0).category);
  53. db.close();
  54. }
  55. @Test
  56. public void testBuildObjects() throws SQLException {
  57. Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
  58. db.insertAll(Product.getList());
  59. // test plain statement
  60. ResultSet rs = db.executeQuery("select * from product where unitsInStock=120");
  61. List<Product> products = db.buildObjects(Product.class, rs);
  62. JdbcUtils.closeSilently(rs, true);
  63. assertEquals(1, products.size());
  64. assertEquals("Condiments", products.get(0).category);
  65. // test prepared statement
  66. rs = db.executeQuery("select * from product where unitsInStock=?", 120);
  67. products = db.buildObjects(Product.class, rs);
  68. JdbcUtils.closeSilently(rs, true);
  69. assertEquals(1, products.size());
  70. assertEquals("Condiments", products.get(0).category);
  71. db.close();
  72. }
  73. }