geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgenen...@apache.org
Subject svn commit: r471033 - in /geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache: server/GCacheManagerTest.java transports/tcp/TCPNotificationTest.java
Date Fri, 03 Nov 2006 22:52:42 GMT
Author: jgenender
Date: Fri Nov  3 14:52:42 2006
New Revision: 471033

URL: http://svn.apache.org/viewvc?view=rev&rev=471033
Log:
Server tests

Modified:
    geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/server/GCacheManagerTest.java
    geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPNotificationTest.java

Modified: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/server/GCacheManagerTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/server/GCacheManagerTest.java?view=diff&rev=471033&r1=471032&r2=471033
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/server/GCacheManagerTest.java
(original)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/server/GCacheManagerTest.java
Fri Nov  3 14:52:42 2006
@@ -19,16 +19,154 @@
 package org.apache.geronimo.gcache.server;
 
 import org.testng.annotations.Test;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.AfterClass;
+import org.apache.geronimo.gcache.command.*;
+import org.apache.geronimo.gcache.util.CipherUtil;
+import org.apache.geronimo.gcache.transports.tcp.Constants;
+import org.apache.mina.common.ByteBuffer;
 
-public class GCacheManagerTest {
+import java.net.Socket;
+import java.net.InetSocketAddress;
+import java.io.OutputStream;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.HashMap;
 
-    @Test
-    public void testGCacheManager() throws Exception{
+import net.sf.ehcache.CacheManager;
 
-        GCacheManager mgr = new GCacheManager(null);
+public class GCacheManagerTest {
+    GCacheManager mgr;
 
+    @BeforeClass
+    public void setUp() throws Exception {
+        mgr = new GCacheManager(null);
         mgr.start();
+    }
 
+    @AfterClass(alwaysRun = true)
+    public void shutdown() throws Exception {
         mgr.stop();
+    }
+
+    @Test
+    public void testGCacheManager() throws Exception {
+
+        //Create a client socket
+        Socket client = new Socket();
+        client.connect(new InetSocketAddress("localhost", 4123), 2000);
+        authenticate(client);
+
+        //Create a second client socket
+        Socket client2 = new Socket();
+        client2.connect(new InetSocketAddress("localhost", 4123), 2000);
+        authenticate(client2);
+
+        //Client 1 will send a session
+        sendSession(client);
+
+        //Client 2 should have a notification
+        Command read = readCommand(client2);
+        assert read instanceof PutSessionCommand;
+
+        Map session = ((PutSessionCommand) read).getSessionFromPayload();
+        assert session.size() == 3;
+
+        assert((PutSessionCommand) read).getCacheName().equals("Cache1");
+
+        assert session.containsKey("key1");
+        assert session.containsKey("key2");
+        assert session.containsKey("key3");
+
+        assert((String) session.get("key1")).equals("data1");
+        assert((String) session.get("key2")).equals("data2");
+        assert((String) session.get("key3")).equals("data3");
+
+    }
+
+
+    protected void authenticate(Socket socket) throws Exception {
+        HandShakeCommand command = new HandShakeCommand();
+        command.setPublicKey(CipherUtil.publicKey);
+
+        //Send the handshake
+        sendCommand(socket, command);
+
+        PublicKeyCommand keyCommand = (PublicKeyCommand) readCommand(socket);
+
+        AuthCommand auth = new AuthCommand();
+        auth.setUserId("system");
+        auth.setPassword("manager");
+        auth.setPublicKey(keyCommand.getPublicKey());
+
+        sendCommand(socket, auth);
+
+    }
+
+    protected void sendCommand(Socket socket, BaseCommand command)
+            throws Exception {
+
+        byte bytes[] = command.createPacket(true);
+
+        OutputStream out = socket.getOutputStream();
+        out.write(bytes);
+        out.flush();
+
+    }
+
+    protected Command readCommand(Socket socket) throws Exception {
+
+        byte recBuf[] = new byte[Constants.HEADER_SIZE];
+
+        socket.setSoTimeout(2000);
+        InputStream is = socket.getInputStream();
+        int read = is.read(recBuf);
+        assert read == Constants.HEADER_SIZE;
+        ByteBuffer receiveHeader = ByteBuffer.wrap(recBuf);
+
+        //Read the magic
+        byte magic[] = new byte[Constants.MAGIC.length];
+        receiveHeader.get(magic);
+
+        //Better match the Magic
+        assert Arrays.equals(Constants.MAGIC, magic);
+
+        //Get the command
+        byte commandIdentifier = receiveHeader.get();
+
+        //Get the command length
+        int length = receiveHeader.getInt();
+        receiveHeader.release();
+
+        //pull the command
+        recBuf = new byte[length];
+        int count = is.read(recBuf);
+        assert count == length;
+        ByteBuffer commandBuffer = ByteBuffer.wrap(recBuf);
+
+        //Create the command and unmarshal the data
+        Command command = CommandTypes.createCommand(commandIdentifier);
+        command.readExternal(ByteBuffer.wrap(commandBuffer.array()));
+
+        return command;
+
+    }
+
+    private void sendSession(Socket socket) throws Exception {
+
+        //Create a session
+        Map<String, String> session = new HashMap<String, String>();
+        session.put("key1", "data1");
+        session.put("key2", "data2");
+        session.put("key3", "data3");
+        PutSessionCommand command = new PutSessionCommand();
+        command.setCacheName("Cache1");
+        command.setSessionId("Session1");
+        command.setPayloadFromSession(session);
+
+        //Send the packet
+        sendCommand(socket, command);
+
     }
 }

Modified: geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPNotificationTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPNotificationTest.java?view=diff&rev=471033&r1=471032&r2=471033
==============================================================================
--- geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPNotificationTest.java
(original)
+++ geronimo/sandbox/gcache/server/src/test/java/org/apache/geronimo/gcache/transports/tcp/TCPNotificationTest.java
Fri Nov  3 14:52:42 2006
@@ -29,52 +29,52 @@
 import org.testng.annotations.Test;
 
 public class TCPNotificationTest extends AbstractService {
-    
+
     @Test
-    public void notificationTest() throws Exception{
-        
-	GetCacheCommand command = new GetCacheCommand();
-	
-	command.setCacheName("Notify");
-	
-	//Send the getCache request from client 1
-	sendCommand(client, command);
-	Command ack = readCommand(client);
-	assert ack instanceof BulkSendCommand;
-	
-	//Send the getCache request from client 2
-	sendCommand(client2, command);
-	ack = readCommand(client2);
-	assert ack instanceof BulkSendCommand;
-	
-	//Send a session
-	sendSession(client);
-	
-	//Client 2 should have a notification 
-	Command read = readCommand(client2);
-	assert read instanceof PutSessionCommand;
-	
-	Map session = ((PutSessionCommand) read).getSessionFromPayload();
-	assert session.size() == 3;
-	
-	assert ((PutSessionCommand) read).getCacheName().equals("Cache1");
-	
-	assert session.containsKey("key1");
-	assert session.containsKey("key2");
-	assert session.containsKey("key3");
-	
-	assert ((String)session.get("key1")).equals("data1");
-	assert ((String)session.get("key2")).equals("data2");
-	assert ((String)session.get("key3")).equals("data3");
+    public void notificationTest() throws Exception {
+
+        GetCacheCommand command = new GetCacheCommand();
+
+        command.setCacheName("Notify");
+
+        //Send the getCache request from client 1
+        sendCommand(client, command);
+        Command ack = readCommand(client);
+        assert ack instanceof BulkSendCommand;
+
+        //Send the getCache request from client 2
+        sendCommand(client2, command);
+        ack = readCommand(client2);
+        assert ack instanceof BulkSendCommand;
+
+        //Send a session
+        sendSession(client);
+
+        //Client 2 should have a notification
+        Command read = readCommand(client2);
+        assert read instanceof PutSessionCommand;
+
+        Map session = ((PutSessionCommand) read).getSessionFromPayload();
+        assert session.size() == 3;
+
+        assert((PutSessionCommand) read).getCacheName().equals("Cache1");
+
+        assert session.containsKey("key1");
+        assert session.containsKey("key2");
+        assert session.containsKey("key3");
+
+        assert((String) session.get("key1")).equals("data1");
+        assert((String) session.get("key2")).equals("data2");
+        assert((String) session.get("key3")).equals("data3");
     }
-    
-    private void sendSession(Socket socket) throws Exception{
-	
+
+    private void sendSession(Socket socket) throws Exception {
+
         //Create a session
-        Map<String,String> session = new HashMap<String, String>();
-        session.put("key1","data1");
-        session.put("key2","data2");
-        session.put("key3","data3");
+        Map<String, String> session = new HashMap<String, String>();
+        session.put("key1", "data1");
+        session.put("key2", "data2");
+        session.put("key3", "data3");
         PutSessionCommand command = new PutSessionCommand();
         command.setCacheName("Cache1");
         command.setSessionId("Session1");
@@ -84,11 +84,11 @@
         sendCommand(socket, command);
 
         //Now receive any data (it Should be a MessageAck)
-        Command ackCommand = this.readCommand(socket); 
+        Command ackCommand = this.readCommand(socket);
 
         //Is the message the type we think it is?
         assert ackCommand instanceof MessageAckCommand;
-	
+
     }
 
 }



Mime
View raw message