You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by be...@apache.org on 2008/04/23 00:04:30 UTC

svn commit: r650674 - in /labs/vysper/src: main/java/org/apache/vysper/xmpp/delivery/ main/java/org/apache/vysper/xmpp/stanza/ test/java/org/apache/vysper/xmpp/delivery/ test/java/org/apache/vysper/xmpp/resourcebinding/ test/java/org/apache/vysper/xmpp...

Author: berndf
Date: Tue Apr 22 15:04:26 2008
New Revision: 650674

URL: http://svn.apache.org/viewvc?rev=650674&view=rev
Log:
[vysper] unit tests for internal relay, plus clean-ups

Added:
    labs/vysper/src/test/java/org/apache/vysper/xmpp/delivery/
    labs/vysper/src/test/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelayTestCase.java
Modified:
    labs/vysper/src/main/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelay.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaBuilder.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/resourcebinding/ResourceRegistryTestCase.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelay.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelay.java?rev=650674&r1=650673&r2=650674&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelay.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelay.java Tue Apr 22 15:04:26 2008
@@ -18,22 +18,20 @@
 
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.resourcebinding.ResourceRegistry;
-import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.server.SessionContext;
+import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.writer.StanzaWriter;
 
+import java.util.List;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.List;
 
 /**
- * relays stanzas to internal or external servers
- * TODO: but right now, it only delivers to internal sessions
+ * relays stanzas to internal sessions
  */
 public class DeliveringStanzaRelay implements StanzaRelay {
 

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaBuilder.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaBuilder.java?rev=650674&r1=650673&r2=650674&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaBuilder.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaBuilder.java Tue Apr 22 15:04:26 2008
@@ -22,6 +22,7 @@
 import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
 import org.apache.vysper.xmpp.xmlfragment.XMLText;
 import org.apache.vysper.xmpp.xmlfragment.NamespaceAttribute;
+import org.apache.vysper.xmpp.addressing.Entity;
 
 import java.util.Stack;
 import java.util.List;
@@ -35,6 +36,15 @@
         StanzaBuilder stanzaBuilder = new StanzaBuilder("iq");
         stanzaBuilder.addAttribute("type", type.value());
         stanzaBuilder.addAttribute("id", id);
+        return stanzaBuilder;        
+    }
+    
+    public static StanzaBuilder createMessageStanza(Entity from, Entity to, String lang, String body) {
+        StanzaBuilder stanzaBuilder = new StanzaBuilder("message");
+        stanzaBuilder.addAttribute("from", from.getFullQualifiedName());
+        stanzaBuilder.addAttribute("to", to.getFullQualifiedName());
+        stanzaBuilder.addAttribute("xml:lang", lang);
+        stanzaBuilder.startInnerElement("body").addText(body).endInnerElement();
         return stanzaBuilder;        
     }
     

