You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ng...@apache.org on 2011/03/06 22:49:13 UTC

svn commit: r1078592 - in /mina/vysper/trunk/server/core/src: main/java/org/apache/vysper/xmpp/modules/core/session/handler/ main/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/ test/java/org/apache/vysper/xmpp/modules/core/session/ tes...

Author: ngn
Date: Sun Mar  6 21:49:13 2011
New Revision: 1078592

URL: http://svn.apache.org/viewvc?rev=1078592&view=rev
Log:
Adding unit tests for XMPP Ping
Adding unit tests for SessionIQHandler

Added:
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/handler/
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandlerTestCase.java
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingModuleTestCase.java
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingerTestCase.java
Modified:
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandler.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandler.java
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandler.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandler.java?rev=1078592&r1=1078591&r2=1078592&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandler.java (original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandler.java Sun Mar  6 21:49:13 2011
@@ -19,7 +19,7 @@
  */
 package org.apache.vysper.xmpp.modules.core.session.handler;
 
-import org.apache.vysper.xmpp.modules.core.base.handler.IQHandler;
+import org.apache.vysper.xmpp.modules.core.base.handler.DefaultIQHandler;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.server.SessionContext;
@@ -33,28 +33,16 @@ import org.apache.vysper.xmpp.stanza.Sta
  *
  * @author The Apache MINA Project (dev@mina.apache.org)
  */
-public class SessionIQHandler extends IQHandler {
+public class SessionIQHandler extends DefaultIQHandler {
 
     @Override
-    protected boolean verifyNamespace(Stanza stanza) {
-        return verifyInnerNamespace(stanza, NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SESSION);
+    protected boolean verifyInnerElement(Stanza stanza) {
+        return verifyInnerElementWorker(stanza, "session") && verifyInnerNamespace(stanza, NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SESSION);
     }
 
     @Override
-    protected Stanza executeIQLogic(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, boolean outboundStanza,
-            SessionContext sessionContext) {
-
-        switch (stanza.getIQType()) {
-
-        case SET:
-            return StanzaBuilder.createIQStanza(null, null, IQStanzaType.RESULT, stanza.getID()).addAttribute("from",
-                    sessionContext.getServerJID().getFullQualifiedName()).build();
-
-        case GET:
-        case ERROR:
-        default:
-            throw new RuntimeException("iq stanza type not supported: " + stanza.getIQType());
-        }
+    protected Stanza handleSet(IQStanza stanza, ServerRuntimeContext serverRuntimeContext, SessionContext sessionContext) {
+        return StanzaBuilder.createIQStanza(null, null, IQStanzaType.RESULT, stanza.getID()).addAttribute("from",
+                sessionContext.getServerJID().getFullQualifiedName()).build();
     }
-
-}
+}
\ No newline at end of file

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandler.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandler.java?rev=1078592&r1=1078591&r2=1078592&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandler.java (original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandler.java Sun Mar  6 21:49:13 2011
@@ -57,10 +57,13 @@ public class XmppPingIQHandler extends D
 
     @Override
     public boolean verify(Stanza stanza) {
+        if(stanza == null) return false;
+        
         boolean extension = super.verify(stanza);
         if(extension) {
             return true;
         } else {
+            // handle result stanzas, which does not contain the extension element
             String type = stanza.getAttributeValue("type");
             if(type != null && type.equals("result")) {
                 String id = stanza.getAttributeValue("id");
@@ -82,13 +85,8 @@ public class XmppPingIQHandler extends D
     }
     
     @Override
-    protected boolean verifyNamespace(Stanza stanza) {
-        return verifyInnerNamespace(stanza, NamespaceURIs.URN_XMPP_PING);
-    }
-
-    @Override
     protected boolean verifyInnerElement(Stanza stanza) {
-        return verifyInnerElementWorker(stanza, "ping");
+        return verifyInnerElementWorker(stanza, "ping") && verifyInnerNamespace(stanza, NamespaceURIs.URN_XMPP_PING);
     }
 
     @Override

Added: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandlerTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandlerTestCase.java?rev=1078592&view=auto
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandlerTestCase.java (added)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/core/session/handler/SessionIQHandlerTestCase.java Sun Mar  6 21:49:13 2011
@@ -0,0 +1,148 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.modules.core.session.handler;
+
+import junit.framework.Assert;
+
+import org.apache.vysper.StanzaAssert;
+import org.apache.vysper.xml.fragment.XMLSemanticError;
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.protocol.SessionStateHolder;
+import org.apache.vysper.xmpp.server.ServerRuntimeContext;
+import org.apache.vysper.xmpp.server.SessionContext;
+import org.apache.vysper.xmpp.stanza.IQStanza;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.state.resourcebinding.BindException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ */
+public class SessionIQHandlerTestCase {
+
+    private static final Entity FROM = EntityImpl.parseUnchecked("from@vysper.org");
+    private static final Entity TO = EntityImpl.parseUnchecked("vysper.org");
+
+    private ServerRuntimeContext serverRuntimeContext = Mockito.mock(ServerRuntimeContext.class);
+    private SessionContext sessionContext = Mockito.mock(SessionContext.class);
+    private SessionStateHolder sessionStateHolder = new SessionStateHolder();
+    
+    private IQStanza verifyStanza = (IQStanza) IQStanza.getWrapper(buildStanza());
+    
+    private SessionIQHandler handler = new SessionIQHandler();
+    
+    @Before
+    public void before() {
+        Mockito.when(sessionContext.getInitiatingEntity()).thenReturn(FROM);
+        Mockito.when(sessionContext.getServerJID()).thenReturn(TO);
+    }
+    
+    
+    private Stanza buildStanza() {
+        return buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "session", NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SESSION);
+    }
+
+    private Stanza buildStanza(String name, String namespaceUri) {
+        return buildStanza(name, namespaceUri, "session", NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SESSION);
+    }
+    
+    private Stanza buildStanza(String name, String namespaceUri, String innerName, String innerNamespaceUri) {
+        return new StanzaBuilder(name, namespaceUri)
+            .addAttribute("type", "get")
+            .addAttribute("id", "1")
+            .startInnerElement(innerName, innerNamespaceUri)
+            .build();
+    }
+    
+    @Test
+    public void nameMustBeIq() {
+        Assert.assertEquals("iq", handler.getName());
+    }
+
+    @Test
+    public void verifyNullStanza() {
+        Assert.assertFalse(handler.verify(null));
+    }
+
+    @Test
+    public void verifyInvalidName() {
+        Assert.assertFalse(handler.verify(buildStanza("dummy", NamespaceURIs.JABBER_CLIENT)));
+    }
+
+    @Test
+    public void verifyInvalidNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", "dummy")));
+    }
+
+    @Test
+    public void verifyNullNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", null)));
+    }
+
+    @Test
+    public void verifyNullInnerNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "session", null)));
+    }
+
+    @Test
+    public void verifyInvalidInnerNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "session", "dummy")));
+    }
+    
+    @Test
+    public void verifyInvalidInnerName() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "dummy", NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SESSION)));
+    }
+
+    @Test
+    public void verifyMissingInnerElement() {
+        Stanza stanza = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT).build();
+        Assert.assertFalse(handler.verify(stanza));
+    }
+    
+    @Test
+    public void verifyValidStanza() {
+        Assert.assertTrue(handler.verify(verifyStanza));
+    }
+
+    @Test
+    public void sessionIsRequired() {
+        Assert.assertTrue(handler.isSessionRequired());
+    }
+
+    @Test
+    public void handleSet() throws BindException, XMLSemanticError {
+        Stanza request = StanzaBuilder.createIQStanza(FROM, FROM, IQStanzaType.SET, "id1")
+            .startInnerElement("session", NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SESSION)
+            .build();
+        
+        Stanza response = handler.execute(request, serverRuntimeContext, true, sessionContext, sessionStateHolder).getResponseStanza();
+
+        Stanza expected = StanzaBuilder.createIQStanza(TO, null, IQStanzaType.RESULT, "id1").build();
+        
+        StanzaAssert.assertEquals(expected, response);
+    }
+
+}

