1 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 ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.ArrayList ; 32 33 37 public class LazyResultSetLoadingTest extends EJBTestCase 38 { 39 public static Test suite() throws Exception 40 { 41 return JBossTestCase.getDeploySetup(CascadeDeleteTest.class, "cmp2-commerce.jar"); 42 } 43 44 public LazyResultSetLoadingTest(String localName) 45 { 46 super(localName); 47 } 48 49 private OrderHome getOrderHome() 50 { 51 try 52 { 53 InitialContext jndiContext = new InitialContext (); 54 return (OrderHome) jndiContext.lookup("commerce/Order"); 55 } 56 catch(Exception 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 jndiContext = new InitialContext (); 69 return (LineItemHome) jndiContext.lookup("commerce/LineItem"); 70 } 71 catch(Exception e) 72 { 73 e.printStackTrace(); 74 fail("Exception in getLineItemHome: " + e.getMessage()); 75 } 76 return null; 77 } 78 79 public void setUpEJB() throws Exception 80 { 81 OrderHome oh = getOrderHome(); 82 Order o = oh.create(new Long (1)); 83 84 LineItemHome lih = getLineItemHome(); 85 LineItem li = lih.create(new Long (11)); 86 o.getLineItems().add(li); 87 88 li = lih.create(new Long (22)); 89 o.getLineItems().add(li); 90 91 li = lih.create(new Long (33)); 92 o.getLineItems().add(li); 93 } 94 95 public void tearDownEJB() throws Exception 96 { 97 getOrderHome().remove(new Long (1)); 98 } 99 100 public void testLazyResultSetLoading() throws Exception 101 { 102 final OrderHome oh = getOrderHome(); 103 104 Collection 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 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 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 firstPassCol = new ArrayList (2); 118 while(i.hasNext()) 119 { 120 firstPassCol.add(i.next()); 121 } 122 123 Collection secondPassCol = new ArrayList (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 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 |