View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote;
2   
3   import java.util.Properties;
4   
5   import org.apache.commons.jcs.JCS;
6   import org.apache.commons.jcs.access.CacheAccess;
7   import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
8   import org.apache.commons.jcs.auxiliary.MockCacheEventLogger;
9   import org.apache.commons.jcs.auxiliary.remote.server.RemoteCacheServerFactory;
10  import org.apache.commons.jcs.engine.CacheElement;
11  import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
12  import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
13  import org.apache.commons.jcs.engine.control.MockElementSerializer;
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
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   * @author Aaron SMuts
40   */
41  public class TestRemoteCache
42      extends TestCase
43  {
44      /** The logger */
45      private static final Log log = LogFactory.getLog( TestRemoteCache.class );
46  
47      /**
48       * Start the cache.
49       */
50      public TestRemoteCache()
51      {
52          super();
53          try
54          {
55              System.out.println( "main> creating registry on the localhost" );
56              RemoteUtils.createRegistry( 1101 );
57              Properties config = RemoteUtils.loadProps("/TestRemoteServer.ccf");
58  
59              RemoteCacheServerFactory.startup( "localhost", 1101, config);
60          }
61          catch ( Exception e )
62          {
63              e.printStackTrace();
64          }
65      }
66  
67      /**
68       * Test setup
69       */
70      @Override
71      public void setUp()
72      {
73          JCS.setConfigFilename( "/TestRemoteClient.ccf" );
74      }
75  
76      /**
77       * @throws Exception
78       *
79       *
80       */
81      public void skiptestSimpleSend()
82          throws Exception
83      {
84          log.info( "testSimpleSend" );
85  
86          CacheAccess<String, String> cache = JCS.getInstance( "testCache" );
87  
88          log.info( "cache = " + cache );
89  
90          for ( int i = 0; i < 1000; i++ )
91          {
92  //            System.out.println( "puttting " + i );
93              cache.put( "key" + i, "data" + i );
94  //            System.out.println( "put " + i );
95              log.info( "put " + i );
96          }
97      }
98  
99      /**
100      * @throws Exception
101      */
102     public void testService()
103         throws Exception
104     {
105 
106         Thread.sleep( 100 );
107 
108         ICompositeCacheManager cacheMgr = new MockCompositeCacheManager();
109 
110         RemoteCacheAttributes rca = new RemoteCacheAttributes();
111         rca.setRemoteLocation( "localhost", 1101 );
112         rca.setCacheName( "testCache" );
113 
114         RemoteCacheFactory factory = new RemoteCacheFactory();
115         factory.initialize();
116         RemoteCacheManager mgr = factory.getManager( rca, cacheMgr, new MockCacheEventLogger(), new MockElementSerializer() );
117         AuxiliaryCache<String, String> cache = mgr.getCache( rca );
118 
119         int numMes = 100;
120         for ( int i = 0; i < numMes; i++ )
121         {
122             String message = "adsfasasfasfasdasf";
123             CacheElement<String, String> ce = new CacheElement<String, String>( "key" + 1, "data" + i, message );
124             cache.update( ce );
125 //            System.out.println( "put " + ce );
126         }
127 
128         // Thread.sleep( 2000 );
129 
130         /*
131          * // the receiver instance. JCS cacheReceiver = JCS.getInstance(
132          * "testCache" );
133          *
134          * log.info( "cache = " + cache );
135          *
136          * for ( int i = 0; i < numMes; i++ ) { System.out.println( "getting " +
137          * i ); Object data = cacheReceiver.get( "key" + i );
138          * System.out.println( i + " = " + data ); }
139          */
140     }
141 }