1   package org.apache.jcs.auxiliary.remote;
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 org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
25  import org.apache.jcs.engine.CacheElement;
26  import org.apache.jcs.engine.behavior.ICacheElement;
27  import org.apache.jcs.engine.behavior.ICacheElementSerialized;
28  import org.apache.jcs.utils.serialization.SerializationConversionUtil;
29  
30  /***
31   * Unit Tests for the Remote Cache.
32   * <p>
33   * @author admin
34   */
35  public class RemoteCacheUnitTest
36      extends TestCase
37  {
38      /***
39       * Verify that the remote service update method is called. The remote cache serializes the obect
40       * first.
41       * <p>
42       * @throws Exception
43       */
44      public void testUpdate()
45          throws Exception
46      {
47          // SETUP
48          IRemoteCacheAttributes cattr = new RemoteCacheAttributes();
49          RemoteCacheServiceMockImpl service = new RemoteCacheServiceMockImpl();
50          RemoteCacheListenerMockImpl listener = new RemoteCacheListenerMockImpl();
51  
52          RemoteCache remoteCache = new RemoteCache( cattr, service, listener );
53  
54          String cacheName = "testUpdate";
55  
56          // DO WORK
57          ICacheElement element = new CacheElement( cacheName, "key", "value" );
58          remoteCache.update( element );
59  
60          // VERIFY
61          assertTrue( "The element should be in the serialized warapper.",
62                      service.lastUpdate instanceof ICacheElementSerialized );
63          ICacheElement result = SerializationConversionUtil
64              .getDeSerializedCacheElement( (ICacheElementSerialized) service.lastUpdate, remoteCache
65                  .getElementSerializer() );
66          assertEquals( "Wrong element updated.", element.getVal(), result.getVal() );
67      }
68  
69      /***
70       * Verify that when we call fix events queued in the zombie are propagated to the new service.
71       * <p>
72       * @throws Exception
73       */
74      public void testUpdateZombieThenFix()
75          throws Exception
76      {
77          // SETUP
78          IRemoteCacheAttributes cattr = new RemoteCacheAttributes();
79          ZombieRemoteCacheService zombie = new ZombieRemoteCacheService( 10 );
80          RemoteCacheServiceMockImpl service = new RemoteCacheServiceMockImpl();
81          RemoteCacheListenerMockImpl listener = new RemoteCacheListenerMockImpl();
82  
83          // set the zombir
84          RemoteCache remoteCache = new RemoteCache( cattr, zombie, listener );
85  
86          String cacheName = "testUpdate";
87  
88          // DO WORK
89          ICacheElement element = new CacheElement( cacheName, "key", "value" );
90          remoteCache.update( element );
91          // set the new service, this should call propogate
92          remoteCache.fixCache( service );
93  
94          // VERIFY
95          assertTrue( "The element should be in the serialized warapper.",
96                      service.lastUpdate instanceof ICacheElementSerialized );
97          ICacheElement result = SerializationConversionUtil
98              .getDeSerializedCacheElement( (ICacheElementSerialized) service.lastUpdate, remoteCache
99                  .getElementSerializer() );
100         assertEquals( "Wrong element updated.", element.getVal(), result.getVal() );
101     }
102 }