KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > optimistic > AbstractOptimisticTestCase


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.cache.pojo.optimistic;
24
25 import junit.framework.TestCase;
26 import org.jboss.cache.Fqn;
27 import org.jboss.cache.CacheListener;
28 import org.jboss.cache.CacheSPI;
29 import org.jboss.cache.pojo.PojoCache;
30 import org.jboss.cache.pojo.PojoCacheFactory;
31 import org.jboss.cache.marshall.MethodCall;
32 import org.jboss.cache.marshall.MethodCallFactory;
33 import org.jboss.cache.marshall.MethodDeclarations;
34 import org.jboss.cache.interceptors.Interceptor;
35 import org.jboss.cache.interceptors.InvocationContextInterceptor;
36 import org.jboss.cache.interceptors.TxInterceptor;
37 import org.jboss.cache.interceptors.OptimisticReplicationInterceptor;
38 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
39 import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
40 import org.jboss.cache.transaction.DummyTransactionManager;
41 import org.jboss.cache.misc.TestingUtil;
42 import org.jboss.cache.lock.IsolationLevel;
43 import org.jboss.cache.factories.XmlConfigurationParser;
44 import org.jboss.cache.xml.XmlHelper;
45 import org.jboss.cache.optimistic.TestListener;
46 import org.jboss.cache.optimistic.DefaultDataVersion;
47 import org.jboss.cache.config.Configuration;
48 import org.jboss.cache.config.CacheLoaderConfig;
49 import org.w3c.dom.Element JavaDoc;
50
51 import javax.transaction.TransactionManager JavaDoc;
52 import javax.transaction.SystemException JavaDoc;
53 import java.io.File JavaDoc;
54 import java.util.Random JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Iterator JavaDoc;
58
59 /**
60  * Base test for optimistic locking. Copied from Cache counterpart.
61  */

