This source file includes following definitions.
- testMessagesAndBuilders
- testMessagesAreCached
- testRemove_WithNestedBuilders
- testRemove_WithNestedMessages
- testMerge
- testGettingBuilderMarksFieldAsHaving
package com.google.protobuf;
import protobuf_unittest.Vehicle;
import protobuf_unittest.Wheel;
import junit.framework.TestCase;
import java.util.List;
import java.util.ArrayList;
public class NestedBuildersTest extends TestCase {
public void testMessagesAndBuilders() {
Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
vehicleBuilder.addWheelBuilder()
.setRadius(4)
.setWidth(1);
vehicleBuilder.addWheelBuilder()
.setRadius(4)
.setWidth(2);
vehicleBuilder.addWheelBuilder()
.setRadius(4)
.setWidth(3);
vehicleBuilder.addWheelBuilder()
.setRadius(4)
.setWidth(4);
vehicleBuilder.getEngineBuilder()
.setLiters(10);
Vehicle vehicle = vehicleBuilder.build();
assertEquals(4, vehicle.getWheelCount());
for (int i = 0; i < 4; i++) {
Wheel wheel = vehicle.getWheel(i);
assertEquals(4, wheel.getRadius());
assertEquals(i + 1, wheel.getWidth());
}
assertEquals(10, vehicle.getEngine().getLiters());
for (int i = 0; i < 4; i++) {
vehicleBuilder.getWheelBuilder(i)
.setRadius(5)
.setWidth(i + 10);
}
vehicleBuilder.getEngineBuilder().setLiters(20);
vehicle = vehicleBuilder.build();
for (int i = 0; i < 4; i++) {
Wheel wheel = vehicle.getWheel(i);
assertEquals(5, wheel.getRadius());
assertEquals(i + 10, wheel.getWidth());
}
assertEquals(20, vehicle.getEngine().getLiters());
assertTrue(vehicle.hasEngine());
}
public void testMessagesAreCached() {
Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
vehicleBuilder.addWheelBuilder()
.setRadius(1)
.setWidth(2);
vehicleBuilder.addWheelBuilder()
.setRadius(3)
.setWidth(4);
vehicleBuilder.addWheelBuilder()
.setRadius(5)
.setWidth(6);
vehicleBuilder.addWheelBuilder()
.setRadius(7)
.setWidth(8);
List<Wheel> wheels = new ArrayList<Wheel>(vehicleBuilder.getWheelList());
for (int i = 0; i < wheels.size(); i++) {
assertSame(wheels.get(i), vehicleBuilder.getWheel(i));
}
for (int i = 0; i < wheels.size(); i++) {
vehicleBuilder.getWheel(i);
}
for (int i = 0; i < wheels.size(); i++) {
assertSame(wheels.get(i), vehicleBuilder.getWheel(i));
}
vehicleBuilder.getWheelBuilder(3)
.setRadius(20).setWidth(20);
for (int i = 0; i < wheels.size(); i++) {
if (i < 3) {
assertSame(wheels.get(i), vehicleBuilder.getWheel(i));
} else {
assertNotSame(wheels.get(i), vehicleBuilder.getWheel(i));
}
}
}
public void testRemove_WithNestedBuilders() {
Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
vehicleBuilder.addWheelBuilder()
.setRadius(1)
.setWidth(1);
vehicleBuilder.addWheelBuilder()
.setRadius(2)
.setWidth(2);
vehicleBuilder.removeWheel(0);
assertEquals(1, vehicleBuilder.getWheelCount());
assertEquals(2, vehicleBuilder.getWheel(0).getRadius());
}
public void testRemove_WithNestedMessages() {
Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
vehicleBuilder.addWheel(Wheel.newBuilder()
.setRadius(1)
.setWidth(1));
vehicleBuilder.addWheel(Wheel.newBuilder()
.setRadius(2)
.setWidth(2));
vehicleBuilder.removeWheel(0);
assertEquals(1, vehicleBuilder.getWheelCount());
assertEquals(2, vehicleBuilder.getWheel(0).getRadius());
}
public void testMerge() {
Vehicle vehicle1 = Vehicle.newBuilder()
.addWheel(Wheel.newBuilder().setRadius(1).build())
.addWheel(Wheel.newBuilder().setRadius(2).build())
.build();
Vehicle vehicle2 = Vehicle.newBuilder()
.mergeFrom(vehicle1)
.build();
assertSame(vehicle1.getWheelList(), vehicle2.getWheelList());
Vehicle vehicle3 = vehicle1.toBuilder().build();
assertSame(vehicle1.getWheelList(), vehicle3.getWheelList());
}
public void testGettingBuilderMarksFieldAsHaving() {
Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
vehicleBuilder.getEngineBuilder();
Vehicle vehicle = vehicleBuilder.buildPartial();
assertTrue(vehicle.hasEngine());
}
}