Example #1
0
  public void run() throws Exception {
    typeRegistry = TypeRegistry.newBuilder().add(Conformance.TestAllTypes.getDescriptor()).build();
    while (doTestIo()) {
      // Empty.
    }

    System.err.println(
        "ConformanceJava: received EOF from test runner after " + this.testCount + " tests");
  }
Example #2
0
  private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
    Conformance.TestAllTypes testMessage;

    switch (request.getPayloadCase()) {
      case PROTOBUF_PAYLOAD:
        {
          try {
            testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPayload());
          } catch (InvalidProtocolBufferException e) {
            return Conformance.ConformanceResponse.newBuilder()
                .setParseError(e.getMessage())
                .build();
          }
          break;
        }
      case JSON_PAYLOAD:
        {
          try {
            Conformance.TestAllTypes.Builder builder = Conformance.TestAllTypes.newBuilder();
            JsonFormat.parser()
                .usingTypeRegistry(typeRegistry)
                .merge(request.getJsonPayload(), builder);
            testMessage = builder.build();
          } catch (InvalidProtocolBufferException e) {
            return Conformance.ConformanceResponse.newBuilder()
                .setParseError(e.getMessage())
                .build();
          }
          break;
        }
      case PAYLOAD_NOT_SET:
        {
          throw new RuntimeException("Request didn't have payload.");
        }

      default:
        {
          throw new RuntimeException("Unexpected payload case.");
        }
    }

    switch (request.getRequestedOutputFormat()) {
      case UNSPECIFIED:
        throw new RuntimeException("Unspecified output format.");

      case PROTOBUF:
        return Conformance.ConformanceResponse.newBuilder()
            .setProtobufPayload(testMessage.toByteString())
            .build();

      case JSON:
        try {
          return Conformance.ConformanceResponse.newBuilder()
              .setJsonPayload(
                  JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage))
              .build();
        } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
          return Conformance.ConformanceResponse.newBuilder()
              .setSerializeError(e.getMessage())
              .build();
        }

      default:
        {
          throw new RuntimeException("Unexpected request output.");
        }
    }
  }