1 package org.apache.jcs.auxiliary.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 import org.apache.jcs.engine.CacheConstants;
25 import org.apache.jcs.engine.CacheElement;
26 import org.apache.jcs.engine.behavior.ICacheElement;
27 import org.apache.jcs.engine.behavior.ICacheEventQueue;
28 import org.apache.jcs.utils.timing.SleepUtil;
29
30 /***
31 * Unit tests for the remote cache no wait. The no wait manages a queue on top of the client.
32 * <p>
33 * @author Aaron Smuts
34 */
35 public class RemoteCacheNoWaitUnitTest
36 extends TestCase
37 {
38 /***
39 * Simply verify that the client gets updated via the no wait.
40 * <p>
41 * @throws Exception
42 */
43 public void testUpdate()
44 throws Exception
45 {
46
47 RemoteCacheClientMockImpl client = new RemoteCacheClientMockImpl();
48 RemoteCacheNoWait noWait = new RemoteCacheNoWait( client );
49
50 ICacheElement element = new CacheElement( "testUpdate", "key", "value" );
51
52
53 noWait.update( element );
54
55 SleepUtil.sleepAtLeast( 10 );
56
57
58 assertEquals( "Wrong number updated.", 1, client.updateList.size() );
59 assertEquals( "Wrong element", element, client.updateList.get( 0 ) );
60 }
61
62 /***
63 * Simply verify that the client get is called from the no wait.
64 * <p>
65 * @throws Exception
66 */
67 public void testGet()
68 throws Exception
69 {
70
71 RemoteCacheClientMockImpl client = new RemoteCacheClientMockImpl();
72 RemoteCacheNoWait noWait = new RemoteCacheNoWait( client );
73
74 ICacheElement input = new CacheElement( "testUpdate", "key", "value" );
75 client.getSetupMap.put( "key", input );
76
77
78 ICacheElement result = noWait.get( "key" );
79
80
81 assertEquals( "Wrong element", input, result );
82 }
83
84 /***
85 * Simply verify that the client gets updated via the no wait.
86 * <p>
87 * @throws Exception
88 */
89 public void testRemove()
90 throws Exception
91 {
92
93 RemoteCacheClientMockImpl client = new RemoteCacheClientMockImpl();
94 RemoteCacheNoWait noWait = new RemoteCacheNoWait( client );
95
96 String input = "MyKey";
97
98
99 noWait.remove( input );
100
101 SleepUtil.sleepAtLeast( 10 );
102
103
104 assertEquals( "Wrong number updated.", 1, client.removeList.size() );
105 assertEquals( "Wrong key", input, client.removeList.get( 0 ) );
106 }
107
108 /***
109 * Simply verify that the client status is returned in the stats.
110 * <p>
111 * @throws Exception
112 */
113 public void testGetStats()
114 throws Exception
115 {
116
117 RemoteCacheClientMockImpl client = new RemoteCacheClientMockImpl();
118 client.status = CacheConstants.STATUS_ALIVE;
119 RemoteCacheNoWait noWait = new RemoteCacheNoWait( client );
120
121
122 String result = noWait.getStats();
123
124
125 assertTrue( "Status should contain 'ALIVE'", result.indexOf( "ALIVE" ) != -1 );
126 }
127
128 /***
129 * Simply verify that we get a status of error if the cache is in error..
130 * <p>
131 * @throws Exception
132 */
133 public void testGetStatus_error()
134 throws Exception
135 {
136
137 RemoteCacheClientMockImpl client = new RemoteCacheClientMockImpl();
138 client.status = CacheConstants.STATUS_ERROR;
139 RemoteCacheNoWait noWait = new RemoteCacheNoWait( client );
140
141
142 int result = noWait.getStatus();
143
144
145 assertEquals( "Wrong status", CacheConstants.STATUS_ERROR, result );
146 }
147
148 /***
149 * Simply verify that the serviced supplied to fix is passed onto the client. Verify that the
150 * original event queue is destroyed. A new event queue willbe plugged in on fix.
151 * <p>
152 * @throws Exception
153 */
154 public void testFixCache()
155 throws Exception
156 {
157
158 RemoteCacheClientMockImpl client = new RemoteCacheClientMockImpl();
159 client.status = CacheConstants.STATUS_ALIVE;
160 RemoteCacheNoWait noWait = new RemoteCacheNoWait( client );
161
162 RemoteCacheServiceMockImpl service = new RemoteCacheServiceMockImpl();
163
164 ICacheElement element = new CacheElement( "testUpdate", "key", "value" );
165
166
167 noWait.update( element );
168 SleepUtil.sleepAtLeast( 10 );
169 ICacheEventQueue originalQueue = noWait.getCacheEventQueue();
170
171 noWait.fixCache( service );
172
173 noWait.update( element );
174 SleepUtil.sleepAtLeast( 10 );
175 ICacheEventQueue newQueue = noWait.getCacheEventQueue();
176
177
178 assertEquals( "Wrong status", service, client.fixed );
179 assertFalse( "Original queue should not alive", originalQueue.isAlive() );
180 assertTrue( "New queue should be alive." + newQueue, newQueue.isAlive() );
181 }
182 }