diff options
author | James Moger <james.moger@gitblit.com> | 2014-11-10 11:59:09 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-11-10 21:46:37 -0500 |
commit | a6df2de41953e10db1527e54acd734c0f0a1fa28 (patch) | |
tree | ad3a86dc2a54cf3ac9418e0e5e86f396f4e1dfb0 /src/test | |
parent | c1d81bcdfc948b417964c6b69be2ee5801e5e1c9 (diff) | |
download | iciql-a6df2de41953e10db1527e54acd734c0f0a1fa28.tar.gz iciql-a6df2de41953e10db1527e54acd734c0f0a1fa28.zip |
Implement DAO externalized statement loading based on runtime Mode
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/iciql/test/IciqlSuite.java | 10 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/ProductDaoTest.java | 53 | ||||
-rw-r--r-- | src/test/java/iciql.properties | 7 |
3 files changed, 66 insertions, 4 deletions
diff --git a/src/test/java/com/iciql/test/IciqlSuite.java b/src/test/java/com/iciql/test/IciqlSuite.java index c088ff9..c80da93 100644 --- a/src/test/java/com/iciql/test/IciqlSuite.java +++ b/src/test/java/com/iciql/test/IciqlSuite.java @@ -48,6 +48,7 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters;
import com.iciql.Constants;
import com.iciql.Db;
+import com.iciql.Iciql.Mode;
import com.iciql.test.DataTypeAdapterTest.SerializedObjectTypeAdapterTest;
import com.iciql.test.models.BooleanModel;
import com.iciql.test.models.CategoryAnnotationOnly;
@@ -144,13 +145,18 @@ public class IciqlSuite { return Math.abs(expected - actual) <= 0.000001d;
}
+ public static Db openNewDb() {
+ return openNewDb(Mode.PROD);
+ }
+
/**
* Open a new Db object. All connections are cached and re-used to eliminate
* embedded database startup costs.
*
+ * @param mode
* @return a fresh Db object
*/
- public static Db openNewDb() {
+ public static Db openNewDb(Mode mode) {
String testUrl = System.getProperty("iciql.url", DEFAULT_TEST_DB.url);
String testUser = System.getProperty("iciql.user", DEFAULT_TEST_DB.username);
String testPassword = System.getProperty("iciql.password", DEFAULT_TEST_DB.password);
@@ -168,7 +174,7 @@ public class IciqlSuite { dataSources.put(testUrl, dataSource);
connectionFactories.put(testUrl, factory);
}
- db = Db.open(dataSource);
+ db = Db.open(dataSource, mode);
// drop views
db.dropView(ProductView.class);
diff --git a/src/test/java/com/iciql/test/ProductDaoTest.java b/src/test/java/com/iciql/test/ProductDaoTest.java index 78987fd..e5549c1 100644 --- a/src/test/java/com/iciql/test/ProductDaoTest.java +++ b/src/test/java/com/iciql/test/ProductDaoTest.java @@ -26,7 +26,9 @@ import org.junit.Before; import org.junit.Test; import com.iciql.Dao; +import com.iciql.DaoClasspathStatementProvider; import com.iciql.Db; +import com.iciql.Iciql.Mode; import com.iciql.IciqlException; import com.iciql.test.DataTypeAdapterTest.SerializedObjectTypeAdapterTest; import com.iciql.test.DataTypeAdapterTest.SupportedTypesAdapter; @@ -48,6 +50,7 @@ public class ProductDaoTest extends Assert { db = IciqlSuite.openNewDb(); db.insertAll(Product.getList()); db.insertAll(Order.getList()); + db.setDaoStatementProvider(new DaoClasspathStatementProvider()); } @After @@ -273,6 +276,47 @@ public class ProductDaoTest extends Assert { assertTrue(obj1.equivalentTo(obj2)); } + @Test + public void testDefaultProdResourceQueryReturnModels() { + + ProductDao dao = db.open(ProductDao.class); + + Product[] products = dao.getProductsFromResourceQuery(); + assertEquals(10, products.length); + } + + @Test + public void testDevResourceQueryReturnModels() { + + Db db = IciqlSuite.openNewDb(Mode.DEV); + db.insertAll(Product.getList()); + db.insertAll(Order.getList()); + db.setDaoStatementProvider(new DaoClasspathStatementProvider()); + + ProductDao dao = db.open(ProductDao.class); + + Product[] products = dao.getProductsFromResourceQuery(); + assertEquals(5, products.length); + + db.close(); + } + + @Test + public void testTestResourceQueryReturnModels() { + + Db db = IciqlSuite.openNewDb(Mode.TEST); + db.insertAll(Product.getList()); + db.insertAll(Order.getList()); + db.setDaoStatementProvider(new DaoClasspathStatementProvider()); + + ProductDao dao = db.open(ProductDao.class); + + Product[] products = dao.getProductsFromResourceQuery(); + assertEquals(2, products.length); + + db.close(); + } + /** * Define the Product DAO interface. */ @@ -320,7 +364,8 @@ public class ProductDaoTest extends Assert { @SqlQuery("select productId from Product where category = :category") long[] getProductIdsForCategory(@Bind("category") String cat); - @SqlQuery("select orderDate from Orders order by orderDate desc limit 1") + // will break ResultSet iteration after retrieving first value + @SqlQuery("select orderDate from Orders order by orderDate desc") Date getMostRecentOrder(); @SqlStatement("update Product set productName = 'test' where productId = 1") @@ -335,12 +380,16 @@ public class ProductDaoTest extends Assert { @SqlStatement("update Product set category = :newCategory where category = :oldCategory") int renameProductCategoryReturnsCount(@Bind("oldCategory") String oldCategory, @Bind("newCategory") String newCategory); - @SqlQuery("select obj from dataTypeAdapters limit 1") + // will break ResultSet iteration after retrieving first value + @SqlQuery("select obj from dataTypeAdapters") @SupportedTypesAdapter SupportedTypes getCustomDataType(); @SqlStatement("update dataTypeAdapters set obj=:2 where id=:1") boolean setSupportedTypes(long id, @SupportedTypesAdapter SupportedTypes obj); + @SqlQuery("get.products") + Product[] getProductsFromResourceQuery(); + } } diff --git a/src/test/java/iciql.properties b/src/test/java/iciql.properties new file mode 100644 index 0000000..cecb056 --- /dev/null +++ b/src/test/java/iciql.properties @@ -0,0 +1,7 @@ +# +# Example resource file for DaoClasspathStatementProvider +# + +get.products = select * from Product +%test.get.products = select * from Product where category = 'Beverages' +%dev.get.products = select * from Product where category = 'Condiments' |