Added: labs/vysper/src/test/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelayTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelayTestCase.java?rev=650674&view=auto
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelayTestCase.java (added)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/delivery/DeliveringStanzaRelayTestCase.java Tue Apr 22 15:04:26 2008
@@ -0,0 +1,72 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you *
+ * may not use this file except in compliance with the License. You    *
+ * may obtain a copy of the License at:                                *
+ *                                                                     *
+ *     http://www.apache.org/licenses/LICENSE-2.0                      *
+ *                                                                     *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS,   *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
+ * implied.  See the License for the specific language governing       *
+ * permissions and limitations under the License.                      *
+ ***********************************************************************/
+package org.apache.vysper.xmpp.delivery;
+
+import org.apache.vysper.xmpp.resourcebinding.ResourceRegistry;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.addressing.EntityFormatException;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.xmlfragment.XMLSemanticError;
+import junit.framework.TestCase;
+
+/**
+ */
+public class DeliveringStanzaRelayTestCase extends TestCase {
+    
+    protected ResourceRegistry resourceRegistry = new ResourceRegistry();
+    protected DeliveringStanzaRelay stanzaRelay = new DeliveringStanzaRelay(resourceRegistry);
+    
+    public void testSimpleRelay() throws EntityFormatException, XMLSemanticError, DeliveryException {
+        EntityImpl fromEntity = EntityImpl.parse("userFrom@vysper.org");
+        EntityImpl toEntity = EntityImpl.parse("userTo@vysper.org");
+        TestSessionContext sessionContext = TestSessionContext.createSessionContext(toEntity);
+        resourceRegistry.bindSession(sessionContext);
+
+        Stanza stanza = StanzaBuilder.createMessageStanza(fromEntity, toEntity, "en", "Hello").getFinalStanza();
+
+        try {
+            stanzaRelay.relay(toEntity, stanza);
+            try { Thread.sleep(60); } catch (InterruptedException e) { ; } // eventually, this gets delivered
+            Stanza recordedStanza = sessionContext.getRecordedResponse();
+            assertNotNull("stanza delivered", recordedStanza);
+            assertEquals("Hello", recordedStanza.getSingleInnerElementsNamed("body").getSingleInnerText().getText());
+        } catch (DeliveryException e) {
+            throw e;
+        }
+    }
+    
+    public void testSimpleRelayToUnboundSession() throws EntityFormatException, XMLSemanticError, DeliveryException {
+        EntityImpl fromEntity = EntityImpl.parse("userFrom@vysper.org");
+        EntityImpl toEntity = EntityImpl.parse("userTo@vysper.org");
+        TestSessionContext sessionContext = TestSessionContext.createSessionContext(toEntity);
+        String resource = resourceRegistry.bindSession(sessionContext);
+        resourceRegistry.unbindResource(resource);
+
+        Stanza stanza = StanzaBuilder.createMessageStanza(fromEntity, toEntity, "en", "Hello").getFinalStanza();
+
+        try {
+            stanzaRelay.relay(toEntity, stanza);
+            try { Thread.sleep(60); } catch (InterruptedException e) { ; } // eventually, this gets delivered
+            Stanza recordedStanza = sessionContext.getRecordedResponse();
+            assertNull("stanza not delivered to unbound", recordedStanza);
+        } catch (DeliveryException e) {
+            throw e;
+        }
+    }
+}

Modified: labs/vysper/src/test/java/org/apache/vysper/xmpp/resourcebinding/ResourceRegistryTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/resourcebinding/ResourceRegistryTestCase.java?rev=650674&r1=650673&r2=650674&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/resourcebinding/ResourceRegistryTestCase.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/resourcebinding/ResourceRegistryTestCase.java Tue Apr 22 15:04:26 2008
@@ -19,8 +19,6 @@
 import junit.framework.TestCase;
 import org.apache.vysper.xmpp.server.TestSessionContext;
 import org.apache.vysper.xmpp.server.SessionContext;
-import org.apache.vysper.xmpp.protocol.SessionStateHolder;
-import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.addressing.EntityFormatException;
 
@@ -32,15 +30,8 @@
     
     protected ResourceRegistry resourceRegistry = new ResourceRegistry();
 
