diff options
Diffstat (limited to 'java/core/src/test')
20 files changed, 405 insertions, 167 deletions
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 cb2d34eb..bb11bd0f 100644 --- a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java @@ -210,8 +210,8 @@ public class AbstractMessageTest extends TestCase { new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); TestUtil.ReflectionTester extensionsReflectionTester = - new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(), - TestUtil.getExtensionRegistry()); + new TestUtil.ReflectionTester( + TestAllExtensions.getDescriptor(), TestUtil.getFullExtensionRegistry()); public void testClear() throws Exception { AbstractMessageWrapper message = 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 4906763c..febe8537 100644 --- a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java @@ -299,20 +299,22 @@ public class BooleanArrayListTest extends TestCase { } public void testRemoveEndOfCapacity() { - BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); + 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); + BooleanList toRemove = + BooleanArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addBoolean(true); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); } - private void assertImmutable(BooleanArrayList list) { + private void assertImmutable(BooleanList list) { try { list.add(true); diff --git a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java index be71f1f5..fc22955d 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java @@ -41,6 +41,7 @@ import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; @@ -86,6 +87,40 @@ public class ByteStringTest extends TestCase { return left.length == right.length && isArrayRange(left, right, 0, left.length); } + public void testCompare_equalByteStrings_compareEqual() throws Exception { + byte[] referenceBytes = getTestBytes(); + ByteString string1 = ByteString.copyFrom(referenceBytes); + ByteString string2 = ByteString.copyFrom(referenceBytes); + + assertEquals( + "ByteString instances containing the same data must compare equal.", + 0, + ByteString.unsignedLexicographicalComparator().compare(string1, string2)); + } + + public void testCompare_byteStringsSortLexicographically() throws Exception { + ByteString app = ByteString.copyFromUtf8("app"); + ByteString apple = ByteString.copyFromUtf8("apple"); + ByteString banana = ByteString.copyFromUtf8("banana"); + + Comparator<ByteString> comparator = ByteString.unsignedLexicographicalComparator(); + + assertTrue("ByteString(app) < ByteString(apple)", comparator.compare(app, apple) < 0); + assertTrue("ByteString(app) < ByteString(banana)", comparator.compare(app, banana) < 0); + assertTrue("ByteString(apple) < ByteString(banana)", comparator.compare(apple, banana) < 0); + } + + public void testCompare_interpretsByteValuesAsUnsigned() throws Exception { + // Two's compliment of `-1` == 0b11111111 == 255 + ByteString twoHundredFiftyFive = ByteString.copyFrom(new byte[] {-1}); + // 0b00000001 == 1 + ByteString one = ByteString.copyFrom(new byte[] {1}); + + assertTrue( + "ByteString comparison treats bytes as unsigned values", + ByteString.unsignedLexicographicalComparator().compare(one, twoHundredFiftyFive) < 0); + } + public void testSubstring_BeginIndex() { byte[] bytes = getTestBytes(); ByteString substring = ByteString.copyFrom(bytes).substring(500); @@ -161,6 +196,34 @@ public class ByteStringTest extends TestCase { byteString, byteStringAlt); } + public void testCopyFrom_LengthTooBig() { + byte[] testBytes = getTestBytes(100); + try { + ByteString.copyFrom(testBytes, 0, 200); + fail("Should throw"); + } catch (IndexOutOfBoundsException expected) { + } + + try { + ByteString.copyFrom(testBytes, 99, 2); + fail(); + } catch (IndexOutOfBoundsException expected) { + } + + ByteBuffer buf = ByteBuffer.wrap(testBytes); + try { + ByteString.copyFrom(buf, 101); + fail(); + } catch (IndexOutOfBoundsException expected) { + } + + try { + ByteString.copyFrom(testBytes, -1, 10); + fail("Should throw"); + } catch (IndexOutOfBoundsException expected) { + } + } + public void testCopyTo_TargetOffset() { byte[] bytes = getTestBytes(); ByteString byteString = ByteString.copyFrom(bytes); @@ -761,6 +824,9 @@ public class ByteStringTest extends TestCase { * Tests ByteString uses Arrays based byte copier when running under Hotstop VM. */ public void testByteArrayCopier() throws Exception { + if (Android.isOnAndroidDevice()) { + return; + } Field field = ByteString.class.getDeclaredField("byteArrayCopier"); field.setAccessible(true); Object byteArrayCopier = field.get(null); diff --git a/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java b/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java index 0f09a51b..36c4611f 100644 --- a/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DiscardUnknownFieldsTest.java @@ -62,22 +62,17 @@ public class DiscardUnknownFieldsTest { } private static void testProto2Message(Message message) throws Exception { - assertUnknownFieldsDefaultPreserved(message); + assertUnknownFieldsPreserved(message); assertUnknownFieldsExplicitlyDiscarded(message); assertReuseCodedInputStreamPreserve(message); assertUnknownFieldsInUnknownFieldSetArePreserve(message); } private static void testProto3Message(Message message) throws Exception { - CodedInputStream.setProto3KeepUnknownsByDefaultForTest(); - assertUnknownFieldsDefaultPreserved(message); + assertUnknownFieldsPreserved(message); assertUnknownFieldsExplicitlyDiscarded(message); assertReuseCodedInputStreamPreserve(message); assertUnknownFieldsInUnknownFieldSetArePreserve(message); - CodedInputStream.setProto3DiscardUnknownsByDefaultForTest(); - assertUnknownFieldsDefaultDiscarded(message); - assertUnknownFieldsExplicitlyDiscarded(message); - assertUnknownFieldsInUnknownFieldSetAreDiscarded(message); } private static void assertReuseCodedInputStreamPreserve(Message message) throws Exception { @@ -122,7 +117,7 @@ public class DiscardUnknownFieldsTest { assertEquals(message.getClass().getName(), 0, built.getSerializedSize()); } - private static void assertUnknownFieldsDefaultPreserved(MessageLite message) throws Exception { + private static void assertUnknownFieldsPreserved(MessageLite message) throws Exception { { MessageLite parsed = message.getParserForType().parseFrom(payload); assertEquals(message.getClass().getName(), payload, parsed.toByteString()); @@ -134,18 +129,6 @@ public class DiscardUnknownFieldsTest { } } - private static void assertUnknownFieldsDefaultDiscarded(MessageLite message) throws Exception { - { - MessageLite parsed = message.getParserForType().parseFrom(payload); - assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize()); - } - - { - MessageLite parsed = message.newBuilderForType().mergeFrom(payload).build(); - assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize()); - } - } - private static void assertUnknownFieldsExplicitlyDiscarded(Message message) throws Exception { Message parsed = DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(payload); 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 923d7f43..a4c2f5aa 100644 --- a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java @@ -78,10 +78,10 @@ public class DoubleArrayListTest extends TestCase { list.addAll(asList(1D, 2D, 3D, 4D)); Iterator<Double> iterator = list.iterator(); assertEquals(4, list.size()); - assertEquals(1D, (double) list.get(0)); - assertEquals(1D, (double) iterator.next()); + assertEquals(1D, (double) list.get(0), 0.0); + assertEquals(1D, (double) iterator.next(), 0.0); list.set(0, 1D); - assertEquals(2D, (double) iterator.next()); + assertEquals(2D, (double) iterator.next(), 0.0); list.remove(0); try { @@ -102,9 +102,9 @@ public class DoubleArrayListTest extends TestCase { } public void testGet() { - assertEquals(1D, (double) TERTIARY_LIST.get(0)); - assertEquals(2D, (double) TERTIARY_LIST.get(1)); - assertEquals(3D, (double) TERTIARY_LIST.get(2)); + assertEquals(1D, (double) TERTIARY_LIST.get(0), 0.0); + assertEquals(2D, (double) TERTIARY_LIST.get(1), 0.0); + assertEquals(3D, (double) TERTIARY_LIST.get(2), 0.0); try { TERTIARY_LIST.get(-1); @@ -122,9 +122,9 @@ public class DoubleArrayListTest extends TestCase { } public void testGetDouble() { - assertEquals(1D, TERTIARY_LIST.getDouble(0)); - assertEquals(2D, TERTIARY_LIST.getDouble(1)); - assertEquals(3D, TERTIARY_LIST.getDouble(2)); + assertEquals(1D, TERTIARY_LIST.getDouble(0), 0.0); + assertEquals(2D, TERTIARY_LIST.getDouble(1), 0.0); + assertEquals(3D, TERTIARY_LIST.getDouble(2), 0.0); try { TERTIARY_LIST.get(-1); @@ -163,11 +163,11 @@ public class DoubleArrayListTest extends TestCase { list.addDouble(2); list.addDouble(4); - assertEquals(2D, (double) list.set(0, 3D)); - assertEquals(3D, list.getDouble(0)); + assertEquals(2D, (double) list.set(0, 3D), 0.0); + assertEquals(3D, list.getDouble(0), 0.0); - assertEquals(4D, (double) list.set(1, 0D)); - assertEquals(0D, list.getDouble(1)); + assertEquals(4D, (double) list.set(1, 0D), 0.0); + assertEquals(0D, list.getDouble(1), 0.0); try { list.set(-1, 0D); @@ -188,11 +188,11 @@ public class DoubleArrayListTest extends TestCase { list.addDouble(1); list.addDouble(3); - assertEquals(1D, list.setDouble(0, 0)); - assertEquals(0D, list.getDouble(0)); + assertEquals(1D, list.setDouble(0, 0), 0.0); + assertEquals(0D, list.getDouble(0), 0.0); - assertEquals(3D, list.setDouble(1, 0)); - assertEquals(0D, list.getDouble(1)); + assertEquals(3D, list.setDouble(1, 0), 0.0); + assertEquals(0D, list.getDouble(1), 0.0); try { list.setDouble(-1, 0); @@ -257,8 +257,8 @@ public class DoubleArrayListTest extends TestCase { assertTrue(list.addAll(Collections.singleton(1D))); assertEquals(1, list.size()); - assertEquals(1D, (double) list.get(0)); - assertEquals(1D, list.getDouble(0)); + assertEquals(1D, (double) list.get(0), 0.0); + assertEquals(1D, list.getDouble(0), 0.0); assertTrue(list.addAll(asList(2D, 3D, 4D, 5D, 6D))); assertEquals(asList(1D, 2D, 3D, 4D, 5D, 6D), list); @@ -272,7 +272,7 @@ public class DoubleArrayListTest extends TestCase { public void testRemove() { list.addAll(TERTIARY_LIST); - assertEquals(1D, (double) list.remove(0)); + assertEquals(1D, (double) list.remove(0), 0.0); assertEquals(asList(2D, 3D), list); assertTrue(list.remove(Double.valueOf(3))); @@ -281,7 +281,7 @@ public class DoubleArrayListTest extends TestCase { assertFalse(list.remove(Double.valueOf(3))); assertEquals(asList(2D), list); - assertEquals(2D, (double) list.remove(0)); + assertEquals(2D, (double) list.remove(0), 0.0); assertEquals(asList(), list); try { @@ -299,20 +299,22 @@ public class DoubleArrayListTest extends TestCase { } public void testRemoveEndOfCapacity() { - DoubleList toRemove = DoubleArrayList.emptyList().mutableCopyWithCapacity(1); + 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); + DoubleList toRemove = + DoubleArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addDouble(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); } - private void assertImmutable(DoubleArrayList list) { + private void assertImmutable(DoubleList 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/DynamicMessageTest.java b/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java index 77d14f6b..346c1e6a 100644 --- a/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java @@ -51,8 +51,8 @@ public class DynamicMessageTest extends TestCase { new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); TestUtil.ReflectionTester extensionsReflectionTester = - new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(), - TestUtil.getExtensionRegistry()); + new TestUtil.ReflectionTester( + TestAllExtensions.getDescriptor(), TestUtil.getFullExtensionRegistry()); TestUtil.ReflectionTester packedReflectionTester = new TestUtil.ReflectionTester(TestPackedTypes.getDescriptor(), null); @@ -194,9 +194,9 @@ public class DynamicMessageTest extends TestCase { public void testDynamicMessageExtensionParsing() throws Exception { ByteString rawBytes = TestUtil.getAllExtensionsSet().toByteString(); - Message message = DynamicMessage.parseFrom( - TestAllExtensions.getDescriptor(), rawBytes, - TestUtil.getExtensionRegistry()); + Message message = + DynamicMessage.parseFrom( + TestAllExtensions.getDescriptor(), rawBytes, TestUtil.getFullExtensionRegistry()); extensionsReflectionTester.assertAllFieldsSetViaReflection(message); // Test Parser interface. 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 903a79db..38eccc93 100644 --- a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java @@ -78,10 +78,10 @@ public class FloatArrayListTest extends TestCase { list.addAll(asList(1F, 2F, 3F, 4F)); Iterator<Float> iterator = list.iterator(); assertEquals(4, list.size()); - assertEquals(1F, (float) list.get(0)); - assertEquals(1F, (float) iterator.next()); + assertEquals(1F, (float) list.get(0), 0.0f); + assertEquals(1F, (float) iterator.next(), 0.0f); list.set(0, 1F); - assertEquals(2F, (float) iterator.next()); + assertEquals(2F, (float) iterator.next(), 0.0f); list.remove(0); try { @@ -102,9 +102,9 @@ public class FloatArrayListTest extends TestCase { } public void testGet() { - assertEquals(1F, (float) TERTIARY_LIST.get(0)); - assertEquals(2F, (float) TERTIARY_LIST.get(1)); - assertEquals(3F, (float) TERTIARY_LIST.get(2)); + assertEquals(1F, (float) TERTIARY_LIST.get(0), 0.0f); + assertEquals(2F, (float) TERTIARY_LIST.get(1), 0.0f); + assertEquals(3F, (float) TERTIARY_LIST.get(2), 0.0f); try { TERTIARY_LIST.get(-1); @@ -122,9 +122,9 @@ public class FloatArrayListTest extends TestCase { } public void testGetFloat() { - assertEquals(1F, TERTIARY_LIST.getFloat(0)); - assertEquals(2F, TERTIARY_LIST.getFloat(1)); - assertEquals(3F, TERTIARY_LIST.getFloat(2)); + assertEquals(1F, TERTIARY_LIST.getFloat(0), 0.0f); + assertEquals(2F, TERTIARY_LIST.getFloat(1), 0.0f); + assertEquals(3F, TERTIARY_LIST.getFloat(2), 0.0f); try { TERTIARY_LIST.get(-1); @@ -163,11 +163,11 @@ public class FloatArrayListTest extends TestCase { list.addFloat(2); list.addFloat(4); - assertEquals(2F, (float) list.set(0, 3F)); - assertEquals(3F, list.getFloat(0)); + assertEquals(2F, (float) list.set(0, 3F), 0.0f); + assertEquals(3F, list.getFloat(0), 0.0f); - assertEquals(4F, (float) list.set(1, 0F)); - assertEquals(0F, list.getFloat(1)); + assertEquals(4F, (float) list.set(1, 0F), 0.0f); + assertEquals(0F, list.getFloat(1), 0.0f); try { list.set(-1, 0F); @@ -188,11 +188,11 @@ public class FloatArrayListTest extends TestCase { list.addFloat(1); list.addFloat(3); - assertEquals(1F, list.setFloat(0, 0)); - assertEquals(0F, list.getFloat(0)); + assertEquals(1F, list.setFloat(0, 0), 0.0f); + assertEquals(0F, list.getFloat(0), 0.0f); - assertEquals(3F, list.setFloat(1, 0)); - assertEquals(0F, list.getFloat(1)); + assertEquals(3F, list.setFloat(1, 0), 0.0f); + assertEquals(0F, list.getFloat(1), 0.0f); try { list.setFloat(-1, 0); @@ -257,8 +257,8 @@ public class FloatArrayListTest extends TestCase { assertTrue(list.addAll(Collections.singleton(1F))); assertEquals(1, list.size()); - assertEquals(1F, (float) list.get(0)); - assertEquals(1F, list.getFloat(0)); + assertEquals(1F, (float) list.get(0), 0.0f); + assertEquals(1F, list.getFloat(0), 0.0f); assertTrue(list.addAll(asList(2F, 3F, 4F, 5F, 6F))); assertEquals(asList(1F, 2F, 3F, 4F, 5F, 6F), list); @@ -272,7 +272,7 @@ public class FloatArrayListTest extends TestCase { public void testRemove() { list.addAll(TERTIARY_LIST); - assertEquals(1F, (float) list.remove(0)); + assertEquals(1F, (float) list.remove(0), 0.0f); assertEquals(asList(2F, 3F), list); assertTrue(list.remove(Float.valueOf(3))); @@ -281,7 +281,7 @@ public class FloatArrayListTest extends TestCase { assertFalse(list.remove(Float.valueOf(3))); assertEquals(asList(2F), list); - assertEquals(2F, (float) list.remove(0)); + assertEquals(2F, (float) list.remove(0), 0.0f); assertEquals(asList(), list); try { @@ -299,20 +299,22 @@ public class FloatArrayListTest extends TestCase { } public void testRemoveEndOfCapacity() { - FloatList toRemove = FloatArrayList.emptyList().mutableCopyWithCapacity(1); + 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); + FloatList toRemove = + FloatArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addFloat(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); } - private void assertImmutable(FloatArrayList list) { + private void assertImmutable(FloatList 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/GeneratedMessageTest.java b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java index c9ebe7f5..a4143cd4 100644 --- a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java @@ -590,8 +590,8 @@ public class GeneratedMessageTest extends TestCase { // Extensions. TestUtil.ReflectionTester extensionsReflectionTester = - new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(), - TestUtil.getExtensionRegistry()); + new TestUtil.ReflectionTester( + TestAllExtensions.getDescriptor(), TestUtil.getFullExtensionRegistry()); public void testExtensionMessageOrBuilder() throws Exception { TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 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 d8e97d4f..9edc4344 100644 --- a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java @@ -299,20 +299,22 @@ public class IntArrayListTest extends TestCase { } public void testRemoveEndOfCapacity() { - IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); + 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); + IntList toRemove = + IntArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addInt(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); } - private void assertImmutable(IntArrayList list) { + private void assertImmutable(IntList list) { if (list.contains(1)) { throw new RuntimeException("Cannot test the immutability of lists that contain 1."); } diff --git a/java/core/src/test/java/com/google/protobuf/LiteTest.java b/java/core/src/test/java/com/google/protobuf/LiteTest.java index 5ab80ca2..b20114e0 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteTest.java @@ -57,6 +57,8 @@ 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.io.IOException; +import java.io.OutputStream; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -2378,4 +2380,63 @@ public class LiteTest extends TestCase { } catch (NullPointerException expected) { } } + + public void testSerializeToOutputStreamThrowsIOException() { + try { + TestAllTypesLite.newBuilder() + .setOptionalBytes(ByteString.copyFromUtf8("hello")) + .build() + .writeTo( + new OutputStream() { + + @Override + public void write(int b) throws IOException { + throw new IOException(); + } + }); + fail(); + } catch (IOException expected) { + } + } + + public void testUnpairedSurrogatesReplacedByQuestionMark() throws InvalidProtocolBufferException { + String testString = "foo \ud83d bar"; + String expectedString = "foo ? bar"; + + TestAllTypesLite testMessage = + TestAllTypesLite.newBuilder().setOptionalString(testString).build(); + ByteString serializedMessage = testMessage.toByteString(); + + // Behavior is compatible with String.getBytes("UTF-8"), which replaces + // unpaired surrogates with a question mark. + TestAllTypesLite parsedMessage = TestAllTypesLite.parseFrom(serializedMessage); + assertEquals(expectedString, parsedMessage.getOptionalString()); + + // Conversion happens during serialization. + ByteString expectedBytes = ByteString.copyFromUtf8(expectedString); + assertTrue( + String.format( + "Expected serializedMessage (%s) to contain \"%s\" (%s).", + encodeHex(serializedMessage), expectedString, encodeHex(expectedBytes)), + contains(serializedMessage, expectedBytes)); + } + + private String encodeHex(ByteString bytes) { + String hexDigits = "0123456789abcdef"; + StringBuilder stringBuilder = new StringBuilder(bytes.size() * 2); + for (byte b : bytes) { + stringBuilder.append(hexDigits.charAt((b & 0xf0) >> 4)); + stringBuilder.append(hexDigits.charAt(b & 0x0f)); + } + return stringBuilder.toString(); + } + + private boolean contains(ByteString a, ByteString b) { + for (int i = 0; i <= a.size() - b.size(); ++i) { + if (a.substring(i, i + b.size()).equals(b)) { + return true; + } + } + return false; + } } 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 e50c7d1e..14a8e159 100644 --- a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java @@ -299,20 +299,22 @@ public class LongArrayListTest extends TestCase { } public void testRemoveEndOfCapacity() { - LongList toRemove = LongArrayList.emptyList().mutableCopyWithCapacity(1); + 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); + LongList toRemove = + LongArrayList.emptyList().mutableCopyWithCapacity(1); toRemove.addLong(3); toRemove.subList(0, 1).clear(); assertEquals(0, toRemove.size()); } - private void assertImmutable(LongArrayList list) { + private void assertImmutable(LongList 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/ProtobufArrayListTest.java b/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java index af717bfd..0a2c3e37 100644 --- a/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java @@ -42,33 +42,33 @@ import junit.framework.TestCase; * Tests for {@link ProtobufArrayList}. */ public class ProtobufArrayListTest extends TestCase { - + private static final ProtobufArrayList<Integer> UNARY_LIST = newImmutableProtoArrayList(1); private static final ProtobufArrayList<Integer> TERTIARY_LIST = newImmutableProtoArrayList(1, 2, 3); - + private ProtobufArrayList<Integer> list; - + @Override protected void setUp() throws Exception { list = new ProtobufArrayList<Integer>(); } - + public void testEmptyListReturnsSameInstance() { assertSame(ProtobufArrayList.emptyList(), ProtobufArrayList.emptyList()); } - + public void testEmptyListIsImmutable() { assertImmutable(ProtobufArrayList.<Integer>emptyList()); } - + public void testModificationWithIteration() { list.addAll(asList(1, 2, 3, 4)); Iterator<Integer> iterator = list.iterator(); assertEquals(4, list.size()); assertEquals(1, (int) list.get(0)); assertEquals(1, (int) iterator.next()); - + list.remove(0); try { iterator.next(); @@ -76,7 +76,7 @@ public class ProtobufArrayListTest extends TestCase { } catch (ConcurrentModificationException e) { // expected } - + iterator = list.iterator(); list.set(0, 1); try { @@ -85,7 +85,7 @@ public class ProtobufArrayListTest extends TestCase { } catch (ConcurrentModificationException e) { // expected } - + iterator = list.iterator(); list.add(0, 0); try { @@ -95,7 +95,7 @@ public class ProtobufArrayListTest extends TestCase { // expected } } - + public void testMakeImmutable() { list.add(2); list.add(4); @@ -104,107 +104,213 @@ public class ProtobufArrayListTest extends TestCase { list.makeImmutable(); assertImmutable(list); } - + public void testRemove() { - list.add(2); - list.add(4); - list.add(6); + list.addAll(TERTIARY_LIST); + assertEquals(1, (int) list.remove(0)); + assertEquals(asList(2, 3), list); - list.remove(1); - assertEquals(asList(2, 6), list); + assertTrue(list.remove(Integer.valueOf(3))); + assertEquals(asList(2), list); - list.remove(1); + assertFalse(list.remove(Integer.valueOf(3))); assertEquals(asList(2), list); - list.remove(0); + assertEquals(2, (int) list.remove(0)); assertEquals(asList(), list); + + try { + list.remove(-1); + fail(); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + list.remove(0); + } catch (IndexOutOfBoundsException e) { + // expected + } } - + public void testGet() { - list.add(2); - list.add(6); - - assertEquals(2, (int) list.get(0)); - assertEquals(6, (int) list.get(1)); + assertEquals(1, (int) TERTIARY_LIST.get(0)); + assertEquals(2, (int) TERTIARY_LIST.get(1)); + assertEquals(3, (int) TERTIARY_LIST.get(2)); + + try { + TERTIARY_LIST.get(-1); + fail(); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + TERTIARY_LIST.get(3); + fail(); + } catch (IndexOutOfBoundsException e) { + // expected + } } - + public void testSet() { list.add(2); - list.add(6); - - list.set(0, 1); + list.add(4); + + assertEquals(2, (int) list.set(0, 3)); + assertEquals(3, (int) list.get(0)); + + assertEquals(4, (int) list.set(1, 0)); + assertEquals(0, (int) list.get(1)); + + try { + list.set(-1, 0); + fail(); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + list.set(2, 0); + fail(); + } catch (IndexOutOfBoundsException e) { + // expected + } + } + + public void testAdd() { + assertEquals(0, list.size()); + + assertTrue(list.add(2)); + assertEquals(asList(2), list); + + assertTrue(list.add(3)); + list.add(0, 4); + assertEquals(asList(4, 2, 3), list); + + list.add(0, 1); + list.add(0, 0); + // Force a resize by getting up to 11 elements. + for (int i = 0; i < 6; i++) { + list.add(Integer.valueOf(5 + i)); + } + assertEquals(asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10), list); + + try { + list.add(-1, 5); + } catch (IndexOutOfBoundsException e) { + // expected + } + + try { + list.add(4, 5); + } catch (IndexOutOfBoundsException e) { + // expected + } + } + + public void testAddAll() { + assertEquals(0, list.size()); + + assertTrue(list.addAll(Collections.singleton(1))); + assertEquals(1, list.size()); assertEquals(1, (int) list.get(0)); - list.set(1, 2); - assertEquals(2, (int) list.get(1)); + + assertTrue(list.addAll(asList(2, 3, 4, 5, 6))); + assertEquals(asList(1, 2, 3, 4, 5, 6), list); + + assertTrue(list.addAll(TERTIARY_LIST)); + assertEquals(asList(1, 2, 3, 4, 5, 6, 1, 2, 3), list); + + assertFalse(list.addAll(Collections.<Integer>emptyList())); + assertFalse(list.addAll(IntArrayList.emptyList())); + } + + public void testSize() { + assertEquals(0, ProtobufArrayList.emptyList().size()); + assertEquals(1, UNARY_LIST.size()); + assertEquals(3, TERTIARY_LIST.size()); + + list.add(3); + list.add(4); + list.add(6); + list.add(8); + assertEquals(4, list.size()); + + list.remove(0); + assertEquals(3, list.size()); + + list.add(17); + assertEquals(4, list.size()); } private void assertImmutable(List<Integer> list) { if (list.contains(1)) { throw new RuntimeException("Cannot test the immutability of lists that contain 1."); } - + try { list.add(1); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.add(0, 1); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(Collections.<Integer>emptyList()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(Collections.singletonList(1)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(new ProtobufArrayList<Integer>()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(UNARY_LIST); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(0, Collections.singleton(1)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(0, UNARY_LIST); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.addAll(0, Collections.<Integer>emptyList()); fail(); } catch (UnsupportedOperationException e) { // expected - } + } try { list.clear(); @@ -219,56 +325,56 @@ public class ProtobufArrayListTest extends TestCase { } catch (UnsupportedOperationException e) { // expected } - + try { list.remove(new Object()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.removeAll(Collections.emptyList()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.removeAll(Collections.singleton(1)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.removeAll(UNARY_LIST); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.retainAll(Collections.emptyList()); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.retainAll(Collections.singleton(1)); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.retainAll(UNARY_LIST); fail(); } catch (UnsupportedOperationException e) { // expected } - + try { list.set(0, 0); fail(); @@ -276,7 +382,7 @@ public class ProtobufArrayListTest extends TestCase { // expected } } - + private static ProtobufArrayList<Integer> newImmutableProtoArrayList(int... elements) { ProtobufArrayList<Integer> list = new ProtobufArrayList<Integer>(); for (int element : elements) { 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 b4bc3a3d..8ec22d3f 100644 --- a/java/core/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/TestUtil.java @@ -130,8 +130,6 @@ import static protobuf_unittest.UnittestProto.defaultFixed64Extension; import static protobuf_unittest.UnittestProto.defaultFloatExtension; import static protobuf_unittest.UnittestProto.defaultForeignEnumExtension; import static protobuf_unittest.UnittestProto.defaultImportEnumExtension; -// The static imports are to avoid 100+ char lines. The following is roughly equivalent to -// import static protobuf_unittest.UnittestProto.*; import static protobuf_unittest.UnittestProto.defaultInt32Extension; import static protobuf_unittest.UnittestProto.defaultInt64Extension; import static protobuf_unittest.UnittestProto.defaultNestedEnumExtension; @@ -263,12 +261,14 @@ public final class TestUtil { return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); } + // BEGIN FULL-RUNTIME /** * Dirties the message by resetting the momoized serialized size. */ public static void resetMemoizedSize(AbstractMessage message) { message.memoizedSize = -1; } + // END FULL-RUNTIME /** * Get a {@code TestAllTypes} with all fields set as they would be by @@ -1201,17 +1201,29 @@ public final class TestUtil { * Get an unmodifiable {@link ExtensionRegistry} containing all the * extensions of {@code TestAllExtensions}. */ - public static ExtensionRegistry getExtensionRegistry() { + public static ExtensionRegistryLite getExtensionRegistry() { + ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); + registerAllExtensions(registry); + return registry.getUnmodifiable(); + } + + // BEGIN FULL-RUNTIME + /** + * Get an unmodifiable {@link ExtensionRegistry} containing all the + * extensions of {@code TestAllExtensions}. + */ + public static ExtensionRegistry getFullExtensionRegistry() { ExtensionRegistry registry = ExtensionRegistry.newInstance(); registerAllExtensions(registry); return registry.getUnmodifiable(); } + // END FULL-RUNTIME /** * Register all of {@code TestAllExtensions}'s extensions with the * given {@link ExtensionRegistry}. */ - public static void registerAllExtensions(ExtensionRegistry registry) { + public static void registerAllExtensions(ExtensionRegistryLite registry) { UnittestProto.registerAllExtensions(registry); TestUtilLite.registerAllExtensionsLite(registry); } @@ -2634,6 +2646,7 @@ public final class TestUtil { } // ================================================================= + // BEGIN FULL-RUNTIME /** * Performs the same things that the methods of {@code TestUtil} do, but @@ -3819,6 +3832,16 @@ public final class TestUtil { "Couldn't read file: " + fullPath.getPath(), e); } } + // END FULL-RUNTIME + + private static ByteString readBytesFromResource(String name) { + try { + return ByteString.copyFrom( + com.google.common.io.ByteStreams.toByteArray(TestUtil.class.getResourceAsStream(name))); + } catch (IOException e) { + throw new RuntimeException(e); + } + } /** * Get the bytes of the "golden message". This is a serialized TestAllTypes @@ -3829,7 +3852,7 @@ public final class TestUtil { */ public static ByteString getGoldenMessage() { if (goldenMessage == null) { - goldenMessage = readBytesFromFile("golden_message_oneof_implemented"); + goldenMessage = readBytesFromResource("/google/protobuf/testdata/golden_message_oneof_implemented"); } return goldenMessage; } @@ -3846,12 +3869,13 @@ public final class TestUtil { public static ByteString getGoldenPackedFieldsMessage() { if (goldenPackedFieldsMessage == null) { goldenPackedFieldsMessage = - readBytesFromFile("golden_packed_fields_message"); + readBytesFromResource("/google/protobuf/testdata/golden_packed_fields_message"); } return goldenPackedFieldsMessage; } private static ByteString goldenPackedFieldsMessage = null; + // BEGIN FULL-RUNTIME /** * Mock implementation of {@link GeneratedMessage.BuilderParent} for testing. * @@ -3871,4 +3895,5 @@ public final class TestUtil { return invalidations; } } + // END FULL-RUNTIME } 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 720061d2..24d5589f 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -382,17 +382,14 @@ public class TextFormatTest extends TestCase { public void testMergeExtensions() throws Exception { TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); - TextFormat.merge(allExtensionsSetText, - TestUtil.getExtensionRegistry(), - builder); + TextFormat.merge(allExtensionsSetText, TestUtil.getFullExtensionRegistry(), builder); TestUtil.assertAllExtensionsSet(builder.build()); } public void testParseExtensions() throws Exception { TestUtil.assertAllExtensionsSet( - TextFormat.parse(allExtensionsSetText, - TestUtil.getExtensionRegistry(), - TestAllExtensions.class)); + TextFormat.parse( + allExtensionsSetText, TestUtil.getFullExtensionRegistry(), TestAllExtensions.class)); } public void testMergeAndParseCompatibility() throws Exception { @@ -523,7 +520,7 @@ public class TextFormatTest extends TestCase { // Test merge(). TestAllTypes.Builder builder = TestAllTypes.newBuilder(); try { - TextFormat.merge(text, TestUtil.getExtensionRegistry(), builder); + TextFormat.merge(text, TestUtil.getFullExtensionRegistry(), builder); fail("Expected parse exception."); } catch (TextFormat.ParseException e) { assertEquals(error, e.getMessage()); @@ -531,8 +528,7 @@ public class TextFormatTest extends TestCase { // Test parse(). try { - TextFormat.parse( - text, TestUtil.getExtensionRegistry(), TestAllTypes.class); + TextFormat.parse(text, TestUtil.getFullExtensionRegistry(), TestAllTypes.class); fail("Expected parse exception."); } catch (TextFormat.ParseException e) { assertEquals(error, e.getMessage()); @@ -544,8 +540,7 @@ public class TextFormatTest extends TestCase { String text) { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); try { - parserWithOverwriteForbidden.merge( - text, TestUtil.getExtensionRegistry(), builder); + parserWithOverwriteForbidden.merge(text, TestUtil.getFullExtensionRegistry(), builder); fail("Expected parse exception."); } catch (TextFormat.ParseException e) { assertEquals(error, e.getMessage()); @@ -555,8 +550,7 @@ public class TextFormatTest extends TestCase { private TestAllTypes assertParseSuccessWithOverwriteForbidden( String text) throws TextFormat.ParseException { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - parserWithOverwriteForbidden.merge( - text, TestUtil.getExtensionRegistry(), builder); + parserWithOverwriteForbidden.merge(text, TestUtil.getFullExtensionRegistry(), builder); return builder.build(); } @@ -1118,8 +1112,7 @@ public class TextFormatTest extends TestCase { String input = "foo_string: \"stringvalue\" foo_int: 123"; TestOneof2.Builder builder = TestOneof2.newBuilder(); try { - parserWithOverwriteForbidden.merge( - input, TestUtil.getExtensionRegistry(), builder); + parserWithOverwriteForbidden.merge(input, TestUtil.getFullExtensionRegistry(), builder); fail("Expected parse exception."); } catch (TextFormat.ParseException e) { assertEquals("1:36: Field \"protobuf_unittest.TestOneof2.foo_int\"" @@ -1131,7 +1124,7 @@ public class TextFormatTest extends TestCase { public void testOneofOverwriteAllowed() throws Exception { String input = "foo_string: \"stringvalue\" foo_int: 123"; TestOneof2.Builder builder = TestOneof2.newBuilder(); - defaultParser.merge(input, TestUtil.getExtensionRegistry(), builder); + defaultParser.merge(input, TestUtil.getFullExtensionRegistry(), builder); // Only the last value sticks. TestOneof2 oneof = builder.build(); assertFalse(oneof.hasFooString()); 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 03c33ecf..425b56da 100644 --- a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java @@ -132,7 +132,7 @@ public class WireFormatTest extends TestCase { TestAllTypes message = TestUtil.getAllSet(); ByteString rawBytes = message.toByteString(); - ExtensionRegistry registry = TestUtil.getExtensionRegistry(); + ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); TestAllExtensions message2 = TestAllExtensions.parseFrom(rawBytes, registry); @@ -145,7 +145,7 @@ public class WireFormatTest extends TestCase { TestPackedExtensions message = TestUtil.getPackedExtensionsSet(); ByteString rawBytes = message.toByteString(); - ExtensionRegistry registry = TestUtil.getExtensionRegistry(); + ExtensionRegistryLite registry = TestUtil.getExtensionRegistry(); TestPackedExtensions message2 = TestPackedExtensions.parseFrom(rawBytes, registry); diff --git a/java/core/src/test/proto/com/google/protobuf/lazy_fields_lite.proto b/java/core/src/test/proto/com/google/protobuf/lazy_fields_lite.proto index 5580f72d..736a344f 100644 --- a/java/core/src/test/proto/com/google/protobuf/lazy_fields_lite.proto +++ b/java/core/src/test/proto/com/google/protobuf/lazy_fields_lite.proto @@ -36,8 +36,6 @@ syntax = "proto2"; package protobuf_unittest; -option optimize_for = LITE_RUNTIME; - message LazyMessageLite { optional int32 num = 1; optional int32 num_with_default = 2 [default = 421]; diff --git a/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto b/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto index b18b0d79..20fa03b9 100644 --- a/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto +++ b/java/core/src/test/proto/com/google/protobuf/lite_equals_and_hash.proto @@ -34,7 +34,6 @@ syntax = "proto2"; package protobuf_unittest.lite_equals_and_hash; -option optimize_for = LITE_RUNTIME; message TestOneofEquals { oneof oneof_field { 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 index c04f5d57..bc2105e5 100644 --- 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 @@ -30,10 +30,9 @@ syntax = "proto3"; -package map_lite_test; +package map_test; -option optimize_for = LITE_RUNTIME; -option java_package = "map_lite_test"; +option java_package = "map_test"; option java_outer_classname = "MapTestProto"; message TestMap { diff --git a/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto b/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto index a95c38b2..6793d6b7 100644 --- a/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto +++ b/java/core/src/test/proto/com/google/protobuf/nested_extension_lite.proto @@ -38,7 +38,6 @@ syntax = "proto2"; package protobuf_unittest; -option optimize_for = LITE_RUNTIME; import "com/google/protobuf/non_nested_extension_lite.proto"; diff --git a/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto b/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto index 37c369ed..e42faaa8 100644 --- a/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto +++ b/java/core/src/test/proto/com/google/protobuf/non_nested_extension_lite.proto @@ -36,7 +36,6 @@ syntax = "proto2"; package protobuf_unittest; -option optimize_for = LITE_RUNTIME; message MessageLiteToBeExtended { extensions 1 to max; |