1 22 package org.jboss.test.cmp2.commerce; 23 24 import java.util.Collection ; 25 import java.util.Set ; 26 import java.util.Iterator ; 27 import java.util.HashSet ; 28 import java.lang.reflect.Method ; 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerFactory ; 31 import javax.management.ObjectName ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 35 import junit.framework.Test; 36 import org.jboss.ejb.EntityContainer; 37 import org.jboss.ejb.plugins.cmp.ejbql.Catalog; 38 import org.jboss.ejb.plugins.cmp.jdbc.JDBCEJBQLCompiler; 39 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData; 40 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData; 41 import org.jboss.mx.server.ServerConstants; 42 import org.jboss.mx.server.registry.MBeanEntry; 43 import org.jboss.mx.server.registry.MBeanRegistry; 44 import org.jboss.test.JBossTestCase; 45 import org.jboss.mx.util.MBeanProxyExt; 46 47 public class LimitOffsetTest extends net.sourceforge.junitejb.EJBTestCase { 48 private JDBCEJBQLCompiler compiler; 49 private Class [] params = { int.class, int.class }; 50 private JDBCQueryMetaData queryMetaData; 51 private OrderHome orderHome; 52 53 public static Test suite() throws Exception { 54 return JBossTestCase.getDeploySetup(LimitOffsetTest.class, "cmp2-commerce.jar"); 55 } 56 57 58 public LimitOffsetTest(String name) { 59 super(name); 60 } 61 62 public void setUpEJB() throws Exception 63 { 64 MBeanServer server = (MBeanServer ) MBeanServerFactory.findMBeanServer(null).get(0); 65 ObjectName name = new ObjectName ("jboss.j2ee:jndiName=commerce/Order,service=EJB"); 66 MBeanRegistry registry = (MBeanRegistry) MBeanProxyExt.create(MBeanRegistry.class, 67 ServerConstants.MBEAN_REGISTRY, 68 server); 69 MBeanEntry entry = registry.get(name); 70 EntityContainer container = (EntityContainer) entry.getResourceInstance(); 71 Catalog catalog = (Catalog) container.getEjbModule().getModuleData("CATALOG"); 72 compiler = new JDBCEJBQLCompiler(catalog); 73 74 queryMetaData = new JDBCQueryMetaData() 75 { 76 public Method getMethod() 77 { 78 throw new UnsupportedOperationException (); 79 } 80 81 public boolean isResultTypeMappingLocal() 82 { 83 return true; 84 } 85 86 public JDBCReadAheadMetaData getReadAhead() 87 { 88 return new JDBCReadAheadMetaData("on-load", 100, "*"); 89 } 90 91 public Class getQLCompilerClass() 92 { 93 throw new UnsupportedOperationException (); 94 } 95 96 public boolean isLazyResultSetLoading() 97 { 98 return false; 99 } 100 }; 101 102 Context ctx = new InitialContext (); 103 orderHome = (OrderHome) ctx.lookup("commerce/Order"); 104 105 for (Iterator i = orderHome.findAll().iterator(); i.hasNext(); ) 106 { 107 Order order = (Order) i.next(); 108 i.remove(); 109 order.remove(); 110 } 111 112 for (int i=100; i < 110; i++) 113 { 114 orderHome.create(new Long (i)); 115 } 116 } 117 118 public void tearDownEJB() throws Exception 119 { 120 for (Iterator i = orderHome.findAll().iterator(); i.hasNext(); ) 121 { 122 Order order = (Order) i.next(); 123 i.remove(); 124 order.remove(); 125 } 126 } 127 128 public void testCompiler() throws Exception 129 { 130 compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o", Collection .class, params, queryMetaData); 131 assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o", compiler.getSQL()); 132 assertEquals(0, compiler.getLimitParam()); 133 assertEquals(0, compiler.getOffsetParam()); 134 135 compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o OFFSET ?2", Collection .class, params, queryMetaData); 136 assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o", compiler.getSQL()); 137 assertEquals(2, compiler.getOffsetParam()); 138 assertEquals(0, compiler.getLimitParam()); 139 140 compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o LIMIT ?1", Collection .class, params, queryMetaData); 141 assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o", compiler.getSQL()); 142 assertEquals(0, compiler.getOffsetParam()); 143 assertEquals(1, compiler.getLimitParam()); 144 145 compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2", Collection .class, params, queryMetaData); 146 assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o", compiler.getSQL()); 147 assertEquals(1, compiler.getOffsetParam()); 148 assertEquals(2, compiler.getLimitParam()); 149 150 try 151 { 152 compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o OFFSET ?1", Collection .class, 153 new Class [] { long.class }, queryMetaData); 154 fail("Expected Exception due to non-int argument"); 155 } 156 catch (Exception e) 157 { 158 } 160 } 161 162 public void testLimitOffset() throws Exception 163 { 164 Set result; 165 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o", new Object [] { } ); 166 checkKeys(result, new long[] { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109}); 167 168 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o LIMIT ?1", new Object [] { new Integer (3) } ); 169 checkKeys(result, new long[] { 100, 101, 102 }); 170 171 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o OFFSET ?1", new Object [] { new Integer (3) } ); 172 checkKeys(result, new long[] { 103, 104, 105, 106, 107, 108, 109 }); 173 174 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2", new Object [] { new Integer (0), new Integer (3) } ); 175 checkKeys(result, new long[] { 100, 101, 102 }); 176 177 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2", new Object [] { new Integer (3), new Integer (3) } ); 178 checkKeys(result, new long[] { 103, 104, 105 }); 179 180 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2", new Object [] { new Integer (6), new Integer (3) } ); 181 checkKeys(result, new long[] { 106, 107, 108 }); 182 183 result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2", new Object [] { new Integer (9), new Integer (3) } ); 184 checkKeys(result, new long[] { 109 }); 185 } 186 187 public void testFinderWithLimitOffset() throws Exception 188 { 189 Collection result; 190 result = orderHome.findWithLimitOffset(6, 3); 191 checkKeys(result, new long[] { 106, 107, 108 }); 192 } 193 194 private void checkKeys(Collection c, long[] expected) 195 { 196 assertEquals(expected.length, c.size()); 197 Set expectedSet = new HashSet (expected.length); 198 for (int i = 0; i < expected.length; i++) 199 { 200 long l = expected[i]; 201 expectedSet.add(new Long (l)); 202 } 203 204 Set actualSet = new HashSet (c.size()); 205 for (Iterator iterator = c.iterator(); iterator.hasNext();) 206 { 207 Order order = (Order) iterator.next(); 208 actualSet.add(order.getPrimaryKey()); 209 } 210 211 assertEquals(expectedSet, actualSet); 212 } 213 } 214 | Popular Tags |