aboutsummaryrefslogtreecommitdiffhomepage
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/README.md81
-rw-r--r--java/core/generate-test-sources-build.xml1
-rw-r--r--java/core/pom.xml3
-rw-r--r--java/core/src/main/java/com/google/protobuf/AbstractMessage.java10
-rw-r--r--java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java10
-rw-r--r--java/core/src/main/java/com/google/protobuf/Android.java57
-rw-r--r--java/core/src/main/java/com/google/protobuf/BooleanArrayList.java16
-rw-r--r--java/core/src/main/java/com/google/protobuf/ByteString.java10
-rw-r--r--java/core/src/main/java/com/google/protobuf/CodedInputStream.java102
-rw-r--r--java/core/src/main/java/com/google/protobuf/CodedOutputStream.java18
-rw-r--r--java/core/src/main/java/com/google/protobuf/DoubleArrayList.java16
-rw-r--r--java/core/src/main/java/com/google/protobuf/DynamicMessage.java18
-rw-r--r--java/core/src/main/java/com/google/protobuf/FieldSet.java5
-rw-r--r--java/core/src/main/java/com/google/protobuf/FloatArrayList.java16
-rw-r--r--java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java164
-rw-r--r--java/core/src/main/java/com/google/protobuf/IntArrayList.java16
-rw-r--r--java/core/src/main/java/com/google/protobuf/LongArrayList.java16
-rw-r--r--java/core/src/main/java/com/google/protobuf/MessageLiteToString.java96
-rw-r--r--java/core/src/main/java/com/google/protobuf/TextFormat.java86
-rw-r--r--java/core/src/main/java/com/google/protobuf/UnknownFieldSetLite.java24
-rw-r--r--java/core/src/main/java/com/google/protobuf/UnsafeUtil.java30
-rw-r--r--java/core/src/main/java/com/google/protobuf/Utf8.java478
-rw-r--r--java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java12
-rw-r--r--java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java15
-rw-r--r--java/core/src/test/java/com/google/protobuf/CheckUtf8Test.java43
-rw-r--r--java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java325
-rw-r--r--java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java20
-rw-r--r--java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java15
-rw-r--r--java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java15
-rw-r--r--java/core/src/test/java/com/google/protobuf/IntArrayListTest.java15
-rw-r--r--java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java9
-rw-r--r--java/core/src/test/java/com/google/protobuf/LiteTest.java1453
-rw-r--r--java/core/src/test/java/com/google/protobuf/LongArrayListTest.java15
-rw-r--r--java/core/src/test/java/com/google/protobuf/MapForProto2Test.java18
-rw-r--r--java/core/src/test/java/com/google/protobuf/MapTest.java18
-rw-r--r--java/core/src/test/java/com/google/protobuf/TestUtil.java6
-rw-r--r--java/core/src/test/java/com/google/protobuf/TextFormatTest.java105
-rw-r--r--java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java21
-rw-r--r--java/core/src/test/java/com/google/protobuf/WireFormatTest.java21
-rw-r--r--java/core/src/test/proto/com/google/protobuf/map_lite_test.proto111
-rw-r--r--java/lite.md50
-rw-r--r--java/lite/generate-sources-build.xml20
-rw-r--r--java/lite/generate-test-sources-build.xml43
-rw-r--r--java/lite/pom.xml185
-rw-r--r--java/pom.xml9
-rw-r--r--java/util/pom.xml2
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java2
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/JsonFormat.java57
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/Timestamps.java11
-rw-r--r--java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java15
50 files changed, 2576 insertions, 1328 deletions
diff --git a/java/README.md b/java/README.md
index 0e0fba64..5e4fb8b4 100644
--- a/java/README.md
+++ b/java/README.md
@@ -1,17 +1,65 @@
-Protocol Buffers - Google's data interchange format
-===================================================
-
-[![Build Status](https://travis-ci.org/google/protobuf.svg?branch=master)](https://travis-ci.org/google/protobuf)
+# Protocol Buffers - Google's data interchange format
Copyright 2008 Google Inc.
-This directory contains the Java Protocol Buffers runtime library.
+https://developers.google.com/protocol-buffers/
+
+## Use Java Protocol Buffers
+
+To use protobuf in Java, first obtain the protocol compiler (a.k.a., protoc,
+see instructions in the toplevel [README.md](../README.md)) and use it to
+generate Java code for your .proto files:
+
+ $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file
+
+Include the generated Java files in your project and add a dependency on the
+protobuf Java runtime. If you are using Maven, use the following:
+
+```xml
+<dependency>
+ <groupId>com.google.protobuf</groupId>
+ <artifactId>protobuf-java</artifactId>
+ <version>3.5.1</version>
+</dependency>
+```
+
+Make sure the version number of the runtime matches (or is newer than) the
+version number of the protoc.
+
+If you want to use features like protobuf JsonFormat, add a dependency on the
+protobuf-java-util package:
+
+```xml
+<dependency>
+ <groupId>com.google.protobuf</groupId>
+ <artifactId>protobuf-java-util</artifactId>
+ <version>3.5.1</version>
+</dependency>
+```
+
+### Use Java Protocol Buffers on Android
+
+For Android users, it's recommended to use protobuf Java Lite runtime because
+of its smaller code size. Java Lite runtime also works better with Proguard
+because it doesn't rely on Java reflection and is optimized to allow as much
+code stripping as possible. You can following these [instructions to use Java
+Lite runtime](lite.md).
+
+### Use Java Protocol Buffers with Bazel
+
+Bazel has native build rules to work with protobuf. For Java, you can use the
+`java_proto_library` rule for server and the `java_lite_proto_library` rule
+for Android. Check out [our build files examples](../examples/BUILD) to learn
+how to use them.
+
+## Build from Source
-Installation - With Maven
-=========================
+Most users should follow the instructions above to use protobuf Java runtime.
+If you are contributing code to protobuf or want to use a protobuf version
+that hasn't been officially released yet, you can folllow the instructions
+below to build protobuf from source code.
-The Protocol Buffers build is managed using Maven. If you would
-rather build without Maven, see below.
+### Build from Source - With Maven
1) Install Apache Maven if you don't have it:
@@ -45,20 +93,15 @@ rather build without Maven, see below.
The .jar will be placed in the "target" directory.
-The above instructions will install 3 maven artifacts:
+The above instructions will install 2 maven artifacts:
* protobuf-java: The core Java Protocol Buffers library. Most users only
need this artifact.
- * protobuf-lite: The lite version of core Java Protobuf Buffers library. It
- is a subset of the core library and is used together with
- the 'lite' code generator flag to reduce generated code size
- for mobile.
* protobuf-java-util: Utilities to work with protos. It contains JSON support
as well as utilities to work with proto3 well-known
types.
-Installation - Without Maven
-============================
+### Build from Source - Without Maven
If you would rather not install Maven to build the library, you may
follow these instructions instead. Note that these instructions skip
@@ -83,8 +126,7 @@ library (without the util package).
4) Install the classes wherever you prefer.
-Compatibility Notice
-====================
+## Compatibility Notice
* Protobuf minor version releases are backwards-compatible. If your code
can build/run against the old version, it's expected to build/run against
@@ -118,8 +160,7 @@ Compatibility Notice
* Protobuf LITE runtime APIs are not stable yet. They are subject to change even
in minor version releases.
-Documentation
-=============
+## Documentation
The complete documentation for Protocol Buffers is available via the
web at:
diff --git a/java/core/generate-test-sources-build.xml b/java/core/generate-test-sources-build.xml
index abcdf5dd..1d11f131 100644
--- a/java/core/generate-test-sources-build.xml
+++ b/java/core/generate-test-sources-build.xml
@@ -39,6 +39,7 @@
<arg value="${test.proto.dir}/com/google/protobuf/field_presence_test.proto"/>
<arg value="${test.proto.dir}/com/google/protobuf/map_for_proto2_lite_test.proto"/>
<arg value="${test.proto.dir}/com/google/protobuf/map_for_proto2_test.proto"/>
+ <arg value="${test.proto.dir}/com/google/protobuf/map_lite_test.proto"/>
<arg value="${test.proto.dir}/com/google/protobuf/map_test.proto"/>
<arg value="${test.proto.dir}/com/google/protobuf/map_initialization_order_test.proto"/>
</exec>
diff --git a/java/core/pom.xml b/java/core/pom.xml
index b0925f74..a7d4ea37 100644
--- a/java/core/pom.xml
+++ b/java/core/pom.xml
@@ -6,7 +6,7 @@
<parent>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-parent</artifactId>
- <version>3.4.1</version>
+ <version>3.6.0</version>
</parent>
<artifactId>protobuf-java</artifactId>
@@ -135,6 +135,7 @@
<Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
<Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName>
<Export-Package>com.google.protobuf;version=${project.version}</Export-Package>
+ <Import-Package>sun.misc;resolution:=optional,*</Import-Package>
</instructions>
</configuration>
</plugin>
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
@@ -125,6 +125,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;
if (size != -1) {
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/Android.java b/java/core/src/main/java/com/google/protobuf/Android.java
new file mode 100644
index 00000000..cad54783
--- /dev/null
+++ b/java/core/src/main/java/com/google/protobuf/Android.java
@@ -0,0 +1,57 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+package com.google.protobuf;
+
+final class Android {
+
+ private static final Class<?> MEMORY_CLASS = getClassForName("libcore.io.Memory");
+ private static final boolean IS_ROBOLECTRIC =
+ getClassForName("org.robolectric.Robolectric") != null;
+
+ /** Returns {@code true} if running on an Android device. */
+ static boolean isOnAndroidDevice() {
+ return MEMORY_CLASS != null && !IS_ROBOLECTRIC;
+ }
+
+ /** Returns the memory class or {@code null} if not on Android device. */
+ static Class<?> getMemoryClass() {
+ return MEMORY_CLASS;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T> Class<T> getClassForName(String name) {
+ try {
+ return (Class<T>) Class.forName(name);
+ } catch (Throwable e) {
+ return null;
+ }
+ }
+}
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
@@ -82,6 +82,18 @@ final class BooleanArrayList extends AbstractProtobufList<Boolean>
}
@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) {
return true;
@@ -246,7 +258,9 @@ final class BooleanArrayList extends AbstractProtobufList<Boolean>
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/ByteString.java b/java/core/src/main/java/com/google/protobuf/ByteString.java
index 99a31209..d67bb54a 100644
--- a/java/core/src/main/java/com/google/protobuf/ByteString.java
+++ b/java/core/src/main/java/com/google/protobuf/ByteString.java
@@ -124,14 +124,8 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
private static final ByteArrayCopier byteArrayCopier;
static {
- boolean isAndroid = true;
- try {
- Class.forName("android.content.Context");
- } catch (ClassNotFoundException e) {
- isAndroid = false;
- }
-
- byteArrayCopier = isAndroid ? new SystemByteArrayCopier() : new ArraysByteArrayCopier();
+ byteArrayCopier =
+ Android.isOnAndroidDevice() ? new SystemByteArrayCopier() : new ArraysByteArrayCopier();
}
/**
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 e08a993b..1297462e 100644
--- a/java/core/src/main/java/com/google/protobuf/CodedInputStream.java
+++ b/java/core/src/main/java/com/google/protobuf/CodedInputStream.java
@@ -64,6 +64,12 @@ public abstract class CodedInputStream {
// Integer.MAX_VALUE == 0x7FFFFFF == INT_MAX from limits.h
private static final int DEFAULT_SIZE_LIMIT = Integer.MAX_VALUE;
+ /**
+ * Whether to enable our custom UTF-8 decode codepath which does not use {@link StringCoding}.
+ * Currently disabled.
+ */
+ private static final boolean ENABLE_CUSTOM_UTF8_DECODE = false;
+
/** Visible for subclasses. See setRecursionLimit() */
int recursionDepth;
@@ -825,13 +831,19 @@ public abstract class CodedInputStream {
public String readStringRequireUtf8() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= (limit - pos)) {
- // TODO(martinrb): We could save a pass by validating while decoding.
- if (!Utf8.isValidUtf8(buffer, pos, pos + size)) {
- throw InvalidProtocolBufferException.invalidUtf8();
+ if (ENABLE_CUSTOM_UTF8_DECODE) {
+ String result = Utf8.decodeUtf8(buffer, pos, size);
+ pos += size;
+ return result;
+ } else {
+ // TODO(martinrb): We could save a pass by validating while decoding.
+ if (!Utf8.isValidUtf8(buffer, pos, pos + size)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ final int tempPos = pos;
+ pos += size;
+ return new String(buffer, tempPos, size, UTF_8);
}
- final int tempPos = pos;
- pos += size;
- return new String(buffer, tempPos, size, UTF_8);
}
if (size == 0) {
@@ -1524,6 +1536,8 @@ public abstract class CodedInputStream {
final int size = readRawVarint32();
if (size > 0 && size <= remaining()) {
// TODO(nathanmittler): Is there a way to avoid this copy?
+ // TODO(anuraaga): It might be possible to share the optimized loop with
+ // readStringRequireUtf8 by implementing Java replacement logic there.
// The same as readBytes' logic
byte[] bytes = new byte[size];
UnsafeUtil.copyMemory(pos, bytes, 0, size);
@@ -1544,19 +1558,26 @@ public abstract class CodedInputStream {
@Override
public String readStringRequireUtf8() throws IOException {
final int size = readRawVarint32();
- if (size >= 0 && size <= remaining()) {
- // TODO(nathanmittler): Is there a way to avoid this copy?
- // The same as readBytes' logic
- byte[] bytes = new byte[size];
- UnsafeUtil.copyMemory(pos, bytes, 0, size);
- // TODO(martinrb): We could save a pass by validating while decoding.
- if (!Utf8.isValidUtf8(bytes)) {
- throw InvalidProtocolBufferException.invalidUtf8();
- }
+ if (size > 0 && size <= remaining()) {
+ if (ENABLE_CUSTOM_UTF8_DECODE) {
+ final int bufferPos = bufferPos(pos);
+ String result = Utf8.decodeUtf8(buffer, bufferPos, size);
+ pos += size;
+ return result;
+ } else {
+ // TODO(nathanmittler): Is there a way to avoid this copy?
+ // The same as readBytes' logic
+ byte[] bytes = new byte[size];
+ UnsafeUtil.copyMemory(pos, bytes, 0, size);
+ // TODO(martinrb): We could save a pass by validating while decoding.
+ if (!Utf8.isValidUtf8(bytes)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
- String result = new String(bytes, UTF_8);
- pos += size;
- return result;
+ String result = new String(bytes, UTF_8);
+ pos += size;
+ return result;
+ }
}
if (size == 0) {
@@ -2324,11 +2345,15 @@ public abstract class CodedInputStream {
bytes = readRawBytesSlowPath(size);
tempPos = 0;
}
- // TODO(martinrb): We could save a pass by validating while decoding.
- if (!Utf8.isValidUtf8(bytes, tempPos, tempPos + size)) {
- throw InvalidProtocolBufferException.invalidUtf8();
+ if (ENABLE_CUSTOM_UTF8_DECODE) {
+ return Utf8.decodeUtf8(bytes, tempPos, size);
+ } else {
+ // TODO(martinrb): We could save a pass by validating while decoding.
+ if (!Utf8.isValidUtf8(bytes, tempPos, tempPos + size)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ return new String(bytes, tempPos, size, UTF_8);
}
- return new String(bytes, tempPos, size, UTF_8);
}
@Override
@@ -3348,23 +3373,34 @@ public abstract class CodedInputStream {
public String readStringRequireUtf8() throws IOException {
final int size = readRawVarint32();
if (size > 0 && size <= currentByteBufferLimit - currentByteBufferPos) {
- byte[] bytes = new byte[size];
- UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
- if (!Utf8.isValidUtf8(bytes)) {
- throw InvalidProtocolBufferException.invalidUtf8();
+ if (ENABLE_CUSTOM_UTF8_DECODE) {
+ final int bufferPos = (int) (currentByteBufferPos - currentByteBufferStartPos);
+ String result = Utf8.decodeUtf8(currentByteBuffer, bufferPos, size);
+ currentByteBufferPos += size;
+ return result;
+ } else {
+ byte[] bytes = new byte[size];
+ UnsafeUtil.copyMemory(currentByteBufferPos, bytes, 0, size);
+ if (!Utf8.isValidUtf8(bytes)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ String result = new String(bytes, UTF_8);
+ currentByteBufferPos += size;
+ return result;
}
- String result = new String(bytes, UTF_8);
- currentByteBufferPos += size;
- return result;
}
if (size >= 0 && size <= remaining()) {
byte[] bytes = new byte[size];
readRawBytesTo(bytes, 0, size);
- if (!Utf8.isValidUtf8(bytes)) {
- throw InvalidProtocolBufferException.invalidUtf8();
+ if (ENABLE_CUSTOM_UTF8_DECODE) {
+ return Utf8.decodeUtf8(bytes, 0, size);
+ } else {
+ if (!Utf8.isValidUtf8(bytes)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ String result = new String(bytes, UTF_8);
+ return result;
}
- String result = new String(bytes, UTF_8);
- return result;
}
if (size == 0) {
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
@@ -82,6 +82,18 @@ final class DoubleArrayList extends AbstractProtobufList<Double>
}
@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) {
return true;
@@ -247,7 +259,9 @@ final class DoubleArrayList extends AbstractProtobufList<Double>
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/FieldSet.java b/java/core/src/main/java/com/google/protobuf/FieldSet.java
index 8a9239ed..c09daa32 100644
--- a/java/core/src/main/java/com/google/protobuf/FieldSet.java
+++ b/java/core/src/main/java/com/google/protobuf/FieldSet.java
@@ -102,6 +102,11 @@ final class FieldSet<FieldDescriptorType extends
@SuppressWarnings("rawtypes")
private static final FieldSet DEFAULT_INSTANCE = new FieldSet(true);
+ /** Returns {@code true} if empty, {@code false} otherwise. */
+ boolean isEmpty() {
+ return fields.isEmpty();
+ }
+
/** Make this FieldSet immutable from this point forward. */
@SuppressWarnings("unchecked")
public void makeImmutable() {
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
@@ -82,6 +82,18 @@ final class FloatArrayList extends AbstractProtobufList<Float>
}
@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) {
return true;
@@ -246,7 +258,9 @@ final class FloatArrayList extends AbstractProtobufList<Float>
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 09084646..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;
+ }
+
/**
@@ -449,13 +463,41 @@ public abstract class GeneratedMessageLite<
}
@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,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
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();
@@ -504,11 +546,8 @@ public abstract class GeneratedMessageLite<
extends GeneratedMessageLite<MessageType, BuilderType>
implements ExtendableMessageOrBuilder<MessageType, BuilderType> {
- /**
- * Represents the set of extensions on this message. For use by generated
- * code only.
- */
- protected FieldSet<ExtensionDescriptor> extensions = FieldSet.newFieldSet();
+ /** Represents the set of extensions on this message. For use by generated code only. */
+ protected FieldSet<ExtensionDescriptor> extensions = FieldSet.emptySet();
@SuppressWarnings("unchecked")
protected final void mergeExtensionFields(final MessageType other) {
@@ -578,7 +617,9 @@ public abstract class GeneratedMessageLite<
if (unknown) { // Unknown field or wrong wire type. Skip.
return parseUnknownField(tag, input);
}
-
+
+ ensureExtensionsAreMutable();
+
if (packed) {
int length = input.readRawVarint32();
int limit = input.pushLimit(length);
@@ -793,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<ExtensionDescriptor> ensureExtensionsAreMutable() {
+ if (extensions.isImmutable()) {
+ extensions = extensions.clone();
+ }
+ return extensions;
}
private void verifyExtensionContainingType(
@@ -868,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
@@ -942,12 +993,6 @@ public abstract class GeneratedMessageLite<
implements ExtendableMessageOrBuilder<MessageType, BuilderType> {
protected ExtendableBuilder(MessageType defaultInstance) {
super(defaultInstance);
-
- // TODO(dweis): This is kind of an unnecessary clone since we construct a
- // new instance in the parent constructor which makes the extensions
- // immutable. This extra allocation shouldn't matter in practice
- // though.
- instance.extensions = instance.extensions.clone();
}
// For immutable message conversion.
@@ -966,6 +1011,15 @@ public abstract class GeneratedMessageLite<
instance.extensions = instance.extensions.clone();
}
+ private FieldSet<ExtensionDescriptor> ensureExtensionsAreMutable() {
+ FieldSet<ExtensionDescriptor> extensions = instance.extensions;
+ if (extensions.isImmutable()) {
+ extensions = extensions.clone();
+ instance.extensions = extensions;
+ }
+ return extensions;
+ }
+
@Override
public final MessageType buildPartial() {
if (isBuilt) {
@@ -1024,7 +1078,8 @@ public abstract class GeneratedMessageLite<
verifyExtensionContainingType(extensionLite);
copyOnWrite();
- instance.extensions.setField(extensionLite.descriptor, extensionLite.toFieldSetType(value));
+ ensureExtensionsAreMutable()
+ .setField(extensionLite.descriptor, extensionLite.toFieldSetType(value));
return (BuilderType) this;
}
@@ -1037,8 +1092,9 @@ public abstract class GeneratedMessageLite<
verifyExtensionContainingType(extensionLite);
copyOnWrite();
- instance.extensions.setRepeatedField(
- extensionLite.descriptor, index, extensionLite.singularToFieldSetType(value));
+ ensureExtensionsAreMutable()
+ .setRepeatedField(
+ extensionLite.descriptor, index, extensionLite.singularToFieldSetType(value));
return (BuilderType) this;
}
@@ -1051,8 +1107,8 @@ public abstract class GeneratedMessageLite<
verifyExtensionContainingType(extensionLite);
copyOnWrite();
- instance.extensions.addRepeatedField(
- extensionLite.descriptor, extensionLite.singularToFieldSetType(value));
+ ensureExtensionsAreMutable()
+ .addRepeatedField(extensionLite.descriptor, extensionLite.singularToFieldSetType(value));
return (BuilderType) this;
}
@@ -1063,7 +1119,7 @@ public abstract class GeneratedMessageLite<
verifyExtensionContainingType(extensionLite);
copyOnWrite();
- instance.extensions.clearField(extensionLite.descriptor);
+ ensureExtensionsAreMutable().clearField(extensionLite.descriptor);
return (BuilderType) this;
}
}
@@ -1462,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);
@@ -1471,10 +1532,6 @@ public abstract class GeneratedMessageLite<
return isInitialized;
}
- protected static final <T extends GeneratedMessageLite<T, ?>> void makeImmutable(T message) {
- message.dynamicMethod(MethodToInvoke.MAKE_IMMUTABLE);
- }
-
protected static IntList emptyIntList() {
return IntArrayList.emptyList();
}
@@ -1554,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);
+ }
}
/**
@@ -1567,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();
@@ -1578,6 +1653,34 @@ public abstract class GeneratedMessageLite<
return result;
}
+ /** A static helper method for parsing a partial from byte array. */
+ static <T extends GeneratedMessageLite<T, ?>> 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 extends GeneratedMessageLite<T, ?>> T parsePartialFrom(
T defaultInstance,
CodedInputStream input)
@@ -1674,8 +1777,7 @@ public abstract class GeneratedMessageLite<
protected static <T extends GeneratedMessageLite<T, ?>> 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
@@ -82,6 +82,18 @@ final class IntArrayList extends AbstractProtobufList<Integer>
}
@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) {
return true;
@@ -246,7 +258,9 @@ final class IntArrayList extends AbstractProtobufList<Integer>
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
@@ -82,6 +82,18 @@ final class LongArrayList extends AbstractProtobufList<Long>
}
@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) {
return true;
@@ -246,7 +258,9 @@ final class LongArrayList extends AbstractProtobufList<Long>
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.
*
* <p>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<String, Method> nameToNoArgMethod = new HashMap<String, Method>();
Map<String, Method> nameToMethod = new HashMap<String, Method>();
Set<String> getters = new TreeSet<String>();
@@ -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 ab9acf2f..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 + "\".");
}
}
@@ -1224,6 +1224,22 @@ public final class TextFormat {
}
/**
+ * Parse a text-format message from {@code input}.
+ *
+ * @return the parsed message, guaranteed initialized
+ */
+ public static <T extends Message> T parse(final CharSequence input,
+ final Class<T> protoClass)
+ throws ParseException {
+ Message.Builder builder =
+ Internal.getDefaultInstance(protoClass).newBuilderForType();
+ merge(input, builder);
+ @SuppressWarnings("unchecked")
+ T output = (T) builder.build();
+ return output;
+ }
+
+ /**
* Parse a text-format message from {@code input} and merge the contents
* into {@code builder}. Extensions will be recognized if they are
* registered in {@code extensionRegistry}.
@@ -1248,6 +1264,25 @@ public final class TextFormat {
PARSER.merge(input, extensionRegistry, builder);
}
+ /**
+ * Parse a text-format message from {@code input}. Extensions will be
+ * recognized if they are registered in {@code extensionRegistry}.
+ *
+ * @return the parsed message, guaranteed initialized
+ */
+ public static <T extends Message> T parse(
+ final CharSequence input,
+ final ExtensionRegistry extensionRegistry,
+ final Class<T> protoClass)
+ throws ParseException {
+ Message.Builder builder =
+ Internal.getDefaultInstance(protoClass).newBuilderForType();
+ merge(input, extensionRegistry, builder);
+ @SuppressWarnings("unchecked")
+ T output = (T) builder.build();
+ return output;
+ }
+
/**
* Parser for text-format proto2 instances. This class is thread-safe.
@@ -1276,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;
}
@@ -1299,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;
@@ -1320,7 +1360,10 @@ public final class TextFormat {
public Parser build() {
return new Parser(
- allowUnknownFields, singularOverwritePolicy, parseInfoTreeBuilder);
+ allowUnknownFields,
+ allowUnknownEnumValues,
+ singularOverwritePolicy,
+ parseInfoTreeBuilder);
}
}
@@ -1384,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<String> unknownFields)
throws ParseException {
@@ -1702,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 88315cb6..d84ef3c5 100644
--- a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java
+++ b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java
@@ -33,7 +33,6 @@ 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;
@@ -82,6 +81,7 @@ final class UnsafeUtil {
return HAS_UNSAFE_BYTEBUFFER_OPERATIONS;
}
+
static long objectFieldOffset(Field field) {
return MEMORY_ACCESSOR.objectFieldOffset(field);
}
@@ -146,10 +146,6 @@ final class UnsafeUtil {
return MEMORY_ACCESSOR.getObject(target, offset);
}
- static void putObject(Object target, long offset, Object value) {
- MEMORY_ACCESSOR.putObject(target, offset, value);
- }
-
static byte getByte(byte[] target, long index) {
return MEMORY_ACCESSOR.getByte(target, BYTE_ARRAY_BASE_OFFSET + index);
}
@@ -266,7 +262,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 =
@@ -346,6 +342,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);
@@ -364,18 +364,16 @@ final class UnsafeUtil {
}
- @SuppressWarnings("unchecked")
- private static <T> Class<T> getClassForName(String name) {
- try {
- return (Class<T>) Class.forName(name);
- } catch (Throwable e) {
- return null;
- }
- }
-
/** Finds the address field within a direct {@link Buffer}. */
private static Field bufferAddressField() {
- return field(Buffer.class, "address");
+ 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() {
+ Field field = field(String.class, "value");
+ return field != null && field.getType() == char[].class ? field : null;
}
/**
diff --git a/java/core/src/main/java/com/google/protobuf/Utf8.java b/java/core/src/main/java/com/google/protobuf/Utf8.java
index 1b136144..de75fe6b 100644
--- a/java/core/src/main/java/com/google/protobuf/Utf8.java
+++ b/java/core/src/main/java/com/google/protobuf/Utf8.java
@@ -34,11 +34,15 @@ import static com.google.protobuf.UnsafeUtil.addressOffset;
import static com.google.protobuf.UnsafeUtil.hasUnsafeArrayOperations;
import static com.google.protobuf.UnsafeUtil.hasUnsafeByteBufferOperations;
import static java.lang.Character.MAX_SURROGATE;
+import static java.lang.Character.MIN_HIGH_SURROGATE;
+import static java.lang.Character.MIN_LOW_SURROGATE;
+import static java.lang.Character.MIN_SUPPLEMENTARY_CODE_POINT;
import static java.lang.Character.MIN_SURROGATE;
import static java.lang.Character.isSurrogatePair;
import static java.lang.Character.toCodePoint;
import java.nio.ByteBuffer;
+import java.util.Arrays;
/**
* A set of low-level, high-performance static utility methods related
@@ -289,7 +293,7 @@ final class Utf8 {
if (Character.MIN_SURROGATE <= c && c <= Character.MAX_SURROGATE) {
// Check that we have a well-formed surrogate pair.
int cp = Character.codePointAt(sequence, i);
- if (cp < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
+ if (cp < MIN_SUPPLEMENTARY_CODE_POINT) {
throw new UnpairedSurrogateException(i, utf16Length);
}
i++;
@@ -331,6 +335,26 @@ final class Utf8 {
}
/**
+ * Decodes the given UTF-8 portion of the {@link ByteBuffer} into a {@link String}.
+ *
+ * @throws InvalidProtocolBufferException if the input is not valid UTF-8.
+ */
+ static String decodeUtf8(ByteBuffer buffer, int index, int size)
+ throws InvalidProtocolBufferException {
+ return processor.decodeUtf8(buffer, index, size);
+ }
+
+ /**
+ * Decodes the given UTF-8 encoded byte array slice into a {@link String}.
+ *
+ * @throws InvalidProtocolBufferException if the input is not valid UTF-8.
+ */
+ static String decodeUtf8(byte[] bytes, int index, int size)
+ throws InvalidProtocolBufferException {
+ return processor.decodeUtf8(bytes, index, size);
+ }
+
+ /**
* Encodes the given characters to the target {@link ByteBuffer} using UTF-8 encoding.
*
* <p>Selects an optimal algorithm based on the type of {@link ByteBuffer} (i.e. heap or direct)
@@ -610,6 +634,116 @@ final class Utf8 {
}
/**
+ * Decodes the given byte array slice into a {@link String}.
+ *
+ * @throws InvalidProtocolBufferException if the byte array slice is not valid UTF-8.
+ */
+ abstract String decodeUtf8(byte[] bytes, int index, int size)
+ throws InvalidProtocolBufferException;
+
+ /**
+ * Decodes the given portion of the {@link ByteBuffer} into a {@link String}.
+ *
+ * @throws InvalidProtocolBufferException if the portion of the buffer is not valid UTF-8.
+ */
+ final String decodeUtf8(ByteBuffer buffer, int index, int size)
+ throws InvalidProtocolBufferException {
+ if (buffer.hasArray()) {
+ final int offset = buffer.arrayOffset();
+ return decodeUtf8(buffer.array(), offset + index, size);
+ } else if (buffer.isDirect()) {
+ return decodeUtf8Direct(buffer, index, size);
+ }
+ return decodeUtf8Default(buffer, index, size);
+ }
+
+ /**
+ * Decodes direct {@link ByteBuffer} instances into {@link String}.
+ */
+ abstract String decodeUtf8Direct(ByteBuffer buffer, int index, int size)
+ throws InvalidProtocolBufferException;
+
+ /**
+ * Decodes {@link ByteBuffer} instances using the {@link ByteBuffer} API rather than
+ * potentially faster approaches.
+ */
+ final String decodeUtf8Default(ByteBuffer buffer, int index, int size)
+ throws InvalidProtocolBufferException {
+ // Bitwise OR combines the sign bits so any negative value fails the check.
+ if ((index | size | buffer.limit() - index - size) < 0) {
+ throw new ArrayIndexOutOfBoundsException(
+ String.format("buffer limit=%d, index=%d, limit=%d", buffer.limit(), index, size));
+ }
+
+ int offset = index;
+ final int limit = offset + size;
+
+ // The longest possible resulting String is the same as the number of input bytes, when it is
+ // all ASCII. For other cases, this over-allocates and we will truncate in the end.
+ char[] resultArr = new char[size];
+ int resultPos = 0;
+
+ // Optimize for 100% ASCII (Hotspot loves small simple top-level loops like this).
+ // This simple loop stops when we encounter a byte >= 0x80 (i.e. non-ASCII).
+ while (offset < limit) {
+ byte b = buffer.get(offset);
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ offset++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+
+ while (offset < limit) {
+ byte byte1 = buffer.get(offset++);
+ if (DecodeUtil.isOneByte(byte1)) {
+ DecodeUtil.handleOneByte(byte1, resultArr, resultPos++);
+ // It's common for there to be multiple ASCII characters in a run mixed in, so add an
+ // extra optimized loop to take care of these runs.
+ while (offset < limit) {
+ byte b = buffer.get(offset);
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ offset++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+ } else if (DecodeUtil.isTwoBytes(byte1)) {
+ if (offset >= limit) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleTwoBytes(
+ byte1, /* byte2 */ buffer.get(offset++), resultArr, resultPos++);
+ } else if (DecodeUtil.isThreeBytes(byte1)) {
+ if (offset >= limit - 1) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleThreeBytes(
+ byte1,
+ /* byte2 */ buffer.get(offset++),
+ /* byte3 */ buffer.get(offset++),
+ resultArr,
+ resultPos++);
+ } else {
+ if (offset >= limit - 2) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleFourBytes(
+ byte1,
+ /* byte2 */ buffer.get(offset++),
+ /* byte3 */ buffer.get(offset++),
+ /* byte4 */ buffer.get(offset++),
+ resultArr,
+ resultPos++);
+ // 4-byte case requires two chars.
+ resultPos++;
+ }
+ }
+
+ return new String(resultArr, 0, resultPos);
+ }
+
+ /**
* Encodes an input character sequence ({@code in}) to UTF-8 in the target array ({@code out}).
* For a string, this method is similar to
* <pre>{@code
@@ -851,6 +985,88 @@ final class Utf8 {
}
@Override
+ String decodeUtf8(byte[] bytes, int index, int size) throws InvalidProtocolBufferException {
+ // Bitwise OR combines the sign bits so any negative value fails the check.
+ if ((index | size | bytes.length - index - size) < 0) {
+ throw new ArrayIndexOutOfBoundsException(
+ String.format("buffer length=%d, index=%d, size=%d", bytes.length, index, size));
+ }
+
+ int offset = index;
+ final int limit = offset + size;
+
+ // The longest possible resulting String is the same as the number of input bytes, when it is
+ // all ASCII. For other cases, this over-allocates and we will truncate in the end.
+ char[] resultArr = new char[size];
+ int resultPos = 0;
+
+ // Optimize for 100% ASCII (Hotspot loves small simple top-level loops like this).
+ // This simple loop stops when we encounter a byte >= 0x80 (i.e. non-ASCII).
+ while (offset < limit) {
+ byte b = bytes[offset];
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ offset++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+
+ while (offset < limit) {
+ byte byte1 = bytes[offset++];
+ if (DecodeUtil.isOneByte(byte1)) {
+ DecodeUtil.handleOneByte(byte1, resultArr, resultPos++);
+ // It's common for there to be multiple ASCII characters in a run mixed in, so add an
+ // extra optimized loop to take care of these runs.
+ while (offset < limit) {
+ byte b = bytes[offset];
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ offset++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+ } else if (DecodeUtil.isTwoBytes(byte1)) {
+ if (offset >= limit) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleTwoBytes(byte1, /* byte2 */ bytes[offset++], resultArr, resultPos++);
+ } else if (DecodeUtil.isThreeBytes(byte1)) {
+ if (offset >= limit - 1) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleThreeBytes(
+ byte1,
+ /* byte2 */ bytes[offset++],
+ /* byte3 */ bytes[offset++],
+ resultArr,
+ resultPos++);
+ } else {
+ if (offset >= limit - 2) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleFourBytes(
+ byte1,
+ /* byte2 */ bytes[offset++],
+ /* byte3 */ bytes[offset++],
+ /* byte4 */ bytes[offset++],
+ resultArr,
+ resultPos++);
+ // 4-byte case requires two chars.
+ resultPos++;
+ }
+ }
+
+ return new String(resultArr, 0, resultPos);
+ }
+
+ @Override
+ String decodeUtf8Direct(ByteBuffer buffer, int index, int size)
+ throws InvalidProtocolBufferException {
+ // For safe processing, we have to use the ByteBufferAPI.
+ return decodeUtf8Default(buffer, index, size);
+ }
+
+ @Override
int encodeUtf8(CharSequence in, byte[] out, int offset, int length) {
int utf16Length = in.length();
int j = offset;
@@ -996,6 +1212,7 @@ final class Utf8 {
@Override
int partialIsValidUtf8(int state, byte[] bytes, final int index, final int limit) {
+ // Bitwise OR combines the sign bits so any negative value fails the check.
if ((index | limit | bytes.length - limit) < 0) {
throw new ArrayIndexOutOfBoundsException(
String.format("Array length=%d, index=%d, limit=%d", bytes.length, index, limit));
@@ -1091,6 +1308,7 @@ final class Utf8 {
@Override
int partialIsValidUtf8Direct(
final int state, ByteBuffer buffer, final int index, final int limit) {
+ // Bitwise OR combines the sign bits so any negative value fails the check.
if ((index | limit | buffer.limit() - limit) < 0) {
throw new ArrayIndexOutOfBoundsException(
String.format("buffer limit=%d, index=%d, limit=%d", buffer.limit(), index, limit));
@@ -1185,6 +1403,157 @@ final class Utf8 {
}
@Override
+ String decodeUtf8(byte[] bytes, int index, int size) throws InvalidProtocolBufferException {
+ if ((index | size | bytes.length - index - size) < 0) {
+ throw new ArrayIndexOutOfBoundsException(
+ String.format("buffer length=%d, index=%d, size=%d", bytes.length, index, size));
+ }
+
+ int offset = index;
+ final int limit = offset + size;
+
+ // The longest possible resulting String is the same as the number of input bytes, when it is
+ // all ASCII. For other cases, this over-allocates and we will truncate in the end.
+ char[] resultArr = new char[size];
+ int resultPos = 0;
+
+ // Optimize for 100% ASCII (Hotspot loves small simple top-level loops like this).
+ // This simple loop stops when we encounter a byte >= 0x80 (i.e. non-ASCII).
+ while (offset < limit) {
+ byte b = UnsafeUtil.getByte(bytes, offset);
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ offset++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+
+ while (offset < limit) {
+ byte byte1 = UnsafeUtil.getByte(bytes, offset++);
+ if (DecodeUtil.isOneByte(byte1)) {
+ DecodeUtil.handleOneByte(byte1, resultArr, resultPos++);
+ // It's common for there to be multiple ASCII characters in a run mixed in, so add an
+ // extra optimized loop to take care of these runs.
+ while (offset < limit) {
+ byte b = UnsafeUtil.getByte(bytes, offset);
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ offset++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+ } else if (DecodeUtil.isTwoBytes(byte1)) {
+ if (offset >= limit) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleTwoBytes(
+ byte1, /* byte2 */ UnsafeUtil.getByte(bytes, offset++), resultArr, resultPos++);
+ } else if (DecodeUtil.isThreeBytes(byte1)) {
+ if (offset >= limit - 1) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleThreeBytes(
+ byte1,
+ /* byte2 */ UnsafeUtil.getByte(bytes, offset++),
+ /* byte3 */ UnsafeUtil.getByte(bytes, offset++),
+ resultArr,
+ resultPos++);
+ } else {
+ if (offset >= limit - 2) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleFourBytes(
+ byte1,
+ /* byte2 */ UnsafeUtil.getByte(bytes, offset++),
+ /* byte3 */ UnsafeUtil.getByte(bytes, offset++),
+ /* byte4 */ UnsafeUtil.getByte(bytes, offset++),
+ resultArr,
+ resultPos++);
+ // 4-byte case requires two chars.
+ resultPos++;
+ }
+ }
+
+ return new String(resultArr, 0, resultPos);
+ }
+
+ @Override
+ String decodeUtf8Direct(ByteBuffer buffer, int index, int size)
+ throws InvalidProtocolBufferException {
+ // Bitwise OR combines the sign bits so any negative value fails the check.
+ if ((index | size | buffer.limit() - index - size) < 0) {
+ throw new ArrayIndexOutOfBoundsException(
+ String.format("buffer limit=%d, index=%d, limit=%d", buffer.limit(), index, size));
+ }
+ long address = UnsafeUtil.addressOffset(buffer) + index;
+ final long addressLimit = address + size;
+
+ // The longest possible resulting String is the same as the number of input bytes, when it is
+ // all ASCII. For other cases, this over-allocates and we will truncate in the end.
+ char[] resultArr = new char[size];
+ int resultPos = 0;
+
+ // Optimize for 100% ASCII (Hotspot loves small simple top-level loops like this).
+ // This simple loop stops when we encounter a byte >= 0x80 (i.e. non-ASCII).
+ while (address < addressLimit) {
+ byte b = UnsafeUtil.getByte(address);
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ address++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+
+ while (address < addressLimit) {
+ byte byte1 = UnsafeUtil.getByte(address++);
+ if (DecodeUtil.isOneByte(byte1)) {
+ DecodeUtil.handleOneByte(byte1, resultArr, resultPos++);
+ // It's common for there to be multiple ASCII characters in a run mixed in, so add an
+ // extra optimized loop to take care of these runs.
+ while (address < addressLimit) {
+ byte b = UnsafeUtil.getByte(address);
+ if (!DecodeUtil.isOneByte(b)) {
+ break;
+ }
+ address++;
+ DecodeUtil.handleOneByte(b, resultArr, resultPos++);
+ }
+ } else if (DecodeUtil.isTwoBytes(byte1)) {
+ if (address >= addressLimit) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleTwoBytes(
+ byte1, /* byte2 */ UnsafeUtil.getByte(address++), resultArr, resultPos++);
+ } else if (DecodeUtil.isThreeBytes(byte1)) {
+ if (address >= addressLimit - 1) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleThreeBytes(
+ byte1,
+ /* byte2 */ UnsafeUtil.getByte(address++),
+ /* byte3 */ UnsafeUtil.getByte(address++),
+ resultArr,
+ resultPos++);
+ } else {
+ if (address >= addressLimit - 2) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ DecodeUtil.handleFourBytes(
+ byte1,
+ /* byte2 */ UnsafeUtil.getByte(address++),
+ /* byte3 */ UnsafeUtil.getByte(address++),
+ /* byte4 */ UnsafeUtil.getByte(address++),
+ resultArr,
+ resultPos++);
+ // 4-byte case requires two chars.
+ resultPos++;
+ }
+ }
+
+ return new String(resultArr, 0, resultPos);
+ }
+
+ @Override
int encodeUtf8(final CharSequence in, final byte[] out, final int offset, final int length) {
long outIx = offset;
final long outLimit = outIx + length;
@@ -1554,5 +1923,112 @@ final class Utf8 {
}
}
+ /**
+ * Utility methods for decoding bytes into {@link String}. Callers are responsible for extracting
+ * bytes (possibly using Unsafe methods), and checking remaining bytes. All other UTF-8 validity
+ * checks and codepoint conversion happen in this class.
+ */
+ private static class DecodeUtil {
+
+ /**
+ * Returns whether this is a single-byte codepoint (i.e., ASCII) with the form '0XXXXXXX'.
+ */
+ private static boolean isOneByte(byte b) {
+ return b >= 0;
+ }
+
+ /**
+ * Returns whether this is a two-byte codepoint with the form '10XXXXXX'.
+ */
+ private static boolean isTwoBytes(byte b) {
+ return b < (byte) 0xE0;
+ }
+
+ /**
+ * Returns whether this is a three-byte codepoint with the form '110XXXXX'.
+ */
+ private static boolean isThreeBytes(byte b) {
+ return b < (byte) 0xF0;
+ }
+
+ private static void handleOneByte(byte byte1, char[] resultArr, int resultPos) {
+ resultArr[resultPos] = (char) byte1;
+ }
+
+ private static void handleTwoBytes(
+ byte byte1, byte byte2, char[] resultArr, int resultPos)
+ throws InvalidProtocolBufferException {
+ // Simultaneously checks for illegal trailing-byte in leading position (<= '11000000') and
+ // overlong 2-byte, '11000001'.
+ if (byte1 < (byte) 0xC2
+ || isNotTrailingByte(byte2)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ resultArr[resultPos] = (char) (((byte1 & 0x1F) << 6) | trailingByteValue(byte2));
+ }
+
+ private static void handleThreeBytes(
+ byte byte1, byte byte2, byte byte3, char[] resultArr, int resultPos)
+ throws InvalidProtocolBufferException {
+ if (isNotTrailingByte(byte2)
+ // overlong? 5 most significant bits must not all be zero
+ || (byte1 == (byte) 0xE0 && byte2 < (byte) 0xA0)
+ // check for illegal surrogate codepoints
+ || (byte1 == (byte) 0xED && byte2 >= (byte) 0xA0)
+ || isNotTrailingByte(byte3)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ resultArr[resultPos] = (char)
+ (((byte1 & 0x0F) << 12) | (trailingByteValue(byte2) << 6) | trailingByteValue(byte3));
+ }
+
+ private static void handleFourBytes(
+ byte byte1, byte byte2, byte byte3, byte byte4, char[] resultArr, int resultPos)
+ throws InvalidProtocolBufferException{
+ if (isNotTrailingByte(byte2)
+ // Check that 1 <= plane <= 16. Tricky optimized form of:
+ // valid 4-byte leading byte?
+ // if (byte1 > (byte) 0xF4 ||
+ // overlong? 4 most significant bits must not all be zero
+ // byte1 == (byte) 0xF0 && byte2 < (byte) 0x90 ||
+ // codepoint larger than the highest code point (U+10FFFF)?
+ // byte1 == (byte) 0xF4 && byte2 > (byte) 0x8F)
+ || (((byte1 << 28) + (byte2 - (byte) 0x90)) >> 30) != 0
+ || isNotTrailingByte(byte3)
+ || isNotTrailingByte(byte4)) {
+ throw InvalidProtocolBufferException.invalidUtf8();
+ }
+ int codepoint = ((byte1 & 0x07) << 18)
+ | (trailingByteValue(byte2) << 12)
+ | (trailingByteValue(byte3) << 6)
+ | trailingByteValue(byte4);
+ resultArr[resultPos] = DecodeUtil.highSurrogate(codepoint);
+ resultArr[resultPos + 1] = DecodeUtil.lowSurrogate(codepoint);
+ }
+
+ /**
+ * Returns whether the byte is not a valid continuation of the form '10XXXXXX'.
+ */
+ private static boolean isNotTrailingByte(byte b) {
+ return b > (byte) 0xBF;
+ }
+
+ /**
+ * Returns the actual value of the trailing byte (removes the prefix '10') for composition.
+ */
+ private static int trailingByteValue(byte b) {
+ return b & 0x3F;
+ }
+
+ private static char highSurrogate(int codePoint) {
+ return (char) ((MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10))
+ + (codePoint >>> 10));
+ }
+
+ private static char lowSurrogate(int codePoint) {
+ return (char) (MIN_LOW_SURROGATE + (codePoint & 0x3ff));
+ }
+ }
+
private Utf8() {}
}
diff --git a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java
index 622e36a4..cb2d34eb 100644
--- a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java
+++ b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java
@@ -30,6 +30,9 @@
package com.google.protobuf;
+import static com.google.protobuf.TestUtil.TEST_REQUIRED_INITIALIZED;
+import static com.google.protobuf.TestUtil.TEST_REQUIRED_UNINITIALIZED;
+
import com.google.protobuf.Descriptors.FieldDescriptor;
import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize;
import protobuf_unittest.UnittestProto;
@@ -346,11 +349,6 @@ public class AbstractMessageTest extends TestCase {
// -----------------------------------------------------------------
// Tests for isInitialized().
- private static final TestRequired TEST_REQUIRED_UNINITIALIZED =
- TestRequired.getDefaultInstance();
- private static final TestRequired TEST_REQUIRED_INITIALIZED =
- TestRequired.newBuilder().setA(1).setB(2).setC(3).build();
-
public void testIsInitialized() throws Exception {
TestRequired.Builder builder = TestRequired.newBuilder();
AbstractMessageWrapper.Builder abstractBuilder =
@@ -380,7 +378,7 @@ public class AbstractMessageTest extends TestCase {
builder.setOptionalMessage(TEST_REQUIRED_UNINITIALIZED);
assertFalse(abstractBuilder.isInitialized());
assertEquals(
- "optional_message.a, optional_message.b, optional_message.c",
+ "optional_message.b, optional_message.c",
abstractBuilder.getInitializationErrorString());
builder.setOptionalMessage(TEST_REQUIRED_INITIALIZED);
@@ -390,7 +388,7 @@ public class AbstractMessageTest extends TestCase {
builder.addRepeatedMessage(TEST_REQUIRED_UNINITIALIZED);
assertFalse(abstractBuilder.isInitialized());
assertEquals(
- "repeated_message[0].a, repeated_message[0].b, repeated_message[0].c",
+ "repeated_message[0].b, repeated_message[0].c",
abstractBuilder.getInitializationErrorString());
builder.setRepeatedMessage(0, TEST_REQUIRED_INITIALIZED);
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/DecodeUtf8Test.java b/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java
new file mode 100644
index 00000000..359d4d74
--- /dev/null
+++ b/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java
@@ -0,0 +1,325 @@
+package com.google.protobuf;
+
+import com.google.protobuf.Utf8.Processor;
+import com.google.protobuf.Utf8.SafeProcessor;
+import com.google.protobuf.Utf8.UnsafeProcessor;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+import junit.framework.TestCase;
+
+public class DecodeUtf8Test extends TestCase {
+ private static Logger logger = Logger.getLogger(DecodeUtf8Test.class.getName());
+
+ private static final Processor SAFE_PROCESSOR = new SafeProcessor();
+ private static final Processor UNSAFE_PROCESSOR = new UnsafeProcessor();
+
+ public void testRoundTripAllValidChars() throws Exception {
+ for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) {
+ if (i < Character.MIN_SURROGATE || i > Character.MAX_SURROGATE) {
+ String str = new String(Character.toChars(i));
+ assertRoundTrips(str);
+ }
+ }
+ }
+
+ // Test all 1, 2, 3 invalid byte combinations. Valid ones would have been covered above.
+
+ public void testOneByte() throws Exception {
+ int valid = 0;
+ for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ ByteString bs = ByteString.copyFrom(new byte[] { (byte) i });
+ if (!bs.isValidUtf8()) {
+ assertInvalid(bs.toByteArray());
+ } else {
+ valid++;
+ }
+ }
+ assertEquals(IsValidUtf8TestUtil.EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, valid);
+ }
+
+ public void testTwoBytes() throws Exception {
+ int valid = 0;
+ for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
+ ByteString bs = ByteString.copyFrom(new byte[]{(byte) i, (byte) j});
+ if (!bs.isValidUtf8()) {
+ assertInvalid(bs.toByteArray());
+ } else {
+ valid++;
+ }
+ }
+ }
+ assertEquals(IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT, valid);
+ }
+
+ public void testThreeBytes() throws Exception {
+ // Travis' OOM killer doesn't like this test
+ if (System.getenv("TRAVIS") == null) {
+ int count = 0;
+ int valid = 0;
+ for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
+ for (int k = Byte.MIN_VALUE; k <= Byte.MAX_VALUE; k++) {
+ byte[] bytes = new byte[]{(byte) i, (byte) j, (byte) k};
+ ByteString bs = ByteString.copyFrom(bytes);
+ if (!bs.isValidUtf8()) {
+ assertInvalid(bytes);
+ } else {
+ valid++;
+ }
+ count++;
+ if (count % 1000000L == 0) {
+ logger.info("Processed " + (count / 1000000L) + " million characters");
+ }
+ }
+ }
+ }
+ assertEquals(IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT, valid);
+ }
+ }
+
+ /**
+ * Tests that round tripping of a sample of four byte permutations work.
+ */
+ public void testInvalid_4BytesSamples() throws Exception {
+ // Bad trailing bytes
+ assertInvalid(0xF0, 0xA4, 0xAD, 0x7F);
+ assertInvalid(0xF0, 0xA4, 0xAD, 0xC0);
+
+ // Special cases for byte2
+ assertInvalid(0xF0, 0x8F, 0xAD, 0xA2);
+ assertInvalid(0xF4, 0x90, 0xAD, 0xA2);
+ }
+
+ public void testRealStrings() throws Exception {
+ // English
+ assertRoundTrips("The quick brown fox jumps over the lazy dog");
+ // German
+ assertRoundTrips("Quizdeltagerne spiste jordb\u00e6r med fl\u00f8de, mens cirkusklovnen");
+ // Japanese
+ assertRoundTrips(
+ "\u3044\u308d\u306f\u306b\u307b\u3078\u3068\u3061\u308a\u306c\u308b\u3092");
+ // Hebrew
+ assertRoundTrips(
+ "\u05d3\u05d2 \u05e1\u05e7\u05e8\u05df \u05e9\u05d8 \u05d1\u05d9\u05dd "
+ + "\u05de\u05d0\u05d5\u05db\u05d6\u05d1 \u05d5\u05dc\u05e4\u05ea\u05e2"
+ + " \u05de\u05e6\u05d0 \u05dc\u05d5 \u05d7\u05d1\u05e8\u05d4 "
+ + "\u05d0\u05d9\u05da \u05d4\u05e7\u05dc\u05d9\u05d8\u05d4");
+ // Thai
+ assertRoundTrips(
+ " \u0e08\u0e07\u0e1d\u0e48\u0e32\u0e1f\u0e31\u0e19\u0e1e\u0e31\u0e12"
+ + "\u0e19\u0e32\u0e27\u0e34\u0e0a\u0e32\u0e01\u0e32\u0e23");
+ // Chinese
+ assertRoundTrips(
+ "\u8fd4\u56de\u94fe\u4e2d\u7684\u4e0b\u4e00\u4e2a\u4ee3\u7406\u9879\u9009\u62e9\u5668");
+ // Chinese with 4-byte chars
+ assertRoundTrips("\uD841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78"
+ + "\uD843\uDC96\uD843\uDCCF\uD843\uDCD5\uD843\uDD15\uD843\uDD7C\uD843\uDD7F"
+ + "\uD843\uDE0E\uD843\uDE0F\uD843\uDE77\uD843\uDE9D\uD843\uDEA2");
+ // Mixed
+ assertRoundTrips(
+ "The quick brown \u3044\u308d\u306f\u306b\u307b\u3078\u8fd4\u56de\u94fe"
+ + "\u4e2d\u7684\u4e0b\u4e00");
+ }
+
+ public void testOverlong() throws Exception {
+ assertInvalid(0xc0, 0xaf);
+ assertInvalid(0xe0, 0x80, 0xaf);
+ assertInvalid(0xf0, 0x80, 0x80, 0xaf);
+
+ // Max overlong
+ assertInvalid(0xc1, 0xbf);
+ assertInvalid(0xe0, 0x9f, 0xbf);
+ assertInvalid(0xf0 ,0x8f, 0xbf, 0xbf);
+
+ // null overlong
+ assertInvalid(0xc0, 0x80);
+ assertInvalid(0xe0, 0x80, 0x80);
+ assertInvalid(0xf0, 0x80, 0x80, 0x80);
+ }
+
+ public void testIllegalCodepoints() throws Exception {
+ // Single surrogate
+ assertInvalid(0xed, 0xa0, 0x80);
+ assertInvalid(0xed, 0xad, 0xbf);
+ assertInvalid(0xed, 0xae, 0x80);
+ assertInvalid(0xed, 0xaf, 0xbf);
+ assertInvalid(0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xbe, 0x80);
+ assertInvalid(0xed, 0xbf, 0xbf);
+
+ // Paired surrogates
+ assertInvalid(0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf);
+ assertInvalid(0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf);
+ assertInvalid(0xed, 0xae, 0x80, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf);
+ assertInvalid(0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf);
+ }
+
+ public void testBufferSlice() throws Exception {
+ String str = "The quick brown fox jumps over the lazy dog";
+ assertRoundTrips(str, 10, 4);
+ assertRoundTrips(str, str.length(), 0);
+ }
+
+ public void testInvalidBufferSlice() throws Exception {
+ byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(Internal.UTF_8);
+ assertInvalidSlice(bytes, bytes.length - 3, 4);
+ assertInvalidSlice(bytes, bytes.length, 1);
+ assertInvalidSlice(bytes, bytes.length + 1, 0);
+ assertInvalidSlice(bytes, 0, bytes.length + 1);
+ }
+
+ private void assertInvalid(int... bytesAsInt) throws Exception {
+ byte[] bytes = new byte[bytesAsInt.length];
+ for (int i = 0; i < bytesAsInt.length; i++) {
+ bytes[i] = (byte) bytesAsInt[i];
+ }
+ assertInvalid(bytes);
+ }
+
+ private void assertInvalid(byte[] bytes) throws Exception {
+ try {
+ UNSAFE_PROCESSOR.decodeUtf8(bytes, 0, bytes.length);
+ fail();
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+ try {
+ SAFE_PROCESSOR.decodeUtf8(bytes, 0, bytes.length);
+ fail();
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+
+ ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length);
+ direct.put(bytes);
+ direct.flip();
+ try {
+ UNSAFE_PROCESSOR.decodeUtf8(direct, 0, bytes.length);
+ fail();
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+ try {
+ SAFE_PROCESSOR.decodeUtf8(direct, 0, bytes.length);
+ fail();
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+
+ ByteBuffer heap = ByteBuffer.allocate(bytes.length);
+ heap.put(bytes);
+ heap.flip();
+ try {
+ UNSAFE_PROCESSOR.decodeUtf8(heap, 0, bytes.length);
+ fail();
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+ try {
+ SAFE_PROCESSOR.decodeUtf8(heap, 0, bytes.length);
+ fail();
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+ }
+
+ private void assertInvalidSlice(byte[] bytes, int index, int size) throws Exception {
+ try {
+ UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size);
+ fail();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Expected.
+ }
+ try {
+ SAFE_PROCESSOR.decodeUtf8(bytes, index, size);
+ fail();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Expected.
+ }
+
+ ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length);
+ direct.put(bytes);
+ direct.flip();
+ try {
+ UNSAFE_PROCESSOR.decodeUtf8(direct, index, size);
+ fail();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Expected.
+ }
+ try {
+ SAFE_PROCESSOR.decodeUtf8(direct, index, size);
+ fail();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Expected.
+ }
+
+ ByteBuffer heap = ByteBuffer.allocate(bytes.length);
+ heap.put(bytes);
+ heap.flip();
+ try {
+ UNSAFE_PROCESSOR.decodeUtf8(heap, index, size);
+ fail();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Expected.
+ }
+ try {
+ SAFE_PROCESSOR.decodeUtf8(heap, index, size);
+ fail();
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Expected.
+ }
+ }
+
+ private void assertRoundTrips(String str) throws Exception {
+ assertRoundTrips(str, 0, -1);
+ }
+
+ private void assertRoundTrips(String str, int index, int size) throws Exception {
+ byte[] bytes = str.getBytes(Internal.UTF_8);
+ if (size == -1) {
+ size = bytes.length;
+ }
+ assertDecode(new String(bytes, index, size, Internal.UTF_8),
+ UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size));
+ assertDecode(new String(bytes, index, size, Internal.UTF_8),
+ SAFE_PROCESSOR.decodeUtf8(bytes, index, size));
+
+ ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length);
+ direct.put(bytes);
+ direct.flip();
+ assertDecode(new String(bytes, index, size, Internal.UTF_8),
+ UNSAFE_PROCESSOR.decodeUtf8(direct, index, size));
+ assertDecode(new String(bytes, index, size, Internal.UTF_8),
+ SAFE_PROCESSOR.decodeUtf8(direct, index, size));
+
+ ByteBuffer heap = ByteBuffer.allocate(bytes.length);
+ heap.put(bytes);
+ heap.flip();
+ assertDecode(new String(bytes, index, size, Internal.UTF_8),
+ UNSAFE_PROCESSOR.decodeUtf8(heap, index, size));
+ assertDecode(new String(bytes, index, size, Internal.UTF_8),
+ SAFE_PROCESSOR.decodeUtf8(heap, index, size));
+ }
+
+ private void assertDecode(String expected, String actual) {
+ if (!expected.equals(actual)) {
+ fail("Failure: Expected (" + codepoints(expected) + ") Actual (" + codepoints(actual) + ")");
+ }
+ }
+
+ private List<String> codepoints(String str) {
+ List<String> codepoints = new ArrayList<String>();
+ for (int i = 0; i < str.length(); i++) {
+ codepoints.add(Long.toHexString(str.charAt(i)));
+ }
+ return codepoints;
+ }
+
+}
diff --git a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java
index 2c272a73..9c0997c4 100644
--- a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java
+++ b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java
@@ -37,22 +37,22 @@ import junit.framework.TestCase;
/**
* Test field deprecation
- *
+ *
* @author birdo@google.com (Roberto Scaramuzzi)
*/
public class DeprecatedFieldTest extends TestCase {
private String[] deprecatedGetterNames = {
"hasDeprecatedInt32",
"getDeprecatedInt32"};
-
+
private String[] deprecatedBuilderGetterNames = {
"hasDeprecatedInt32",
"getDeprecatedInt32",
"clearDeprecatedInt32"};
-
+
private String[] deprecatedBuilderSetterNames = {
- "setDeprecatedInt32"};
-
+ "setDeprecatedInt32"};
+
public void testDeprecatedField() throws Exception {
Class<?> deprecatedFields = TestDeprecatedFields.class;
Class<?> deprecatedFieldsBuilder = TestDeprecatedFields.Builder.class;
@@ -72,7 +72,15 @@ public class DeprecatedFieldTest extends TestCase {
isDeprecated(method));
}
}
-
+
+ public void testDeprecatedFieldInOneof() throws Exception {
+ Class<?> oneofCase = TestDeprecatedFields.OneofFieldsCase.class;
+ String name = "DEPRECATED_INT32_IN_ONEOF";
+ java.lang.reflect.Field enumValue = oneofCase.getField(name);
+ assertTrue("Enum value " + name + " should be deprecated.",
+ isDeprecated(enumValue));
+ }
+
private boolean isDeprecated(AnnotatedElement annotated) {
return annotated.isAnnotationPresent(Deprecated.class);
}
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/IsValidUtf8TestUtil.java b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java
index 16a808bf..1bcf63e7 100644
--- a/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java
+++ b/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java
@@ -273,6 +273,15 @@ final class IsValidUtf8TestUtil {
assertEquals(isRoundTrippable, Utf8.isValidUtf8(bytes));
assertEquals(isRoundTrippable, Utf8.isValidUtf8(bytes, 0, numBytes));
+ try {
+ assertEquals(s, Utf8.decodeUtf8(bytes, 0, numBytes));
+ } catch (InvalidProtocolBufferException e) {
+ if (isRoundTrippable) {
+ System.out.println("Could not decode utf-8");
+ outputFailure(byteChar, bytes, bytesReencoded);
+ }
+ }
+
// Test partial sequences.
// Partition numBytes into three segments (not necessarily non-empty).
int i = rnd.nextInt(numBytes);
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/TestUtil.java b/java/core/src/test/java/com/google/protobuf/TestUtil.java
index e96adf07..b4bc3a3d 100644
--- a/java/core/src/test/java/com/google/protobuf/TestUtil.java
+++ b/java/core/src/test/java/com/google/protobuf/TestUtil.java
@@ -231,6 +231,7 @@ import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder;
import protobuf_unittest.UnittestProto.TestOneof2;
import protobuf_unittest.UnittestProto.TestPackedExtensions;
import protobuf_unittest.UnittestProto.TestPackedTypes;
+import protobuf_unittest.UnittestProto.TestRequired;
import protobuf_unittest.UnittestProto.TestUnpackedTypes;
import java.io.File;
import java.io.IOException;
@@ -252,6 +253,11 @@ import junit.framework.Assert;
public final class TestUtil {
private TestUtil() {}
+ public static final TestRequired TEST_REQUIRED_UNINITIALIZED =
+ TestRequired.newBuilder().setA(1).buildPartial();
+ public static final TestRequired TEST_REQUIRED_INITIALIZED =
+ TestRequired.newBuilder().setA(1).setB(2).setC(3).build();
+
/** Helper to convert a String to ByteString. */
static ByteString toBytes(String str) {
return ByteString.copyFrom(str.getBytes(Internal.UTF_8));
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 28c4fdea..720061d2 100644
--- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java
+++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java
@@ -30,6 +30,9 @@
package com.google.protobuf;
+import static com.google.protobuf.TestUtil.TEST_REQUIRED_INITIALIZED;
+import static com.google.protobuf.TestUtil.TEST_REQUIRED_UNINITIALIZED;
+
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.TextFormat.Parser.SingularOverwritePolicy;
@@ -42,6 +45,7 @@ import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage;
import protobuf_unittest.UnittestProto.TestEmptyMessage;
import protobuf_unittest.UnittestProto.TestOneof2;
+import protobuf_unittest.UnittestProto.TestRequired;
import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet;
import java.io.StringReader;
import java.util.List;
@@ -57,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 \\\\";
@@ -326,19 +329,58 @@ public class TextFormatTest extends TestCase {
// =================================================================
- public void testParse() throws Exception {
+ public void testMerge() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge(allFieldsSetText, builder);
TestUtil.assertAllFieldsSet(builder.build());
}
- public void testParseReader() throws Exception {
+ public void testParse() throws Exception {
+ TestUtil.assertAllFieldsSet(
+ TextFormat.parse(allFieldsSetText, TestAllTypes.class));
+ }
+
+ public void testMergeInitialized() throws Exception {
+ TestRequired.Builder builder = TestRequired.newBuilder();
+ TextFormat.merge(TEST_REQUIRED_INITIALIZED.toString(), builder);
+ assertEquals(TEST_REQUIRED_INITIALIZED.toString(),
+ builder.buildPartial().toString());
+ assertTrue(builder.isInitialized());
+ }
+
+ public void testParseInitialized() throws Exception {
+ TestRequired parsed =
+ TextFormat.parse(TEST_REQUIRED_INITIALIZED.toString(),
+ TestRequired.class);
+ assertEquals(TEST_REQUIRED_INITIALIZED.toString(), parsed.toString());
+ assertTrue(parsed.isInitialized());
+ }
+
+ public void testMergeUninitialized() throws Exception {
+ TestRequired.Builder builder = TestRequired.newBuilder();
+ TextFormat.merge(TEST_REQUIRED_UNINITIALIZED.toString(), builder);
+ assertEquals(TEST_REQUIRED_UNINITIALIZED.toString(),
+ builder.buildPartial().toString());
+ assertFalse(builder.isInitialized());
+ }
+
+ public void testParseUninitialized() throws Exception {
+ try {
+ TextFormat.parse(TEST_REQUIRED_UNINITIALIZED.toString(),
+ TestRequired.class);
+ fail("Expected UninitializedMessageException.");
+ } catch (UninitializedMessageException e) {
+ assertEquals("Message missing required fields: b, c", e.getMessage());
+ }
+ }
+
+ public void testMergeReader() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge(new StringReader(allFieldsSetText), builder);
TestUtil.assertAllFieldsSet(builder.build());
}
- public void testParseExtensions() throws Exception {
+ public void testMergeExtensions() throws Exception {
TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
TextFormat.merge(allExtensionsSetText,
TestUtil.getExtensionRegistry(),
@@ -346,7 +388,14 @@ public class TextFormatTest extends TestCase {
TestUtil.assertAllExtensionsSet(builder.build());
}
- public void testParseCompatibility() throws Exception {
+ public void testParseExtensions() throws Exception {
+ TestUtil.assertAllExtensionsSet(
+ TextFormat.parse(allExtensionsSetText,
+ TestUtil.getExtensionRegistry(),
+ TestAllExtensions.class));
+ }
+
+ public void testMergeAndParseCompatibility() throws Exception {
String original = "repeated_float: inf\n" +
"repeated_float: -inf\n" +
"repeated_float: nan\n" +
@@ -371,21 +420,29 @@ public class TextFormatTest extends TestCase {
"repeated_double: Infinity\n" +
"repeated_double: -Infinity\n" +
"repeated_double: NaN\n";
+
+ // Test merge().
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge(original, builder);
assertEquals(canonical, builder.build().toString());
+
+ // Test parse().
+ assertEquals(canonical,
+ TextFormat.parse(original, TestAllTypes.class).toString());
}
- public void testParseExotic() throws Exception {
+ public void testMergeAndParseExotic() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge(exoticText, builder);
// Too lazy to check things individually. Don't try to debug this
// if testPrintExotic() is failing.
assertEquals(canonicalExoticText, builder.build().toString());
+ assertEquals(canonicalExoticText,
+ TextFormat.parse(exoticText, TestAllTypes.class).toString());
}
- public void testParseMessageSet() throws Exception {
+ public void testMergeMessageSet() throws Exception {
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
extensionRegistry.add(TestMessageSetExtension1.messageSetExtension);
extensionRegistry.add(TestMessageSetExtension2.messageSetExtension);
@@ -411,7 +468,7 @@ public class TextFormatTest extends TestCase {
TestMessageSetExtension1.messageSetExtension).getI());
}
- public void testParseMessageSetWithOverwriteForbidden() throws Exception {
+ public void testMergeMessageSetWithOverwriteForbidden() throws Exception {
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
extensionRegistry.add(TestMessageSetExtension1.messageSetExtension);
extensionRegistry.add(TestMessageSetExtension2.messageSetExtension);
@@ -438,20 +495,20 @@ public class TextFormatTest extends TestCase {
}
}
- public void testParseNumericEnum() throws Exception {
+ public void testMergeNumericEnum() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge("optional_nested_enum: 2", builder);
assertEquals(TestAllTypes.NestedEnum.BAR, builder.getOptionalNestedEnum());
}
- public void testParseAngleBrackets() throws Exception {
+ public void testMergeAngleBrackets() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge("OptionalGroup: < a: 1 >", builder);
assertTrue(builder.hasOptionalGroup());
assertEquals(1, builder.getOptionalGroup().getA());
}
- public void testParseComment() throws Exception {
+ public void testMergeComment() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge(
"# this is a comment\n" +
@@ -463,6 +520,7 @@ public class TextFormatTest extends TestCase {
}
private void assertParseError(String error, String text) {
+ // Test merge().
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
try {
TextFormat.merge(text, TestUtil.getExtensionRegistry(), builder);
@@ -470,6 +528,15 @@ public class TextFormatTest extends TestCase {
} catch (TextFormat.ParseException e) {
assertEquals(error, e.getMessage());
}
+
+ // Test parse().
+ try {
+ TextFormat.parse(
+ text, TestUtil.getExtensionRegistry(), TestAllTypes.class);
+ fail("Expected parse exception.");
+ } catch (TextFormat.ParseException e) {
+ assertEquals(error, e.getMessage());
+ }
}
@@ -508,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.",
@@ -575,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.
diff --git a/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto b/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto
new file mode 100644
index 00000000..c04f5d57
--- /dev/null
+++ b/java/core/src/test/proto/com/google/protobuf/map_lite_test.proto
@@ -0,0 +1,111 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package map_lite_test;
+
+option optimize_for = LITE_RUNTIME;
+option java_package = "map_lite_test";
+option java_outer_classname = "MapTestProto";
+
+message TestMap {
+ message MessageValue {
+ int32 value = 1;
+ }
+ enum EnumValue {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ QUX = 3;
+ }
+
+ map<int32, int32> int32_to_int32_field = 1;
+ map<int32, string> int32_to_string_field = 2;
+ map<int32, bytes> int32_to_bytes_field = 3;
+ map<int32, EnumValue> int32_to_enum_field = 4;
+ map<int32, MessageValue> int32_to_message_field = 5;
+ map<string, int32> string_to_int32_field = 6;
+ map<uint32, int32> uint32_to_int32_field = 7;
+ map<int64, int32> int64_to_int32_field = 8;
+}
+
+// Used to test that a nested builder containing map fields will properly
+// propagate the onChange event and mark its parent dirty when a change
+// is made to a map field.
+message TestOnChangeEventPropagation {
+ TestMap optional_message = 1;
+}
+
+// a decoy of TestMap for testing parsing errors
+message BizarroTestMap {
+ map<int32, bytes> int32_to_int32_field = 1; // same key type, different value
+ map<string, int32> int32_to_string_field = 2; // different key and value types
+ map<string, int32> int32_to_bytes_field = 3; // different key types, same value
+ map<string, bytes> int32_to_enum_field = 4; // different key and value types
+ map<string, bytes> int32_to_message_field = 5; // different key and value types
+ map<string, bytes> string_to_int32_field = 6; // same key type, different value
+}
+
+// Used to test that java reserved words can be used as protobuf field names
+// Not all reserved words are tested (to avoid bloat) but instead an arbitrary
+// subset of them chosen to cover various keyword categories like
+// type, modifier, declaration, etc.
+message ReservedAsMapField {
+ map<string, uint32> if = 1;
+ map<string, uint32> const = 2;
+ map<string, uint32> private = 3;
+ map<string, uint32> class = 4;
+ map<string, uint32> int = 5;
+ map<string, uint32> void = 6;
+ map<string, uint32> string = 7; // These are also proto keywords
+ map<string, uint32> package = 8;
+ map<string, uint32> enum = 9; // Most recent Java reserved word
+ map<string, uint32> null = 10;
+ // null is not a 'reserved word' per se but as a literal needs similar care
+}
+
+message ReservedAsMapFieldWithEnumValue {
+ enum SampleEnum {
+ A = 0;
+ B = 1;
+ }
+ map<string, SampleEnum> if = 1;
+ map<string, SampleEnum> const = 2;
+ map<string, SampleEnum> private = 3;
+ map<string, SampleEnum> class = 4;
+ map<string, SampleEnum> int = 5;
+ map<string, SampleEnum> void = 6;
+ map<string, SampleEnum> string = 7; // These are also proto keywords
+ map<string, SampleEnum> package = 8;
+ map<string, SampleEnum> enum = 9; // Most recent Java reserved word
+ map<string, SampleEnum> null = 10;
+ // null is not a 'reserved word' per se but as a literal needs similar care
+}
diff --git a/java/lite.md b/java/lite.md
new file mode 100644
index 00000000..84a45ec5
--- /dev/null
+++ b/java/lite.md
@@ -0,0 +1,50 @@
+# Protocol Buffers - Google's data interchange format
+
+Copyright 2008 Google Inc.
+
+https://developers.google.com/protocol-buffers/
+
+## Use Protobuf Java Lite Runtime
+
+Protobuf Java Lite runtime is separated from the main Java runtime because
+it's designed/implemented with different constraints. In particular, Java
+Lite runtime has a much smaller code size which makes it more suitable to
+be used on Android.
+
+To use Java Lite runtime, you need to install protoc and the protoc plugin for
+Java Lite runtime. You can obtain protoc following the instructions in the
+toplevel [README.md](../README.md) file. For the protoc plugin, you can
+download it from maven:
+
+ https://repo1.maven.org/maven2/com/google/protobuf/protoc-gen-javalite/
+
+Choose the version that works on your platform (e.g., on windows you can
+download `protoc-gen-javalite-3.0.0-windows-x86_32.exe`), rename it to
+protoc-gen-javalite (or protoc-gen-javalite.exe on windows) and place it
+in a directory where it can be find in PATH.
+
+Once you have the protoc and protoc plugin, you can generate Java Lite code
+for your .proto files:
+
+ $ protoc --javalite_out=${OUTPUT_DIR} path/to/your/proto/file
+
+Include the generated Java files in your project and add a dependency on the
+protobuf Java runtime. If you are using Maven, use the following:
+
+```xml
+<dependency>
+ <groupId>com.google.protobuf</groupId>
+ <artifactId>protobuf-lite</artifactId>
+ <version>3.0.1</version>
+</dependency>
+```
+
+Make sure the version number of the runtime matches (or is newer than) the
+version number of the protoc plugin. The version number of the protoc doesn't
+matter and any version >= 3.0.0 should work.
+
+### Use Protobuf Java Lite Runtime with Bazel
+
+Bazel has native build rules to work with protobuf. For Java Lite runtime,
+you can use the `java_lite_proto_library` rule. Check out [our build files
+examples](../examples/BUILD) to learn how to use it.
diff --git a/java/lite/generate-sources-build.xml b/java/lite/generate-sources-build.xml
deleted file mode 100644
index 89c21c13..00000000
--- a/java/lite/generate-sources-build.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-<project name="generate-sources">
- <echo message="Running protoc ..."/>
- <mkdir dir="${generated.sources.lite.dir}"/>
- <exec executable="${protoc}">
- <arg value="--java_out=lite:${generated.sources.lite.dir}"/>
- <arg value="--proto_path=${protobuf.source.dir}"/>
- <arg value="${protobuf.source.dir}/google/protobuf/any.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/api.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/descriptor.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/duration.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/empty.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/field_mask.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/source_context.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/struct.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/timestamp.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/type.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/wrappers.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/compiler/plugin.proto"/>
- </exec>
-</project>
diff --git a/java/lite/generate-test-sources-build.xml b/java/lite/generate-test-sources-build.xml
deleted file mode 100644
index cdd1ee89..00000000
--- a/java/lite/generate-test-sources-build.xml
+++ /dev/null
@@ -1,43 +0,0 @@
-<project name="generate-test-sources">
- <mkdir dir="${generated.testsources.lite.dir}"/>
- <exec executable="${protoc}">
- <arg value="--java_out=lite:${generated.testsources.lite.dir}"/>
- <arg value="--proto_path=${protobuf.source.dir}"/>
- <arg value="--proto_path=${test.proto.dir}"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_import.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_import_public.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_mset.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_mset_wire_format.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_optimize_for.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_custom_options.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_lite.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_import_lite.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_import_public_lite.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_lite_imports_nonlite.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_enormous_descriptor.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_no_generic_services.proto"/>
- <arg value="${protobuf.source.dir}/google/protobuf/unittest_well_known_types.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/lazy_fields_lite.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/lite_equals_and_hash.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/multiple_files_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/nested_builders_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/nested_extension.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/nested_extension_lite.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/non_nested_extension.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/non_nested_extension_lite.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/outer_class_name_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/outer_class_name_test2.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/outer_class_name_test3.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/test_bad_identifiers.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/test_check_utf8.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/test_check_utf8_size.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/test_custom_options.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/any_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/field_presence_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/map_for_proto2_lite_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/map_for_proto2_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/map_test.proto"/>
- <arg value="${test.proto.dir}/com/google/protobuf/map_initialization_order_test.proto"/>
- </exec>
-</project>
diff --git a/java/lite/pom.xml b/java/lite/pom.xml
deleted file mode 100644
index c902f819..00000000
--- a/java/lite/pom.xml
+++ /dev/null
@@ -1,185 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.google.protobuf</groupId>
- <artifactId>protobuf-parent</artifactId>
- <version>3.0.0</version>
- </parent>
-
- <artifactId>protobuf-lite</artifactId>
- <packaging>bundle</packaging>
-
- <name>Protocol Buffers [Lite]</name>
- <description>A trimmed-down version of the Protocol Buffers library.</description>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymock</artifactId>
- </dependency>
- <dependency>
- <groupId>org.easymock</groupId>
- <artifactId>easymockclassextension</artifactId>
- </dependency>
- </dependencies>
-
- <properties>
- <core.root>../core</core.root>
- <test.proto.dir>${core.root}/src/test/proto</test.proto.dir>
- </properties>
-
- <build>
- <sourceDirectory>${core.root}/src/main/java</sourceDirectory>
- <testSourceDirectory>${core.root}/src/test/java</testSourceDirectory>
-
- <plugins>
- <!-- Use Antrun plugin to generate sources with protoc -->
- <plugin>
- <artifactId>maven-antrun-plugin</artifactId>
- <executions>
- <!-- Generate core protos -->
- <execution>
- <id>generate-sources</id>
- <phase>generate-sources</phase>
- <configuration>
- <target>
- <ant antfile="generate-sources-build.xml"/>
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
-
- <!-- Generate the test protos -->
- <execution>
- <id>generate-test-sources</id>
- <phase>generate-test-sources</phase>
- <configuration>
- <target>
- <ant antfile="generate-test-sources-build.xml"/>
- </target>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
- <!-- Only compile a subset of the files -->
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>build-helper-maven-plugin</artifactId>
- <executions>
- <execution>
- <id>add-generated-sources</id>
- <phase>generate-sources</phase>
- <goals>
- <goal>add-source</goal>
- </goals>
- <configuration>
- <sources>
- <source>${generated.sources.lite.dir}</source>
- </sources>
- </configuration>
- </execution>
- <execution>
- <id>add-generated-test-sources</id>
- <phase>generate-test-sources</phase>
- <goals>
- <goal>add-test-source</goal>
- </goals>
- <configuration>
- <sources>
- <source>${generated.testsources.lite.dir}</source>
- </sources>
- </configuration>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <includes>
- <include>**/AbstractMessageLite.java</include>
- <include>**/AbstractParser.java</include>
- <include>**/AbstractProtobufList.java</include>
- <include>**/BooleanArrayList.java</include>
- <include>**/ByteString.java</include>
- <include>**/CodedInputStream.java</include>
- <include>**/CodedOutputStream.java</include>
- <include>**/DoubleArrayList.java</include>
- <include>**/ExtensionLite.java</include>
- <include>**/ExtensionRegistryLite.java</include>
- <include>**/FieldSet.java</include>
- <include>**/FloatArrayList.java</include>
- <include>**/GeneratedMessageLite.java</include>
- <include>**/IntArrayList.java</include>
- <include>**/Internal.java</include>
- <include>**/InvalidProtocolBufferException.java</include>
- <include>**/LazyFieldLite.java</include>
- <include>**/LazyStringArrayList.java</include>
- <include>**/LazyStringList.java</include>
- <include>**/LongArrayList.java</include>
- <include>**/MapEntryLite.java</include>
- <include>**/MapFieldLite.java</include>
- <include>**/MessageLite.java</include>
- <include>**/MessageLiteOrBuilder.java</include>
- <include>**/MessageLiteToString.java</include>
- <include>**/MutabilityOracle.java</include>
- <include>**/NioByteString.java</include>
- <include>**/Parser.java</include>
- <include>**/PrimitiveNonBoxingCollection.java</include>
- <include>**/ProtobufArrayList.java</include>
- <include>**/ProtocolStringList.java</include>
- <include>**/RopeByteString.java</include>
- <include>**/SmallSortedMap.java</include>
- <include>**/TextFormatEscaper.java</include>
- <include>**/UninitializedMessageException.java</include>
- <include>**/UnknownFieldSetLite.java</include>
- <include>**/UnmodifiableLazyStringList.java</include>
- <include>**/UnsafeByteOperations.java</include>
- <include>**/Utf8.java</include>
- <include>**/WireFormat.java</include>
- </includes>
- <testIncludes>
- <testInclude>**/*Lite.java</testInclude>
- <testInclude>**/BooleanArrayListTest.java</testInclude>
- <testInclude>**/DoubleArrayListTest.java</testInclude>
- <testInclude>**/FloatArrayListTest.java</testInclude>
- <testInclude>**/IntArrayListTest.java</testInclude>
- <testInclude>**/LazyMessageLiteTest.java</testInclude>
- <testInclude>**/LiteTest.java</testInclude>
- <testInclude>**/LongArrayListTest.java</testInclude>
- <testInclude>**/NioByteStringTest.java</testInclude>
- <testInclude>**/ProtobufArrayListTest.java</testInclude>
- <testInclude>**/UnknownFieldSetLiteTest.java</testInclude>
- </testIncludes>
- </configuration>
- </plugin>
-
- <!-- OSGI bundle configuration -->
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <extensions>true</extensions>
- <configuration>
- <instructions>
- <Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
- <Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName>
- <Export-Package>com.google.${project.artifactId};version=${project.version}</Export-Package>
- </instructions>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
-</project>
diff --git a/java/pom.xml b/java/pom.xml
index bc8e89b5..35d653b4 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -11,7 +11,7 @@
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-parent</artifactId>
- <version>3.4.1</version>
+ <version>3.6.0</version>
<packaging>pom</packaging>
<name>Protocol Buffers [Parent]</name>
@@ -33,8 +33,6 @@
<test.proto.dir>src/test/proto</test.proto.dir>
<generated.sources.dir>${project.build.directory}/generated-sources</generated.sources.dir>
<generated.testsources.dir>${project.build.directory}/generated-test-sources</generated.testsources.dir>
- <generated.sources.lite.dir>${project.build.directory}/generated-sources-lite</generated.sources.lite.dir>
- <generated.testsources.lite.dir>${project.build.directory}/generated-test-sources-lite</generated.testsources.lite.dir>
</properties>
<licenses>
@@ -96,8 +94,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
- <source>1.6</source>
- <target>1.6</target>
+ <source>1.7</source>
+ <target>1.7</target>
</configuration>
</plugin>
<plugin>
@@ -208,7 +206,6 @@
<modules>
<module>core</module>
- <!-- <module>lite</module> -->
<module>util</module>
</modules>
diff --git a/java/util/pom.xml b/java/util/pom.xml
index 35afb844..8ea4a40d 100644
--- a/java/util/pom.xml
+++ b/java/util/pom.xml
@@ -6,7 +6,7 @@
<parent>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-parent</artifactId>
- <version>3.4.1</version>
+ <version>3.6.0</version>
</parent>
<artifactId>protobuf-java-util</artifactId>
diff --git a/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java b/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java
index e017b987..4a13fb1d 100644
--- a/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java
+++ b/java/util/src/main/java/com/google/protobuf/util/FieldMaskTree.java
@@ -245,7 +245,7 @@ final class FieldMaskTree {
}
if (!source.hasField(field) && !destination.hasField(field)) {
// If the message field is not present in both source and destination, skip recursing
- // so we don't create unneccessary empty messages.
+ // so we don't create unnecessary empty messages.
continue;
}
String childPath = path.isEmpty() ? entry.getKey() : path + "." + entry.getKey();
diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
index a26dbc2d..7f69ee68 100644
--- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
+++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
@@ -105,7 +105,7 @@ public class JsonFormat {
public static Printer printer() {
return new Printer(
TypeRegistry.getEmptyTypeRegistry(), false, Collections.<FieldDescriptor>emptySet(),
- false, false);
+ false, false, false);
}
/**
@@ -125,18 +125,21 @@ public class JsonFormat {
private Set<FieldDescriptor> includingDefaultValueFields;
private final boolean preservingProtoFieldNames;
private final boolean omittingInsignificantWhitespace;
+ private final boolean printingEnumsAsInts;
private Printer(
TypeRegistry registry,
boolean alwaysOutputDefaultValueFields,
Set<FieldDescriptor> includingDefaultValueFields,
boolean preservingProtoFieldNames,
- boolean omittingInsignificantWhitespace) {
+ boolean omittingInsignificantWhitespace,
+ boolean printingEnumsAsInts) {
this.registry = registry;
this.alwaysOutputDefaultValueFields = alwaysOutputDefaultValueFields;
this.includingDefaultValueFields = includingDefaultValueFields;
this.preservingProtoFieldNames = preservingProtoFieldNames;
this.omittingInsignificantWhitespace = omittingInsignificantWhitespace;
+ this.printingEnumsAsInts = printingEnumsAsInts;
}
/**
@@ -154,7 +157,8 @@ public class JsonFormat {
alwaysOutputDefaultValueFields,
includingDefaultValueFields,
preservingProtoFieldNames,
- omittingInsignificantWhitespace);
+ omittingInsignificantWhitespace,
+ printingEnumsAsInts);
}
/**
@@ -170,7 +174,31 @@ public class JsonFormat {
true,
Collections.<FieldDescriptor>emptySet(),
preservingProtoFieldNames,
- omittingInsignificantWhitespace);
+ omittingInsignificantWhitespace,
+ printingEnumsAsInts);
+ }
+
+ /**
+ * Creates a new {@link Printer} that will print enum field values as integers instead of as
+ * string.
+ * The new Printer clones all other configurations from the current
+ * {@link Printer}.
+ */
+ public Printer printingEnumsAsInts() {
+ checkUnsetPrintingEnumsAsInts();
+ return new Printer(
+ registry,
+ alwaysOutputDefaultValueFields,
+ Collections.<FieldDescriptor>emptySet(),
+ preservingProtoFieldNames,
+ omittingInsignificantWhitespace,
+ true);
+ }
+
+ private void checkUnsetPrintingEnumsAsInts() {
+ if (printingEnumsAsInts) {
+ throw new IllegalStateException("JsonFormat printingEnumsAsInts has already been set.");
+ }
}
/**
@@ -191,7 +219,8 @@ public class JsonFormat {
false,
fieldsToAlwaysOutput,
preservingProtoFieldNames,
- omittingInsignificantWhitespace);
+ omittingInsignificantWhitespace,
+ printingEnumsAsInts);
}
private void checkUnsetIncludingDefaultValueFields() {
@@ -213,7 +242,8 @@ public class JsonFormat {
alwaysOutputDefaultValueFields,
includingDefaultValueFields,
true,
- omittingInsignificantWhitespace);
+ omittingInsignificantWhitespace,
+ printingEnumsAsInts);
}
@@ -240,7 +270,8 @@ public class JsonFormat {
alwaysOutputDefaultValueFields,
includingDefaultValueFields,
preservingProtoFieldNames,
- true);
+ true,
+ printingEnumsAsInts);
}
/**
@@ -259,7 +290,8 @@ public class JsonFormat {
includingDefaultValueFields,
preservingProtoFieldNames,
output,
- omittingInsignificantWhitespace)
+ omittingInsignificantWhitespace,
+ printingEnumsAsInts)
.print(message);
}
@@ -416,7 +448,7 @@ public class JsonFormat {
*/
public Builder add(Iterable<Descriptor> messageTypes) {
if (types == null) {
- throw new IllegalStateException("A TypeRegistry.Builer can only be used once.");
+ throw new IllegalStateException("A TypeRegistry.Builder can only be used once.");
}
for (Descriptor type : messageTypes) {
addFile(type.getFile());
@@ -570,6 +602,7 @@ public class JsonFormat {
private final boolean alwaysOutputDefaultValueFields;
private final Set<FieldDescriptor> includingDefaultValueFields;
private final boolean preservingProtoFieldNames;
+ private final boolean printingEnumsAsInts;
private final TextGenerator generator;
// We use Gson to help handle string escapes.
private final Gson gson;
@@ -586,11 +619,13 @@ public class JsonFormat {
Set<FieldDescriptor> includingDefaultValueFields,
boolean preservingProtoFieldNames,
Appendable jsonOutput,
- boolean omittingInsignificantWhitespace) {
+ boolean omittingInsignificantWhitespace,
+ boolean printingEnumsAsInts) {
this.registry = registry;
this.alwaysOutputDefaultValueFields = alwaysOutputDefaultValueFields;
this.includingDefaultValueFields = includingDefaultValueFields;
this.preservingProtoFieldNames = preservingProtoFieldNames;
+ this.printingEnumsAsInts = printingEnumsAsInts;
this.gson = GsonHolder.DEFAULT_GSON;
// json format related properties, determined by printerType
if (omittingInsignificantWhitespace) {
@@ -1069,7 +1104,7 @@ public class JsonFormat {
generator.print("\"");
}
} else {
- if (((EnumValueDescriptor) value).getIndex() == -1) {
+ if (printingEnumsAsInts || ((EnumValueDescriptor) value).getIndex() == -1) {
generator.print(String.valueOf(((EnumValueDescriptor) value).getNumber()));
} else {
generator.print("\"" + ((EnumValueDescriptor) value).getName() + "\"");
diff --git a/java/util/src/main/java/com/google/protobuf/util/Timestamps.java b/java/util/src/main/java/com/google/protobuf/util/Timestamps.java
index 7a1f2014..9e528d4a 100644
--- a/java/util/src/main/java/com/google/protobuf/util/Timestamps.java
+++ b/java/util/src/main/java/com/google/protobuf/util/Timestamps.java
@@ -74,6 +74,11 @@ public final class Timestamps {
public static final Timestamp MAX_VALUE =
Timestamp.newBuilder().setSeconds(TIMESTAMP_SECONDS_MAX).setNanos(999999999).build();
+ /**
+ * A constant holding the {@link Timestamp} of epoch time, {@code 1970-01-01T00:00:00.000000000Z}.
+ */
+ public static final Timestamp EPOCH = Timestamp.newBuilder().setSeconds(0).setNanos(0).build();
+
private static final ThreadLocal<SimpleDateFormat> timestampFormat =
new ThreadLocal<SimpleDateFormat>() {
@Override
@@ -358,10 +363,12 @@ public final class Timestamps {
static Timestamp normalizedTimestamp(long seconds, int nanos) {
if (nanos <= -NANOS_PER_SECOND || nanos >= NANOS_PER_SECOND) {
seconds = checkedAdd(seconds, nanos / NANOS_PER_SECOND);
- nanos %= NANOS_PER_SECOND;
+ nanos = (int) (nanos % NANOS_PER_SECOND);
}
if (nanos < 0) {
- nanos += NANOS_PER_SECOND; // no overflow since nanos is negative (and we're adding)
+ nanos =
+ (int)
+ (nanos + NANOS_PER_SECOND); // no overflow since nanos is negative (and we're adding)
seconds = checkedSubtract(seconds, 1);
}
Timestamp timestamp = Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build();
diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
index e4c5387a..6ef08508 100644
--- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
+++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
@@ -1174,6 +1174,14 @@ public class JsonFormatTest extends TestCase {
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
}
+ public void testParserIntegerEnumValue() throws Exception {
+ TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder();
+ mergeFromJson("{\n" + " \"optionalNestedEnum\": 2\n" + "}", actualBuilder);
+
+ TestAllTypes expected = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.BAZ).build();
+ assertEquals(expected, actualBuilder.build());
+ }
+
public void testCustomJsonName() throws Exception {
TestCustomJsonName message = TestCustomJsonName.newBuilder().setValue(12345).build();
assertEquals("{\n" + " \"@value\": 12345\n" + "}", JsonFormat.printer().print(message));
@@ -1441,6 +1449,13 @@ public class JsonFormatTest extends TestCase {
assertEquals(54321, builder.getOptionalInt32());
}
+ public void testPrintingEnumsAsInts() throws Exception {
+ TestAllTypes message = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.BAR).build();
+ assertEquals(
+ "{\n" + " \"optionalNestedEnum\": 1\n" + "}",
+ JsonFormat.printer().printingEnumsAsInts().print(message));
+ }
+
public void testOmittingInsignificantWhiteSpace() throws Exception {
TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
assertEquals(