View Javadoc
1   package org.apache.commons.jcs3.auxiliary.lateral;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes;
7   import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
8   
9   /*
10   * Licensed to the Apache Software Foundation (ASF) under one
11   * or more contributor license agreements.  See the NOTICE file
12   * distributed with this work for additional information
13   * regarding copyright ownership.  The ASF licenses this file
14   * to you under the Apache License, Version 2.0 (the
15   * "License"); you may not use this file except in compliance
16   * with the License.  You may obtain a copy of the License at
17   *
18   *   http://www.apache.org/licenses/LICENSE-2.0
19   *
20   * Unless required by applicable law or agreed to in writing,
21   * software distributed under the License is distributed on an
22   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23   * KIND, either express or implied.  See the License for the
24   * specific language governing permissions and limitations
25   * under the License.
26   */
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * Tests for LateralCacheNoWaitFacade.
32   */
33  public class LateralCacheNoWaitFacadeUnitTest
34      extends TestCase
35  {
36      private LateralCacheNoWaitFacade<String, String> facade;
37      private LateralCache<String, String> cache;
38  
39      @Override
40      protected void setUp() throws Exception
41      {
42          // SETUP
43          List<LateralCacheNoWait<String, String>> noWaits = new ArrayList<>();
44          TCPLateralCacheAttributes cattr = new TCPLateralCacheAttributes();
45          cattr.setCacheName( "testCache1" );
46          cattr.setTcpServer("localhost:7890");
47  
48  
49          facade = new LateralCacheNoWaitFacade<>( null, noWaits, cattr );
50          cache = new LateralCache<>(cattr, new ZombieCacheServiceNonLocal<>(), null);
51      }
52  
53      /**
54       * Verify that we can remove an item.
55       */
56      public void testAddThenRemoveNoWait_InList()
57      {
58          LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
59  
60          // DO WORK
61          facade.addNoWait( noWait );
62  
63          // VERIFY
64          assertTrue( "Should be in the list.", facade.containsNoWait( noWait ) );
65  
66          // DO WORK
67          facade.removeNoWait( noWait );
68  
69          // VERIFY
70          assertEquals( "Should have 0", 0, facade.getNoWaitSize() );
71          assertFalse( "Should not be in the list. ", facade.containsNoWait( noWait ) );
72      }
73  
74      /**
75       * Verify that we can remove an item.
76       */
77      public void testAddThenRemoveNoWait_InListSize2()
78      {
79          final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
80          noWait.setIdentityKey("1234");
81          final LateralCacheNoWait<String, String> noWait2 = new LateralCacheNoWait<>( cache );
82          noWait2.setIdentityKey("2345");
83  
84          // DO WORK
85          facade.addNoWait( noWait );
86          facade.addNoWait( noWait2 );
87  
88          // VERIFY
89          assertEquals( "Should have 2", 2, facade.getNoWaitSize() );
90          assertTrue( "Should be in the list.", facade.containsNoWait( noWait ) );
91          assertTrue( "Should be in the list.", facade.containsNoWait( noWait2 ) );
92  
93          // DO WORK
94          facade.removeNoWait( noWait );
95  
96          // VERIFY
97          assertEquals( "Should only have 1", 1, facade.getNoWaitSize() );
98          assertFalse( "Should not be in the list. ", facade.containsNoWait( noWait ) );
99          assertTrue( "Should be in the list.", facade.containsNoWait( noWait2 ) );
100     }
101 
102     /**
103      * Verify that we can remove an item.
104      */
105     public void testAdd_InList()
106     {
107         final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
108 
109         // DO WORK
110         facade.addNoWait( noWait );
111         facade.addNoWait( noWait );
112 
113         // VERIFY
114         assertTrue( "Should be in the list.", facade.containsNoWait( noWait ) );
115         assertEquals( "Should only have 1", 1, facade.getNoWaitSize() );
116     }
117 
118     /**
119      * Verify that we can remove an item.
120      */
121     public void testAddThenRemoveNoWait_NotInList()
122     {
123         final LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<>( cache );
124 
125         // DO WORK
126         facade.removeNoWait( noWait );
127 
128         // VERIFY
129         assertFalse( "Should not be in the list.", facade.containsNoWait( noWait ) );
130     }
131 }