KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > commerce > LimitOffsetTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.commerce;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
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 JavaDoc[] params = { int.class, int.class };
50    private JDBCQueryMetaData queryMetaData;
51    private OrderHome orderHome;
52
53    public static Test suite() throws Exception JavaDoc {
54         return JBossTestCase.getDeploySetup(LimitOffsetTest.class, "cmp2-commerce.jar");
55    }
56
57
58    public LimitOffsetTest(String JavaDoc name) {
59       super(name);
60    }
61
62    public void setUpEJB() throws Exception JavaDoc
63    {
64       MBeanServer JavaDoc server = (MBeanServer JavaDoc) MBeanServerFactory.findMBeanServer(null).get(0);
65       ObjectName JavaDoc name = new ObjectName JavaDoc("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 JavaDoc getMethod()
77          {
78             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc getQLCompilerClass()
92          {
93             throw new UnsupportedOperationException JavaDoc();
94          }
95
96          public boolean isLazyResultSetLoading()
97          {
98             return false;
99          }
100       };
101
102       Context JavaDoc ctx = new InitialContext JavaDoc();
103       orderHome = (OrderHome) ctx.lookup("commerce/Order");
104
105       for (Iterator JavaDoc 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 JavaDoc(i));
115       }
116    }
117
118    public void tearDownEJB() throws Exception JavaDoc
119    {
120       for (Iterator JavaDoc 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 JavaDoc
129    {
130       compiler.compileJBossQL("SELECT OBJECT(o) FROM OrderX o", Collection JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.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 JavaDoc.class,
153             new Class JavaDoc[] { long.class }, queryMetaData);
154          fail("Expected Exception due to non-int argument");
155       }
156       catch (Exception JavaDoc e)
157       {
158          // OK
159
}
160    }
161
162    public void testLimitOffset() throws Exception JavaDoc
163    {
164       Set JavaDoc result;
165       result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o", new Object JavaDoc[] { } );
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 JavaDoc[] { new Integer JavaDoc(3) } );
169       checkKeys(result, new long[] { 100, 101, 102 });
170
171       result = orderHome.getStuff("SELECT OBJECT(o) FROM OrderX o OFFSET ?1", new Object JavaDoc[] { new Integer JavaDoc(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 JavaDoc[] { new Integer JavaDoc(0), new Integer JavaDoc(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 JavaDoc[] { new Integer JavaDoc(3), new Integer JavaDoc(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 JavaDoc[] { new Integer JavaDoc(6), new Integer JavaDoc(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 JavaDoc[] { new Integer JavaDoc(9), new Integer JavaDoc(3) } );
184       checkKeys(result, new long[] { 109 });
185    }
186
187    public void testFinderWithLimitOffset() throws Exception JavaDoc
188    {
189       Collection JavaDoc result;
190       result = orderHome.findWithLimitOffset(6, 3);
191       checkKeys(result, new long[] { 106, 107, 108 });
192    }
193
194    private void checkKeys(Collection JavaDoc c, long[] expected)
195    {
196       assertEquals(expected.length, c.size());
197       Set JavaDoc expectedSet = new HashSet JavaDoc(expected.length);
198       for (int i = 0; i < expected.length; i++)
199       {
200          long l = expected[i];
201          expectedSet.add(new Long JavaDoc(l));
202       }
203
204       Set JavaDoc actualSet = new HashSet JavaDoc(c.size());
205       for (Iterator JavaDoc 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