pekko/akka-spring/src/test/scala/SupervisionBeanDefinitionParserTest.scala

133 lines
5.6 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka.spring
import org.scalatest.Spec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import ScalaDom._
import se.scalablesolutions.akka.config.JavaConfig._
import org.w3c.dom.Element
import org.springframework.beans.factory.support.BeanDefinitionBuilder
/**
* Test for SupervisionBeanDefinitionParser
* @author michaelkober
*/
@RunWith(classOf[JUnitRunner])
class SupervisionBeanDefinitionParserTest extends Spec with ShouldMatchers {
private class Parser extends SupervisionBeanDefinitionParser
describe("A SupervisionBeanDefinitionParser") {
val parser = new Parser()
val builder = BeanDefinitionBuilder.genericBeanDefinition("foo.bar.Foo")
it("should be able to parse typed actor configuration") {
2010-08-12 15:54:46 +02:00
val props = parser.parseActor(createTypedActorElement);
assert(props ne null)
assert(props.timeout == 1000)
assert(props.target == "foo.bar.MyPojo")
assert(props.transactional)
}
it("should parse the supervisor restart strategy") {
parser.parseSupervisor(createSupervisorElement, builder);
val strategy = builder.getBeanDefinition.getPropertyValues.getPropertyValue("restartStrategy").getValue.asInstanceOf[RestartStrategy]
assert(strategy ne null)
assert(strategy.scheme match {
case x:AllForOne => true
case _ => false })
expect(3) { strategy.maxNrOfRetries }
expect(1000) { strategy.withinTimeRange }
}
it("should parse the supervised typed actors") {
parser.parseSupervisor(createSupervisorElement, builder);
2010-08-12 15:54:46 +02:00
val supervised = builder.getBeanDefinition.getPropertyValues.getPropertyValue("supervised").getValue.asInstanceOf[List[ActorProperties]]
assert(supervised ne null)
expect(4) { supervised.length }
2010-04-15 16:05:16 +02:00
val iterator = supervised.iterator
val prop1 = iterator.next
val prop2 = iterator.next
val prop3 = iterator.next
val prop4 = iterator.next
expect("foo.bar.Foo") { prop1.target }
expect("foo.bar.Bar") { prop2.target }
expect("foo.bar.MyPojo") { prop3.target }
expect("foo.bar.MyPojo") { prop4.target }
expect("permanent") { prop1.lifecycle }
expect("temporary") { prop4.lifecycle }
}
it("should throw IllegalArgumentException on missing mandatory attributes") {
evaluating { parser.parseSupervisor(createSupervisorMissingAttribute, builder) } should produce [IllegalArgumentException]
}
it("should throw IllegalArgumentException on missing mandatory elements") {
evaluating { parser.parseSupervisor(createSupervisorMissingElement, builder) } should produce [IllegalArgumentException]
}
}
private def createTypedActorElement : Element = {
val xml = <akka:typed-actor id="typed-actor1"
implementation="foo.bar.MyPojo"
2010-03-20 10:56:46 +01:00
timeout="1000"
transactional="true"/>
dom(xml).getDocumentElement
}
private def createSupervisorElement : Element = {
val xml = <akka:supervision id="supervision1">
2010-03-20 10:56:46 +01:00
<akka:restart-strategy failover="AllForOne" retries="3" timerange="1000">
<akka:trap-exits>
<akka:trap-exit>java.io.IOException</akka:trap-exit>
<akka:trap-exit>java.lang.NullPointerException</akka:trap-exit>
</akka:trap-exits>
</akka:restart-strategy>
<akka:typed-actors>
<akka:typed-actor implementation="foo.bar.Foo" lifecycle="permanent" timeout="1000"/>
<akka:typed-actor interface="foo.bar.IBar" implementation="foo.bar.Bar" lifecycle="permanent" timeout="1000"/>
<akka:typed-actor implementation="foo.bar.MyPojo" lifecycle="temporary" timeout="1000">
2010-03-20 10:56:46 +01:00
<akka:restart-callbacks pre="preRestart" post="postRestart"/>
</akka:typed-actor>
<akka:typed-actor implementation="foo.bar.MyPojo" lifecycle="temporary" timeout="1000">
<akka:shutdown-callback method="shutdown"/>
</akka:typed-actor>
</akka:typed-actors>
</akka:supervision>
dom(xml).getDocumentElement
}
private def createSupervisorMissingAttribute : Element = {
val xml = <akka:supervision id="supervision1">
2010-03-20 10:56:46 +01:00
<akka:restart-strategy failover="AllForOne" retries="3">
<akka:trap-exits>
<akka:trap-exit>java.io.IOException</akka:trap-exit>
</akka:trap-exits>
</akka:restart-strategy>
<akka:typed-actors>
<akka:typed-actor implementation="foo.bar.Foo" lifecycle="permanent" timeout="1000"/>
</akka:typed-actors>
</akka:supervision>
dom(xml).getDocumentElement
}
private def createSupervisorMissingElement : Element = {
val xml = <akka:supervision id="supervision1">
2010-03-20 10:56:46 +01:00
<akka:restart-strategy failover="AllForOne" retries="3" timerange="1000">
</akka:restart-strategy>
<akka:typed-actors>
<akka:typed-actor implementation="foo.bar.Foo" lifecycle="permanent" timeout="1000"/>
<akka:typed-actor interface="foo.bar.IBar" implementation="foo.bar.Bar" lifecycle="permanent" timeout="1000"/>
</akka:typed-actors>
</akka:supervision>
dom(xml).getDocumentElement
}
}