KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > cache > SecondLevelCacheTest


1 //$Id: SecondLevelCacheTest.java,v 1.6 2005/07/01 18:59:52 epbernard Exp $
2
package org.hibernate.test.cache;
3
4 import java.util.Map JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.cache.TreeCacheProvider;
12 import org.hibernate.cfg.Configuration;
13 import org.hibernate.cfg.Environment;
14 import org.hibernate.stat.SecondLevelCacheStatistics;
15 import org.hibernate.stat.Statistics;
16 import org.hibernate.test.TestCase;
17
18 /**
19  * @author Gavin King
20  */

21 public class SecondLevelCacheTest extends TestCase {
22     
23     public SecondLevelCacheTest(String JavaDoc str) {
24         super(str);
25     }
26     
27     public void testQueryCacheInvalidation() {
28         Session s = openSession();
29         Transaction t = s.beginTransaction();
30         Item i = new Item();
31         i.setName("widget");
32         i.setDescription("A really top-quality, full-featured widget.");
33         s.persist(i);
34         t.commit();
35         s.close();
36         
37         SecondLevelCacheStatistics slcs = s.getSessionFactory()
38             .getStatistics()
39             .getSecondLevelCacheStatistics( Item.class.getName() );
40         
41         assertEquals( slcs.getPutCount(), 1 );
42         assertEquals( slcs.getElementCountInMemory(), 1 );
43         assertEquals( slcs.getEntries().size(), 1 );
44         
45         s = openSession();
46         t = s.beginTransaction();
47         i = (Item) s.get( Item.class, i.getId() );
48         
49         assertEquals( slcs.getHitCount(), 1 );
50         assertEquals( slcs.getMissCount(), 0 );
51         
52         i.setDescription("A bog standard item");
53         
54         t.commit();
55         s.close();
56
57         assertEquals( slcs.getPutCount(), 2 );
58         
59         Map JavaDoc map = (Map JavaDoc) slcs.getEntries().get( i.getId() );
60         assertTrue( map.get("description").equals("A bog standard item") );
61         assertTrue( map.get("name").equals("widget") );
62
63     }
64
65     public void testEmptySecondLevelCacheEntry() throws Exception JavaDoc {
66         getSessions().evictEntity( Item.class.getName() );
67         Statistics stats = getSessions().getStatistics();
68         stats.clear();
69         SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics( Item.class.getName() );
70         Map JavaDoc cacheEntries = statistics.getEntries();
71         assertEquals( 0, cacheEntries.size() );
72     }
73
74
75     protected String JavaDoc[] getMappings() {
76         return new String JavaDoc[] { "cache/Item.hbm.xml" };
77     }
78
79     public static Test suite() {
80         return new TestSuite(SecondLevelCacheTest.class);
81     }
82
83     protected void configure(Configuration cfg) {
84         super.configure( cfg ); //todo: implement overriden method body
85
cfg.setProperty( Environment.CACHE_REGION_PREFIX, "" );
86         cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
87         cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
88         cfg.setProperty( Environment.USE_STRUCTURED_CACHE, "true" );
89         cfg.setProperty( Environment.CACHE_PROVIDER, TreeCacheProvider.class.getName() );
90     }
91
92     public String JavaDoc getCacheConcurrencyStrategy() {
93         return "transactional";
94     }
95 }
96
97
Popular Tags