View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.http.server;
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.MockCacheEventLogger;
24  import org.apache.commons.jcs.engine.CacheElement;
25  import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
26  
27  import java.util.HashSet;
28  
29  /** Unit tests for the service. */
30  public class RemoteHttpCacheServiceUnitTest
31      extends TestCase
32  {
33      /**
34       * Verify event log calls.
35       * <p>
36       * @throws Exception
37       */
38      public void testUpdate_simple()
39          throws Exception
40      {
41          // SETUP
42          MockCompositeCacheManager manager = new MockCompositeCacheManager();
43          MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
44  
45          RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
46          RemoteHttpCacheService<String, String> server =
47              new RemoteHttpCacheService<String, String>( manager, rcsa, cacheEventLogger );
48  
49          String cacheName = "test";
50          String key = "key";
51          long requesterId = 2;
52          CacheElement<String, String> element = new CacheElement<String, String>( cacheName, key, null );
53  
54          // DO WORK
55          server.update( element, requesterId );
56  
57          // VERIFY
58          assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
59          assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
60      }
61  
62      /**
63       * Verify event log calls.
64       * <p>
65       * @throws Exception
66       */
67      public void testGet_simple()
68          throws Exception
69      {
70          // SETUP
71          MockCompositeCacheManager manager = new MockCompositeCacheManager();
72          MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
73  
74          RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
75          RemoteHttpCacheService<String, String> server =
76              new RemoteHttpCacheService<String, String>( manager, rcsa, cacheEventLogger );
77  
78          // DO WORK
79          server.get( "region", "key" );
80  
81          // VERIFY
82          assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
83          assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
84      }
85  
86      /**
87       * Verify event log calls.
88       * <p>
89       * @throws Exception
90       */
91      public void testGetMatching_simple()
92          throws Exception
93      {
94          // SETUP
95          MockCompositeCacheManager manager = new MockCompositeCacheManager();
96          MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
97  
98          RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
99          RemoteHttpCacheService<String, String> server =
100             new RemoteHttpCacheService<String, String>( manager, rcsa, cacheEventLogger );
101 
102         // DO WORK
103         server.getMatching( "region", "pattern", 0 );
104 
105         // VERIFY
106         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
107         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
108     }
109 
110     /**
111      * Verify event log calls.
112      * <p>
113      * @throws Exception
114      */
115     public void testGetMultiple_simple()
116         throws Exception
117     {
118         // SETUP
119         MockCompositeCacheManager manager = new MockCompositeCacheManager();
120         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
121 
122         RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
123         RemoteHttpCacheService<String, String> server =
124             new RemoteHttpCacheService<String, String>( manager, rcsa, cacheEventLogger );
125 
126         // DO WORK
127         server.getMultiple( "region", new HashSet<String>() );
128 
129         // VERIFY
130         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
131         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
132     }
133 
134     /**
135      * Verify event log calls.
136      * <p>
137      * @throws Exception
138      */
139     public void testRemove_simple()
140         throws Exception
141     {
142         // SETUP
143         MockCompositeCacheManager manager = new MockCompositeCacheManager();
144         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
145 
146         RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
147         RemoteHttpCacheService<String, String> server =
148             new RemoteHttpCacheService<String, String>( manager, rcsa, cacheEventLogger );
149 
150         // DO WORK
151         server.remove( "region", "key" );
152 
153         // VERIFY
154         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
155         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
156     }
157 
158     /**
159      * Verify event log calls.
160      * <p>
161      * @throws Exception
162      */
163     public void testRemoveAll_simple()
164         throws Exception
165     {
166         // SETUP
167         MockCompositeCacheManager manager = new MockCompositeCacheManager();
168         MockCacheEventLogger cacheEventLogger = new MockCacheEventLogger();
169 
170         RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
171         RemoteHttpCacheService<String, String> server =
172             new RemoteHttpCacheService<String, String>( manager, rcsa, cacheEventLogger );
173 
174         // DO WORK
175         server.removeAll( "region" );
176 
177         // VERIFY
178         assertEquals( "Start should have been called.", 1, cacheEventLogger.startICacheEventCalls );
179         assertEquals( "End should have been called.", 1, cacheEventLogger.endICacheEventCalls );
180     }
181 }