public void testParseExtensionsLite() throws Exception { // TestAllTypes and TestAllExtensions should have compatible wire formats, // so if we serialize a TestAllTypes then parse it as TestAllExtensions // it should work. TestAllTypes message = TestUtil.getAllSet(); ByteString rawBytes = message.toByteString(); ExtensionRegistryLite registry_lite = TestUtil.getExtensionRegistryLite(); TestAllExtensionsLite message2 = TestAllExtensionsLite.parseFrom(rawBytes, registry_lite); TestUtil.assertAllExtensionsSet(message2); // Try again using a full extension registry. ExtensionRegistry registry = TestUtil.getExtensionRegistry(); TestAllExtensionsLite message3 = TestAllExtensionsLite.parseFrom(rawBytes, registry); TestUtil.assertAllExtensionsSet(message3); }
public void testMemoization() throws Exception { TestAllExtensionsLite message = TestUtilLite.getAllLiteExtensionsSet(); // Test serialized size is memoized message.memoizedSerializedSize = -1; int size = message.getSerializedSize(); assertTrue(size > 0); assertEquals(size, message.memoizedSerializedSize); // Test hashCode is memoized assertEquals(0, message.memoizedHashCode); int hashCode = message.hashCode(); assertTrue(hashCode != 0); assertEquals(hashCode, message.memoizedHashCode); // Test isInitialized is memoized Field memo = message.getClass().getDeclaredField("memoizedIsInitialized"); memo.setAccessible(true); memo.set(message, (byte) -1); boolean initialized = message.isInitialized(); assertTrue(initialized); // We have to cast to Byte first. Casting to byte causes a type error assertEquals(1, ((Byte) memo.get(message)).intValue()); }
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()); assertToStringEquals( "1: 123\n18: \"\\b\\a\"\n21: 3\n44: \"spam\"\n44: \"eggs\"", messageWithUnknownFields); }
public void testParseFromByteBuffer_extensions() throws Exception { 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(); ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance(); UnittestLite.registerAllExtensions(registry); TestAllExtensionsLite copy = TestAllExtensionsLite.parseFrom(message.toByteString().asReadOnlyByteBuffer(), registry); assertEquals(message, copy); }
public void testParseExtensionsLite() throws Exception { // TestAllTypes and TestAllExtensions should have compatible wire formats, // so if we serialize a TestAllTypes then parse it as TestAllExtensions // it should work. TestAllTypes message = TestUtil.getAllSet(); ByteString rawBytes = message.toByteString(); ExtensionRegistryLite registry_lite = TestUtilLite.getExtensionRegistryLite(); TestAllExtensionsLite message2 = TestAllExtensionsLite.parseFrom(rawBytes, registry_lite); TestUtil.assertAllExtensionsSet(message2); // Try again using a full extension registry. ExtensionRegistry registry = TestUtil.getExtensionRegistry(); TestAllExtensionsLite message3 = TestAllExtensionsLite.parseFrom(rawBytes, registry); TestUtil.assertAllExtensionsSet(message3); }
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(); // 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()); }
public void testSerializeExtensionsLite() throws Exception { // TestAllTypes and TestAllExtensions should have compatible wire formats, // so if we serialize a TestAllExtensions then parse it as TestAllTypes // it should work. TestAllExtensionsLite message = TestUtil.getAllLiteExtensionsSet(); ByteString rawBytes = message.toByteString(); assertEquals(rawBytes.size(), message.getSerializedSize()); TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); TestUtil.assertAllFieldsSet(message2); }
/** * Modify the repeated extensions of {@code message} to contain the values * expected by {@code assertRepeatedExtensionsModified()}. */ public static void modifyRepeatedExtensions( TestAllExtensionsLite.Builder message) { message.setExtension(repeatedInt32ExtensionLite , 1, 501); message.setExtension(repeatedInt64ExtensionLite , 1, 502L); message.setExtension(repeatedUint32ExtensionLite , 1, 503); message.setExtension(repeatedUint64ExtensionLite , 1, 504L); message.setExtension(repeatedSint32ExtensionLite , 1, 505); message.setExtension(repeatedSint64ExtensionLite , 1, 506L); message.setExtension(repeatedFixed32ExtensionLite , 1, 507); message.setExtension(repeatedFixed64ExtensionLite , 1, 508L); message.setExtension(repeatedSfixed32ExtensionLite, 1, 509); message.setExtension(repeatedSfixed64ExtensionLite, 1, 510L); message.setExtension(repeatedFloatExtensionLite , 1, 511F); message.setExtension(repeatedDoubleExtensionLite , 1, 512D); message.setExtension(repeatedBoolExtensionLite , 1, true); message.setExtension(repeatedStringExtensionLite , 1, "515"); message.setExtension(repeatedBytesExtensionLite , 1, toBytes("516")); message.setExtension(repeatedGroupExtensionLite, 1, RepeatedGroup_extension_lite.newBuilder().setA(517).build()); message.setExtension(repeatedNestedMessageExtensionLite, 1, TestAllTypesLite.NestedMessage.newBuilder().setBb(518).build()); message.setExtension(repeatedForeignMessageExtensionLite, 1, ForeignMessageLite.newBuilder().setC(519).build()); message.setExtension(repeatedImportMessageExtensionLite, 1, ImportMessageLite.newBuilder().setD(520).build()); message.setExtension(repeatedNestedEnumExtensionLite , 1, TestAllTypesLite.NestedEnum.FOO); message.setExtension(repeatedForeignEnumExtensionLite, 1, ForeignEnumLite.FOREIGN_LITE_FOO); message.setExtension(repeatedImportEnumExtensionLite , 1, ImportEnumLite.IMPORT_LITE_FOO); message.setExtension(repeatedStringPieceExtensionLite, 1, "524"); message.setExtension(repeatedCordExtensionLite, 1, "525"); }
public void testLiteExtensionRepeatedSetters() throws Exception { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); TestUtil.setAllExtensions(builder); TestUtil.modifyRepeatedExtensions(builder); TestAllExtensionsLite message = builder.build(); TestUtil.assertRepeatedExtensionsModified(message); }
public void testClearLiteExtension() throws Exception { // clearExtension() is not actually used in TestUtil, so try it manually. assertFalse( TestAllExtensionsLite.newBuilder() .setExtension(UnittestLite.optionalInt32ExtensionLite, 1) .clearExtension(UnittestLite.optionalInt32ExtensionLite) .hasExtension(UnittestLite.optionalInt32ExtensionLite)); assertEquals(0, TestAllExtensionsLite.newBuilder() .addExtension(UnittestLite.repeatedInt32ExtensionLite, 1) .clearExtension(UnittestLite.repeatedInt32ExtensionLite) .getExtensionCount(UnittestLite.repeatedInt32ExtensionLite)); }
public void testLiteExtensionMergeFrom() throws Exception { TestAllExtensionsLite original = TestAllExtensionsLite.newBuilder() .setExtension(UnittestLite.optionalInt32ExtensionLite, 1).build(); TestAllExtensionsLite merged = TestAllExtensionsLite.newBuilder().mergeFrom(original).build(); assertTrue(merged.hasExtension(UnittestLite.optionalInt32ExtensionLite)); assertEquals( 1, (int) merged.getExtension(UnittestLite.optionalInt32ExtensionLite)); }
/** * Modify the repeated extensions of {@code message} to contain the values * expected by {@code assertRepeatedExtensionsModified()}. */ public static void modifyRepeatedExtensions( TestAllExtensionsLite.Builder message) { message.setExtension(repeatedInt32ExtensionLite , 1, 501); message.setExtension(repeatedInt64ExtensionLite , 1, 502L); message.setExtension(repeatedUint32ExtensionLite , 1, 503); message.setExtension(repeatedUint64ExtensionLite , 1, 504L); message.setExtension(repeatedSint32ExtensionLite , 1, 505); message.setExtension(repeatedSint64ExtensionLite , 1, 506L); message.setExtension(repeatedFixed32ExtensionLite , 1, 507); message.setExtension(repeatedFixed64ExtensionLite , 1, 508L); message.setExtension(repeatedSfixed32ExtensionLite, 1, 509); message.setExtension(repeatedSfixed64ExtensionLite, 1, 510L); message.setExtension(repeatedFloatExtensionLite , 1, 511F); message.setExtension(repeatedDoubleExtensionLite , 1, 512D); message.setExtension(repeatedBoolExtensionLite , 1, true); message.setExtension(repeatedStringExtensionLite , 1, "515"); message.setExtension(repeatedBytesExtensionLite , 1, toBytes("516")); message.setExtension(repeatedGroupExtensionLite, 1, RepeatedGroup_extension_lite.newBuilder().setA(517).build()); message.setExtension(repeatedNestedMessageExtensionLite, 1, TestAllTypesLite.NestedMessage.newBuilder().setBb(518).build()); message.setExtension(repeatedForeignMessageExtensionLite, 1, ForeignMessageLite.newBuilder().setC(519).build()); message.setExtension(repeatedImportMessageExtensionLite, 1, ImportMessageLite.newBuilder().setD(520).build()); message.setExtension(repeatedLazyMessageExtensionLite, 1, TestAllTypesLite.NestedMessage.newBuilder().setBb(527).build()); message.setExtension(repeatedNestedEnumExtensionLite , 1, TestAllTypesLite.NestedEnum.FOO); message.setExtension(repeatedForeignEnumExtensionLite, 1, ForeignEnumLite.FOREIGN_LITE_FOO); message.setExtension(repeatedImportEnumExtensionLite , 1, ImportEnumLite.IMPORT_LITE_FOO); message.setExtension(repeatedStringPieceExtensionLite, 1, "524"); message.setExtension(repeatedCordExtensionLite, 1, "525"); }
public void testLiteExtensionMessageOrBuilder() throws Exception { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); TestUtil.setAllExtensions(builder); TestUtil.assertAllExtensionsSet(builder); TestAllExtensionsLite message = builder.build(); TestUtil.assertAllExtensionsSet(message); }
public void testLiteExtensionRepeatedSetters() throws Exception { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); TestUtil.setAllExtensions(builder); TestUtil.modifyRepeatedExtensions(builder); TestUtil.assertRepeatedExtensionsModified(builder); TestAllExtensionsLite message = builder.build(); TestUtil.assertRepeatedExtensionsModified(message); }
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); assertEquals( expected2.getExtension(UnittestLite.optionalInt32ExtensionLite), expected2.clone().getExtension(UnittestLite.optionalInt32ExtensionLite)); }
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(); assertToStringEquals( "[1]: 123\n[18] {\n bb: 7\n}\n[21]: 3\n[44]: \"spam\"\n[44]: \"eggs\"", message); }
public void testSerializeExtensionsLite() throws Exception { // TestAllTypes and TestAllExtensions should have compatible wire formats, // so if we serialize a TestAllExtensions then parse it as TestAllTypes // it should work. TestAllExtensionsLite message = TestUtilLite.getAllLiteExtensionsSet(); ByteString rawBytes = message.toByteString(); assertEquals(rawBytes.size(), message.getSerializedSize()); TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); TestUtil.assertAllFieldsSet(message2); }
public void testLiteExtensionMessageOrBuilder() throws Exception { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); TestUtilLite.setAllExtensions(builder); TestUtil.assertAllExtensionsSet(builder); TestAllExtensionsLite message = builder.build(); TestUtil.assertAllExtensionsSet(message); }
public void testLiteExtensionRepeatedSetters() throws Exception { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); TestUtilLite.setAllExtensions(builder); TestUtilLite.modifyRepeatedExtensions(builder); TestUtil.assertRepeatedExtensionsModified(builder); TestAllExtensionsLite message = builder.build(); TestUtil.assertRepeatedExtensionsModified(message); }
public static TestAllExtensionsLite getAllLiteExtensionsSet() { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); setAllExtensions(builder); return builder.build(); }
public void testLiteExtensionAccessors() throws Exception { TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); TestUtil.setAllExtensions(builder); TestAllExtensionsLite message = builder.build(); TestUtil.assertAllExtensionsSet(message); }
public void testLiteExtensionDefaults() throws Exception { TestUtil.assertExtensionsClear(TestAllExtensionsLite.getDefaultInstance()); TestUtil.assertExtensionsClear(TestAllExtensionsLite.newBuilder().build()); }
public void testLiteExtensionCopy() throws Exception { TestAllExtensionsLite original = TestUtil.getAllLiteExtensionsSet(); TestAllExtensionsLite copy = TestAllExtensionsLite.newBuilder(original).build(); TestUtil.assertAllExtensionsSet(copy); }