~statik/ubuntu/maverick/protobuf/A

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2009-11-16 10:41:33 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091116104133-ykhy3deg5l4975tw
Tags: 2.1.0-1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Disable the death tests on IA64, now as a quilt patch.
  - Don't use python2.4, also as a quilt patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
package com.google.protobuf;
32
32
 
33
33
import junit.framework.TestCase;
 
34
 
 
35
import java.io.ByteArrayInputStream;
 
36
import java.io.ByteArrayOutputStream;
 
37
 
34
38
import protobuf_unittest.UnittestProto;
 
39
import protobuf_unittest.UnittestProto.TestAllExtensions;
35
40
import protobuf_unittest.UnittestProto.TestAllTypes;
36
 
import protobuf_unittest.UnittestProto.TestAllExtensions;
37
41
import protobuf_unittest.UnittestProto.TestFieldOrderings;
 
42
import protobuf_unittest.UnittestProto.TestPackedExtensions;
 
43
import protobuf_unittest.UnittestProto.TestPackedTypes;
38
44
import protobuf_unittest.UnittestMset.TestMessageSet;
39
45
import protobuf_unittest.UnittestMset.RawMessageSet;
40
46
import protobuf_unittest.UnittestMset.TestMessageSetExtension1;
57
63
    TestUtil.assertAllFieldsSet(message2);
58
64
  }
59
65
 
 
66
  public void testSerializationPacked() throws Exception {
 
67
    TestPackedTypes message = TestUtil.getPackedSet();
 
68
 
 
69
    ByteString rawBytes = message.toByteString();
 
70
    assertEquals(rawBytes.size(), message.getSerializedSize());
 
71
 
 
72
    TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes);
 
73
 
 
74
    TestUtil.assertPackedFieldsSet(message2);
 
75
  }
 
76
 
60
77
  public void testSerializeExtensions() throws Exception {
61
78
    // TestAllTypes and TestAllExtensions should have compatible wire formats,
62
 
    // so if we serealize a TestAllExtensions then parse it as TestAllTypes
 
79
    // so if we serialize a TestAllExtensions then parse it as TestAllTypes
63
80
    // it should work.
64
81
 
65
82
    TestAllExtensions message = TestUtil.getAllExtensionsSet();
71
88
    TestUtil.assertAllFieldsSet(message2);
72
89
  }
73
90
 
 
91
  public void testSerializePackedExtensions() throws Exception {
 
92
    // TestPackedTypes and TestPackedExtensions should have compatible wire
 
93
    // formats; check that they serialize to the same string.
 
94
    TestPackedExtensions message = TestUtil.getPackedExtensionsSet();
 
95
    ByteString rawBytes = message.toByteString();
 
96
 
 
97
    TestPackedTypes message2 = TestUtil.getPackedSet();
 
98
    ByteString rawBytes2 = message2.toByteString();
 
99
 
 
100
    assertEquals(rawBytes, rawBytes2);
 
101
  }
 
102
 
74
103
  public void testParseExtensions() throws Exception {
75
104
    // TestAllTypes and TestAllExtensions should have compatible wire formats,
76
 
    // so if we serealize a TestAllTypes then parse it as TestAllExtensions
 
105
    // so if we serialize a TestAllTypes then parse it as TestAllExtensions
77
106
    // it should work.
78
107
 
79
108
    TestAllTypes message = TestUtil.getAllSet();
80
109
    ByteString rawBytes = message.toByteString();
81
110
 
82
 
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
83
 
    TestUtil.registerAllExtensions(registry);
84
 
    registry = registry.getUnmodifiable();
 
111
    ExtensionRegistry registry = TestUtil.getExtensionRegistry();
85
112
 
86
113
    TestAllExtensions message2 =
87
114
      TestAllExtensions.parseFrom(rawBytes, registry);
89
116
    TestUtil.assertAllExtensionsSet(message2);
90
117
  }
91
118
 
 
119
  public void testParsePackedExtensions() throws Exception {
 
120
    // Ensure that packed extensions can be properly parsed.
 
121
    TestPackedExtensions message = TestUtil.getPackedExtensionsSet();
 
122
    ByteString rawBytes = message.toByteString();
 
123
 
 
124
    ExtensionRegistry registry = TestUtil.getExtensionRegistry();
 
125
 
 
126
    TestPackedExtensions message2 =
 
127
        TestPackedExtensions.parseFrom(rawBytes, registry);
 
128
 
 
129
    TestUtil.assertPackedExtensionsSet(message2);
 
130
  }
 
131
 
92
132
  public void testExtensionsSerializedSize() throws Exception {
93
133
    assertEquals(TestUtil.getAllSet().getSerializedSize(),
94
134
                 TestUtil.getAllExtensionsSet().getSerializedSize());
95
135
  }
96
136
 
 
137
  public void testSerializeDelimited() throws Exception {
 
138
    ByteArrayOutputStream output = new ByteArrayOutputStream();
 
139
    TestUtil.getAllSet().writeDelimitedTo(output);
 
140
    output.write(12);
 
141
    TestUtil.getPackedSet().writeDelimitedTo(output);
 
142
    output.write(34);
 
143
 
 
144
    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
 
145
 
 
146
    TestUtil.assertAllFieldsSet(TestAllTypes.parseDelimitedFrom(input));
 
147
    assertEquals(12, input.read());
 
148
    TestUtil.assertPackedFieldsSet(TestPackedTypes.parseDelimitedFrom(input));
 
149
    assertEquals(34, input.read());
 
150
    assertEquals(-1, input.read());
 
151
  }
 
152
 
97
153
  private void assertFieldsInOrder(ByteString data) throws Exception {
98
154
    CodedInputStream input = data.newCodedInput();
99
155
    int previousTag = 0;
279
335
    assertEquals("bar", field.getLengthDelimitedList().get(0).toStringUtf8());
280
336
  }
281
337
}
282