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

    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();
    ProtobufIOUtil.writeTo(out, sys, schema, buf());
    byte[] listData = out.toByteArray();
    InputSystem deserSystem = new InputSystem();
    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    ProtobufIOUtil.mergeFrom(in, deserSystem, schema, buf());

    assertEquals(sys, deserSystem);
  }
Ejemplo n.º 2
0
  /**
   * HasHasBar wraps an object without a schema. That object will have to be serialized via the
   * default java serialization and it will be delimited.
   *
   * <p>HasBar wraps a message {@link Bar}.
   */
  public void testJavaSerializable() throws Exception {
    Schema<HasHasBar> schema = RuntimeSchema.getSchema(HasHasBar.class);

    HasHasBar hhbCompare = new HasHasBar("hhb", new HasBar(12345, "hb", SerializableObjects.bar));
    HasHasBar dhhb = new HasHasBar();

    int expectedSize = ComputedSizeOutput.getSize(hhbCompare, schema);

    byte[] deferred = toByteArray(hhbCompare, schema);
    assertTrue(deferred.length == expectedSize);
    ProtostuffIOUtil.mergeFrom(deferred, dhhb, schema);
    assertEquals(hhbCompare, dhhb);
  }
Ejemplo n.º 3
0
  public void testFoo() throws Exception {
    Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);

    Foo fooCompare = foo;
    Foo dfoo = new Foo();

    byte[] deferred = toByteArray(fooCompare, schema);

    // ComputedSizeOutput is format compatible with protobuf
    // E.g collections are not serialized ... only its members/elements are.
    if (!RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS)
      assertTrue(deferred.length == ComputedSizeOutput.getSize(fooCompare, schema));

    ProtostuffIOUtil.mergeFrom(deferred, dfoo, schema);
    SerializableObjects.assertEquals(fooCompare, dfoo);
  }
Ejemplo n.º 4
0
  public void testPojoWithArrayAndSet() throws Exception {
    PojoWithArrayAndSet pojoCompare = filledPojoWithArrayAndSet();

    Schema<PojoWithArrayAndSet> schema = RuntimeSchema.getSchema(PojoWithArrayAndSet.class);

    PojoWithArrayAndSet dpojo = new PojoWithArrayAndSet();

    int expectedSize = ComputedSizeOutput.getSize(pojoCompare, schema, true);

    byte[] deferred = toByteArray(pojoCompare, schema);
    assertTrue(deferred.length == expectedSize);
    ProtostuffIOUtil.mergeFrom(deferred, dpojo, schema);
    assertEquals(pojoCompare, dpojo);
    // System.err.println(dpojo.getSomeEnumAsSet());
    // System.err.println(dpojo.getSomeFloatAsSet());
  }
Ejemplo n.º 5
0
  public void testBaz() throws Exception {
    Schema<Baz> schema = RuntimeSchema.getSchema(Baz.class);

    for (Baz bazCompare : new Baz[] {baz, negativeBaz}) {
      Baz dbaz = new Baz();

      int expectedSize = ComputedSizeOutput.getSize(bazCompare, schema);

      byte[] deferred = toByteArray(bazCompare, schema);
      assertTrue(deferred.length == expectedSize);
      ProtostuffIOUtil.mergeFrom(deferred, dbaz, schema);
      SerializableObjects.assertEquals(bazCompare, dbaz);
      // System.err.println(dbaz.getId());
      // System.err.println(dbaz.getName());
      // System.err.println(dbaz.getTimestamp());
    }
  }
Ejemplo n.º 6
0
  public void testBar() throws Exception {
    Schema<Bar> schema = RuntimeSchema.getSchema(Bar.class);

    for (Bar barCompare : new Bar[] {bar, negativeBar}) {
      Bar dbar = new Bar();

      int expectedSize = ComputedSizeOutput.getSize(barCompare, schema);

      byte[] deferred = toByteArray(barCompare, schema);
      assertTrue(deferred.length == expectedSize);
      ProtostuffIOUtil.mergeFrom(deferred, dbar, schema);
      SerializableObjects.assertEquals(barCompare, dbar);
      // System.err.println(dbar.getSomeInt());
      // System.err.println(dbar.getSomeLong());
      // System.err.println(dbar.getSomeFloat());
      // System.err.println(dbar.getSomeDouble());
      // System.err.println(dbar.getSomeBytes());
      // System.err.println(dbar.getSomeString());
      // System.err.println(dbar.getSomeEnum());
      // System.err.println(dbar.getSomeBoolean());
    }
  }
  public void testCollectionAndMapSchema() throws Exception {
    if (!RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS) {
      System.err.println("RuntimeSchema.COLLECTION_SCHEMA_ON_REPEATED_FIELDS was not enabled.");
      return;
    }

    Schema<PojoFoo> schema = RuntimeSchema.getSchema(PojoFoo.class);
    Pipe.Schema<PojoFoo> pipeSchema = ((MappedSchema<PojoFoo>) schema).pipeSchema;

    PojoFoo p = new PojoFoo().fill();

    byte[] data = toByteArray(p, schema);

    PojoFoo pFromByteArray = new PojoFoo();
    mergeFrom(data, 0, data.length, pFromByteArray, schema);
    assertEquals(p, pFromByteArray);

    PojoFoo pFromStream = new PojoFoo();
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    mergeFrom(in, pFromStream, schema);
    assertEquals(p, pFromByteArray);

    roundTrip(p, schema, pipeSchema);
  }