Modified: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java?rev=1078592&r1=1078591&r2=1078592&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java (original)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java Sun Mar  6 21:49:13 2011
@@ -19,50 +19,138 @@
  */
 package org.apache.vysper.xmpp.modules.extension.xep0199_xmppping;
 
-import junit.framework.TestCase;
-
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.addressing.EntityImpl;
-import org.apache.vysper.xmpp.modules.extension.xep0199_xmppping.XmppPingIQHandler;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 import org.apache.vysper.xmpp.protocol.ResponseStanzaContainer;
 import org.apache.vysper.xmpp.server.TestSessionContext;
 import org.apache.vysper.xmpp.stanza.IQStanzaType;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  */
-public class XmppPingIQHandlerTestCase extends TestCase {
+public class XmppPingIQHandlerTestCase {
 
-    private static final String IQ_ID = "id1";
+    private static final String IQ_ID = "xmppping-1";
 
-    private TestSessionContext sessionContext;
+    private TestSessionContext sessionContext = TestSessionContext.createWithStanzaReceiverRelayAuthenticated();
 
-    protected Entity client;
+    protected Entity client = EntityImpl.parseUnchecked("tester@vysper.org");
 
     protected Entity boundClient;
 
     protected Entity server;
 
-    protected XmppPingIQHandler handler;
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        client = EntityImpl.parse("tester@vysper.org");
+    protected XmppPingIQHandler handler = new XmppPingIQHandler();
 
-        sessionContext = TestSessionContext.createWithStanzaReceiverRelayAuthenticated();
+    @Before
+    public void before() throws Exception {
         sessionContext.setInitiatingEntity(client);
 
         boundClient = new EntityImpl(client, sessionContext.bindResource());
         server = sessionContext.getServerJID();
+    }
+    
+    private Stanza buildStanza() {
+        return buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "ping", NamespaceURIs.URN_XMPP_PING);
+    }
+
+    private Stanza buildStanza(String name, String namespaceUri) {
+        return buildStanza(name, namespaceUri, "ping", NamespaceURIs.URN_XMPP_PING);
+    }
+    
+    private Stanza buildStanza(String name, String namespaceUri, String innerName, String innerNamespaceUri) {
+        return new StanzaBuilder(name, namespaceUri)
+            .addAttribute("type", "get")
+            .addAttribute("id", "1")
+            .startInnerElement(innerName, innerNamespaceUri)
+            .build();
+    }
+    
+    @Test
+    public void nameMustBeIq() {
+        Assert.assertEquals("iq", handler.getName());
+    }
+
+    @Test
+    public void verifyNullStanza() {
+        Assert.assertFalse(handler.verify(null));
+    }
+
+    @Test
+    public void verifyInvalidName() {
+        Assert.assertFalse(handler.verify(buildStanza("dummy", NamespaceURIs.JABBER_CLIENT)));
+    }
+
+    @Test
+    public void verifyInvalidNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", "dummy")));
+    }
+
+    @Test
+    public void verifyNullNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", null)));
+    }
+
+    @Test
+    public void verifyNullInnerNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "ping", null)));
+    }
 
