PageRenderTime 50ms CodeModel.GetById 11ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/test/java/com/google/protobuf/NestedBuildersTest.java

http://github.com/tomahawk-player/tomahawk
Java | 185 lines | 123 code | 22 blank | 40 comment | 9 complexity | 808f7ee38a834c5f2f05ccbb890226ed MD5 | raw file
  1// Protocol Buffers - Google's data interchange format
  2// Copyright 2008 Google Inc.  All rights reserved.
  3// http://code.google.com/p/protobuf/
  4//
  5// Redistribution and use in source and binary forms, with or without
  6// modification, are permitted provided that the following conditions are
  7// met:
  8//
  9//     * Redistributions of source code must retain the above copyright
 10// notice, this list of conditions and the following disclaimer.
 11//     * Redistributions in binary form must reproduce the above
 12// copyright notice, this list of conditions and the following disclaimer
 13// in the documentation and/or other materials provided with the
 14// distribution.
 15//     * Neither the name of Google Inc. nor the names of its
 16// contributors may be used to endorse or promote products derived from
 17// this software without specific prior written permission.
 18//
 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30
 31package com.google.protobuf;
 32
 33import protobuf_unittest.Vehicle;
 34import protobuf_unittest.Wheel;
 35
 36import junit.framework.TestCase;
 37
 38import java.util.List;
 39import java.util.ArrayList;
 40
 41/**
 42 * Test cases that exercise end-to-end use cases involving
 43 * {@link SingleFieldBuilder} and {@link RepeatedFieldBuilder}.
 44 *
 45 * @author jonp@google.com (Jon Perlow)
 46 */
 47public class NestedBuildersTest extends TestCase {
 48
 49  public void testMessagesAndBuilders() {
 50    Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
 51    vehicleBuilder.addWheelBuilder()
 52        .setRadius(4)
 53        .setWidth(1);
 54    vehicleBuilder.addWheelBuilder()
 55        .setRadius(4)
 56        .setWidth(2);
 57    vehicleBuilder.addWheelBuilder()
 58        .setRadius(4)
 59        .setWidth(3);
 60    vehicleBuilder.addWheelBuilder()
 61        .setRadius(4)
 62        .setWidth(4);
 63    vehicleBuilder.getEngineBuilder()
 64        .setLiters(10);
 65
 66    Vehicle vehicle = vehicleBuilder.build();
 67    assertEquals(4, vehicle.getWheelCount());
 68    for (int i = 0; i < 4; i++) {
 69      Wheel wheel = vehicle.getWheel(i);
 70      assertEquals(4, wheel.getRadius());
 71      assertEquals(i + 1, wheel.getWidth());
 72    }
 73    assertEquals(10, vehicle.getEngine().getLiters());
 74
 75    for (int i = 0; i < 4; i++) {
 76      vehicleBuilder.getWheelBuilder(i)
 77          .setRadius(5)
 78          .setWidth(i + 10);
 79    }
 80    vehicleBuilder.getEngineBuilder().setLiters(20);
 81
 82    vehicle = vehicleBuilder.build();
 83    for (int i = 0; i < 4; i++) {
 84      Wheel wheel = vehicle.getWheel(i);
 85      assertEquals(5, wheel.getRadius());
 86      assertEquals(i + 10, wheel.getWidth());
 87    }
 88    assertEquals(20, vehicle.getEngine().getLiters());
 89    assertTrue(vehicle.hasEngine());
 90  }
 91
 92  public void testMessagesAreCached() {
 93    Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
 94    vehicleBuilder.addWheelBuilder()
 95        .setRadius(1)
 96        .setWidth(2);
 97    vehicleBuilder.addWheelBuilder()
 98        .setRadius(3)
 99        .setWidth(4);
100    vehicleBuilder.addWheelBuilder()
101        .setRadius(5)
102        .setWidth(6);
103    vehicleBuilder.addWheelBuilder()
104        .setRadius(7)
105        .setWidth(8);
106
107    // Make sure messages are cached.
108    List<Wheel> wheels = new ArrayList<Wheel>(vehicleBuilder.getWheelList());
109    for (int i = 0; i < wheels.size(); i++) {
110      assertSame(wheels.get(i), vehicleBuilder.getWheel(i));
111    }
112
113    // Now get builders and check they didn't change.
114    for (int i = 0; i < wheels.size(); i++) {
115      vehicleBuilder.getWheel(i);
116    }
117    for (int i = 0; i < wheels.size(); i++) {
118      assertSame(wheels.get(i), vehicleBuilder.getWheel(i));
119    }
120
121    // Change just one
122    vehicleBuilder.getWheelBuilder(3)
123        .setRadius(20).setWidth(20);
124
125    // Now get wheels and check that only that one changed
126    for (int i = 0; i < wheels.size(); i++) {
127      if (i < 3) {
128        assertSame(wheels.get(i), vehicleBuilder.getWheel(i));
129      } else {
130        assertNotSame(wheels.get(i), vehicleBuilder.getWheel(i));
131      }
132    }
133  }
134
135  public void testRemove_WithNestedBuilders() {
136    Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
137    vehicleBuilder.addWheelBuilder()
138        .setRadius(1)
139        .setWidth(1);
140    vehicleBuilder.addWheelBuilder()
141        .setRadius(2)
142        .setWidth(2);
143    vehicleBuilder.removeWheel(0);
144
145    assertEquals(1, vehicleBuilder.getWheelCount());
146    assertEquals(2, vehicleBuilder.getWheel(0).getRadius());
147  }
148
149  public void testRemove_WithNestedMessages() {
150    Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
151    vehicleBuilder.addWheel(Wheel.newBuilder()
152        .setRadius(1)
153        .setWidth(1));
154    vehicleBuilder.addWheel(Wheel.newBuilder()
155        .setRadius(2)
156        .setWidth(2));
157    vehicleBuilder.removeWheel(0);
158
159    assertEquals(1, vehicleBuilder.getWheelCount());
160    assertEquals(2, vehicleBuilder.getWheel(0).getRadius());
161  }
162
163  public void testMerge() {
164    Vehicle vehicle1 = Vehicle.newBuilder()
165        .addWheel(Wheel.newBuilder().setRadius(1).build())
166        .addWheel(Wheel.newBuilder().setRadius(2).build())
167        .build();
168
169    Vehicle vehicle2 = Vehicle.newBuilder()
170        .mergeFrom(vehicle1)
171        .build();
172    // List should be the same -- no allocation
173    assertSame(vehicle1.getWheelList(), vehicle2.getWheelList());
174
175    Vehicle vehicle3 = vehicle1.toBuilder().build();
176    assertSame(vehicle1.getWheelList(), vehicle3.getWheelList());
177  }
178
179  public void testGettingBuilderMarksFieldAsHaving() {
180    Vehicle.Builder vehicleBuilder = Vehicle.newBuilder();
181    vehicleBuilder.getEngineBuilder();
182    Vehicle vehicle = vehicleBuilder.buildPartial();
183    assertTrue(vehicle.hasEngine());
184  }
185}