我使用带有循环引用和泛型的原型二进制文件。作为一个非常简单的场景,我有以下几个类:
public class A {
private String name;
private B b;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
this.b.setA(this);
}
}///////////////////////////////////////////////////////////////////////////////
public class B {
private String name;
private A a;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}///////////////////////////////////////////////////////////////////////////////
public class Container<E> {
private E element;
private String name;
public Container() {
}
public Container(E e, String name) {
super();
this.element = e;
this.name = name;
}
public E getElement() {
return element;
}
public void setElement(E e) {
this.element = e;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}当我运行以下单元测试来检查往返序列化/反序列化是否正确执行时,我得到了一个非常奇怪的结果。最后一个断言失败:
public class CircularRefTest {
@Test
public void testCircularReferences() {
A a = new A();
a.setName("a");
B b = new B();
b.setName("b");
a.setB(b);
Container<A> container = new Container<A>(a, "container");
Schema<Container> schema = RuntimeSchema.getSchema(Container.class);
LinkedBuffer buffer = LinkedBuffer.allocate(256);
byte[] data = GraphIOUtil.toByteArray(container, schema, buffer);
Container<A> copy = new Container<A>();
GraphIOUtil.mergeFrom(data, copy, schema);
assertEquals(container.getName(), copy.getName());
assertEquals(container.getElement().getName(), copy.getElement().getName());
assertEquals(container.getElement().getB().getName(), copy.getElement().getB().getName());
// something weird happens here with the circular references here
System.out.println(copy.getElement().getB().getA().getClass());
assertTrue(copy.getElement().getB().getA() instanceof A); // fails
}
}原型破坏了从子类返回到父类的循环引用。最后一个断言应该通过,但是由于某种原因,这个类是Container类型。
我做错了什么?
如果我将Container类更改为使用强类型对象,则单元测试将通过。这是个bug吗??
我使用的maven工件是:
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-api</artifactId>
<version>1.0.4</version>
</dependency>发布于 2012-02-02 22:40:37
我非常仔细地查看了我的项目中的完整依赖项列表,我发现了以下内容:
protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.2
protostuff-runtime: 1.0.2
protostuff-core: 1.0.2我觉得1.0.2的罐子看起来很奇怪。然后我删除了所有的依赖项。
然后我添加了以下依赖项:
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.4</version>
</dependency>这是在以下jars中提取的:
protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.4
protostuff-runtime: 1.0.4
protostuff-core: 1.0.4然后我重新运行了单元测试&一切正常!
为了仔细检查,我对基于版本1.0.2的所有jars运行了单元测试,测试失败。这个问题似乎已经在1.0.4版本中得到了解决
https://stackoverflow.com/questions/9101464
复制相似问题