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 org.jboss.test.util.ejb.EJBTestCase;
025: import junit.framework.Test;
026: import org.jboss.test.JBossTestCase;
027:
028: import javax.naming.InitialContext;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.ArrayList;
032:
033: /**
034: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
035: * @version <tt>$Revision: 61780 $</tt>
036: */
037: public class LazyResultSetLoadingTest extends EJBTestCase {
038: public static Test suite() throws Exception {
039: return JBossTestCase.getDeploySetup(CascadeDeleteTest.class,
040: "cmp2-commerce.jar");
041: }
042:
043: public LazyResultSetLoadingTest(String localName) {
044: super (localName);
045: }
046:
047: private OrderHome getOrderHome() {
048: try {
049: InitialContext jndiContext = new InitialContext();
050: return (OrderHome) jndiContext.lookup("commerce/Order");
051: } catch (Exception e) {
052: e.printStackTrace();
053: fail("Exception in getOrderHome: " + e.getMessage());
054: }
055: return null;
056: }
057:
058: private LineItemHome getLineItemHome() {
059: try {
060: InitialContext jndiContext = new InitialContext();
061: return (LineItemHome) jndiContext
062: .lookup("commerce/LineItem");
063: } catch (Exception e) {
064: e.printStackTrace();
065: fail("Exception in getLineItemHome: " + e.getMessage());
066: }
067: return null;
068: }
069:
070: public void setUpEJB() throws Exception {
071: OrderHome oh = getOrderHome();
072: Order o = oh.create(new Long(1));
073:
074: LineItemHome lih = getLineItemHome();
075: LineItem li = lih.create(new Long(11));
076: o.getLineItems().add(li);
077:
078: li = lih.create(new Long(22));
079: o.getLineItems().add(li);
080:
081: li = lih.create(new Long(33));
082: o.getLineItems().add(li);
083: }
084:
085: public void tearDownEJB() throws Exception {
086: getOrderHome().remove(new Long(1));
087: }
088:
089: public void testLazyResultSetLoading() throws Exception {
090: final OrderHome oh = getOrderHome();
091:
092: // empty result
093: Collection col = oh.selectLazy(
094: "select object(o) from Address o where o.state='CA'",
095: null);
096: assertTrue("Expected empty collection but got " + col.size(),
097: col.isEmpty());
098:
099: // collection of results
100: col = oh.selectLazy("select object(o) from LineItem o", null);
101: assertTrue("Expected 3 line items but got " + col.size(),
102: 3 == col.size());
103:
104: Iterator i = col.iterator();
105: LineItem removed = (LineItem) i.next();
106: i.remove();
107: assertTrue("Expected 2 line items but got " + col.size(),
108: 2 == col.size());
109:
110: Collection firstPassCol = new ArrayList(2);
111: while (i.hasNext()) {
112: firstPassCol.add(i.next());
113: }
114:
115: Collection secondPassCol = new ArrayList(3);
116: i = col.iterator();
117: while (i.hasNext()) {
118: final LineItem li = (LineItem) i.next();
119: assertTrue(firstPassCol.contains(li));
120: secondPassCol.add(li);
121: }
122: assertTrue("Expected 2 line items but got "
123: + secondPassCol.size(), secondPassCol.size() == 2);
124: secondPassCol.add(removed);
125: assertTrue("Expected 3 line items but got "
126: + secondPassCol.size(), secondPassCol.size() == 3);
127:
128: // limit & offset
129: col = oh.selectLazy(
130: "select object(o) from LineItem o offset 1 limit 2",
131: null);
132: assertTrue("Expected 2 line items but got " + col.size(), col
133: .size() == 2);
134: int count = 0;
135: for (i = col.iterator(); i.hasNext();) {
136: i.next();
137: ++count;
138: }
139: assertTrue("Expected 2 but got " + count, count == 2);
140: }
141: }
|