TestOneof2.Builder builder = TestOneof2.newBuilder();
assertEquals(builder.getFooInt(), 0);
assertFalse(builder.hasFooInt());
assertTrue(builder.setFooInt(123).hasFooInt());
assertEquals(builder.getFooInt(), 123);
TestOneof2 message = builder.buildPartial();
assertTrue(message.hasFooInt());
assertEquals(message.getFooInt(), 123);
assertFalse(builder.clearFooInt().hasFooInt());
TestOneof2 message2 = builder.build();
assertFalse(message2.hasFooInt());
assertEquals(message2.getFooInt(), 0);
}
// Enum
{
TestOneof2.Builder builder = TestOneof2.newBuilder();
assertEquals(builder.getFooEnum(), TestOneof2.NestedEnum.FOO);
assertTrue(builder.setFooEnum(TestOneof2.NestedEnum.BAR).hasFooEnum());
assertEquals(builder.getFooEnum(), TestOneof2.NestedEnum.BAR);
TestOneof2 message = builder.buildPartial();
assertTrue(message.hasFooEnum());
assertEquals(message.getFooEnum(), TestOneof2.NestedEnum.BAR);
assertFalse(builder.clearFooEnum().hasFooEnum());
TestOneof2 message2 = builder.build();
assertFalse(message2.hasFooEnum());
assertEquals(message2.getFooEnum(), TestOneof2.NestedEnum.FOO);
}
// String
{
TestOneof2.Builder builder = TestOneof2.newBuilder();
assertEquals(builder.getFooString(), "");
builder.setFooString("foo");
assertTrue(builder.hasFooString());
assertEquals(builder.getFooString(), "foo");
TestOneof2 message = builder.buildPartial();
assertTrue(message.hasFooString());
assertEquals(message.getFooString(), "foo");
assertEquals(message.getFooStringBytes(), TestUtil.toBytes("foo"));
assertFalse(builder.clearFooString().hasFooString());
TestOneof2 message2 = builder.buildPartial();
assertFalse(message2.hasFooString());
assertEquals(message2.getFooString(), "");
assertEquals(message2.getFooStringBytes(), TestUtil.toBytes(""));
// Get method should not change the oneof value.
builder.setFooInt(123);
assertEquals(builder.getFooString(), "");
assertEquals(builder.getFooStringBytes(), TestUtil.toBytes(""));
assertEquals(123, builder.getFooInt());
message = builder.build();
assertEquals(message.getFooString(), "");
assertEquals(message.getFooStringBytes(), TestUtil.toBytes(""));
assertEquals(123, message.getFooInt());
}
// Cord
{
TestOneof2.Builder builder = TestOneof2.newBuilder();
assertEquals(builder.getFooCord(), "");
builder.setFooCord("foo");
assertTrue(builder.hasFooCord());
assertEquals(builder.getFooCord(), "foo");
TestOneof2 message = builder.buildPartial();
assertTrue(message.hasFooCord());
assertEquals(message.getFooCord(), "foo");
assertEquals(message.getFooCordBytes(), TestUtil.toBytes("foo"));
assertFalse(builder.clearFooCord().hasFooCord());
TestOneof2 message2 = builder.build();
assertFalse(message2.hasFooCord());
assertEquals(message2.getFooCord(), "");
assertEquals(message2.getFooCordBytes(), TestUtil.toBytes(""));
}
// StringPiece
{
TestOneof2.Builder builder = TestOneof2.newBuilder();
assertEquals(builder.getFooStringPiece(), "");
builder.setFooStringPiece("foo");
assertTrue(builder.hasFooStringPiece());
assertEquals(builder.getFooStringPiece(), "foo");
TestOneof2 message = builder.buildPartial();
assertTrue(message.hasFooStringPiece());
assertEquals(message.getFooStringPiece(), "foo");
assertEquals(message.getFooStringPieceBytes(), TestUtil.toBytes("foo"));
assertFalse(builder.clearFooStringPiece().hasFooStringPiece());
TestOneof2 message2 = builder.build();
assertFalse(message2.hasFooStringPiece());
assertEquals(message2.getFooStringPiece(), "");
assertEquals(message2.getFooStringPieceBytes(), TestUtil.toBytes(""));
}
// Message
{
// set
TestOneof2.Builder builder = TestOneof2.newBuilder();
assertEquals(builder.getFooMessage().getQuxInt(), 0);
builder.setFooMessage(
TestOneof2.NestedMessage.newBuilder().setQuxInt(234).build());
assertTrue(builder.hasFooMessage());
assertEquals(builder.getFooMessage().getQuxInt(), 234);
TestOneof2 message = builder.buildPartial();
assertTrue(message.hasFooMessage());
assertEquals(message.getFooMessage().getQuxInt(), 234);
// clear
assertFalse(builder.clearFooMessage().hasFooString());
message = builder.build();
assertFalse(message.hasFooMessage());
assertEquals(message.getFooMessage().getQuxInt(), 0);
// nested builder
builder = TestOneof2.newBuilder();
assertSame(builder.getFooMessageOrBuilder(),
TestOneof2.NestedMessage.getDefaultInstance());
assertFalse(builder.hasFooMessage());
builder.getFooMessageBuilder().setQuxInt(123);
assertTrue(builder.hasFooMessage());
assertEquals(builder.getFooMessage().getQuxInt(), 123);
message = builder.build();
assertTrue(message.hasFooMessage());
assertEquals(message.getFooMessage().getQuxInt(), 123);
}
// LazyMessage is tested in LazyMessageLiteTest.java
}