Exemplo n.º 1
0
  public void testInheritance() throws IOException {
    if (skipTests) {
      System.err.println("RuntimeSchema.MORPH_NON_FINAL_POJOS was not enabled.");
      return;
    }
    System.err.println("executing inheritance test for protostuff ... ");

    Schema<InputSystem> schema = RuntimeSchema.getSchema(InputSystem.class);
    InputSystem sys = new InputSystem();
    KeyBoard kb = new KeyBoard();
    Mouse ms = new Mouse();
    kb.setName("Test");
    kb.setNumberOfKeys(10);
    ms.setName("Test1");
    ms.setNumberOfButtons(2);
    List<InputDevice> devices = new ArrayList<InputDevice>();
    devices.add(ms);
    devices.add(kb);
    sys.setInputDevices(devices);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtostuffIOUtil.writeTo(out, sys, schema, buf());
    byte[] listData = out.toByteArray();
    InputSystem deserSystem = new InputSystem();
    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    ProtostuffIOUtil.mergeFrom(in, deserSystem, schema, buf());

    assertEquals(sys, deserSystem);
  }
  @Override
  protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema)
      throws Exception {
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff =
        ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf());

    byte[] protostuffFromStream =
        ProtostuffIOUtil.toByteArray(ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protostuff, protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(protostuff);

    byte[] protobufRoundTrip =
        ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf());

    byte[] protobufRoundTripFromStream =
        ProtobufIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protobufRoundTrip, protobufRoundTripFromStream));

    assertTrue(Arrays.equals(protobufRoundTrip, protobuf));
  }