From: Alexey L. <lou...@us...> - 2004-03-16 10:47:41
|
User: loubyansky Date: 04/03/16 02:38:25 Modified: src/main/org/jboss/test/cmp2/commerce QueryTest.java Log: added EJBQL21 tests for JBossQL Revision Changes Path 1.15 +116 -158 jbosstest/src/main/org/jboss/test/cmp2/commerce/QueryTest.java Index: QueryTest.java =================================================================== RCS file: /cvsroot/jboss/jbosstest/src/main/org/jboss/test/cmp2/commerce/QueryTest.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- QueryTest.java 14 Mar 2004 14:44:29 -0000 1.14 +++ QueryTest.java 16 Mar 2004 10:38:24 -0000 1.15 @@ -22,6 +22,8 @@ { private JDBCEJBQLCompiler compiler; private static final String javaVersion = System.getProperty("java.specification.version"); + private static final Class[] EMPTY_CLS_ARR = new Class[]{}; + private static final Class[] TWO_STRINGS_CLS_ARR = new Class[]{String.class, String.class}; public static Test suite() throws Exception { @@ -48,7 +50,7 @@ private String compileEJBQL(String ejbql) { - return compileEJBQL(ejbql, java.util.Collection.class, new Class[]{}); + return compileEJBQL(ejbql, java.util.Collection.class, EMPTY_CLS_ARR); } private String compileEJBQL(String ejbql, Class returnType, Class[] paramClasses) @@ -102,7 +104,7 @@ "SELECT t0_o1.ORDER_NUMBER FROM ORDER_DATA t0_o1, ORDER_DATA t3_o2, CUSTOMEREJB t1_o1_customer, CUSTOMEREJB t2_o2_customer WHERE (( NOT (t1_o1_customer.id=t2_o2_customer.id)) AND (t0_o1.CC_TYPE=t3_o2.CC_TYPE AND t0_o1.CC_FIRST_NAME=t3_o2.CC_FIRST_NAME AND t0_o1.CC_MI=t3_o2.CC_MI AND t0_o1.CC_LAST_NAME=t3_o2.CC_LAST_NAME AND t0_o1.CC_BILLING_ZIP=t3_o2.CC_BILLING_ZIP AND t0_o1.CC_CARD_NUMBER=t3_o2.CC_CARD_NUMBER) AND t0_o1.customer=t1_o1_customer.id AND t3_o2.customer=t2_o2_customer.id)"; String compiled = compileJBossQL( "SELECT OBJECT(o1) FROM OrderX o1, OrderX o2 WHERE o1.customer <> o2.customer AND o1.creditCard = o2.creditCard", - Collection.class, new Class[]{} + Collection.class, EMPTY_CLS_ARR ); assertTrue("Expected: " + expected + " but got: " + compiled, expected.equals(compiled)); @@ -138,15 +140,15 @@ assertEquals( "SELECT DISTINCT t0_u.USER_ID, t0_u.USER_NAME FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC", - compileJBossQL("SELECT DISTINCT OBJECT(u) FROM user u ORDER BY u.userName", Collection.class, new Class[]{}) + compileJBossQL("SELECT DISTINCT OBJECT(u) FROM user u ORDER BY u.userName", Collection.class, EMPTY_CLS_ARR) ); assertEquals( "SELECT DISTINCT t0_u.USER_ID FROM USER_DATA t0_u ORDER BY t0_u.USER_ID ASC", - compileJBossQL("SELECT DISTINCT OBJECT(u) FROM user u ORDER BY u.userId", Collection.class, new Class[]{}) + compileJBossQL("SELECT DISTINCT OBJECT(u) FROM user u ORDER BY u.userId", Collection.class, EMPTY_CLS_ARR) ); assertEquals( "SELECT DISTINCT t0_u.USER_NAME FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC", - compileJBossQL("SELECT DISTINCT u.userName FROM user u ORDER BY u.userName", Collection.class, new Class[]{}) + compileJBossQL("SELECT DISTINCT u.userName FROM user u ORDER BY u.userName", Collection.class, EMPTY_CLS_ARR) ); assertEquals( "SELECT DISTINCT ucase(t0_u.USER_NAME) FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC", @@ -161,7 +163,7 @@ compileJBossQL( "SELECT OBJECT(o) FROM OrderX o WHERE o.shippingAddress.city=?1 OR o.shippingAddress.state=?2", Collection.class, - new Class[]{String.class, String.class} + TWO_STRINGS_CLS_ARR ) ); @@ -170,7 +172,7 @@ compileJBossQL( "SELECT OBJECT(o) FROM OrderX o ORDER BY o.shippingAddress.state", Collection.class, - new Class[]{String.class, String.class} + TWO_STRINGS_CLS_ARR ) ); } @@ -187,7 +189,7 @@ compileEJBQL( "SELECT OBJECT(o) FROM OrderX o WHERE o.shippingAddress.city=?1 OR o.shippingAddress.state=?2", Collection.class, - new Class[]{String.class, String.class} + TWO_STRINGS_CLS_ARR ) ); @@ -305,27 +307,23 @@ public void testEJBQL21OrderBy() throws Exception { - assertEquals( - "SELECT DISTINCT t0_u.USER_ID, t0_u.USER_NAME FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC", - compileEJBQL("SELECT DISTINCT OBJECT(u) FROM user u ORDER BY u.userName", Collection.class, new Class[]{}) - ); - - assertEquals( - "SELECT DISTINCT t0_u.USER_NAME FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC", - compileEJBQL("SELECT DISTINCT u.userName FROM user u ORDER BY u.userName", Collection.class, new Class[]{}) - ); + String ql = "SELECT DISTINCT OBJECT(u) FROM user u ORDER BY u.userName"; + String sql = "SELECT DISTINCT t0_u.USER_ID, t0_u.USER_NAME FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + ql = "SELECT DISTINCT u.userName FROM user u ORDER BY u.userName"; + sql ="SELECT DISTINCT t0_u.USER_NAME FROM USER_DATA t0_u ORDER BY t0_u.USER_NAME ASC"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); - assertEquals( - "SELECT t0_addr.id, t0_addr.state, t0_addr.city " + + ql = "SELECT OBJECT(addr) FROM OrderX o, Address addr WHERE o.shippingAddress = addr ORDER BY addr.state DESC, addr.city ASC"; + sql = "SELECT t0_addr.id, t0_addr.state, t0_addr.city " + "FROM ORDER_DATA t2_o, ADDRESSEJB t0_addr, ADDRESSEJB t1_o_shippingAddress " + "WHERE ((t1_o_shippingAddress.id=t0_addr.id) AND t2_o.SHIPPING_ADDRESS=t1_o_shippingAddress.id) " + - "ORDER BY t0_addr.state DESC, t0_addr.city ASC", - compileEJBQL( - "SELECT OBJECT(addr) FROM OrderX o, Address addr WHERE o.shippingAddress = addr ORDER BY addr.state DESC, addr.city ASC", - Collection.class, - new Class[]{String.class, String.class} - ) - ); + "ORDER BY t0_addr.state DESC, t0_addr.city ASC"; + assertEquals(sql, compileEJBQL(ql, Collection.class, TWO_STRINGS_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, TWO_STRINGS_CLS_ARR)); } /** @@ -335,14 +333,11 @@ */ public void testEJBQL21InExpression() throws Exception { - assertEquals( - "SELECT t0_o.id FROM LINEITEMEJB t0_o WHERE (t0_o.id IN (1, 2, ?))", - compileEJBQL( - "select object(o) from LineItem o where o.id in(1, 2, ?1)", - Collection.class, - new Class[]{java.lang.Long.class} - ) - ); + final String sql = "SELECT t0_o.id FROM LINEITEMEJB t0_o WHERE (t0_o.id IN (1, 2, ?))"; + final String ql = "select object(o) from LineItem o where o.id in(1, 2, ?1)"; + Class[] params = new Class[]{Long.class}; + assertEquals(sql, compileEJBQL(ql, Collection.class, params)); + assertEquals(sql, compileJBossQL(ql, Collection.class, params)); } /** @@ -357,14 +352,32 @@ */ public void testEJBQL21LikeExpression() throws Exception { - assertEquals( - "SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o WHERE (t0_o.ORDER_STATUS LIKE ? ESCAPE ?)", - compileEJBQL( - "select object(o) from OrderX o where o.orderStatus like ?1 escape ?2", - Collection.class, - new Class[]{String.class, Character.class} - ) - ); + final String sql = "SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o WHERE (t0_o.ORDER_STATUS LIKE ? ESCAPE ?)"; + final String ql = "select object(o) from OrderX o where o.orderStatus like ?1 escape ?2"; + final Class[] params = new Class[]{String.class, Character.class}; + assertEquals(sql, compileEJBQL(ql, Collection.class, params)); + assertEquals(sql, compileJBossQL(ql, Collection.class, params)); + } + + /** + * Tests IS [NOT] NULL for input parameter. + */ + public void testEJBQL21IsNullExpression() throws Exception + { + String ql = "select object(o) from OrderX o where ?1 IS NULL"; + String sql = "SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o WHERE (? IS NULL)"; + Class[] params = new Class[]{Product.class}; + assertEquals(sql, compileEJBQL(ql, Collection.class, params)); + assertEquals(sql, compileJBossQL(ql, Collection.class, params)); + } + + public void testEJBQL21ModFunction() throws Exception + { + String sql = "SELECT t0_o.id FROM LINEITEMEJB t0_o WHERE (mod(t0_o.quantity, ?) < 5)"; + String ql = "select object(o) from LineItem o where mod(o.quantity, ?1) < 5"; + Class[] params = new Class[]{Integer.class}; + assertEquals(sql, compileEJBQL(ql, Collection.class, params)); + assertEquals(sql, compileJBossQL(ql, Collection.class, params)); } /** @@ -378,137 +391,82 @@ */ public void testEJBQL21AggregateFunctions() throws Exception { - assertEquals( - "SELECT avg( t0_o.quantity) FROM LINEITEMEJB t0_o", - compileEJBQL( - "select avg(o.quantity) from LineItem o", - Collection.class, - new Class[]{} - ) - ); - - assertEquals( - "SELECT min( t0_o.quantity) FROM LINEITEMEJB t0_o", - compileEJBQL( - "select min(o.quantity) from LineItem o", - Collection.class, - new Class[]{} - ) - ); - - assertEquals( - "SELECT max( t0_o.quantity) FROM LINEITEMEJB t0_o", - compileEJBQL( - "select max(o.quantity) from LineItem o", - Collection.class, - new Class[]{} - ) - ); - - assertEquals( - "SELECT sum( t0_o.quantity) FROM LINEITEMEJB t0_o", - compileEJBQL( - "select sum(o.quantity) from LineItem o", - Collection.class, - new Class[]{} - ) - ); + String sql = "SELECT avg( t0_o.quantity) FROM LINEITEMEJB t0_o"; + String ql = "select avg(o.quantity) from LineItem o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + sql = "SELECT min( t0_o.quantity) FROM LINEITEMEJB t0_o"; + ql = "select min(o.quantity) from LineItem o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + sql = "SELECT max( t0_o.quantity) FROM LINEITEMEJB t0_o"; + ql = "select max(o.quantity) from LineItem o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + sql = "SELECT sum( t0_o.quantity) FROM LINEITEMEJB t0_o"; + ql = "select sum(o.quantity) from LineItem o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + sql = "SELECT count( t0_o.quantity) FROM LINEITEMEJB t0_o"; + ql = "select count(o.quantity) from LineItem o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + sql = "SELECT count(DISTINCT t0_o.quantity) FROM LINEITEMEJB t0_o"; + ql = "select count(distinct o.quantity) from LineItem o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); + + sql = "SELECT count( t0_o.ORDER_NUMBER) FROM ORDER_DATA t0_o"; + ql = "select count(o) from OrderX o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); - assertEquals( - "SELECT count( t0_o.quantity) FROM LINEITEMEJB t0_o", - compileEJBQL( - "select count(o.quantity) from LineItem o", - Collection.class, - new Class[]{} - ) - ); - - assertEquals( - "SELECT count(DISTINCT t0_o.quantity) FROM LINEITEMEJB t0_o", - compileEJBQL( - "select count(distinct o.quantity) from LineItem o", - Collection.class, - new Class[]{} - ) - ); - - assertEquals( - "SELECT count( t0_o.ORDER_NUMBER) FROM ORDER_DATA t0_o", - compileEJBQL( - "select count(o) from OrderX o", - Collection.class, - new Class[]{} - ) - ); - - assertEquals( - "SELECT count( t0_o_shippingAddress.id) " + + sql = "SELECT count( t0_o_shippingAddress.id) " + "FROM ORDER_DATA t1_o, ADDRESSEJB t0_o_shippingAddress " + - "WHERE t1_o.SHIPPING_ADDRESS=t0_o_shippingAddress.id", - compileEJBQL( - "select count(o.shippingAddress) from OrderX o", - Collection.class, - new Class[]{} - ) - ); + "WHERE t1_o.SHIPPING_ADDRESS=t0_o_shippingAddress.id"; + ql = "select count(o.shippingAddress) from OrderX o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); - assertEquals( - "SELECT count( t0_l.id) " + + sql = "SELECT count( t0_l.id) " + "FROM ORDER_DATA t1_o, LINEITEMEJB t0_l " + - "WHERE ((t1_o.ORDER_NUMBER = ?)) AND t1_o.ORDER_NUMBER=t0_l.ORDER_NUMBER", - compileEJBQL( - "select count(l) from OrderX o, in(o.lineItems) l where o.ordernumber=?1", - Collection.class, - new Class[]{java.lang.Long.class} - ) - ); + "WHERE ((t1_o.ORDER_NUMBER = ?)) AND t1_o.ORDER_NUMBER=t0_l.ORDER_NUMBER"; + ql = "select count(l) from OrderX o, in(o.lineItems) l where o.ordernumber=?1"; + Class[] params = new Class[]{java.lang.Long.class}; + assertEquals(sql, compileEJBQL(ql, Collection.class, params)); + assertEquals(sql, compileJBossQL(ql, Collection.class, params)); - assertEquals( - "SELECT COUNT(*) FROM (" + + sql = "SELECT COUNT(*) FROM (" + "SELECT t0_o_parent.id, t0_o_parent.subId " + "FROM PRODUCTCATEGORYEJB t1_o, PRODUCTCATEGORYEJB t0_o_parent " + "WHERE t1_o.parent_id=t0_o_parent.id AND t1_o.parent_subId=t0_o_parent.subId" + - ") t_count", - compileEJBQL( - "select count(o.parent) from ProductCategory o", - Collection.class, - new Class[]{} - ) - ); + ") t_count"; + ql = "select count(o.parent) from ProductCategory o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); - assertEquals( - "SELECT COUNT(*) FROM (" + + sql = "SELECT COUNT(*) FROM (" + "SELECT DISTINCT t0_o_parent.id, t0_o_parent.subId " + "FROM PRODUCTCATEGORYEJB t1_o, PRODUCTCATEGORYEJB t0_o_parent " + "WHERE t1_o.parent_id=t0_o_parent.id AND t1_o.parent_subId=t0_o_parent.subId" + - ") t_count", - compileEJBQL( - "select count(distinct o.parent) from ProductCategory o", - Collection.class, - new Class[]{} - ) - ); + ") t_count"; + ql = "select count(distinct o.parent) from ProductCategory o"; + assertEquals(sql, compileEJBQL(ql, Collection.class, EMPTY_CLS_ARR)); + assertEquals(sql, compileJBossQL(ql, Collection.class, EMPTY_CLS_ARR)); - assertEquals( - "SELECT COUNT(*) FROM (" + + sql = "SELECT COUNT(*) FROM (" + "SELECT t0_l.id, t0_l.subId " + "FROM PRODUCTCATEGORYEJB t1_o, PRODUCTCATEGORYEJB t0_l " + "WHERE ((t1_o.id = ? AND t1_o.subId = ?)) AND t1_o.id=t0_l.parent_id AND t1_o.subId=t0_l.parent_subId" + - ") t_count", - compileEJBQL( - "select count(l) from ProductCategory o, in(o.subcategories) l where o.id=?1 and o.subId=?2", - Collection.class, - new Class[]{long.class, long.class} - ) - ); - - assertEquals( - "SELECT t0_o.id FROM LINEITEMEJB t0_o WHERE (mod(t0_o.quantity, ?) < 5)", - compileEJBQL( - "select object(o) from LineItem o where mod(o.quantity, ?1) < 5", - Collection.class, - new Class[]{Integer.class} - ) - ); + ") t_count"; + ql = "select count(l) from ProductCategory o, in(o.subcategories) l where o.id=?1 and o.subId=?2"; + params = new Class[]{long.class, long.class}; + assertEquals(sql, compileEJBQL(ql, Collection.class, params)); + assertEquals(sql, compileJBossQL(ql, Collection.class, params)); } } |