Search in sources :

Example 1 with TestClass

use of org.infinispan.protostream.annotations.impl.testdomain.TestClass 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 TestClass

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

the class ProtoSchemaBuilderTest method testDocumentation.

@Test
public void testDocumentation() throws Exception {
    SerializationContext ctx = createContext();
    ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder();
    String schemaFile = protoSchemaBuilder.fileName("test1.proto").packageName("test_package1").addClass(TestEnum.class).addClass(TestClass.class).build(ctx);
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test1.proto", schemaFile);
    Map<String, FileDescriptor> fileDescriptors = new SquareProtoParser(ctx.getConfiguration()).parse(fileDescriptorSource);
    FileDescriptor fd = fileDescriptors.get("test1.proto");
    assertNotNull(fd);
    Map<String, EnumDescriptor> enums = new HashMap<>();
    for (EnumDescriptor e : fd.getEnumTypes()) {
        enums.put(e.getFullName(), e);
    }
    Map<String, Descriptor> messages = new HashMap<>();
    for (Descriptor m : fd.getMessageTypes()) {
        messages.put(m.getFullName(), m);
    }
    EnumDescriptor testEnum = enums.get("test_package1.TestEnumABC");
    assertNotNull(testEnum);
    assertEquals("bla bla bla\nand some more bla\n@TypeId(100777)", testEnum.getDocumentation());
    assertEquals("This should never be read.", testEnum.getValues().get(0).getDocumentation());
    Descriptor testClass = messages.get("test_package1.TestClass");
    assertNotNull(testClass);
    assertEquals("@Indexed()\nbla bla bla\nand some more bla", testClass.getDocumentation());
    assertEquals("The surname, of course", testClass.getFields().get(0).getDocumentation());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ProtoSchemaBuilder(org.infinispan.protostream.annotations.ProtoSchemaBuilder) HashMap(java.util.HashMap) TestClass(org.infinispan.protostream.annotations.impl.testdomain.TestClass) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) SquareProtoParser(org.infinispan.protostream.impl.parser.SquareProtoParser) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 3 with TestClass

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

the class ProtoSchemaBuilderTest method testAutoImport.

@Test
public void testAutoImport() throws Exception {
    SerializationContext ctx = createContext();
    new ProtoSchemaBuilder().fileName("test1.proto").packageName("test_package1").addClass(TestEnum.class).build(ctx);
    assertTrue(ctx.canMarshall(TestEnum.class));
    assertTrue(ctx.canMarshall("test_package1.TestEnumABC"));
    ProtoSchemaBuilder protoSchemaBuilder2 = new ProtoSchemaBuilder();
    protoSchemaBuilder2.autoImportClasses(false).fileName("test2.proto").packageName("test_package2").addClass(TestClass.class);
    try {
        // TestClass2 was not added explicitly and is not auto-added because autoImportClasses == false so we expect an exception
        protoSchemaBuilder2.build(ctx);
        fail("ProtoSchemaBuilderException expected");
    } catch (ProtoSchemaBuilderException e) {
        assertTrue(e.getMessage().contains("Found a reference to class org.infinispan.protostream.annotations.impl.testdomain.subpackage.TestClass2 which was not added to the builder and 'autoImportClasses' is disabled."));
    }
    assertFalse(ctx.canMarshall(TestClass.class));
    assertFalse(ctx.canMarshall(TestClass.InnerClass.class));
    assertFalse(ctx.canMarshall(TestClass.InnerClass2.class));
    assertFalse(ctx.canMarshall(TestClass2.class));
    // it must work after explicitly adding TestClass2
    protoSchemaBuilder2.addClass(TestClass2.class).build(ctx);
    assertTrue(ctx.canMarshall(TestClass.class));
    assertTrue(ctx.canMarshall("test_package2.TestClass"));
    assertTrue(ctx.canMarshall(TestClass.InnerClass.class));
    assertTrue(ctx.canMarshall(TestClass.InnerClass2.class));
    assertTrue(ctx.canMarshall(TestClass2.class));
    assertTrue(ctx.canMarshall("test_package1.TestEnumABC"));
    assertFalse(ctx.canMarshall("test_package2.TestEnumABC"));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ProtoSchemaBuilder(org.infinispan.protostream.annotations.ProtoSchemaBuilder) ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) TestEnum(org.infinispan.protostream.annotations.impl.testdomain.TestEnum) TestClass(org.infinispan.protostream.annotations.impl.testdomain.TestClass) TestClass2(org.infinispan.protostream.annotations.impl.testdomain.subpackage.TestClass2) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 4 with TestClass

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

the class ProtoSchemaBuilderTest method testTwoFilesGeneration.

@Test
public void testTwoFilesGeneration() throws Exception {
    SerializationContext ctx = createContext();
    ProtoSchemaBuilder protoSchemaBuilder1 = new ProtoSchemaBuilder();
    protoSchemaBuilder1.fileName("test1.proto").packageName("test_package1").addClass(TestEnum.class).build(ctx);
    assertTrue(ctx.canMarshall(TestEnum.class));
    assertTrue(ctx.canMarshall("test_package1.TestEnumABC"));
    ProtoSchemaBuilder protoSchemaBuilder2 = new ProtoSchemaBuilder();
    protoSchemaBuilder2.fileName("test2.proto").packageName("test_package2").addClass(TestClass.class).build(ctx);
    assertTrue(ctx.canMarshall(TestClass.class));
    assertTrue(ctx.canMarshall("test_package2.TestClass"));
    assertTrue(ctx.canMarshall("test_package1.TestEnumABC"));
    assertFalse(ctx.canMarshall("test_package2.TestEnumABC"));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ProtoSchemaBuilder(org.infinispan.protostream.annotations.ProtoSchemaBuilder) TestEnum(org.infinispan.protostream.annotations.impl.testdomain.TestEnum) TestClass(org.infinispan.protostream.annotations.impl.testdomain.TestClass) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Aggregations

SerializationContext (org.infinispan.protostream.SerializationContext)4 ProtoSchemaBuilder (org.infinispan.protostream.annotations.ProtoSchemaBuilder)4 TestClass (org.infinispan.protostream.annotations.impl.testdomain.TestClass)4 AbstractProtoStreamTest (org.infinispan.protostream.test.AbstractProtoStreamTest)4 Test (org.junit.Test)4 TestEnum (org.infinispan.protostream.annotations.impl.testdomain.TestEnum)3 TestClass2 (org.infinispan.protostream.annotations.impl.testdomain.subpackage.TestClass2)2 HashMap (java.util.HashMap)1 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)1 ProtoSchemaBuilderException (org.infinispan.protostream.annotations.ProtoSchemaBuilderException)1 Simple (org.infinispan.protostream.annotations.impl.testdomain.Simple)1 TestClass3 (org.infinispan.protostream.annotations.impl.testdomain.TestClass3)1 Descriptor (org.infinispan.protostream.descriptors.Descriptor)1 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)1 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)1 SquareProtoParser (org.infinispan.protostream.impl.parser.SquareProtoParser)1