From 0400cca3236de1ca303af38bf81eab332d042b7c Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 13 Mar 2018 16:37:29 -0700 Subject: Integrated internal changes from Google --- .../java/com/google/protobuf/AbstractMessage.java | 10 + .../com/google/protobuf/AbstractMessageLite.java | 10 + .../java/com/google/protobuf/BooleanArrayList.java | 16 +- .../java/com/google/protobuf/CodedInputStream.java | 6 +- .../com/google/protobuf/CodedOutputStream.java | 18 + .../java/com/google/protobuf/DoubleArrayList.java | 16 +- .../java/com/google/protobuf/DynamicMessage.java | 18 + .../java/com/google/protobuf/FloatArrayList.java | 16 +- .../com/google/protobuf/GeneratedMessageLite.java | 128 +- .../java/com/google/protobuf/IntArrayList.java | 16 +- .../java/com/google/protobuf/LongArrayList.java | 16 +- .../com/google/protobuf/MessageLiteToString.java | 96 +- .../main/java/com/google/protobuf/TextFormat.java | 51 +- .../com/google/protobuf/UnknownFieldSetLite.java | 24 +- .../main/java/com/google/protobuf/UnsafeUtil.java | 19 +- .../com/google/protobuf/BooleanArrayListTest.java | 15 + .../java/com/google/protobuf/CheckUtf8Test.java | 43 +- .../com/google/protobuf/DoubleArrayListTest.java | 15 + .../com/google/protobuf/FloatArrayListTest.java | 15 + .../java/com/google/protobuf/IntArrayListTest.java | 15 + .../test/java/com/google/protobuf/LiteTest.java | 1453 ++++++++------------ .../com/google/protobuf/LongArrayListTest.java | 15 + .../java/com/google/protobuf/MapForProto2Test.java | 18 + .../src/test/java/com/google/protobuf/MapTest.java | 18 + .../java/com/google/protobuf/TextFormatTest.java | 17 +- .../google/protobuf/UnknownFieldSetLiteTest.java | 21 + .../java/com/google/protobuf/WireFormatTest.java | 21 + 27 files changed, 1173 insertions(+), 953 deletions(-) (limited to 'java') diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java index 908764df..fc3c2a5d 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java @@ -124,6 +124,16 @@ public abstract class AbstractMessage protected int memoizedSize = -1; + @Override + int getMemoizedSerializedSize() { + return memoizedSize; + } + + @Override + void setMemoizedSerializedSize(int size) { + memoizedSize = size; + } + @Override public int getSerializedSize() { int size = memoizedSize; diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java index 24830c0a..b22bbaab 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java @@ -99,6 +99,16 @@ public abstract class AbstractMessageLite< codedOutput.flush(); } + // We'd like these to be abstract but some folks are extending this class directly. They shouldn't + // be doing that and they should feel bad. + int getMemoizedSerializedSize() { + throw new UnsupportedOperationException(); + } + + void setMemoizedSerializedSize(int size) { + throw new UnsupportedOperationException(); + } + /** * Package private helper method for AbstractParser to create diff --git a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java index fd4c142b..4d7a9727 100644 --- a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java @@ -81,6 +81,18 @@ final class BooleanArrayList extends AbstractProtobufList this.size = size; } + @Override + protected void removeRange(int fromIndex, int toIndex) { + ensureIsMutable(); + if (toIndex < fromIndex) { + throw new IndexOutOfBoundsException("toIndex < fromIndex"); + } + + System.arraycopy(array, toIndex, array, fromIndex, size - toIndex); + size -= (toIndex - fromIndex); + modCount++; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -246,7 +258,9 @@ final class BooleanArrayList extends AbstractProtobufList ensureIsMutable(); ensureIndexInRange(index); boolean value = array[index]; - System.arraycopy(array, index + 1, array, index, size - index); + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index); + } size--; modCount++; return value; diff --git a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java index 7a3f0eb9..1297462e 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java @@ -66,11 +66,9 @@ public abstract class CodedInputStream { /** * Whether to enable our custom UTF-8 decode codepath which does not use {@link StringCoding}. - * Enabled by default, disable by setting - * {@code -Dcom.google.protobuf.enableCustomutf8Decode=false} in JVM args. + * Currently disabled. */ - private static final boolean ENABLE_CUSTOM_UTF8_DECODE - = !"false".equals(System.getProperty("com.google.protobuf.enableCustomUtf8Decode")); + private static final boolean ENABLE_CUSTOM_UTF8_DECODE = false; /** Visible for subclasses. See setRecursionLimit() */ int recursionDepth; diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 093a5f61..7b1ac651 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -377,6 +377,7 @@ public abstract class CodedOutputStream extends ByteOutput { public abstract void writeMessage(final int fieldNumber, final MessageLite value) throws IOException; + /** * Write a MessageSet extension field to the stream. For historical reasons, * the wire format differs from normal fields. @@ -481,6 +482,7 @@ public abstract class CodedOutputStream extends ByteOutput { // Abstract to avoid overhead of additional virtual method calls. public abstract void writeMessageNoTag(final MessageLite value) throws IOException; + //================================================================= @ExperimentalApi @@ -666,6 +668,7 @@ public abstract class CodedOutputStream extends ByteOutput { return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value); } + /** * Compute the number of bytes that would be needed to encode a * MessageSet extension to the stream. For historical reasons, @@ -913,6 +916,7 @@ public abstract class CodedOutputStream extends ByteOutput { return computeLengthDelimitedFieldSize(value.getSerializedSize()); } + static int computeLengthDelimitedFieldSize(int fieldLength) { return computeUInt32SizeNoTag(fieldLength) + fieldLength; } @@ -1049,6 +1053,7 @@ public abstract class CodedOutputStream extends ByteOutput { writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP); } + /** * Write a {@code group} field to the stream. * @@ -1059,6 +1064,7 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + /** * Compute the number of bytes that would be needed to encode a * {@code group} field, including tag. @@ -1070,6 +1076,7 @@ public abstract class CodedOutputStream extends ByteOutput { return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value); } + /** * Compute the number of bytes that would be needed to encode a * {@code group} field. @@ -1079,6 +1086,7 @@ public abstract class CodedOutputStream extends ByteOutput { return value.getSerializedSize(); } + /** * Encode and write a varint. {@code value} is treated as * unsigned, so it won't be sign-extended if negative. @@ -1273,6 +1281,7 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override public final void writeMessageSetExtension(final int fieldNumber, final MessageLite value) throws IOException { @@ -1297,6 +1306,7 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override public final void write(byte value) throws IOException { try { @@ -1608,6 +1618,7 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) throws IOException { @@ -1632,6 +1643,7 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override public void write(byte value) throws IOException { try { @@ -1928,6 +1940,7 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override public void writeMessageSetExtension(int fieldNumber, MessageLite value) throws IOException { writeTag(WireFormat.MESSAGE_SET_ITEM, WireFormat.WIRETYPE_START_GROUP); @@ -1950,6 +1963,7 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override public void write(byte value) throws IOException { if (position >= limit) { @@ -2456,6 +2470,7 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) throws IOException { @@ -2480,6 +2495,7 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override public void write(byte value) throws IOException { if (position == limit) { @@ -2759,6 +2775,7 @@ public abstract class CodedOutputStream extends ByteOutput { writeMessageNoTag(value); } + @Override public void writeMessageSetExtension(final int fieldNumber, final MessageLite value) throws IOException { @@ -2783,6 +2800,7 @@ public abstract class CodedOutputStream extends ByteOutput { value.writeTo(this); } + @Override public void write(byte value) throws IOException { if (position == limit) { diff --git a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java index 867b85ce..5b28b4a8 100644 --- a/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/DoubleArrayList.java @@ -81,6 +81,18 @@ final class DoubleArrayList extends AbstractProtobufList this.size = size; } + @Override + protected void removeRange(int fromIndex, int toIndex) { + ensureIsMutable(); + if (toIndex < fromIndex) { + throw new IndexOutOfBoundsException("toIndex < fromIndex"); + } + + System.arraycopy(array, toIndex, array, fromIndex, size - toIndex); + size -= (toIndex - fromIndex); + modCount++; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -247,7 +259,9 @@ final class DoubleArrayList extends AbstractProtobufList ensureIsMutable(); ensureIndexInRange(index); double value = array[index]; - System.arraycopy(array, index + 1, array, index, size - index); + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index); + } size--; modCount++; return value; diff --git a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java index ba532021..a6a774b7 100644 --- a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java +++ b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java @@ -339,6 +339,20 @@ public final class DynamicMessage extends AbstractMessage { this.fields = FieldSet.newFieldSet(); this.unknownFields = UnknownFieldSet.getDefaultInstance(); this.oneofCases = new FieldDescriptor[type.toProto().getOneofDeclCount()]; + // A MapEntry has all of its fields present at all times. + if (type.getOptions().getMapEntry()) { + populateMapEntry(); + } + } + + private void populateMapEntry() { + for (FieldDescriptor field : type.getFields()) { + if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { + fields.setField(field, getDefaultInstance(field.getMessageType())); + } else { + fields.setField(field, field.getDefaultValue()); + } + } } // --------------------------------------------------------------- @@ -351,6 +365,10 @@ public final class DynamicMessage extends AbstractMessage { } else { fields.clear(); } + // A MapEntry has all of its fields present at all times. + if (type.getOptions().getMapEntry()) { + populateMapEntry(); + } unknownFields = UnknownFieldSet.getDefaultInstance(); return this; } diff --git a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java index 76645583..7c080af3 100644 --- a/java/core/src/main/java/com/google/protobuf/FloatArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/FloatArrayList.java @@ -81,6 +81,18 @@ final class FloatArrayList extends AbstractProtobufList this.size = size; } + @Override + protected void removeRange(int fromIndex, int toIndex) { + ensureIsMutable(); + if (toIndex < fromIndex) { + throw new IndexOutOfBoundsException("toIndex < fromIndex"); + } + + System.arraycopy(array, toIndex, array, fromIndex, size - toIndex); + size -= (toIndex - fromIndex); + modCount++; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -246,7 +258,9 @@ final class FloatArrayList extends AbstractProtobufList ensureIsMutable(); ensureIndexInRange(index); float value = array[index]; - System.arraycopy(array, index + 1, array, index, size - index); + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index); + } size--; modCount++; return value; diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java index 8b8d9fc6..df01547e 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java @@ -230,9 +230,13 @@ public abstract class GeneratedMessageLite< * Called by subclasses to complete parsing. For use by generated code only. */ protected void makeImmutable() { + // BEGIN REGULAR dynamicMethod(MethodToInvoke.MAKE_IMMUTABLE); - unknownFields.makeImmutable(); + // END REGULAR + // BEGIN EXPERIMENTAL + // Protobuf.getInstance().schemaFor(this).makeImmutable(this); + // END EXPERIMENTAL } protected final < @@ -269,15 +273,15 @@ public abstract class GeneratedMessageLite< * For use by generated code only. */ public static enum MethodToInvoke { - IS_INITIALIZED, // BEGIN REGULAR + IS_INITIALIZED, VISIT, + MERGE_FROM_STREAM, + MAKE_IMMUTABLE, // END REGULAR // Rely on/modify instance state GET_MEMOIZED_IS_INITIALIZED, SET_MEMOIZED_IS_INITIALIZED, - MERGE_FROM_STREAM, - MAKE_IMMUTABLE, // Rely on static state NEW_MUTABLE_INSTANCE, @@ -339,6 +343,16 @@ public abstract class GeneratedMessageLite< } // END REGULAR + @Override + int getMemoizedSerializedSize() { + return memoizedSerializedSize; + } + + @Override + void setMemoizedSerializedSize(int size) { + memoizedSerializedSize = size; + } + /** @@ -448,6 +462,28 @@ public abstract class GeneratedMessageLite< return defaultInstance; } + @Override + public BuilderType mergeFrom(byte[] input, int offset, int length) + throws InvalidProtocolBufferException { + // BEGIN REGULAR + return super.mergeFrom(input, offset, length); + // END REGULAR + // BEGIN EXPERIMENTAL + // copyOnWrite(); + // try { + // Protobuf.getInstance().schemaFor(instance).mergeFrom( + // instance, input, offset, offset + length, new ArrayDecoders.Registers()); + // } catch (InvalidProtocolBufferException e) { + // throw e; + // } catch (IndexOutOfBoundsException e) { + // throw InvalidProtocolBufferException.truncatedMessage(); + // } catch (IOException e) { + // throw new RuntimeException("Reading from byte array should not throw IOException.", e); + // } + // return (BuilderType) this; + // END EXPERIMENTAL + } + @Override public BuilderType mergeFrom( com.google.protobuf.CodedInputStream input, @@ -455,7 +491,13 @@ public abstract class GeneratedMessageLite< throws IOException { copyOnWrite(); try { + // BEGIN REGULAR instance.dynamicMethod(MethodToInvoke.MERGE_FROM_STREAM, input, extensionRegistry); + // END REGULAR + // BEGIN EXPERIMENTAL + // Protobuf.getInstance().schemaFor(instance).mergeFrom( + // instance, CodedInputStreamReader.forCodedInput(input), extensionRegistry); + // END EXPERIMENTAL } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); @@ -576,9 +618,7 @@ public abstract class GeneratedMessageLite< return parseUnknownField(tag, input); } - if (extensions.isImmutable()) { - extensions = extensions.clone(); - } + ensureExtensionsAreMutable(); if (packed) { int length = input.readRawVarint32(); @@ -794,10 +834,18 @@ public abstract class GeneratedMessageLite< if (subBuilder == null) { subBuilder = extension.getMessageDefaultInstance().newBuilderForType(); } - rawBytes.newCodedInput().readMessage(subBuilder, extensionRegistry); + subBuilder.mergeFrom(rawBytes, extensionRegistry); MessageLite value = subBuilder.build(); - extensions.setField(extension.descriptor, extension.singularToFieldSetType(value)); + ensureExtensionsAreMutable().setField( + extension.descriptor, extension.singularToFieldSetType(value)); + } + + private FieldSet ensureExtensionsAreMutable() { + if (extensions.isImmutable()) { + extensions = extensions.clone(); + } + return extensions; } private void verifyExtensionContainingType( @@ -869,10 +917,12 @@ public abstract class GeneratedMessageLite< @Override protected final void makeImmutable() { super.makeImmutable(); - + // BEGIN REGULAR extensions.makeImmutable(); + // END REGULAR } + /** * Used by subclasses to serialize extensions. Extension ranges may be * interleaved with field numbers, but we must write them in canonical @@ -1468,8 +1518,13 @@ public abstract class GeneratedMessageLite< if (memoizedIsInitialized == 0) { return false; } + // BEGIN EXPERIMENTAL + // boolean isInitialized = Protobuf.getInstance().schemaFor(message).isInitialized(message); + // END EXPERIMENTAL + // BEGIN REGULAR boolean isInitialized = message.dynamicMethod(MethodToInvoke.IS_INITIALIZED, Boolean.FALSE) != null; + // END REGULAR if (shouldMemoize) { message.dynamicMethod( MethodToInvoke.SET_MEMOIZED_IS_INITIALIZED, isInitialized ? message : null); @@ -1477,10 +1532,6 @@ public abstract class GeneratedMessageLite< return isInitialized; } - protected static final > void makeImmutable(T message) { - message.dynamicMethod(MethodToInvoke.MAKE_IMMUTABLE); - } - protected static IntList emptyIntList() { return IntArrayList.emptyList(); } @@ -1560,6 +1611,11 @@ public abstract class GeneratedMessageLite< throws InvalidProtocolBufferException { return GeneratedMessageLite.parsePartialFrom(defaultInstance, input, extensionRegistry); } + + @Override + public T parsePartialFrom(byte[] input) throws InvalidProtocolBufferException { + return GeneratedMessageLite.parsePartialFrom(defaultInstance, input); + } } /** @@ -1573,8 +1629,21 @@ public abstract class GeneratedMessageLite< @SuppressWarnings("unchecked") // Guaranteed by protoc T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); try { + // BEGIN REGULAR result.dynamicMethod(MethodToInvoke.MERGE_FROM_STREAM, input, extensionRegistry); + // END REGULAR + // BEGIN EXPERIMENTAL + // Protobuf.getInstance().schemaFor(result).mergeFrom( + // result, CodedInputStreamReader.forCodedInput(input), extensionRegistry); + // END EXPERIMENTAL result.makeImmutable(); + // BEGIN EXPERIMENTAL + // } catch (IOException e) { + // if (e.getCause() instanceof InvalidProtocolBufferException) { + // throw (InvalidProtocolBufferException) e.getCause(); + // } + // throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); + // END EXPERIMENTAL } catch (RuntimeException e) { if (e.getCause() instanceof InvalidProtocolBufferException) { throw (InvalidProtocolBufferException) e.getCause(); @@ -1584,6 +1653,34 @@ public abstract class GeneratedMessageLite< return result; } + /** A static helper method for parsing a partial from byte array. */ + static > T parsePartialFrom(T instance, byte[] input) + throws InvalidProtocolBufferException { + // BEGIN REGULAR + return parsePartialFrom(instance, input, ExtensionRegistryLite.getEmptyRegistry()); + // END REGULAR + // BEGIN EXPERIMENTAL + // @SuppressWarnings("unchecked") // Guaranteed by protoc + // T result = (T) instance.dynamicMethod(MethodToInvoke.NEW_MUTABLE_INSTANCE); + // try { + // Protobuf.getInstance().schemaFor(result).mergeFrom( + // result, input, 0, input.length, new ArrayDecoders.Registers()); + // result.makeImmutable(); + // if (result.memoizedHashCode != 0) { + // throw new RuntimeException(); + // } + // } catch (IOException e) { + // if (e.getCause() instanceof InvalidProtocolBufferException) { + // throw (InvalidProtocolBufferException) e.getCause(); + // } + // throw new InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(result); + // } catch (IndexOutOfBoundsException e) { + // throw InvalidProtocolBufferException.truncatedMessage().setUnfinishedMessage(result); + // } + // return result; + // END EXPERIMENTAL + } + protected static > T parsePartialFrom( T defaultInstance, CodedInputStream input) @@ -1680,8 +1777,7 @@ public abstract class GeneratedMessageLite< protected static > T parseFrom( T defaultInstance, byte[] data) throws InvalidProtocolBufferException { - return checkMessageInitialized( - parsePartialFrom(defaultInstance, data, ExtensionRegistryLite.getEmptyRegistry())); + return checkMessageInitialized(parsePartialFrom(defaultInstance, data)); } // Validates last tag. diff --git a/java/core/src/main/java/com/google/protobuf/IntArrayList.java b/java/core/src/main/java/com/google/protobuf/IntArrayList.java index aff5c21b..aacd71e1 100644 --- a/java/core/src/main/java/com/google/protobuf/IntArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/IntArrayList.java @@ -81,6 +81,18 @@ final class IntArrayList extends AbstractProtobufList this.size = size; } + @Override + protected void removeRange(int fromIndex, int toIndex) { + ensureIsMutable(); + if (toIndex < fromIndex) { + throw new IndexOutOfBoundsException("toIndex < fromIndex"); + } + + System.arraycopy(array, toIndex, array, fromIndex, size - toIndex); + size -= (toIndex - fromIndex); + modCount++; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -246,7 +258,9 @@ final class IntArrayList extends AbstractProtobufList ensureIsMutable(); ensureIndexInRange(index); int value = array[index]; - System.arraycopy(array, index + 1, array, index, size - index); + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index); + } size--; modCount++; return value; diff --git a/java/core/src/main/java/com/google/protobuf/LongArrayList.java b/java/core/src/main/java/com/google/protobuf/LongArrayList.java index fc146e23..95945cb7 100644 --- a/java/core/src/main/java/com/google/protobuf/LongArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/LongArrayList.java @@ -81,6 +81,18 @@ final class LongArrayList extends AbstractProtobufList this.size = size; } + @Override + protected void removeRange(int fromIndex, int toIndex) { + ensureIsMutable(); + if (toIndex < fromIndex) { + throw new IndexOutOfBoundsException("toIndex < fromIndex"); + } + + System.arraycopy(array, toIndex, array, fromIndex, size - toIndex); + size -= (toIndex - fromIndex); + modCount++; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -246,7 +258,9 @@ final class LongArrayList extends AbstractProtobufList ensureIsMutable(); ensureIndexInRange(index); long value = array[index]; - System.arraycopy(array, index + 1, array, index, size - index); + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index); + } size--; modCount++; return value; diff --git a/java/core/src/main/java/com/google/protobuf/MessageLiteToString.java b/java/core/src/main/java/com/google/protobuf/MessageLiteToString.java index 23373ef4..8e265935 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageLiteToString.java +++ b/java/core/src/main/java/com/google/protobuf/MessageLiteToString.java @@ -31,6 +31,7 @@ package com.google.protobuf; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -38,20 +39,18 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; -/** - * Helps generate {@link String} representations of {@link MessageLite} protos. - */ -// TODO(dweis): Fix map fields. +/** Helps generate {@link String} representations of {@link MessageLite} protos. */ final class MessageLiteToString { private static final String LIST_SUFFIX = "List"; private static final String BUILDER_LIST_SUFFIX = "OrBuilderList"; + private static final String MAP_SUFFIX = "Map"; private static final String BYTES_SUFFIX = "Bytes"; - + /** - * Returns a {@link String} representation of the {@link MessageLite} object. The first line of + * Returns a {@link String} representation of the {@link MessageLite} object. The first line of * the {@code String} representation representation includes a comment string to uniquely identify - * the objcet instance. This acts as an indicator that this should not be relied on for + * the object instance. This acts as an indicator that this should not be relied on for * comparisons. * *

