~ubuntu-branches/ubuntu/trusty/protobuf/trusty-proposed

« back to all changes in this revision

Viewing changes to java/src/test/java/com/google/protobuf/TextFormatTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
package com.google.protobuf;
32
32
 
33
33
import com.google.protobuf.Descriptors.FieldDescriptor;
 
34
import protobuf_unittest.UnittestMset.TestMessageSet;
 
35
import protobuf_unittest.UnittestMset.TestMessageSetExtension1;
 
36
import protobuf_unittest.UnittestMset.TestMessageSetExtension2;
34
37
import protobuf_unittest.UnittestProto.OneString;
 
38
import protobuf_unittest.UnittestProto.TestAllExtensions;
35
39
import protobuf_unittest.UnittestProto.TestAllTypes;
36
 
import protobuf_unittest.UnittestProto.TestAllExtensions;
 
40
import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage;
37
41
import protobuf_unittest.UnittestProto.TestEmptyMessage;
38
 
import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage;
39
 
import protobuf_unittest.UnittestMset.TestMessageSet;
40
 
import protobuf_unittest.UnittestMset.TestMessageSetExtension1;
41
 
import protobuf_unittest.UnittestMset.TestMessageSetExtension2;
42
42
 
43
43
import junit.framework.TestCase;
44
44
 
60
60
 
61
61
  // A representation of the above string with all the characters escaped.
62
62
  private final static String kEscapeTestStringEscaped =
63
 
      "\"\\\"A string with \\' characters \\n and \\r newlines "
64
 
          + "and \\t tabs and \\001 slashes \\\\\"";
 
63
      "\\\"A string with \\' characters \\n and \\r newlines "
 
64
          + "and \\t tabs and \\001 slashes \\\\";
65
65
 
66
66
  private static String allFieldsSetText = TestUtil.readTextFromFile(
67
67
    "text_format_unittest_data.txt");
133
133
    assertEquals(allExtensionsSetText, javaText);
134
134
  }
135
135
 
 
136
  // Creates an example unknown field set.
 
137
  private UnknownFieldSet makeUnknownFieldSet() {
 
138
    return UnknownFieldSet.newBuilder()
 
139
        .addField(5,
 
140
            UnknownFieldSet.Field.newBuilder()
 
141
            .addVarint(1)
 
142
            .addFixed32(2)
 
143
            .addFixed64(3)
 
144
            .addLengthDelimited(ByteString.copyFromUtf8("4"))
 
145
            .addGroup(
 
146
                UnknownFieldSet.newBuilder()
 
147
                .addField(10,
 
148
                    UnknownFieldSet.Field.newBuilder()
 
149
                    .addVarint(5)
 
150
                    .build())
 
151
                .build())
 
152
            .build())
 
153
        .addField(8,
 
154
            UnknownFieldSet.Field.newBuilder()
 
155
            .addVarint(1)
 
156
            .addVarint(2)
 
157
            .addVarint(3)
 
158
            .build())
 
159
        .addField(15,
 
160
            UnknownFieldSet.Field.newBuilder()
 
161
            .addVarint(0xABCDEF1234567890L)
 
162
            .addFixed32(0xABCD1234)
 
163
            .addFixed64(0xABCDEF1234567890L)
 
164
            .build())
 
165
        .build();
 
166
  }
 
167
 
