Source code: org/hibernate/test/stats/SessionStatsTest.java
1 //$Id: SessionStatsTest.java 7688 2005-07-29 21:43:18Z epbernard $
2 package org.hibernate.test.stats;
3
4 import java.util.HashSet;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8 import org.hibernate.Hibernate;
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.stat.SessionStatistics;
12 import org.hibernate.stat.Statistics;
13 import org.hibernate.test.TestCase;
14
15 /**
16 * @author Emmanuel Bernard
17 */
18 public class SessionStatsTest extends TestCase {
19 public void testSessionStatistics() throws Exception {
20 Session s = openSession();
21 Transaction tx = s.beginTransaction();
22 Statistics stats = getSessions().getStatistics();
23 stats.clear();
24 boolean isStats = stats.isStatisticsEnabled();
25 stats.setStatisticsEnabled(true);
26 Continent europe = fillDb(s);
27 tx.commit();
28 s.clear();
29 tx = s.beginTransaction();
30 SessionStatistics sessionStats = s.getStatistics();
31 assertEquals( 0, sessionStats.getEntityKeys().size() );
32 assertEquals( 0, sessionStats.getEntityCount() );
33 assertEquals( 0, sessionStats.getCollectionKeys().size() );
34 assertEquals( 0, sessionStats.getCollectionCount() );
35 europe = (Continent) s.get( Continent.class, europe.getId() );
36 Hibernate.initialize( europe.getCountries() );
37 Hibernate.initialize( europe.getCountries().iterator().next() );
38 assertEquals( 2, sessionStats.getEntityKeys().size() );
39 assertEquals( 2, sessionStats.getEntityCount() );
40 assertEquals( 1, sessionStats.getCollectionKeys().size() );
41 assertEquals( 1, sessionStats.getCollectionCount() );
42 tx.commit();
43 s.close();
44
45 stats.setStatisticsEnabled( isStats);
46
47 }
48
49 private Continent fillDb(Session s) {
50 Continent europe = new Continent();
51 europe.setName("Europe");
52 Country france = new Country();
53 france.setName("France");
54 europe.setCountries( new HashSet() );
55 europe.getCountries().add(france);
56 s.persist(france);
57 s.persist(europe);
58 return europe;
59 }
60
61 protected String[] getMappings() {
62 return new String[] {
63 "stats/Continent2.hbm.xml"
64 };
65 }
66
67 public SessionStatsTest(String x) {
68 super(x);
69 }
70
71 public static Test suite() {
72 return new TestSuite(SessionStatsTest.class);
73 }
74 }