View Javadoc
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   * Licensed to the Apache Software Foundation (ASF) under one
19   * or more contributor license agreements.  See the NOTICE file
20   * distributed with this work for additional information
21   * regarding copyright ownership.  The ASF licenses this file
22   * to you under the Apache License, Version 2.0 (the
23   * "License"); you may not use this file except in compliance
24   * with the License.  You may obtain a copy of the License at
25   *
26   *   http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing,
29   * software distributed under the License is distributed on an
30   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
31   * KIND, either express or implied.  See the License for the
32   * specific language governing permissions and limitations
33   * under the License.
34   */
35  
36  import junit.framework.TestCase;
37  
38  /** Test for the listener that observers UDP discovery events. */
39  public class LateralTCPDiscoveryListenerUnitTest
40      extends TestCase
41  {
42      /** the listener */
43      private LateralTCPDiscoveryListener listener;
44  
45      /** the cache factory */
46      private LateralTCPCacheFactory factory;
47  
48      /** The cache manager. */
49      private CompositeCacheManager cacheMgr;
50  
51      /** The event logger. */
52      protected MockCacheEventLogger cacheEventLogger;
53  
54      /** The serializer. */
55      protected IElementSerializer elementSerializer;
56  
57      /** Create the listener for testing */
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       * Add a no wait facade.
92       */
93      public void testAddNoWaitFacade_NotInList()
94      {
95          // SETUP
96          final String cacheName = "testAddNoWaitFacade_NotInList";
97          final LateralCacheNoWaitFacade<String, String> facade = setupFacade(cacheName);
98  
99          // DO WORK
100         listener.addNoWaitFacade( cacheName, facade );
101 
102         // VERIFY
103         assertTrue( "Should have the facade.", listener.containsNoWaitFacade( cacheName ) );
104     }
105 
106     /**
107      * Add a no wait to a known facade.
108      */
109     public void testAddNoWait_FacadeInList()
110     {
111         // SETUP
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         // DO WORK
119         final boolean result = listener.addNoWait( noWait );
120 
121         // VERIFY
122         assertTrue( "Should have added the no wait.", result );
123     }
124 
125     /**
126      * Add a no wait from an unknown facade.
127      */
128     public void testAddNoWait_FacadeNotInList()
129     {
130         // SETUP
131         final String cacheName = "testAddNoWaitFacade_FacadeInList";
132         final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
133 
134         // DO WORK
135         final boolean result = listener.addNoWait( noWait );
136 
137         // VERIFY
138         assertFalse( "Should not have added the no wait.", result );
139     }
140 
141     /**
142      * Remove a no wait from an unknown facade.
143      */
144     public void testRemoveNoWait_FacadeNotInList()
145     {
146         // SETUP
147         final String cacheName = "testRemoveNoWaitFacade_FacadeNotInList";
148         final LateralCacheNoWait<String, String> noWait = setupNoWait(cacheName);
149 
150         // DO WORK
151         final boolean result = listener.removeNoWait( noWait );
152 
153         // VERIFY
154         assertFalse( "Should not have removed the no wait.", result );
155     }
156 
157     /**
158      * Remove a no wait from a known facade.
159      */
160     public void testRemoveNoWait_FacadeInList_NoWaitNot()
161     {
162         // SETUP
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         // DO WORK
170         final boolean result = listener.removeNoWait( noWait );
171 
172         // VERIFY
173         assertFalse( "Should not have removed the no wait.", result );
174     }
175 
176     /**
177      * Remove a no wait from a known facade.
178      */
179     public void testRemoveNoWait_FacadeInList_NoWaitIs()
180     {
181         // SETUP
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         // DO WORK
190         final boolean result = listener.removeNoWait( noWait );
191 
192         // VERIFY
193         assertTrue( "Should have removed the no wait.", result );
194     }
195 
196     /**
197      * Test cache creation with empty noWaits.
198      */
199     public void testEmptyNoWaits()
200     {
201         // SETUP
202         final String cacheName = "testEmptyNoWaits";
203 
204         final ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes();
205         lca.setTcpServers(""); // default
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         // VERIFY
212         assertFalse( "No waits should be empty.", noWait.containsNoWait(""));
213     }
214 
215     /**
216      * Add a no wait to a known facade.
217      */
218     public void testAddDiscoveredService_FacadeInList_NoWaitNot()
219     {
220         // SETUP
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         // used as identifying key by factory
232         lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() );
233         lca.setCacheName(cacheName);
234         final LateralCacheNoWait<String, String> noWait = factory.createCacheNoWait(lca, cacheEventLogger, elementSerializer);
235         // this is the normal process, the discovery service expects it there
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         // DO WORK
243         listener.addDiscoveredService( service );
244 
245         // VERIFY
246         assertTrue( "Should have no wait.", listener.containsNoWait( cacheName, noWait ) );
247     }
248 
249     /**
250      * Remove a no wait from a known facade.
251      */
252     public void testRemoveDiscoveredService_FacadeInList_NoWaitIs()
253     {
254         // SETUP
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         // used as identifying key by factory
267         lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() );
268         lca.setCacheName(cacheName);
269         final LateralCacheNoWait<String, String> noWait = factory.createCacheNoWait(lca, cacheEventLogger, elementSerializer);
270         // this is the normal process, the discovery service expects it there
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         // DO WORK
279         listener.removeDiscoveredService( service );
280 
281         // VERIFY
282         assertFalse( "Should not have no wait.", listener.containsNoWait( cacheName, noWait ) );
283     }
284 }