Created
June 29, 2012 07:35
-
-
Save yamashiro/3016492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package study | |
import org.specs2.mutable.Specification | |
import scalaz.Identity | |
import org.sisioh.dddbase.core.Entity | |
import java.io._ | |
class Hoge extends Specification { | |
"Serial " should { | |
"aaa" in { | |
val bar = new Bar(Identity(1), "aaa") | |
val bos = new ByteArrayOutputStream() | |
val oos = new ObjectOutputStream(bos) | |
oos.writeObject(bar) | |
val bis = new ByteArrayInputStream(bos.toByteArray) | |
val ois = new ObjectInputStream(bis) | |
val readBar = ois.readObject().asInstanceOf[Bar] | |
readBar.identity must_== Identity(1) | |
readBar.str must_== "aaa" | |
} | |
} | |
} | |
class Bar(@transient val identity:Identity[Int], val str:String) extends Entity[Int] with Serializable { | |
@throws(classOf[IOException]) | |
private def writeObject(stream:java.io.ObjectOutputStream) : Unit = { | |
stream.defaultWriteObject() | |
stream.writeInt(identity.value) | |
} | |
@throws(classOf[IOException]) | |
private def readObject(stream:java.io.ObjectInputStream) : Unit = { | |
stream.defaultReadObject() | |
val hoge = stream.readInt() | |
val field = getClass.getDeclaredField("identity") | |
field.setAccessible(true) | |
println("hoge [" + hoge + "]") | |
field.set(this, Identity(hoge)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment