@Test public void testMoreUnknown() throws Exception { UnknownFieldSet unknownGroupLevel2 = UnknownFieldSet.newBuilder() .addField(16, UnknownFieldSet.Field.newBuilder().addVarint(566667).build()) .build(); UnknownFieldSet unknownGroup = UnknownFieldSet.newBuilder() .addField(11, UnknownFieldSet.Field.newBuilder().addVarint(566667).build()) .addField(15, UnknownFieldSet.Field.newBuilder().addGroup(unknownGroupLevel2).build()) .build(); ByteString bs = ByteString.copyFromUtf8("testUnknown"); OneString data = OneString.newBuilder() .setUnknownFields( UnknownFieldSet.newBuilder() .addField(5, UnknownFieldSet.Field.newBuilder().addFixed32(999).build()) .addField(6, UnknownFieldSet.Field.newBuilder().addGroup(unknownGroup).build()) .addField(7, UnknownFieldSet.Field.newBuilder().addLengthDelimited(bs).build()) .build()) .setData("12345") .build(); String javaText = formatter.printToString(data); // System.out.println(javaText); OneString.Builder builder = OneString.newBuilder(); formatter.merge(TextUtils.toInputStream(javaText), builder); assertEquals(data.getData(), builder.build().getData()); }
@Test public void testChineseCharacters() throws Exception { String data = "検索jan5検索[email protected]"; String testString = "{\"data\":\"" + data + "\"}"; OneString.Builder builder = OneString.newBuilder(); formatter.merge(TextUtils.toInputStream(testString, Charset.forName("UTF-8")), builder); OneString msg = builder.build(); // System.out.println(msg.getData()); assertEquals(data, msg.getData()); }
@Test public void testStringValueContainsSurrogatePair() throws Exception { String testString = new String(Character.toChars(0x1D11E)); OneString msg = OneString.newBuilder().setData(testString).build(); String json = formatter.printToString(msg); // Assert that the surrogate pair was encoded assertEquals("{\"data\":\"\\uD834\\uDD1E\"}", json); // Assert that we can read the string back into a msg OneString.Builder builder = OneString.newBuilder(); formatter.merge(TextUtils.toInputStream(json), builder); assertEquals(msg, builder.build()); }
@Test public void testStringValueContainsCharactersThatShouldBeEscaped() throws Exception { // input string is \"' String testString = "\\\"'"; OneString msg = OneString.newBuilder().setData(testString).build(); String json = formatter.printToString(msg); // Assert that reverse-solidus and double quotes where escaped using a reverse-solidus // Expected string is {"name": "\\\"'"} assertEquals("{\"data\":\"\\\\\\\"\'\"}", json); // Assert that we can read the string back into a msg OneString.Builder builder = OneString.newBuilder(); formatter.merge(TextUtils.toInputStream(json), builder); assertEquals(msg, builder.build()); }
@Test public void testStringValueContainsControlCharacters() throws Exception { char[] ctrlChars = new char[0x001F + 1]; for (char c = 0; c < 0x001F + 1; c++) { ctrlChars[c] = c; } String testString = new String(ctrlChars); OneString msg = OneString.newBuilder().setData(testString).build(); String json = formatter.printToString(msg); // Assert that we can read the string back into a msg OneString.Builder builder = OneString.newBuilder(); formatter.merge(TextUtils.toInputStream(json), builder); OneString item = builder.build(); assertEquals(msg, item); assertTrue(Arrays.equals(ctrlChars, item.getData().toCharArray())); }