View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote;
2   
3   import java.util.Properties;
4   
5   import org.apache.commons.jcs3.auxiliary.MockCacheEventLogger;
6   import org.apache.commons.jcs3.engine.control.MockCompositeCacheManager;
7   import org.apache.commons.jcs3.engine.control.MockElementSerializer;
8   import org.apache.commons.jcs3.JCS;
9   import org.apache.commons.jcs3.access.CacheAccess;
10  import org.apache.commons.jcs3.auxiliary.AuxiliaryCache;
11  import org.apache.commons.jcs3.auxiliary.remote.server.RemoteCacheServerFactory;
12  import org.apache.commons.jcs3.engine.CacheElement;
13  import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
14  import org.apache.commons.jcs3.log.Log;
15  import org.apache.commons.jcs3.log.LogManager;
16  
17  /*
18   * Licensed to the Apache Software Foundation (ASF) under one
19   * or more contributor license agreements.  See the NOTICE file
20   * distributed with this work for additional information
21   * regarding copyright ownership.  The ASF licenses this file
22   * to you under the Apache License, Version 2.0 (the
23   * "License"); you may not use this file except in compliance
24   * with the License.  You may obtain a copy of the License at
25   *
26   *   http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing,
29   * software distributed under the License is distributed on an
30   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
31   * KIND, either express or implied.  See the License for the
32   * specific language governing permissions and limitations
33   * under the License.
34   */
35  
36  import junit.framework.TestCase;
37  
38  /**
39   */
40  public class TestRemoteCache
41      extends TestCase
42  {
43      /** The logger */
44      private static final Log log = LogManager.getLog( TestRemoteCache.class );
45  
46      /**
47       * Start the cache.
48       */
49      public TestRemoteCache()
50      {
51          try
52          {
53              System.out.println( "main> creating registry on the localhost" );
54              RemoteUtils.createRegistry( 1101 );
55              final Properties config = RemoteUtils.loadProps("/TestRemoteServer.ccf");
56  
57              RemoteCacheServerFactory.startup( "localhost", 1101, config);
58          }
59          catch ( final Exception e )
60          {
61              e.printStackTrace();
62          }
63      }
64  
65      /**
66       * Test setup
67       */
68      @Override
69      public void setUp()
70      {
71          JCS.setConfigFilename( "/TestRemoteClient.ccf" );
72      }
73  
74      /**
75       * @throws Exception
76       *
77       *
78       */
79      public void skiptestSimpleSend()
80          throws Exception
81      {
82          log.info( "testSimpleSend" );
83  
84          final CacheAccess<String, String> cache = JCS.getInstance( "testCache" );
85  
86          log.info( "cache = " + cache );
87  
88          for ( int i = 0; i < 1000; i++ )
89          {
90  //            System.out.println( "puttting " + i );
91              cache.put( "key" + i, "data" + i );
92  //            System.out.println( "put " + i );
93              log.info( "put " + i );
94          }
95      }
96  
97      /**
98       * @throws Exception
99       */
100     public void testService()
101         throws Exception
102     {
103 
104         Thread.sleep( 100 );
105 
106         final ICompositeCacheManager cacheMgr = new MockCompositeCacheManager();
107 
108         final RemoteCacheAttributes rca = new RemoteCacheAttributes();
109         rca.setRemoteLocation( "localhost", 1101 );
110         rca.setCacheName( "testCache" );
111 
112         final RemoteCacheFactory factory = new RemoteCacheFactory();
113         factory.initialize();
114         final RemoteCacheManager mgr = factory.getManager( rca, cacheMgr, new MockCacheEventLogger(), new MockElementSerializer() );
115         final AuxiliaryCache<String, String> cache = mgr.getCache( rca );
116 
117         final int numMes = 100;
118         for ( int i = 0; i < numMes; i++ )
119         {
120             final String message = "adsfasasfasfasdasf";
121             final CacheElement<String, String> ce = new CacheElement<>( "key" + 1, "data" + i, message );
122             cache.update( ce );
123 //            System.out.println( "put " + ce );
124         }
125 
126         // Thread.sleep( 2000 );
127 
128         /*
129          * // the receiver instance. JCS cacheReceiver = JCS.getInstance(
130          * "testCache" );
131          *
132          * log.info( "cache = " + cache );
133          *
134          * for ( int i = 0; i < numMes; i++ ) { System.out.println( "getting " +
135          * i ); Object data = cacheReceiver.get( "key" + i );
136          * System.out.println( i + " = " + data ); }
137          */
138     }
139 }