View Javadoc
1   package org.apache.commons.jcs.auxiliary.lateral.socket.tcp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.commons.jcs.auxiliary.lateral.LateralCache;
24  import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes;
25  import org.apache.commons.jcs.auxiliary.lateral.LateralCacheNoWait;
26  import org.apache.commons.jcs.auxiliary.lateral.LateralCacheNoWaitFacade;
27  import org.apache.commons.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
28  import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
29  import org.apache.commons.jcs.engine.behavior.IElementSerializer;
30  import org.apache.commons.jcs.engine.control.CompositeCacheManager;
31  import org.apache.commons.jcs.engine.logging.MockCacheEventLogger;
32  import org.apache.commons.jcs.utils.discovery.DiscoveredService;
33  import org.apache.commons.jcs.utils.serialization.StandardSerializer;
34  
35  import java.util.ArrayList;
36  
37  /** Test for the listener that observers UDP discovery events. */
38  public class LateralTCPDiscoveryListenerUnitTest
39      extends TestCase
40  {
41      /** the listener */
42      private LateralTCPDiscoveryListener listener;
43  
44      /** the cache factory */
45      private LateralTCPCacheFactory factory;
46  
47      /** The cache manager. */
48      private CompositeCacheManager cacheMgr;
49  
50      /** The event logger. */
51      protected MockCacheEventLogger cacheEventLogger;
52  
53      /** The serializer. */
54      protected IElementSerializer elementSerializer;
55  
56      /** Create the listener for testing */
57      @Override
58      protected void setUp() throws Exception
59      {
60          factory = new LateralTCPCacheFactory();
61          factory.initialize();
62  
63          cacheMgr = CompositeCacheManager.getInstance();
64          cacheEventLogger = new MockCacheEventLogger();
65          elementSerializer = new StandardSerializer();
66  
67          listener = new LateralTCPDiscoveryListener( factory.getName(), cacheMgr );
68      }
69  
70      /**
71       * Add a no wait facade.
72       */
73      public void testAddNoWaitFacade_NotInList()
74      {
75          // SETUP
76          String cacheName = "testAddNoWaitFacade_NotInList";
77  
78          @SuppressWarnings("unchecked")
79          LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
80          ILateralCacheAttributes cattr = new LateralCacheAttributes();
81          cattr.setCacheName( cacheName );
82  
83          LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
84  
85          // DO WORK
86          listener.addNoWaitFacade( cacheName, facade );
87  
88          // VERIFY
89          assertTrue( "Should have the facade.", listener.containsNoWaitFacade( cacheName ) );
90      }
91  
92      /**
93       * Add a no wait to a known facade.
94       */
95      public void testAddNoWait_FacadeInList()
96      {
97          // SETUP
98          String cacheName = "testAddNoWaitFacade_FacadeInList";
99  
100         @SuppressWarnings("unchecked")
101         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
102         ILateralCacheAttributes cattr = new LateralCacheAttributes();
103         cattr.setCacheName( cacheName );
104 
105         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
106         listener.addNoWaitFacade( cacheName, facade );
107 
108         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
109         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
110 
111         // DO WORK
112         boolean result = listener.addNoWait( noWait );
113 
114         // VERIFY
115         assertTrue( "Should have added the no wait.", result );
116     }
117 
118     /**
119      * Add a no wait from an unknown facade.
120      */
121     public void testAddNoWait_FacadeNotInList()
122     {
123         // SETUP
124         String cacheName = "testAddNoWaitFacade_FacadeInList";
125         ILateralCacheAttributes cattr = new LateralCacheAttributes();
126         cattr.setCacheName( cacheName );
127 
128         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
129         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
130 
131         // DO WORK
132         boolean result = listener.addNoWait( noWait );
133 
134         // VERIFY
135         assertFalse( "Should not have added the no wait.", result );
136     }
137 
138     /**
139      * Remove a no wait from an unknown facade.
140      */
141     public void testRemoveNoWait_FacadeNotInList()
142     {
143         // SETUP
144         String cacheName = "testRemoveNoWaitFacade_FacadeNotInList";
145         ILateralCacheAttributes cattr = new LateralCacheAttributes();
146         cattr.setCacheName( cacheName );
147 
148         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
149         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
150 
151         // DO WORK
152         boolean result = listener.removeNoWait( noWait );
153 
154         // VERIFY
155         assertFalse( "Should not have removed the no wait.", result );
156     }
157 
158     /**
159      * Remove a no wait from a known facade.
160      */
161     public void testRemoveNoWait_FacadeInList_NoWaitNot()
162     {
163         // SETUP
164         String cacheName = "testAddNoWaitFacade_FacadeInList";
165 
166         @SuppressWarnings("unchecked")
167         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
168         ILateralCacheAttributes cattr = new LateralCacheAttributes();
169         cattr.setCacheName( cacheName );
170 
171         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
172         listener.addNoWaitFacade( cacheName, facade );
173 
174         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
175         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
176 
177         // DO WORK
178         boolean result = listener.removeNoWait( noWait );
179 
180         // VERIFY
181         assertFalse( "Should not have removed the no wait.", result );
182     }
183 
184     /**
185      * Remove a no wait from a known facade.
186      */
187     public void testRemoveNoWait_FacadeInList_NoWaitIs()
188     {
189         // SETUP
190         String cacheName = "testRemoveNoWaitFacade_FacadeInListNoWaitIs";
191 
192         @SuppressWarnings("unchecked")
193         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
194         ILateralCacheAttributes cattr = new LateralCacheAttributes();
195         cattr.setCacheName( cacheName );
196 
197         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
198         listener.addNoWaitFacade( cacheName, facade );
199 
200         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
201         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
202         listener.addNoWait( noWait );
203 
204         // DO WORK
205         boolean result = listener.removeNoWait( noWait );
206 
207         // VERIFY
208         assertTrue( "Should have removed the no wait.", result );
209     }
210 
211     /**
212      * Add a no wait to a known facade.
213      */
214     public void testAddDiscoveredService_FacadeInList_NoWaitNot()
215     {
216         // SETUP
217         String cacheName = "testAddDiscoveredService_FacadeInList_NoWaitNot";
218 
219         ArrayList<String> cacheNames = new ArrayList<String>();
220         cacheNames.add( cacheName );
221 
222         DiscoveredService service = new DiscoveredService();
223         service.setCacheNames( cacheNames );
224         service.setServiceAddress( "localhost" );
225         service.setServicePort( 9999 );
226 
227         // since the no waits are compared by object equality, I have to do this
228         // TODO add an equals method to the noWait.  the problem if is figuring out what to compare.
229         ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes();
230         lca.setTransmissionType( LateralCacheAttributes.Type.TCP );
231         lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() );
232         lca.setCacheName(cacheName);
233         LateralCacheNoWait<String, String> noWait = factory.createCacheNoWait(lca, cacheEventLogger, elementSerializer);
234         // this is the normal process, the discovery service expects it there
235         cacheMgr.addAuxiliaryCache(factory.getName(), cacheName, noWait);
236 
237         @SuppressWarnings("unchecked")
238         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
239         ILateralCacheAttributes cattr = new LateralCacheAttributes();
240         cattr.setCacheName( cacheName );
241         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
242         listener.addNoWaitFacade( cacheName, facade );
243 
244         // DO WORK
245         listener.addDiscoveredService( service );
246 
247         // VERIFY
248         assertTrue( "Should have no wait.", listener.containsNoWait( cacheName, noWait ) );
249     }
250 
251     /**
252      * Remove a no wait from a known facade.
253      */
254     public void testRemoveDiscoveredService_FacadeInList_NoWaitIs()
255     {
256         // SETUP
257         String cacheName = "testRemoveDiscoveredService_FacadeInList_NoWaitIs";
258 
259         ArrayList<String> cacheNames = new ArrayList<String>();
260         cacheNames.add( cacheName );
261 
262         DiscoveredService service = new DiscoveredService();
263         service.setCacheNames( cacheNames );
264         service.setServiceAddress( "localhost" );
265         service.setServicePort( 9999 );
266 
267         // since the no waits are compared by object equality, I have to do this
268         // TODO add an equals method to the noWait.  the problem if is figuring out what to compare.
269         ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes();
270         lca.setTransmissionType( LateralCacheAttributes.Type.TCP );
271         lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() );
272         lca.setCacheName(cacheName);
273         LateralCacheNoWait<String, String> noWait = factory.createCacheNoWait(lca, cacheEventLogger, elementSerializer);
274         // this is the normal process, the discovery service expects it there
275         cacheMgr.addAuxiliaryCache(factory.getName(), cacheName, noWait);
276 
277         @SuppressWarnings("unchecked")
278         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
279         ILateralCacheAttributes cattr = new LateralCacheAttributes();
280         cattr.setCacheName( cacheName );
281         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
282         listener.addNoWaitFacade( cacheName, facade );
283         listener.addDiscoveredService( service );
284 
285         // DO WORK
286         listener.removeDiscoveredService( service );
287 
288         // VERIFY
289         assertFalse( "Should not have no wait.", listener.containsNoWait( cacheName, noWait ) );
290     }
291 }