Author: asmuts
Date: Fri Aug 25 21:59:44 2006
New Revision: 437083

URL: http://svn.apache.org/viewvc?rev=437083&view=rev
Log:
adding unit test

Modified:
    
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java

Modified: 
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java
URL: 
http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java?rev=437083&r1=437082&r2=437083&view=diff
==============================================================================
--- 
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java
 (original)
+++ 
jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/remote/server/BasicRemoteCacheClientServerUnitTest.java
 Fri Aug 25 21:59:44 2006
@@ -13,6 +13,7 @@
 
 import org.apache.jcs.auxiliary.AuxiliaryCache;
 import org.apache.jcs.auxiliary.remote.RemoteCacheAttributes;
+import org.apache.jcs.auxiliary.remote.RemoteCacheListenerMockImpl;
 import org.apache.jcs.auxiliary.remote.RemoteCacheManager;
 import org.apache.jcs.engine.CacheElement;
 import org.apache.jcs.engine.behavior.ICacheElement;
@@ -27,6 +28,16 @@
 public class BasicRemoteCacheClientServerUnitTest
     extends TestCase
 {
+    RemoteCacheServer server = null;
+
+    /**
+     * Starts the server. This is not in a setup, since the server is slow to 
kill right now.
+     */
+    public BasicRemoteCacheClientServerUnitTest()
+    {
+        String configFile = "TestRemoteCacheClientServer.ccf";
+        server = RemoteCacheServerStartupUtil.startServerUsingProperties( 
configFile );
+    }
 
     /**
      * Verify that we can start the remote cache server. Send an item to the 
remote. Verify that the
@@ -44,9 +55,6 @@
         throws Exception
     {
         // SETUP
-        String configFile = "TestRemoteCacheClientServer.ccf";
-        RemoteCacheServer server = 
RemoteCacheServerStartupUtil.startServerUsingProperties( configFile );
-
         CompositeCacheManagerMockImpl compositeCacheManager = new 
CompositeCacheManagerMockImpl();
 
         RemoteCacheAttributes attributes = new RemoteCacheAttributes();
@@ -59,19 +67,144 @@
         AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
 
         // DO WORK
+        int numPutsPrior = server.getPutCount();
+        ICacheElement element = new CacheElement( regionName, "key", "value" );
+        cache.update( element );
+        SleepUtil.sleepAtLeast( 50 );
+
+        // VERIFY
+        System.out.println( server.getStats() );
+        assertEquals( "Wrong number of puts", 1, server.getPutCount() - 
numPutsPrior );
+
+        // DO WORK
+        ICacheElement result = cache.get( "key" );
+
+        // VERIFY
+        assertEquals( "Wrong element.", element.getVal(), result.getVal() );
+    }
+
+    /**
+     * Verify that we can remove an item via the remote server.
+     * <p>
+     * @throws Exception
+     */
+    public void testPutRemove()
+        throws Exception
+    {
+        // SETUP
+        CompositeCacheManagerMockImpl compositeCacheManager = new 
CompositeCacheManagerMockImpl();
+
+        RemoteCacheAttributes attributes = new RemoteCacheAttributes();
+        attributes.setRemoteHost( "localhost" );
+        attributes.setLocalPort( 1202 );
+        attributes.setRemotePort( 1101 );
+
+        RemoteCacheManager remoteCacheManager = 
RemoteCacheManager.getInstance( attributes, compositeCacheManager );
+        String regionName = "testPutRemove";
+        AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
+
+        // DO WORK
+        int numPutsPrior = server.getPutCount();
         ICacheElement element = new CacheElement( regionName, "key", "value" );
         cache.update( element );
         SleepUtil.sleepAtLeast( 50 );
 
         // VERIFY
         System.out.println( server.getStats() );
-        assertEquals( "Wrong number of puts", 1, server.getPutCount() );
+        assertEquals( "Wrong number of puts", 1, server.getPutCount() - 
numPutsPrior );
 
         // DO WORK
         ICacheElement result = cache.get( "key" );
 
         // VERIFY
         assertEquals( "Wrong element.", element.getVal(), result.getVal() );
+
+        // DO WORK
+        cache.remove( "key" );
+        SleepUtil.sleepAtLeast( 50 );
+        ICacheElement resultAfterRemote = cache.get( "key" );
+
+        // VERIFY
+        assertNull( "Element resultAfterRemote should be null.", 
resultAfterRemote );
+    }
+
+    /**
+     * Register a listener with the server. Send an update. Verify that the 
listener received it.
+     * @throws Exception
+     */
+    public void testPutAndListen()
+        throws Exception
+    {
+        // SETUP
+        CompositeCacheManagerMockImpl compositeCacheManager = new 
CompositeCacheManagerMockImpl();
+
+        RemoteCacheAttributes attributes = new RemoteCacheAttributes();
+        attributes.setRemoteHost( "localhost" );
+        attributes.setLocalPort( 1202 );
+        attributes.setRemotePort( 1101 );
+
+        RemoteCacheManager remoteCacheManager = 
RemoteCacheManager.getInstance( attributes, compositeCacheManager );
+        String regionName = "testPutAndListen";
+        AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
+
+        RemoteCacheListenerMockImpl listener = new 
RemoteCacheListenerMockImpl();
+        server.addCacheListener( regionName, listener );
+
+        // DO WORK
+        int numPutsPrior = server.getPutCount();
+        ICacheElement element = new CacheElement( regionName, "key", "value" );
+        cache.update( element );
+        SleepUtil.sleepAtLeast( 50 );
+
+        // VERIFY
+        try
+        {
+            System.out.println( server.getStats() );
+            assertEquals( "Wrong number of puts", 1, server.getPutCount() - 
numPutsPrior );
+            assertEquals( "Wrong number of puts to listener.", 1, 
listener.putCount );
+        }
+        finally
+        {
+            // remove from all regions.
+            server.removeCacheListener( listener );
+        }
     }
 
+    /**
+     * Register a listener with the server. Send an update. Verify that the 
listener received it.
+     * @throws Exception
+     */
+    public void testPutaMultipleAndListen()
+        throws Exception
+    {
+        // SETUP
+        CompositeCacheManagerMockImpl compositeCacheManager = new 
CompositeCacheManagerMockImpl();
+
+        RemoteCacheAttributes attributes = new RemoteCacheAttributes();
+        attributes.setRemoteHost( "localhost" );
+        attributes.setLocalPort( 1202 );
+        attributes.setRemotePort( 1101 );
+
+        RemoteCacheManager remoteCacheManager = 
RemoteCacheManager.getInstance( attributes, compositeCacheManager );
+        String regionName = "testPutAndListen";
+        AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
+
+        RemoteCacheListenerMockImpl listener = new 
RemoteCacheListenerMockImpl();
+        server.addCacheListener( regionName, listener );
+
+        // DO WORK
+        int numPutsPrior = server.getPutCount();
+        int numToPut = 100;
+        for ( int i = 0; i < numToPut; i++ )
+        {
+            ICacheElement element = new CacheElement( regionName, "key" + 1, 
"value" + i );
+            cache.update( element );
+        }
+        SleepUtil.sleepAtLeast( 500 );
+
+        // VERIFY
+        System.out.println( server.getStats() );
+        assertEquals( "Wrong number of puts", numToPut, server.getPutCount() - 
numPutsPrior );
+        assertEquals( "Wrong number of puts to listener.", numToPut, 
listener.putCount );
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to