Geeks With Blogs

News
Geekus Con Livus Malcolm Anderson's home for Geeks With Lives

I don't know about you, but I have an attention span that is about 20 minutes long.

If I can get your sample working, doing something (anything) in the first 20 minutes, I may be playing with it when the sun comes up.

You just have to capture my attention QUICKLY.

 

In that light, I wanted to post a utility that I wrote to make unit testing mo-bedda (read : easier)

This consists of the class, and the unit tests to exercise the class.  If all goes correctly, you should be able to copy these into new classes, set your references and go.  If you already have testdriven.net installed on your machine, this should take you 3 minutes to get running, 7 tops.

 

How you arrange these two classes is up to you, however,  I did it this way

SerializeTest (D)

SerializeTest.sln

Serializer (D)

Serializer.vb

Serializer_UnitTests (D)

Serializer_UnitTests.vb

 

Let me know if you have any problems with this, I'm assuming that you have NUnit installed on your machine, and you know how to set your references up.

 

Final Disclaimer: I'm passionate about unit testing and agile development in general, mix in the fact that I'm still learning tons.  This occationally has me spouting off loudly about something, only to find that I'm completely wrong.  If you see something glaring in this code, or my approach .... please tell me, it's the only way I'm going to improve.

 

Serializer.vb

Option Strict On

Public Class Serializer

Public Sub saveObjectToBinary(ByRef pThingy As Object, ByVal pPath As String)

Dim myFileStream As System.IO.Stream = System.IO.File.Create(pPath)

Dim mySerializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

mySerializer.Serialize(myFileStream, pThingy)

myFileStream.Close()

End Sub

Public Function loadObjectFromBinary(ByVal path As String) As Object

Dim newThingy As Object

Dim myFileStream As System.IO.Stream = System.IO.File.OpenRead(path)

Dim mySerializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

newThingy = mySerializer.Deserialize(myFileStream)

myFileStream.Close()

Return newThingy

End Function

End Class

 

Serializer_UnitTests.vb

Option Strict On

Imports NUnit.Framework

Imports System.IO

Imports System

 

'used for testing serializer

<Serializable()> Public Class Thingy

Public myString As String

Public myInt As Integer

End Class

 

<TestFixture()> Public Class SerializeTest_UnitTests

Dim filePath As String

Dim mFileExists As Boolean

Dim file As file

Dim myThingy As Thingy

Dim o As Serializer ' o is always the object under test

<SetUp()> Public Sub setup()

myThingy = New Thingy

o = New Serializer

End Sub

 

Public Sub DeleteFile(ByVal filePath As String)

Dim file As File

If file.Exists(filePath) Then

file.SetAttributes(filePath, FileAttributes.ReadOnly.Normal)

file.Delete(filePath)

End If

End Sub

<Test()> Public Sub Binary_SomethingSaves()

filePath = "Binary_SomethingSaves.thingy"

DeleteFile(filePath)

Assert.AreEqual(False, file.Exists(filePath), "Precondition: file should not exist")

 

myThingy.myInt = 5

myThingy.myString = "hello"

o.saveObjectToBinary(DirectCast(myThingy, Object), filePath)

 

Assert.AreEqual(True, file.Exists(filePath), "Postcondition: file should exist")

End Sub

 

<Test()> Public Sub Binary_SaveAndLoad()

filePath = "BinaryObject_SaveAndLoad.thingy"

DeleteFile(filePath)

Assert.AreEqual(False, file.Exists(filePath), "Precondition: file should not exist")

 

myThingy.myInt = 5

myThingy.myString = "hello"

o.saveObjectToBinary(DirectCast(myThingy, Object), filePath)

Assert.AreEqual(True, file.Exists(filePath), "Postcondition: file should exist")

 

'Change the data to double check that we are working on 2 copies of thingy

myThingy.myInt = 23

myThingy.myString = "world"

 

Dim myNewThingy As Thingy

myNewThingy = DirectCast(o.loadObjectFromBinary(filePath), Thingy)

Assert.AreEqual(5, myNewThingy.myInt, "newThingy myInt did not load correctly")

Assert.AreEqual("hello", myNewThingy.myString, "newThingy myString did not load correctly")

Assert.AreEqual(23, myThingy.myInt, "myThingy myInt should not have been modified")

Assert.AreEqual("world", myThingy.myString, "myThingy myString should not have been modified")

End Sub

End Class

 

One last gotcha, if you are using 2 different classes, make sure that both projects have the same root namespace.

Posted on Thursday, July 20, 2006 12:35 PM Agile Development , Con Livus , Stupid Code Tricks | Back to top


Comments on this post: Object serialization in VS 2003

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Malcolm Anderson | Powered by: GeeksWithBlogs.net