public void testLite() throws Exception { // Since lite messages are a subset of regular messages, we can mostly // assume that the functionality of lite messages is already thoroughly // tested by the regular tests. All this test really verifies is that // a proto with optimize_for = LITE_RUNTIME compiles correctly when // linked only against the lite library. That is all tested at compile // time, leaving not much to do in this method. Let's just do some random // 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(); ByteString data = message.toByteString(); TestAllTypesLite message2 = TestAllTypesLite.parseFrom(data); assertEquals(123, message2.getOptionalInt32()); assertEquals(1, message2.getRepeatedStringCount()); assertEquals("hello", message2.getRepeatedString(0)); assertEquals(7, message2.getOptionalNestedMessage().getBb()); }
public void testSerialize() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); TestAllTypesLite expected = TestAllTypesLite.newBuilder() .setOptionalInt32(123) .addRepeatedString("hello") .setOptionalNestedMessage( TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) .build(); ObjectOutputStream out = new ObjectOutputStream(baos); try { out.writeObject(expected); } finally { out.close(); } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); TestAllTypesLite actual = (TestAllTypesLite) in.readObject(); assertEquals(expected.getOptionalInt32(), actual.getOptionalInt32()); assertEquals(expected.getRepeatedStringCount(), actual.getRepeatedStringCount()); assertEquals(expected.getRepeatedString(0), actual.getRepeatedString(0)); assertEquals(expected.getOptionalNestedMessage().getBb(), actual.getOptionalNestedMessage().getBb()); }
public void testToStringPrimitives() throws Exception { 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(); assertToStringEquals("optional_bool: true\noptional_nested_enum: BAZ", proto); proto = TestAllTypesLite.newBuilder() .setOptionalFloat(2.72f) .setOptionalDouble(3.14) .build(); assertToStringEquals("optional_double: 3.14\noptional_float: 2.72", proto); }
public void testToStringRepeatedFields() throws Exception { 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(); assertToStringEquals( "repeated_lazy_message {\n bb: 7\n}\nrepeated_lazy_message {\n bb: 8\n}", proto); }
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 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 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 concat = bb.concat(cc); TestAllTypesLite message = TestAllTypesLite.parseFrom(concat); assertEquals(11, message.getOneofLazyNestedMessage().getBb()); assertEquals(22L, message.getOneofLazyNestedMessage().getCc()); }
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 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(); }
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()); }
/** * 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"); }
/** * 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"); }
/** Helper method for {@link #testParsingMergeLite()}.*/ private void assertMessageMerged(TestAllTypesLite allTypes) throws Exception { assertEquals(3, allTypes.getOptionalInt32()); assertEquals(2, allTypes.getOptionalInt64()); assertEquals("hello", allTypes.getOptionalString()); }
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 testAddAll() { try { TestAllTypesLite.newBuilder() .addAllRepeatedBytes(null); fail(); } catch (NullPointerException e) { // expected. } }
public void testToStringStringFields() throws Exception { TestAllTypesLite proto = TestAllTypesLite.newBuilder() .setOptionalString("foo\"bar\nbaz\\") .build(); assertToStringEquals("optional_string: \"foo\\\"bar\\nbaz\\\\\"", proto); 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(); assertToStringEquals("optional_nested_message {\n}", proto); proto = TestAllTypesLite.newBuilder() .setOptionalNestedMessage( TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) .build(); assertToStringEquals("optional_nested_message {\n bb: 7\n}", proto); }
public void testToStringForeignFields() throws Exception { 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); }
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 testMergeFromStream_repeatedField() throws Exception { 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); try { builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(Internal.UTF_8))); fail(); } 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); assertEquals(two, one); assertEquals(one.hashCode(), two.hashCode()); }
public void testMergeFromNoLazyFieldSharing() throws Exception { 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)); assertEquals(1, targetBuilder.getOptionalLazyMessage().getBb()); }
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 testEquals_oneOfMessages() { TestAllTypesLite mine = TestAllTypesLite.newBuilder() .setOneofString("Hello") .build(); TestAllTypesLite other = TestAllTypesLite.newBuilder() .setOneofNestedMessage(NestedMessage.getDefaultInstance()) .build(); assertFalse(mine.equals(other)); assertFalse(other.equals(mine)); }
public void testParseFromByteBuffer() throws Exception { TestAllTypesLite message = TestAllTypesLite.newBuilder() .setOptionalInt32(123) .addRepeatedString("hello") .setOptionalNestedMessage(TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) .build(); TestAllTypesLite copy = TestAllTypesLite.parseFrom(message.toByteString().asReadOnlyByteBuffer()); assertEquals(message, copy); }
public void testForNumberLite_oneof() { TestAllTypesLite.OneofFieldCase e = TestAllTypesLite.OneofFieldCase.forNumber( TestAllTypesLite.OneofFieldCase.ONEOF_NESTED_MESSAGE.getNumber()); assertEquals(TestAllTypesLite.OneofFieldCase.ONEOF_NESTED_MESSAGE, e); e = TestAllTypesLite.OneofFieldCase.forNumber(1000); assertEquals(null, e); }
private static void assertEqualsExactType(TestAllTypesLite.NestedEnum a, TestAllTypesLite.NestedEnum b) { Assert.assertEquals(a, b); }