This source file includes following definitions.
- setUp
- testLite
- testLiteExtensions
- testSerialize
package com.google.protobuf;
import com.google.protobuf.UnittestLite;
import com.google.protobuf.UnittestLite.TestAllTypesLite;
import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
import com.google.protobuf.UnittestLite.TestNestedExtensionLite;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class LiteTest extends TestCase {
public void setUp() throws Exception {
assertTrue(TestNestedExtensionLite.nestedExtension != null);
}
public void testLite() throws Exception {
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 testLiteExtensions() 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();
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 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());
}
}