2011-06-07 06:36:21 +05:30
|
|
|
/**
|
2011-07-14 16:03:08 +02:00
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
2011-06-07 06:36:21 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.serialization
|
|
|
|
|
|
|
|
|
|
import akka.serialization.Serialization._
|
|
|
|
|
import scala.reflect._
|
2011-10-11 16:05:48 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
2011-11-10 20:08:00 +01:00
|
|
|
import akka.actor.ActorSystem
|
2011-11-01 11:20:02 +01:00
|
|
|
import java.io.{ ObjectInputStream, ByteArrayInputStream, ByteArrayOutputStream, ObjectOutputStream }
|
|
|
|
|
import akka.actor.DeadLetterActorRef
|
2011-06-07 06:36:21 +05:30
|
|
|
|
|
|
|
|
object SerializeSpec {
|
|
|
|
|
@BeanInfo
|
|
|
|
|
case class Address(no: String, street: String, city: String, zip: String) { def this() = this("", "", "", "") }
|
|
|
|
|
@BeanInfo
|
|
|
|
|
case class Person(name: String, age: Int, address: Address) { def this() = this("", 0, null) }
|
|
|
|
|
|
|
|
|
|
case class Record(id: Int, person: Person)
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-21 17:01:22 +02:00
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
2011-10-11 16:05:48 +02:00
|
|
|
class SerializeSpec extends AkkaSpec {
|
2011-06-07 06:36:21 +05:30
|
|
|
import SerializeSpec._
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
import app.serialization._
|
2011-06-07 06:36:21 +05:30
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
"Serialization" must {
|
|
|
|
|
|
|
|
|
|
"serialize Address" in {
|
|
|
|
|
val addr = Address("120", "Monroe Street", "Santa Clara", "95050")
|
|
|
|
|
val b = serialize(addr) match {
|
|
|
|
|
case Left(exception) ⇒ fail(exception)
|
|
|
|
|
case Right(bytes) ⇒ bytes
|
|
|
|
|
}
|
|
|
|
|
deserialize(b.asInstanceOf[Array[Byte]], classOf[Address], None) match {
|
|
|
|
|
case Left(exception) ⇒ fail(exception)
|
|
|
|
|
case Right(add) ⇒ assert(add === addr)
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
"serialize Person" in {
|
|
|
|
|
val person = Person("debasish ghosh", 25, Address("120", "Monroe Street", "Santa Clara", "95050"))
|
|
|
|
|
val b = serialize(person) match {
|
|
|
|
|
case Left(exception) ⇒ fail(exception)
|
|
|
|
|
case Right(bytes) ⇒ bytes
|
|
|
|
|
}
|
|
|
|
|
deserialize(b.asInstanceOf[Array[Byte]], classOf[Person], None) match {
|
|
|
|
|
case Left(exception) ⇒ fail(exception)
|
|
|
|
|
case Right(p) ⇒ assert(p === person)
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
|
|
|
|
|
"serialize record with default serializer" in {
|
|
|
|
|
val person = Person("debasish ghosh", 25, Address("120", "Monroe Street", "Santa Clara", "95050"))
|
|
|
|
|
val r = Record(100, person)
|
|
|
|
|
val b = serialize(r) match {
|
|
|
|
|
case Left(exception) ⇒ fail(exception)
|
|
|
|
|
case Right(bytes) ⇒ bytes
|
|
|
|
|
}
|
|
|
|
|
deserialize(b.asInstanceOf[Array[Byte]], classOf[Record], None) match {
|
|
|
|
|
case Left(exception) ⇒ fail(exception)
|
|
|
|
|
case Right(p) ⇒ assert(p === r)
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
2011-11-01 11:20:02 +01:00
|
|
|
|
|
|
|
|
"serialize DeadLetterActorRef" in {
|
|
|
|
|
val outbuf = new ByteArrayOutputStream()
|
|
|
|
|
val out = new ObjectOutputStream(outbuf)
|
2011-11-10 20:08:00 +01:00
|
|
|
val a = new ActorSystem()
|
2011-11-01 11:20:02 +01:00
|
|
|
out.writeObject(a.deadLetters)
|
|
|
|
|
out.flush()
|
|
|
|
|
out.close()
|
|
|
|
|
|
|
|
|
|
val in = new ObjectInputStream(new ByteArrayInputStream(outbuf.toByteArray))
|
|
|
|
|
Serialization.app.withValue(a) {
|
|
|
|
|
val deadLetters = in.readObject().asInstanceOf[DeadLetterActorRef]
|
2011-11-01 11:31:53 +01:00
|
|
|
(deadLetters eq a.deadLetters) must be(true)
|
2011-11-01 11:20:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
}
|