在 .NET 中测试 Oracle Coherence IPortableObject 实现
为了获得良好的测试覆盖率,我想测试 IPortableObject
的 WriteExternal
和 ReadExternal
方法(如 创建 IPortableObject 实现 (. NET))。
这是与我的测试方法中的代码类似的代码(我喜欢,并且它有效)。
Person person = new Person { Name = "John Doe" };
Person personCopy = CoherenceTestHelper.CopyUsingPofSerialization<Person>(person);
Assert.AreEqual(person, personCopy);
但为了让它工作,我必须子类 PofStreamWriter
来覆盖 BeginProperty
和 PofStreamReader
来覆盖 AdvanceTo
。 对我来说,这有点奇怪,但我想不出更好的解决方案。 下面是我的 CoherenceTestHelper 的实际代码。
问题:是否有其他人以更好的方式测试了他们的 IPortableObject
实现?
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;
}
}
}
In order to get good test coverage, I want to test the WriteExternal
and ReadExternal
methods of IPortableObject
(as described at Creating an IPortableObject Implementation (.NET) ).
Here is 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 do subclass PofStreamWriter
to override BeginProperty
and PofStreamReader
to override AdvanceTo
. To me this smells a little funny, but I couldn't come up with a better solution. Below is my actual code for my CoherenceTestHelper.
Question: Has anybody else unit tested their IPortableObject
implementation a better way?
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此测试过于关注
IPofReader
和IPofWriter
的实际执行。 我建议您使用 Rhino Mocks 之类的框架创建读者/作者的模拟。 将您的 POF 方法传递给模拟的读取器/写入器。 然后简单地断言使用正确的参数调用了读/写方法。This test is too concerned with the actual execution of the
IPofReader
andIPofWriter
. I suggest you create a mock of the reader/writer using a framework like Rhino Mocks. Pass your POF method(s) the mocked reader/writer. Then simply assert that the read/write methods were called with the correct parameters.我一直在研究如何对 IPofSerializer 进行单元测试,我希望它与您的问题类似。 我发现,为了正确序列化和反序列化用户类型,您必须调用 PofStreamWriter.WriteObject 和 PofStreamReader.ReadObject。 用户类型还必须在 PofContext 中注册才能正常工作。
原因是 PofStreamWriter 类包含一个嵌套子类,它在序列化用户类型时委托给该子类。 该子类写入有关类型的附加数据,然后 PofStreamReader 使用这些数据来正确反序列化该类型。 如果没有这些附加数据,PofStreamReader 无法确定它是什么类型以及应该使用什么序列化器。 看起来它只是不允许在完全隔离的情况下测试序列化器,因为序列化时 PofStreamReader 和 PofStreamWriter 在内部维护了很多状态。
为了测试 IPofSerializers,我正在执行以下操作
,我怀疑但尚未测试,对于 IPortableObjects,您不必向上下文注册任何内容即可使其正常工作。
I've been looking into how to unit test IPofSerializer's and I expect it's similar to your problem. What I've found is that in order to correctly serialize and deserialize the user type you have to call PofStreamWriter.WriteObject and PofStreamReader.ReadObject. The user type also has to be registered with the PofContext for this to work correctly.
The reason for this is that the PofStreamWriter class contains a nested subclass that it delgates to when serializing user types. This subclass writes additonal data about the type that the PofStreamReader then uses to correctly deserialize the type. Without this addtional data the PofStreamReader can't determine what type it is and what serializer should be used. It looks like it's just not setup to allow for testing of a serializer in complete isolation as there is to much state maintained internally by the PofStreamReader and PofStreamWriter while serializing.
For testing IPofSerializers I'm doing the following
I suspect, but haven't tested, that for IPortableObjects you don't have to register anything with the context inorder for this to work.