-        handler = new XmppPingIQHandler();
+    @Test
+    public void verifyInvalidInnerNamespace() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "ping", "dummy")));
+    }
+    
+    @Test
+    public void verifyInvalidInnerName() {
+        Assert.assertFalse(handler.verify(buildStanza("iq", NamespaceURIs.JABBER_CLIENT, "dummy", NamespaceURIs.URN_XMPP_PING)));
+    }
+
+    @Test
+    public void verifyMissingInnerElement() {
+        Stanza stanza = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT).build();
+        Assert.assertFalse(handler.verify(stanza));
+    }
+    
+    @Test
+    public void verifyValidStanza() {
+        Assert.assertTrue(handler.verify(buildStanza()));
+    }
+
+    @Test
+    public void verifyValidResultStanza() {
+        Stanza stanza = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT)
+        .addAttribute("type", "result")
+        .addAttribute("id", "xmppping-1")
+        .build();
+
+        Assert.assertTrue(handler.verify(stanza));
+    }
+
+    @Test
+    public void verifyInvalidResultStanza() {
+        Stanza stanza = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT)
+        .addAttribute("type", "result")
+        .addAttribute("id", "dummy-1")
+        .build();
+        
+        Assert.assertFalse(handler.verify(stanza));
+    }
+    
+    @Test
+    public void sessionIsRequired() {
+        Assert.assertTrue(handler.isSessionRequired());
     }
 
