Testing the Oracle Coherence IPortableObject implementation in .NET.

To get good testing coverage, I want to test the methods WriteExternal

and ReadExternal

IPortableObject

(as described in Creating an IPortableObject Implementation (.NET) ).

Here is some code similar to what I have in my test method (which I like and it works).

Person person = new Person { Name = "John Doe" };
Person personCopy = CoherenceTestHelper.CopyUsingPofSerialization<Person>(person);
Assert.AreEqual(person, personCopy);

      

But to get this to work I had a subclass PofStreamWriter

to override BeginProperty

and PofStreamReader

to override AdvanceTo

. It smells a little to me, but I couldn't find a better solution. Below is my actual code for my CoherenceTestHelper.

Question: Has anyone else tested their implementation IPortableObject

better?

public static class CoherenceTestHelper
{
    public static T CopyUsingPofSerialization<T>(T ipoIn) where T : IPortableObject, new()
    {
        T ipoOut = new T();

        IPofContext context = new SimplePofContext();
        using (MemoryStream ms = new MemoryStream())
        {
        IPofWriter writer = new MyPofStreamWriter(new DataWriter(ms), context);
        ipoIn.WriteExternal(writer);

        ms.Seek(0, 0);
        IPofReader reader = new MyPofStreamReader(new DataReader(ms), context);
        ipoOut.ReadExternal(reader);
        }

        return ipoOut;
    }

    private class MyPofStreamWriter : PofStreamWriter
    {
        public MyPofStreamWriter(DataWriter writer, IPofContext context)
        : base(writer, context)
        {
        }

        protected override void BeginProperty(int index)
        {
        }
    }

    private class MyPofStreamReader : PofStreamReader
    {
        public MyPofStreamReader(DataReader reader, IPofContext context)
        : base(reader, context)
        {
        }

        protected override bool AdvanceTo(int index)
        {
        return true;
        }
    }
}

      

0


a source to share


2 answers


This test is too worried about the actual execution of IPofReader

and IPofWriter

. I suggest you create a reader / writer mock using a framework like Rhino Mocks . Pass your POF method to the mocked reader / writer. Then just assert that the read / write methods were called with the correct parameters.



0


a source


I was looking into how to unit test IPofSerializer and I expect it to be similar to your problem. I found that you need to call PofStreamWriter.WriteObject and PofStreamReader.ReadObject to properly serialize and deserialize the user type. The user type must also be registered with the PofContext to work properly.

The reason for this is that the PofStreamWriter class contains a nested subclass to which it breaks down when serializing user types. This subclass writes additional information about the type, which then uses the PofStreamReader to deserialize the type correctly. Without this additional data, the PofStreamReader cannot determine which type and which serializer should be used. It looks like it is simply not configured to allow testing of the serializer in complete isolation, as there are many states maintained internally by PofStreamReader and PofStreamWriter during serialization.

For testing IPofSerializers I am doing the following



var context = new SimplePofContext()
context.RegisterUserType(1001, typeof(SerializableType), new SerializatableTypeSerializer())

using (var ms = new MemoryStream())
{
    var writer = new PofStreamWriter(new DataWriter(ms), context);
    writer.WriteObject(0, object);

    ms.Seek(0,0);

    var reader = new PofStreamReader(new DataReader(ms), context);
    var ob = reader.ReadObject(0)
} 

      

I suspect, but haven't tested, that for IPortableObjects, you don't need to register anything with the inorder context for this to work.

0


a source







All Articles