31
31
package com.google.protobuf;
33
33
import junit.framework.TestCase;
35
import java.io.ByteArrayInputStream;
36
import java.io.ByteArrayOutputStream;
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);
66
public void testSerializationPacked() throws Exception {
67
TestPackedTypes message = TestUtil.getPackedSet();
69
ByteString rawBytes = message.toByteString();
70
assertEquals(rawBytes.size(), message.getSerializedSize());
72
TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes);
74
TestUtil.assertPackedFieldsSet(message2);
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
65
82
TestAllExtensions message = TestUtil.getAllExtensionsSet();
71
88
TestUtil.assertAllFieldsSet(message2);
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();
97
TestPackedTypes message2 = TestUtil.getPackedSet();
98
ByteString rawBytes2 = message2.toByteString();
100
assertEquals(rawBytes, rawBytes2);
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.
79
108
TestAllTypes message = TestUtil.getAllSet();
80
109
ByteString rawBytes = message.toByteString();
82
ExtensionRegistry registry = ExtensionRegistry.newInstance();
83
TestUtil.registerAllExtensions(registry);
84
registry = registry.getUnmodifiable();
111
ExtensionRegistry registry = TestUtil.getExtensionRegistry();
86
113
TestAllExtensions message2 =
87
114
TestAllExtensions.parseFrom(rawBytes, registry);
89
116
TestUtil.assertAllExtensionsSet(message2);
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();
124
ExtensionRegistry registry = TestUtil.getExtensionRegistry();
126
TestPackedExtensions message2 =
127
TestPackedExtensions.parseFrom(rawBytes, registry);
129
TestUtil.assertPackedExtensionsSet(message2);
92
132
public void testExtensionsSerializedSize() throws Exception {
93
133
assertEquals(TestUtil.getAllSet().getSerializedSize(),
94
134
TestUtil.getAllExtensionsSet().getSerializedSize());
137
public void testSerializeDelimited() throws Exception {
138
ByteArrayOutputStream output = new ByteArrayOutputStream();
139
TestUtil.getAllSet().writeDelimitedTo(output);
141
TestUtil.getPackedSet().writeDelimitedTo(output);
144
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
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());
97
153
private void assertFieldsInOrder(ByteString data) throws Exception {
98
154
CodedInputStream input = data.newCodedInput();
99
155
int previousTag = 0;