Author: ngn
Date: Wed Jul 15 06:03:11 2009
New Revision: 794145

URL: http://svn.apache.org/viewvc?rev=794145&view=rev
Log:
Adding integration tests for XEP-0199 XMPP Ping (VYSPER-12). 
AbstractIntegrationTest should go somewhere else once we decide that we want to 
do tests like this.

Added:
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/PingPacket.java
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIntegrationTestCase.java
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingNoSupportIntegrationTestCase.java

Added: 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java?rev=794145&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java
 Wed Jul 15 06:03:11 2009
@@ -0,0 +1,167 @@
+/*
+ *  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.io.File;
+import java.io.IOException;
+import java.net.ServerSocket;
+
+import junit.framework.TestCase;
+
+import org.apache.vysper.mina.TCPEndpoint;
+import org.apache.vysper.storage.StorageProviderRegistry;
+import org.apache.vysper.storage.inmemory.MemoryStorageProviderRegistry;
+import org.apache.vysper.xmpp.authorization.AccountManagement;
+import org.apache.vysper.xmpp.server.XMPPServer;
+import org.jivesoftware.smack.ConnectionConfiguration;
+import org.jivesoftware.smack.PacketCollector;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.filter.PacketIDFilter;
+import org.jivesoftware.smack.packet.Packet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ */
+public abstract class AbstractIntegrationTestCase extends TestCase {
+
+    private final Logger logger = 
LoggerFactory.getLogger(AbstractIntegrationTestCase.class);
+    
+    protected static final String SERVER_DOMAIN = "vysper.org";
+    protected static final String TEST_USERNAME = "[email protected]";
+    protected static final String TEST_PASSWORD = "test";
+
+    private static final int DEFAULT_SERVER_PORT = 25222;
+    
+    protected XMPPConnection client;
+    private XMPPServer server;
+
+    protected void addModules(XMPPServer server) {
+        // default, do nothing
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        int port = findFreePort();
+        
+        startServer(port);
+        
+        connectClient(port);
+    }
+
+    private void startServer(int port) throws Exception {
+        StorageProviderRegistry providerRegistry = new 
MemoryStorageProviderRegistry();
+        
+        AccountManagement accountManagement = (AccountManagement) 
providerRegistry.retrieve(AccountManagement.class);
+        accountManagement.addUser(TEST_USERNAME, TEST_PASSWORD);
+
+        server = new XMPPServer(SERVER_DOMAIN);
+        
+        TCPEndpoint endpoint = new TCPEndpoint();
+        endpoint.setPort(port);
+        server.addEndpoint(endpoint);
+        server.setStorageProviderRegistry(providerRegistry);
+        
+        server.setTLSCertificateInfo(new 
File("src/main/config/bogus_mina_tls.cert"), "boguspw");
+
+        server.start();
+
+        addModules(server);
+    }
+    
+    private void connectClient(int port) throws Exception {
+        ConnectionConfiguration connectionConfiguration = new 
ConnectionConfiguration("localhost", port);
+        connectionConfiguration.setCompressionEnabled(false);
+        
connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
+        connectionConfiguration.setSASLAuthenticationEnabled(true);
+        connectionConfiguration.setDebuggerEnabled(false);
+        
+        XMPPConnection.DEBUG_ENABLED = true;
+        client = new XMPPConnection(connectionConfiguration);
+        
+        client.connect();
+        
+        client.login(TEST_USERNAME, TEST_PASSWORD);
+    }
+
+    protected Packet sendSync(XMPPConnection client, Packet request) {
+        // Create a packet collector to listen for a response.
+        PacketCollector collector = client.createPacketCollector(
+                       new PacketIDFilter(request.getPacketID()));
+
+        client.sendPacket(request);
+        
+        // Wait up to 5 seconds for a result.
+        return collector.nextResult(5000);
+    }
+
+    private int findFreePort() {
+        ServerSocket ss = null;
+        
+        // try using a predefined default port
+        // makes netstat -a debugging easier
+        try {
+            ss = new ServerSocket(DEFAULT_SERVER_PORT);
+            ss.setReuseAddress(true);
+            
+            // succeeded, return the default port
+            logger.info("Test is using the default test port {}", 
DEFAULT_SERVER_PORT);
+            return DEFAULT_SERVER_PORT;
+        } catch(IOException e) {
+            try {
+                ss = new ServerSocket(0);
+                ss.setReuseAddress(true);
+                int port = ss.getLocalPort(); 
+                logger.info("Failed to use default test port ({}), using {} 
instead", DEFAULT_SERVER_PORT, port);
+                return port;
+            } catch (IOException ee) {
+                // we could not even open a random port so
+                // the test will probably fail, anyways
+                // return the default port
+                return DEFAULT_SERVER_PORT;
+            }
+        } finally {
+            if(ss != null) {
+                try {
+                    ss.close();
+                } catch (IOException ignored) {
+                    ;
+                }
+            }
+        }
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        try {
+            client.disconnect();
+        } catch(Exception ignored) {
+            ;
+        }
+
+        try {
+            server.stop();
+        } catch(Exception ignored) {
+            ;
+        }
+    }
+    
+    
+}

Added: 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/PingPacket.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/PingPacket.java?rev=794145&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/PingPacket.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/PingPacket.java
 Wed Jul 15 06:03:11 2009
@@ -0,0 +1,30 @@
+/*
+ *  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 org.jivesoftware.smack.packet.IQ;
+
+public class PingPacket extends IQ {
+    @Override
+    public String getChildElementXML() {
+        return "<ping xmlns='urn:xmpp:ping'/>";
+    }
+    
+}
\ No newline at end of file

Added: 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIntegrationTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIntegrationTestCase.java?rev=794145&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIntegrationTestCase.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIntegrationTestCase.java
 Wed Jul 15 06:03:11 2009
@@ -0,0 +1,48 @@
+/*
+ *  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 
org.apache.vysper.xmpp.modules.extension.xep0119_xmppping.XmppPingModule;
+import org.apache.vysper.xmpp.server.XMPPServer;
+import org.jivesoftware.smack.packet.IQ;
+
+/**
+ */
+public class XmppPingIntegrationTestCase extends AbstractIntegrationTestCase {
+
+    @Override
+    protected void addModules(XMPPServer server) {
+        server.addModule(new XmppPingModule());
+    }
+
+    public void testClientServerPing() throws Exception {
+        PingPacket pingRequest = new PingPacket();
+        pingRequest.setType(IQ.Type.GET);
+        pingRequest.setTo(SERVER_DOMAIN);
+        pingRequest.setFrom(TEST_USERNAME);
+
+        IQ result = (IQ)sendSync(client, pingRequest);
+        
+        assertNotNull(result);
+        assertEquals(IQ.Type.RESULT, result.getType());
+        assertEquals(SERVER_DOMAIN, result.getFrom());
+        assertEquals(TEST_USERNAME, result.getTo());
+    }
+}

Added: 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingNoSupportIntegrationTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingNoSupportIntegrationTestCase.java?rev=794145&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingNoSupportIntegrationTestCase.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingNoSupportIntegrationTestCase.java
 Wed Jul 15 06:03:11 2009
@@ -0,0 +1,49 @@
+/*
+ *  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 org.apache.vysper.xmpp.server.XMPPServer;
+import org.jivesoftware.smack.packet.IQ;
+
+/**
+ */
+public class XmppPingNoSupportIntegrationTestCase extends 
AbstractIntegrationTestCase {
+
+    @Override
+    protected void addModules(XMPPServer server) {
+        // ping module not added
+    }
+
+    public void testClientServerPing() throws Exception {
+        PingPacket pingRequest = new PingPacket();
+        pingRequest.setType(IQ.Type.GET);
+        pingRequest.setTo(SERVER_DOMAIN);
+        pingRequest.setFrom(TEST_USERNAME);
+
+        IQ result = (IQ)sendSync(client, pingRequest);
+        
+        
+        assertNotNull(result);
+        assertEquals(IQ.Type.ERROR, result.getType());
+        assertEquals(SERVER_DOMAIN, result.getFrom());
+        assertEquals(TEST_USERNAME, result.getTo());
+        assertEquals("service-unavailable", result.getError().getCondition());
+    }
+}


Reply via email to