KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > CacheTest


1 //$Id: CacheTest.java,v 1.4 2005/02/12 07:27:27 steveebersole Exp $
2
package org.hibernate.test.legacy;
3
4 import junit.framework.Test;
5 import junit.framework.TestCase;
6 import junit.framework.TestSuite;
7
8 import org.hibernate.cache.Cache;
9 import org.hibernate.cache.CacheConcurrencyStrategy;
10 import org.hibernate.cache.CacheProvider;
11 import org.hibernate.cache.ReadWriteCache;
12 import org.hibernate.cache.CacheConcurrencyStrategy.SoftLock;
13
14 public class CacheTest extends TestCase {
15
16     public CacheTest(String JavaDoc arg0) {
17         super(arg0);
18     }
19
20     public static Test suite() {
21         return new TestSuite(CacheTest.class);
22     }
23
24     public void testCaches() throws Exception JavaDoc {
25         //doTestCache( new CoherenceCacheProvider() );
26
// steve - commented this out becuase it is breaking the build after the package rename
27
//doTestCache( new Provider() );
28
}
29
30     public void doTestCache(CacheProvider cacheProvider) throws Exception JavaDoc {
31
32         Cache cache = cacheProvider.buildCache( String JavaDoc.class.getName(), System.getProperties() );
33
34         long longBefore = cache.nextTimestamp();
35
36         Thread.sleep(15);
37
38         long before = cache.nextTimestamp();
39
40         Thread.sleep(15);
41
42         //cache.setTimeout(1000);
43
CacheConcurrencyStrategy ccs = new ReadWriteCache();
44         ccs.setCache(cache);
45
46         // cache something
47

48         assertTrue( ccs.put("foo", "foo", before, null, null, false) );
49
50         Thread.sleep(15);
51
52         long after = cache.nextTimestamp();
53
54         assertTrue( ccs.get("foo", longBefore)==null );
55         assertTrue( ccs.get("foo", after).equals("foo") );
56
57         assertTrue( !ccs.put("foo", "foo", before, null, null, false) );
58
59         // update it:
60

61         SoftLock lock = ccs.lock("foo", null);
62
63         assertTrue( ccs.get("foo", after)==null );
64         assertTrue( ccs.get("foo", longBefore)==null );
65
66         assertTrue( !ccs.put("foo", "foo", before, null, null, false) );
67
68         Thread.sleep(15);
69
70         long whileLocked = cache.nextTimestamp();
71
72         assertTrue( !ccs.put("foo", "foo", whileLocked, null, null, false) );
73
74         Thread.sleep(15);
75
76         ccs.release("foo", lock);
77
78         assertTrue( ccs.get("foo", after)==null );
79         assertTrue( ccs.get("foo", longBefore)==null );
80
81         assertTrue( !ccs.put("foo", "bar", whileLocked, null, null, false) );
82         assertTrue( !ccs.put("foo", "bar", after, null, null, false) );
83
84         Thread.sleep(15);
85
86         long longAfter = cache.nextTimestamp();
87
88         assertTrue( ccs.put("foo", "baz", longAfter, null, null, false) );
89
90         assertTrue( ccs.get("foo", after)==null );
91         assertTrue( ccs.get("foo", whileLocked)==null );
92
93         Thread.sleep(15);
94
95         long longLongAfter = cache.nextTimestamp();
96
97         assertTrue( ccs.get("foo", longLongAfter).equals("baz") );
98
99         // update it again, with multiple locks:
100

101         SoftLock lock1 = ccs.lock("foo", null);
102         SoftLock lock2 = ccs.lock("foo", null);
103
104         assertTrue( ccs.get("foo", longLongAfter)==null );
105
106         Thread.sleep(15);
107
108         whileLocked = cache.nextTimestamp();
109
110         assertTrue( !ccs.put("foo", "foo", whileLocked, null, null, false) );
111
112         Thread.sleep(15);
113
114         ccs.release("foo", lock2);
115
116         Thread.sleep(15);
117
118         long betweenReleases = cache.nextTimestamp();
119
120         assertTrue( !ccs.put("foo", "bar", betweenReleases, null, null, false) );
121         assertTrue( ccs.get("foo", betweenReleases)==null );
122
123         Thread.sleep(15);
124
125         ccs.release("foo", lock1);
126
127         assertTrue( !ccs.put("foo", "bar", whileLocked, null, null, false) );
128
129         Thread.sleep(15);
130
131         longAfter = cache.nextTimestamp();
132
133         assertTrue( ccs.put("foo", "baz", longAfter, null, null, false) );
134         assertTrue( ccs.get("foo", whileLocked)==null );
135
136         Thread.sleep(15);
137
138         longLongAfter = cache.nextTimestamp();
139
140         assertTrue( ccs.get("foo", longLongAfter).equals("baz") );
141
142     }
143
144 }
145
146
147
148
149
150
151
Popular Tags