001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.commerce;
023:
024: import java.util.Collection;
025: import java.util.Set;
026: import java.util.Iterator;
027: import java.util.HashSet;
028: import java.lang.reflect.Method;
029: import javax.management.MBeanServer;
030: import javax.management.MBeanServerFactory;
031: import javax.management.ObjectName;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034:
035: import junit.framework.Test;
036: import org.jboss.ejb.EntityContainer;
037: import org.jboss.ejb.plugins.cmp.ejbql.Catalog;
038: import org.jboss.ejb.plugins.cmp.jdbc.JDBCEJBQLCompiler;
039: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
040: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
041: import org.jboss.mx.server.ServerConstants;
042: import org.jboss.mx.server.registry.MBeanEntry;
043: import org.jboss.mx.server.registry.MBeanRegistry;
044: import org.jboss.test.JBossTestCase;
045: import org.jboss.mx.util.MBeanProxyExt;
046:
047: public class LimitOffsetTest extends
048: org.jboss.test.util.ejb.EJBTestCase {
049: private JDBCEJBQLCompiler compiler;
050: private Class[] params = { int.class, int.class };
051: private JDBCQueryMetaData queryMetaData;
052: private OrderHome orderHome;
053:
054: public static Test suite() throws Exception {
055: return JBossTestCase.getDeploySetup(LimitOffsetTest.class,
056: "cmp2-commerce.jar");
057: }
058:
059: public LimitOffsetTest(String name) {
060: super (name);
061: }
062:
063: public void setUpEJB() throws Exception {
064: MBeanServer server = (MBeanServer) MBeanServerFactory
065: .findMBeanServer(null).get(0);
066: ObjectName name = new ObjectName(
067: "jboss.j2ee:jndiName=commerce/Order,service=EJB");
068: MBeanRegistry registry = (MBeanRegistry) MBeanProxyExt.create(
069: MBeanRegistry.class, ServerConstants.MBEAN_REGISTRY,
070: server);
071: MBeanEntry entry = registry.get(name);
072: EntityContainer container = (EntityContainer) entry
073: .getResourceInstance();
074: Catalog catalog = (Catalog) container.getEjbModule()
075: .getModuleData("CATALOG");
076: compiler = new JDBCEJBQLCompiler(catalog);
077:
078: queryMetaData = new JDBCQueryMetaData() {
079: public Method getMethod() {
080: throw new UnsupportedOperationException();
081: }
082:
083: public boolean isResultTypeMappingLocal() {
084: return true;
085: }
086:
087: public JDBCReadAheadMetaData getReadAhead() {
088: return new JDBCReadAheadMetaData("on-load", 100, "*");
089: }
090:
091: public Class getQLCompilerClass() {
092: throw new UnsupportedOperationException();
093: }
094:
095: public boolean isLazyResultSetLoading() {
096: return false;
097: }
098: };
099:
100: Context ctx = new InitialContext();
101: orderHome = (OrderHome) ctx.lookup("commerce/Order");
102:
103: for (Iterator i = orderHome.findAll().iterator(); i.hasNext();) {
104: Order order = (Order) i.next();
105: i.remove();
106: order.remove();
107: }
108:
109: for (int i = 100; i < 110; i++) {
110: orderHome.create(new Long(i));
111: }
112: }
113:
114: public void tearDownEJB() throws Exception {
115: for (Iterator i = orderHome.findAll().iterator(); i.hasNext();) {
116: Order order = (Order) i.next();
117: i.remove();
118: order.remove();
119: }
120: }
121:
122: public void testCompiler() throws Exception {
123: compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o",
124: Collection.class, params, queryMetaData);
125: assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o",
126: compiler.getSQL());
127: assertEquals(0, compiler.getLimitParam());
128: assertEquals(0, compiler.getOffsetParam());
129:
130: compiler.compileJBossQL(
131: "SELECT OBJECT(o) FROM OrderX o OFFSET ?2",
132: Collection.class, params, queryMetaData);
133: assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o",
134: compiler.getSQL());
135: assertEquals(2, compiler.getOffsetParam());
136: assertEquals(0, compiler.getLimitParam());
137:
138: compiler.compileJBossQL(
139: "SELECT OBJECT(o) FROM OrderX o LIMIT ?1",
140: Collection.class, params, queryMetaData);
141: assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o",
142: compiler.getSQL());
143: assertEquals(0, compiler.getOffsetParam());
144: assertEquals(1, compiler.getLimitParam());
145:
146: compiler.compileJBossQL(
147: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2",
148: Collection.class, params, queryMetaData);
149: assertEquals("SELECT t0_o.ORDER_NUMBER FROM ORDER_DATA t0_o",
150: compiler.getSQL());
151: assertEquals(1, compiler.getOffsetParam());
152: assertEquals(2, compiler.getLimitParam());
153:
154: try {
155: compiler.compileJBossQL(
156: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1",
157: Collection.class, new Class[] { long.class },
158: queryMetaData);
159: fail("Expected Exception due to non-int argument");
160: } catch (Exception e) {
161: // OK
162: }
163: }
164:
165: public void testLimitOffset() throws Exception {
166: Set result;
167: result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o",
168: new Object[] {});
169: checkKeys(result, new long[] { 100, 101, 102, 103, 104, 105,
170: 106, 107, 108, 109 });
171:
172: result = orderHome.getStuff(
173: "SELECT OBJECT(o) FROM OrderX o LIMIT ?1",
174: new Object[] { new Integer(3) });
175: checkKeys(result, new long[] { 100, 101, 102 });
176:
177: result = orderHome.getStuff(
178: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1",
179: new Object[] { new Integer(3) });
180: checkKeys(result, new long[] { 103, 104, 105, 106, 107, 108,
181: 109 });
182:
183: result = orderHome.getStuff(
184: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2",
185: new Object[] { new Integer(0), new Integer(3) });
186: checkKeys(result, new long[] { 100, 101, 102 });
187:
188: result = orderHome.getStuff(
189: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2",
190: new Object[] { new Integer(3), new Integer(3) });
191: checkKeys(result, new long[] { 103, 104, 105 });
192:
193: result = orderHome.getStuff(
194: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2",
195: new Object[] { new Integer(6), new Integer(3) });
196: checkKeys(result, new long[] { 106, 107, 108 });
197:
198: result = orderHome.getStuff(
199: "SELECT OBJECT(o) FROM OrderX o OFFSET ?1 LIMIT ?2",
200: new Object[] { new Integer(9), new Integer(3) });
201: checkKeys(result, new long[] { 109 });
202: }
203:
204: public void testFinderWithLimitOffset() throws Exception {
205: Collection result;
206: result = orderHome.findWithLimitOffset(6, 3);
207: checkKeys(result, new long[] { 106, 107, 108 });
208: }
209:
210: private void checkKeys(Collection c, long[] expected) {
211: assertEquals(expected.length, c.size());
212: Set expectedSet = new HashSet(expected.length);
213: for (int i = 0; i < expected.length; i++) {
214: long l = expected[i];
215: expectedSet.add(new Long(l));
216: }
217:
218: Set actualSet = new HashSet(c.size());
219: for (Iterator iterator = c.iterator(); iterator.hasNext();) {
220: Order order = (Order) iterator.next();
221: actualSet.add(order.getPrimaryKey());
222: }
223:
224: assertEquals(expectedSet, actualSet);
225: }
226: }
|