KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sourceforge.junitejb.EJBTestCase;
25 import junit.framework.Test;
26 import org.jboss.test.JBossTestCase;
27
28 import javax.naming.InitialContext JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 /**
34  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
35  * @version <tt>$Revision: 37406 $</tt>
36  */

37 public class LazyResultSetLoadingTest extends EJBTestCase
38 {
39    public static Test suite() throws Exception JavaDoc
40    {
41       return JBossTestCase.getDeploySetup(CascadeDeleteTest.class, "cmp2-commerce.jar");
42    }
43
44    public LazyResultSetLoadingTest(String JavaDoc localName)
45    {
46       super(localName);
47    }
48
49    private OrderHome getOrderHome()
50    {
51       try
52       {
53          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
54          return (OrderHome) jndiContext.lookup("commerce/Order");
55       }
56       catch(Exception JavaDoc e)
57       {
58          e.printStackTrace();
59          fail("Exception in getOrderHome: " + e.getMessage());
60       }
61       return null;
62    }
63
64    private LineItemHome getLineItemHome()
65    {
66       try
67       {
68          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
69          return (LineItemHome) jndiContext.lookup("commerce/LineItem");
70       }
71       catch(Exception JavaDoc e)
72       {
73          e.printStackTrace();
74          fail("Exception in getLineItemHome: " + e.getMessage());
75       }
76       return null;
77    }
78
79    public void setUpEJB() throws Exception JavaDoc
80    {
81       OrderHome oh = getOrderHome();
82       Order o = oh.create(new Long JavaDoc(1));
83
84       LineItemHome lih = getLineItemHome();
85       LineItem li = lih.create(new Long JavaDoc(11));
86       o.getLineItems().add(li);
87
88       li = lih.create(new Long JavaDoc(22));
89       o.getLineItems().add(li);
90
91       li = lih.create(new Long JavaDoc(33));
92       o.getLineItems().add(li);
93    }
94
95    public void tearDownEJB() throws Exception JavaDoc
96    {
97       getOrderHome().remove(new Long JavaDoc(1));
98    }
99
100    public void testLazyResultSetLoading() throws Exception JavaDoc
101    {
102       final OrderHome oh = getOrderHome();
103
104       // empty result
105
Collection JavaDoc col = oh.selectLazy("select object(o) from Address o where o.state='CA'", null);
106       assertTrue("Expected empty collection but got " + col.size(), col.isEmpty());
107
108       // collection of results
109
col = oh.selectLazy("select object(o) from LineItem o", null);
110       assertTrue("Expected 3 line items but got " + col.size(), 3 == col.size());
111
112       Iterator JavaDoc i = col.iterator();
113       LineItem removed = (LineItem)i.next();
114       i.remove();
115       assertTrue("Expected 2 line items but got " + col.size(), 2 == col.size());
116
117       Collection JavaDoc firstPassCol = new ArrayList JavaDoc(2);
118       while(i.hasNext())
119       {
120          firstPassCol.add(i.next());
121       }
122
123       Collection JavaDoc secondPassCol = new ArrayList JavaDoc(3);
124       i = col.iterator();
125       while(i.hasNext())
126       {
127          final LineItem li = (LineItem)i.next();
128          assertTrue(firstPassCol.contains(li));
129          secondPassCol.add(li);
130       }
131       assertTrue("Expected 2 line items but got " + secondPassCol.size(), secondPassCol.size() == 2);
132       secondPassCol.add(removed);
133       assertTrue("Expected 3 line items but got " + secondPassCol.size(), secondPassCol.size() == 3);
134
135       // limit & offset
136
col = oh.selectLazy("select object(o) from LineItem o offset 1 limit 2", null);
137       assertTrue("Expected 2 line items but got " + col.size(), col.size() == 2);
138       int count = 0;
139       for(i = col.iterator(); i.hasNext();)
140       {
141          i.next();
142          ++count;
143       }
144       assertTrue("Expected 2 but got " + count, count == 2);
145    }
146 }
147
Popular Tags