62 public abstract class AbstractOptimisticTestCase extends TestCase
63 {
64    private int instanceNumber;
65
66    // some test data shared among all the test cases
67
protected Fqn fqn = Fqn.fromString("/blah");
68    protected String JavaDoc key = "myKey", value = "myValue";
69
70    protected String JavaDoc getTempDir()
71    {
72       return getTempDir("tempdir");
73    }
74
75    private String JavaDoc getTempDir(String JavaDoc name)
76    {
77       String JavaDoc tempDir = System.getProperty("java.io.tmpdir", "/tmp");
78       tempDir = tempDir + File.separator + name;
79       System.out.println("tmpdir property: " + System.getProperty("java.io.tmpdir"));
80       System.out.println("Attempting to create dir [" + tempDir + "]");
81       File JavaDoc tempDirFile = new File JavaDoc(tempDir);
82       if (!tempDirFile.exists())
83       {
84          tempDirFile.mkdirs();
85       }
86       return tempDir;
87    }
88
89    public AbstractOptimisticTestCase(String JavaDoc name)
90    {
91       super(name);
92    }
93
94    protected PojoCache createCacheUnstarted() throws Exception JavaDoc
95    {
96       return createCacheUnstarted(true);
97    }
98
99    protected PojoCache createCacheUnstarted(boolean optimistic) throws Exception JavaDoc
100    {
101       Configuration c = new Configuration();
102       if (optimistic) c.setNodeLockingScheme("OPTIMISTIC");
103
104       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
105       c.setCacheMode("LOCAL");
106       PojoCache cache = PojoCacheFactory.createCache(c, false);
107       return cache;
108    }
109
110    protected PojoCache createCacheWithListener() throws Exception JavaDoc
111    {
112       return createCacheWithListener(new TestListener());
113    }
114
115    protected PojoCache createCacheWithListener(CacheListener listener) throws Exception JavaDoc
116    {
117       PojoCache cache = createCacheUnstarted();
118       cache.create();
119       cache.start();
120       cache.getCache().addCacheListener(listener);
121       return cache;
122    }
123
124    /**
125     * Returns a tree cache with passivation disabled in the loader.
126     *
127     * @return
128     * @throws Exception
129     */

130    protected PojoCache createCacheWithLoader() throws Exception JavaDoc
131    {
132       return createCacheWithLoader(false);
133    }
134
135    protected CacheLoaderConfig getCacheLoaderConfig(boolean shared, String JavaDoc filename, boolean passivation) throws Exception JavaDoc
136    {
137       String JavaDoc xml = " <config>\n" +
138               " <passivation>" + passivation + "</passivation>\n" +
139               " <preload></preload>\n" +
140               " <shared>" + shared + "</shared>\n" +
141               " <cacheloader>\n" +
142               " <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
143               " <properties>\n" +
144               " </properties>\n" +
145               " <async>false</async>\n" +
146               " <fetchPersistentState>" + (!shared) + "</fetchPersistentState>\n" +
147               " <ignoreModifications>false</ignoreModifications>\n" +
148               " </cacheloader>\n" +
149               " </config>";
150       Element JavaDoc element = XmlHelper.stringToElement(xml);
151       return XmlConfigurationParser.parseCacheLoaderConfig(element);
152    }
153
154    protected PojoCache createCacheWithLoader(boolean passivationEnabled) throws Exception JavaDoc
155    {
156       PojoCache cache = createCacheUnstarted();
157       Configuration c = cache.getCache().getConfiguration();
158       c.setCacheLoaderConfig(getCacheLoaderConfig(true, getTempDir(), passivationEnabled));
159       cache.create();
160       cache.start();
161       return cache;
162    }
163
164    protected PojoCache createCache() throws Exception JavaDoc
165    {
166       PojoCache cache = createCacheUnstarted();
167       cache.create();
168       cache.start();
169       return cache;
170    }
171
172    protected void destroyCache(PojoCache c)
173    {
174       c.stop();
175       c.destroy();
176    }
177
178
179    protected PojoCache createPessimisticCache() throws Exception JavaDoc
180    {
181       Configuration c = new Configuration();
182       c.setClusterName("name");
183       c.setInitialStateRetrievalTimeout(5000);
184       c.setClusterConfig(getDefaultProperties());
185       c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
186       c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
187       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
188       
189       PojoCache cache = PojoCacheFactory.createCache(c, false);
190       cache.create();
191       cache.start();
192
193
194       return cache;
195    }
196
197    protected PojoCache createPessimisticCacheLocal() throws Exception JavaDoc
198    {
199       Configuration c = new Configuration();
200
201       c.setClusterName("name");
202       c.setInitialStateRetrievalTimeout(5000);
203       c.setClusterConfig(getDefaultProperties());
204
205       c.setCacheMode(Configuration.CacheMode.LOCAL);
206       c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
207       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
208       PojoCache cache = PojoCacheFactory.createCache(c, false);
209       cache.create();
210       cache.start();
211
212       return cache;
213    }
214
215    protected String JavaDoc getDefaultProperties()
216    {
217       return "UDP(mcast_addr=228.1.2.3;mcast_port=48866;ip_ttl=32;" +
218               "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;loopback=true;ip_mcast=true;bind_addr=127.0.0.1):" +
219               "PING(timeout=1000;num_initial_members=2):" +
220               "MERGE2(min_interval=5000;max_interval=10000):" +
221               "FD_SOCK:" +
222               "VERIFY_SUSPECT(timeout=1500):" +
223               "pbcast.NAKACK(gc_lag=50;max_xmit_size=8192;retransmit_timeout=600,1200,2400,4800):" +
224               "UNICAST(timeout=600,1200,2400,4800):" +
225               "pbcast.STABLE(desired_avg_gossip=20000):" +
226               "FRAG(frag_size=8192;down_thread=false;up_thread=false):" +
227               "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
228               "shun=false;print_local_addr=true):" +
229               "pbcast.STATE_TRANSFER";
230    }
231
232    protected PojoCache createReplicatedCache(Configuration.CacheMode mode) throws Exception JavaDoc
233    {
234       return createReplicatedCache("test", mode);
235    }
236
237    protected PojoCache createReplicatedCache(String JavaDoc name, Configuration.CacheMode mode) throws Exception JavaDoc
238    {
239       Configuration c = new Configuration();
240
241       c.setClusterName(name);
242       c.setInitialStateRetrievalTimeout(5000);
243       c.setClusterConfig(getDefaultProperties());
244       c.setCacheMode(mode);
245       if (mode == Configuration.CacheMode.REPL_SYNC)
246       {
247          // make sure commits and rollbacks are sync as well
248
c.setSyncCommitPhase(true);
249          c.setSyncRollbackPhase(true);
250       }
251       c.setNodeLockingScheme("OPTIMISTIC");
252       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
253       PojoCache cache = PojoCacheFactory.createCache(c, false);
254       cache.create();
255       cache.start();
256
257       return cache;
258    }
259
260    protected PojoCache createReplicatedCacheWithLoader(boolean shared, Configuration.CacheMode cacheMode) throws Exception JavaDoc
261    {
262       return createReplicatedCacheWithLoader("temp-loader", shared, cacheMode);
263    }
264
265    protected PojoCache createReplicatedCacheWithLoader(boolean shared) throws Exception JavaDoc
266    {
267       return createReplicatedCacheWithLoader("temp-loader", shared, Configuration.CacheMode.REPL_SYNC);
268    }
269
270    protected PojoCache createReplicatedCacheWithLoader(String JavaDoc name, boolean shared) throws Exception JavaDoc
271    {
272       return createReplicatedCacheWithLoader(name, shared, Configuration.CacheMode.REPL_SYNC);
273    }
274
275    protected PojoCache createReplicatedCacheWithLoader(String JavaDoc name, boolean shared, Configuration.CacheMode cacheMode) throws Exception JavaDoc
276    {
277       Configuration c = new Configuration();
278       c.setClusterName(name);
279       c.setInitialStateRetrievalTimeout(5000);
280       c.setClusterConfig(getDefaultProperties());
281       c.setCacheMode(cacheMode);
282       c.setSyncCommitPhase(true);
283       c.setSyncRollbackPhase(true);
284       c.setNodeLockingScheme("OPTIMISTIC");
285       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
286       c.setCacheLoaderConfig(getCacheLoaderConfig(shared, shared ? getTempDir(name + "-shared") : getTempDir(name + instanceNumber++), false));
287
288       PojoCache cache = PojoCacheFactory.createCache(c, false);
289       cache.create();
290       cache.start();
291       return cache;
292    }
293
294    protected Random JavaDoc random;
295
296    protected void randomSleep(int min, int max)
297    {
298       if (random == null) random = new Random JavaDoc();
299       long l = -1;
300       while (l < min) l = random.nextInt(max);
301       TestingUtil.sleepThread(l);
302    }
303
304    protected void tearDown()
305    {
306       TransactionManager mgr = DummyTransactionManager.getInstance();
307       try
308       {
309          if (mgr.getTransaction() != null)
310          {
311             mgr.rollback();
312          }
313       }
314       catch (SystemException JavaDoc e)
315       {
316          // do nothing
317
}
318    }
319
320    protected Interceptor getAlteredInterceptorChain(Interceptor newLast, CacheSPI spi, boolean replicated)
321    {
322       Interceptor ici = new InvocationContextInterceptor();
323       ici.setCache(spi);
324
325       Interceptor txInterceptor = new TxInterceptor();
326       txInterceptor.setCache(spi);
327
328       Interceptor replicationInterceptor = new OptimisticReplicationInterceptor();
329       replicationInterceptor.setCache(spi);
330
331       Interceptor createInterceptor = new OptimisticCreateIfNotExistsInterceptor();
332       createInterceptor.setCache(spi);
333
334       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
335       nodeInterceptor.setCache(spi);
336
337       ici.setNext(txInterceptor);
338       if (replicated)
339       {
340          txInterceptor.setNext(replicationInterceptor);
341          replicationInterceptor.setNext(createInterceptor);
342       }
343       else
344       {
345          txInterceptor.setNext(createInterceptor);
346       }
347       createInterceptor.setNext(nodeInterceptor);
348       nodeInterceptor.setNext(newLast);
349
350       return ici;
351    }
352
353    public abstract class ExceptionThread extends Thread JavaDoc
354    {
355       protected Exception JavaDoc exception;
356
357       public void setException(Exception JavaDoc e)
358       {
359          exception = e;
360       }
361
362       public Exception JavaDoc getException()
363       {
364          return exception;
365       }
366    }
367
368    protected List JavaDoc injectDataVersion(List JavaDoc modifications)
369    {
370       List JavaDoc newList = new ArrayList JavaDoc();
371       Iterator JavaDoc mi = modifications.iterator();
372       while (mi.hasNext())
373       {
374          MethodCall c = (MethodCall) mi.next();
375          Object JavaDoc[] oa = c.getArgs();
376          Object JavaDoc[] na = new Object JavaDoc[oa.length + 1];
377          System.out.println("*** " + oa.length);
378          for (int i = 0; i < oa.length; i++) na[i] = oa[i];
379          na[oa.length] = new DefaultDataVersion();
380          newList.add(MethodCallFactory.create(MethodDeclarations.getVersionedMethod(c.getMethodId()), na));
381       }
382       return newList;
383    }
384
385 }
386
Popular Tags