1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
9
// * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
15
// * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
package com.google.protobuf;
33
import protobuf_unittest.UnittestProto.TestAllTypes;
34
import protobuf_unittest.UnittestProto.TestAllExtensions;
35
import protobuf_unittest.UnittestProto.TestPackedTypes;
37
import junit.framework.TestCase;
38
import java.util.Arrays;
41
* Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which
42
* tests some {@link DynamicMessage} functionality.
44
* @author kenton@google.com Kenton Varda
46
public class DynamicMessageTest extends TestCase {
47
TestUtil.ReflectionTester reflectionTester =
48
new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null);
50
TestUtil.ReflectionTester extensionsReflectionTester =
51
new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(),
52
TestUtil.getExtensionRegistry());
53
TestUtil.ReflectionTester packedReflectionTester =
54
new TestUtil.ReflectionTester(TestPackedTypes.getDescriptor(), null);
56
public void testDynamicMessageAccessors() throws Exception {
57
Message.Builder builder =
58
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
59
reflectionTester.setAllFieldsViaReflection(builder);
60
Message message = builder.build();
61
reflectionTester.assertAllFieldsSetViaReflection(message);
64
public void testDoubleBuildError() throws Exception {
65
Message.Builder builder =
66
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
70
fail("Should have thrown exception.");
71
} catch (IllegalStateException e) {
76
public void testClearAfterBuildError() throws Exception {
77
Message.Builder builder =
78
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
82
fail("Should have thrown exception.");
83
} catch (IllegalStateException e) {
88
public void testDynamicMessageSettersRejectNull() throws Exception {
89
Message.Builder builder =
90
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
91
reflectionTester.assertReflectionSettersRejectNull(builder);
94
public void testDynamicMessageExtensionAccessors() throws Exception {
95
// We don't need to extensively test DynamicMessage's handling of
96
// extensions because, frankly, it doesn't do anything special with them.
97
// It treats them just like any other fields.
98
Message.Builder builder =
99
DynamicMessage.newBuilder(TestAllExtensions.getDescriptor());
100
extensionsReflectionTester.setAllFieldsViaReflection(builder);
101
Message message = builder.build();
102
extensionsReflectionTester.assertAllFieldsSetViaReflection(message);
105
public void testDynamicMessageExtensionSettersRejectNull() throws Exception {
106
Message.Builder builder =
107
DynamicMessage.newBuilder(TestAllExtensions.getDescriptor());
108
extensionsReflectionTester.assertReflectionSettersRejectNull(builder);
111
public void testDynamicMessageRepeatedSetters() throws Exception {
112
Message.Builder builder =
113
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
114
reflectionTester.setAllFieldsViaReflection(builder);
115
reflectionTester.modifyRepeatedFieldsViaReflection(builder);
116
Message message = builder.build();
117
reflectionTester.assertRepeatedFieldsModifiedViaReflection(message);
120
public void testDynamicMessageRepeatedSettersRejectNull() throws Exception {
121
Message.Builder builder =
122
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
123
reflectionTester.assertReflectionRepeatedSettersRejectNull(builder);
126
public void testDynamicMessageDefaults() throws Exception {
127
reflectionTester.assertClearViaReflection(
128
DynamicMessage.getDefaultInstance(TestAllTypes.getDescriptor()));
129
reflectionTester.assertClearViaReflection(
130
DynamicMessage.newBuilder(TestAllTypes.getDescriptor()).build());
133
public void testDynamicMessageSerializedSize() throws Exception {
134
TestAllTypes message = TestUtil.getAllSet();
136
Message.Builder dynamicBuilder =
137
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
138
reflectionTester.setAllFieldsViaReflection(dynamicBuilder);
139
Message dynamicMessage = dynamicBuilder.build();
141
assertEquals(message.getSerializedSize(),
142
dynamicMessage.getSerializedSize());
145
public void testDynamicMessageSerialization() throws Exception {
146
Message.Builder builder =
147
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
148
reflectionTester.setAllFieldsViaReflection(builder);
149
Message message = builder.build();
151
ByteString rawBytes = message.toByteString();
152
TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes);
154
TestUtil.assertAllFieldsSet(message2);
156
// In fact, the serialized forms should be exactly the same, byte-for-byte.
157
assertEquals(TestUtil.getAllSet().toByteString(), rawBytes);
160
public void testDynamicMessageParsing() throws Exception {
161
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
162
TestUtil.setAllFields(builder);
163
TestAllTypes message = builder.build();
165
ByteString rawBytes = message.toByteString();
168
DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), rawBytes);
169
reflectionTester.assertAllFieldsSetViaReflection(message2);
172
public void testDynamicMessagePackedSerialization() throws Exception {
173
Message.Builder builder =
174
DynamicMessage.newBuilder(TestPackedTypes.getDescriptor());
175
packedReflectionTester.setPackedFieldsViaReflection(builder);
176
Message message = builder.build();
178
ByteString rawBytes = message.toByteString();
179
TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes);
181
TestUtil.assertPackedFieldsSet(message2);
183
// In fact, the serialized forms should be exactly the same, byte-for-byte.
184
assertEquals(TestUtil.getPackedSet().toByteString(), rawBytes);
187
public void testDynamicMessagePackedParsing() throws Exception {
188
TestPackedTypes.Builder builder = TestPackedTypes.newBuilder();
189
TestUtil.setPackedFields(builder);
190
TestPackedTypes message = builder.build();
192
ByteString rawBytes = message.toByteString();
195
DynamicMessage.parseFrom(TestPackedTypes.getDescriptor(), rawBytes);
196
packedReflectionTester.assertPackedFieldsSetViaReflection(message2);
199
public void testDynamicMessageCopy() throws Exception {
200
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
201
TestUtil.setAllFields(builder);
202
TestAllTypes message = builder.build();
204
DynamicMessage copy = DynamicMessage.newBuilder(message).build();
205
reflectionTester.assertAllFieldsSetViaReflection(copy);
208
public void testToBuilder() throws Exception {
209
DynamicMessage.Builder builder =
210
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
211
reflectionTester.setAllFieldsViaReflection(builder);
212
int unknownFieldNum = 9;
213
long unknownFieldVal = 90;
214
builder.setUnknownFields(UnknownFieldSet.newBuilder()
215
.addField(unknownFieldNum,
216
UnknownFieldSet.Field.newBuilder()
217
.addVarint(unknownFieldVal).build())
219
DynamicMessage message = builder.build();
221
DynamicMessage derived = message.toBuilder().build();
222
reflectionTester.assertAllFieldsSetViaReflection(derived);
223
assertEquals(Arrays.asList(unknownFieldVal),
224
derived.getUnknownFields().getField(unknownFieldNum).getVarintList());