Skip to content

Commit 49ab88f

Browse files
author
Sheridan C Rawlins
committed
[Issue #23] Write a unit test that shows that unknown fields are not read into the message.
1 parent eed18d4 commit 49ab88f

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<exec executable="${protoc}" failonerror="true">
162162
<arg value="--java_out=${project.build.directory}/generated-test-sources" />
163163
<arg value="--proto_path=${project.basedir}/src/test/resources" />
164+
<arg value="${project.basedir}/src/test/resources/proto/issue23.proto" />
164165
<arg value="${project.basedir}/src/test/resources/proto/unittest.proto" />
165166
<arg value="${project.basedir}/src/test/resources/proto/unittest_import.proto" />
166167
<arg value="${project.basedir}/src/test/resources/proto/unittest_couchdb.proto" />

src/test/java/com/googlecode/protobuf/format/JsonFormatTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.googlecode.protobuf.format;
22

3+
import com.google.protobuf.ByteString;
4+
import com.google.protobuf.ExtensionRegistry;
35
import com.google.protobuf.Message;
6+
import com.google.protobuf.UnknownFieldSet;
7+
import com.googlecode.protobuf.format.issue23.Issue23;
48
import org.testng.annotations.DataProvider;
59
import org.testng.annotations.Test;
610
import org.testng.reporters.Files;
711
import protobuf_unittest.UnittestProto;
12+
import sun.nio.cs.StandardCharsets;
813

914
import java.io.IOException;
15+
import java.io.StringBufferInputStream;
16+
import java.nio.charset.Charset;
1017
import java.util.Arrays;
1118

1219
import static org.hamcrest.CoreMatchers.is;
@@ -19,11 +26,11 @@
1926
public class JsonFormatTest {
2027
private static final String RESOURCE_PATH = "/expectations/JsonFormatTest/";
2128
private static final FormatFactory FORMAT_FACTORY = new FormatFactory();
22-
private static final ProtobufFormatter JSON_FORMATTER =
23-
FORMAT_FACTORY.createFormatter(FormatFactory.Formatter.JSON);
29+
private static final JsonFormat JSON_FORMATTER =
30+
(JsonFormat) FORMAT_FACTORY.createFormatter(FormatFactory.Formatter.JSON);
2431

2532
private static String getExpected(String name) throws IOException {
26-
return Files.readFile(JsonFormatTest.class.getResourceAsStream(RESOURCE_PATH + "test1.json")).trim();
33+
return Files.readFile(JsonFormatTest.class.getResourceAsStream(RESOURCE_PATH + name)).trim();
2734
}
2835

2936
@DataProvider(name = "data")
@@ -44,4 +51,28 @@ public void testJsonFormat(
4451
String desc, Message input, ProtobufFormatter formatter, String expectedString) throws Exception {
4552
assertThat(formatter.printToString(input), is(expectedString));
4653
}
54+
55+
// TODO(scr): Re-enable test when the code is fixed to enable slurping unknown fields into the message.
56+
@Test(enabled = false, description = "https://github.com/bivas/protobuf-java-format/issues/23")
57+
public void testIssue23() throws Exception {
58+
Issue23.MsgWithUnknownFields issue23Message = Issue23.MsgWithUnknownFields.newBuilder()
59+
.setLeaf1("Hello")
60+
.setLeaf2(23)
61+
.addLeaf3(41)
62+
.setUnknownFields(
63+
UnknownFieldSet.newBuilder()
64+
.addField(4, UnknownFieldSet.Field.newBuilder()
65+
.addLengthDelimited(ByteString.copyFromUtf8("world"))
66+
.build())
67+
.build())
68+
.build();
69+
String message = JSON_FORMATTER.printToString(issue23Message);
70+
assertThat(message, is("{\"leaf1\": \"Hello\",\"leaf2\": 23,\"leaf3\": [41], \"4\": [\"world\"]}"));
71+
72+
Issue23.MsgWithUnknownFields.Builder issue23Builder = Issue23.MsgWithUnknownFields.newBuilder();
73+
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
74+
Issue23.registerAllExtensions(extensionRegistry);
75+
JSON_FORMATTER.merge(message, extensionRegistry, issue23Builder);
76+
assertThat("No unknown field 4", issue23Builder.getUnknownFields().hasField(4));
77+
}
4778
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package issue23;
2+
3+
option optimize_for = SPEED;
4+
5+
option java_package = "com.googlecode.protobuf.format.issue23";
6+
7+
message MsgWithUnknownFields {
8+
optional string leaf1 = 1;
9+
optional int32 leaf2 = 2;
10+
repeated int32 leaf3 = 3;
11+
}

0 commit comments

Comments
 (0)