136
168
  public void testPrintUnknownFields() throws Exception {
137
169
    // Test printing of unknown fields in a message.
138
170
 
139
171
    TestEmptyMessage message =
140
172
      TestEmptyMessage.newBuilder()
141
 
        .setUnknownFields(
142
 
          UnknownFieldSet.newBuilder()
143
 
            .addField(5,
144
 
              UnknownFieldSet.Field.newBuilder()
145
 
                .addVarint(1)
146
 
                .addFixed32(2)
147
 
                .addFixed64(3)
148
 
                .addLengthDelimited(ByteString.copyFromUtf8("4"))
149
 
                .addGroup(
150
 
                  UnknownFieldSet.newBuilder()
151
 
                    .addField(10,
152
 
                      UnknownFieldSet.Field.newBuilder()
153
 
                        .addVarint(5)
154
 
                        .build())
155
 
                    .build())
156
 
                .build())
157
 
            .addField(8,
158
 
              UnknownFieldSet.Field.newBuilder()
159
 
                .addVarint(1)
160
 
                .addVarint(2)
161
 
                .addVarint(3)
162
 
                .build())
163
 
            .addField(15,
164
 
              UnknownFieldSet.Field.newBuilder()
165
 
                .addVarint(0xABCDEF1234567890L)
166
 
                .addFixed32(0xABCD1234)
167
 
                .addFixed64(0xABCDEF1234567890L)
168
 
                .build())
169
 
            .build())
 
173
        .setUnknownFields(makeUnknownFieldSet())
170
174
        .build();
171
175
 
172
176
    assertEquals(
409
413
      "1:16: Expected \"true\" or \"false\".",
410
414
      "optional_bool: maybe");
411
415
    assertParseError(
 
416
      "1:16: Expected \"true\" or \"false\".",
 
417
      "optional_bool: 2");
 
418
    assertParseError(
412
419
      "1:18: Expected string.",
413
420
      "optional_string: 123");
414
421
    assertParseError(
469
476
      TextFormat.unescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
470
477
    assertEquals("\0\001\007\b\f\n\r\t\013\\\'\"",
471
478
      TextFormat.unescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
 
479
    assertEquals(kEscapeTestStringEscaped,
 
480
      TextFormat.escapeText(kEscapeTestString));
 
481
    assertEquals(kEscapeTestString,
 
482
      TextFormat.unescapeText(kEscapeTestStringEscaped));
472
483
 
473
484
    // Unicode handling.
474
485
    assertEquals("\\341\\210\\264", TextFormat.escapeText("\u1234"));
481
492
    assertEquals(bytes(0xe1, 0x88, 0xb4),
482
493
                 TextFormat.unescapeBytes("\\xe1\\x88\\xb4"));
483
494
 
 
495
    // Handling of strings with unescaped Unicode characters > 255.
 
496
    final String zh = "\u9999\u6e2f\u4e0a\u6d77\ud84f\udf80\u8c50\u9280\u884c";
 
497
    ByteString zhByteString = ByteString.copyFromUtf8(zh);
 
498
    assertEquals(zhByteString, TextFormat.unescapeBytes(zh));
 
499
 
484
500
    // Errors.
485
501
    try {
486
502
      TextFormat.unescapeText("\\x");
622
638
    }
623
639
  }
624
640
 
 
641
  public void testParseString() throws Exception {
 
642
    final String zh = "\u9999\u6e2f\u4e0a\u6d77\ud84f\udf80\u8c50\u9280\u884c";
 
643
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
 
644
    TextFormat.merge("optional_string: \"" + zh + "\"", builder);
 
645
    assertEquals(zh, builder.getOptionalString());
 
646
  }
 
647
 
625
648
  public void testParseLongString() throws Exception {
626
649
    String longText =
627
650
      "123456789012345678901234567890123456789012345678901234567890" +
650
673
    assertEquals(longText, builder.getOptionalString());
651
674
  }
652
675
 
 
676
  public void testParseBoolean() throws Exception {
 
677
    String goodText =
 
678
        "repeated_bool: t  repeated_bool : 0\n" +
 
679
        "repeated_bool :f repeated_bool:1";
 
680
    String goodTextCanonical =
 
681
        "repeated_bool: true\n" +
 
682
        "repeated_bool: false\n" +
 
683
        "repeated_bool: false\n" +
 
684
        "repeated_bool: true\n";
 
685
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
 
686
    TextFormat.merge(goodText, builder);
 
687
    assertEquals(goodTextCanonical, builder.build().toString());
 
688
 
 
689
    try {
 
690
      TestAllTypes.Builder badBuilder = TestAllTypes.newBuilder();
 
691
      TextFormat.merge("optional_bool:2", badBuilder);
 
692
      fail("Should have thrown an exception.");
 
693
    } catch (TextFormat.ParseException e) {
 
694
      // success
 
695
    }
 
696
    try {
 
697
      TestAllTypes.Builder badBuilder = TestAllTypes.newBuilder();
 
698
      TextFormat.merge("optional_bool: foo", badBuilder);
 
699
      fail("Should have thrown an exception.");
 
700
    } catch (TextFormat.ParseException e) {
 
701
      // success
 
702
    }
 
703
  }
 
704
 
653
705
  public void testParseAdjacentStringLiterals() throws Exception {
654
706
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
655
707
    TextFormat.merge("optional_string: \"foo\" 'corge' \"grault\"", builder);
656
708
    assertEquals("foocorgegrault", builder.getOptionalString());
657
709
  }
 
710
 
 
711
  public void testPrintFieldValue() throws Exception {
 
712
    assertPrintFieldValue("\"Hello\"", "Hello", "repeated_string");
 
713
    assertPrintFieldValue("123.0",  123f, "repeated_float");
 
714
    assertPrintFieldValue("123.0",  123d, "repeated_double");
 
715
    assertPrintFieldValue("123",  123, "repeated_int32");
 
716
    assertPrintFieldValue("123",  123L, "repeated_int64");
 
717
    assertPrintFieldValue("true",  true, "repeated_bool");
 
718
    assertPrintFieldValue("4294967295", 0xFFFFFFFF, "repeated_uint32");
 
719
    assertPrintFieldValue("18446744073709551615",  0xFFFFFFFFFFFFFFFFL,
 
720
        "repeated_uint64");
 
721
    assertPrintFieldValue("\"\\001\\002\\003\"",
 
722
        ByteString.copyFrom(new byte[] {1, 2, 3}), "repeated_bytes");
 
723
  }
 
724
 
 
725
  private void assertPrintFieldValue(String expect, Object value,
 
726
      String fieldName) throws Exception {
 
727
    TestAllTypes.Builder builder = TestAllTypes.newBuilder();
 
728
    StringBuilder sb = new StringBuilder();
 
729
    TextFormat.printFieldValue(
 
730
        TestAllTypes.getDescriptor().findFieldByName(fieldName),
 
731
        value, sb);
 
732
    assertEquals(expect, sb.toString());
 
733
  }
 
734
 
 
735
  public void testShortDebugString() {
 
736
    assertEquals("optional_nested_message { bb: 42 } repeated_int32: 1"
 
737
        + " repeated_uint32: 2",
 
738
        TextFormat.shortDebugString(TestAllTypes.newBuilder()
 
739
            .addRepeatedInt32(1)
 
740
            .addRepeatedUint32(2)
 
741
            .setOptionalNestedMessage(
 
742
                NestedMessage.newBuilder().setBb(42).build())
 
743
            .build()));
 
744
  }
 
745
 
 
746
  public void testShortDebugString_unknown() {
 
747
    assertEquals("5: 1 5: 0x00000002 5: 0x0000000000000003 5: \"4\" 5 { 10: 5 }"
 
748
        + " 8: 1 8: 2 8: 3 15: 12379813812177893520 15: 0xabcd1234 15:"
 
749
        + " 0xabcdef1234567890",
 
750
        TextFormat.shortDebugString(makeUnknownFieldSet()));
 
751
  }
658
752
}