1 package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.commons.jcs3.auxiliary.lateral.LateralCache;
7 import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheNoWait;
8 import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheNoWaitFacade;
9 import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
10 import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
11 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
12 import org.apache.commons.jcs3.engine.control.CompositeCacheManager;
13 import org.apache.commons.jcs3.engine.logging.MockCacheEventLogger;
14 import org.apache.commons.jcs3.utils.discovery.DiscoveredService;
15 import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import junit.framework.TestCase;
37
38
39 public class LateralTCPDiscoveryListenerUnitTest
40 extends TestCase
41 {
42
43 private LateralTCPDiscoveryListener listener;
44
45
46 private LateralTCPCacheFactory factory;
47
48
49 private CompositeCacheManager cacheMgr;
50
51
52 protected MockCacheEventLogger cacheEventLogger;
53
54
55 protected IElementSerializer elementSerializer;
56
57
58 @Override
59 protected void setUp() throws Exception
60 {
61 factory = new LateralTCPCacheFactory();
62 factory.initialize();
63
64 cacheMgr = CompositeCacheManager.getInstance();
65 cacheEventLogger = new MockCacheEventLogger();
66 elementSerializer = new StandardSerializer();
67
68 listener = new LateralTCPDiscoveryListener( factory.getName(), cacheMgr,
69 cacheEventLogger, elementSerializer );
70 }
71
72 private LateralCacheNoWaitFacade<String, String> setupFacade(final String cacheName)
73 {
74 List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
75 final ITCPLateralCacheAttributes cattr = new TCPLateralCacheAttributes();
76 cattr.setCacheName( cacheName );
77
78 return new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
79 }
80
81 private LateralCacheNoWait<String, String> setupNoWait(final String cacheName)
82 {
83 final ITCPLateralCacheAttributes cattr = new TCPLateralCacheAttributes();
84 cattr.setCacheName( cacheName );
85
86 final LateralCache<String, String> cache = new LateralCache<>(cattr, new ZombieCacheServiceNonLocal<>(), null);
87 return new LateralCacheNoWait<>( cache );
88 }
89
90
91
92
93 public void testAddNoWaitFacade_NotInList()
94 {
95
96 final String cacheName = "testAddNoWaitFacade_NotInList";
97 final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
98
99
100 listener.addNoWaitFacade( cacheName, facade );
101
102
103 assertTrue( "Should have the facade.", listener.containsNoWaitFacade( cacheName ) );
104 }
105
106
107
108
109 public void testAddNoWait_FacadeInList()
110 {
111
112 final String cacheName = "testAddNoWaitFacade_FacadeInList";
113 final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
114 listener.addNoWaitFacade( cacheName, facade );
115
116 final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
117
118
119 final boolean result = listener.addNoWait( noWait );
120
121
122 assertTrue( "Should have added the no wait.", result );
123 }
124
125
126
127
128 public void testAddNoWait_FacadeNotInList()
129 {
130
131 final String cacheName = "testAddNoWaitFacade_FacadeInList";
132 final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
133
134
135 final boolean result = listener.addNoWait( noWait );
136
137
138 assertFalse( "Should not have added the no wait.", result );
139 }
140
141
142
143
144 public void testRemoveNoWait_FacadeNotInList()
145 {
146
147 final String cacheName = "testRemoveNoWaitFacade_FacadeNotInList";
148 final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
149
150
151 final boolean result = listener.removeNoWait( noWait );
152
153
154 assertFalse( "Should not have removed the no wait.", result );
155 }
156
157
158
159
160 public void testRemoveNoWait_FacadeInList_NoWaitNot()
161 {
162
163 final String cacheName = "testAddNoWaitFacade_FacadeInList";
164 final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
165 listener.addNoWaitFacade( cacheName, facade );
166
167 final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
168
169
170 final boolean result = listener.removeNoWait( noWait );
171
172
173 assertFalse( "Should not have removed the no wait.", result );
174 }
175
176
177
178
179 public void testRemoveNoWait_FacadeInList_NoWaitIs()
180 {
181
182 final String cacheName = "testRemoveNoWaitFacade_FacadeInListNoWaitIs";
183 final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
184 listener.addNoWaitFacade( cacheName, facade );
185
186 final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
187 listener.addNoWait( noWait );
188
189
190 final boolean result = listener.removeNoWait( noWait );
191
192
193 assertTrue( "Should have removed the no wait.", result );
194 }
195
196
197
198
199 public void testEmptyNoWaits()
200 {
201
202 final String cacheName = "testEmptyNoWaits";
203
204 final ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes();
205 lca.setTcpServers("");
206 lca.setTcpListenerPort(1120);
207 lca.setCacheName(cacheName);
208 lca.setUdpDiscoveryEnabled(false);
209 final LateralCacheNoWaitFacade<String, String> noWait = factory.createCache(lca, cacheMgr, cacheEventLogger, elementSerializer);
210
211
212 assertFalse( "No waits should be empty.", noWait.containsNoWait(""));
213 }
214
215
216
217
218 public void testAddDiscoveredService_FacadeInList_NoWaitNot()
219 {
220
221 final String cacheName = "testAddDiscoveredService_FacadeInList_NoWaitNot";
222 final ArrayList<String> cacheNames = new ArrayList<>();
223 cacheNames.add( cacheName );
224
225 final DiscoveredService service = new DiscoveredService();
226 service.setCacheNames( cacheNames );
227 service.setServiceAddress( "localhost" );
228 service.setServicePort( 9999 );
229
230 final ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes();
231
232 lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() );
233 lca.setCacheName(cacheName);
234 final LateralCacheNoWait<String, String> noWait = factory.createCacheNoWait(lca, cacheEventLogger, elementSerializer);
235
236 cacheMgr.addAuxiliaryCache(factory.getName(), cacheName, noWait);
237 cacheMgr.registryFacPut(factory);
238
239 final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
240 listener.addNoWaitFacade( cacheName, facade );
241
242
243 listener.addDiscoveredService( service );
244
245
246 assertTrue( "Should have no wait.", listener.containsNoWait( cacheName, noWait ) );
247 }
248
249
250
251
252 public void testRemoveDiscoveredService_FacadeInList_NoWaitIs()
253 {
254
255 final String cacheName = "testRemoveDiscoveredService_FacadeInList_NoWaitIs";
256
257 final ArrayList<String> cacheNames = new ArrayList<>();
258 cacheNames.add( cacheName );
259
260 final DiscoveredService service = new DiscoveredService();
261 service.setCacheNames( cacheNames );
262 service.setServiceAddress( "localhost" );
263 service.setServicePort( 9999 );
264
265 final ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes();
266
267 lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() );
268 lca.setCacheName(cacheName);
269 final LateralCacheNoWait<String, String> noWait = factory.createCacheNoWait(lca, cacheEventLogger, elementSerializer);
270
271 cacheMgr.addAuxiliaryCache(factory.getName(), cacheName, noWait);
272 cacheMgr.registryFacPut(factory);
273
274 final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
275 listener.addNoWaitFacade( cacheName, facade );
276 listener.addDiscoveredService( service );
277
278
279 listener.removeDiscoveredService( service );
280
281
282 assertFalse( "Should not have no wait.", listener.containsNoWait( cacheName, noWait ) );
283 }
284 }