Search in sources :

Example 1 with Simple

use of org.infinispan.protostream.annotations.impl.testdomain.Simple in project protostream by infinispan.

the class ProtoSchemaBuilderTest method testGeneration.

@Test
public void testGeneration() throws Exception {
    SerializationContext ctx = createContext();
    ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder();
    protoSchemaBuilder.fileName("test.proto").packageName("test_package").addClass(TestClass.class).addClass(TestClass3.class).addClass(Simple.class).build(ctx);
    assertTrue(ctx.canMarshall(TestEnum.class));
    assertTrue(ctx.canMarshall(Simple.class));
    assertTrue(ctx.canMarshall(TestClass.class));
    assertTrue(ctx.canMarshall(TestClass.InnerClass.class));
    assertTrue(ctx.canMarshall(TestClass.InnerClass2.class));
    assertTrue(ctx.canMarshall(TestClass2.class));
    assertTrue(ctx.canMarshall(TestClass3.class));
    assertTrue(ctx.canMarshall("test_package.TestEnumABC"));
    assertTrue(ctx.canMarshall("test_package.Simple"));
    assertTrue(ctx.canMarshall("test_package.TestClass2"));
    assertTrue(ctx.canMarshall("test_package.TestClass3"));
    assertTrue(ctx.canMarshall("test_package.TestClass"));
    assertTrue(ctx.canMarshall("test_package.TestClass.InnerClass"));
    assertTrue(ctx.canMarshall("test_package.TestClass.InnerClass2"));
    Simple simple = new Simple();
    simple.afloat = 3.14f;
    byte[] bytes = ProtobufUtil.toWrappedByteArray(ctx, simple);
    Object unmarshalled = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
    assertTrue(unmarshalled instanceof Simple);
    Simple unwrapped = (Simple) unmarshalled;
    assertEquals(3.14f, unwrapped.afloat, 0.001);
    TestClass testClass = new TestClass();
    testClass.surname = "test";
    testClass.longField = 100L;
    testClass.testClass2 = new TestClass2();
    testClass.testClass2.address = "test address";
    bytes = ProtobufUtil.toWrappedByteArray(ctx, testClass);
    unmarshalled = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
    assertTrue(unmarshalled instanceof TestClass);
    assertEquals("test", ((TestClass) unmarshalled).surname);
    assertEquals(100L, ((TestClass) unmarshalled).longField);
    assertEquals("test address", ((TestClass) unmarshalled).testClass2.address);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ProtoSchemaBuilder(org.infinispan.protostream.annotations.ProtoSchemaBuilder) TestClass(org.infinispan.protostream.annotations.impl.testdomain.TestClass) TestEnum(org.infinispan.protostream.annotations.impl.testdomain.TestEnum) TestClass3(org.infinispan.protostream.annotations.impl.testdomain.TestClass3) TestClass2(org.infinispan.protostream.annotations.impl.testdomain.subpackage.TestClass2) Simple(org.infinispan.protostream.annotations.impl.testdomain.Simple) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 2 with Simple

use of org.infinispan.protostream.annotations.impl.testdomain.Simple in project protostream by infinispan.

the class ProtoSchemaBuilderTest method testUnknownFields.

@Test
public void testUnknownFields() throws Exception {
    SerializationContext ctx = createContext();
    ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder();
    protoSchemaBuilder.fileName("test.proto").packageName("test_package").addClass(Simple.class).build(ctx);
    assertTrue(ctx.canMarshall(Simple.class));
    assertTrue(ctx.canMarshall("test_package.Simple"));
    byte[] msg1 = ProtobufUtil.toByteArray(ctx, new Simple());
    byte[] msg2 = ProtobufUtil.toWrappedByteArray(ctx, 1234);
    // concatenate the two messages to have some unknown fields in the stream
    byte[] concatenatedMsg = new byte[msg1.length + msg2.length];
    System.arraycopy(msg1, 0, concatenatedMsg, 0, msg1.length);
    System.arraycopy(msg2, 0, concatenatedMsg, msg1.length, msg2.length);
    // we should be able to deal with those unknown fields gracefully when unmarshalling
    Object unmarshalled = ProtobufUtil.fromByteArray(ctx, concatenatedMsg, Simple.class);
    assertTrue(unmarshalled instanceof Simple);
    Simple simple = (Simple) unmarshalled;
    // ensure we do have some unknown fields there
    assertFalse(simple.unknownFieldSet.isEmpty());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ProtoSchemaBuilder(org.infinispan.protostream.annotations.ProtoSchemaBuilder) Simple(org.infinispan.protostream.annotations.impl.testdomain.Simple) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Aggregations

SerializationContext (org.infinispan.protostream.SerializationContext)2 ProtoSchemaBuilder (org.infinispan.protostream.annotations.ProtoSchemaBuilder)2 Simple (org.infinispan.protostream.annotations.impl.testdomain.Simple)2 AbstractProtoStreamTest (org.infinispan.protostream.test.AbstractProtoStreamTest)2 Test (org.junit.Test)2 TestClass (org.infinispan.protostream.annotations.impl.testdomain.TestClass)1 TestClass3 (org.infinispan.protostream.annotations.impl.testdomain.TestClass3)1 TestEnum (org.infinispan.protostream.annotations.impl.testdomain.TestEnum)1 TestClass2 (org.infinispan.protostream.annotations.impl.testdomain.subpackage.TestClass2)1