| OLD | NEW |
| 1 | 1 |
| 2 import com.google.protobuf.conformance.Conformance; | 2 import com.google.protobuf.conformance.Conformance; |
| 3 import com.google.protobuf.util.JsonFormat; | |
| 4 import com.google.protobuf.util.JsonFormat.TypeRegistry; | |
| 5 import com.google.protobuf.InvalidProtocolBufferException; | 3 import com.google.protobuf.InvalidProtocolBufferException; |
| 6 | 4 |
| 7 class ConformanceJava { | 5 class ConformanceJavaLite { |
| 8 private int testCount = 0; | 6 private int testCount = 0; |
| 9 private TypeRegistry typeRegistry; | |
| 10 | 7 |
| 11 private boolean readFromStdin(byte[] buf, int len) throws Exception { | 8 private boolean readFromStdin(byte[] buf, int len) throws Exception { |
| 12 int ofs = 0; | 9 int ofs = 0; |
| 13 while (len > 0) { | 10 while (len > 0) { |
| 14 int read = System.in.read(buf, ofs, len); | 11 int read = System.in.read(buf, ofs, len); |
| 15 if (read == -1) { | 12 if (read == -1) { |
| 16 return false; // EOF | 13 return false; // EOF |
| 17 } | 14 } |
| 18 ofs += read; | 15 ofs += read; |
| 19 len -= read; | 16 len -= read; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 switch (request.getPayloadCase()) { | 50 switch (request.getPayloadCase()) { |
| 54 case PROTOBUF_PAYLOAD: { | 51 case PROTOBUF_PAYLOAD: { |
| 55 try { | 52 try { |
| 56 testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPa
yload()); | 53 testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPa
yload()); |
| 57 } catch (InvalidProtocolBufferException e) { | 54 } catch (InvalidProtocolBufferException e) { |
| 58 return Conformance.ConformanceResponse.newBuilder().setParseError(e.ge
tMessage()).build(); | 55 return Conformance.ConformanceResponse.newBuilder().setParseError(e.ge
tMessage()).build(); |
| 59 } | 56 } |
| 60 break; | 57 break; |
| 61 } | 58 } |
| 62 case JSON_PAYLOAD: { | 59 case JSON_PAYLOAD: { |
| 63 try { | 60 return Conformance.ConformanceResponse.newBuilder().setSkipped( |
| 64 Conformance.TestAllTypes.Builder builder = Conformance.TestAllTypes.ne
wBuilder(); | 61 "Lite runtime does not suport Json Formant.").build(); |
| 65 JsonFormat.parser().usingTypeRegistry(typeRegistry) | |
| 66 .merge(request.getJsonPayload(), builder); | |
| 67 testMessage = builder.build(); | |
| 68 } catch (InvalidProtocolBufferException e) { | |
| 69 return Conformance.ConformanceResponse.newBuilder().setParseError(e.ge
tMessage()).build(); | |
| 70 } | |
| 71 break; | |
| 72 } | 62 } |
| 73 case PAYLOAD_NOT_SET: { | 63 case PAYLOAD_NOT_SET: { |
| 74 throw new RuntimeException("Request didn't have payload."); | 64 throw new RuntimeException("Request didn't have payload."); |
| 75 } | 65 } |
| 76 | 66 |
| 77 default: { | 67 default: { |
| 78 throw new RuntimeException("Unexpected payload case."); | 68 throw new RuntimeException("Unexpected payload case."); |
| 79 } | 69 } |
| 80 } | 70 } |
| 81 | 71 |
| 82 switch (request.getRequestedOutputFormat()) { | 72 switch (request.getRequestedOutputFormat()) { |
| 83 case UNSPECIFIED: | 73 case UNSPECIFIED: |
| 84 throw new RuntimeException("Unspecified output format."); | 74 throw new RuntimeException("Unspecified output format."); |
| 85 | 75 |
| 86 case PROTOBUF: | 76 case PROTOBUF: |
| 87 return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(t
estMessage.toByteString()).build(); | 77 return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(t
estMessage.toByteString()).build(); |
| 88 | 78 |
| 89 case JSON: | 79 case JSON: |
| 90 try { | 80 return Conformance.ConformanceResponse.newBuilder().setSkipped( |
| 91 return Conformance.ConformanceResponse.newBuilder().setJsonPayload( | 81 "Lite runtime does not suport Json Formant.").build(); |
| 92 JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMes
sage)).build(); | |
| 93 } catch (InvalidProtocolBufferException | IllegalArgumentException e) { | |
| 94 return Conformance.ConformanceResponse.newBuilder().setSerializeError( | |
| 95 e.getMessage()).build(); | |
| 96 } | |
| 97 | 82 |
| 98 default: { | 83 default: { |
| 99 throw new RuntimeException("Unexpected request output."); | 84 throw new RuntimeException("Unexpected request output."); |
| 100 } | 85 } |
| 101 } | 86 } |
| 102 } | 87 } |
| 103 | 88 |
| 104 private boolean doTestIo() throws Exception { | 89 private boolean doTestIo() throws Exception { |
| 105 int bytes = readLittleEndianIntFromStdin(); | 90 int bytes = readLittleEndianIntFromStdin(); |
| 106 | 91 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 119 Conformance.ConformanceResponse response = doTest(request); | 104 Conformance.ConformanceResponse response = doTest(request); |
| 120 byte[] serializedOutput = response.toByteArray(); | 105 byte[] serializedOutput = response.toByteArray(); |
| 121 | 106 |
| 122 writeLittleEndianIntToStdout(serializedOutput.length); | 107 writeLittleEndianIntToStdout(serializedOutput.length); |
| 123 writeToStdout(serializedOutput); | 108 writeToStdout(serializedOutput); |
| 124 | 109 |
| 125 return true; | 110 return true; |
| 126 } | 111 } |
| 127 | 112 |
| 128 public void run() throws Exception { | 113 public void run() throws Exception { |
| 129 typeRegistry = TypeRegistry.newBuilder().add( | |
| 130 Conformance.TestAllTypes.getDescriptor()).build(); | |
| 131 while (doTestIo()) { | 114 while (doTestIo()) { |
| 132 // Empty. | 115 this.testCount++; |
| 133 } | 116 } |
| 134 | 117 |
| 135 System.err.println("ConformanceJava: received EOF from test runner after " + | 118 System.err.println("ConformanceJavaLite: received EOF from test runner after
" + |
| 136 this.testCount + " tests"); | 119 this.testCount + " tests"); |
| 137 } | 120 } |
| 138 | 121 |
| 139 public static void main(String[] args) throws Exception { | 122 public static void main(String[] args) throws Exception { |
| 140 new ConformanceJava().run(); | 123 new ConformanceJavaLite().run(); |
| 141 } | 124 } |
| 142 } | 125 } |
| OLD | NEW |