11package com .googlecode .protobuf .format ;
22
3+ import com .google .protobuf .ByteString ;
4+ import com .google .protobuf .ExtensionRegistry ;
35import com .google .protobuf .Message ;
6+ import com .google .protobuf .UnknownFieldSet ;
7+ import com .googlecode .protobuf .format .issue23 .Issue23 ;
48import org .testng .annotations .DataProvider ;
59import org .testng .annotations .Test ;
610import org .testng .reporters .Files ;
711import protobuf_unittest .UnittestProto ;
12+ import sun .nio .cs .StandardCharsets ;
813
914import java .io .IOException ;
15+ import java .io .StringBufferInputStream ;
16+ import java .nio .charset .Charset ;
1017import java .util .Arrays ;
1118
1219import static org .hamcrest .CoreMatchers .is ;
1926public 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}
0 commit comments