2011-10-10 11:12:34 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.testkit
|
|
|
|
|
|
2011-10-12 11:35:41 +02:00
|
|
|
import akka.config.Configuration
|
2011-10-20 20:45:02 +02:00
|
|
|
import org.scalatest.{ WordSpec, BeforeAndAfterAll }
|
2011-10-10 11:12:34 +02:00
|
|
|
import org.scalatest.matchers.MustMatchers
|
|
|
|
|
import akka.AkkaApplication
|
2011-10-10 15:45:55 +02:00
|
|
|
import akka.actor.{ Actor, ActorRef, Props }
|
2011-10-11 16:05:48 +02:00
|
|
|
import akka.dispatch.MessageDispatcher
|
2011-10-10 11:12:34 +02:00
|
|
|
|
|
|
|
|
abstract class AkkaSpec(_application: AkkaApplication = AkkaApplication())
|
2011-10-20 20:45:02 +02:00
|
|
|
extends TestKit(_application) with WordSpec with MustMatchers with BeforeAndAfterAll {
|
|
|
|
|
|
|
|
|
|
final override def beforeAll {
|
|
|
|
|
atStartup()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final override def afterAll {
|
|
|
|
|
app.stop()
|
|
|
|
|
atTermination()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected def atStartup() {}
|
|
|
|
|
|
|
|
|
|
protected def atTermination() {}
|
2011-10-10 11:12:34 +02:00
|
|
|
|
2011-10-12 11:35:41 +02:00
|
|
|
def this(config: Configuration) = this(new AkkaApplication(getClass.getSimpleName, AkkaApplication.defaultConfig ++ config))
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf(props: Props): ActorRef = app.actorOf(props)
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf[T <: Actor](clazz: Class[T]): ActorRef = actorOf(Props(clazz))
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf[T <: Actor: Manifest]: ActorRef = actorOf(manifest[T].erasure.asInstanceOf[Class[_ <: Actor]])
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
def actorOf[T <: Actor](factory: ⇒ T): ActorRef = actorOf(Props(factory))
|
2011-10-10 15:45:55 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
def spawn(body: ⇒ Unit)(implicit dispatcher: MessageDispatcher) {
|
2011-10-18 17:56:23 +02:00
|
|
|
actorOf(Props(ctx ⇒ { case "go" ⇒ try body finally ctx.self.stop() }).withDispatcher(dispatcher)) ! "go"
|
2011-10-11 16:05:48 +02:00
|
|
|
}
|
2011-10-10 11:12:34 +02:00
|
|
|
}
|