aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/test/java/com/google/protobuf/NanoTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/test/java/com/google/protobuf/NanoTest.java')
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java54
1 files changed, 52 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 4f2ac3f..dbc0ddd 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -2677,13 +2677,63 @@ public class NanoTest extends TestCase {
assertHasWireData(message, false);
}
+ public void testNullRepeatedFields() throws Exception {
+ // Check that serialization after explicitly setting a repeated field
+ // to null doesn't NPE.
+ TestAllTypesNano message = new TestAllTypesNano();
+ message.repeatedInt32 = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message = new TestAllTypesNano();
+ message.repeatedNestedEnum = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message = new TestAllTypesNano();
+ message.repeatedBytes = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message = new TestAllTypesNano();
+ message.repeatedNestedMessage = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ // Create a second message to merge into message.
+ TestAllTypesNano secondMessage = new TestAllTypesNano();
+ TestAllTypesNano.NestedMessage nested =
+ new TestAllTypesNano.NestedMessage();
+ nested.bb = 55;
+ secondMessage.repeatedNestedMessage =
+ new TestAllTypesNano.NestedMessage[] { nested };
+
+ // Should not NPE
+ message.mergeFrom(CodedInputByteBufferNano.newInstance(
+ MessageNano.toByteArray(secondMessage)));
+ assertEquals(55, message.repeatedNestedMessage[0].bb);
+ }
+
private void assertHasWireData(MessageNano message, boolean expected) {
- int wireLength = MessageNano.toByteArray(message).length;
+ byte[] bytes = MessageNano.toByteArray(message);
+ int wireLength = bytes.length;
if (expected) {
assertFalse(wireLength == 0);
} else {
- assertEquals(0, wireLength);
+ if (wireLength != 0) {
+ fail("Expected no wire data for message \n" + message
+ + "\nBut got:\n"
+ + hexDump(bytes));
+ }
+ }
+ }
+
+ private static String hexDump(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+ for (byte b : bytes) {
+ sb.append(String.format("%02x ", b));
}
+ return sb.toString();
}
private <T> List<T> list(T first, T... remaining) {