From: Jeremy B. <jb...@us...> - 2003-01-30 20:32:57
|
User: jboynes Date: 03/01/30 12:32:55 Modified: src/main/org/jboss/test/cmp2/commerce QueryTest.java Log: Updated to use JDBCEJBQLCompiler directly and verify the SQL generated Should make it easier to add SQL generation tests Revision Changes Path 1.6 +212 -82 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- QueryTest.java 28 Jan 2003 23:56:06 -0000 1.5 +++ QueryTest.java 30 Jan 2003 20:32:55 -0000 1.6 @@ -1,108 +1,238 @@ package org.jboss.test.cmp2.commerce; import java.util.Collection; -import java.util.Iterator; import java.util.Set; +import javax.management.MBeanServer; +import javax.management.MBeanServerFactory; +import javax.management.ObjectName; import javax.naming.InitialContext; + import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; import org.jboss.blocks.junit.ejb.EJBTestCase; +import org.jboss.ejb.EntityContainer; +import org.jboss.ejb.plugins.cmp.ejbql.Catalog; +import org.jboss.ejb.plugins.cmp.jdbc.JDBCEJBQLCompiler; +import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData; +import org.jboss.mx.server.ServerConstants; +import org.jboss.mx.server.registry.MBeanEntry; +import org.jboss.mx.server.registry.MBeanRegistry; import org.jboss.test.JBossTestCase; +import org.jboss.util.jmx.MBeanProxy; +import org.jboss.util.UnreachableStatementException; public class QueryTest extends EJBTestCase { + private JDBCEJBQLCompiler compiler; + public static Test suite() throws Exception { return JBossTestCase.getDeploySetup( QueryTest.class, "cmp2-commerce.jar"); - } + } + public QueryTest(String name) { super(name); } - private OrderHome getOrderHome() { - try { - InitialContext jndiContext = new InitialContext(); - - return (OrderHome) jndiContext.lookup("commerce/Order"); - } catch(Exception e) { - e.printStackTrace(); - fail("Exception in getOrderHome: " + e.getMessage()); + public void setUpEJB() throws Exception + { + + MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0); + ObjectName name = new ObjectName("jboss.j2ee:jndiName=commerce/Order,service=EJB"); + MBeanRegistry registry = (MBeanRegistry) MBeanProxy.create(MBeanRegistry.class, + ServerConstants.MBEAN_REGISTRY, + server); + MBeanEntry entry = registry.get(name); + EntityContainer container = (EntityContainer) entry.getResourceInstance(); + Catalog catalog = (Catalog) container.getEjbModule().getModuleData("CATALOG"); + compiler = new JDBCEJBQLCompiler(catalog); + } + + private String compileEJBQL(String ejbql) + { + return compileEJBQL(ejbql, java.util.Collection.class, new Class[] { }); + } + + private String compileEJBQL(String ejbql, Class returnType, Class[] paramClasses) + { + try + { + compiler.compileEJBQL(ejbql, returnType, paramClasses, new JDBCReadAheadMetaData("on-load", 100, "*")); + return compiler.getSQL(); + } + catch (Throwable t) + { + fail(t.getMessage()); + throw new UnreachableStatementException(); + } + } + + private String compileJBossQL(String ejbql, Class returnType, Class[] paramClasses) + { + try + { + compiler.compileJBossQL(ejbql, returnType, paramClasses, new JDBCReadAheadMetaData("on-load", 100, "*")); + return compiler.getSQL(); + } + catch (Throwable t) + { + fail(t.getMessage()); + throw new UnreachableStatementException(); } - return null; } - public void test_queries() throws Exception { + public void testJBossQL() throws Exception { + assertEquals("SELECT t0_u.USER_ID FROM USER_DATA t0_u WHERE ucase(t0_u.USER_NAME) = ?", + compileJBossQL("SELECT OBJECT(u) FROM user u WHERE UCASE(u.userName) = ?1", + Collection.class, new Class[] { String.class })); + + assertEquals("SELECT t0_u.USER_ID FROM USER_DATA t0_u WHERE lcase(t0_u.USER_NAME) = ?", + compileJBossQL("SELECT OBJECT(u) FROM user u WHERE LCASE(u.userName) = ?1", + Collection.class, new Class[] { String.class })); + + assertEquals("SELECT t0_o1.ORDER_NUMBER "+ + "FROM ORDER_DATA t0_o1, ORDER_DATA t3_o2, CUSTOMEREJB t2_o2_customer, CUSTOMEREJB t1_o1_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 (t3_o2.customer=t2_o2_customer.id AND t0_o1.customer=t1_o1_customer.id)", + compileJBossQL("SELECT OBJECT(o1) " + + "FROM OrderX o1, OrderX o2 " + + "WHERE o1.customer <> o2.customer AND o1.creditCard = o2.creditCard", + Collection.class, new Class[] {})); + + assertEquals("SELECT t0_o.ORDER_NUMBER "+ + "FROM ORDER_DATA t0_o "+ + "WHERE (t0_o.CC_TYPE=? "+ + "AND t0_o.CC_FIRST_NAME=? "+ + "AND t0_o.CC_MI=? "+ + "AND t0_o.CC_LAST_NAME=? "+ + "AND t0_o.CC_BILLING_ZIP=? "+ + "AND t0_o.CC_CARD_NUMBER=?)", + compileJBossQL("SELECT OBJECT(o) FROM OrderX o WHERE o.creditCard = ?1", + Collection.class, new Class[] { Card.class })); + + assertEquals("SELECT t0_o.ORDER_NUMBER "+ + "FROM ORDER_DATA t0_o "+ + "WHERE (NOT(t0_o.CC_TYPE=? "+ + "AND t0_o.CC_FIRST_NAME=? "+ + "AND t0_o.CC_MI=? "+ + "AND t0_o.CC_LAST_NAME=? "+ + "AND t0_o.CC_BILLING_ZIP=? "+ + "AND t0_o.CC_CARD_NUMBER=?))", + compileJBossQL("SELECT OBJECT(o) FROM OrderX o WHERE o.creditCard <> ?1", + Collection.class, new Class[] { Card.class })); - OrderHome oh = getOrderHome(); - oh.getStuff( - "SELECT OBJECT(o) " + - "FROM OrderX o, " + - " IN(o.lineItems) l, " + - " IN(l.product.productCategories) pc " + - "WHERE o.ordernumber = ?1 and pc.name=?2", - new Object[] { new Long(1), "stuff" }); - - oh.getStuff( - "SELECT OBJECT(u) " + - "FROM user u " + - "WHERE UCASE(u.userName) = ?1", - new Object[] { "DAIN" }); - - oh.getStuff( - "SELECT OBJECT(u) " + - "FROM user u " + - "WHERE LCASE(u.userName) = ?1", - new Object[] { "dain" }); - - oh.getStuff( - "SELECT OBJECT(o1) " + - "FROM OrderX o1, OrderX o2 " + - "WHERE o1.customer <> o2.customer AND o1.creditCard = o2.creditCard", - new Object[] { }); - - oh.getStuff( - "SELECT OBJECT(o) " + - "FROM OrderX o " + - "WHERE o.creditCard = ?1", - new Object[] { new Card() }); - - oh.getStuff( - "SELECT OBJECT(o) " + - "FROM OrderX o " + - "WHERE o.creditCard <> ?1", - new Object[] { new Card() }); - - oh.getStuff( - "SELECT OBJECT(o) " + - "FROM OrderX o " + - "WHERE o.ordernumber = ?1 OR o.lineItems IS NOT EMPTY", - new Object[] { new Long(1) }); - - oh.getStuff( - "SELECT OBJECT(o) " + - "FROM OrderX o, LineItem l " + - "WHERE o.ordernumber = ?1 OR l MEMBER o.lineItems", - new Object[] { new Long(1) }); - - oh.getStuff( - "SELECT OBJECT(o) " + - "FROM OrderX o, LineItem l " + - "WHERE o.ordernumber = ?1 OR l NOT MEMBER o.lineItems", - new Object[] { new Long(1) }); - - oh.getStuff( - "SELECT OBJECT(p) " + - "FROM Product p, ProductCategory pc" + - "WHERE p.id = ?1 OR pc MEMBER p.productCategories", - new Object[] { new Long(1) }); - - oh.getStuff( - "SELECT OBJECT(p) " + - "FROM Product p, ProductCategory pc" + - "WHERE p.id = ?1 OR pc NOT MEMBER p.productCategories", - new Object[] { new Long(1) }); + } + + public void testEJBQL() throws Exception { + assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o", + compileEJBQL("SELECT OBJECT(o) FROM OrderX o")); + + assertEquals("SELECT t0_o.ORDER_NUMBER "+ + "FROM ORDER_DATA t0_o, "+ + "LINEITEMEJB t2_l, "+ + "PRODUCTCATEGORYEJB t1_pc, "+ + "PRODUCT_PRODUCT_CATEGORY t3_l_product_productCategories_R, "+ + "PRODUCT t4_l_product "+ + "WHERE (t0_o.ORDER_NUMBER = ? AND t1_pc.name = ?) "+ + "AND (t4_l_product.id=t3_l_product_productCategories_R.PRODUCT_ID "+ + "AND t1_pc.id=t3_l_product_productCategories_R.PRODUCT_CATEGORY_ID "+ + "AND t2_l.product=t4_l_product.id "+ + "AND t0_o.ORDER_NUMBER=t2_l.ORDER_NUMBER)", + compileEJBQL("SELECT OBJECT(o) FROM OrderX o, "+ + "IN(o.lineItems) l, "+ + "IN(l.product.productCategories) pc "+ + "WHERE o.ordernumber = ?1 and pc.name=?2", + Collection.class, new Class[] { Long.class, String.class })); + + assertEquals("SELECT DISTINCT t0_o.ORDER_NUMBER " + + "FROM ORDER_DATA t0_o, LINEITEMEJB t2_l " + + "WHERE t0_o.ORDER_NUMBER = ? OR EXISTS (SELECT t1_o_lineItems.id FROM LINEITEMEJB t1_o_lineItems " + + "WHERE t0_o.ORDER_NUMBER=t1_o_lineItems.ORDER_NUMBER AND t1_o_lineItems.id = t2_l.id)", + compileEJBQL("SELECT OBJECT(o) FROM OrderX o, LineItem l WHERE o.ordernumber = ?1 OR l MEMBER o.lineItems", + Set.class, new Class[] { Long.class })); + + assertEquals("SELECT DISTINCT t0_o.ORDER_NUMBER " + + "FROM ORDER_DATA t0_o, LINEITEMEJB t2_l " + + "WHERE t0_o.ORDER_NUMBER = ? OR NOT EXISTS (SELECT t1_o_lineItems.id FROM LINEITEMEJB t1_o_lineItems " + + "WHERE t0_o.ORDER_NUMBER=t1_o_lineItems.ORDER_NUMBER AND t1_o_lineItems.id = t2_l.id)", + compileEJBQL("SELECT OBJECT(o) FROM OrderX o, LineItem l WHERE o.ordernumber = ?1 OR l NOT MEMBER o.lineItems", + Set.class, new Class[] { Long.class })); + + assertEquals("SELECT DISTINCT t0_p.id " + + "FROM PRODUCT t0_p, PRODUCTCATEGORYEJB t3_pc " + + "WHERE t0_p.id = ? OR EXISTS (SELECT t2_p_productCategories_RELATION_.PRODUCT_CATEGORY_ID " + + "FROM PRODUCT_PRODUCT_CATEGORY t2_p_productCategories_RELATION_ " + + "WHERE t0_p.id=t2_p_productCategories_RELATION_.PRODUCT_ID " + + "AND t3_pc.id=t2_p_productCategories_RELATION_.PRODUCT_CATEGORY_ID)", + compileEJBQL("SELECT OBJECT(p) FROM Product p, ProductCategory pcWHERE p.id = ?1 OR pc MEMBER p.productCategories", + Set.class, new Class[] { Long.class })); + + assertEquals("SELECT DISTINCT t0_p.id " + + "FROM PRODUCT t0_p, PRODUCTCATEGORYEJB t3_pc " + + "WHERE t0_p.id = ? OR NOT EXISTS (SELECT t2_p_productCategories_RELATION_.PRODUCT_CATEGORY_ID " + + "FROM PRODUCT_PRODUCT_CATEGORY t2_p_productCategories_RELATION_ " + + "WHERE t0_p.id=t2_p_productCategories_RELATION_.PRODUCT_ID " + + "AND t3_pc.id=t2_p_productCategories_RELATION_.PRODUCT_CATEGORY_ID)", + compileEJBQL("SELECT OBJECT(p) FROM Product p, ProductCategory pcWHERE p.id = ?1 OR pc NOT MEMBER p.productCategories", + Set.class, new Class[] { Long.class })); + + assertEquals("SELECT DISTINCT t0_o.ORDER_NUMBER " + + "FROM ORDER_DATA t0_o " + + "WHERE t0_o.ORDER_NUMBER = ? OR EXISTS (SELECT t1_o_lineItems.id " + + "FROM LINEITEMEJB t1_o_lineItems " + + "WHERE t0_o.ORDER_NUMBER=t1_o_lineItems.ORDER_NUMBER)", + compileEJBQL("SELECT OBJECT(o) FROM OrderX o WHERE o.ordernumber = ?1 OR o.lineItems IS NOT EMPTY", + Set.class, new Class[] { Long.class })); + + OrderHome oh = (OrderHome) (new InitialContext()).lookup("commerce/Order"); + +// oh.getStuff( +// "SELECT OBJECT(o) " + +// "FROM OrderX o, LineItem l " + +// "WHERE o.ordernumber = ?1 OR l MEMBER o.lineItems", +// new Object[] { new Long(1) }); + +// oh.getStuff( +// "SELECT OBJECT(o) " + +// "FROM OrderX o, LineItem l " + +// "WHERE o.ordernumber = ?1 OR l NOT MEMBER o.lineItems", +// new Object[] { new Long(1) }); + +// oh.getStuff( +// "SELECT OBJECT(p) " + +// "FROM Product p, ProductCategory pc" + +// "WHERE p.id = ?1 OR pc MEMBER p.productCategories", +// new Object[] { new Long(1) }); + +// oh.getStuff( +// "SELECT OBJECT(p) " + +// "FROM Product p, ProductCategory pc" + +// "WHERE p.id = ?1 OR pc NOT MEMBER p.productCategories", +// new Object[] { new Long(1) }); + +// oh.getStuff( +// "SELECT OBJECT(o) " + +// "FROM OrderX o " + +// "WHERE o.ordernumber = ?1 OR o.lineItems IS NOT EMPTY", +// new Object[] { new Long(1) }); +// +// oh.getStuff( +// "SELECT OBJECT(l) " + +// "FROM LineItem l " + +// "WHERE l.order.billingAddress IS NULL", +// new Object[] { }); +// +// oh.getStuff( +// "SELECT OBJECT(l) " + +// "FROM LineItem l " + +// "WHERE l.product.name = ?1 OR l.product.productCategories IS EMPTY", +// new Object[] { "stuff" }); } } |