add FCP adapter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 9 Apr 2008 09:53:21 +0000 (09:53 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 9 Apr 2008 09:53:21 +0000 (09:53 +0000)
adapt test class

git-svn-id: http://trooper/svn/projects/jSite/trunk@655 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/util/fcp/FcpAdapter.java [new file with mode: 0644]
src/net/pterodactylus/util/fcp/FcpTest.java

diff --git a/src/net/pterodactylus/util/fcp/FcpAdapter.java b/src/net/pterodactylus/util/fcp/FcpAdapter.java
new file mode 100644 (file)
index 0000000..9a70d0f
--- /dev/null
@@ -0,0 +1,41 @@
+/**
+ * © 2008 INA Service GmbH
+ */
+package net.pterodactylus.util.fcp;
+
+import net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName;
+import net.pterodactylus.util.fcp.message.NodeHello;
+
+/**
+ * Adapter for {@link FcpListener}.
+ * 
+ * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
+ * @version $Id$
+ */
+public class FcpAdapter implements FcpListener {
+
+       /**
+        * @see net.pterodactylus.util.fcp.FcpListener#receivedNodeHello(net.pterodactylus.util.fcp.FcpConnection,
+        *      net.pterodactylus.util.fcp.message.NodeHello)
+        */
+       public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
+               /* empty. */
+       }
+
+       /**
+        * @see net.pterodactylus.util.fcp.FcpListener#receivedCloseConnectionDuplicateClientName(net.pterodactylus.util.fcp.FcpConnection,
+        *      net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName)
+        */
+       public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
+               /* empty. */
+       }
+
+       /**
+        * @see net.pterodactylus.util.fcp.FcpListener#receivedMessage(net.pterodactylus.util.fcp.FcpConnection,
+        *      net.pterodactylus.util.fcp.FcpMessage)
+        */
+       public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
+               /* empty. */
+       }
+
+}
index cedce1d..b07bb85 100644 (file)
 package net.pterodactylus.util.fcp;
 
 import java.io.IOException;
-import java.util.List;
-import java.util.Map;
 
 import junit.framework.TestCase;
-
+import net.pterodactylus.util.fcp.message.ClientHello;
+import net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName;
+import net.pterodactylus.util.fcp.message.GenerateSSK;
+import net.pterodactylus.util.fcp.message.NodeHello;
 
 /**
- * TODO
+ * Tests various commands and the FCP connection.
+ * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
 public class FcpTest extends TestCase {
 
+       /** The FCP connection. */
        private FcpConnection fcpConnection;
-       
+
        /**
         * {@inheritDoc}
         */
        @Override
        protected void setUp() throws Exception {
-               fcpConnection = new FcpConnection("wing", "FcpTest");
+               fcpConnection = new FcpConnection("wing");
                fcpConnection.connect();
+               fcpConnection.sendMessage(new ClientHello("FcpTest"));
        }
-       
+
        /**
         * {@inheritDoc}
         */
@@ -51,32 +55,80 @@ public class FcpTest extends TestCase {
        protected void tearDown() throws Exception {
                fcpConnection.disconnect();
        }
-       
+
+       /**
+        * Tests the FCP connection be simply {@link #setUp() setting it up} and
+        * {@link #tearDown() tearing it down} again.
+        * 
+        * @throws FcpException
+        *             if an FCP error occurs
+        * @throws IOException
+        *             if an I/O error occurs
+        */
        public void testFcpConnection() throws FcpException, IOException {
+               /* do nothing. */
        }
-       
-       public void testListPeers() throws FcpException, IOException {
-               List<Map<String, String>> peers = fcpConnection.sendListPeers(true, true);
-               for (Map<String, String> peer: peers) {
-                       for (Map.Entry<String, String> entry: peer.entrySet()) {
-                               System.out.println(entry.getKey() + ": " + entry.getValue());
+
+//     public void testListPeers() throws FcpException, IOException {
+//             List<Map<String, String>> peers = fcpConnection.sendListPeers(true, true);
+//             for (Map<String, String> peer: peers) {
+//                     for (Map.Entry<String, String> entry: peer.entrySet()) {
+//                             System.out.println(entry.getKey() + ": " + entry.getValue());
+//                     }
+//             }
+//     }
+
+//     public void testListPeerNotes() throws FcpException, IOException {
+//             List<Map<String, String>> peers = fcpConnection.sendListPeerNotes("AY6rGAGjayoyQD2CkvQibf1Ct3mh6iwqyntzNpfRsiM");
+//             for (Map<String, String> peer: peers) {
+//                     for (Map.Entry<String, String> entry: peer.entrySet()) {
+//                             System.out.println(entry.getKey() + ": " + entry.getValue());
+//                     }
+//             }
+//     }
+
+       /**
+        * Generates an SSK key pair.
+        * 
+        * @throws FcpException
+        *             if an FCP error occurs
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws InterruptedException
+        *             if {@link Object#wait()} wakes up spuriously
+        */
+       public void testGenerateSSK() throws IOException, FcpException, InterruptedException {
+               final boolean[] result = new boolean[1];
+               FcpAdapter fcpAdapter = new FcpAdapter() {
+                       /**
+                        * @see net.pterodactylus.util.fcp.FcpAdapter#receivedNodeHello(net.pterodactylus.util.fcp.FcpConnection,
+                        *      net.pterodactylus.util.fcp.message.NodeHello)
+                        */
+                       @Override
+                       public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
+                               result[0] = true;
+                               synchronized (this) {
+                                       notify();
+                               }
                        }
-               }
-       }
-       
-       public void testListPeerNotes() throws FcpException, IOException {
-               List<Map<String, String>> peers = fcpConnection.sendListPeerNotes("AY6rGAGjayoyQD2CkvQibf1Ct3mh6iwqyntzNpfRsiM");
-               for (Map<String, String> peer: peers) {
-                       for (Map.Entry<String, String> entry: peer.entrySet()) {
-                               System.out.println(entry.getKey() + ": " + entry.getValue());
+
+                       /**
+                        * @see net.pterodactylus.util.fcp.FcpAdapter#receivedCloseConnectionDuplicateClientName(net.pterodactylus.util.fcp.FcpConnection,
+                        *      net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName)
+                        */
+                       @Override
+                       public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
+                               result[0] = false;
+                               synchronized (this) {
+                                       notify();
+                               }
                        }
+               };
+               synchronized (fcpAdapter) {
+                       fcpConnection.sendMessage(new GenerateSSK());
+                       fcpAdapter.wait();
                }
+               assertTrue("received NodeHello", result[0]);
        }
-       
-       public void testGenerateSSK() throws IOException, FcpException {
-               FcpKeyPair keyPair = fcpConnection.generateSSK();
-               System.out.println("PrivateKey: " + keyPair.getPrivateKey());
-               System.out.println("PublicKey: " + keyPair.getPublicKey());
-       }
-       
+
 }