-    private TestSessionContext createSessionContext(Entity entity) {
-        SessionStateHolder sessionStateHolder = new SessionStateHolder();
-        TestSessionContext sessionContext = new TestSessionContext(sessionStateHolder);
-        sessionContext.setInitiatingEntity(entity);
-        return sessionContext;
-    }
-
     public void testSessionNotWellDefinedForResourceBinding() {
-        TestSessionContext sessionContext = createSessionContext(null);
+        TestSessionContext sessionContext = TestSessionContext.createSessionContext(null);
         assertNull(sessionContext.getInitiatingEntity());
         try {
             resourceRegistry.bindSession(sessionContext);
@@ -51,7 +42,7 @@
     }
     
     public void testAddSession() throws EntityFormatException {
-        TestSessionContext sessionContext = createSessionContext(EntityImpl.parse("me@test"));
+        TestSessionContext sessionContext = TestSessionContext.createSessionContext(EntityImpl.parse("me@test"));
         String resourceId = resourceRegistry.bindSession(sessionContext);
         assertNotNull(resourceId);
         List<String> resourceList = resourceRegistry.getResourcesForSession(sessionContext);
@@ -60,9 +51,9 @@
     }
 
     public void testAddMultipleSession() throws EntityFormatException {
-        TestSessionContext sessionContext1 = createSessionContext(EntityImpl.parse("me1@test"));
+        TestSessionContext sessionContext1 = TestSessionContext.createSessionContext(EntityImpl.parse("me1@test"));
         String resourceId1 = resourceRegistry.bindSession(sessionContext1);
-        TestSessionContext sessionContext2 = createSessionContext(EntityImpl.parse("me2@test"));
+        TestSessionContext sessionContext2 = TestSessionContext.createSessionContext(EntityImpl.parse("me2@test"));
         String resourceId2 = resourceRegistry.bindSession(sessionContext2);
         assertNotNull(resourceId2);
         List<String> resourceList = resourceRegistry.getResourcesForSession(sessionContext1);
@@ -79,10 +70,10 @@
     public void testAddOneEntityMultipleResources() throws EntityFormatException {
         EntityImpl entity = EntityImpl.parse("me@test");
         
-        TestSessionContext sessionContext1 = createSessionContext(entity);
+        TestSessionContext sessionContext1 = TestSessionContext.createSessionContext(entity);
         String resourceId1 = resourceRegistry.bindSession(sessionContext1);
         
-        TestSessionContext sessionContext2 = createSessionContext(entity);
+        TestSessionContext sessionContext2 = TestSessionContext.createSessionContext(entity);
         String resourceId2 = resourceRegistry.bindSession(sessionContext2);
         
         assertNotNull(resourceId1);
@@ -102,10 +93,10 @@
     public void testAddOneEntityMultipleResources_TolerateResourceIds() throws EntityFormatException {
         EntityImpl entity = EntityImpl.parse("me@test");
         
-        TestSessionContext sessionContext1 = createSessionContext(EntityImpl.parse("me@test/xy"));
+        TestSessionContext sessionContext1 = TestSessionContext.createSessionContext(EntityImpl.parse("me@test/xy"));
         String resourceId1 = resourceRegistry.bindSession(sessionContext1);
         
-        TestSessionContext sessionContext2 = createSessionContext(EntityImpl.parse("me@test/ab"));
+        TestSessionContext sessionContext2 = TestSessionContext.createSessionContext(EntityImpl.parse("me@test/ab"));
         String resourceId2 = resourceRegistry.bindSession(sessionContext2);
         
         assertNotNull(resourceId1);
@@ -125,10 +116,10 @@
     public void testSameEntityMultipleResources() throws EntityFormatException {
         EntityImpl entity = EntityImpl.parse("me@test");
         
-        TestSessionContext sessionContext1 = createSessionContext(entity);
+        TestSessionContext sessionContext1 = TestSessionContext.createSessionContext(entity);
         String resourceId1 = resourceRegistry.bindSession(sessionContext1);
         
-        TestSessionContext sessionContext2 = createSessionContext(entity);
+        TestSessionContext sessionContext2 = TestSessionContext.createSessionContext(entity);
         String resourceId2 = resourceRegistry.bindSession(sessionContext2);
         
         // resource ids are different
@@ -138,7 +129,7 @@
     public void testUnbindResourceSimple() throws EntityFormatException {
         EntityImpl entity = EntityImpl.parse("me@test");
         
-        TestSessionContext sessionContext1 = createSessionContext(entity);
+        TestSessionContext sessionContext1 = TestSessionContext.createSessionContext(entity);
         String resourceId1 = resourceRegistry.bindSession(sessionContext1);
         assertEquals(sessionContext1, resourceRegistry.getSessionContext(resourceId1));
         
@@ -151,7 +142,7 @@
     public void testUnbindSessionSimple() throws EntityFormatException {
         EntityImpl entity = EntityImpl.parse("me@test");
         
-        TestSessionContext sessionContext1 = createSessionContext(entity);
+        TestSessionContext sessionContext1 = TestSessionContext.createSessionContext(entity);
         String resourceId1 = resourceRegistry.bindSession(sessionContext1);
         assertEquals(sessionContext1, resourceRegistry.getSessionContext(resourceId1));
         

Modified: labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java?rev=650674&r1=650673&r2=650674&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java Tue Apr 22 15:04:26 2008
@@ -18,6 +18,7 @@
 package org.apache.vysper.xmpp.server;
 
 import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.delivery.RecordingStanzaRelay;
 import org.apache.vysper.xmpp.protocol.SessionStateHolder;
 import org.apache.vysper.xmpp.stanza.Stanza;
@@ -77,5 +78,12 @@
 
     public boolean isSwitchToTLSCalled() {
         return switchToTLSCalled;
+    }
+
+    public static TestSessionContext createSessionContext(Entity entity) {
+        SessionStateHolder sessionStateHolder = new SessionStateHolder();
+        TestSessionContext sessionContext = new TestSessionContext(sessionStateHolder);
+        sessionContext.setInitiatingEntity(entity);
+        return sessionContext;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org