KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > iterate > IterateTest


1 //$Id: IterateTest.java,v 1.4 2005/06/16 13:51:33 oneovthafew Exp $
2
package org.hibernate.test.iterate;
3
4 import java.util.Iterator JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.ScrollableResults;
11 import org.hibernate.Session;
12 import org.hibernate.Transaction;
13 import org.hibernate.cfg.Configuration;
14 import org.hibernate.cfg.Environment;
15 import org.hibernate.test.TestCase;
16
17 /**
18  * @author Gavin King
19  */

20 public class IterateTest extends TestCase {
21     
22     public IterateTest(String JavaDoc str) {
23         super(str);
24     }
25     
26     public void testIterate() throws Exception JavaDoc {
27         getSessions().getStatistics().clear();
28         Session s = openSession();
29         Transaction t = s.beginTransaction();
30         Item i1 = new Item("foo");
31         Item i2 = new Item("bar");
32         s.persist("Item", i1);
33         s.persist("Item", i2);
34         t.commit();
35         s.close();
36
37         s = openSession();
38         t = s.beginTransaction();
39         Iterator JavaDoc iter = s.getNamedQuery("Item.nameDesc").iterate();
40         i1 = (Item) iter.next();
41         i2 = (Item) iter.next();
42         assertFalse( Hibernate.isInitialized(i1) );
43         assertFalse( Hibernate.isInitialized(i2) );
44         i1.getName();
45         i2.getName();
46         assertFalse( Hibernate.isInitialized(i1) );
47         assertFalse( Hibernate.isInitialized(i2) );
48         assertEquals( i1.getName(), "foo" );
49         assertEquals( i2.getName(), "bar" );
50         Hibernate.initialize(i1);
51         assertFalse( iter.hasNext() );
52         s.delete(i1);
53         s.delete(i2);
54         t.commit();
55         s.close();
56         assertEquals( getSessions().getStatistics().getEntityFetchCount(), 2 );
57     }
58     
59     public void testScroll() throws Exception JavaDoc {
60         getSessions().getStatistics().clear();
61         Session s = openSession();
62         Transaction t = s.beginTransaction();
63         Item i1 = new Item("foo");
64         Item i2 = new Item("bar");
65         s.persist("Item", i1);
66         s.persist("Item", i2);
67         t.commit();
68         s.close();
69
70         s = openSession();
71         t = s.beginTransaction();
72         ScrollableResults sr = s.getNamedQuery("Item.nameDesc").scroll();
73         assertTrue( sr.next() );
74         i1 = (Item) sr.get(0);
75         assertTrue( sr.next() );
76         i2 = (Item) sr.get(0);
77         assertTrue( Hibernate.isInitialized(i1) );
78         assertTrue( Hibernate.isInitialized(i2) );
79         assertEquals( i1.getName(), "foo" );
80         assertEquals( i2.getName(), "bar" );
81         assertFalse( sr.next() );
82         s.delete(i1);
83         s.delete(i2);
84         t.commit();
85         s.close();
86         assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
87     }
88     
89     protected String JavaDoc[] getMappings() {
90         return new String JavaDoc[] { "iterate/Item.hbm.xml" };
91     }
92
93     public static Test suite() {
94         return new TestSuite(IterateTest.class);
95     }
96
97     protected void configure(Configuration cfg) {
98         super.configure( cfg );
99         cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
100         cfg.setProperty( Environment.CACHE_REGION_PREFIX, "foo" );
101         cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
102         cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
103     }
104 }
105
106
Popular Tags