7 import org.specs.mock.Mockito
8 import org.mockito.Mock._
9 import org.mockito.Mockito._
11 class SocketOperationTest(val host:String, val port: Int) extends SocketOperations
13 object SocketOperationsSpec extends Specification with Mockito {
15 "Socket Operations" should {
16 var socketOperation: SocketOperationTest = null
17 var socket: Socket = null
18 var in: BufferedReader = null
21 socketOperation = new SocketOperationTest("localhost", 6379666)
23 in = mock[BufferedReader]
24 socketOperation.socket = socket
25 socketOperation.in = in
28 def readOkFromInput = { when(in.readLine()).thenReturn(socketOperation.OK) }
29 def readSingleFromInput = { when(in.readLine()).thenReturn(socketOperation.SINGLE) }
30 def readBulkFromInput = { in.readLine() returns("$6\r\nfoobar\r\n") thenReturns("$6\r\nfoobar\r\n") }
31 def readIntFromInput = { when(in.readLine()).thenReturn(socketOperation.INT+"666") }
33 "tell if it's connected" in {
34 socketOperation.connected mustEqual true
35 socketOperation.socket = null
36 socketOperation.connected mustEqual false
39 "return false when can't connect" in {
40 socketOperation.connect mustEqual false
43 "return current data input stream" in {
44 socketOperation.getInputStream mustEqual in
47 "read a line from socket" in {
49 socketOperation.in mustEqual in
50 socketOperation.readline mustEqual socketOperation.OK
53 "read type response" in {
55 socketOperation.readtype mustEqual ("+", socketOperation.OK)
58 "when reading responses" in {
62 socketOperation.readResponse mustEqual socketOperation.OK
65 "read single line" in {
67 socketOperation.readResponse mustEqual socketOperation.SINGLE
70 "reconnect on error" in {
71 socketOperation.readResponse mustEqual false
72 socket.close was called
73 socketOperation.connected mustEqual true
78 // this shouldn't be the response, it doesn't seem to work return and then returns.
79 // Here's what should happen: '$6\r\n' on first readLine and then 'foobar\r\n'
81 socketOperation.readtype mustEqual ("$", "$6\r\nfoobar\r\n")
82 socketOperation.readResponse mustEqual "$6\r\nfoobar\r\n"
83 socketOperation.bulkReply("$6\r\nfoobar\r\n") was called
88 socketOperation.readInt mustEqual 666
91 "read a boolean return value" in {
93 socketOperation.readBoolean mustEqual true