For use by generated code only. @@ -71,8 +70,9 @@ final class MessageLiteToString { */ private static void reflectivePrintWithIndent( MessageLite messageLite, StringBuilder buffer, int indent) { - // Build a map of method name to method. We're looking for methods like getFoo(), hasFoo(), and - // getFooList() which might be useful for building an object's string representation. + // Build a map of method name to method. We're looking for methods like getFoo(), hasFoo(), + // getFooList() and getFooMap() which might be useful for building an object's string + // representation. Map nameToNoArgMethod = new HashMap(); Map nameToMethod = new HashMap(); Set getters = new TreeSet(); @@ -89,12 +89,16 @@ final class MessageLiteToString { for (String getter : getters) { String suffix = getter.replaceFirst("get", ""); - if (suffix.endsWith(LIST_SUFFIX) && !suffix.endsWith(BUILDER_LIST_SUFFIX)) { - String camelCase = suffix.substring(0, 1).toLowerCase() - + suffix.substring(1, suffix.length() - LIST_SUFFIX.length()); + if (suffix.endsWith(LIST_SUFFIX) + && !suffix.endsWith(BUILDER_LIST_SUFFIX) + // Sometimes people have fields named 'list' that aren't repeated. + && !suffix.equals(LIST_SUFFIX)) { + String camelCase = + suffix.substring(0, 1).toLowerCase() + + suffix.substring(1, suffix.length() - LIST_SUFFIX.length()); // Try to reflectively get the value and toString() the field as if it were repeated. This - // only works if the method names have not be proguarded out or renamed. - Method listMethod = nameToNoArgMethod.get("get" + suffix); + // only works if the method names have not been proguarded out or renamed. + Method listMethod = nameToNoArgMethod.get(getter); if (listMethod != null && listMethod.getReturnType().equals(List.class)) { printField( buffer, @@ -104,6 +108,30 @@ final class MessageLiteToString { continue; } } + if (suffix.endsWith(MAP_SUFFIX) + // Sometimes people have fields named 'map' that aren't maps. + && !suffix.equals(MAP_SUFFIX)) { + String camelCase = + suffix.substring(0, 1).toLowerCase() + + suffix.substring(1, suffix.length() - MAP_SUFFIX.length()); + // Try to reflectively get the value and toString() the field as if it were a map. This only + // works if the method names have not been proguarded out or renamed. + Method mapMethod = nameToNoArgMethod.get(getter); + if (mapMethod != null + && mapMethod.getReturnType().equals(Map.class) + // Skip the deprecated getter method with no prefix "Map" when the field name ends with + // "map". + && !mapMethod.isAnnotationPresent(Deprecated.class) + // Skip the internal mutable getter method. + && Modifier.isPublic(mapMethod.getModifiers())) { + printField( + buffer, + indent, + camelCaseToSnakeCase(camelCase), + GeneratedMessageLite.invokeOrDie(mapMethod, messageLite)); + continue; + } + } Method setter = nameToMethod.get("set" + suffix); if (setter == null) { @@ -119,22 +147,19 @@ final class MessageLiteToString { String camelCase = suffix.substring(0, 1).toLowerCase() + suffix.substring(1); // Try to reflectively get the value and toString() the field as if it were optional. This - // only works if the method names have not be proguarded out or renamed. + // only works if the method names have not been proguarded out or renamed. Method getMethod = nameToNoArgMethod.get("get" + suffix); Method hasMethod = nameToNoArgMethod.get("has" + suffix); // TODO(dweis): Fix proto3 semantics. if (getMethod != null) { Object value = GeneratedMessageLite.invokeOrDie(getMethod, messageLite); - final boolean hasValue = hasMethod == null - ? !isDefaultValue(value) - : (Boolean) GeneratedMessageLite.invokeOrDie(hasMethod, messageLite); - // TODO(dweis): This doesn't stop printing oneof case twice: value and enum style. + final boolean hasValue = + hasMethod == null + ? !isDefaultValue(value) + : (Boolean) GeneratedMessageLite.invokeOrDie(hasMethod, messageLite); + // TODO(dweis): This doesn't stop printing oneof case twice: value and enum style. if (hasValue) { - printField( - buffer, - indent, - camelCaseToSnakeCase(camelCase), - value); + printField(buffer, indent, camelCaseToSnakeCase(camelCase), value); } continue; } @@ -153,7 +178,7 @@ final class MessageLiteToString { ((GeneratedMessageLite) messageLite).unknownFields.printWithIndent(buffer, indent); } } - + private static boolean isDefaultValue(Object o) { if (o instanceof Boolean) { return !((Boolean) o); @@ -179,7 +204,7 @@ final class MessageLiteToString { if (o instanceof java.lang.Enum) { // Catches oneof enums. return ((java.lang.Enum) o).ordinal() == 0; } - + return false; } @@ -201,6 +226,13 @@ final class MessageLiteToString { } return; } + if (object instanceof Map) { + Map map = (Map) object; + for (Map.Entry entry : map.entrySet()) { + printField(buffer, indent, name, entry); + } + return; + } buffer.append('\n'); for (int i = 0; i < indent; i++) { @@ -220,11 +252,21 @@ final class MessageLiteToString { buffer.append(' '); } buffer.append("}"); + } else if (object instanceof Map.Entry) { + buffer.append(" {"); + Map.Entry entry = (Map.Entry) object; + printField(buffer, indent + 2, "key", entry.getKey()); + printField(buffer, indent + 2, "value", entry.getValue()); + buffer.append("\n"); + for (int i = 0; i < indent; i++) { + buffer.append(' '); + } + buffer.append("}"); } else { buffer.append(": ").append(object.toString()); } } - + private static final String camelCaseToSnakeCase(String camelCase) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < camelCase.length(); i++) { diff --git a/java/core/src/main/java/com/google/protobuf/TextFormat.java b/java/core/src/main/java/com/google/protobuf/TextFormat.java index 0c8b898c..25c3474f 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormat.java @@ -987,7 +987,7 @@ public final class TextFormat { nextToken(); return false; } else { - throw parseException("Expected \"true\" or \"false\"."); + throw parseException("Expected \"true\" or \"false\". Found \"" + currentToken + "\"."); } } @@ -1311,13 +1311,17 @@ public final class TextFormat { } private final boolean allowUnknownFields; + private final boolean allowUnknownEnumValues; private final SingularOverwritePolicy singularOverwritePolicy; private TextFormatParseInfoTree.Builder parseInfoTreeBuilder; private Parser( - boolean allowUnknownFields, SingularOverwritePolicy singularOverwritePolicy, + boolean allowUnknownFields, + boolean allowUnknownEnumValues, + SingularOverwritePolicy singularOverwritePolicy, TextFormatParseInfoTree.Builder parseInfoTreeBuilder) { this.allowUnknownFields = allowUnknownFields; + this.allowUnknownEnumValues = allowUnknownEnumValues; this.singularOverwritePolicy = singularOverwritePolicy; this.parseInfoTreeBuilder = parseInfoTreeBuilder; } @@ -1334,6 +1338,7 @@ public final class TextFormat { */ public static class Builder { private boolean allowUnknownFields = false; + private boolean allowUnknownEnumValues = false; private SingularOverwritePolicy singularOverwritePolicy = SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES; private TextFormatParseInfoTree.Builder parseInfoTreeBuilder = null; @@ -1355,7 +1360,10 @@ public final class TextFormat { public Parser build() { return new Parser( - allowUnknownFields, singularOverwritePolicy, parseInfoTreeBuilder); + allowUnknownFields, + allowUnknownEnumValues, + singularOverwritePolicy, + parseInfoTreeBuilder); } } @@ -1419,7 +1427,7 @@ public final class TextFormat { return text; } - // Check both unknown fields and unknown extensions and log warming messages + // Check both unknown fields and unknown extensions and log warning messages // or throw exceptions according to the flag. private void checkUnknownFields(final List unknownFields) throws ParseException { @@ -1737,17 +1745,40 @@ public final class TextFormat { final int number = tokenizer.consumeInt32(); value = enumType.findValueByNumber(number); if (value == null) { - throw tokenizer.parseExceptionPreviousToken( - "Enum type \"" + enumType.getFullName() - + "\" has no value with number " + number + '.'); + String unknownValueMsg = + "Enum type \"" + + enumType.getFullName() + + "\" has no value with number " + + number + + '.'; + if (allowUnknownEnumValues) { + logger.warning(unknownValueMsg); + return; + } else { + throw tokenizer.parseExceptionPreviousToken( + "Enum type \"" + + enumType.getFullName() + + "\" has no value with number " + + number + + '.'); + } } } else { final String id = tokenizer.consumeIdentifier(); value = enumType.findValueByName(id); if (value == null) { - throw tokenizer.parseExceptionPreviousToken( - "Enum type \"" + enumType.getFullName() - + "\" has no value named \"" + id + "\"."); + String unknownValueMsg = + "Enum type \"" + + enumType.getFullName() + + "\" has no value named \"" + + id + + "\"."; + if (allowUnknownEnumValues) { + logger.warning(unknownValueMsg); + return; + } else { + throw tokenizer.parseExceptionPreviousToken(unknownValueMsg); + } } } diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java index 2a614c84..f0b919ad 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java @@ -295,14 +295,30 @@ public final class UnknownFieldSetLite { return true; } + private static int hashCode(int[] tags, int count) { + int hashCode = 17; + for (int i = 0; i < count; ++i) { + hashCode = 31 * hashCode + tags[i]; + } + return hashCode; + } + + private static int hashCode(Object[] objects, int count) { + int hashCode = 17; + for (int i = 0; i < count; ++i) { + hashCode = 31 * hashCode + objects[i].hashCode(); + } + return hashCode; + } + @Override public int hashCode() { int hashCode = 17; - + hashCode = 31 * hashCode + count; - hashCode = 31 * hashCode + Arrays.hashCode(tags); - hashCode = 31 * hashCode + Arrays.deepHashCode(objects); - + hashCode = 31 * hashCode + hashCode(tags, count); + hashCode = 31 * hashCode + hashCode(objects, count); + return hashCode; } diff --git a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java index b09ff673..c9e2904b 100644 --- a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java +++ b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java @@ -33,6 +33,7 @@ package com.google.protobuf; import java.lang.reflect.Field; import java.nio.Buffer; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.security.AccessController; import java.security.PrivilegedExceptionAction; import java.util.logging.Level; @@ -83,6 +84,7 @@ final class UnsafeUtil { return HAS_UNSAFE_BYTEBUFFER_OPERATIONS; } + static long objectFieldOffset(Field field) { return MEMORY_ACCESSOR.objectFieldOffset(field); } @@ -287,7 +289,7 @@ final class UnsafeUtil { /** * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform. */ - private static sun.misc.Unsafe getUnsafe() { + static sun.misc.Unsafe getUnsafe() { sun.misc.Unsafe unsafe = null; try { unsafe = @@ -367,6 +369,10 @@ final class UnsafeUtil { clazz.getMethod("objectFieldOffset", Field.class); clazz.getMethod("getLong", Object.class, long.class); + if (bufferAddressField() == null) { + return false; + } + clazz.getMethod("getByte", long.class); clazz.getMethod("putByte", long.class, byte.class); clazz.getMethod("getInt", long.class); @@ -387,12 +393,14 @@ final class UnsafeUtil { /** Finds the address field within a direct {@link Buffer}. */ private static Field bufferAddressField() { - return field(Buffer.class, "address", long.class); + Field field = field(Buffer.class, "address"); + return field != null && field.getType() == long.class ? field : null; } /** Finds the value field within a {@link String}. */ private static Field stringValueField() { - return field(String.class, "value", char[].class); + Field field = field(String.class, "value"); + return field != null && field.getType() == char[].class ? field : null; } /** @@ -407,14 +415,11 @@ final class UnsafeUtil { * Gets the field with the given name within the class, or {@code null} if not found. If found, * the field is made accessible. */ - private static Field field(Class clazz, String fieldName, Class expectedType) { + private static Field field(Class clazz, String fieldName) { Field field; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); - if (!field.getType().equals(expectedType)) { - return null; - } } catch (Throwable t) { // Failed to access the fields. field = null; diff --git a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java index 18132e9e..4906763c 100644 --- a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java @@ -32,6 +32,7 @@ package com.google.protobuf; import static java.util.Arrays.asList; +import com.google.protobuf.Internal.BooleanList; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; @@ -297,6 +298,20 @@ public class BooleanArrayListTest extends TestCase { } } + public void testRemoveEndOfCapacity() { + BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addBoolean(true); + toRemove.remove(0); + assertEquals(0, toRemove.size()); + } + + public void testSublistRemoveEndOfCapacity() { + BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addBoolean(true); + toRemove.subList(0, 1).clear(); + assertEquals(0, toRemove.size()); + } + private void assertImmutable(BooleanArrayList list) { try { diff --git a/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java b/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java index cc65d19a..50b87ae3 100644 --- a/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java +++ b/java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java @@ -34,7 +34,7 @@ import proto2_test_check_utf8.TestCheckUtf8.BytesWrapper; import proto2_test_check_utf8.TestCheckUtf8.StringWrapper; import proto2_test_check_utf8_size.TestCheckUtf8Size.BytesWrapperSize; import proto2_test_check_utf8_size.TestCheckUtf8Size.StringWrapperSize; - +import java.io.ByteArrayInputStream; import junit.framework.TestCase; /** @@ -90,14 +90,9 @@ public class CheckUtf8Test extends TestCase { } public void testParseRequiredStringWithBadUtf8() throws Exception { - ByteString serialized = - BytesWrapper.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteString(); - try { - StringWrapper.parser().parseFrom(serialized); - fail("Expected InvalidProtocolBufferException for non UTF-8 byte string."); - } catch (InvalidProtocolBufferException exception) { - assertEquals("Protocol message had invalid UTF-8.", exception.getMessage()); - } + byte[] serialized = + BytesWrapper.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteArray(); + assertParseBadUtf8(StringWrapper.getDefaultInstance(), serialized); } public void testBuildRequiredStringWithBadUtf8Size() throws Exception { @@ -128,14 +123,36 @@ public class CheckUtf8Test extends TestCase { } public void testParseRequiredStringWithBadUtf8Size() throws Exception { - ByteString serialized = - BytesWrapperSize.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteString(); + byte[] serialized = + BytesWrapperSize.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteArray(); + assertParseBadUtf8(StringWrapperSize.getDefaultInstance(), serialized); + } + + private void assertParseBadUtf8(MessageLite defaultInstance, byte[] data) throws Exception { + // Check combinations of (parser vs. builder) x (byte[] vs. InputStream) + try { + defaultInstance.getParserForType().parseFrom(data); + fail("Expected InvalidProtocolBufferException for non UTF-8 byte string."); + } catch (InvalidProtocolBufferException exception) { + assertEquals("Protocol message had invalid UTF-8.", exception.getMessage()); + } + try { + defaultInstance.newBuilderForType().mergeFrom(data); + fail("Expected InvalidProtocolBufferException for non UTF-8 byte string."); + } catch (InvalidProtocolBufferException exception) { + assertEquals("Protocol message had invalid UTF-8.", exception.getMessage()); + } try { - StringWrapperSize.parser().parseFrom(serialized); + defaultInstance.getParserForType().parseFrom(new ByteArrayInputStream(data)); + fail("Expected InvalidProtocolBufferException for non UTF-8 byte string."); + } catch (InvalidProtocolBufferException exception) { + assertEquals("Protocol message had invalid UTF-8.", exception.getMessage()); + } + try { + defaultInstance.newBuilderForType().mergeFrom(new ByteArrayInputStream(data)); fail("Expected InvalidProtocolBufferException for non UTF-8 byte string."); } catch (InvalidProtocolBufferException exception) { assertEquals("Protocol message had invalid UTF-8.", exception.getMessage()); } } - } diff --git a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java index d8942792..923d7f43 100644 --- a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java @@ -32,6 +32,7 @@ package com.google.protobuf; import static java.util.Arrays.asList; +import com.google.protobuf.Internal.DoubleList; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; @@ -297,6 +298,20 @@ public class DoubleArrayListTest extends TestCase { } } + public void testRemoveEndOfCapacity() { + DoubleList toRemove = DoubleArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addDouble(3); + toRemove.remove(0); + assertEquals(0, toRemove.size()); + } + + public void testSublistRemoveEndOfCapacity() { + DoubleList toRemove = DoubleArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addDouble(3); + toRemove.subList(0, 1).clear(); + assertEquals(0, toRemove.size()); + } + private void assertImmutable(DoubleArrayList list) { if (list.contains(1D)) { throw new RuntimeException("Cannot test the immutability of lists that contain 1."); diff --git a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java index aa36be49..903a79db 100644 --- a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java @@ -32,6 +32,7 @@ package com.google.protobuf; import static java.util.Arrays.asList; +import com.google.protobuf.Internal.FloatList; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; @@ -297,6 +298,20 @@ public class FloatArrayListTest extends TestCase { } } + public void testRemoveEndOfCapacity() { + FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addFloat(3); + toRemove.remove(0); + assertEquals(0, toRemove.size()); + } + + public void testSublistRemoveEndOfCapacity() { + FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addFloat(3); + toRemove.subList(0, 1).clear(); + assertEquals(0, toRemove.size()); + } + private void assertImmutable(FloatArrayList list) { if (list.contains(1F)) { throw new RuntimeException("Cannot test the immutability of lists that contain 1."); diff --git a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java index 60c85450..d8e97d4f 100644 --- a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java @@ -32,6 +32,7 @@ package com.google.protobuf; import static java.util.Arrays.asList; +import com.google.protobuf.Internal.IntList; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; @@ -297,6 +298,20 @@ public class IntArrayListTest extends TestCase { } } + public void testRemoveEndOfCapacity() { + IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addInt(3); + toRemove.remove(0); + assertEquals(0, toRemove.size()); + } + + public void testSublistRemoveEndOfCapacity() { + IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addInt(3); + toRemove.subList(0, 1).clear(); + assertEquals(0, toRemove.size()); + } + private void assertImmutable(IntArrayList list) { if (list.contains(1)) { throw new RuntimeException("Cannot test the immutability of lists that contain 1."); diff --git a/java/core/src/test/java/com/google/protobuf/LiteTest.java b/java/core/src/test/java/com/google/protobuf/LiteTest.java index 40b44538..5ab80ca2 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteTest.java @@ -48,11 +48,15 @@ import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup; import com.google.protobuf.UnittestLite.TestAllTypesLiteOrBuilder; import com.google.protobuf.UnittestLite.TestHugeFieldNumbersLite; import com.google.protobuf.UnittestLite.TestNestedExtensionLite; +import map_lite_test.MapTestProto.TestMap; +import map_lite_test.MapTestProto.TestMap.MessageValue; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestOneofEquals; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestRecursiveOneof; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -88,12 +92,11 @@ public class LiteTest extends TestCase { // stuff to make sure the lite message is actually here and usable. TestAllTypesLite message = - TestAllTypesLite.newBuilder() - .setOptionalInt32(123) - .addRepeatedString("hello") - .setOptionalNestedMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) - .build(); + TestAllTypesLite.newBuilder() + .setOptionalInt32(123) + .addRepeatedString("hello") + .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) + .build(); ByteString data = message.toByteString(); @@ -105,57 +108,66 @@ public class LiteTest extends TestCase { assertEquals(7, message2.getOptionalNestedMessage().getBb()); } + public void testLite_unknownEnumAtListBoundary() throws Exception { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + CodedOutputStream output = CodedOutputStream.newInstance(byteStream); + for (int i = 0; i < AbstractProtobufList.DEFAULT_CAPACITY; i++) { + output.writeInt32(TestAllTypesLite.REPEATED_NESTED_ENUM_FIELD_NUMBER, 1); + } + // 0 is not a valid enum value for NestedEnum + output.writeInt32(TestAllTypesLite.REPEATED_NESTED_ENUM_FIELD_NUMBER, 0); + output.flush(); + // This tests a bug we had once with removal right at the boundary of the array. It would throw + // at runtime so no need to assert. + TestAllTypesLite.parseFrom(new ByteArrayInputStream(byteStream.toByteArray())); + } + public void testLiteExtensions() throws Exception { // TODO(kenton): Unlike other features of the lite library, extensions are // implemented completely differently from the regular library. We // should probably test them more thoroughly. TestAllExtensionsLite message = - TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) - .addExtension(UnittestLite.repeatedStringExtensionLite, "hello") - .setExtension(UnittestLite.optionalNestedEnumExtensionLite, - TestAllTypesLite.NestedEnum.BAZ) - .setExtension(UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) - .build(); + TestAllExtensionsLite.newBuilder() + .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) + .addExtension(UnittestLite.repeatedStringExtensionLite, "hello") + .setExtension( + UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension( + UnittestLite.optionalNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + .build(); // Test copying a message, since coping extensions actually does use a // different code path between lite and regular libraries, and as of this // writing, parsing hasn't been implemented yet. TestAllExtensionsLite message2 = message.toBuilder().build(); - assertEquals(123, (int) message2.getExtension( - UnittestLite.optionalInt32ExtensionLite)); - assertEquals(1, message2.getExtensionCount( - UnittestLite.repeatedStringExtensionLite)); - assertEquals(1, message2.getExtension( - UnittestLite.repeatedStringExtensionLite).size()); - assertEquals("hello", message2.getExtension( - UnittestLite.repeatedStringExtensionLite, 0)); - assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension( - UnittestLite.optionalNestedEnumExtensionLite)); - assertEquals(7, message2.getExtension( - UnittestLite.optionalNestedMessageExtensionLite).getBb()); + assertEquals(123, (int) message2.getExtension(UnittestLite.optionalInt32ExtensionLite)); + assertEquals(1, message2.getExtensionCount(UnittestLite.repeatedStringExtensionLite)); + assertEquals(1, message2.getExtension(UnittestLite.repeatedStringExtensionLite).size()); + assertEquals("hello", message2.getExtension(UnittestLite.repeatedStringExtensionLite, 0)); + assertEquals( + TestAllTypesLite.NestedEnum.BAZ, + message2.getExtension(UnittestLite.optionalNestedEnumExtensionLite)); + assertEquals(7, message2.getExtension(UnittestLite.optionalNestedMessageExtensionLite).getBb()); } - + public void testClone() { - TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder() - .setOptionalInt32(123); - assertEquals( - expected.getOptionalInt32(), expected.clone().getOptionalInt32()); - - TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 123); + TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder().setOptionalInt32(123); + assertEquals(expected.getOptionalInt32(), expected.clone().getOptionalInt32()); + + TestAllExtensionsLite.Builder expected2 = + TestAllExtensionsLite.newBuilder() + .setExtension(UnittestLite.optionalInt32ExtensionLite, 123); assertEquals( expected2.getExtension(UnittestLite.optionalInt32ExtensionLite), expected2.clone().getExtension(UnittestLite.optionalInt32ExtensionLite)); } - + public void testAddAll() { try { - TestAllTypesLite.newBuilder() - .addAllRepeatedBytes(null); + TestAllTypesLite.newBuilder().addAllRepeatedBytes(null); fail(); } catch (NullPointerException e) { // expected. @@ -186,7 +198,7 @@ public class LiteTest extends TestCase { // We have to cast to Byte first. Casting to byte causes a type error assertEquals(1, ((Byte) memo.get(message)).intValue()); } - + public void testSanityCopyOnWrite() throws InvalidProtocolBufferException { // Since builders are implemented as a thin wrapper around a message // instance, we attempt to verify that we can't cause the builder to modify @@ -210,13 +222,11 @@ public class LiteTest extends TestCase { assertEquals(ByteString.EMPTY, message.getOptionalBytes()); assertEquals(ByteString.copyFromUtf8("hi"), builder.getOptionalBytes()); messageAfterBuild = builder.build(); - assertEquals( - ByteString.copyFromUtf8("hi"), messageAfterBuild.getOptionalBytes()); + assertEquals(ByteString.copyFromUtf8("hi"), messageAfterBuild.getOptionalBytes()); assertEquals(ByteString.EMPTY, message.getOptionalBytes()); builder.clearOptionalBytes(); assertEquals(ByteString.EMPTY, builder.getOptionalBytes()); - assertEquals( - ByteString.copyFromUtf8("hi"), messageAfterBuild.getOptionalBytes()); + assertEquals(ByteString.copyFromUtf8("hi"), messageAfterBuild.getOptionalBytes()); message = builder.build(); builder.setOptionalCord("hi"); @@ -234,27 +244,23 @@ public class LiteTest extends TestCase { assertEquals(ByteString.EMPTY, message.getOptionalCordBytes()); assertEquals(ByteString.copyFromUtf8("no"), builder.getOptionalCordBytes()); messageAfterBuild = builder.build(); - assertEquals( - ByteString.copyFromUtf8("no"), - messageAfterBuild.getOptionalCordBytes()); + assertEquals(ByteString.copyFromUtf8("no"), messageAfterBuild.getOptionalCordBytes()); assertEquals(ByteString.EMPTY, message.getOptionalCordBytes()); builder.clearOptionalCord(); assertEquals(ByteString.EMPTY, builder.getOptionalCordBytes()); - assertEquals( - ByteString.copyFromUtf8("no"), - messageAfterBuild.getOptionalCordBytes()); - + assertEquals(ByteString.copyFromUtf8("no"), messageAfterBuild.getOptionalCordBytes()); + message = builder.build(); builder.setOptionalDouble(1); - assertEquals(0D, message.getOptionalDouble()); - assertEquals(1D, builder.getOptionalDouble()); + assertEquals(0D, message.getOptionalDouble(), 0.0); + assertEquals(1D, builder.getOptionalDouble(), 0.0); messageAfterBuild = builder.build(); - assertEquals(1D, messageAfterBuild.getOptionalDouble()); - assertEquals(0D, message.getOptionalDouble()); + assertEquals(1D, messageAfterBuild.getOptionalDouble(), 0.0); + assertEquals(0D, message.getOptionalDouble(), 0.0); builder.clearOptionalDouble(); - assertEquals(0D, builder.getOptionalDouble()); - assertEquals(1D, messageAfterBuild.getOptionalDouble()); - + assertEquals(0D, builder.getOptionalDouble(), 0.0); + assertEquals(1D, messageAfterBuild.getOptionalDouble(), 0.0); + message = builder.build(); builder.setOptionalFixed32(1); assertEquals(0, message.getOptionalFixed32()); @@ -265,7 +271,7 @@ public class LiteTest extends TestCase { builder.clearOptionalFixed32(); assertEquals(0, builder.getOptionalFixed32()); assertEquals(1, messageAfterBuild.getOptionalFixed32()); - + message = builder.build(); builder.setOptionalFixed64(1); assertEquals(0L, message.getOptionalFixed64()); @@ -279,105 +285,72 @@ public class LiteTest extends TestCase { message = builder.build(); builder.setOptionalFloat(1); - assertEquals(0F, message.getOptionalFloat()); - assertEquals(1F, builder.getOptionalFloat()); + assertEquals(0F, message.getOptionalFloat(), 0.0f); + assertEquals(1F, builder.getOptionalFloat(), 0.0f); messageAfterBuild = builder.build(); - assertEquals(1F, messageAfterBuild.getOptionalFloat()); - assertEquals(0F, message.getOptionalFloat()); + assertEquals(1F, messageAfterBuild.getOptionalFloat(), 0.0f); + assertEquals(0F, message.getOptionalFloat(), 0.0f); builder.clearOptionalFloat(); - assertEquals(0F, builder.getOptionalFloat()); - assertEquals(1F, messageAfterBuild.getOptionalFloat()); + assertEquals(0F, builder.getOptionalFloat(), 0.0f); + assertEquals(1F, messageAfterBuild.getOptionalFloat(), 0.0f); message = builder.build(); builder.setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_FOO, message.getOptionalForeignEnum()); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_BAR, builder.getOptionalForeignEnum()); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, message.getOptionalForeignEnum()); + assertEquals(ForeignEnumLite.FOREIGN_LITE_BAR, builder.getOptionalForeignEnum()); messageAfterBuild = builder.build(); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_BAR, - messageAfterBuild.getOptionalForeignEnum()); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_FOO, message.getOptionalForeignEnum()); + assertEquals(ForeignEnumLite.FOREIGN_LITE_BAR, messageAfterBuild.getOptionalForeignEnum()); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, message.getOptionalForeignEnum()); builder.clearOptionalForeignEnum(); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_FOO, builder.getOptionalForeignEnum()); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_BAR, - messageAfterBuild.getOptionalForeignEnum()); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, builder.getOptionalForeignEnum()); + assertEquals(ForeignEnumLite.FOREIGN_LITE_BAR, messageAfterBuild.getOptionalForeignEnum()); message = builder.build(); - ForeignMessageLite foreignMessage = ForeignMessageLite.newBuilder() - .setC(1) - .build(); + ForeignMessageLite foreignMessage = ForeignMessageLite.newBuilder().setC(1).build(); builder.setOptionalForeignMessage(foreignMessage); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - message.getOptionalForeignMessage()); + assertEquals(ForeignMessageLite.getDefaultInstance(), message.getOptionalForeignMessage()); assertEquals(foreignMessage, builder.getOptionalForeignMessage()); messageAfterBuild = builder.build(); assertEquals(foreignMessage, messageAfterBuild.getOptionalForeignMessage()); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - message.getOptionalForeignMessage()); + assertEquals(ForeignMessageLite.getDefaultInstance(), message.getOptionalForeignMessage()); builder.clearOptionalForeignMessage(); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - builder.getOptionalForeignMessage()); + assertEquals(ForeignMessageLite.getDefaultInstance(), builder.getOptionalForeignMessage()); assertEquals(foreignMessage, messageAfterBuild.getOptionalForeignMessage()); message = builder.build(); - ForeignMessageLite.Builder foreignMessageBuilder = - ForeignMessageLite.newBuilder() - .setC(3); + ForeignMessageLite.Builder foreignMessageBuilder = ForeignMessageLite.newBuilder().setC(3); builder.setOptionalForeignMessage(foreignMessageBuilder); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - message.getOptionalForeignMessage()); + assertEquals(ForeignMessageLite.getDefaultInstance(), message.getOptionalForeignMessage()); assertEquals(foreignMessageBuilder.build(), builder.getOptionalForeignMessage()); messageAfterBuild = builder.build(); assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getOptionalForeignMessage()); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - message.getOptionalForeignMessage()); + assertEquals(ForeignMessageLite.getDefaultInstance(), message.getOptionalForeignMessage()); builder.clearOptionalForeignMessage(); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - builder.getOptionalForeignMessage()); + assertEquals(ForeignMessageLite.getDefaultInstance(), builder.getOptionalForeignMessage()); assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getOptionalForeignMessage()); message = builder.build(); - OptionalGroup optionalGroup = OptionalGroup.newBuilder() - .setA(1) - .build(); + OptionalGroup optionalGroup = OptionalGroup.newBuilder().setA(1).build(); builder.setOptionalGroup(optionalGroup); - assertEquals( - OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); + assertEquals(OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); assertEquals(optionalGroup, builder.getOptionalGroup()); messageAfterBuild = builder.build(); assertEquals(optionalGroup, messageAfterBuild.getOptionalGroup()); - assertEquals( - OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); + assertEquals(OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); builder.clearOptionalGroup(); - assertEquals( - OptionalGroup.getDefaultInstance(), builder.getOptionalGroup()); + assertEquals(OptionalGroup.getDefaultInstance(), builder.getOptionalGroup()); assertEquals(optionalGroup, messageAfterBuild.getOptionalGroup()); message = builder.build(); - OptionalGroup.Builder optionalGroupBuilder = OptionalGroup.newBuilder() - .setA(3); + OptionalGroup.Builder optionalGroupBuilder = OptionalGroup.newBuilder().setA(3); builder.setOptionalGroup(optionalGroupBuilder); - assertEquals( - OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); + assertEquals(OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); assertEquals(optionalGroupBuilder.build(), builder.getOptionalGroup()); messageAfterBuild = builder.build(); assertEquals(optionalGroupBuilder.build(), messageAfterBuild.getOptionalGroup()); - assertEquals( - OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); + assertEquals(OptionalGroup.getDefaultInstance(), message.getOptionalGroup()); builder.clearOptionalGroup(); - assertEquals( - OptionalGroup.getDefaultInstance(), builder.getOptionalGroup()); + assertEquals(OptionalGroup.getDefaultInstance(), builder.getOptionalGroup()); assertEquals(optionalGroupBuilder.build(), messageAfterBuild.getOptionalGroup()); message = builder.build(); @@ -401,43 +374,29 @@ public class LiteTest extends TestCase { builder.clearOptionalInt64(); assertEquals(0L, builder.getOptionalInt64()); assertEquals(1L, messageAfterBuild.getOptionalInt64()); - + message = builder.build(); - NestedMessage nestedMessage = NestedMessage.newBuilder() - .setBb(1) - .build(); + NestedMessage nestedMessage = NestedMessage.newBuilder().setBb(1).build(); builder.setOptionalLazyMessage(nestedMessage); - assertEquals( - NestedMessage.getDefaultInstance(), - message.getOptionalLazyMessage()); + assertEquals(NestedMessage.getDefaultInstance(), message.getOptionalLazyMessage()); assertEquals(nestedMessage, builder.getOptionalLazyMessage()); messageAfterBuild = builder.build(); assertEquals(nestedMessage, messageAfterBuild.getOptionalLazyMessage()); - assertEquals( - NestedMessage.getDefaultInstance(), - message.getOptionalLazyMessage()); + assertEquals(NestedMessage.getDefaultInstance(), message.getOptionalLazyMessage()); builder.clearOptionalLazyMessage(); - assertEquals( - NestedMessage.getDefaultInstance(), builder.getOptionalLazyMessage()); + assertEquals(NestedMessage.getDefaultInstance(), builder.getOptionalLazyMessage()); assertEquals(nestedMessage, messageAfterBuild.getOptionalLazyMessage()); message = builder.build(); - NestedMessage.Builder nestedMessageBuilder = - NestedMessage.newBuilder() - .setBb(3); + NestedMessage.Builder nestedMessageBuilder = NestedMessage.newBuilder().setBb(3); builder.setOptionalLazyMessage(nestedMessageBuilder); - assertEquals( - NestedMessage.getDefaultInstance(), - message.getOptionalLazyMessage()); + assertEquals(NestedMessage.getDefaultInstance(), message.getOptionalLazyMessage()); assertEquals(nestedMessageBuilder.build(), builder.getOptionalLazyMessage()); messageAfterBuild = builder.build(); assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getOptionalLazyMessage()); - assertEquals( - NestedMessage.getDefaultInstance(), - message.getOptionalLazyMessage()); + assertEquals(NestedMessage.getDefaultInstance(), message.getOptionalLazyMessage()); builder.clearOptionalLazyMessage(); - assertEquals( - NestedMessage.getDefaultInstance(), builder.getOptionalLazyMessage()); + assertEquals(NestedMessage.getDefaultInstance(), builder.getOptionalLazyMessage()); assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getOptionalLazyMessage()); message = builder.build(); @@ -497,19 +456,14 @@ public class LiteTest extends TestCase { message = builder.build(); builder.setOptionalStringBytes(ByteString.copyFromUtf8("no")); assertEquals(ByteString.EMPTY, message.getOptionalStringBytes()); - assertEquals( - ByteString.copyFromUtf8("no"), builder.getOptionalStringBytes()); + assertEquals(ByteString.copyFromUtf8("no"), builder.getOptionalStringBytes()); messageAfterBuild = builder.build(); - assertEquals( - ByteString.copyFromUtf8("no"), - messageAfterBuild.getOptionalStringBytes()); + assertEquals(ByteString.copyFromUtf8("no"), messageAfterBuild.getOptionalStringBytes()); assertEquals(ByteString.EMPTY, message.getOptionalStringBytes()); builder.clearOptionalString(); assertEquals(ByteString.EMPTY, builder.getOptionalStringBytes()); - assertEquals( - ByteString.copyFromUtf8("no"), - messageAfterBuild.getOptionalStringBytes()); - + assertEquals(ByteString.copyFromUtf8("no"), messageAfterBuild.getOptionalStringBytes()); + message = builder.build(); builder.setOptionalStringPiece("hi"); assertEquals("", message.getOptionalStringPiece()); @@ -524,18 +478,13 @@ public class LiteTest extends TestCase { message = builder.build(); builder.setOptionalStringPieceBytes(ByteString.copyFromUtf8("no")); assertEquals(ByteString.EMPTY, message.getOptionalStringPieceBytes()); - assertEquals( - ByteString.copyFromUtf8("no"), builder.getOptionalStringPieceBytes()); + assertEquals(ByteString.copyFromUtf8("no"), builder.getOptionalStringPieceBytes()); messageAfterBuild = builder.build(); - assertEquals( - ByteString.copyFromUtf8("no"), - messageAfterBuild.getOptionalStringPieceBytes()); + assertEquals(ByteString.copyFromUtf8("no"), messageAfterBuild.getOptionalStringPieceBytes()); assertEquals(ByteString.EMPTY, message.getOptionalStringPieceBytes()); builder.clearOptionalStringPiece(); assertEquals(ByteString.EMPTY, builder.getOptionalStringPieceBytes()); - assertEquals( - ByteString.copyFromUtf8("no"), - messageAfterBuild.getOptionalStringPieceBytes()); + assertEquals(ByteString.copyFromUtf8("no"), messageAfterBuild.getOptionalStringPieceBytes()); message = builder.build(); builder.setOptionalUint32(1); @@ -572,16 +521,13 @@ public class LiteTest extends TestCase { message = builder.build(); builder.addAllRepeatedBytes(singletonList(ByteString.copyFromUtf8("hi"))); assertEquals(emptyList(), message.getRepeatedBytesList()); - assertEquals( - singletonList(ByteString.copyFromUtf8("hi")), - builder.getRepeatedBytesList()); + assertEquals(singletonList(ByteString.copyFromUtf8("hi")), builder.getRepeatedBytesList()); assertEquals(emptyList(), message.getRepeatedBytesList()); messageAfterBuild = builder.build(); builder.clearRepeatedBytes(); assertEquals(emptyList(), builder.getRepeatedBytesList()); assertEquals( - singletonList(ByteString.copyFromUtf8("hi")), - messageAfterBuild.getRepeatedBytesList()); + singletonList(ByteString.copyFromUtf8("hi")), messageAfterBuild.getRepeatedBytesList()); message = builder.build(); builder.addAllRepeatedCord(singletonList("hi")); @@ -634,12 +580,10 @@ public class LiteTest extends TestCase { assertEquals(singletonList(1F), messageAfterBuild.getRepeatedFloatList()); message = builder.build(); - builder.addAllRepeatedForeignEnum( - singletonList(ForeignEnumLite.FOREIGN_LITE_BAR)); + builder.addAllRepeatedForeignEnum(singletonList(ForeignEnumLite.FOREIGN_LITE_BAR)); assertEquals(emptyList(), message.getRepeatedForeignEnumList()); assertEquals( - singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), - builder.getRepeatedForeignEnumList()); + singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), builder.getRepeatedForeignEnumList()); assertEquals(emptyList(), message.getRepeatedForeignEnumList()); messageAfterBuild = builder.build(); builder.clearRepeatedForeignEnum(); @@ -651,23 +595,17 @@ public class LiteTest extends TestCase { message = builder.build(); builder.addAllRepeatedForeignMessage(singletonList(foreignMessage)); assertEquals(emptyList(), message.getRepeatedForeignMessageList()); - assertEquals( - singletonList(foreignMessage), builder.getRepeatedForeignMessageList()); + assertEquals(singletonList(foreignMessage), builder.getRepeatedForeignMessageList()); assertEquals(emptyList(), message.getRepeatedForeignMessageList()); messageAfterBuild = builder.build(); builder.clearRepeatedForeignMessage(); assertEquals(emptyList(), builder.getRepeatedForeignMessageList()); - assertEquals( - singletonList(foreignMessage), - messageAfterBuild.getRepeatedForeignMessageList()); + assertEquals(singletonList(foreignMessage), messageAfterBuild.getRepeatedForeignMessageList()); message = builder.build(); - builder.addAllRepeatedGroup( - singletonList(RepeatedGroup.getDefaultInstance())); + builder.addAllRepeatedGroup(singletonList(RepeatedGroup.getDefaultInstance())); assertEquals(emptyList(), message.getRepeatedGroupList()); - assertEquals( - singletonList(RepeatedGroup.getDefaultInstance()), - builder.getRepeatedGroupList()); + assertEquals(singletonList(RepeatedGroup.getDefaultInstance()), builder.getRepeatedGroupList()); assertEquals(emptyList(), message.getRepeatedGroupList()); messageAfterBuild = builder.build(); builder.clearRepeatedGroup(); @@ -699,15 +637,12 @@ public class LiteTest extends TestCase { message = builder.build(); builder.addAllRepeatedLazyMessage(singletonList(nestedMessage)); assertEquals(emptyList(), message.getRepeatedLazyMessageList()); - assertEquals( - singletonList(nestedMessage), builder.getRepeatedLazyMessageList()); + assertEquals(singletonList(nestedMessage), builder.getRepeatedLazyMessageList()); assertEquals(emptyList(), message.getRepeatedLazyMessageList()); messageAfterBuild = builder.build(); builder.clearRepeatedLazyMessage(); assertEquals(emptyList(), builder.getRepeatedLazyMessageList()); - assertEquals( - singletonList(nestedMessage), - messageAfterBuild.getRepeatedLazyMessageList()); + assertEquals(singletonList(nestedMessage), messageAfterBuild.getRepeatedLazyMessageList()); message = builder.build(); builder.addAllRepeatedSfixed32(singletonList(1)); @@ -727,8 +662,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); builder.clearRepeatedSfixed64(); assertEquals(emptyList(), builder.getRepeatedSfixed64List()); - assertEquals( - singletonList(1L), messageAfterBuild.getRepeatedSfixed64List()); + assertEquals(singletonList(1L), messageAfterBuild.getRepeatedSfixed64List()); message = builder.build(); builder.addAllRepeatedSint32(singletonList(1)); @@ -758,8 +692,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); builder.clearRepeatedString(); assertEquals(emptyList(), builder.getRepeatedStringList()); - assertEquals( - singletonList("hi"), messageAfterBuild.getRepeatedStringList()); + assertEquals(singletonList("hi"), messageAfterBuild.getRepeatedStringList()); message = builder.build(); builder.addAllRepeatedStringPiece(singletonList("hi")); @@ -769,8 +702,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); builder.clearRepeatedStringPiece(); assertEquals(emptyList(), builder.getRepeatedStringPieceList()); - assertEquals( - singletonList("hi"), messageAfterBuild.getRepeatedStringPieceList()); + assertEquals(singletonList("hi"), messageAfterBuild.getRepeatedStringPieceList()); message = builder.build(); builder.addAllRepeatedUint32(singletonList(1)); @@ -805,16 +737,13 @@ public class LiteTest extends TestCase { message = builder.build(); builder.addRepeatedBytes(ByteString.copyFromUtf8("hi")); assertEquals(emptyList(), message.getRepeatedBytesList()); - assertEquals( - singletonList(ByteString.copyFromUtf8("hi")), - builder.getRepeatedBytesList()); + assertEquals(singletonList(ByteString.copyFromUtf8("hi")), builder.getRepeatedBytesList()); assertEquals(emptyList(), message.getRepeatedBytesList()); messageAfterBuild = builder.build(); builder.clearRepeatedBytes(); assertEquals(emptyList(), builder.getRepeatedBytesList()); assertEquals( - singletonList(ByteString.copyFromUtf8("hi")), - messageAfterBuild.getRepeatedBytesList()); + singletonList(ByteString.copyFromUtf8("hi")), messageAfterBuild.getRepeatedBytesList()); message = builder.build(); builder.addRepeatedCord("hi"); @@ -870,8 +799,7 @@ public class LiteTest extends TestCase { builder.addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR); assertEquals(emptyList(), message.getRepeatedForeignEnumList()); assertEquals( - singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), - builder.getRepeatedForeignEnumList()); + singletonList(ForeignEnumLite.FOREIGN_LITE_BAR), builder.getRepeatedForeignEnumList()); assertEquals(emptyList(), message.getRepeatedForeignEnumList()); messageAfterBuild = builder.build(); builder.clearRepeatedForeignEnum(); @@ -883,22 +811,17 @@ public class LiteTest extends TestCase { message = builder.build(); builder.addRepeatedForeignMessage(foreignMessage); assertEquals(emptyList(), message.getRepeatedForeignMessageList()); - assertEquals( - singletonList(foreignMessage), builder.getRepeatedForeignMessageList()); + assertEquals(singletonList(foreignMessage), builder.getRepeatedForeignMessageList()); assertEquals(emptyList(), message.getRepeatedForeignMessageList()); messageAfterBuild = builder.build(); builder.removeRepeatedForeignMessage(0); assertEquals(emptyList(), builder.getRepeatedForeignMessageList()); - assertEquals( - singletonList(foreignMessage), - messageAfterBuild.getRepeatedForeignMessageList()); + assertEquals(singletonList(foreignMessage), messageAfterBuild.getRepeatedForeignMessageList()); message = builder.build(); builder.addRepeatedGroup(RepeatedGroup.getDefaultInstance()); assertEquals(emptyList(), message.getRepeatedGroupList()); - assertEquals( - singletonList(RepeatedGroup.getDefaultInstance()), - builder.getRepeatedGroupList()); + assertEquals(singletonList(RepeatedGroup.getDefaultInstance()), builder.getRepeatedGroupList()); assertEquals(emptyList(), message.getRepeatedGroupList()); messageAfterBuild = builder.build(); builder.removeRepeatedGroup(0); @@ -930,15 +853,12 @@ public class LiteTest extends TestCase { message = builder.build(); builder.addRepeatedLazyMessage(nestedMessage); assertEquals(emptyList(), message.getRepeatedLazyMessageList()); - assertEquals( - singletonList(nestedMessage), builder.getRepeatedLazyMessageList()); + assertEquals(singletonList(nestedMessage), builder.getRepeatedLazyMessageList()); assertEquals(emptyList(), message.getRepeatedLazyMessageList()); messageAfterBuild = builder.build(); builder.removeRepeatedLazyMessage(0); assertEquals(emptyList(), builder.getRepeatedLazyMessageList()); - assertEquals( - singletonList(nestedMessage), - messageAfterBuild.getRepeatedLazyMessageList()); + assertEquals(singletonList(nestedMessage), messageAfterBuild.getRepeatedLazyMessageList()); message = builder.build(); builder.addRepeatedSfixed32(1); @@ -958,8 +878,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); builder.clearRepeatedSfixed64(); assertEquals(emptyList(), builder.getRepeatedSfixed64List()); - assertEquals( - singletonList(1L), messageAfterBuild.getRepeatedSfixed64List()); + assertEquals(singletonList(1L), messageAfterBuild.getRepeatedSfixed64List()); message = builder.build(); builder.addRepeatedSint32(1); @@ -989,8 +908,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); builder.clearRepeatedString(); assertEquals(emptyList(), builder.getRepeatedStringList()); - assertEquals( - singletonList("hi"), messageAfterBuild.getRepeatedStringList()); + assertEquals(singletonList("hi"), messageAfterBuild.getRepeatedStringList()); message = builder.build(); builder.addRepeatedStringPiece("hi"); @@ -1000,8 +918,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); builder.clearRepeatedStringPiece(); assertEquals(emptyList(), builder.getRepeatedStringPieceList()); - assertEquals( - singletonList("hi"), messageAfterBuild.getRepeatedStringPieceList()); + assertEquals(singletonList("hi"), messageAfterBuild.getRepeatedStringPieceList()); message = builder.build(); builder.addRepeatedUint32(1); @@ -1022,7 +939,7 @@ public class LiteTest extends TestCase { builder.clearRepeatedUint64(); assertEquals(emptyList(), builder.getRepeatedUint64List()); assertEquals(singletonList(1L), messageAfterBuild.getRepeatedUint64List()); - + message = builder.build(); builder.addRepeatedBool(true); messageAfterBuild = builder.build(); @@ -1031,14 +948,13 @@ public class LiteTest extends TestCase { assertEquals(true, messageAfterBuild.getRepeatedBool(0)); assertEquals(false, builder.getRepeatedBool(0)); builder.clearRepeatedBool(); - + message = builder.build(); builder.addRepeatedBytes(ByteString.copyFromUtf8("hi")); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedBytesCount()); builder.setRepeatedBytes(0, ByteString.EMPTY); - assertEquals( - ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedBytes(0)); + assertEquals(ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedBytes(0)); assertEquals(ByteString.EMPTY, builder.getRepeatedBytes(0)); builder.clearRepeatedBytes(); @@ -1056,8 +972,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedCordCount()); builder.setRepeatedCord(0, ""); - assertEquals( - ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedCordBytes(0)); + assertEquals(ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedCordBytes(0)); assertEquals(ByteString.EMPTY, builder.getRepeatedCordBytes(0)); builder.clearRepeatedCord(); @@ -1066,8 +981,8 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedDoubleCount()); builder.setRepeatedDouble(0, 0D); - assertEquals(1D, messageAfterBuild.getRepeatedDouble(0)); - assertEquals(0D, builder.getRepeatedDouble(0)); + assertEquals(1D, messageAfterBuild.getRepeatedDouble(0), 0.0); + assertEquals(0D, builder.getRepeatedDouble(0), 0.0); builder.clearRepeatedDouble(); message = builder.build(); @@ -1093,8 +1008,8 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedFloatCount()); builder.setRepeatedFloat(0, 0F); - assertEquals(1F, messageAfterBuild.getRepeatedFloat(0)); - assertEquals(0F, builder.getRepeatedFloat(0)); + assertEquals(1F, messageAfterBuild.getRepeatedFloat(0), 0.0f); + assertEquals(0F, builder.getRepeatedFloat(0), 0.0f); builder.clearRepeatedFloat(); message = builder.build(); @@ -1102,36 +1017,26 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedForeignEnumCount()); builder.setRepeatedForeignEnum(0, ForeignEnumLite.FOREIGN_LITE_FOO); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_BAR, - messageAfterBuild.getRepeatedForeignEnum(0)); - assertEquals( - ForeignEnumLite.FOREIGN_LITE_FOO, builder.getRepeatedForeignEnum(0)); + assertEquals(ForeignEnumLite.FOREIGN_LITE_BAR, messageAfterBuild.getRepeatedForeignEnum(0)); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, builder.getRepeatedForeignEnum(0)); builder.clearRepeatedForeignEnum(); message = builder.build(); builder.addRepeatedForeignMessage(foreignMessage); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedForeignMessageCount()); - builder.setRepeatedForeignMessage( - 0, ForeignMessageLite.getDefaultInstance()); - assertEquals( - foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - builder.getRepeatedForeignMessage(0)); + builder.setRepeatedForeignMessage(0, ForeignMessageLite.getDefaultInstance()); + assertEquals(foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); + assertEquals(ForeignMessageLite.getDefaultInstance(), builder.getRepeatedForeignMessage(0)); builder.clearRepeatedForeignMessage(); - + message = builder.build(); builder.addRepeatedForeignMessage(foreignMessageBuilder); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedForeignMessageCount()); - builder.setRepeatedForeignMessage( - 0, ForeignMessageLite.getDefaultInstance()); + builder.setRepeatedForeignMessage(0, ForeignMessageLite.getDefaultInstance()); assertEquals(foreignMessageBuilder.build(), messageAfterBuild.getRepeatedForeignMessage(0)); - assertEquals( - ForeignMessageLite.getDefaultInstance(), - builder.getRepeatedForeignMessage(0)); + assertEquals(ForeignMessageLite.getDefaultInstance(), builder.getRepeatedForeignMessage(0)); builder.clearRepeatedForeignMessage(); message = builder.build(); @@ -1139,54 +1044,46 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedForeignMessageCount()); builder.setRepeatedForeignMessage(0, foreignMessageBuilder); - assertEquals( - foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); + assertEquals(foreignMessage, messageAfterBuild.getRepeatedForeignMessage(0)); assertEquals(foreignMessageBuilder.build(), builder.getRepeatedForeignMessage(0)); builder.clearRepeatedForeignMessage(); message = builder.build(); - RepeatedGroup repeatedGroup = RepeatedGroup.newBuilder() - .setA(1) - .build(); + RepeatedGroup repeatedGroup = RepeatedGroup.newBuilder().setA(1).build(); builder.addRepeatedGroup(repeatedGroup); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedGroupCount()); builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); assertEquals(repeatedGroup, messageAfterBuild.getRepeatedGroup(0)); - assertEquals( - RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); + assertEquals(RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); builder.clearRepeatedGroup(); - + message = builder.build(); builder.addRepeatedGroup(0, repeatedGroup); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedGroupCount()); builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); assertEquals(repeatedGroup, messageAfterBuild.getRepeatedGroup(0)); - assertEquals( - RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); + assertEquals(RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); builder.clearRepeatedGroup(); - + message = builder.build(); - RepeatedGroup.Builder repeatedGroupBuilder = RepeatedGroup.newBuilder() - .setA(3); + RepeatedGroup.Builder repeatedGroupBuilder = RepeatedGroup.newBuilder().setA(3); builder.addRepeatedGroup(repeatedGroupBuilder); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedGroupCount()); builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); assertEquals(repeatedGroupBuilder.build(), messageAfterBuild.getRepeatedGroup(0)); - assertEquals( - RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); + assertEquals(RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); builder.clearRepeatedGroup(); - + message = builder.build(); builder.addRepeatedGroup(0, repeatedGroupBuilder); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedGroupCount()); builder.setRepeatedGroup(0, RepeatedGroup.getDefaultInstance()); assertEquals(repeatedGroupBuilder.build(), messageAfterBuild.getRepeatedGroup(0)); - assertEquals( - RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); + assertEquals(RepeatedGroup.getDefaultInstance(), builder.getRepeatedGroup(0)); builder.clearRepeatedGroup(); message = builder.build(); @@ -1206,45 +1103,41 @@ public class LiteTest extends TestCase { assertEquals(1L, messageAfterBuild.getRepeatedInt64(0)); assertEquals(0L, builder.getRepeatedInt64(0)); builder.clearRepeatedInt64(); - + message = builder.build(); builder.addRepeatedLazyMessage(nestedMessage); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedLazyMessageCount()); builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); assertEquals(nestedMessage, messageAfterBuild.getRepeatedLazyMessage(0)); - assertEquals( - NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); + assertEquals(NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); builder.clearRepeatedLazyMessage(); - + message = builder.build(); builder.addRepeatedLazyMessage(0, nestedMessage); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedLazyMessageCount()); builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); assertEquals(nestedMessage, messageAfterBuild.getRepeatedLazyMessage(0)); - assertEquals( - NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); + assertEquals(NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); builder.clearRepeatedLazyMessage(); - + message = builder.build(); builder.addRepeatedLazyMessage(nestedMessageBuilder); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedLazyMessageCount()); builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getRepeatedLazyMessage(0)); - assertEquals( - NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); + assertEquals(NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); builder.clearRepeatedLazyMessage(); - + message = builder.build(); builder.addRepeatedLazyMessage(0, nestedMessageBuilder); messageAfterBuild = builder.build(); assertEquals(0, message.getRepeatedLazyMessageCount()); builder.setRepeatedLazyMessage(0, NestedMessage.getDefaultInstance()); assertEquals(nestedMessageBuilder.build(), messageAfterBuild.getRepeatedLazyMessage(0)); - assertEquals( - NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); + assertEquals(NestedMessage.getDefaultInstance(), builder.getRepeatedLazyMessage(0)); builder.clearRepeatedLazyMessage(); message = builder.build(); @@ -1297,9 +1190,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0L, message.getRepeatedStringCount()); builder.setRepeatedString(0, ""); - assertEquals( - ByteString.copyFromUtf8("hi"), - messageAfterBuild.getRepeatedStringBytes(0)); + assertEquals(ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedStringBytes(0)); assertEquals(ByteString.EMPTY, builder.getRepeatedStringBytes(0)); builder.clearRepeatedString(); @@ -1317,9 +1208,7 @@ public class LiteTest extends TestCase { messageAfterBuild = builder.build(); assertEquals(0L, message.getRepeatedStringPieceCount()); builder.setRepeatedStringPiece(0, ""); - assertEquals( - ByteString.copyFromUtf8("hi"), - messageAfterBuild.getRepeatedStringPieceBytes(0)); + assertEquals(ByteString.copyFromUtf8("hi"), messageAfterBuild.getRepeatedStringPieceBytes(0)); assertEquals(ByteString.EMPTY, builder.getRepeatedStringPieceBytes(0)); builder.clearRepeatedStringPiece(); @@ -1343,18 +1232,14 @@ public class LiteTest extends TestCase { message = builder.build(); assertEquals(0, message.getSerializedSize()); - builder.mergeFrom(TestAllTypesLite.newBuilder() - .setOptionalBool(true) - .build()); + builder.mergeFrom(TestAllTypesLite.newBuilder().setOptionalBool(true).build()); assertEquals(0, message.getSerializedSize()); assertEquals(true, builder.build().getOptionalBool()); builder.clearOptionalBool(); message = builder.build(); assertEquals(0, message.getSerializedSize()); - builder.mergeFrom(TestAllTypesLite.newBuilder() - .setOptionalBool(true) - .build()); + builder.mergeFrom(TestAllTypesLite.newBuilder().setOptionalBool(true).build()); assertEquals(0, message.getSerializedSize()); assertEquals(true, builder.build().getOptionalBool()); builder.clear(); @@ -1364,93 +1249,68 @@ public class LiteTest extends TestCase { assertEquals(0, message.getSerializedSize()); builder.mergeOptionalForeignMessage(foreignMessage); assertEquals(0, message.getSerializedSize()); - assertEquals( - foreignMessage.getC(), - builder.build().getOptionalForeignMessage().getC()); + assertEquals(foreignMessage.getC(), builder.build().getOptionalForeignMessage().getC()); builder.clearOptionalForeignMessage(); message = builder.build(); assertEquals(0, message.getSerializedSize()); builder.mergeOptionalLazyMessage(nestedMessage); assertEquals(0, message.getSerializedSize()); - assertEquals( - nestedMessage.getBb(), - builder.build().getOptionalLazyMessage().getBb()); + assertEquals(nestedMessage.getBb(), builder.build().getOptionalLazyMessage().getBb()); builder.clearOptionalLazyMessage(); - + message = builder.build(); builder.setOneofString("hi"); - assertEquals( - OneofFieldCase.ONEOFFIELD_NOT_SET, message.getOneofFieldCase()); + assertEquals(OneofFieldCase.ONEOFFIELD_NOT_SET, message.getOneofFieldCase()); assertEquals(OneofFieldCase.ONEOF_STRING, builder.getOneofFieldCase()); assertEquals("hi", builder.getOneofString()); messageAfterBuild = builder.build(); - assertEquals( - OneofFieldCase.ONEOF_STRING, messageAfterBuild.getOneofFieldCase()); + assertEquals(OneofFieldCase.ONEOF_STRING, messageAfterBuild.getOneofFieldCase()); assertEquals("hi", messageAfterBuild.getOneofString()); builder.setOneofUint32(1); - assertEquals( - OneofFieldCase.ONEOF_STRING, messageAfterBuild.getOneofFieldCase()); + assertEquals(OneofFieldCase.ONEOF_STRING, messageAfterBuild.getOneofFieldCase()); assertEquals("hi", messageAfterBuild.getOneofString()); assertEquals(OneofFieldCase.ONEOF_UINT32, builder.getOneofFieldCase()); assertEquals(1, builder.getOneofUint32()); TestAllTypesLiteOrBuilder messageOrBuilder = builder; assertEquals(OneofFieldCase.ONEOF_UINT32, messageOrBuilder.getOneofFieldCase()); - - TestAllExtensionsLite.Builder extendableMessageBuilder = - TestAllExtensionsLite.newBuilder(); + + TestAllExtensionsLite.Builder extendableMessageBuilder = TestAllExtensionsLite.newBuilder(); TestAllExtensionsLite extendableMessage = extendableMessageBuilder.build(); - extendableMessageBuilder.setExtension( - UnittestLite.optionalInt32ExtensionLite, 1); - assertFalse(extendableMessage.hasExtension( - UnittestLite.optionalInt32ExtensionLite)); + extendableMessageBuilder.setExtension(UnittestLite.optionalInt32ExtensionLite, 1); + assertFalse(extendableMessage.hasExtension(UnittestLite.optionalInt32ExtensionLite)); extendableMessage = extendableMessageBuilder.build(); assertEquals( - 1, (int) extendableMessageBuilder.getExtension( - UnittestLite.optionalInt32ExtensionLite)); + 1, (int) extendableMessageBuilder.getExtension(UnittestLite.optionalInt32ExtensionLite)); + assertEquals(1, (int) extendableMessage.getExtension(UnittestLite.optionalInt32ExtensionLite)); + extendableMessageBuilder.setExtension(UnittestLite.optionalInt32ExtensionLite, 3); assertEquals( - 1, (int) extendableMessage.getExtension( - UnittestLite.optionalInt32ExtensionLite)); - extendableMessageBuilder.setExtension( - UnittestLite.optionalInt32ExtensionLite, 3); - assertEquals( - 3, (int) extendableMessageBuilder.getExtension( - UnittestLite.optionalInt32ExtensionLite)); - assertEquals( - 1, (int) extendableMessage.getExtension( - UnittestLite.optionalInt32ExtensionLite)); + 3, (int) extendableMessageBuilder.getExtension(UnittestLite.optionalInt32ExtensionLite)); + assertEquals(1, (int) extendableMessage.getExtension(UnittestLite.optionalInt32ExtensionLite)); extendableMessage = extendableMessageBuilder.build(); assertEquals( - 3, (int) extendableMessageBuilder.getExtension( - UnittestLite.optionalInt32ExtensionLite)); - assertEquals( - 3, (int) extendableMessage.getExtension( - UnittestLite.optionalInt32ExtensionLite)); - + 3, (int) extendableMessageBuilder.getExtension(UnittestLite.optionalInt32ExtensionLite)); + assertEquals(3, (int) extendableMessage.getExtension(UnittestLite.optionalInt32ExtensionLite)); + // No extension registry, so it should be in unknown fields. - extendableMessage = - TestAllExtensionsLite.parseFrom(extendableMessage.toByteArray()); - assertFalse(extendableMessage.hasExtension( - UnittestLite.optionalInt32ExtensionLite)); - + extendableMessage = TestAllExtensionsLite.parseFrom(extendableMessage.toByteArray()); + assertFalse(extendableMessage.hasExtension(UnittestLite.optionalInt32ExtensionLite)); + extendableMessageBuilder = extendableMessage.toBuilder(); - extendableMessageBuilder.mergeFrom(TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalFixed32ExtensionLite, 11) - .build()); - + extendableMessageBuilder.mergeFrom( + TestAllExtensionsLite.newBuilder() + .setExtension(UnittestLite.optionalFixed32ExtensionLite, 11) + .build()); + extendableMessage = extendableMessageBuilder.build(); ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); UnittestLite.registerAllExtensions(registry); - extendableMessage = TestAllExtensionsLite.parseFrom( - extendableMessage.toByteArray(), registry); - + extendableMessage = TestAllExtensionsLite.parseFrom(extendableMessage.toByteArray(), registry); + // The unknown field was preserved. + assertEquals(3, (int) extendableMessage.getExtension(UnittestLite.optionalInt32ExtensionLite)); assertEquals( - 3, (int) extendableMessage.getExtension( - UnittestLite.optionalInt32ExtensionLite)); - assertEquals( - 11, (int) extendableMessage.getExtension( - UnittestLite.optionalFixed32ExtensionLite)); + 11, (int) extendableMessage.getExtension(UnittestLite.optionalFixed32ExtensionLite)); } public void testBuilderMergeFromNull() throws Exception { @@ -1497,140 +1357,174 @@ public class LiteTest extends TestCase { } public void testToStringScalarFieldsSuffixedWithList() throws Exception { - assertToStringEquals("deceptively_named_list: 7", TestAllTypesLite.newBuilder() - .setDeceptivelyNamedList(7) - .build()); + assertToStringEquals( + "deceptively_named_list: 7", + TestAllTypesLite.newBuilder().setDeceptivelyNamedList(7).build()); } public void testToStringPrimitives() throws Exception { - TestAllTypesLite proto = TestAllTypesLite.newBuilder() - .setOptionalInt32(1) - .setOptionalInt64(9223372036854775807L) - .build(); + TestAllTypesLite proto = + TestAllTypesLite.newBuilder() + .setOptionalInt32(1) + .setOptionalInt64(9223372036854775807L) + .build(); assertToStringEquals("optional_int32: 1\noptional_int64: 9223372036854775807", proto); - proto = TestAllTypesLite.newBuilder() - .setOptionalBool(true) - .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) - .build(); + proto = + TestAllTypesLite.newBuilder() + .setOptionalBool(true) + .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) + .build(); assertToStringEquals("optional_bool: true\noptional_nested_enum: BAZ", proto); - proto = TestAllTypesLite.newBuilder() - .setOptionalFloat(2.72f) - .setOptionalDouble(3.14) - .build(); + proto = TestAllTypesLite.newBuilder().setOptionalFloat(2.72f).setOptionalDouble(3.14).build(); assertToStringEquals("optional_double: 3.14\noptional_float: 2.72", proto); } public void testToStringStringFields() throws Exception { - TestAllTypesLite proto = TestAllTypesLite.newBuilder() - .setOptionalString("foo\"bar\nbaz\\") - .build(); + TestAllTypesLite proto = + TestAllTypesLite.newBuilder().setOptionalString("foo\"bar\nbaz\\").build(); assertToStringEquals("optional_string: \"foo\\\"bar\\nbaz\\\\\"", proto); - proto = TestAllTypesLite.newBuilder() - .setOptionalString("\u6587") - .build(); + proto = TestAllTypesLite.newBuilder().setOptionalString("\u6587").build(); assertToStringEquals("optional_string: \"\\346\\226\\207\"", proto); } public void testToStringNestedMessage() throws Exception { - TestAllTypesLite proto = TestAllTypesLite.newBuilder() - .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.getDefaultInstance()) - .build(); + TestAllTypesLite proto = + TestAllTypesLite.newBuilder() + .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.getDefaultInstance()) + .build(); assertToStringEquals("optional_nested_message {\n}", proto); - proto = TestAllTypesLite.newBuilder() - .setOptionalNestedMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) - .build(); + proto = + TestAllTypesLite.newBuilder() + .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) + .build(); assertToStringEquals("optional_nested_message {\n bb: 7\n}", proto); } public void testToStringRepeatedFields() throws Exception { - TestAllTypesLite proto = TestAllTypesLite.newBuilder() - .addRepeatedInt32(32) - .addRepeatedInt32(32) - .addRepeatedInt64(64) - .build(); + TestAllTypesLite proto = + TestAllTypesLite.newBuilder() + .addRepeatedInt32(32) + .addRepeatedInt32(32) + .addRepeatedInt64(64) + .build(); assertToStringEquals("repeated_int32: 32\nrepeated_int32: 32\nrepeated_int64: 64", proto); - proto = TestAllTypesLite.newBuilder() - .addRepeatedLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) - .addRepeatedLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(8)) - .build(); + proto = + TestAllTypesLite.newBuilder() + .addRepeatedLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) + .addRepeatedLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(8)) + .build(); assertToStringEquals( - "repeated_lazy_message {\n bb: 7\n}\nrepeated_lazy_message {\n bb: 8\n}", - proto); + "repeated_lazy_message {\n bb: 7\n}\nrepeated_lazy_message {\n bb: 8\n}", proto); } public void testToStringForeignFields() throws Exception { - TestAllTypesLite proto = TestAllTypesLite.newBuilder() - .setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) - .setOptionalForeignMessage( - ForeignMessageLite.newBuilder() - .setC(3)) - .build(); + TestAllTypesLite proto = + TestAllTypesLite.newBuilder() + .setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) + .setOptionalForeignMessage(ForeignMessageLite.newBuilder().setC(3)) + .build(); assertToStringEquals( - "optional_foreign_enum: FOREIGN_LITE_BAR\noptional_foreign_message {\n c: 3\n}", - proto); + "optional_foreign_enum: FOREIGN_LITE_BAR\noptional_foreign_message {\n c: 3\n}", proto); } public void testToStringExtensions() throws Exception { - TestAllExtensionsLite message = TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) - .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") - .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") - .setExtension(UnittestLite.optionalNestedEnumExtensionLite, - TestAllTypesLite.NestedEnum.BAZ) - .setExtension(UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) - .build(); + TestAllExtensionsLite message = + TestAllExtensionsLite.newBuilder() + .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) + .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") + .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") + .setExtension( + UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension( + UnittestLite.optionalNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + .build(); assertToStringEquals( - "[1]: 123\n[18] {\n bb: 7\n}\n[21]: 3\n[44]: \"spam\"\n[44]: \"eggs\"", - message); + "[1]: 123\n[18] {\n bb: 7\n}\n[21]: 3\n[44]: \"spam\"\n[44]: \"eggs\"", message); } public void testToStringUnknownFields() throws Exception { - TestAllExtensionsLite messageWithExtensions = TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) - .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") - .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") - .setExtension(UnittestLite.optionalNestedEnumExtensionLite, - TestAllTypesLite.NestedEnum.BAZ) - .setExtension(UnittestLite.optionalNestedMessageExtensionLite, - TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) - .build(); - TestAllExtensionsLite messageWithUnknownFields = TestAllExtensionsLite.parseFrom( - messageWithExtensions.toByteArray()); + TestAllExtensionsLite messageWithExtensions = + TestAllExtensionsLite.newBuilder() + .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) + .addExtension(UnittestLite.repeatedStringExtensionLite, "spam") + .addExtension(UnittestLite.repeatedStringExtensionLite, "eggs") + .setExtension( + UnittestLite.optionalNestedEnumExtensionLite, TestAllTypesLite.NestedEnum.BAZ) + .setExtension( + UnittestLite.optionalNestedMessageExtensionLite, + TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build()) + .build(); + TestAllExtensionsLite messageWithUnknownFields = + TestAllExtensionsLite.parseFrom(messageWithExtensions.toByteArray()); assertToStringEquals( - "1: 123\n18: \"\\b\\a\"\n21: 3\n44: \"spam\"\n44: \"eggs\"", - messageWithUnknownFields); + "1: 123\n18: \"\\b\\a\"\n21: 3\n44: \"spam\"\n44: \"eggs\"", messageWithUnknownFields); } - + public void testToStringLazyMessage() throws Exception { - TestAllTypesLite message = TestAllTypesLite.newBuilder() - .setOptionalLazyMessage(NestedMessage.newBuilder().setBb(1).build()) - .build(); + TestAllTypesLite message = + TestAllTypesLite.newBuilder() + .setOptionalLazyMessage(NestedMessage.newBuilder().setBb(1).build()) + .build(); assertToStringEquals("optional_lazy_message {\n bb: 1\n}", message); } - + public void testToStringGroup() throws Exception { - TestAllTypesLite message = TestAllTypesLite.newBuilder() - .setOptionalGroup(OptionalGroup.newBuilder().setA(1).build()) - .build(); + TestAllTypesLite message = + TestAllTypesLite.newBuilder() + .setOptionalGroup(OptionalGroup.newBuilder().setA(1).build()) + .build(); assertToStringEquals("optional_group {\n a: 1\n}", message); } - + public void testToStringOneof() throws Exception { - TestAllTypesLite message = TestAllTypesLite.newBuilder() - .setOneofString("hello") - .build(); + TestAllTypesLite message = TestAllTypesLite.newBuilder().setOneofString("hello").build(); assertToStringEquals("oneof_string: \"hello\"", message); } + public void testToStringMapFields() throws Exception { + TestMap message1 = + TestMap.newBuilder() + .putInt32ToStringField(1, "alpha") + .putInt32ToStringField(2, "beta") + .build(); + assertToStringEquals( + "int32_to_string_field {\n" + + " key: 1\n" + + " value: \"alpha\"\n" + + "}\n" + + "int32_to_string_field {\n" + + " key: 2\n" + + " value: \"beta\"\n" + + "}", + message1); + + TestMap message2 = + TestMap.newBuilder() + .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(10).build()) + .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(20).build()) + .build(); + assertToStringEquals( + "int32_to_message_field {\n" + + " key: 1\n" + + " value {\n" + + " value: 10\n" + + " }\n" + + "}\n" + + "int32_to_message_field {\n" + + " key: 2\n" + + " value {\n" + + " value: 20\n" + + " }\n" + + "}", + message2); + } + // Asserts that the toString() representation of the message matches the expected. This verifies // the first line starts with a comment; but, does not factor in said comment as part of the // comparison as it contains unstable addresses. @@ -1644,38 +1538,38 @@ public class LiteTest extends TestCase { } assertEquals(expected, toString); } - + public void testParseLazy() throws Exception { - ByteString bb = TestAllTypesLite.newBuilder() - .setOptionalLazyMessage(NestedMessage.newBuilder() - .setBb(11) - .build()) - .build().toByteString(); - ByteString cc = TestAllTypesLite.newBuilder() - .setOptionalLazyMessage(NestedMessage.newBuilder() - .setCc(22) - .build()) - .build().toByteString(); - + ByteString bb = + TestAllTypesLite.newBuilder() + .setOptionalLazyMessage(NestedMessage.newBuilder().setBb(11).build()) + .build() + .toByteString(); + ByteString cc = + TestAllTypesLite.newBuilder() + .setOptionalLazyMessage(NestedMessage.newBuilder().setCc(22).build()) + .build() + .toByteString(); + ByteString concat = bb.concat(cc); TestAllTypesLite message = TestAllTypesLite.parseFrom(concat); assertEquals(11, message.getOptionalLazyMessage().getBb()); assertEquals(22L, message.getOptionalLazyMessage().getCc()); } - + public void testParseLazy_oneOf() throws Exception { - ByteString bb = TestAllTypesLite.newBuilder() - .setOneofLazyNestedMessage(NestedMessage.newBuilder() - .setBb(11) - .build()) - .build().toByteString(); - ByteString cc = TestAllTypesLite.newBuilder() - .setOneofLazyNestedMessage(NestedMessage.newBuilder() - .setCc(22) - .build()) - .build().toByteString(); - + ByteString bb = + TestAllTypesLite.newBuilder() + .setOneofLazyNestedMessage(NestedMessage.newBuilder().setBb(11).build()) + .build() + .toByteString(); + ByteString cc = + TestAllTypesLite.newBuilder() + .setOneofLazyNestedMessage(NestedMessage.newBuilder().setCc(22).build()) + .build() + .toByteString(); + ByteString concat = bb.concat(cc); TestAllTypesLite message = TestAllTypesLite.parseFrom(concat); @@ -1684,27 +1578,26 @@ public class LiteTest extends TestCase { } public void testMergeFromStream_repeatedField() throws Exception { - TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() - .addRepeatedString("hello"); + TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder().addRepeatedString("hello"); builder.mergeFrom(CodedInputStream.newInstance(builder.build().toByteArray())); assertEquals(2, builder.getRepeatedStringCount()); } public void testMergeFromStream_invalidBytes() throws Exception { - TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() - .setDefaultBool(true); + TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder().setDefaultBool(true); try { builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(Internal.UTF_8))); fail(); - } catch (InvalidProtocolBufferException expected) {} + } catch (InvalidProtocolBufferException expected) { + } } public void testMergeFrom_sanity() throws Exception { TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); byte[] bytes = one.toByteArray(); TestAllTypesLite two = TestAllTypesLite.parseFrom(bytes); - + one = one.toBuilder().mergeFrom(one).build(); two = two.toBuilder().mergeFrom(bytes).build(); assertEquals(one, two); @@ -1713,14 +1606,14 @@ public class LiteTest extends TestCase { } public void testMergeFromNoLazyFieldSharing() throws Exception { - TestAllTypesLite.Builder sourceBuilder = TestAllTypesLite.newBuilder().setOptionalLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(1)); + TestAllTypesLite.Builder sourceBuilder = + TestAllTypesLite.newBuilder() + .setOptionalLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(1)); TestAllTypesLite.Builder targetBuilder = TestAllTypesLite.newBuilder().mergeFrom(sourceBuilder.build()); assertEquals(1, sourceBuilder.getOptionalLazyMessage().getBb()); // now change the sourceBuilder, and target value shouldn't be affected. - sourceBuilder.setOptionalLazyMessage( - TestAllTypesLite.NestedMessage.newBuilder().setBb(2)); + sourceBuilder.setOptionalLazyMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(2)); assertEquals(1, targetBuilder.getOptionalLazyMessage().getBb()); } @@ -1728,508 +1621,373 @@ public class LiteTest extends TestCase { TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); byte[] bytes = one.toByteArray(); TestAllTypesLite two = one.toBuilder().mergeFrom(one).mergeFrom(bytes).build(); - + assertFalse(one.equals(two)); assertFalse(two.equals(one)); - + assertFalse(one.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(one)); - - TestAllTypesLite oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultBool(true) - .build(); + + TestAllTypesLite oneFieldSet = TestAllTypesLite.newBuilder().setDefaultBool(true).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultCord("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultCord("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultCordBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultCordBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultDouble(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultDouble(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultFixed32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultFixed32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultFixed64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultFixed64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultFloat(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultFloat(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder() + .setDefaultForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultImportEnum(ImportEnumLite.IMPORT_LITE_BAR) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder().setDefaultImportEnum(ImportEnumLite.IMPORT_LITE_BAR).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultInt32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultInt32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultInt64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultInt64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultNestedEnum(NestedEnum.BAR) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultNestedEnum(NestedEnum.BAR).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultSfixed32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultSfixed32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultSfixed64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultSfixed64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultSint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultSint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultSint64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultSint64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultString("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultString("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultStringBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultStringBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultStringPiece("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultStringPiece("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultStringPieceBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder().setDefaultStringPieceBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultUint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultUint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setDefaultUint64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setDefaultUint64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedBool(true) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedBool(true).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedCord("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedCord("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedCordBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedCordBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedDouble(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedDouble(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedFixed32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedFixed32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedFixed64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedFixed64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedFloat(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedFloat(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder() + .addRepeatedForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedImportEnum(ImportEnumLite.IMPORT_LITE_BAR) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder().addRepeatedImportEnum(ImportEnumLite.IMPORT_LITE_BAR).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedInt32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedInt32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedInt64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedInt64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedNestedEnum(NestedEnum.BAR) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedNestedEnum(NestedEnum.BAR).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedSfixed32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedSfixed32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedSfixed64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedSfixed64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedSint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedSint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedSint64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedSint64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedString("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedString("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedStringBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedStringBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedStringPiece("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedStringPiece("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedStringPieceBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder().addRepeatedStringPieceBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedUint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedUint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedUint64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().addRepeatedUint64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalBool(true) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalBool(true).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalCord("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalCord("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalCordBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalCordBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalDouble(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalDouble(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalFixed32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalFixed32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalFixed64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalFixed64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalFloat(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalFloat(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder() + .setOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_BAR) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAR) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder().setOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAR).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalInt32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalInt32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalInt64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalInt64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalNestedEnum(NestedEnum.BAR) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalNestedEnum(NestedEnum.BAR).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalSfixed32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalSfixed32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalSfixed64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalSfixed64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalSint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalSint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalSint64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalSint64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalString("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalString("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalStringBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalStringBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalStringPiece("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalStringPiece("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalStringPieceBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder().setOptionalStringPieceBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalUint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalUint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalUint64(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOptionalUint64(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOneofBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOneofBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOneofLazyNestedMessage(NestedMessage.getDefaultInstance()) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder() + .setOneofLazyNestedMessage(NestedMessage.getDefaultInstance()) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOneofNestedMessage(NestedMessage.getDefaultInstance()) - .build(); + + oneFieldSet = + TestAllTypesLite.newBuilder() + .setOneofNestedMessage(NestedMessage.getDefaultInstance()) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOneofString("") - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOneofString("").build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOneofStringBytes(ByteString.EMPTY) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOneofStringBytes(ByteString.EMPTY).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - - oneFieldSet = TestAllTypesLite.newBuilder() - .setOneofUint32(0) - .build(); + + oneFieldSet = TestAllTypesLite.newBuilder().setOneofUint32(0).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalForeignMessage(ForeignMessageLite.getDefaultInstance()) - .build(); + oneFieldSet = + TestAllTypesLite.newBuilder() + .setOptionalForeignMessage(ForeignMessageLite.getDefaultInstance()) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalGroup(OptionalGroup.getDefaultInstance()) - .build(); + oneFieldSet = + TestAllTypesLite.newBuilder().setOptionalGroup(OptionalGroup.getDefaultInstance()).build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalPublicImportMessage(PublicImportMessageLite.getDefaultInstance()) - .build(); + oneFieldSet = + TestAllTypesLite.newBuilder() + .setOptionalPublicImportMessage(PublicImportMessageLite.getDefaultInstance()) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - oneFieldSet = TestAllTypesLite.newBuilder() - .setOptionalLazyMessage(NestedMessage.getDefaultInstance()) - .build(); + oneFieldSet = + TestAllTypesLite.newBuilder() + .setOptionalLazyMessage(NestedMessage.getDefaultInstance()) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); - + assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); - oneFieldSet = TestAllTypesLite.newBuilder() - .addRepeatedLazyMessage(NestedMessage.getDefaultInstance()) - .build(); + oneFieldSet = + TestAllTypesLite.newBuilder() + .addRepeatedLazyMessage(NestedMessage.getDefaultInstance()) + .build(); assertFalse(oneFieldSet.equals(TestAllTypesLite.getDefaultInstance())); assertFalse(TestAllTypesLite.getDefaultInstance().equals(oneFieldSet)); } public void testEquals() throws Exception { // Check that two identical objs are equal. - Foo foo1a = Foo.newBuilder() - .setValue(1) - .addBar(Bar.newBuilder().setName("foo1")) - .build(); - Foo foo1b = Foo.newBuilder() - .setValue(1) - .addBar(Bar.newBuilder().setName("foo1")) - .build(); - Foo foo2 = Foo.newBuilder() - .setValue(1) - .addBar(Bar.newBuilder().setName("foo2")) - .build(); + Foo foo1a = Foo.newBuilder().setValue(1).addBar(Bar.newBuilder().setName("foo1")).build(); + Foo foo1b = Foo.newBuilder().setValue(1).addBar(Bar.newBuilder().setName("foo1")).build(); + Foo foo2 = Foo.newBuilder().setValue(1).addBar(Bar.newBuilder().setName("foo2")).build(); // Check that equals is doing value rather than object equality. assertEquals(foo1a, foo1b); @@ -2246,17 +2004,10 @@ public class LiteTest extends TestCase { } public void testEqualsAndHashCodeForTrickySchemaTypes() { - Foo foo1 = Foo.newBuilder() - .build(); - Foo foo2 = Foo.newBuilder() - .setSint64(1) - .build(); - Foo foo3 = Foo.newBuilder() - .putMyMap("key", "value2") - .build(); - Foo foo4 = Foo.newBuilder() - .setMyGroup(Foo.MyGroup.newBuilder().setValue(4).build()) - .build(); + Foo foo1 = Foo.getDefaultInstance(); + Foo foo2 = Foo.newBuilder().setSint64(1).build(); + Foo foo3 = Foo.newBuilder().putMyMap("key", "value2").build(); + Foo foo4 = Foo.newBuilder().setMyGroup(Foo.MyGroup.newBuilder().setValue(4).build()).build(); assertEqualsAndHashCodeAreFalse(foo1, foo2); assertEqualsAndHashCodeAreFalse(foo1, foo3); @@ -2272,29 +2023,27 @@ public class LiteTest extends TestCase { TestOneofEquals message2 = builder.build(); assertFalse(message1.equals(message2)); } - + public void testEquals_sanity() throws Exception { TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); TestAllTypesLite two = TestAllTypesLite.parseFrom(one.toByteArray()); assertEquals(one, two); assertEquals(one.hashCode(), two.hashCode()); - + assertEquals( one.toBuilder().mergeFrom(two).build(), two.toBuilder().mergeFrom(two.toByteArray()).build()); } public void testEqualsAndHashCodeWithUnknownFields() throws InvalidProtocolBufferException { - Foo fooWithOnlyValue = Foo.newBuilder() - .setValue(1) - .build(); + Foo fooWithOnlyValue = Foo.newBuilder().setValue(1).build(); - Foo fooWithValueAndExtension = fooWithOnlyValue.toBuilder() - .setValue(1) - .setExtension(Bar.fooExt, Bar.newBuilder() - .setName("name") - .build()) - .build(); + Foo fooWithValueAndExtension = + fooWithOnlyValue + .toBuilder() + .setValue(1) + .setExtension(Bar.fooExt, Bar.newBuilder().setName("name").build()) + .build(); Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.toByteArray()); @@ -2303,30 +2052,27 @@ public class LiteTest extends TestCase { } public void testEqualsAndHashCodeWithExtensions() throws InvalidProtocolBufferException { - Foo fooWithOnlyValue = Foo.newBuilder() - .setValue(1) - .build(); + Foo fooWithOnlyValue = Foo.newBuilder().setValue(1).build(); - Foo fooWithValueAndExtension = fooWithOnlyValue.toBuilder() - .setValue(1) - .setExtension(Bar.fooExt, Bar.newBuilder() - .setName("name") - .build()) - .build(); + Foo fooWithValueAndExtension = + fooWithOnlyValue + .toBuilder() + .setValue(1) + .setExtension(Bar.fooExt, Bar.newBuilder().setName("name").build()) + .build(); assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndExtension); } - + // Test to ensure we avoid a class cast exception with oneofs. public void testEquals_oneOfMessages() { - TestAllTypesLite mine = TestAllTypesLite.newBuilder() - .setOneofString("Hello") - .build(); - - TestAllTypesLite other = TestAllTypesLite.newBuilder() - .setOneofNestedMessage(NestedMessage.getDefaultInstance()) - .build(); - + TestAllTypesLite mine = TestAllTypesLite.newBuilder().setOneofString("Hello").build(); + + TestAllTypesLite other = + TestAllTypesLite.newBuilder() + .setOneofNestedMessage(NestedMessage.getDefaultInstance()) + .build(); + assertFalse(mine.equals(other)); assertFalse(other.equals(mine)); } @@ -2376,16 +2122,13 @@ public class LiteTest extends TestCase { public void testParseFromByteBufferThrows() { try { - TestAllTypesLite.parseFrom(ByteBuffer.wrap(new byte[] { 0x5 })); + TestAllTypesLite.parseFrom(ByteBuffer.wrap(new byte[] {0x5})); fail(); } catch (InvalidProtocolBufferException expected) { } TestAllTypesLite message = - TestAllTypesLite.newBuilder() - .setOptionalInt32(123) - .addRepeatedString("hello") - .build(); + TestAllTypesLite.newBuilder().setOptionalInt32(123).addRepeatedString("hello").build(); ByteBuffer buffer = ByteBuffer.wrap(message.toByteArray(), 0, message.getSerializedSize() - 1); try { @@ -2393,9 +2136,7 @@ public class LiteTest extends TestCase { fail(); } catch (InvalidProtocolBufferException expected) { assertEquals( - TestAllTypesLite.newBuilder() - .setOptionalInt32(123) - .build(), + TestAllTypesLite.newBuilder().setOptionalInt32(123).build(), expected.getUnfinishedMessage()); } } @@ -2425,7 +2166,7 @@ public class LiteTest extends TestCase { ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); UnittestLite.registerAllExtensions(registry); try { - TestAllExtensionsLite.parseFrom(ByteBuffer.wrap(new byte[] { 0x5 }), registry); + TestAllExtensionsLite.parseFrom(ByteBuffer.wrap(new byte[] {0x5}), registry); fail(); } catch (InvalidProtocolBufferException expected) { } @@ -2466,26 +2207,24 @@ public class LiteTest extends TestCase { } public void testAddAllIteratesOnce() { - TestAllTypesLite message = - TestAllTypesLite.newBuilder() - .addAllRepeatedBool(new OneTimeIterableList(false)) - .addAllRepeatedInt32(new OneTimeIterableList(0)) - .addAllRepeatedInt64(new OneTimeIterableList(0L)) - .addAllRepeatedFloat(new OneTimeIterableList(0f)) - .addAllRepeatedDouble(new OneTimeIterableList(0d)) - .addAllRepeatedBytes(new OneTimeIterableList(ByteString.EMPTY)) - .addAllRepeatedString(new OneTimeIterableList("")) - .addAllRepeatedNestedMessage( - new OneTimeIterableList(NestedMessage.getDefaultInstance())) - .addAllRepeatedBool(new OneTimeIterable(false)) - .addAllRepeatedInt32(new OneTimeIterable(0)) - .addAllRepeatedInt64(new OneTimeIterable(0L)) - .addAllRepeatedFloat(new OneTimeIterable(0f)) - .addAllRepeatedDouble(new OneTimeIterable(0d)) - .addAllRepeatedBytes(new OneTimeIterable(ByteString.EMPTY)) - .addAllRepeatedString(new OneTimeIterable("")) - .addAllRepeatedNestedMessage(new OneTimeIterable(NestedMessage.getDefaultInstance())) - .build(); + TestAllTypesLite.newBuilder() + .addAllRepeatedBool(new OneTimeIterableList(false)) + .addAllRepeatedInt32(new OneTimeIterableList(0)) + .addAllRepeatedInt64(new OneTimeIterableList(0L)) + .addAllRepeatedFloat(new OneTimeIterableList(0f)) + .addAllRepeatedDouble(new OneTimeIterableList(0d)) + .addAllRepeatedBytes(new OneTimeIterableList(ByteString.EMPTY)) + .addAllRepeatedString(new OneTimeIterableList("")) + .addAllRepeatedNestedMessage(new OneTimeIterableList(NestedMessage.getDefaultInstance())) + .addAllRepeatedBool(new OneTimeIterable(false)) + .addAllRepeatedInt32(new OneTimeIterable(0)) + .addAllRepeatedInt64(new OneTimeIterable(0L)) + .addAllRepeatedFloat(new OneTimeIterable(0f)) + .addAllRepeatedDouble(new OneTimeIterable(0d)) + .addAllRepeatedBytes(new OneTimeIterable(ByteString.EMPTY)) + .addAllRepeatedString(new OneTimeIterable("")) + .addAllRepeatedNestedMessage(new OneTimeIterable(NestedMessage.getDefaultInstance())) + .build(); } public void testAddAllIteratesOnce_throwsOnNull() { diff --git a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java index 6bbdfcaa..e50c7d1e 100644 --- a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java @@ -32,6 +32,7 @@ package com.google.protobuf; import static java.util.Arrays.asList; +import com.google.protobuf.Internal.LongList; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; @@ -297,6 +298,20 @@ public class LongArrayListTest extends TestCase { } } + public void testRemoveEndOfCapacity() { + LongList toRemove = LongArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addLong(3); + toRemove.remove(0); + assertEquals(0, toRemove.size()); + } + + public void testSublistRemoveEndOfCapacity() { + LongList toRemove = LongArrayList.emptyList().mutableCopyWithCapacity(1); + toRemove.addLong(3); + toRemove.subList(0, 1).clear(); + assertEquals(0, toRemove.size()); + } + private void assertImmutable(LongArrayList list) { if (list.contains(1L)) { throw new RuntimeException("Cannot test the immutability of lists that contain 1."); diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java index 37827f76..bcfd927c 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java @@ -788,6 +788,24 @@ public class MapForProto2Test extends TestCase { assertEquals(message.hashCode(), dynamicMessage.hashCode()); } + // Check that DynamicMessage handles map field serialization the same way as generated code + // regarding unset key and value field in a map entry. + public void testDynamicMessageUnsetKeyAndValue() throws Exception { + FieldDescriptor field = f("int32_to_int32_field"); + + Message dynamicDefaultInstance = + DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message.Builder builder = dynamicDefaultInstance.newBuilderForType(); + // Add an entry without key and value. + builder.addRepeatedField(field, builder.newBuilderForField(field).build()); + Message message = builder.build(); + ByteString bytes = message.toByteString(); + // Parse it back to the same generated type. + Message generatedMessage = TestMap.parseFrom(bytes); + // Assert the serialized bytes are equivalent. + assertEquals(generatedMessage.toByteString(), bytes); + } + public void testReflectionEqualsAndHashCode() throws Exception { // Test that generated equals() and hashCode() will disregard the order // of map entries when comparing/hashing map fields. diff --git a/java/core/src/test/java/com/google/protobuf/MapTest.java b/java/core/src/test/java/com/google/protobuf/MapTest.java index 64ae4435..58efce92 100644 --- a/java/core/src/test/java/com/google/protobuf/MapTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapTest.java @@ -893,6 +893,24 @@ public class MapTest extends TestCase { assertEquals(message.hashCode(), dynamicMessage.hashCode()); } + // Check that DynamicMessage handles map field serialization the same way as generated code + // regarding unset key and value field in a map entry. + public void testDynamicMessageUnsetKeyAndValue() throws Exception { + FieldDescriptor field = f("int32_to_int32_field"); + + Message dynamicDefaultInstance = + DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); + Message.Builder builder = dynamicDefaultInstance.newBuilderForType(); + // Add an entry without key and value. + builder.addRepeatedField(field, builder.newBuilderForField(field).build()); + Message message = builder.build(); + ByteString bytes = message.toByteString(); + // Parse it back to the same generated type. + Message generatedMessage = TestMap.parseFrom(bytes); + // Assert the serialized bytes are equivalent. + assertEquals(generatedMessage.toByteString(), bytes); + } + public void testReflectionEqualsAndHashCode() throws Exception { // Test that generated equals() and hashCode() will disregard the order // of map entries when comparing/hashing map fields. diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java index 5d38ca51..720061d2 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -61,12 +61,11 @@ import junit.framework.TestCase; public class TextFormatTest extends TestCase { // A basic string with different escapable characters for testing. - private final static String kEscapeTestString = - "\"A string with ' characters \n and \r newlines and \t tabs and \001 " - + "slashes \\"; + private static final String ESCAPE_TEST_STRING = + "\"A string with ' characters \n and \r newlines and \t tabs and \001 " + "slashes \\"; // A representation of the above string with all the characters escaped. - private final static String kEscapeTestStringEscaped = + private static final String ESCAPE_TEST_STRING_ESCAPED = "\\\"A string with \\' characters \\n and \\r newlines " + "and \\t tabs and \\001 slashes \\\\"; @@ -576,10 +575,10 @@ public class TextFormatTest extends TestCase { "integer: 82301481290849012385230157", "optional_int32: 82301481290849012385230157"); assertParseError( - "1:16: Expected \"true\" or \"false\".", + "1:16: Expected \"true\" or \"false\". Found \"maybe\".", "optional_bool: maybe"); assertParseError( - "1:16: Expected \"true\" or \"false\".", + "1:16: Expected \"true\" or \"false\". Found \"2\".", "optional_bool: 2"); assertParseError( "1:18: Expected string.", @@ -643,10 +642,8 @@ public class TextFormatTest extends TestCase { TextFormat.unescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"")); assertEquals("\0\001\007\b\f\n\r\t\013\\\'\"", TextFormat.unescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"")); - assertEquals(kEscapeTestStringEscaped, - TextFormat.escapeText(kEscapeTestString)); - assertEquals(kEscapeTestString, - TextFormat.unescapeText(kEscapeTestStringEscaped)); + assertEquals(ESCAPE_TEST_STRING_ESCAPED, TextFormat.escapeText(ESCAPE_TEST_STRING)); + assertEquals(ESCAPE_TEST_STRING, TextFormat.unescapeText(ESCAPE_TEST_STRING_ESCAPED)); // Invariant assertEquals("hello", diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java index 9928d44a..8ce0ca73 100644 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java @@ -30,6 +30,8 @@ package com.google.protobuf; +import static junit.framework.TestCase.assertEquals; + import com.google.protobuf.UnittestLite.TestAllExtensionsLite; import com.google.protobuf.UnittestLite.TestAllTypesLite; import protobuf_unittest.UnittestProto; @@ -133,6 +135,25 @@ public class UnknownFieldSetLiteTest extends TestCase { assertEquals(foo.toByteString().size(), instance.getSerializedSize()); } + public void testHashCodeAfterDeserialization() throws IOException { + Foo foo = Foo.newBuilder() + .setValue(2) + .build(); + + Foo fooDeserialized = Foo.parseFrom(foo.toByteArray()); + + assertEquals(fooDeserialized, foo); + assertEquals(foo.hashCode(), fooDeserialized.hashCode()); + } + + public void testNewInstanceHashCode() { + UnknownFieldSetLite emptyFieldSet = UnknownFieldSetLite.getDefaultInstance(); + UnknownFieldSetLite paddedFieldSet = UnknownFieldSetLite.newInstance(); + + assertEquals(emptyFieldSet, paddedFieldSet); + assertEquals(emptyFieldSet.hashCode(), paddedFieldSet.hashCode()); + } + public void testMergeVarintField() throws IOException { UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance(); unknownFields.mergeVarintField(10, 2); diff --git a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java index 625d4b42..03c33ecf 100644 --- a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java @@ -36,6 +36,7 @@ import protobuf_unittest.UnittestMset.TestMessageSetExtension2; import protobuf_unittest.UnittestProto; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; +import protobuf_unittest.UnittestProto.TestExtensionInsideTable; import protobuf_unittest.UnittestProto.TestFieldOrderings; import protobuf_unittest.UnittestProto.TestOneof2; import protobuf_unittest.UnittestProto.TestOneofBackwardsCompatible; @@ -235,6 +236,26 @@ public class WireFormatTest extends TestCase { getTestFieldOrderingsRegistry()); assertEquals(source, dest); } + + private static ExtensionRegistry getTestExtensionInsideTableRegistry() { + ExtensionRegistry result = ExtensionRegistry.newInstance(); + result.add(UnittestProto.testExtensionInsideTableExtension); + return result; + } + + public void testExtensionInsideTable() throws Exception { + // Make sure the extension within the range of table is parsed correctly in experimental + // runtime. + TestExtensionInsideTable source = + TestExtensionInsideTable.newBuilder() + .setField1(1) + .setExtension(UnittestProto.testExtensionInsideTableExtension, 23) + .build(); + TestExtensionInsideTable dest = + TestExtensionInsideTable.parseFrom(source.toByteString(), + getTestExtensionInsideTableRegistry()); + assertEquals(source, dest); + } public void testParseMultipleExtensionRangesDynamic() throws Exception { // Same as above except with DynamicMessage. -- cgit v1.2.3