Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: third_party/protobuf/conformance/ConformanceJava.java

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/protobuf/configure.ac ('k') | third_party/protobuf/conformance/Makefile.am » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1
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;
6
7 class ConformanceJava {
8 private int testCount = 0;
9 private TypeRegistry typeRegistry;
10
11 private boolean readFromStdin(byte[] buf, int len) throws Exception {
12 int ofs = 0;
13 while (len > 0) {
14 int read = System.in.read(buf, ofs, len);
15 if (read == -1) {
16 return false; // EOF
17 }
18 ofs += read;
19 len -= read;
20 }
21
22 return true;
23 }
24
25 private void writeToStdout(byte[] buf) throws Exception {
26 System.out.write(buf);
27 }
28
29 // Returns -1 on EOF (the actual values will always be positive).
30 private int readLittleEndianIntFromStdin() throws Exception {
31 byte[] buf = new byte[4];
32 if (!readFromStdin(buf, 4)) {
33 return -1;
34 }
35 return (buf[0] & 0xff)
36 | ((buf[1] & 0xff) << 8)
37 | ((buf[2] & 0xff) << 16)
38 | ((buf[3] & 0xff) << 24);
39 }
40
41 private void writeLittleEndianIntToStdout(int val) throws Exception {
42 byte[] buf = new byte[4];
43 buf[0] = (byte)val;
44 buf[1] = (byte)(val >> 8);
45 buf[2] = (byte)(val >> 16);
46 buf[3] = (byte)(val >> 24);
47 writeToStdout(buf);
48 }
49
50 private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
51 Conformance.TestAllTypes testMessage;
52
53 switch (request.getPayloadCase()) {
54 case PROTOBUF_PAYLOAD: {
55 try {
56 testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPa yload());
57 } catch (InvalidProtocolBufferException e) {
58 return Conformance.ConformanceResponse.newBuilder().setParseError(e.ge tMessage()).build();
59 }
60 break;
61 }
62 case JSON_PAYLOAD: {
63 try {
64 Conformance.TestAllTypes.Builder builder = Conformance.TestAllTypes.ne wBuilder();
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 }
73 case PAYLOAD_NOT_SET: {
74 throw new RuntimeException("Request didn't have payload.");
75 }
76
77 default: {
78 throw new RuntimeException("Unexpected payload case.");
79 }
80 }
81
82 switch (request.getRequestedOutputFormat()) {
83 case UNSPECIFIED:
84 throw new RuntimeException("Unspecified output format.");
85
86 case PROTOBUF:
87 return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(t estMessage.toByteString()).build();
88
89 case JSON:
90 try {
91 return Conformance.ConformanceResponse.newBuilder().setJsonPayload(
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
98 default: {
99 throw new RuntimeException("Unexpected request output.");
100 }
101 }
102 }
103
104 private boolean doTestIo() throws Exception {
105 int bytes = readLittleEndianIntFromStdin();
106
107 if (bytes == -1) {
108 return false; // EOF
109 }
110
111 byte[] serializedInput = new byte[bytes];
112
113 if (!readFromStdin(serializedInput, bytes)) {
114 throw new RuntimeException("Unexpected EOF from test program.");
115 }
116
117 Conformance.ConformanceRequest request =
118 Conformance.ConformanceRequest.parseFrom(serializedInput);
119 Conformance.ConformanceResponse response = doTest(request);
120 byte[] serializedOutput = response.toByteArray();
121
122 writeLittleEndianIntToStdout(serializedOutput.length);
123 writeToStdout(serializedOutput);
124
125 return true;
126 }
127
128 public void run() throws Exception {
129 typeRegistry = TypeRegistry.newBuilder().add(
130 Conformance.TestAllTypes.getDescriptor()).build();
131 while (doTestIo()) {
132 // Empty.
133 }
134
135 System.err.println("ConformanceJava: received EOF from test runner after " +
136 this.testCount + " tests");
137 }
138
139 public static void main(String[] args) throws Exception {
140 new ConformanceJava().run();
141 }
142 }
OLDNEW
« no previous file with comments | « third_party/protobuf/configure.ac ('k') | third_party/protobuf/conformance/Makefile.am » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698