View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote.util;
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  
24  import java.io.Serializable;
25  import java.util.Collections;
26  import java.util.Set;
27  
28  import org.apache.commons.jcs3.auxiliary.remote.value.RemoteCacheRequest;
29  import org.apache.commons.jcs3.auxiliary.remote.value.RemoteRequestType;
30  import org.apache.commons.jcs3.engine.CacheElement;
31  
32  /** Unit tests for the request creator. */
33  public class RemoteCacheRequestFactoryUnitTest
34      extends TestCase
35  {
36      /** Simple test */
37      public void testCreateGetRequest_Normal()
38      {
39          // SETUP
40          final String cacheName = "test";
41          final Serializable key = "key";
42          final long requesterId = 2;
43  
44          // DO WORK
45          final RemoteCacheRequest<Serializable, Serializable> result =
46              RemoteCacheRequestFactory.createGetRequest( cacheName, key, requesterId );
47  
48          // VERIFY
49          assertNotNull( "Should have a result", result );
50          assertEquals( "Wrong cacheName", cacheName, result.getCacheName() );
51          assertEquals( "Wrong type", RemoteRequestType.GET, result.getRequestType() );
52      }
53  
54      /** Simple test */
55      public void testCreateGetMatchingRequest_Normal()
56      {
57          // SETUP
58          final String cacheName = "test";
59          final String pattern = "pattern";
60          final long requesterId = 2;
61  
62          // DO WORK
63          final RemoteCacheRequest<Serializable, Serializable> result =
64              RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId );
65  
66          // VERIFY
67          assertNotNull( "Should have a result", result );
68          assertEquals( "Wrong cacheName", cacheName, result.getCacheName() );
69          assertEquals( "Wrong type", RemoteRequestType.GET_MATCHING, result.getRequestType() );
70      }
71  
72      /** Simple test */
73      public void testCreateGetMultipleRequest_Normal()
74      {
75          // SETUP
76          final String cacheName = "test";
77          final Set<Serializable> keys = Collections.emptySet();
78          final long requesterId = 2;
79  
80          // DO WORK
81          final RemoteCacheRequest<Serializable, Serializable> result =
82              RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId );
83  
84          // VERIFY
85          assertNotNull( "Should have a result", result );
86          assertEquals( "Wrong cacheName", cacheName, result.getCacheName() );
87          assertEquals( "Wrong type", RemoteRequestType.GET_MULTIPLE, result.getRequestType() );
88      }
89  
90      /** Simple test */
91      public void testCreateRemoveRequest_Normal()
92      {
93          // SETUP
94          final String cacheName = "test";
95          final Serializable key = "key";
96          final long requesterId = 2;
97  
98          // DO WORK
99          final RemoteCacheRequest<Serializable, Serializable> result = RemoteCacheRequestFactory
100             .createRemoveRequest( cacheName, key, requesterId );
101 
102         // VERIFY
103         assertNotNull( "Should have a result", result );
104         assertEquals( "Wrong cacheName", cacheName, result.getCacheName() );
105         assertEquals( "Wrong type", RemoteRequestType.REMOVE, result.getRequestType() );
106     }
107 
108     /** Simple test */
109     public void testCreateRemoveAllRequest_Normal()
110     {
111         // SETUP
112         final String cacheName = "test";
113         final long requesterId = 2;
114 
115         // DO WORK
116         final RemoteCacheRequest<Serializable, Serializable> result =
117             RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId );
118 
119         // VERIFY
120         assertNotNull( "Should have a result", result );
121         assertEquals( "Wrong cacheName", cacheName, result.getCacheName() );
122         assertEquals( "Wrong type", RemoteRequestType.REMOVE_ALL, result.getRequestType() );
123     }
124 
125     /** Simple test */
126     public void testCreateUpdateRequest_Normal()
127     {
128         // SETUP
129         final String cacheName = "test";
130         final Serializable key = "key";
131         final long requesterId = 2;
132 
133         final CacheElement<Serializable, Serializable> element =
134             new CacheElement<>( cacheName, key, null );
135 
136         // DO WORK
137         final RemoteCacheRequest<Serializable, Serializable> result =
138             RemoteCacheRequestFactory.createUpdateRequest( element, requesterId );
139 
140         // VERIFY
141         assertNotNull( "Should have a result", result );
142         assertEquals( "Wrong cacheName", cacheName, result.getCacheName() );
143         assertEquals( "Wrong type", RemoteRequestType.UPDATE, result.getRequestType() );
144     }
145 }