KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > stats > StatsTest


1 //$Id: StatsTest.java,v 1.1 2005/05/09 16:09:37 epbernard Exp $
2
package org.hibernate.test.stats;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6 import org.hibernate.FetchMode;
7 import org.hibernate.Session;
8 import org.hibernate.SessionFactory;
9 import org.hibernate.Transaction;
10 import org.hibernate.mapping.Collection;
11 import org.hibernate.stat.Statistics;
12 import org.hibernate.test.TestCase;
13
14 import java.util.HashSet JavaDoc;
15
16 /**
17  * Show the difference between fetch and load
18  *
19  * @author Emmanuel Bernard
20  */

21 public class StatsTest extends TestCase {
22     public void testCollectionFetchVsLoad() throws Exception JavaDoc {
23         Session s = openSession();
24         Transaction tx = s.beginTransaction();
25         Statistics stats = getSessions().getStatistics();
26         stats.clear();
27         boolean isStats = stats.isStatisticsEnabled();
28         stats.setStatisticsEnabled(true);
29         Continent europe = fillDb(s);
30         tx.commit();
31         s.clear();
32         tx = s.beginTransaction();
33         assertEquals(0, stats.getCollectionLoadCount() );
34         assertEquals(0, stats.getCollectionFetchCount() );
35         Continent europe2 = (Continent) s.get( Continent.class, europe.getId() );
36         assertEquals("Lazy true: no collection should be loaded", 0, stats.getCollectionLoadCount() );
37         assertEquals( 0, stats.getCollectionFetchCount() );
38         europe2.getCountries().size();
39         assertEquals( 1, stats.getCollectionLoadCount() );
40         assertEquals("Explicit fetch of the collection state", 1, stats.getCollectionFetchCount() );
41         tx.commit();
42         s.close();
43
44         s = openSession();
45         tx = s.beginTransaction();
46         stats.clear();
47         europe = fillDb(s);
48         tx.commit();
49         s.clear();
50         tx = s.beginTransaction();
51         assertEquals( 0, stats.getCollectionLoadCount() );
52         assertEquals( 0, stats.getCollectionFetchCount() );
53         europe2 = (Continent) s.createQuery(
54                 "from " + Continent.class.getName() + " a join fetch a.countries where a.id = " + europe.getId()
55             ).uniqueResult();
56         assertEquals( 1, stats.getCollectionLoadCount() );
57         assertEquals( "collection should be loaded in the same query as its parent", 0, stats.getCollectionFetchCount() );
58         tx.commit();
59         s.close();
60
61         Collection coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
62         coll.setFetchMode(FetchMode.JOIN);
63         coll.setLazy(false);
64         SessionFactory sf = getCfg().buildSessionFactory();
65         stats = sf.getStatistics();
66         stats.clear();
67         stats.setStatisticsEnabled(true);
68         s = sf.openSession();
69         tx = s.beginTransaction();
70         europe = fillDb(s);
71         tx.commit();
72         s.clear();
73         tx = s.beginTransaction();
74         assertEquals( 0, stats.getCollectionLoadCount() );
75         assertEquals( 0, stats.getCollectionFetchCount() );
76         europe2 = (Continent) s.get( Continent.class, europe.getId() );
77         assertEquals( 1, stats.getCollectionLoadCount() );
78         assertEquals( "Should do direct load, not indirect second load when lazy false and JOIN", 0, stats.getCollectionFetchCount() );
79         tx.commit();
80         s.close();
81         sf.close();
82
83         coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
84         coll.setFetchMode(FetchMode.SELECT);
85         coll.setLazy(false);
86         sf = getCfg().buildSessionFactory();
87         stats = sf.getStatistics();
88         stats.clear();
89         stats.setStatisticsEnabled(true);
90         s = sf.openSession();
91         tx = s.beginTransaction();
92         europe = fillDb(s);
93         tx.commit();
94         s.clear();
95         tx = s.beginTransaction();
96         assertEquals( 0, stats.getCollectionLoadCount() );
97         assertEquals( 0, stats.getCollectionFetchCount() );
98         europe2 = (Continent) s.get( Continent.class, europe.getId() );
99         assertEquals( 1, stats.getCollectionLoadCount() );
100         assertEquals( "Should do explicit collection load, not part of the first one", 1, stats.getCollectionFetchCount() );
101         tx.commit();
102         s.close();
103         sf.close();
104
105         stats.setStatisticsEnabled(isStats);
106     }
107
108     private Continent fillDb(Session s) {
109         Continent europe = new Continent();
110         europe.setName("Europe");
111         Country france = new Country();
112         france.setName("France");
113         europe.setCountries( new HashSet JavaDoc() );
114         europe.getCountries().add(france);
115         s.persist(france);
116         s.persist(europe);
117         return europe;
118     }
119
120     protected String JavaDoc[] getMappings() {
121         return new String JavaDoc[] {
122             "stats/Continent.hbm.xml"
123         };
124     }
125
126     public StatsTest(String JavaDoc x) {
127         super(x);
128     }
129
130     public static Test suite() {
131         return new TestSuite(StatsTest.class);
132     }
133 }
134
Popular Tags