diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a057556 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +logs +project/project +project/target +target +tmp +.history +dist +/.idea +/*.iml +META-INF +/out +/.idea_modules +/.classpath +/.project +/RUNNING_PID +/.settings \ No newline at end of file diff --git a/pom.xml b/pom.xml index c6f89ff..73ddbda 100644 --- a/pom.xml +++ b/pom.xml @@ -52,8 +52,8 @@ org.scalatest - scalatest_2.9.1 - 1.6.1 + scalatest_2.11 + 2.2.4 test @@ -95,7 +95,7 @@ org.scala-lang scala-compiler - 2.9.1 + 2.11.7 compile @@ -118,7 +118,7 @@ 2.8.1 true - 1.6 + 1.5 UTF-8 1g @@ -253,7 +253,6 @@ -Xmx384m - -target:jvm-1.5 -deprecation @@ -279,6 +278,7 @@ org.scala-tools maven-scala-plugin + 2.15.2 org.apache.maven.plugins @@ -286,7 +286,7 @@ 2.8.1 true - 1.6 + 1.5 UTF-8 1g @@ -324,8 +324,8 @@ http://oss.sonatype.org/content/repositories/snapshots true - 1.7 - 1.7 + 1.5 + 1.5 diff --git a/src/test/scala/org/jfarcand/wcs/test/BaseTest.scala b/src/test/scala/org/jfarcand/wcs/test/BaseTest.scala index 609a546..9e073a2 100644 --- a/src/test/scala/org/jfarcand/wcs/test/BaseTest.scala +++ b/src/test/scala/org/jfarcand/wcs/test/BaseTest.scala @@ -9,40 +9,32 @@ import javax.servlet.http.{ HttpServletResponse, HttpServletRequest } import org.eclipse.jetty.server.{ Request, Server } import org.scalatest.{ FlatSpec, BeforeAndAfterAll } -import org.scalatest.matchers.ShouldMatchers +import org.scalatest.Matchers -abstract class BaseTest extends Server with FlatSpec with BeforeAndAfterAll with ShouldMatchers { +trait BaseTest extends FlatSpec with BeforeAndAfterAll with Matchers { + + protected val server: Server = new Server() protected final val log: Logger = LoggerFactory.getLogger(classOf[BaseTest]) protected var port1: Int = 0 private var _connector: SelectChannelConnector = null - override def beforeAll(configMap: Map[String, Any]) = { - setUpGlobal - } - - override def afterAll(configMap: Map[String, Any]) = { - tearDownGlobal - } - - def setUpGlobal = { + override protected def beforeAll(): Unit = { port1 = findFreePort _connector = new SelectChannelConnector _connector.setPort(port1) - addConnector(_connector) + server.addConnector(_connector) val _wsHandler: BaseTest#WebSocketHandler = getWebSocketHandler - setHandler(_wsHandler) - start + server.setHandler(_wsHandler) + server.start() log.info("Local HTTP server started successfully") } - def tearDownGlobal = { - stop - } + override protected def afterAll(): Unit = server.stop() abstract class WebSocketHandler extends HandlerWrapper with WebSocketFactory.Acceptor { def getWebSocketFactory: WebSocketFactory = { - return _webSocketFactory + _webSocketFactory } override def handle(target: String, baseRequest: Request, request: HttpServletRequest, response: HttpServletResponse): Unit = { @@ -51,7 +43,7 @@ abstract class BaseTest extends Server with FlatSpec with BeforeAndAfterAll with } def checkOrigin(request: HttpServletRequest, origin: String): Boolean = { - return true + true } private final val _webSocketFactory: WebSocketFactory = new WebSocketFactory(this, 32 * 1024) @@ -61,16 +53,16 @@ abstract class BaseTest extends Server with FlatSpec with BeforeAndAfterAll with var socket: ServerSocket = null try { socket = new ServerSocket(0) - return socket.getLocalPort + socket.getLocalPort } finally { if (socket != null) { - socket.close + socket.close() } } } protected def getTargetUrl: String = { - "ws://127.0.0.1:" + port1; + "ws://127.0.0.1:" + port1 } def getWebSocketHandler: BaseTest#WebSocketHandler diff --git a/src/test/scala/org/jfarcand/wcs/test/WSSTest.scala b/src/test/scala/org/jfarcand/wcs/test/WSSTest.scala new file mode 100644 index 0000000..13167d4 --- /dev/null +++ b/src/test/scala/org/jfarcand/wcs/test/WSSTest.scala @@ -0,0 +1,30 @@ +package org.jfarcand.wcs.test + +import java.util.concurrent.CountDownLatch + +import org.jfarcand.wcs.{MessageListener, WebSocket} +import org.scalatest.{Matchers, FlatSpec} + +class WSSTest extends FlatSpec with Matchers { + + it should "receive three messages" in { + val w = WebSocket() + val latch: CountDownLatch = new CountDownLatch(3) + var messages: Seq[String] = Seq() + w open "wss://stream.pushbullet.com/websocket/wHDLQQ4cWz7uH89MjpKh47dcc8AhyFrd" listener new MessageListener { + override def onMessage(message: String): Unit = { + println(message) + messages = messages ++ Seq(message) + latch.countDown() + } + override def onOpen: Unit = println("open connection") + override def onClose: Unit = println("close connection") + override def onClose(code: Int, reason: String): Unit = println(s"close connection, code ${code.toString}. reason: $reason") + override def onError(t: Throwable): Unit = t.printStackTrace() + override def onMessage(message: Array[Byte]): Unit = onMessage(new String(message)) + } + latch.await() + assert(messages.size == 3) + } + +} diff --git a/src/test/scala/org/jfarcand/wcs/test/WebSocketTest.scala b/src/test/scala/org/jfarcand/wcs/test/WebSocketTest.scala index 6ed9f85..dd3b0da 100644 --- a/src/test/scala/org/jfarcand/wcs/test/WebSocketTest.scala +++ b/src/test/scala/org/jfarcand/wcs/test/WebSocketTest.scala @@ -17,19 +17,17 @@ package org.jfarcand.wcs.test import java.io.IOException +import java.util.concurrent.atomic.AtomicReference +import java.util.concurrent.{CountDownLatch, TimeUnit} import javax.servlet.http.HttpServletRequest +import org.jfarcand.wcs._ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner -import org.scalatest.FlatSpec -import org.scalatest.matchers.ShouldMatchers - -import org.jfarcand.wcs._ -import java.util.concurrent.{TimeUnit, CountDownLatch} -import java.util.concurrent.atomic.AtomicReference +import org.scalatest.{Matchers => ShouldMatchers} @RunWith(classOf[JUnitRunner]) -class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { +class WebSocketTest extends BaseTest with ShouldMatchers { private final class EchoTextWebSocket extends org.eclipse.jetty.websocket.WebSocket with org.eclipse.jetty.websocket.WebSocket.OnTextMessage with org.eclipse.jetty.websocket.WebSocket.OnBinaryMessage { private var connection: org.eclipse.jetty.websocket.WebSocket.Connection = null @@ -39,23 +37,18 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { connection.setMaxTextMessageSize(1000) } - def onClose(i: Int, s: String): Unit = { - connection.close - } + def onClose(i: Int, s: String): Unit = connection.close() def onMessage(s: String): Unit = { try { connection.sendMessage(s) } catch { - case e: IOException => { + case e: IOException => try { connection.sendMessage("FAIL") } catch { - case e1: IOException => { - e1.printStackTrace - } + case e1: IOException => e1.printStackTrace() } - } } } @@ -63,42 +56,37 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { try { connection.sendMessage(data, offset, length) } catch { - case e: IOException => { + case e: IOException => try { connection.sendMessage("FAIL") } catch { - case e1: IOException => { - e1.printStackTrace - } + case e1: IOException => e1.printStackTrace() } - } } } } - def getWebSocketHandler: BaseTest#WebSocketHandler = { - return new WebSocketHandler { - def doWebSocketConnect(httpServletRequest: HttpServletRequest, s: String): org.eclipse.jetty.websocket.WebSocket = { - return new EchoTextWebSocket - } + def getWebSocketHandler: BaseTest#WebSocketHandler = new WebSocketHandler { + def doWebSocketConnect(httpServletRequest: HttpServletRequest, s: String): org.eclipse.jetty.websocket.WebSocket = { + new EchoTextWebSocket } } it should "send a text message" in { val w = WebSocket() - var s = ""; - var latch: CountDownLatch = new CountDownLatch(1) + var s = "" + val latch: CountDownLatch = new CountDownLatch(1) w.open(getTargetUrl).listener(new TextListener { override def onMessage(message: String) { s = message - latch.countDown + latch.countDown() } }).send("foo") - latch.await + latch.await() assert(s === "foo") } @@ -106,17 +94,17 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { val w = WebSocket() var s = "" - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) w.open(getTargetUrl).listener(new BinaryListener { override def onMessage(message: Array[Byte]) { s = new String(message) - latch.countDown + latch.countDown() } }).send("foo".getBytes) - latch.await + latch.await() assert(s === "foo") } @@ -124,17 +112,17 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { val w = WebSocket() var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) w.listener(new TextListener { override def onOpen { s = true - latch.countDown + latch.countDown() } }).open(getTargetUrl) - latch.await + latch.await() assert(s) } @@ -142,17 +130,17 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { val w = WebSocket() var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) w.open(getTargetUrl).listener(new TextListener() { override def onOpen { s = true - latch.countDown + latch.countDown() } }) - latch.await + latch.await() assert(s) } @@ -160,7 +148,7 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { var w = WebSocket() val s: AtomicReference[String] = new AtomicReference[String] - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) w = w.listener(new TextListener { override def onMessage(message: String) { @@ -169,12 +157,12 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { override def onClose(code :Int, reason : String) { s.set(code + "-" + reason) - latch.countDown + latch.countDown() } }).open(getTargetUrl).send("foo") - latch.await + latch.await() assert(s.get() == "1000-Normal closure; the connection successfully completed whatever purpose for which it was created.") } @@ -183,7 +171,7 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { var w = WebSocket() var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) w = w.listener(new TextListener { override def onMessage(message: String) { @@ -192,12 +180,12 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { override def onClose { s = true - latch.countDown + latch.countDown() } }).open(getTargetUrl).send("foo") - latch.await + latch.await() assert(s) } @@ -207,7 +195,7 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { var w = WebSocket(o) var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) w = w.listener(new TextListener { override def onMessage(message: String) { @@ -216,12 +204,12 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { override def onClose { s = true - latch.countDown + latch.countDown() } }).open(getTargetUrl).send("foo") - latch.await + latch.await() assert(s) } @@ -229,12 +217,12 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { var w = WebSocket() var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) val t = new TextListener { override def onMessage(message: String) { s = true - latch.countDown + latch.countDown() } override def onClose { @@ -251,12 +239,12 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { var w = WebSocket() var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(1) + val latch: CountDownLatch = new CountDownLatch(1) val t = new TextListener { override def onMessage(message: String) { s = true - latch.countDown + latch.countDown() } override def onClose { @@ -272,13 +260,12 @@ class WebSocketTest extends BaseTest with FlatSpec with ShouldMatchers { it should "remove a listener after receiving a message" in { var w = WebSocket() - var s: Boolean = false - var latch: CountDownLatch = new CountDownLatch(2) + val latch: CountDownLatch = new CountDownLatch(2) val t = new TextListener { override def onMessage(message: String) { w.removeListener(this) - latch.countDown + latch.countDown() } override def onClose {