KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > plugins > clustersupport > BaseTestBroadcastingListener


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.plugins.clustersupport;
6
7 import com.opensymphony.oscache.base.*;
8 import com.opensymphony.oscache.base.events.CacheEntryEventListener;
9
10 import junit.framework.TestCase;
11
12 import java.util.Date JavaDoc;
13
14 /**
15  * A base class that provides the framework for testing a cluster listener
16  * implementation.
17  *
18  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
19  */

20 public abstract class BaseTestBroadcastingListener extends TestCase {
21     /**
22      * The persistance listener used for the tests
23      */

24     protected static AbstractBroadcastingListener listener = null;
25
26     /**
27      * A cache instance to use for the tests
28      */

29     protected static Cache cache = null;
30
31     /**
32      * The number of tests in this class. This is used to keep
33      * track of how many tests remain; once we reach zero we shut
34      * down the broadcasting listener.
35      */

36     int testsRemaining = 0;
37
38     /**
39      * Cache group
40      */

41     private final String JavaDoc GROUP = "test group";
42
43     /**
44      * Object key
45      */

46     private final String JavaDoc KEY = "Test clustersupport persistence listener key";
47
48     public BaseTestBroadcastingListener(String JavaDoc str) {
49         super(str);
50     }
51
52     /**
53      * Tests the listener by causing the cache to fire off all its
54      * events
55      */

56     public void testListener() {
57         CacheEntry entry = new CacheEntry(KEY, null);
58
59         cache.putInCache(KEY, entry);
60         cache.putInCache(KEY, entry, new String JavaDoc[] {GROUP});
61         cache.flushEntry(KEY);
62         cache.flushGroup(GROUP);
63         cache.flushAll(new Date JavaDoc());
64
65         // Note that the remove event is not called since it's not exposed.
66
}
67
68     /**
69      * This method is invoked before each testXXXX methods of the
70      * class. It set up the broadcasting listener required for each test.
71      */

72     public void setUp() {
73         // At first invocation, create a listener
74
if (listener == null) {
75             testsRemaining = countTestCases(); // This seems to always return 1 even if there are multiple tests?
76

77             listener = getListener();
78             assertNotNull(listener);
79
80             cache = new Cache(true, false, false);
81             assertNotNull(cache);
82
83             try {
84                 listener.initialize(cache, getConfig());
85             } catch (InitializationException e) {
86                 fail(e.getMessage());
87             }
88
89             cache.addCacheEventListener(listener, CacheEntryEventListener.class);
90         }
91     }
92
93     /**
94      * Once all the tests are complete this will shut down the broadcasting listener.
95      */

96     protected void tearDown() throws Exception JavaDoc {
97         if (--testsRemaining == 0) {
98             try {
99                 listener.finialize();
100                 listener = null;
101             } catch (FinalizationException e) {
102                 fail(e.getMessage());
103             }
104         }
105     }
106
107     /**
108      * Child classes implement this to return the broadcasting listener instance
109      * that will be tested.
110      */

111     abstract AbstractBroadcastingListener getListener();
112
113     /**
114      * Child classes implement this to return the configuration for their listener
115      * @return
116      */

117     abstract Config getConfig();
118 }
119
Popular Tags