summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorAurash Mahbod <aurash@google.com>2013-08-14 14:09:52 -0700
committerMax Cai <maxtroy@google.com>2013-11-05 18:17:06 +0000
commitbeb57e08a44a140bf52235717f1f907ca857f360 (patch)
tree461e3f0b97a639536d0b3b34e748eeefc8890917 /java
parentbb971d53626cb286f8dc491c15d2731001c4891b (diff)
downloadexternal_protobuf-beb57e08a44a140bf52235717f1f907ca857f360.zip
external_protobuf-beb57e08a44a140bf52235717f1f907ca857f360.tar.gz
external_protobuf-beb57e08a44a140bf52235717f1f907ca857f360.tar.bz2
Allow for ref-type arrays containing null elements.
Strip the null elements out before serializing the array. This is helpful in the cases where the user wants to construct an array of an inexact size for serialization. For example: User constructs array of size 5 because they anticipate adding more than 1 element before serialization. Only 3 get added, so the array looks like [Obj, Obj, Obj, null, null]. This would curently crash without this CL. All repeated fields of ref-type elements can contain null elements: repeated strings, repeated bytes, and repeated messages/groups. Change-Id: I117391c868c9a436536d70d6151780e9cc7e8227 Conflicts: src/google/protobuf/compiler/javanano/javanano_message_field.cc
Diffstat (limited to 'java')
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java33
1 files changed, 31 insertions, 2 deletions
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index b9061e9..68d2c45 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -2724,8 +2724,6 @@ public class NanoTest extends TestCase {
// Complete equality for messages with accessors:
TestNanoAccessors f = createMessageWithAccessorsForHashCodeEqualsTest();
TestNanoAccessors fEquivalent = createMessageWithAccessorsForHashCodeEqualsTest();
- System.out.println("equals: " + f.equals(fEquivalent));
- System.out.println("hashCode: " + f.hashCode() + " vs " + fEquivalent.hashCode());
// If using accessors, explicitly setting a field to its default value
// should make the message different.
@@ -2965,6 +2963,37 @@ public class NanoTest extends TestCase {
assertEquals(TestAllTypesNano.BAR, message.repeatedPackedNestedEnum[1]);
}
+ public void testNullRepeatedFieldElements() throws Exception {
+ // Check that serialization with null array elements doesn't NPE.
+ String string1 = "1";
+ String string2 = "2";
+ byte[] bytes1 = {3, 4};
+ byte[] bytes2 = {5, 6};
+ TestAllTypesNano.NestedMessage msg1 = new TestAllTypesNano.NestedMessage();
+ msg1.bb = 7;
+ TestAllTypesNano.NestedMessage msg2 = new TestAllTypesNano.NestedMessage();
+ msg2.bb = 8;
+
+ TestAllTypesNano message = new TestAllTypesNano();
+ message.repeatedString = new String[] {null, string1, string2};
+ message.repeatedBytes = new byte[][] {bytes1, null, bytes2};
+ message.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {msg1, msg2, null};
+ message.repeatedGroup = new TestAllTypesNano.RepeatedGroup[] {null, null, null};
+
+ byte[] serialized = MessageNano.toByteArray(message); // should not NPE
+ TestAllTypesNano deserialized = MessageNano.mergeFrom(new TestAllTypesNano(), serialized);
+ assertEquals(2, deserialized.repeatedString.length);
+ assertEquals(string1, deserialized.repeatedString[0]);
+ assertEquals(string2, deserialized.repeatedString[1]);
+ assertEquals(2, deserialized.repeatedBytes.length);
+ assertTrue(Arrays.equals(bytes1, deserialized.repeatedBytes[0]));
+ assertTrue(Arrays.equals(bytes2, deserialized.repeatedBytes[1]));
+ assertEquals(2, deserialized.repeatedNestedMessage.length);
+ assertEquals(msg1.bb, deserialized.repeatedNestedMessage[0].bb);
+ assertEquals(msg2.bb, deserialized.repeatedNestedMessage[1].bb);
+ assertEquals(0, deserialized.repeatedGroup.length);
+ }
+
public void testRepeatedMerge() throws Exception {
// Check that merging repeated fields cause the arrays to expand with
// new data.