Search in sources :

Example 11 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class DescriptorsTest method testRepeatedAnnotation.

@Test
public void testRepeatedAnnotation() {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Field", AnnotationElement.AnnotationTarget.FIELD).repeatable("Fields").attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).build();
    String testProto = "message M {\n" + "  /** @Field @Field */\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    Map<String, AnnotationElement.Annotation> annotations = descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations();
    assertFalse(annotations.containsKey("Field"));
    assertTrue(annotations.containsKey("Fields"));
    List<AnnotationElement.Annotation> innerAnnotations = (List<AnnotationElement.Annotation>) annotations.get("Fields").getDefaultAttributeValue().getValue();
    assertEquals(2, innerAnnotations.size());
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) List(java.util.List) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 12 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class DescriptorsTest method testNotImportedInAnotherPackage.

@Test
public void testNotImportedInAnotherPackage() {
    exception.expect(DescriptorParserException.class);
    exception.expectMessage("Failed to resolve type of field \"test2.M2.b\". Type not found : test1.M1");
    String file1 = "package test1;\n" + "message M1 {\n" + "  required string a = 1;\n" + "}";
    String file2 = "package test2;\n" + "message M2 {\n" + "  required test1.M1 b = 2;\n" + "}";
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource();
    fileDescriptorSource.addProtoFile("file1.proto", file1);
    fileDescriptorSource.addProtoFile("file2.proto", file2);
    parseAndResolve(fileDescriptorSource);
}
Also used : FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 13 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class DescriptorsTest method testAnnotationParserMissingRequiredAttribute.

@Test
public void testAnnotationParserMissingRequiredAttribute() {
    exception.expect(DescriptorParserException.class);
    exception.expectMessage("org.infinispan.protostream.AnnotationParserException: Attribute 'value' of annotation 'AnnotationWithRequiredAttribute' on M is required");
    Configuration config = Configuration.builder().annotationsConfig().annotation("AnnotationWithRequiredAttribute", AnnotationElement.AnnotationTarget.MESSAGE).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).build();
    String testProto = "/** @AnnotationWithRequiredAttribute */\n" + "message M {\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    parseAndResolve(fileDescriptorSource, config);
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 14 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class DescriptorsTest method testDuplicateEnumConstantName.

@Test
public void testDuplicateEnumConstantName() {
    exception.expect(DescriptorParserException.class);
    exception.expectMessage("Enum constant 'A' is already defined in test1.E");
    String file1 = "package test1;\n" + "enum E {\n" + "   A = 1;\n" + "   A = 2;\n" + "}";
    FileDescriptorSource source = new FileDescriptorSource();
    source.addProtoFile("test.proto", file1);
    parseAndResolve(source);
}
Also used : FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 15 with FileDescriptorSource

use of org.infinispan.protostream.FileDescriptorSource in project protostream by infinispan.

the class DescriptorsTest method testDuplicateUndefinedAnnotation.

@Test
public void testDuplicateUndefinedAnnotation() {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Field", AnnotationElement.AnnotationTarget.FIELD).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).build();
    String testProto = "message M {\n" + "  /** @SomeAnnotation @SomeAnnotation @Field */\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    // todo [anistor] The processing of annotations is waaay too lazy
    AnnotationElement.Annotation someAnnotation = descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations().get("SomeAnnotation");
    // 'SomeAnnotation' annotation is not defined, but we accept it silently
    assertNull(someAnnotation);
    AnnotationElement.Annotation fieldAnnotation = descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations().get("Field");
    assertNotNull(fieldAnnotation);
    assertEquals("Field", fieldAnnotation.getName());
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Aggregations

FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)74 Test (org.junit.Test)61 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)24 SerializationContext (org.infinispan.protostream.SerializationContext)23 Configuration (org.infinispan.protostream.config.Configuration)13 DescriptorParserException (org.infinispan.protostream.DescriptorParserException)9 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)8 HashMap (java.util.HashMap)7 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)7 Descriptor (org.infinispan.protostream.descriptors.Descriptor)7 List (java.util.List)6 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)5 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)5 TagReader (org.infinispan.protostream.TagReader)4 SerializationContextImpl (org.infinispan.protostream.impl.SerializationContextImpl)4 Map (java.util.Map)3 ReadContext (org.infinispan.protostream.ProtobufTagMarshaller.ReadContext)3