You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@toree.apache.org by lb...@apache.org on 2016/01/22 23:07:21 UTC
[13/51] [abbrv] incubator-toree git commit: Moved scala files to new
locations based on new package
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/kernel/protocol/v5/stream/KernelOuputStreamSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/kernel/protocol/v5/stream/KernelOuputStreamSpec.scala b/kernel/src/test/scala/com/ibm/spark/kernel/protocol/v5/stream/KernelOuputStreamSpec.scala
deleted file mode 100644
index cb4f158..0000000
--- a/kernel/src/test/scala/com/ibm/spark/kernel/protocol/v5/stream/KernelOuputStreamSpec.scala
+++ /dev/null
@@ -1,283 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.kernel.protocol.v5.stream
-
-import java.util.UUID
-
-import akka.actor.{ActorSelection, ActorSystem}
-import akka.testkit.{TestKit, TestProbe}
-import com.ibm.spark.kernel.protocol.v5._
-import com.ibm.spark.kernel.protocol.v5.kernel.ActorLoader
-import com.ibm.spark.utils.ScheduledTaskManager
-import org.mockito.Mockito._
-import org.scalatest.mock.MockitoSugar
-import org.scalatest._
-import play.api.libs.json._
-import com.ibm.spark.kernel.protocol.v5.content.StreamContent
-
-import scala.concurrent.duration._
-
-class KernelOuputStreamSpec
- extends TestKit(ActorSystem("KernelOutputStreamActorSystem"))
- with FunSpecLike with Matchers with GivenWhenThen with BeforeAndAfter
- with MockitoSugar
-{
-
- private var mockActorLoader: ActorLoader = _
- private var mockScheduledTaskManager: MockScheduledTaskManager = _
- private var kernelOutputRelayProbe: TestProbe = _
-
- //
- // SHARED ELEMENTS BETWEEN TESTS
- //
-
- private val ExecutionCount = 3
-
- private val MaxMessageTimeout = 1.second
- private val MaxNoMessageTimeout = 200.milliseconds
-
- private val GeneratedTaskId = UUID.randomUUID().toString
-
- private val skeletonBuilder = KMBuilder()
- .withIds(Nil).withSignature("").withContentString("")
- .withParentHeader(Header("", "", "", "", "5.0"))
-
- /**
- * This stubs out the methods of the scheduled task manager and provides a
- * form of verification, which is not (easily) doable with Mockito due to the
- * call-by-name argument in addTask.
- */
- private class MockScheduledTaskManager extends ScheduledTaskManager {
- private var addTaskCalled = false
- private var removeTaskCalled = false
- private var stopCalled = false
-
- def verifyAddTaskCalled(): Unit = addTaskCalled should be (true)
- def verifyRemoveTaskCalled(): Unit = removeTaskCalled should be (true)
- def verifyStopCalled(): Unit = stopCalled should be (true)
- def verifyAddTaskNotCalled(): Unit = addTaskCalled should be (false)
- def verifyRemoveTaskNotCalled(): Unit = removeTaskCalled should be (false)
- def verifyStopNotCalled(): Unit = stopCalled should be (false)
- def resetVerify(): Unit = {
- addTaskCalled = false
- removeTaskCalled = false
- stopCalled = false
- }
-
- override def addTask[T](executionDelay: Long, timeInterval: Long, task: => T): String =
- { addTaskCalled = true; GeneratedTaskId }
-
- override def removeTask(taskId: String): Boolean =
- { removeTaskCalled = true; true }
-
- override def stop(): Unit = stopCalled = true
-
- def teardown(): Unit = super.stop()
- }
-
- before {
- // Create a mock ActorLoader for the KernelOutputStream we are testing
- mockActorLoader = mock[ActorLoader]
-
- mockScheduledTaskManager = new MockScheduledTaskManager
-
- // Create a probe for the relay and mock the ActorLoader to return the
- // associated ActorSelection
- kernelOutputRelayProbe = TestProbe()
- val kernelOutputRelaySelection: ActorSelection =
- system.actorSelection(kernelOutputRelayProbe.ref.path.toString)
- doReturn(kernelOutputRelaySelection)
- .when(mockActorLoader).load(SystemActorType.KernelMessageRelay)
- }
-
- after {
- mockScheduledTaskManager.teardown()
- }
-
- describe("KernelOutputStream") {
- describe("#write(Int)") {
- it("should add a new byte to the internal list") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("a byte is written to the stream")
- val expected = 'a'
- kernelOutputStream.write(expected)
-
- Then("it should be appended to the internal list")
- kernelOutputStream.flush()
- val message = kernelOutputRelayProbe
- .receiveOne(MaxMessageTimeout).asInstanceOf[KernelMessage]
- val executeResult = Json.parse(message.contentString).as[StreamContent]
- executeResult.text should be (expected.toString)
- }
-
- it("should enable periodic flushing") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("a byte is written to the stream")
- val expected = 'a'
- kernelOutputStream.write(expected)
-
- Then("it should add a task to periodically flush")
- mockScheduledTaskManager.verifyAddTaskCalled()
- }
-
- it("should not enable periodic flushing if already enabled") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- And("periodic flushing is already enabled")
- kernelOutputStream.write('a')
- mockScheduledTaskManager.verifyAddTaskCalled()
- mockScheduledTaskManager.resetVerify()
-
- When("a byte is written to the stream")
- kernelOutputStream.write('b')
-
- Then("it should not add a task to periodically flush")
- mockScheduledTaskManager.verifyAddTaskNotCalled()
- }
- }
- describe("#flush") {
- it("should disable periodic flushing") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("a byte is written to the stream")
- val expected = 'a'
- kernelOutputStream.write(expected)
-
- And("flush is invoked")
- kernelOutputStream.flush()
-
- Then("it should remove the task to periodically flush")
- mockScheduledTaskManager.verifyRemoveTaskCalled()
- }
-
- it("should not disable periodic flushing if not enabled") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("flush is invoked")
- kernelOutputStream.flush()
-
- Then("it should not remove the task to periodically flush")
- mockScheduledTaskManager.verifyRemoveTaskNotCalled()
- }
-
- it("should not send empty (whitespace) messages if flag is false") {
- Given("a kernel output stream with send empty output set to false")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager,
- sendEmptyOutput = false
- )
-
- When("whitespace is created and flushed")
- val expected = "\r \r \n \t"
- kernelOutputStream.write(expected.getBytes)
- kernelOutputStream.flush()
-
- Then("no message should be sent")
- kernelOutputRelayProbe.expectNoMsg(MaxNoMessageTimeout)
- }
-
- it("should send empty (whitespace) messages if flag is true") {
- Given("a kernel output stream with send empty output set to false")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager,
- sendEmptyOutput = true
- )
-
- When("whitespace is created and flushed")
- val expected = "\r \r \n \t"
- kernelOutputStream.write(expected.getBytes)
- kernelOutputStream.flush()
-
- Then("the whitespace message should have been sent")
- val message = kernelOutputRelayProbe
- .receiveOne(MaxMessageTimeout).asInstanceOf[KernelMessage]
- val actual = Json.parse(message.contentString).as[StreamContent].text
-
- actual should be (expected)
- }
-
- it("should set the ids of the kernel message") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("a string is written as the result and flushed")
- val expected = "some string"
- kernelOutputStream.write(expected.getBytes)
- kernelOutputStream.flush()
-
- Then("the ids should be set to execute_result")
- val message = kernelOutputRelayProbe
- .receiveOne(MaxMessageTimeout).asInstanceOf[KernelMessage]
- message.ids should be (Seq(MessageType.Outgoing.Stream.toString))
- }
-
- it("should set the message type in the header of the kernel message to an execute_result") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("a string is written as the result and flushed")
- val expected = "some string"
- kernelOutputStream.write(expected.getBytes)
- kernelOutputStream.flush()
-
- Then("the msg_type in the header should be execute_result")
- val message = kernelOutputRelayProbe
- .receiveOne(MaxMessageTimeout).asInstanceOf[KernelMessage]
- message.header.msg_type should be (MessageType.Outgoing.Stream.toString)
- }
-
- it("should set the content string of the kernel message") {
- Given("a kernel output stream with a skeleton kernel builder")
- val kernelOutputStream = new KernelOutputStream(
- mockActorLoader, skeletonBuilder, mockScheduledTaskManager
- )
-
- When("a string is written as the result and flushed")
- val expected = "some string"
- kernelOutputStream.write(expected.getBytes)
- kernelOutputStream.flush()
-
- Then("the content string should have text/plain set to the string")
- val message = kernelOutputRelayProbe
- .receiveOne(MaxMessageTimeout).asInstanceOf[KernelMessage]
- val executeResult = Json.parse(message.contentString).as[StreamContent]
- executeResult.text should be (expected)
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddDepsSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddDepsSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddDepsSpec.scala
deleted file mode 100644
index 956088c..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddDepsSpec.scala
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.magic.builtin
-
-import java.io.{ByteArrayOutputStream, OutputStream}
-import java.net.URL
-
-import com.ibm.spark.dependencies.DependencyDownloader
-import com.ibm.spark.interpreter.Interpreter
-import com.ibm.spark.utils.ArgumentParsingSupport
-import org.apache.spark.SparkContext
-import org.scalatest.mock.MockitoSugar
-import org.scalatest.{GivenWhenThen, Matchers, FunSpec}
-import org.mockito.Mockito._
-import org.mockito.Matchers._
-
-import com.ibm.spark.magic._
-import com.ibm.spark.magic.dependencies._
-
-class AddDepsSpec extends FunSpec with Matchers with MockitoSugar
- with GivenWhenThen
-{
- describe("AddDeps"){
- describe("#execute") {
- it("should print out the help message if the input is invalid") {
- val byteArrayOutputStream = new ByteArrayOutputStream()
- val mockIntp = mock[Interpreter]
- val mockSC = mock[SparkContext]
- val mockDownloader = mock[DependencyDownloader]
- var printHelpWasRun = false
-
- val addDepsMagic = new AddDeps
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeDependencyDownloader
- with ArgumentParsingSupport
- {
- override val sparkContext: SparkContext = mockSC
- override val interpreter: Interpreter = mockIntp
- override val dependencyDownloader: DependencyDownloader =
- mockDownloader
- override val outputStream: OutputStream = byteArrayOutputStream
-
- override def printHelp(
- outputStream: OutputStream, usage: String
- ): Unit = printHelpWasRun = true
- }
-
- val expected = LineMagicOutput
- val actual = addDepsMagic.execute("notvalid")
-
- printHelpWasRun should be (true)
- verify(mockIntp, times(0)).addJars(any())
- verify(mockIntp, times(0)).bind(any(), any(), any(), any())
- verify(mockSC, times(0)).addJar(any())
- verify(mockDownloader, times(0)).retrieve(
- anyString(), anyString(), anyString(), anyBoolean(), anyBoolean())
- actual should be (expected)
- }
-
- it("should set the retrievals transitive to true if provided") {
- val mockDependencyDownloader = mock[DependencyDownloader]
- doReturn(Nil).when(mockDependencyDownloader).retrieve(
- anyString(), anyString(), anyString(), anyBoolean(), anyBoolean())
-
- val addDepsMagic = new AddDeps
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeDependencyDownloader
- with ArgumentParsingSupport
- {
- override val sparkContext: SparkContext = mock[SparkContext]
- override val interpreter: Interpreter = mock[Interpreter]
- override val dependencyDownloader: DependencyDownloader =
- mockDependencyDownloader
- override val outputStream: OutputStream = mock[OutputStream]
- }
-
- val expected = "com.ibm.spark" :: "kernel" :: "1.0" :: "--transitive" :: Nil
- addDepsMagic.execute(expected.mkString(" "))
-
- verify(mockDependencyDownloader).retrieve(
- expected(0), expected(1), expected(2), true)
- }
-
- it("should set the retrieval's transitive to false if not provided") {
- val mockDependencyDownloader = mock[DependencyDownloader]
- doReturn(Nil).when(mockDependencyDownloader).retrieve(
- anyString(), anyString(), anyString(), anyBoolean(), anyBoolean())
-
- val addDepsMagic = new AddDeps
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeDependencyDownloader
- with ArgumentParsingSupport
- {
- override val sparkContext: SparkContext = mock[SparkContext]
- override val interpreter: Interpreter = mock[Interpreter]
- override val dependencyDownloader: DependencyDownloader =
- mockDependencyDownloader
- override val outputStream: OutputStream = mock[OutputStream]
- }
-
- val expected = "com.ibm.spark" :: "kernel" :: "1.0" :: Nil
- addDepsMagic.execute(expected.mkString(" "))
-
- verify(mockDependencyDownloader).retrieve(
- expected(0), expected(1), expected(2), false)
- }
-
- it("should add retrieved artifacts to the interpreter") {
- val mockDependencyDownloader = mock[DependencyDownloader]
- doReturn(Nil).when(mockDependencyDownloader).retrieve(
- anyString(), anyString(), anyString(), anyBoolean(), anyBoolean())
- val mockInterpreter = mock[Interpreter]
-
- val addDepsMagic = new AddDeps
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeDependencyDownloader
- with ArgumentParsingSupport
- {
- override val sparkContext: SparkContext = mock[SparkContext]
- override val interpreter: Interpreter = mockInterpreter
- override val dependencyDownloader: DependencyDownloader =
- mockDependencyDownloader
- override val outputStream: OutputStream = mock[OutputStream]
- }
-
- val expected = "com.ibm.spark" :: "kernel" :: "1.0" :: Nil
- addDepsMagic.execute(expected.mkString(" "))
-
- verify(mockInterpreter).addJars(any[URL])
- }
-
- it("should add retrieved artifacts to the spark context") {
- val mockDependencyDownloader = mock[DependencyDownloader]
- val fakeUrl = new URL("file:/foo")
- doReturn(fakeUrl :: fakeUrl :: fakeUrl :: Nil)
- .when(mockDependencyDownloader).retrieve(
- anyString(), anyString(), anyString(), anyBoolean(), anyBoolean()
- )
- val mockSparkContext = mock[SparkContext]
-
- val addDepsMagic = new AddDeps
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeDependencyDownloader
- with ArgumentParsingSupport
- {
- override val sparkContext: SparkContext = mockSparkContext
- override val interpreter: Interpreter = mock[Interpreter]
- override val dependencyDownloader: DependencyDownloader =
- mockDependencyDownloader
- override val outputStream: OutputStream = mock[OutputStream]
- }
-
- val expected = "com.ibm.spark" :: "kernel" :: "1.0" :: Nil
- addDepsMagic.execute(expected.mkString(" "))
-
- verify(mockSparkContext, times(3)).addJar(anyString())
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddJarSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddJarSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddJarSpec.scala
deleted file mode 100644
index 5612c8a..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/AddJarSpec.scala
+++ /dev/null
@@ -1,220 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.magic.builtin
-
-import java.io.OutputStream
-import java.net.URL
-import java.nio.file.{FileSystems, Files}
-
-import com.ibm.spark.interpreter.Interpreter
-import com.ibm.spark.magic.dependencies.{IncludeConfig, IncludeOutputStream, IncludeInterpreter, IncludeSparkContext}
-import com.typesafe.config.ConfigFactory
-import org.apache.spark.SparkContext
-import org.scalatest.{Matchers, FunSpec}
-import org.scalatest.mock.MockitoSugar
-
-import org.mockito.Mockito._
-import org.mockito.Matchers._
-import com.ibm.spark.magic.MagicLoader
-
-class AddJarSpec extends FunSpec with Matchers with MockitoSugar {
- describe("AddJar"){
- describe("#execute") {
- it("should call addJar on the provided SparkContext and addJars on the " +
- "provided interpreter") {
- val mockSparkContext = mock[SparkContext]
- val mockInterpreter = mock[Interpreter]
- val mockOutputStream = mock[OutputStream]
- val mockMagicLoader = mock[MagicLoader]
- val testConfig = ConfigFactory.load()
-
- val addJarMagic = new AddJar
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeConfig
- {
- override val sparkContext: SparkContext = mockSparkContext
- override val interpreter: Interpreter = mockInterpreter
- override val outputStream: OutputStream = mockOutputStream
- override lazy val magicLoader: MagicLoader = mockMagicLoader
- override val config = testConfig
- override def downloadFile(fileUrl: URL, destinationUrl: URL): URL =
- new URL("file://someFile") // Cannot mock URL
- }
-
- addJarMagic.execute("""http://www.example.com/someJar.jar""")
-
- verify(mockSparkContext).addJar(anyString())
- verify(mockInterpreter).addJars(any[URL])
- verify(mockMagicLoader, times(0)).addJar(any())
- }
-
- it("should raise exception if jar file does not end in .jar or .zip") {
- val mockOutputStream = mock[OutputStream]
-
- val addJarMagic = new AddJar
- with IncludeOutputStream
- {
- override val outputStream: OutputStream = mockOutputStream
- }
-
- intercept[IllegalArgumentException] {
- addJarMagic.execute("""http://www.example.com/""")
- }
- intercept[IllegalArgumentException] {
- addJarMagic.execute("""http://www.example.com/not_a_jar""")
- }
- }
-
- it("should extract jar file name from jar URL") {
- val mockOutputStream = mock[OutputStream]
-
- val addJarMagic = new AddJar
- with IncludeOutputStream
- {
- override val outputStream: OutputStream = mockOutputStream
- }
-
- var url = """http://www.example.com/someJar.jar"""
- var jarName = addJarMagic.getFileFromLocation(url)
- assert(jarName == "someJar.jar")
-
- url = """http://www.example.com/remotecontent?filepath=/path/to/someJar.jar"""
- jarName = addJarMagic.getFileFromLocation(url)
- assert(jarName == "someJar.jar")
-
- url = """http://www.example.com/"""
- jarName = addJarMagic.getFileFromLocation(url)
- assert(jarName == "")
- }
-
- it("should use a cached jar if the force option is not provided") {
- val mockSparkContext = mock[SparkContext]
- val mockInterpreter = mock[Interpreter]
- val mockOutputStream = mock[OutputStream]
- var downloadFileCalled = false // Used to verify that downloadFile
- // was or was not called in this test
- val testConfig = ConfigFactory.load()
-
- val addJarMagic = new AddJar
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeConfig
- {
- override val sparkContext: SparkContext = mockSparkContext
- override val interpreter: Interpreter = mockInterpreter
- override val outputStream: OutputStream = mockOutputStream
- override val config = testConfig
- override def downloadFile(fileUrl: URL, destinationUrl: URL): URL = {
- downloadFileCalled = true
- new URL("file://someFile") // Cannot mock URL
- }
- }
-
- // Create a temporary file representing our jar to fake the cache
- val tmpFilePath = Files.createTempFile(
- FileSystems.getDefault.getPath(AddJar.getJarDir(testConfig)),
- "someJar",
- ".jar"
- )
-
- addJarMagic.execute(
- """http://www.example.com/""" + tmpFilePath.getFileName)
-
- tmpFilePath.toFile.delete()
-
- downloadFileCalled should be (false)
- verify(mockSparkContext).addJar(anyString())
- verify(mockInterpreter).addJars(any[URL])
- }
-
- it("should not use a cached jar if the force option is provided") {
- val mockSparkContext = mock[SparkContext]
- val mockInterpreter = mock[Interpreter]
- val mockOutputStream = mock[OutputStream]
- var downloadFileCalled = false // Used to verify that downloadFile
- // was or was not called in this test
- val testConfig = ConfigFactory.load()
-
- val addJarMagic = new AddJar
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeConfig
- {
- override val sparkContext: SparkContext = mockSparkContext
- override val interpreter: Interpreter = mockInterpreter
- override val outputStream: OutputStream = mockOutputStream
- override val config = testConfig
- override def downloadFile(fileUrl: URL, destinationUrl: URL): URL = {
- downloadFileCalled = true
- new URL("file://someFile") // Cannot mock URL
- }
- }
-
- // Create a temporary file representing our jar to fake the cache
- val tmpFilePath = Files.createTempFile(
- FileSystems.getDefault.getPath(AddJar.getJarDir(testConfig)),
- "someJar",
- ".jar"
- )
-
- addJarMagic.execute(
- """-f http://www.example.com/""" + tmpFilePath.getFileName)
-
- tmpFilePath.toFile.delete()
-
- downloadFileCalled should be (true)
- verify(mockSparkContext).addJar(anyString())
- verify(mockInterpreter).addJars(any[URL])
- }
-
- it("should add magic jar to magicloader and not to interpreter and spark"+
- "context") {
- val mockSparkContext = mock[SparkContext]
- val mockInterpreter = mock[Interpreter]
- val mockOutputStream = mock[OutputStream]
- val mockMagicLoader = mock[MagicLoader]
- val testConfig = ConfigFactory.load()
-
- val addJarMagic = new AddJar
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- with IncludeConfig
- {
- override val sparkContext: SparkContext = mockSparkContext
- override val interpreter: Interpreter = mockInterpreter
- override val outputStream: OutputStream = mockOutputStream
- override lazy val magicLoader: MagicLoader = mockMagicLoader
- override val config = testConfig
- override def downloadFile(fileUrl: URL, destinationUrl: URL): URL =
- new URL("file://someFile") // Cannot mock URL
- }
-
- addJarMagic.execute(
- """--magic http://www.example.com/someJar.jar""")
-
- verify(mockMagicLoader).addJar(any())
- verify(mockSparkContext, times(0)).addJar(anyString())
- verify(mockInterpreter, times(0)).addJars(any[URL])
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/BuiltinLoaderSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/BuiltinLoaderSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/BuiltinLoaderSpec.scala
deleted file mode 100644
index d2abde7..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/BuiltinLoaderSpec.scala
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.magic.builtin
-
-import org.scalatest.mock.MockitoSugar
-import org.scalatest.{Matchers, FunSpec}
-
-class BuiltinLoaderSpec extends FunSpec with Matchers with MockitoSugar {
- describe("BuiltinLoader") {
- describe("#getClasses") {
- it("should return classes in a package") {
- val pkg = this.getClass.getPackage.getName
- val classes = new BuiltinLoader().getClasses(pkg)
- classes.size shouldNot be(0)
- }
- }
-
- describe("#loadClasses") {
- it("should return class objects for classes in a package") {
- val pkg = this.getClass.getPackage.getName
- val classes = new BuiltinLoader().loadClasses(pkg).toList
- classes.contains(this.getClass) should be (true)
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/HtmlSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/HtmlSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/HtmlSpec.scala
deleted file mode 100644
index 541cfcd..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/HtmlSpec.scala
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.magic.builtin
-
-import com.ibm.spark.kernel.protocol.v5.MIMEType
-import com.ibm.spark.magic.CellMagicOutput
-import org.scalatest.mock.MockitoSugar
-import org.scalatest.{FunSpec, Matchers}
-
-class HtmlSpec extends FunSpec with Matchers with MockitoSugar {
- describe("Html"){
- describe("#execute") {
- it("should return the entire cell's contents with the MIME type of " +
- "text/html") {
- val htmlMagic = new Html
-
- val code = "some code on a line\nanother line"
- val expected = CellMagicOutput(MIMEType.TextHtml -> code)
- htmlMagic.execute(code) should be (expected)
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/JavaScriptSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/JavaScriptSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/JavaScriptSpec.scala
deleted file mode 100644
index 33dfbd5..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/JavaScriptSpec.scala
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.magic.builtin
-
-import org.scalatest.mock.MockitoSugar
-import org.scalatest.{FunSpec, Matchers}
-import com.ibm.spark.magic.CellMagicOutput
-import com.ibm.spark.kernel.protocol.v5.MIMEType
-
-class JavaScriptSpec extends FunSpec with Matchers with MockitoSugar {
- describe("JavaScript"){
- describe("#execute") {
- it("should return the entire cell's contents with the MIME type of text/javascript") {
- val javaScriptMagic = new JavaScript
-
- val code = "some code on a line\nmore code on another line"
- val expected = CellMagicOutput(MIMEType.ApplicationJavaScript -> code)
- javaScriptMagic.execute(code) should be (expected)
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/LSMagicSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/LSMagicSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/LSMagicSpec.scala
deleted file mode 100644
index 60b4a6a..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/LSMagicSpec.scala
+++ /dev/null
@@ -1,65 +0,0 @@
-package com.ibm.spark.magic.builtin
-
-import java.io.OutputStream
-import java.net.URL
-
-import com.ibm.spark.interpreter.Interpreter
-import com.ibm.spark.magic.dependencies.{IncludeOutputStream, IncludeInterpreter, IncludeSparkContext}
-import com.ibm.spark.magic.{CellMagic, LineMagic}
-import org.apache.spark.SparkContext
-import org.scalatest.{Matchers, FunSpec}
-import org.scalatest.mock.MockitoSugar
-
-import org.mockito.Mockito._
-import org.mockito.Matchers._
-
-class TestLSMagic(sc: SparkContext, intp: Interpreter, os: OutputStream)
- extends LSMagic
- with IncludeSparkContext
- with IncludeInterpreter
- with IncludeOutputStream
- {
- override val sparkContext: SparkContext = sc
- override val interpreter: Interpreter = intp
- override val outputStream: OutputStream = os
- }
-
-class LSMagicSpec extends FunSpec with Matchers with MockitoSugar {
- describe("LSMagic") {
-
- describe("#execute") {
- it("should call println with a magics message") {
- val lsm = spy(new TestLSMagic(
- mock[SparkContext], mock[Interpreter], mock[OutputStream])
- )
- val classList = new BuiltinLoader().loadClasses()
- lsm.execute("")
- verify(lsm).magicNames("%", classOf[LineMagic], classList)
- verify(lsm).magicNames("%%", classOf[CellMagic], classList)
- }
- }
-
- describe("#magicNames") {
- it("should filter classnames by interface") {
- val prefix = "%"
- val interface = classOf[LineMagic]
- val classes : List[Class[_]] = List(classOf[LSMagic], classOf[Integer])
- val lsm = new TestLSMagic(
- mock[SparkContext], mock[Interpreter], mock[OutputStream])
- lsm.magicNames(prefix, interface, classes).length should be(1)
- }
- it("should prepend prefix to each name"){
- val prefix = "%"
- val className = classOf[LSMagic].getSimpleName
- val interface = classOf[LineMagic]
- val expected = s"${prefix}${className}"
- val classes : List[Class[_]] = List(classOf[LSMagic], classOf[Integer])
- val lsm = new TestLSMagic(
- mock[SparkContext], mock[Interpreter], mock[OutputStream])
- lsm.magicNames(prefix, interface, classes) should be(List(expected))
- }
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/magic/builtin/RDDSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/magic/builtin/RDDSpec.scala b/kernel/src/test/scala/com/ibm/spark/magic/builtin/RDDSpec.scala
deleted file mode 100644
index 56b4cb7..0000000
--- a/kernel/src/test/scala/com/ibm/spark/magic/builtin/RDDSpec.scala
+++ /dev/null
@@ -1,117 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.magic.builtin
-
-import com.ibm.spark.interpreter.Results.Result
-import com.ibm.spark.interpreter.{Results, ExecuteAborted, ExecuteError, Interpreter}
-import com.ibm.spark.kernel.protocol.v5.MIMEType
-import com.ibm.spark.magic.dependencies.{IncludeKernelInterpreter, IncludeInterpreter}
-import org.apache.spark.sql.DataFrame
-import org.apache.spark.sql.types.StructType
-import org.mockito.Matchers._
-import org.mockito.Mockito._
-import org.scalatest.mock.MockitoSugar
-import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
-import play.api.libs.json.Json
-
-class RDDSpec extends FunSpec with Matchers with MockitoSugar with BeforeAndAfter {
-
- val resOutput = "res1: org.apache.spark.sql.SchemaRDD ="
-
- val mockInterpreter = mock[Interpreter]
- val mockDataFrame = mock[DataFrame]
- val mockRdd = mock[org.apache.spark.rdd.RDD[Any]]
- val mockStruct = mock[StructType]
- val columns = Seq("foo", "bar").toArray
- val rows = Array( Array("a", "b"), Array("c", "d") )
-
- doReturn(mockStruct).when(mockDataFrame).schema
- doReturn(columns).when(mockStruct).fieldNames
- doReturn(mockRdd).when(mockDataFrame).map(any())(any())
- doReturn(rows).when(mockRdd).take(anyInt())
-
- val rddMagic = new RDD with IncludeKernelInterpreter {
- override val kernelInterpreter: Interpreter = mockInterpreter
- }
-
- before {
- doReturn(Some("someRDD")).when(mockInterpreter).lastExecutionVariableName
- doReturn(Some(mockDataFrame)).when(mockInterpreter).read(anyString())
- doReturn((Results.Success, Left(resOutput)))
- .when(mockInterpreter).interpret(anyString(), anyBoolean())
- }
-
- describe("RDD") {
- describe("#execute") {
- it("should return valid JSON when the executed code evaluates to a " +
- "SchemaRDD") {
- val magicOutput = rddMagic.execute("schemaRDD")
- magicOutput.contains(MIMEType.ApplicationJson) should be (true)
- Json.parse(magicOutput(MIMEType.ApplicationJson))
- }
-
- it("should return normally when the executed code does not evaluate to " +
- "a SchemaRDD") {
- doReturn((mock[Result], Left("foo"))).when(mockInterpreter)
- .interpret(anyString(), anyBoolean())
- val magicOutput = rddMagic.execute("")
- magicOutput.contains(MIMEType.PlainText) should be (true)
- }
-
- it("should return error message when the interpreter does not return " +
- "SchemaRDD as expected") {
- doReturn(Some("foo")).when(mockInterpreter).read(anyString())
- val magicOutput = rddMagic.execute("")
- magicOutput.contains(MIMEType.PlainText) should be (true)
- }
-
- it("should throw a Throwable if the interpreter returns an ExecuteError"){
- val expected = "some error message"
- val mockExecuteError = mock[ExecuteError]
- doReturn(expected).when(mockExecuteError).value
-
- doReturn((mock[Result], Right(mockExecuteError))).when(mockInterpreter)
- .interpret(anyString(), anyBoolean())
- val actual = {
- val exception = intercept[Throwable] {
- rddMagic.execute("")
- }
- exception.getLocalizedMessage
- }
-
- actual should be (expected)
- }
-
- it("should throw a Throwable if the interpreter returns an " +
- "ExecuteAborted") {
- val expected = "RDD magic aborted!"
- val mockExecuteAborted = mock[ExecuteAborted]
-
- doReturn((mock[Result], Right(mockExecuteAborted)))
- .when(mockInterpreter).interpret(anyString(), anyBoolean())
- val actual = {
- val exception = intercept[Throwable] {
- rddMagic.execute("")
- }
- exception.getLocalizedMessage
- }
-
- actual should be (expected)
- }
- }
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/com/ibm/spark/utils/json/RddToJsonSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/com/ibm/spark/utils/json/RddToJsonSpec.scala b/kernel/src/test/scala/com/ibm/spark/utils/json/RddToJsonSpec.scala
deleted file mode 100644
index 121d35e..0000000
--- a/kernel/src/test/scala/com/ibm/spark/utils/json/RddToJsonSpec.scala
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright 2014 IBM Corp.
- *
- * Licensed 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 com.ibm.spark.utils.json
-
-import org.apache.spark.rdd.RDD
-import org.apache.spark.sql.DataFrame
-import org.apache.spark.sql.types.StructType
-import org.scalatest.mock.MockitoSugar
-import org.scalatest.{Matchers, FunSpec}
-import org.mockito.Mockito._
-import org.mockito.Matchers._
-import play.api.libs.json.{JsArray, JsString, Json}
-
-class RddToJsonSpec extends FunSpec with MockitoSugar with Matchers {
-
- val mockDataFrame = mock[DataFrame]
- val mockRdd = mock[RDD[Any]]
- val mockStruct = mock[StructType]
- val columns = Seq("foo", "bar").toArray
- val rows = Array( Array("a", "b"), Array("c", "d") )
-
- doReturn(mockStruct).when(mockDataFrame).schema
- doReturn(columns).when(mockStruct).fieldNames
- doReturn(mockRdd).when(mockDataFrame).map(any())(any())
- doReturn(rows).when(mockRdd).take(anyInt())
-
- describe("RddToJson") {
- describe("#convert(SchemaRDD)") {
- it("should convert to valid JSON object") {
-
- val json = RddToJson.convert(mockDataFrame)
- val jsValue = Json.parse(json)
-
- jsValue \ "columns" should be (JsArray(Seq(JsString("foo"), JsString("bar"))))
- jsValue \ "rows" should be (JsArray(Seq(
- JsArray(Seq(JsString("a"), JsString("b"))),
- JsArray(Seq(JsString("c"), JsString("d"))))))
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/boot/CommandLineOptionsSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/boot/CommandLineOptionsSpec.scala b/kernel/src/test/scala/org/apache/toree/boot/CommandLineOptionsSpec.scala
new file mode 100644
index 0000000..703d677
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/boot/CommandLineOptionsSpec.scala
@@ -0,0 +1,328 @@
+/*
+ * Copyright 2014 IBM Corp.
+ *
+ * Licensed 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 com.ibm.spark.boot
+
+import java.io.File
+
+import com.typesafe.config.Config
+import joptsimple.OptionException
+import org.scalatest.{FunSpec, Matchers}
+
+import scala.collection.JavaConverters._
+
+class CommandLineOptionsSpec extends FunSpec with Matchers {
+
+ describe("CommandLineOptions") {
+ describe("when received --max-interpreter-threads=<int>") {
+ it("should set the configuration to the specified value") {
+ val expected = 999
+ val options = new CommandLineOptions(
+ s"--max-interpreter-threads=$expected" :: Nil
+ )
+
+ val actual = options.toConfig.getInt("max_interpreter_threads")
+
+ actual should be (expected)
+ }
+ }
+
+ describe("when received --help") {
+ it("should set the help flag to true") {
+ val options = new CommandLineOptions("--help" :: Nil)
+
+ options.help should be (true)
+ }
+ }
+
+ describe("when received -h") {
+ it("should set the help flag to true") {
+ val options = new CommandLineOptions("-h" :: Nil)
+
+ options.help should be (true)
+ }
+ }
+
+ describe("when not received --help or -h") {
+ it("should set the help flag to false") {
+ val options = new CommandLineOptions(Nil)
+
+ options.help should be (false)
+ }
+ }
+
+ describe("when received --version") {
+ it("should set the version flag to true") {
+ val options = new CommandLineOptions("--version" :: Nil)
+
+ options.version should be (true)
+ }
+ }
+
+ describe("when received -v") {
+ it("should set the version flag to true") {
+ val options = new CommandLineOptions("-v" :: Nil)
+
+ options.version should be (true)
+ }
+ }
+
+ describe("when not received --version or -v") {
+ it("should set the version flag to false") {
+ val options = new CommandLineOptions(Nil)
+
+ options.version should be (false)
+ }
+ }
+
+ describe("when received --spark-conf=<key>=<value>") {
+ it("should add the key-value pair to the string representation") {
+ val expected = "key=value"
+ val options = new CommandLineOptions(s"--spark-conf=$expected" :: Nil)
+ val actual = options.toConfig.getString("spark_configuration")
+
+ actual should be (expected)
+ }
+
+ it("should append to existing command line key-value pairs") {
+ val expected = "key1=value1" :: "key2=value2" :: Nil
+ val options = new CommandLineOptions(
+ s"--spark-conf=${expected(0)}" ::
+ s"--spark-conf=${expected(1)}" ::
+ Nil
+ )
+ val actual = options.toConfig.getString("spark_configuration")
+
+ actual should be (expected.mkString(","))
+ }
+ }
+
+ describe("when received -S<key>=<value>") {
+ it("should add the key-value pair to the string representation") {
+ val expected = "key=value"
+ val options = new CommandLineOptions(s"-S$expected" :: Nil)
+ val actual = options.toConfig.getString("spark_configuration")
+
+ actual should be(expected)
+ }
+
+ it("should append to existing command line key-value pairs") {
+ val expected = "key1=value1" :: "key2=value2" :: Nil
+ val options = new CommandLineOptions(
+ s"-S${expected(0)}" ::
+ s"-S${expected(1)}" ::
+ Nil
+ )
+ val actual = options.toConfig.getString("spark_configuration")
+
+ actual should be (expected.mkString(","))
+ }
+ }
+
+ describe("when received --profile=<path>") {
+ it("should error if path is not set") {
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--profile"))
+ }
+ }
+
+ describe("#toConfig") {
+ it("should include values specified in file") {
+
+ val pathToProfileFixture: String = new File(getClass.getResource("/fixtures/profile.json").toURI).getAbsolutePath
+ val options = new CommandLineOptions(Seq("--profile="+pathToProfileFixture))
+
+ val config: Config = options.toConfig
+
+ config.entrySet() should not be ('empty)
+ config.getInt("stdin_port") should be(12345)
+ config.getInt("shell_port") should be(54321)
+ config.getInt("iopub_port") should be(11111)
+ config.getInt("control_port") should be(22222)
+ config.getInt("hb_port") should be(33333)
+ }
+ }
+ }
+
+ describe("when received --<protocol port name>=<value>"){
+ it("should error if value is not set") {
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--stdin-port"))
+ }
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--shell-port"))
+ }
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--iopub-port"))
+ }
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--control-port"))
+ }
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--heartbeat-port"))
+ }
+ }
+
+ describe("#toConfig") {
+ it("should return config with commandline option values") {
+
+ val options = new CommandLineOptions(List(
+ "--stdin-port", "99999",
+ "--shell-port", "88888",
+ "--iopub-port", "77777",
+ "--control-port", "55555",
+ "--heartbeat-port", "44444"
+ ))
+
+ val config: Config = options.toConfig
+
+ config.entrySet() should not be ('empty)
+ config.getInt("stdin_port") should be(99999)
+ config.getInt("shell_port") should be(88888)
+ config.getInt("iopub_port") should be(77777)
+ config.getInt("control_port") should be(55555)
+ config.getInt("hb_port") should be(44444)
+ }
+ }
+ }
+
+ describe("when received --profile and --<protocol port name>=<value>"){
+ describe("#toConfig") {
+ it("should return config with <protocol port> argument value") {
+
+ val pathToProfileFixture: String = (new File(getClass.getResource("/fixtures/profile.json").toURI)).getAbsolutePath
+ val options = new CommandLineOptions(List("--profile", pathToProfileFixture, "--stdin-port", "99999", "--shell-port", "88888"))
+
+ val config: Config = options.toConfig
+
+ config.entrySet() should not be ('empty)
+ config.getInt("stdin_port") should be(99999)
+ config.getInt("shell_port") should be(88888)
+ config.getInt("iopub_port") should be(11111)
+ config.getInt("control_port") should be(22222)
+ }
+ }
+
+ }
+
+ describe("when no arguments are received"){
+ describe("#toConfig") {
+ it("should read default value set in reference.conf") {
+
+ val options = new CommandLineOptions(Nil)
+
+ val config: Config = options.toConfig
+ config.getInt("stdin_port") should be(48691)
+ config.getInt("shell_port") should be(40544)
+ config.getInt("iopub_port") should be(43462)
+ config.getInt("control_port") should be(44808)
+ }
+ }
+ }
+
+ describe("when using -- to separate interpreter arguments"){
+ describe("#toConfig") {
+ it("should return interpreter_args config property when there are args before --") {
+
+ val options = new CommandLineOptions(List("--stdin-port", "99999", "--shell-port", "88888", "--", "someArg1", "someArg2", "someArg3"))
+
+ val config: Config = options .toConfig
+
+ config.entrySet() should not be ('empty)
+ config.getStringList("interpreter_args").asScala should be (List("someArg1", "someArg2", "someArg3"))
+ }
+
+ it("should return interpreter_args config property when args is at the beginning") {
+
+ val options = new CommandLineOptions(List("--", "someArg1", "someArg2", "someArg3"))
+
+ val config: Config = options .toConfig
+
+ config.entrySet() should not be ('empty)
+ config.getStringList("interpreter_args").asScala should be (List("someArg1", "someArg2", "someArg3"))
+ }
+
+ it("should return interpreter_args config property as empty list when there is nothing after --") {
+
+ val options = new CommandLineOptions(List("--stdin-port", "99999", "--shell-port", "88888", "--"))
+
+ val config: Config = options .toConfig
+
+ config.entrySet() should not be ('empty)
+ config.getStringList("interpreter_args").asScala should be ('empty)
+ }
+ }
+ }
+
+ describe("when received --ip=<value>") {
+ it("should error if value is not set") {
+ intercept[OptionException] {
+ new CommandLineOptions(Seq("--ip"))
+ }
+ }
+
+ describe("#toConfig") {
+ it("should set ip to specified value") {
+ val expected = "1.2.3.4"
+ val options = new CommandLineOptions(s"--ip=${expected}" :: Nil)
+ val config: Config = options.toConfig
+
+ config.getString("ip") should be(expected)
+ }
+
+ it("should set ip to 127.0.0.1") {
+ val options = new CommandLineOptions(Nil)
+ val config: Config = options.toConfig
+
+ config.getString("ip") should be("127.0.0.1")
+ }
+ }
+ }
+
+ describe("when received options with surrounding whitespace") {
+ it("should trim whitespace") {
+ val url1 = "url1"
+ val url2 = "url2"
+
+ val options = new CommandLineOptions(Seq(
+ " --magic-url ", s" ${url1}\t",
+ "--magic-url", s" \t ${url2} \t"
+ ))
+ val config: Config = options.toConfig
+
+ config.getList("magic_urls").unwrapped.asScala should
+ be (Seq(url1, url2))
+ }
+ }
+
+ describe("when received --interpreter-plugin") {
+ it("should return the interpreter-plugin along with the defaults") {
+ val options = new CommandLineOptions(Seq(
+ "--interpreter-plugin",
+ "dummy:test.utils.DummyInterpreter"
+ ))
+
+ val config: Config = options.toConfig
+
+ val p = config.getList("interpreter_plugins")
+
+ p should not be empty
+
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/comm/KernelCommManagerSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/comm/KernelCommManagerSpec.scala b/kernel/src/test/scala/org/apache/toree/comm/KernelCommManagerSpec.scala
new file mode 100644
index 0000000..7b4442c
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/comm/KernelCommManagerSpec.scala
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2014 IBM Corp.
+ *
+ * Licensed 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 com.ibm.spark.comm
+
+import com.ibm.spark.kernel.protocol.v5
+import com.ibm.spark.kernel.protocol.v5._
+import com.ibm.spark.kernel.protocol.v5.content.CommContent
+import com.ibm.spark.kernel.protocol.v5.kernel.ActorLoader
+import org.scalatest.mock.MockitoSugar
+import org.mockito.Mockito._
+import org.mockito.Matchers._
+import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
+
+class KernelCommManagerSpec extends FunSpec with Matchers with BeforeAndAfter
+ with MockitoSugar
+{
+ private val TestTargetName = "some target"
+
+ private var mockActorLoader: ActorLoader = _
+ private var mockKMBuilder: KMBuilder = _
+ private var mockCommRegistrar: CommRegistrar = _
+ private var kernelCommManager: KernelCommManager = _
+
+ private var generatedCommWriter: CommWriter = _
+
+ before {
+ mockActorLoader = mock[ActorLoader]
+ mockKMBuilder = mock[KMBuilder]
+ mockCommRegistrar = mock[CommRegistrar]
+
+ kernelCommManager = new KernelCommManager(
+ mockActorLoader,
+ mockKMBuilder,
+ mockCommRegistrar
+ ) {
+ override protected def newCommWriter(commId: UUID): CommWriter = {
+ val commWriter = super.newCommWriter(commId)
+
+ generatedCommWriter = commWriter
+
+ val spyCommWriter = spy(commWriter)
+ doNothing().when(spyCommWriter)
+ .sendCommKernelMessage(any[KernelMessageContent with CommContent])
+
+ spyCommWriter
+ }
+ }
+ }
+
+ describe("KernelCommManager") {
+ describe("#open") {
+ it("should return a wrapped instance of KernelCommWriter") {
+ kernelCommManager.open(TestTargetName, v5.MsgData.Empty)
+
+ // Exposed hackishly for testing
+ generatedCommWriter shouldBe a [KernelCommWriter]
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/comm/KernelCommWriterSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/comm/KernelCommWriterSpec.scala b/kernel/src/test/scala/org/apache/toree/comm/KernelCommWriterSpec.scala
new file mode 100644
index 0000000..eb792bb
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/comm/KernelCommWriterSpec.scala
@@ -0,0 +1,270 @@
+/*
+ * Copyright 2014 IBM Corp.
+ *
+ * Licensed 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 com.ibm.spark.comm
+
+import java.util.UUID
+import com.ibm.spark.kernel.protocol.v5._
+import com.ibm.spark.kernel.protocol.v5.content._
+import com.ibm.spark.kernel.protocol.v5.kernel.ActorLoader
+import play.api.libs.json.Json
+import scala.concurrent.duration._
+
+import akka.actor.{ActorSelection, ActorSystem}
+import akka.testkit.{TestProbe, TestKit}
+import com.typesafe.config.ConfigFactory
+import org.scalatest.mock.MockitoSugar
+import org.scalatest.{BeforeAndAfter, FunSpecLike, Matchers}
+import org.mockito.Mockito._
+import org.mockito.Matchers._
+
+object KernelCommWriterSpec {
+ val config ="""
+ akka {
+ loglevel = "WARNING"
+ }"""
+}
+
+class KernelCommWriterSpec extends TestKit(
+ ActorSystem("KernelCommWriterSpec",
+ ConfigFactory.parseString(KernelCommWriterSpec.config))
+) with FunSpecLike with Matchers with BeforeAndAfter with MockitoSugar
+{
+
+ private val commId = UUID.randomUUID().toString
+ private var kernelCommWriter: KernelCommWriter = _
+ private var kernelMessageBuilder: KMBuilder = _
+
+ private var actorLoader: ActorLoader = _
+ private var kernelMessageRelayProbe: TestProbe = _
+
+ /**
+ * Retrieves the next message available.
+ *
+ * @return The KernelMessage instance (or an error if timed out)
+ */
+ private def getNextMessage =
+ kernelMessageRelayProbe.receiveOne(200.milliseconds)
+ .asInstanceOf[KernelMessage]
+
+ /**
+ * Retrieves the next message available and returns its type.
+ *
+ * @return The type of the message (pulled from message header)
+ */
+ private def getNextMessageType = getNextMessage.header.msg_type
+
+ /**
+ * Retrieves the next message available and parses the content string.
+ *
+ * @tparam T The type to coerce the content string into
+ *
+ * @return The resulting KernelMessageContent instance
+ */
+ private def getNextMessageContents[T <: KernelMessageContent]
+ (implicit fjs: play.api.libs.json.Reads[T], mf: Manifest[T]) =
+ {
+ val receivedMessage = getNextMessage
+
+ Json.parse(receivedMessage.contentString).as[T]
+ }
+
+ before {
+ kernelMessageBuilder = spy(KMBuilder())
+
+ // Construct path for kernel message relay
+ actorLoader = mock[ActorLoader]
+ kernelMessageRelayProbe = TestProbe()
+ val kernelMessageRelaySelection: ActorSelection =
+ system.actorSelection(kernelMessageRelayProbe.ref.path.toString)
+ doReturn(kernelMessageRelaySelection)
+ .when(actorLoader).load(SystemActorType.KernelMessageRelay)
+
+ // Create a new writer to use for testing
+ kernelCommWriter = new KernelCommWriter(actorLoader, kernelMessageBuilder, commId)
+ }
+
+ describe("KernelCommWriter") {
+ describe("#writeOpen") {
+ it("should send a comm_open message to the relay") {
+ kernelCommWriter.writeOpen(anyString())
+
+ getNextMessageType should be (CommOpen.toTypeString)
+ }
+
+ it("should include the comm_id in the message") {
+ val expected = commId
+ kernelCommWriter.writeOpen(anyString())
+
+ val actual = getNextMessageContents[CommOpen].comm_id
+
+ actual should be (expected)
+ }
+
+ it("should include the target name in the message") {
+ val expected = "<TARGET_NAME>"
+ kernelCommWriter.writeOpen(expected)
+
+ val actual = getNextMessageContents[CommOpen].target_name
+
+ actual should be (expected)
+ }
+
+ it("should provide empty data in the message if no data is provided") {
+ val expected = MsgData.Empty
+ kernelCommWriter.writeOpen(anyString())
+
+ val actual = getNextMessageContents[CommOpen].data
+
+ actual should be (expected)
+ }
+
+ it("should include the data in the message") {
+ val expected = MsgData("some key" -> "some value")
+ kernelCommWriter.writeOpen(anyString(), expected)
+
+ val actual = getNextMessageContents[CommOpen].data
+
+ actual should be (expected)
+ }
+ }
+
+ describe("#writeMsg") {
+ it("should send a comm_msg message to the relay") {
+ kernelCommWriter.writeMsg(MsgData.Empty)
+
+ getNextMessageType should be (CommMsg.toTypeString)
+ }
+
+ it("should include the comm_id in the message") {
+ val expected = commId
+ kernelCommWriter.writeMsg(MsgData.Empty)
+
+ val actual = getNextMessageContents[CommMsg].comm_id
+
+ actual should be (expected)
+ }
+
+ it("should fail a require if the data is null") {
+ intercept[IllegalArgumentException] {
+ kernelCommWriter.writeMsg(null)
+ }
+ }
+
+ it("should include the data in the message") {
+ val expected = MsgData("some key" -> "some value")
+ kernelCommWriter.writeMsg(expected)
+
+ val actual = getNextMessageContents[CommMsg].data
+
+ actual should be (expected)
+ }
+ }
+
+ describe("#writeClose") {
+ it("should send a comm_close message to the relay") {
+ kernelCommWriter.writeClose()
+
+ getNextMessageType should be (CommClose.toTypeString)
+ }
+
+ it("should include the comm_id in the message") {
+ val expected = commId
+ kernelCommWriter.writeClose()
+
+ val actual = getNextMessageContents[CommClose].comm_id
+
+ actual should be (expected)
+ }
+
+ it("should provide empty data in the message if no data is provided") {
+ val expected = MsgData.Empty
+ kernelCommWriter.writeClose()
+
+ val actual = getNextMessageContents[CommClose].data
+
+ actual should be (expected)
+ }
+
+ it("should include the data in the message") {
+ val expected = MsgData("some key" -> "some value")
+ kernelCommWriter.writeClose(expected)
+
+ val actual = getNextMessageContents[CommClose].data
+
+ actual should be (expected)
+ }
+ }
+
+ describe("#write") {
+ it("should send a comm_msg message to the relay") {
+ kernelCommWriter.write(Array('a'), 0, 1)
+
+ getNextMessageType should be (CommMsg.toTypeString)
+ }
+
+ it("should include the comm_id in the message") {
+ val expected = commId
+ kernelCommWriter.write(Array('a'), 0, 1)
+
+ val actual = getNextMessageContents[CommMsg].comm_id
+
+ actual should be (expected)
+ }
+
+ it("should package the string as part of the data with a 'message' key") {
+ val expected = MsgData("message" -> "a")
+ kernelCommWriter.write(Array('a'), 0, 1)
+
+ val actual = getNextMessageContents[CommMsg].data
+
+ actual should be (expected)
+ }
+ }
+
+ describe("#flush") {
+ it("should do nothing") {
+ // TODO: Is this test necessary? It does nothing.
+ kernelCommWriter.flush()
+ }
+ }
+
+ describe("#close") {
+ it("should send a comm_close message to the relay") {
+ kernelCommWriter.close()
+
+ getNextMessageType should be (CommClose.toTypeString)
+ }
+
+ it("should include the comm_id in the message") {
+ val expected = commId
+ kernelCommWriter.close()
+
+ val actual = getNextMessageContents[CommClose].comm_id
+
+ actual should be (expected)
+ }
+
+ it("should provide empty data in the message") {
+ val expected = MsgData.Empty
+ kernelCommWriter.close()
+
+ val actual = getNextMessageContents[CommClose].data
+
+ actual should be (expected)
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/global/ExecutionCounterSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/global/ExecutionCounterSpec.scala b/kernel/src/test/scala/org/apache/toree/global/ExecutionCounterSpec.scala
new file mode 100644
index 0000000..4d1641f
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/global/ExecutionCounterSpec.scala
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2014 IBM Corp.
+ *
+ * Licensed 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 com.ibm.spark.global
+
+import org.scalatest.{FunSpec, Matchers}
+
+class ExecutionCounterSpec extends FunSpec with Matchers {
+ describe("ExecutionCounter") {
+ describe("#increment( String )"){
+ it("should increment value when key is not present"){
+ ExecutionCounter incr "foo" should be(1)
+ }
+ it("should increment value for key when it is present"){
+ ExecutionCounter incr "bar" should be(1)
+ ExecutionCounter incr "bar" should be(2)
+ }
+
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/kernel/api/KernelSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/kernel/api/KernelSpec.scala b/kernel/src/test/scala/org/apache/toree/kernel/api/KernelSpec.scala
new file mode 100644
index 0000000..58ea0c5
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/kernel/api/KernelSpec.scala
@@ -0,0 +1,178 @@
+package com.ibm.spark.kernel.api
+
+import java.io.{InputStream, PrintStream}
+
+import com.ibm.spark.boot.layer.InterpreterManager
+import com.ibm.spark.comm.CommManager
+import com.ibm.spark.interpreter._
+import com.ibm.spark.kernel.protocol.v5._
+import com.ibm.spark.kernel.protocol.v5.kernel.ActorLoader
+import com.ibm.spark.magic.MagicLoader
+import com.typesafe.config.Config
+import org.apache.spark.{SparkConf, SparkContext}
+import org.mockito.Mockito._
+import org.mockito.Matchers._
+import org.scalatest.mock.MockitoSugar
+import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
+import com.ibm.spark.global.ExecuteRequestState
+
+class KernelSpec extends FunSpec with Matchers with MockitoSugar
+ with BeforeAndAfter
+{
+ private val BadCode = Some("abc foo bar")
+ private val GoodCode = Some("val foo = 1")
+ private val ErrorCode = Some("val foo = bar")
+ private val ErrorMsg = "Name: error\n" +
+ "Message: bad\n" +
+ "StackTrace: 1"
+
+ private var mockConfig: Config = _
+ private var mockSparkContext: SparkContext = _
+ private var mockSparkConf: SparkConf = _
+ private var mockActorLoader: ActorLoader = _
+ private var mockInterpreter: Interpreter = _
+ private var mockInterpreterManager: InterpreterManager = _
+ private var mockCommManager: CommManager = _
+ private var mockMagicLoader: MagicLoader = _
+ private var kernel: Kernel = _
+ private var spyKernel: Kernel = _
+
+ before {
+ mockConfig = mock[Config]
+ mockInterpreter = mock[Interpreter]
+ mockInterpreterManager = mock[InterpreterManager]
+ mockSparkContext = mock[SparkContext]
+ mockSparkConf = mock[SparkConf]
+ when(mockInterpreterManager.defaultInterpreter)
+ .thenReturn(Some(mockInterpreter))
+ when(mockInterpreterManager.interpreters)
+ .thenReturn(Map[String, com.ibm.spark.interpreter.Interpreter]())
+ when(mockInterpreter.interpret(BadCode.get))
+ .thenReturn((Results.Incomplete, null))
+ when(mockInterpreter.interpret(GoodCode.get))
+ .thenReturn((Results.Success, Left(new ExecuteOutput("ok"))))
+ when(mockInterpreter.interpret(ErrorCode.get))
+ .thenReturn((Results.Error, Right(ExecuteError("error","bad", List("1")))))
+
+
+ mockCommManager = mock[CommManager]
+ mockActorLoader = mock[ActorLoader]
+ mockMagicLoader = mock[MagicLoader]
+
+ kernel = new Kernel(
+ mockConfig, mockActorLoader, mockInterpreterManager, mockCommManager,
+ mockMagicLoader
+ )
+
+ spyKernel = spy(kernel)
+
+ }
+
+ after {
+ ExecuteRequestState.reset()
+ }
+
+ describe("Kernel") {
+ describe("#eval") {
+ it("should return syntax error") {
+ kernel eval BadCode should be((false, "Syntax Error!"))
+ }
+
+ it("should return ok") {
+ kernel eval GoodCode should be((true, "ok"))
+ }
+
+ it("should return error") {
+ kernel eval ErrorCode should be((false, ErrorMsg))
+ }
+
+ it("should return error on None") {
+ kernel eval None should be ((false, "Error!"))
+ }
+ }
+
+ describe("#out") {
+ it("should throw an exception if the ExecuteRequestState has not been set") {
+ intercept[IllegalArgumentException] {
+ kernel.out
+ }
+ }
+
+ it("should create a new PrintStream instance if the ExecuteRequestState has been set") {
+ ExecuteRequestState.processIncomingKernelMessage(
+ new KernelMessage(Nil, "", mock[Header], mock[ParentHeader],
+ mock[Metadata], "")
+ )
+ kernel.out shouldBe a [PrintStream]
+ }
+ }
+
+ describe("#err") {
+ it("should throw an exception if the ExecuteRequestState has not been set") {
+ intercept[IllegalArgumentException] {
+ kernel.err
+ }
+ }
+
+ it("should create a new PrintStream instance if the ExecuteRequestState has been set") {
+ ExecuteRequestState.processIncomingKernelMessage(
+ new KernelMessage(Nil, "", mock[Header], mock[ParentHeader],
+ mock[Metadata], "")
+ )
+
+ // TODO: Access the underlying streamType field to assert stderr?
+ kernel.err shouldBe a [PrintStream]
+ }
+ }
+
+ describe("#in") {
+ it("should throw an exception if the ExecuteRequestState has not been set") {
+ intercept[IllegalArgumentException] {
+ kernel.in
+ }
+ }
+
+ it("should create a new InputStream instance if the ExecuteRequestState has been set") {
+ ExecuteRequestState.processIncomingKernelMessage(
+ new KernelMessage(Nil, "", mock[Header], mock[ParentHeader],
+ mock[Metadata], "")
+ )
+
+ kernel.in shouldBe a [InputStream]
+ }
+ }
+
+ describe("#stream") {
+ it("should throw an exception if the ExecuteRequestState has not been set") {
+ intercept[IllegalArgumentException] {
+ kernel.stream
+ }
+ }
+
+ it("should create a StreamMethods instance if the ExecuteRequestState has been set") {
+ ExecuteRequestState.processIncomingKernelMessage(
+ new KernelMessage(Nil, "", mock[Header], mock[ParentHeader],
+ mock[Metadata], "")
+ )
+
+ kernel.stream shouldBe a [StreamMethods]
+ }
+ }
+
+ describe("when spark.master is set in config") {
+
+ it("should create SparkConf") {
+ val expected = "some value"
+ doReturn(expected).when(mockConfig).getString("spark.master")
+ doReturn("").when(mockConfig).getString("spark_configuration")
+
+ // Provide stub for interpreter classServerURI since also executed
+ doReturn("").when(mockInterpreter).classServerURI
+
+ val sparkConf = kernel.createSparkConf(new SparkConf().setMaster(expected))
+
+ sparkConf.get("spark.master") should be (expected)
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/kernel/api/StreamMethodsSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/kernel/api/StreamMethodsSpec.scala b/kernel/src/test/scala/org/apache/toree/kernel/api/StreamMethodsSpec.scala
new file mode 100644
index 0000000..fc87588
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/kernel/api/StreamMethodsSpec.scala
@@ -0,0 +1,70 @@
+package com.ibm.spark.kernel.api
+
+import akka.actor.ActorSystem
+import akka.testkit.{ImplicitSender, TestKit, TestProbe}
+import com.ibm.spark.kernel.protocol.v5
+import com.ibm.spark.kernel.protocol.v5.KernelMessage
+import org.scalatest.mock.MockitoSugar
+import org.scalatest.{FunSpecLike, BeforeAndAfter, Matchers, FunSpec}
+import play.api.libs.json.Json
+
+import scala.concurrent.duration._
+
+import org.mockito.Mockito._
+
+class StreamMethodsSpec extends TestKit(
+ ActorSystem("StreamMethodsSpec")
+) with ImplicitSender with FunSpecLike with Matchers with MockitoSugar
+ with BeforeAndAfter
+{
+ private val MaxDuration = 300.milliseconds
+
+ private var kernelMessageRelayProbe: TestProbe = _
+ private var mockParentHeader: v5.ParentHeader = _
+ private var mockActorLoader: v5.kernel.ActorLoader = _
+ private var mockKernelMessage: v5.KernelMessage = _
+ private var streamMethods: StreamMethods = _
+
+ before {
+ kernelMessageRelayProbe = TestProbe()
+
+ mockParentHeader = mock[v5.ParentHeader]
+
+ mockActorLoader = mock[v5.kernel.ActorLoader]
+ doReturn(system.actorSelection(kernelMessageRelayProbe.ref.path))
+ .when(mockActorLoader).load(v5.SystemActorType.KernelMessageRelay)
+
+ mockKernelMessage = mock[v5.KernelMessage]
+ doReturn(mockParentHeader).when(mockKernelMessage).header
+
+ streamMethods = new StreamMethods(mockActorLoader, mockKernelMessage)
+ }
+
+ describe("StreamMethods") {
+ describe("#()") {
+ it("should put the header of the given message as the parent header") {
+ val expected = mockKernelMessage.header
+ val actual = streamMethods.kmBuilder.build.parentHeader
+
+ actual should be (expected)
+ }
+ }
+
+ describe("#sendAll") {
+ it("should send a message containing all of the given text") {
+ val expected = "some text"
+
+ streamMethods.sendAll(expected)
+
+ val outgoingMessage = kernelMessageRelayProbe.receiveOne(MaxDuration)
+ val kernelMessage = outgoingMessage.asInstanceOf[KernelMessage]
+
+ val actual = Json.parse(kernelMessage.contentString)
+ .as[v5.content.StreamContent].text
+
+ actual should be (expected)
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/dispatch/StatusDispatchSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/dispatch/StatusDispatchSpec.scala b/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/dispatch/StatusDispatchSpec.scala
new file mode 100644
index 0000000..60d3b42
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/dispatch/StatusDispatchSpec.scala
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2014 IBM Corp.
+ *
+ * Licensed 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 com.ibm.spark.kernel.protocol.v5.dispatch
+
+import akka.actor.{ActorRef, ActorSystem, Props}
+import akka.testkit.{TestKit, TestProbe}
+import com.ibm.spark.kernel.protocol.v5._
+import com.ibm.spark.kernel.protocol.v5.content.KernelStatus
+import com.ibm.spark.kernel.protocol.v5.kernel.ActorLoader
+import org.mockito.Mockito._
+import org.scalatest.mock.MockitoSugar
+import org.scalatest.{BeforeAndAfter, FunSpecLike, Matchers}
+import play.api.libs.json.Json
+
+import scala.concurrent.duration._
+
+class StatusDispatchSpec extends TestKit(ActorSystem("StatusDispatchSystem"))
+with FunSpecLike with Matchers with MockitoSugar with BeforeAndAfter{
+ var statusDispatchRef: ActorRef = _
+ var relayProbe: TestProbe = _
+ before {
+ // Mock the relay with a probe
+ relayProbe = TestProbe()
+ // Mock the ActorLoader
+ val mockActorLoader: ActorLoader = mock[ActorLoader]
+ when(mockActorLoader.load(SystemActorType.KernelMessageRelay))
+ .thenReturn(system.actorSelection(relayProbe.ref.path.toString))
+
+ statusDispatchRef = system.actorOf(Props(classOf[StatusDispatch],mockActorLoader))
+ }
+
+
+ describe("StatusDispatch") {
+ describe("#receive( KernelStatusType )") {
+ it("should send a status message to the relay") {
+ statusDispatchRef ! KernelStatusType.Busy
+ // Check the kernel message is the correct type
+ val statusMessage: KernelMessage = relayProbe.receiveOne(500.milliseconds).asInstanceOf[KernelMessage]
+ statusMessage.header.msg_type should be (MessageType.Outgoing.Status.toString)
+ // Check the status is what we sent
+ val status: KernelStatus = Json.parse(statusMessage.contentString).as[KernelStatus]
+ status.execution_state should be (KernelStatusType.Busy.toString)
+ }
+ }
+
+ describe("#receive( KernelStatusType, Header )") {
+ it("should send a status message to the relay") {
+ val tuple = Tuple2(KernelStatusType.Busy, mock[Header])
+ statusDispatchRef ! tuple
+ // Check the kernel message is the correct type
+ val statusMessage: KernelMessage = relayProbe.receiveOne(500.milliseconds).asInstanceOf[KernelMessage]
+ statusMessage.header.msg_type should be (MessageType.Outgoing.Status.toString)
+ // Check the status is what we sent
+ val status: KernelStatus = Json.parse(statusMessage.contentString).as[KernelStatus]
+ status.execution_state should be (KernelStatusType.Busy.toString)
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/68f7ddd6/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/handler/CodeCompleteHandlerSpec.scala
----------------------------------------------------------------------
diff --git a/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/handler/CodeCompleteHandlerSpec.scala b/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/handler/CodeCompleteHandlerSpec.scala
new file mode 100644
index 0000000..b44ad1c
--- /dev/null
+++ b/kernel/src/test/scala/org/apache/toree/kernel/protocol/v5/handler/CodeCompleteHandlerSpec.scala
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2014 IBM Corp.
+ *
+ * Licensed 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 com.ibm.spark.kernel.protocol.v5.handler
+
+import akka.actor._
+import akka.testkit.{TestProbe, ImplicitSender, TestKit}
+import com.ibm.spark.kernel.protocol.v5._
+import com.ibm.spark.kernel.protocol.v5.content.CompleteRequest
+import com.ibm.spark.kernel.protocol.v5.kernel.ActorLoader
+import com.ibm.spark.kernel.protocol.v5Test._
+import org.scalatest.mock.MockitoSugar
+import org.scalatest.{FunSpecLike, BeforeAndAfter, Matchers}
+import org.mockito.Mockito._
+import scala.concurrent.duration._
+
+class CodeCompleteHandlerSpec extends TestKit(
+ ActorSystem("CodeCompleteHandlerSpec")
+) with ImplicitSender with FunSpecLike with Matchers with MockitoSugar
+ with BeforeAndAfter {
+
+ var actorLoader: ActorLoader = _
+ var handlerActor: ActorRef = _
+ var kernelMessageRelayProbe: TestProbe = _
+ var interpreterProbe: TestProbe = _
+ var statusDispatchProbe: TestProbe = _
+
+ before {
+ actorLoader = mock[ActorLoader]
+
+ handlerActor = system.actorOf(Props(classOf[CodeCompleteHandler], actorLoader))
+
+ kernelMessageRelayProbe = TestProbe()
+ when(actorLoader.load(SystemActorType.KernelMessageRelay))
+ .thenReturn(system.actorSelection(kernelMessageRelayProbe.ref.path.toString))
+
+ interpreterProbe = new TestProbe(system)
+ when(actorLoader.load(SystemActorType.Interpreter))
+ .thenReturn(system.actorSelection(interpreterProbe.ref.path.toString))
+
+ statusDispatchProbe = new TestProbe(system)
+ when(actorLoader.load(SystemActorType.StatusDispatch))
+ .thenReturn(system.actorSelection(statusDispatchProbe.ref.path.toString))
+ }
+
+ def replyToHandlerWithOkAndResult() = {
+ val expectedClass = classOf[CompleteRequest]
+ interpreterProbe.expectMsgClass(expectedClass)
+ interpreterProbe.reply((0, List[String]()))
+ }
+
+ def replyToHandlerWithOkAndBadResult() = {
+ val expectedClass = classOf[CompleteRequest]
+ interpreterProbe.expectMsgClass(expectedClass)
+ interpreterProbe.reply("hello")
+ }
+
+ describe("CodeCompleteHandler (ActorLoader)") {
+ it("should send a CompleteRequest") {
+ handlerActor ! MockCompleteRequestKernelMessage
+ replyToHandlerWithOkAndResult()
+ kernelMessageRelayProbe.fishForMessage(500.milliseconds) {
+ case KernelMessage(_, _, header, _, _, _) =>
+ header.msg_type == MessageType.Outgoing.CompleteReply.toString
+ }
+ }
+
+ it("should throw an error for bad JSON") {
+ handlerActor ! MockKernelMessageWithBadJSON
+ var result = false
+ try {
+ replyToHandlerWithOkAndResult()
+ }
+ catch {
+ case t: Throwable => result = true
+ }
+ result should be (true)
+ }
+
+ it("should throw an error for bad code completion") {
+ handlerActor ! MockCompleteRequestKernelMessage
+ try {
+ replyToHandlerWithOkAndBadResult()
+ }
+ catch {
+ case error: Exception => error.getMessage should be ("Parse error in CodeCompleteHandler")
+ }
+ }
+
+ it("should send an idle message") {
+ handlerActor ! MockCompleteRequestKernelMessage
+ replyToHandlerWithOkAndResult()
+ statusDispatchProbe.fishForMessage(500.milliseconds) {
+ case Tuple2(status, _) =>
+ status == KernelStatusType.Idle
+ }
+ }
+ }
+}