1 package org.hibernate.test.legacy; 3 4 5 import junit.framework.Test; 6 import junit.framework.TestSuite; 7 import junit.textui.TestRunner; 8 9 import org.hibernate.Query; 10 import org.hibernate.Session; 11 import org.hibernate.SessionFactory; 12 import org.hibernate.Transaction; 13 import org.hibernate.stat.Statistics; 14 import org.hibernate.test.TestCase; 15 16 19 public class StatisticsTest extends TestCase { 20 21 public StatisticsTest(String x) { 22 super(x); 23 } 24 25 public void testSessionStats() throws Exception { 26 27 SessionFactory sf = getSessions(); 28 Statistics stats = sf.getStatistics(); 29 boolean isStats = stats.isStatisticsEnabled(); 30 stats.clear(); 31 stats.setStatisticsEnabled(true); 32 Session s = sf.openSession(); 33 assertEquals( 1, stats.getSessionOpenCount() ); 34 s.close(); 35 assertEquals( 1, stats.getSessionCloseCount() ); 36 s = sf.openSession(); 37 Transaction tx = s.beginTransaction(); 38 A a = new A(); 39 a.setName("mya"); 40 s.save(a); 41 a.setName("b"); 42 tx.commit(); 43 s.close(); 44 assertEquals( 1, stats.getFlushCount() ); 45 s = sf.openSession(); 46 tx = s.beginTransaction(); 47 String hql = "from " + A.class.getName(); 48 Query q = s.createQuery(hql); 49 q.list(); 50 tx.commit(); 51 s.close(); 52 assertEquals(1, stats.getQueryExecutionCount() ); 53 assertEquals(1, stats.getQueryStatistics(hql).getExecutionCount() ); 54 55 stats.setStatisticsEnabled(isStats); 56 } 57 58 public String [] getMappings() { 59 return new String [] { "legacy/ABC.hbm.xml", "legacy/ABCExtends.hbm.xml" }; 60 } 61 62 public static Test suite() { 63 return new TestSuite(StatisticsTest.class); 64 } 65 66 69 public static void main(String [] args) throws Exception { 70 TestRunner.run( suite() ); 71 } 72 73 } 74 | Popular Tags |