-    public void testClientToServerPing() {
+
+    @Test
+    public void clientToServerPing() {
         // C: <iq from='juliet@capulet.lit/balcony' to='capulet.lit' id='c2s1' type='get'>
         //      <ping xmlns='urn:xmpp:ping'/>
         //    </iq>
@@ -77,14 +165,40 @@ public class XmppPingIQHandlerTestCase e
                 sessionContext, null);
 
         // we should always get a response
-        assertTrue(resp.hasResponse());
+        Assert.assertTrue(resp.hasResponse());
 
         Stanza respStanza = resp.getResponseStanza();
 
-        assertEquals("iq", respStanza.getName());
-        assertEquals(boundClient, respStanza.getTo());
-        assertEquals(server, respStanza.getFrom());
-        assertEquals(IQ_ID, respStanza.getAttributeValue("id"));
-        assertEquals("result", respStanza.getAttributeValue("type"));
+        Assert.assertEquals("iq", respStanza.getName());
+        Assert.assertEquals(boundClient, respStanza.getTo());
+        Assert.assertEquals(server, respStanza.getFrom());
+        Assert.assertEquals(IQ_ID, respStanza.getAttributeValue("id"));
+        Assert.assertEquals("result", respStanza.getAttributeValue("type"));
     }
+
+    @Test
+    public void handleResult() {
+        // S: <iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='c2s1' type='result'/>
+
+        Stanza stanza = StanzaBuilder.createIQStanza(boundClient, server, IQStanzaType.RESULT, IQ_ID).build();
+
+        XmppPinger pinger1 = Mockito.mock(XmppPinger.class);
+        XmppPinger pinger2 = Mockito.mock(XmppPinger.class);
+        XmppPinger pinger3 = Mockito.mock(XmppPinger.class);
+        
+        handler.addPinger(pinger1);
+        handler.addPinger(pinger2);
+        handler.addPinger(pinger3);
+        handler.removePinger(pinger3);
+        
+        ResponseStanzaContainer resp = handler.execute(stanza, sessionContext.getServerRuntimeContext(), true,
+                sessionContext, null);
+
+        Assert.assertNull(resp);
+        
+        Mockito.verify(pinger1).pong(IQ_ID);
+        Mockito.verify(pinger1).pong(IQ_ID);
+        Mockito.verifyZeroInteractions(pinger3);
+    }
+
 }

Added: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingModuleTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingModuleTestCase.java?rev=1078592&view=auto
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingModuleTestCase.java (added)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingModuleTestCase.java Sun Mar  6 21:49:13 2011
@@ -0,0 +1,86 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.modules.extension.xep0199_xmppping;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.vysper.xmpp.modules.servicediscovery.management.Feature;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.InfoElement;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.InfoRequest;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.ServerInfoRequestListener;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.ServiceDiscoveryRequestException;
+import org.apache.vysper.xmpp.protocol.DefaultHandlerDictionary;
+import org.apache.vysper.xmpp.protocol.HandlerDictionary;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.junit.Test;
+
+/**
+ */
+public class XmppPingModuleTestCase {
+
+    private XmppPingModule module = new XmppPingModule();
+    
+    @Test
+    public void nameMustBeProvided() {
+        Assert.assertNotNull(module.getName());
+    }
+
+    @Test
+    public void versionMustBeProvided() {
+        Assert.assertNotNull(module.getVersion());
+    }
+    
+    @Test
+    public void getServerInfosFor() throws ServiceDiscoveryRequestException {
+        List<ServerInfoRequestListener> serverInfoRequestListeners = new ArrayList<ServerInfoRequestListener>();
+        
+        module.addServerInfoRequestListeners(serverInfoRequestListeners);
+        
+        Assert.assertEquals(1, serverInfoRequestListeners.size());
+        
+        List<InfoElement> infoElements = serverInfoRequestListeners.get(0).getServerInfosFor(new InfoRequest(null, null, null, null));
+        
+        Assert.assertEquals(1, infoElements.size());
+        Assert.assertTrue(infoElements.get(0) instanceof Feature);
+        Assert.assertEquals(NamespaceURIs.URN_XMPP_PING, ((Feature)infoElements.get(0)).getVar());
+    }
+
+    @Test
+    public void getServerInfosForWithNode() throws ServiceDiscoveryRequestException {
+        List<ServerInfoRequestListener> serverInfoRequestListeners = new ArrayList<ServerInfoRequestListener>();
+        module.addServerInfoRequestListeners(serverInfoRequestListeners);
+        Assert.assertEquals(1, serverInfoRequestListeners.size());
+        
+        Assert.assertNull(serverInfoRequestListeners.get(0).getServerInfosFor(new InfoRequest(null, null, "node", null)));
+    }
+    
+    @Test
+    public void addHandlerDictionaries() {
+        List<HandlerDictionary> dictionaries = new ArrayList<HandlerDictionary>();
+        
+        module.addHandlerDictionaries(dictionaries);
+        
+        Assert.assertEquals(1, dictionaries.size());
+        Assert.assertTrue(dictionaries.get(0) instanceof DefaultHandlerDictionary);
+    }
+}

