3

我正在使用带有循环引用和泛型的 protostuff 二进制文件。作为一个非常简单的场景,我有以下类:

    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
    }
}

Protostuff 正在破坏从子类到父类的循环引用。最后一个断言应该通过,但由于某种原因,该类是 Container 类型。

我究竟做错了什么 ?

如果我将 Container 类更改为使用强类型对象,则单元测试通过。这是一个错误吗?

我正在使用的 Maven 工件是:

<dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-api</artifactId>
        <version>1.0.4</version>
    </dependency>
4

1 回答 1

2

我非常仔细地查看了项目中的完整依赖项列表,发现以下内容:

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>

这拉入了以下罐子:

protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.4
protostuff-runtime: 1.0.4
protostuff-core: 1.0.4

然后我重新运行了单元测试,一切正常!

只是为了仔细检查我对基于版本 1.0.2 的所有 jar 运行单元测试,测试失败。看起来这个问题已经在 1.0.4 版本中修复了

于 2012-02-02T14:40:36.917 回答