KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  */

4 package org.jboss.cache.optimistic;
5
6 import junit.framework.TestCase;
7 import org.jboss.cache.CacheImpl;
8 import org.jboss.cache.CacheListener;
9 import org.jboss.cache.CacheSPI;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.config.CacheLoaderConfig;
12 import org.jboss.cache.config.Configuration;
13 import org.jboss.cache.factories.XmlConfigurationParser;
14 import org.jboss.cache.interceptors.Interceptor;
15 import org.jboss.cache.interceptors.InvocationContextInterceptor;
16 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
17 import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
18 import org.jboss.cache.interceptors.OptimisticReplicationInterceptor;
19 import org.jboss.cache.interceptors.TxInterceptor;
20 import org.jboss.cache.lock.IsolationLevel;
21 import org.jboss.cache.marshall.MethodCall;
22 import org.jboss.cache.marshall.MethodCallFactory;
23 import org.jboss.cache.marshall.MethodDeclarations;
24 import org.jboss.cache.misc.TestingUtil;
25 import org.jboss.cache.transaction.DummyTransactionManager;
26 import org.jboss.cache.xml.XmlHelper;
27 import org.jgroups.Address;
28 import org.w3c.dom.Element JavaDoc;
29
30 import javax.transaction.SystemException JavaDoc;
31 import javax.transaction.TransactionManager JavaDoc;
32 import java.io.DataInputStream JavaDoc;
33 import java.io.DataOutputStream JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.ObjectInput JavaDoc;
36 import java.io.ObjectOutput JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Random JavaDoc;
41
42 /**
43  * @author manik
44  */

45 public abstract class AbstractOptimisticTestCase extends TestCase
46 {
47    private int instanceNumber;
48
49    // some test data shared among all the test cases
50
protected Fqn fqn = Fqn.fromString("/blah");
51    protected String JavaDoc key = "myKey", value = "myValue";
52
53    protected String JavaDoc getTempDir()
54    {
55       return getTempDir("tempdir");
56    }
57
58    private String JavaDoc getTempDir(String JavaDoc name)
59    {
60       String JavaDoc tempDir = System.getProperty("java.io.tmpdir", "/tmp");
61       tempDir = tempDir + File.separator + name;
62       System.out.println("tmpdir property: " + System.getProperty("java.io.tmpdir"));
63       System.out.println("Attempting to create dir [" + tempDir + "]");
64       File JavaDoc tempDirFile = new File JavaDoc(tempDir);
65       if (!tempDirFile.exists())
66       {
67          tempDirFile.mkdirs();
68       }
69       return tempDir;
70    }
71
72    public AbstractOptimisticTestCase(String JavaDoc name)
73    {
74       super(name);
75    }
76
77    protected CacheImpl createCacheUnstarted() throws Exception JavaDoc
78    {
79       return createCacheUnstarted(true);
80    }
81
82    protected CacheImpl createCacheUnstarted(boolean optimistic) throws Exception JavaDoc
83    {
84       CacheImpl cache = new CacheImpl();
85       Configuration c = new Configuration();
86       cache.setConfiguration(c);
87       if (optimistic) c.setNodeLockingScheme("OPTIMISTIC");
88
89       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
90       c.setCacheMode("LOCAL");
91       return cache;
92    }
93
94    protected CacheImpl createCacheWithListener() throws Exception JavaDoc
95    {
96       return createCacheWithListener(new TestListener());
97    }
98
99    protected CacheImpl createCacheWithListener(CacheListener listener) throws Exception JavaDoc
100    {
101       CacheImpl cache = createCacheUnstarted();
102       cache.create();
103       cache.start();
104       cache.getNotifier().addCacheListener(listener);
105       return cache;
106    }
107
108    /**
109     * Returns a tree cache with passivation disabled in the loader.
110     *
111     * @return
112     * @throws Exception
113     */

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