activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r1342813 - in /activemq/trunk/activemq-optional: ./ src/test/java/org/apache/activemq/transport/ws/ src/test/resources/org/apache/activemq/transport/ src/test/resources/org/apache/activemq/transport/ws/
Date Fri, 25 May 2012 21:07:53 GMT
Author: tabish
Date: Fri May 25 21:07:52 2012
New Revision: 1342813

URL: http://svn.apache.org/viewvc?rev=1342813&view=rev
Log:
Add tests for Websockets using selenium

Added:
    activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/
    activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/
    activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/websocket.html
  (with props)
Modified:
    activemq/trunk/activemq-optional/pom.xml
    activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java

Modified: activemq/trunk/activemq-optional/pom.xml
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-optional/pom.xml?rev=1342813&r1=1342812&r2=1342813&view=diff
==============================================================================
--- activemq/trunk/activemq-optional/pom.xml (original)
+++ activemq/trunk/activemq-optional/pom.xml Fri May 25 21:07:52 2012
@@ -202,6 +202,24 @@
       <artifactId>gentlyweb-utils</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>org.seleniumhq.selenium</groupId>
+      <artifactId>selenium-java</artifactId>
+      <version>2.21.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.seleniumhq.selenium</groupId>
+      <artifactId>selenium-chrome-driver</artifactId>
+      <version>2.21.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.seleniumhq.selenium</groupId>
+      <artifactId>selenium-firefox-driver</artifactId>
+      <version>2.21.0</version>
+      <scope>test</scope>
+    </dependency>
 
   </dependencies>
 

Modified: activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java?rev=1342813&r1=1342812&r2=1342813&view=diff
==============================================================================
--- activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java
(original)
+++ activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/transport/ws/WSTransportTest.java
Fri May 25 21:07:52 2012
@@ -17,29 +17,190 @@
 
 package org.apache.activemq.transport.ws;
 
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.Socket;
 import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.UnknownHostException;
 
 import org.apache.activemq.broker.BrokerFactory;
 import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.transport.stomp.StompConnection;
