Search in sources :

Example 6 with Configuration

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

the class DescriptorsTest method testBrokenUndefinedAnnotation.

@Test
public void testBrokenUndefinedAnnotation() {
    exception.expect(AnnotationParserException.class);
    exception.expectMessage("Error: 2,21: ')' expected");
    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" + "  /** Here we have an annotation that fails to parse\n" + "  @SomeAnnotation(777 @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
    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 7 with Configuration

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

the class DescriptorsTest method testMultipleAnnotationAttribute.

@Test
public void testMultipleAnnotationAttribute() {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Xyz", AnnotationElement.AnnotationTarget.MESSAGE).attribute("elem1").type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).multiple(true).build();
    String testProto = "/** @Xyz(elem1 = {true, false, true}) */\n" + "message M {\n" + "  optional int32 field1 = 1; \n" + "}\n";
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString("test.proto", testProto);
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    FileDescriptor fileDescriptor = descriptors.get("test.proto");
    List<Descriptor> messageTypes = fileDescriptor.getMessageTypes();
    Descriptor messageType = messageTypes.get(0);
    assertEquals("M", messageType.getFullName());
    AnnotationElement.Annotation annotation = messageType.getAnnotations().get("Xyz");
    assertNotNull(annotation);
    AnnotationElement.Value attr = annotation.getAttributeValue("elem1");
    assertTrue(attr instanceof AnnotationElement.Array);
    assertTrue(attr.getValue() instanceof List);
    List values = (List) attr.getValue();
    assertEquals(3, values.size());
    assertEquals(true, values.get(0));
    assertEquals(false, values.get(1));
    assertEquals(true, values.get(2));
}
Also used : Configuration(org.infinispan.protostream.config.Configuration) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) 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) List(java.util.List) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Test(org.junit.Test)

Example 8 with Configuration

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

the class DescriptorsTest method testUndefinedAnnotation.

@Test
public void testUndefinedAnnotation() {
    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(x=777, y=\"YES\") @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");
    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)

Example 9 with Configuration

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

the class ProtobufUtilTest method testWrappedMessageTypeIdMapper.

@Test
public void testWrappedMessageTypeIdMapper() throws Exception {
    WrappedMessageTypeIdMapper mapper = new WrappedMessageTypeIdMapper() {

        @Override
        public int mapTypeIdOut(int typeId, ImmutableSerializationContext ctx) {
            if (typeId == 100042) {
                // change typeId ouf User
                return 100021;
            }
            return typeId;
        }
    };
    Configuration cfg = Configuration.builder().wrappingConfig().wrappedMessageTypeIdMapper(mapper).build();
    ImmutableSerializationContext ctx = createContext(cfg);
    // this has TypeId 100042
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    byte[] bytes = ProtobufUtil.toWrappedByteArray(ctx, user);
    int[] seenTypeId = new int[] { -1 };
    TagHandler tagHandler = new TagHandler() {

        @Override
        public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
            if (fieldNumber == WrappedMessage.WRAPPED_TYPE_ID) {
                seenTypeId[0] = (Integer) tagValue;
            }
        }
    };
    Descriptor wrappedMessageDescriptor = ctx.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME);
    ProtobufParser.INSTANCE.parse(tagHandler, wrappedMessageDescriptor, bytes);
    assertEquals(100021, seenTypeId[0]);
}
Also used : User(org.infinispan.protostream.domain.User) Configuration(org.infinispan.protostream.config.Configuration) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 10 with Configuration

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

the class DescriptorsTest method testAnnotationParser.

@Test
public void testAnnotationParser() throws Exception {
    Configuration config = Configuration.builder().annotationsConfig().annotation("Indexed", AnnotationElement.AnnotationTarget.MESSAGE).attribute(AnnotationElement.Annotation.VALUE_DEFAULT_ATTRIBUTE).type(AnnotationElement.AttributeType.BOOLEAN).defaultValue(true).metadataCreator((descriptor, annotation) -> annotation.getDefaultAttributeValue().getValue()).build();
    FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromResources("sample_bank_account/bank.proto");
    Map<String, FileDescriptor> descriptors = parseAndResolve(fileDescriptorSource, config);
    FileDescriptor fileDescriptor = descriptors.get("sample_bank_account/bank.proto");
    List<Descriptor> messageTypes = fileDescriptor.getMessageTypes();
    Descriptor userMessageType = messageTypes.get(0);
    assertEquals("sample_bank_account.User", userMessageType.getFullName());
    assertEquals(Boolean.TRUE, userMessageType.getProcessedAnnotation("Indexed"));
    Descriptor accountMessageType = messageTypes.get(1);
    assertEquals("sample_bank_account.Account", accountMessageType.getFullName());
    assertEquals(Boolean.TRUE, accountMessageType.getProcessedAnnotation("Indexed"));
}
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) 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) 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