DescriptorsTest.java 文件源码

java
阅读 32 收藏 0 点赞 0 评论 0

项目:s-store 作者:
/**
 * Tests that the DescriptorValidationException works as intended.
 */
public void testDescriptorValidatorException() throws Exception {
  FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder()
    .setName("foo.proto")
    .addMessageType(DescriptorProto.newBuilder()
    .setName("Foo")
      .addField(FieldDescriptorProto.newBuilder()
        .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
        .setType(FieldDescriptorProto.Type.TYPE_INT32)
        .setName("foo")
        .setNumber(1)
        .setDefaultValue("invalid")
        .build())
      .build())
    .build();
  try {
    Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, 
        new FileDescriptor[0]);
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    // Expected; check that the error message contains some useful hints
    assertTrue(e.getMessage().indexOf("foo") != -1);
    assertTrue(e.getMessage().indexOf("Foo") != -1);
    assertTrue(e.getMessage().indexOf("invalid") != -1);
    assertTrue(e.getCause() instanceof NumberFormatException);
    assertTrue(e.getCause().getMessage().indexOf("invalid") != -1);
  }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号