+import org.apache.activemq.util.Wait;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
-
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.chrome.ChromeDriver;
+import org.openqa.selenium.chrome.ChromeOptions;
+import org.openqa.selenium.firefox.FirefoxDriver;
+import org.openqa.selenium.firefox.FirefoxProfile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class WSTransportTest {
-    
-    protected String getBindLocation() {
-        return "ws://localhost:61614";
+
+    private static final Logger LOG = LoggerFactory.getLogger(WSTransportTest.class);
+    private static final int MESSAGE_COUNT = 1000;
+
+    private BrokerService broker;
+    private WebDriver driver;
+    private File profileDir;
+
+    private String stompUri;
+    private String wsUri;
+
+    private StompConnection stompConnection = new StompConnection();
+
+    protected BrokerService createBroker(boolean deleteMessages) throws Exception {
+        BrokerService broker = BrokerFactory.createBroker(
+                new URI("broker:()/localhost?persistent=false&useJmx=false"));
+
+        stompUri = broker.addConnector("stomp://localhost:0").getPublishableConnectString();
+        wsUri = broker.addConnector("ws://127.0.0.1:61623").getPublishableConnectString();
+        broker.setDeleteAllMessagesOnStartup(deleteMessages);
+        broker.start();
+        broker.waitUntilStarted();
+
+        return broker;
+    }
+
+    protected void stopBroker() throws Exception {
+        if (broker != null) {
+            broker.stop();
+            broker.waitUntilStopped();
+            broker = null;
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        profileDir = new File("activemq-data/profiles");
+        broker = createBroker(true);
+        stompConnect();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        try {
+            stompDisconnect();
+        } catch(Exception e) {
+            // Some tests explicitly disconnect from stomp so can ignore
+        } finally {
+            stopBroker();
+            if (driver != null) {
+                driver.quit();
+                driver = null;
+            }
+        }
     }
 
     @Test
     public void testBrokerStart() throws Exception {
-        BrokerService broker = BrokerFactory.createBroker(new URI("broker:()/localhost?persistent=false&useJmx=false"));
-        broker.addConnector(getBindLocation());
-        broker.start();
-        broker.waitUntilStarted();
-        Thread.sleep(2000);
-        //System.in.read();
-        broker.stop();
-        broker.waitUntilStopped();
+        assertTrue(broker.isStarted());
+    }
+
+    @Ignore
+    @Test
+    public void testFireFoxWebSockets() throws Exception {
+        driver = createFireFoxWebDriver();
+        doTestWebSockets(driver);
+    }
+
+    @Ignore
+    @Test
+    public void testChromeWebSockets() throws Exception {
+        driver = createChromeWebDriver();
+        doTestWebSockets(driver);
+    }
+
+    protected WebDriver createChromeWebDriver() throws Exception {
+        File profile = new File(profileDir, "chrome");
+        profile.mkdirs();
+        ChromeOptions options = new ChromeOptions();
+        options.addArguments("--enable-udd-profiles",
+                             "--user-data-dir=" + profile,
+                             "--allow-file-access-from-files");
+        return new ChromeDriver(options);
+    }
+
+    protected WebDriver createFireFoxWebDriver() throws Exception {
+        File profile = new File(profileDir, "firefox");
+        profile.mkdirs();
+        return new FirefoxDriver(new FirefoxProfile(profile));
+    }
+
+    private void stompConnect() throws IOException, URISyntaxException, UnknownHostException
{
+        URI connectUri = new URI(stompUri);
+        stompConnection.open(createSocket(connectUri));
+    }
+
+    private Socket createSocket(URI connectUri) throws IOException {
+        return new Socket("127.0.0.1", connectUri.getPort());
+    }
+
+    private void stompDisconnect() throws IOException {
+        if (stompConnection != null) {
+            stompConnection.close();
+            stompConnection = null;
+        }
+    }
+
+    public void doTestWebSockets(WebDriver driver) throws Exception {
+
+        URL url = getClass().getResource("websocket.html");
+
+        LOG.info("working dir = ");
+
+        LOG.info("page url: " + url);
+
+        driver.get(url + "#" + wsUri);
+
+        final WebElement webStatus = driver.findElement(By.id("status"));
+        final WebElement webReceived = driver.findElement(By.id("received"));
+
+        while ("Loading" == webStatus.getText()) {
+            Thread.sleep(100);
+        }
+
+        // Skip test if browser does not support websockets..
+        if (webStatus.getText() != "No WebSockets") {
+
+            assertTrue("Should have connected", Wait.waitFor(new Wait.Condition() {
+
+                @Override
+                public boolean isSatisified() throws Exception {
+                    return webStatus.getText().equals("Connected");
+                }
+            }));
+
+            stompConnection.connect("system", "manager");
+
+            stompConnection.send("/queue/websocket", "Hello");
+            assertTrue("Should have received message by now.", Wait.waitFor(new Wait.Condition()
{
+                @Override
+                public boolean isSatisified() throws Exception {
+                    return webReceived.getText().equals("Hello");
+                }
+            }));
+
+            for (int i = 1; i <= MESSAGE_COUNT; ++i) {
+                stompConnection.send("/queue/websocket", "messages #" + i);
+            }
+
+            assertTrue("Should have received messages by now.", Wait.waitFor(new Wait.Condition()
{
+                @Override
+                public boolean isSatisified() throws Exception {
+                    return webReceived.getText().equals("messages #" + MESSAGE_COUNT);
+                }
+            }));
+        }
     }
-    
 }

Added: activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/websocket.html
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/websocket.html?rev=1342813&view=auto
==============================================================================
--- activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/websocket.html
(added)
+++ activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/websocket.html
Fri May 25 21:07:52 2012
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<!--
+  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.
+-->
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <title>Chat Example Using STOMP Over WebSockets</title>
+    <!--[if lt IE 9]>
+      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
+    <![endif]-->
+    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
+    <script src="../../../../../../../../activemq-web-demo/src/main/webapp/websocket/stomp.js"></script>
+    <script>//<![CDATA[
+    $(document).ready(function() {
+      if(window.WebSocket) {
+        var url = "ws://localhost:61623";
+        if(window.location.hash) {
+          url = window.location.hash.substring(1);
+        }
+        var login = "admin";
+        var passcode = "password";
+        destination = "/queue/websocket";
+
+        $("#status").html("Connecting");
+        client = Stomp.client(url);
+
+        // the client is notified when it is connected to the server.
+        client.connect(login, passcode, function(frame) {
+          $("#status").html("Connected");
+          client.subscribe(destination, function(message) {
+            $("#received").html(message.body);
+          });
+        });
+      } else {
+        $("#status").html("No WebSockets");
+      }
+    });
+    //]]></script>
+  </head>
+  <body>
+    <div id="status">Loading</div>
+    <div id="received"></div>
+  </body>
+</html>

Propchange: activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/transport/ws/websocket.html
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message