KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > querycache > QueryCacheTest


1 //$Id: QueryCacheTest.java,v 1.12 2005/05/26 15:26:57 oneovthafew Exp $
2
package org.hibernate.test.querycache;
3
4 import java.util.List JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.cfg.Configuration;
13 import org.hibernate.cfg.Environment;
14 import org.hibernate.dialect.MySQLDialect;
15 import org.hibernate.dialect.SQLServerDialect;
16 import org.hibernate.stat.EntityStatistics;
17 import org.hibernate.stat.QueryStatistics;
18 import org.hibernate.test.TestCase;
19
20 /**
21  * @author Gavin King
22  */

23 public class QueryCacheTest extends TestCase {
24     
25     public QueryCacheTest(String JavaDoc str) {
26         super(str);
27     }
28     
29     public void testQueryCacheInvalidation() throws Exception JavaDoc {
30         
31         getSessions().evictQueries();
32         
33         Session s = openSession();
34         Transaction t = s.beginTransaction();
35         s.createQuery("from Item i where i.name='widget'").setCacheable(true).list();
36         Item i = new Item();
37         i.setName("widget");
38         i.setDescription("A really top-quality, full-featured widget.");
39         s.save(i);
40         t.commit();
41         s.close();
42         
43         QueryStatistics qs = s.getSessionFactory()
44             .getStatistics()
45             .getQueryStatistics("from org.hibernate.test.querycache.Item i where i.name='widget'");
46         EntityStatistics es = s.getSessionFactory()
47             .getStatistics()
48             .getEntityStatistics( Item.class.getName() );
49
50         Thread.sleep(200);
51
52         s = openSession();
53         t = s.beginTransaction();
54         List JavaDoc result = s.createQuery("from Item i where i.name='widget'").setCacheable(true).list();
55         assertEquals( result.size(), 1 );
56         t.commit();
57         s.close();
58         
59         assertEquals( qs.getCacheHitCount(), 0 );
60                 
61         s = openSession();
62         t = s.beginTransaction();
63         result = s.createQuery("from Item i where i.name='widget'").setCacheable(true).list();
64         assertEquals( result.size(), 1 );
65         t.commit();
66         s.close();
67         
68         assertEquals( qs.getCacheHitCount(), 1 );
69         
70         s = openSession();
71         t = s.beginTransaction();
72         result = s.createQuery("from Item i where i.name='widget'").setCacheable(true).list();
73         assertEquals( result.size(), 1 );
74         assertTrue( Hibernate.isInitialized( result.get(0) ) );
75         i = (Item) result.get(0);
76         i.setName("Widget");
77         t.commit();
78         s.close();
79         
80         assertEquals( qs.getCacheHitCount(), 2 );
81         assertEquals( qs.getCacheMissCount(), 2 );
82
83         Thread.sleep(200);
84
85         s = openSession();
86         t = s.beginTransaction();
87         result = s.createQuery("from Item i where i.name='widget'").setCacheable(true).list();
88         if ( (!(getDialect() instanceof MySQLDialect)) && (!(getDialect() instanceof SQLServerDialect)) ) assertEquals( result.size(), 0 ); //MySQL and SQLServer is case insensitive on strings
89
i = (Item) s.get( Item.class, new Long JavaDoc(i.getId()) );
90         assertEquals( i.getName(), "Widget" );
91         
92         s.delete(i);
93         t.commit();
94         s.close();
95
96         assertEquals( qs.getCacheHitCount(), 2 );
97         assertEquals( qs.getCacheMissCount(), 3 );
98         assertEquals( qs.getCachePutCount(), 3 );
99         assertEquals( qs.getExecutionCount(), 3 );
100         assertEquals( es.getFetchCount(), 0 ); //check that it was being cached
101

102     }
103
104     public void testProjectionCache() throws Exception JavaDoc {
105
106         getSessions().evictQueries();
107         
108         Session s = openSession();
109         s.getSessionFactory().getStatistics().clear();
110         Transaction t = s.beginTransaction();
111         s.createQuery("select i.description from Item i where i.name='widget'").setCacheable(true).list();
112         Item i = new Item();
113         i.setName("widget");
114         i.setDescription("A really top-quality, full-featured widget.");
115         s.save(i);
116         t.commit();
117         s.close();
118
119         QueryStatistics qs = s.getSessionFactory()
120             .getStatistics()
121             .getQueryStatistics("select i.description from org.hibernate.test.querycache.Item i where i.name='widget'");
122         EntityStatistics es = s.getSessionFactory()
123             .getStatistics()
124             .getEntityStatistics( Item.class.getName() );
125
126         Thread.sleep(200);
127
128         s = openSession();
129         t = s.beginTransaction();
130         List JavaDoc result = s.createQuery("select i.description from Item i where i.name='widget'").setCacheable(true).list();
131         assertEquals( result.size(), 1 );
132         t.commit();
133         s.close();
134
135         assertEquals( qs.getCacheHitCount(), 0 );
136
137         s = openSession();
138         t = s.beginTransaction();
139         result = s.createQuery("select i.description from Item i where i.name='widget'").setCacheable(true).list();
140         assertEquals( result.size(), 1 );
141         t.commit();
142         s.close();
143
144         assertEquals( qs.getCacheHitCount(), 1 );
145
146         s = openSession();
147         t = s.beginTransaction();
148         result = s.createQuery("select i.description from Item i where i.name='widget'").setCacheable(true).list();
149         assertEquals( result.size(), 1 );
150         assertTrue( Hibernate.isInitialized( result.get(0) ) );
151         i = (Item) s.get( Item.class, new Long JavaDoc(i.getId()) );
152         i.setName("widget");
153         i.setDescription("A middle-quality widget.");
154         t.commit();
155         s.close();
156
157         assertEquals( qs.getCacheHitCount(), 2 );
158         assertEquals( qs.getCacheMissCount(), 2 );
159
160         Thread.sleep(200);
161
162         s = openSession();
163         t = s.beginTransaction();
164         result = s.createQuery("select i.description from Item i where i.name='widget'").setCacheable(true).list();
165         assertEquals( result.size(), 1 );
166         i = (Item) s.get( Item.class, new Long JavaDoc(i.getId()) );
167         assertEquals( (String JavaDoc) result.get(0), "A middle-quality widget." );
168         
169         s.delete(i);
170         t.commit();
171         s.close();
172
173         assertEquals( qs.getCacheHitCount(), 2 );
174         assertEquals( qs.getCacheMissCount(), 3 );
175         assertEquals( qs.getCachePutCount(), 3 );
176         assertEquals( qs.getExecutionCount(), 3 );
177         assertEquals( es.getFetchCount(), 0 ); //check that it was being cached
178

179     }
180
181
182     protected String JavaDoc[] getMappings() {
183         return new String JavaDoc[] { "querycache/Item.hbm.xml" };
184     }
185
186     public static Test suite() {
187         return new TestSuite(QueryCacheTest.class);
188     }
189
190     protected void configure(Configuration cfg) {
191         super.configure( cfg );
192         cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
193         cfg.setProperty( Environment.CACHE_REGION_PREFIX, "foo" );
194         cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
195         cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
196     }
197 }
198
199
Popular Tags