View Javadoc
1   package org.apache.commons.jcs.auxiliary.lateral;
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.behavior.ILateralCacheAttributes;
24  
25  /**
26   * Tests for LateralCacheNoWaitFacade.
27   */
28  public class LateralCacheNoWaitFacadeUnitTest
29      extends TestCase
30  {
31      /**
32       * Verify that we can remove an item.
33       */
34      public void testAddThenRemoveNoWait_InList()
35      {
36          // SETUP
37          @SuppressWarnings("unchecked")
38          LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
39          ILateralCacheAttributes cattr = new LateralCacheAttributes();
40          cattr.setCacheName( "testCache1" );
41  
42          LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
43  
44          LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
45          LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
46  
47          // DO WORK
48          facade.addNoWait( noWait );
49  
50          // VERIFY
51          assertTrue( "Should be in the list.", facade.containsNoWait( noWait ) );
52  
53          // DO WORK
54          facade.removeNoWait( noWait );
55  
56          // VERIFY
57          assertEquals( "Should have 0", 0, facade.noWaits.length );
58          assertFalse( "Should not be in the list. ", facade.containsNoWait( noWait ) );
59      }
60  
61      /**
62       * Verify that we can remove an item.
63       */
64      public void testAddThenRemoveNoWait_InListSize2()
65      {
66          // SETUP
67          @SuppressWarnings("unchecked")
68          LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
69          ILateralCacheAttributes cattr = new LateralCacheAttributes();
70          cattr.setCacheName( "testCache1" );
71  
72          LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
73  
74          LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
75          LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
76          LateralCacheNoWait<String, String> noWait2 = new LateralCacheNoWait<String, String>( cache );
77  
78          // DO WORK
79          facade.addNoWait( noWait );
80          facade.addNoWait( noWait2 );
81  
82          // VERIFY
83          assertEquals( "Should have 2", 2, facade.noWaits.length );
84          assertTrue( "Should be in the list.", facade.containsNoWait( noWait ) );
85          assertTrue( "Should be in the list.", facade.containsNoWait( noWait2 ) );
86  
87          // DO WORK
88          facade.removeNoWait( noWait );
89  
90          // VERIFY
91          assertEquals( "Should only have 1", 1, facade.noWaits.length );
92          assertFalse( "Should not be in the list. ", facade.containsNoWait( noWait ) );
93          assertTrue( "Should be in the list.", facade.containsNoWait( noWait2 ) );
94      }
95  
96      /**
97       * Verify that we can remove an item.
98       */
99      public void testAdd_InList()
100     {
101         // SETUP
102         @SuppressWarnings("unchecked")
103         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
104         ILateralCacheAttributes cattr = new LateralCacheAttributes();
105         cattr.setCacheName( "testCache1" );
106 
107         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
108 
109         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
110         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
111 
112         // DO WORK
113         facade.addNoWait( noWait );
114         facade.addNoWait( noWait );
115 
116         // VERIFY
117         assertTrue( "Should be in the list.", facade.containsNoWait( noWait ) );
118         assertEquals( "Should only have 1", 1, facade.noWaits.length );
119     }
120 
121     /**
122      * Verify that we can remove an item.
123      */
124     public void testAddThenRemoveNoWait_NotInList()
125     {
126         // SETUP
127         @SuppressWarnings("unchecked")
128         LateralCacheNoWait<String, String>[] noWaits = new LateralCacheNoWait[0];
129         ILateralCacheAttributes cattr = new LateralCacheAttributes();
130         cattr.setCacheName( "testCache1" );
131 
132         LateralCacheNoWaitFacade<String, String> facade = new LateralCacheNoWaitFacade<String, String>( null, noWaits, cattr );
133 
134         LateralCache<String, String> cache = new LateralCache<String, String>( cattr );
135         LateralCacheNoWait<String, String> noWait = new LateralCacheNoWait<String, String>( cache );
136 
137         // DO WORK
138         facade.removeNoWait( noWait );
139 
140         // VERIFY
141         assertFalse( "Should not be in the list.", facade.containsNoWait( noWait ) );
142     }
143 }