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 java.io.Serializable;
23  import java.util.Collections;
24  import java.util.Set;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.commons.jcs.auxiliary.remote.MockRemoteCacheService;
29  import org.apache.commons.jcs.auxiliary.remote.util.RemoteCacheRequestFactory;
30  import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest;
31  import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse;
32  import org.apache.commons.jcs.engine.CacheElement;
33  
34  /** Unit tests for the servlet. */
35  public class RemoteHttpCacheServletUnitTest
36      extends TestCase
37  {
38      private RemoteHttpCacheServlet servlet;
39      private MockRemoteCacheService<Serializable, Serializable> remoteHttpCacheService;
40  
41      /**
42       * @see junit.framework.TestCase#setUp()
43       */
44      @Override
45      protected void setUp() throws Exception
46      {
47          super.setUp();
48          servlet = new RemoteHttpCacheServlet();
49          servlet.init(null);
50  
51          remoteHttpCacheService = new MockRemoteCacheService<Serializable, Serializable>();
52          servlet.setRemoteCacheService( remoteHttpCacheService );
53      }
54  
55      /**
56       * @see junit.framework.TestCase#tearDown()
57       */
58      @Override
59      protected void tearDown() throws Exception
60      {
61          servlet.destroy();
62          super.tearDown();
63      }
64  
65      /** Verify that we balk and return an error. */
66      public void testProcessRequest_null()
67      {
68          RemoteCacheRequest<Serializable, Serializable> request = null;
69  
70          // DO WORK
71          RemoteCacheResponse<Object> result = servlet.processRequest( request );
72  
73          // VERIFY
74          assertNotNull( "Should have a result.", result );
75          assertTrue( "Should have 'The request is null' in the errorMessage", result.getErrorMessage().indexOf( "The request is null" ) != -1 );
76          assertTrue( "Should have 'The request is null' in the toString", result.toString().indexOf( "The request is null" ) != -1 );
77      }
78  
79      /** Verify that the service is called. */
80      public void testProcessRequest_Get()
81      {
82          String cacheName = "test";
83          Serializable key = "key";
84          long requesterId = 2;
85          RemoteCacheRequest<Serializable, Serializable> request = RemoteCacheRequestFactory.createGetRequest( cacheName, key, requesterId );
86  
87          // DO WORK
88          RemoteCacheResponse<Object> result = servlet.processRequest( request );
89  
90          // VERIFY
91          assertNotNull( "Should have a result.", result );
92          assertEquals( "Wrong key.", key, remoteHttpCacheService.lastGetKey );
93      }
94  
95      /** Verify that the service is called. */
96      public void testProcessRequest_GetMatching()
97      {
98          String cacheName = "test";
99          String pattern = "pattern";
100         long requesterId = 2;
101         RemoteCacheRequest<Serializable, Serializable> request = RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern,
102                                                                                                   requesterId );
103 
104         // DO WORK
105         RemoteCacheResponse<Object> result = servlet.processRequest( request );
106 
107         // VERIFY
108         assertNotNull( "Should have a result.", result );
109         assertEquals( "Wrong pattern.", pattern, remoteHttpCacheService.lastGetMatchingPattern );
110     }
111 
112     /** Verify that the service is called. */
113     public void testProcessRequest_GetMultiple()
114     {
115         String cacheName = "test";
116         Set<Serializable> keys = Collections.emptySet();
117         long requesterId = 2;
118         RemoteCacheRequest<Serializable, Serializable> request = RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys,
119                                                                                                   requesterId );
120 
121         // DO WORK
122         RemoteCacheResponse<Object> result = servlet.processRequest( request );
123 
124         // VERIFY
125         assertNotNull( "Should have a result.", result );
126         assertEquals( "Wrong keys.", keys, remoteHttpCacheService.lastGetMultipleKeys );
127 
128     }
129 
130     /** Verify that the service is called. */
131     public void testProcessRequest_Update()
132     {
133         String cacheName = "test";
134         String key = "key";
135         long requesterId = 2;
136         CacheElement<Serializable, Serializable> element = new CacheElement<Serializable, Serializable>( cacheName, key, null );
137         RemoteCacheRequest<Serializable, Serializable> request = RemoteCacheRequestFactory.createUpdateRequest( element, requesterId );
138 
139         // DO WORK
140         RemoteCacheResponse<Object> result = servlet.processRequest( request );
141 
142         // VERIFY
143         assertNotNull( "Should have a result.", result );
144         assertEquals( "Wrong object.", element, remoteHttpCacheService.lastUpdate );
145     }
146 
147     /** Verify that the service is called. */
148     public void testProcessRequest_Remove()
149     {
150         String cacheName = "test";
151         Serializable key = "key";
152         long requesterId = 2;
153         RemoteCacheRequest<Serializable, Serializable> request = RemoteCacheRequestFactory.createRemoveRequest( cacheName, key, requesterId );
154 
155         // DO WORK
156         RemoteCacheResponse<Object> result = servlet.processRequest( request );
157 
158         // VERIFY
159         assertNotNull( "Should have a result.", result );
160         assertEquals( "Wrong key.", key, remoteHttpCacheService.lastRemoveKey );
161     }
162 
163     /** Verify that the service is called. */
164     public void testProcessRequest_RemoveAll()
165     {
166         String cacheName = "testRemoveALl";
167         long requesterId = 2;
168         RemoteCacheRequest<Serializable, Serializable> request = RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId );
169 
170         // DO WORK
171         RemoteCacheResponse<Object> result = servlet.processRequest( request );
172 
173         // VERIFY
174         assertNotNull( "Should have a result.", result );
175         assertEquals( "Wrong cacheName.", cacheName, remoteHttpCacheService.lastRemoveAllCacheName );
176     }
177 }