Added: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingerTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingerTestCase.java?rev=1078592&view=auto
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingerTestCase.java (added)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingerTestCase.java Sun Mar  6 21:49:13 2011
@@ -0,0 +1,123 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.modules.extension.xep0199_xmppping;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.vysper.StanzaAssert;
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.writer.StanzaWriter;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+
+/**
+ */
+public class XmppPingerTestCase {
+
+    private static final Entity FROM = EntityImpl.parseUnchecked("from@vysper.org");
+    private static final Entity TO = EntityImpl.parseUnchecked("vysper.org");
+    
+    private StanzaWriter stanzaWriter = Mockito.mock(StanzaWriter.class);
+    private XmppPingIQHandler handler = Mockito.mock(XmppPingIQHandler.class);
+    private XmppPingListener listener = Mockito.mock(XmppPingListener.class);
+    
+    private ExecutorService executor = Executors.newFixedThreadPool(2);
+    
+    @Test
+    public void ping() throws InterruptedException {
+        final XmppPinger pinger = new XmppPinger(handler);
+        
+        executor.execute(new Runnable() {
+            public void run() {
+                pinger.ping(stanzaWriter, FROM, TO, 10000, listener);
+            }
+        });
+        
+        // wait for the ping to be written
+        Thread.sleep(1000);
+        ArgumentCaptor<Stanza> pingCaptor = ArgumentCaptor.forClass(Stanza.class);
+        
+        Mockito.verify(stanzaWriter).write(pingCaptor.capture());
+        
+        Stanza actualPing = pingCaptor.getValue();
+        final String pingId = actualPing.getAttributeValue("id");
+        Assert.assertNotNull(pingId);
+        
+        executor.execute(new Runnable() {
+            public void run() {
+                pinger.pong(pingId);
+            }
+        });
+        
+        // wait for the pong to be processed
+        Thread.sleep(1000);
+        Stanza expectedPing = StanzaBuilder.createIQStanza(FROM, TO, IQStanzaType.GET, pingId)
+            .startInnerElement("ping", NamespaceURIs.URN_XMPP_PING)
+            .build();
+        
+        StanzaAssert.assertEquals(expectedPing, actualPing);
+        Assert.assertTrue(pingId.startsWith("xmppping-"));
+
+        Mockito.verify(listener).pong();
+        Mockito.verifyNoMoreInteractions(listener);
+    }
+
+    
+    @Test
+    public void pingTimeout() {
+        final XmppPinger pinger = new XmppPinger(handler);
+        pinger.ping(stanzaWriter, FROM, TO, 1000, listener);
+        
+        ArgumentCaptor<Stanza> pingCaptor = ArgumentCaptor.forClass(Stanza.class);
+        
+        Mockito.verify(stanzaWriter).write(pingCaptor.capture());
+        
+        Stanza actualPing = pingCaptor.getValue();
+        final String pingId = actualPing.getAttributeValue("id");
+        
+        Assert.assertNotNull(pingId);
+        
+        executor.execute(new Runnable() {
+            public void run() {
+                pinger.pong(pingId);
+            }
+        });
+        
+        Stanza expectedPing = StanzaBuilder.createIQStanza(FROM, TO, IQStanzaType.GET, pingId)
+            .startInnerElement("ping", NamespaceURIs.URN_XMPP_PING)
+            .build();
+        
+        StanzaAssert.assertEquals(expectedPing, actualPing);
+        Assert.assertTrue(pingId.startsWith("xmppping-"));
+
+        Mockito.verify(listener).timeout();
+        Mockito.verifyNoMoreInteractions(listener);
+    }
+
+}