Search in sources :

Example 11 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class DescriptorsTest method testAnnotationTarget.

@Test
public void testAnnotationTarget() {
    exception.expect(DescriptorParserException.class);
    exception.expectMessage("Annotation '@Field()' cannot be applied to message types.");
    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 = "/** @Field */\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 12 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class DescriptorsTest method testDuplicateAnnotation.

@Test
public void testDuplicateAnnotation() {
    exception.expect(AnnotationParserException.class);
    exception.expectMessage("Error: 1,8: duplicate annotation definition \"Field\"");
    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" + "  /** @Field @Field */\n" + "  optional int32 field1 = 1; \n" + "}";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    // todo [anistor] this is waaay too lazy
    descriptors.get("test.proto").getMessageTypes().get(0).getFields().get(0).getAnnotations();
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 13 with Configuration

use of org.infinispan.protostream.config.Configuration in project protostream by infinispan.

the class DescriptorsTest method testDocAnnotations.

@Test
public void testDocAnnotations() {
    String file1 = "package test1;\n" + "/**  \n" + " *  @Foo(fooValue) \n" + "  *    some more doc text \n" + "      **/\n\n" + "message X {\n" + " /**\n" + "  * @Bar(barValue)  \n\n" + "  */\n" + "  optional int32 field1 = 1;\n" + "}\n";
    Configuration config = Configuration.builder().annotationsConfig().annotation("Foo", AnnotationElement.AnnotationTarget.MESSAGE).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.IDENTIFIER).metadataCreator((descriptor, annotation) -> annotation.getDefaultAttributeValue().getValue()).annotation("Bar", AnnotationElement.AnnotationTarget.FIELD).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.IDENTIFIER).metadataCreator((fieldDescriptor, annotation) -> annotation.getDefaultAttributeValue().getValue()).build();
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource();
    fileDescriptorSource.addProtoFile("file1.proto", file1);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    assertEquals(1, descriptors.size());
    assertTrue(descriptors.containsKey("file1.proto"));
    Map<String, GenericDescriptor> types = descriptors.get("file1.proto").getTypes();
    Descriptor typeX = (Descriptor) types.get("test1.X");
    assertNotNull(typeX);
    assertEquals(1, typeX.getFields().size());
    FieldDescriptor field1 = typeX.getFields().get(0);
    assertEquals("@Foo(fooValue) \n   some more doc text", typeX.getDocumentation());
    Map<String, AnnotationElement.Annotation> typeAnnotations = typeX.getAnnotations();
    assertEquals("fooValue", typeAnnotations.get("Foo").getDefaultAttributeValue().getValue());
    assertEquals("fooValue", typeX.getProcessedAnnotation("Foo"));
    assertEquals("@Bar(barValue)", field1.getDocumentation());
    Map<String, AnnotationElement.Annotation> fieldAnnotations = field1.getAnnotations();
    assertEquals("barValue", fieldAnnotations.get("Bar").getDefaultAttributeValue().getValue());
    assertEquals("barValue", field1.getProcessedAnnotation("Bar"));
}
Also used : Type(org.infinispan.protostream.descriptors.Type) URL(java.net.URL) HashMap(java.util.HashMap) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Map(java.util.Map) ExpectedException(org.junit.rules.ExpectedException) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Label(org.infinispan.protostream.descriptors.Label) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) Test(org.junit.Test) File(java.io.File) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AnnotationParserException(org.infinispan.protostream.AnnotationParserException) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Configuration(org.infinispan.protostream.config.Configuration) ResolutionContext(org.infinispan.protostream.descriptors.ResolutionContext) Assertions.assertThat(org.fest.assertions.api.Assertions.assertThat) Assert.assertEquals(org.junit.Assert.assertEquals) JavaType(org.infinispan.protostream.descriptors.JavaType) Configuration(org.infinispan.protostream.config.Configuration) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) ExtendDescriptor(org.infinispan.protostream.descriptors.ExtendDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test)

Aggregations

Configuration (org.infinispan.protostream.config.Configuration)13 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)12 Test (org.junit.Test)12 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)9 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)7 List (java.util.List)5 Descriptor (org.infinispan.protostream.descriptors.Descriptor)5 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)5 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)4 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)4 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)4 File (java.io.File)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Assertions.assertThat (org.fest.assertions.api.Assertions.assertThat)2 AnnotationParserException (org.infinispan.protostream.AnnotationParserException)2 DescriptorParserException (org.infinispan.protostream.DescriptorParserException)2 JavaType (org.infinispan.protostream.descriptors.JavaType)2 Label (org.infinispan.protostream.descriptors.Label)2