This source file includes following definitions.
- testDynamicMessageAccessors
- testSettersAfterBuild
- testUnknownFields
- testDynamicMessageSettersRejectNull
- testDynamicMessageExtensionAccessors
- testDynamicMessageExtensionSettersRejectNull
- testDynamicMessageRepeatedSetters
- testDynamicMessageRepeatedSettersRejectNull
- testDynamicMessageDefaults
- testDynamicMessageSerializedSize
- testDynamicMessageSerialization
- testDynamicMessageParsing
- testDynamicMessageExtensionParsing
- testDynamicMessagePackedSerialization
- testDynamicMessagePackedParsing
- testDynamicMessageCopy
- testToBuilder
package com.google.protobuf;
import protobuf_unittest.UnittestProto.TestAllExtensions;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestEmptyMessage;
import protobuf_unittest.UnittestProto.TestPackedTypes;
import junit.framework.TestCase;
import java.util.Arrays;
public class DynamicMessageTest extends TestCase {
TestUtil.ReflectionTester reflectionTester =
new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null);
TestUtil.ReflectionTester extensionsReflectionTester =
new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(),
TestUtil.getExtensionRegistry());
TestUtil.ReflectionTester packedReflectionTester =
new TestUtil.ReflectionTester(TestPackedTypes.getDescriptor(), null);
public void testDynamicMessageAccessors() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.setAllFieldsViaReflection(builder);
Message message = builder.build();
reflectionTester.assertAllFieldsSetViaReflection(message);
}
public void testSettersAfterBuild() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
Message firstMessage = builder.build();
builder.build();
builder.clear();
reflectionTester.setAllFieldsViaReflection(builder);
Message message = builder.build();
reflectionTester.assertAllFieldsSetViaReflection(message);
reflectionTester.modifyRepeatedFieldsViaReflection(builder);
message = builder.build();
reflectionTester.assertRepeatedFieldsModifiedViaReflection(message);
reflectionTester.assertClearViaReflection(firstMessage);
}
public void testUnknownFields() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestEmptyMessage.getDescriptor());
builder.setUnknownFields(UnknownFieldSet.newBuilder()
.addField(1, UnknownFieldSet.Field.newBuilder().addVarint(1).build())
.addField(2, UnknownFieldSet.Field.newBuilder().addFixed32(1).build())
.build());
Message message = builder.build();
assertEquals(2, message.getUnknownFields().asMap().size());
Message.Builder newBuilder = builder.clone();
assertEquals(2, newBuilder.getUnknownFields().asMap().size());
newBuilder.clear();
assertTrue(newBuilder.getUnknownFields().asMap().isEmpty());
newBuilder.mergeFrom(message.toByteString());
assertEquals(2, newBuilder.getUnknownFields().asMap().size());
}
public void testDynamicMessageSettersRejectNull() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.assertReflectionSettersRejectNull(builder);
}
public void testDynamicMessageExtensionAccessors() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllExtensions.getDescriptor());
extensionsReflectionTester.setAllFieldsViaReflection(builder);
Message message = builder.build();
extensionsReflectionTester.assertAllFieldsSetViaReflection(message);
}
public void testDynamicMessageExtensionSettersRejectNull() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllExtensions.getDescriptor());
extensionsReflectionTester.assertReflectionSettersRejectNull(builder);
}
public void testDynamicMessageRepeatedSetters() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.setAllFieldsViaReflection(builder);
reflectionTester.modifyRepeatedFieldsViaReflection(builder);
Message message = builder.build();
reflectionTester.assertRepeatedFieldsModifiedViaReflection(message);
}
public void testDynamicMessageRepeatedSettersRejectNull() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.assertReflectionRepeatedSettersRejectNull(builder);
}
public void testDynamicMessageDefaults() throws Exception {
reflectionTester.assertClearViaReflection(
DynamicMessage.getDefaultInstance(TestAllTypes.getDescriptor()));
reflectionTester.assertClearViaReflection(
DynamicMessage.newBuilder(TestAllTypes.getDescriptor()).build());
}
public void testDynamicMessageSerializedSize() throws Exception {
TestAllTypes message = TestUtil.getAllSet();
Message.Builder dynamicBuilder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.setAllFieldsViaReflection(dynamicBuilder);
Message dynamicMessage = dynamicBuilder.build();
assertEquals(message.getSerializedSize(),
dynamicMessage.getSerializedSize());
}
public void testDynamicMessageSerialization() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.setAllFieldsViaReflection(builder);
Message message = builder.build();
ByteString rawBytes = message.toByteString();
TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes);
TestUtil.assertAllFieldsSet(message2);
assertEquals(TestUtil.getAllSet().toByteString(), rawBytes);
}
public void testDynamicMessageParsing() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TestUtil.setAllFields(builder);
TestAllTypes message = builder.build();
ByteString rawBytes = message.toByteString();
Message message2 =
DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), rawBytes);
reflectionTester.assertAllFieldsSetViaReflection(message2);
Message message3 = message2.getParserForType().parseFrom(rawBytes);
reflectionTester.assertAllFieldsSetViaReflection(message3);
}
public void testDynamicMessageExtensionParsing() throws Exception {
ByteString rawBytes = TestUtil.getAllExtensionsSet().toByteString();
Message message = DynamicMessage.parseFrom(
TestAllExtensions.getDescriptor(), rawBytes,
TestUtil.getExtensionRegistry());
extensionsReflectionTester.assertAllFieldsSetViaReflection(message);
Message message2 = message.getParserForType().parseFrom(
rawBytes, TestUtil.getExtensionRegistry());
extensionsReflectionTester.assertAllFieldsSetViaReflection(message2);
}
public void testDynamicMessagePackedSerialization() throws Exception {
Message.Builder builder =
DynamicMessage.newBuilder(TestPackedTypes.getDescriptor());
packedReflectionTester.setPackedFieldsViaReflection(builder);
Message message = builder.build();
ByteString rawBytes = message.toByteString();
TestPackedTypes message2 = TestPackedTypes.parseFrom(rawBytes);
TestUtil.assertPackedFieldsSet(message2);
assertEquals(TestUtil.getPackedSet().toByteString(), rawBytes);
}
public void testDynamicMessagePackedParsing() throws Exception {
TestPackedTypes.Builder builder = TestPackedTypes.newBuilder();
TestUtil.setPackedFields(builder);
TestPackedTypes message = builder.build();
ByteString rawBytes = message.toByteString();
Message message2 =
DynamicMessage.parseFrom(TestPackedTypes.getDescriptor(), rawBytes);
packedReflectionTester.assertPackedFieldsSetViaReflection(message2);
Message message3 = message2.getParserForType().parseFrom(rawBytes);
packedReflectionTester.assertPackedFieldsSetViaReflection(message3);
}
public void testDynamicMessageCopy() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TestUtil.setAllFields(builder);
TestAllTypes message = builder.build();
DynamicMessage copy = DynamicMessage.newBuilder(message).build();
reflectionTester.assertAllFieldsSetViaReflection(copy);
}
public void testToBuilder() throws Exception {
DynamicMessage.Builder builder =
DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
reflectionTester.setAllFieldsViaReflection(builder);
int unknownFieldNum = 9;
long unknownFieldVal = 90;
builder.setUnknownFields(UnknownFieldSet.newBuilder()
.addField(unknownFieldNum,
UnknownFieldSet.Field.newBuilder()
.addVarint(unknownFieldVal).build())
.build());
DynamicMessage message = builder.build();
DynamicMessage derived = message.toBuilder().build();
reflectionTester.assertAllFieldsSetViaReflection(derived);
assertEquals(Arrays.asList(unknownFieldVal),
derived.getUnknownFields().getField(unknownFieldNum).getVarintList());
}
}