| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 package com.google.protobuf; | 31 package com.google.protobuf; |
| 32 | 32 |
| 33 |
| 33 import com.google.protobuf.Descriptors.Descriptor; | 34 import com.google.protobuf.Descriptors.Descriptor; |
| 34 import com.google.protobuf.Descriptors.EnumDescriptor; | 35 import com.google.protobuf.Descriptors.EnumDescriptor; |
| 35 import com.google.protobuf.Descriptors.EnumValueDescriptor; | 36 import com.google.protobuf.Descriptors.EnumValueDescriptor; |
| 36 import com.google.protobuf.Descriptors.FieldDescriptor; | 37 import com.google.protobuf.Descriptors.FieldDescriptor; |
| 38 import map_test.MapTestProto.BizarroTestMap; |
| 39 import map_test.MapTestProto.ReservedAsMapField; |
| 40 import map_test.MapTestProto.ReservedAsMapFieldWithEnumValue; |
| 37 import map_test.MapTestProto.TestMap; | 41 import map_test.MapTestProto.TestMap; |
| 38 import map_test.MapTestProto.TestMap.MessageValue; | 42 import map_test.MapTestProto.TestMap.MessageValue; |
| 43 import map_test.MapTestProto.TestMapOrBuilder; |
| 39 import map_test.MapTestProto.TestOnChangeEventPropagation; | 44 import map_test.MapTestProto.TestOnChangeEventPropagation; |
| 40 import junit.framework.TestCase; | 45 import java.io.ByteArrayOutputStream; |
| 41 | 46 import java.io.IOException; |
| 42 import java.util.ArrayList; | 47 import java.util.ArrayList; |
| 43 import java.util.Arrays; | 48 import java.util.Arrays; |
| 44 import java.util.HashMap; | 49 import java.util.HashMap; |
| 45 import java.util.List; | 50 import java.util.List; |
| 46 import java.util.Map; | 51 import java.util.Map; |
| 52 import junit.framework.TestCase; |
| 47 | 53 |
| 48 /** | 54 /** |
| 49 * Unit tests for map fields. | 55 * Unit tests for map fields. |
| 50 */ | 56 */ |
| 51 public class MapTest extends TestCase { | 57 public class MapTest extends TestCase { |
| 52 private void setMapValues(TestMap.Builder builder) { | 58 |
| 59 private void setMapValuesUsingMutableMap(TestMap.Builder builder) { |
| 53 builder.getMutableInt32ToInt32Field().put(1, 11); | 60 builder.getMutableInt32ToInt32Field().put(1, 11); |
| 54 builder.getMutableInt32ToInt32Field().put(2, 22); | 61 builder.getMutableInt32ToInt32Field().put(2, 22); |
| 55 builder.getMutableInt32ToInt32Field().put(3, 33); | 62 builder.getMutableInt32ToInt32Field().put(3, 33); |
| 56 | 63 // |
| 57 builder.getMutableInt32ToStringField().put(1, "11"); | 64 builder.getMutableInt32ToStringField().put(1, "11"); |
| 58 builder.getMutableInt32ToStringField().put(2, "22"); | 65 builder.getMutableInt32ToStringField().put(2, "22"); |
| 59 builder.getMutableInt32ToStringField().put(3, "33"); | 66 builder.getMutableInt32ToStringField().put(3, "33"); |
| 60 | 67 // |
| 61 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); | 68 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); |
| 62 builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); | 69 builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); |
| 63 builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); | 70 builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); |
| 64 | 71 // |
| 65 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); | 72 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); |
| 66 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); | 73 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); |
| 67 builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); | 74 builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); |
| 68 | 75 // |
| 69 builder.getMutableInt32ToMessageField().put( | 76 builder.getMutableInt32ToMessageField().put( |
| 70 1, MessageValue.newBuilder().setValue(11).build()); | 77 1, MessageValue.newBuilder().setValue(11).build()); |
| 71 builder.getMutableInt32ToMessageField().put( | 78 builder.getMutableInt32ToMessageField().put( |
| 72 2, MessageValue.newBuilder().setValue(22).build()); | 79 2, MessageValue.newBuilder().setValue(22).build()); |
| 73 builder.getMutableInt32ToMessageField().put( | 80 builder.getMutableInt32ToMessageField().put( |
| 74 3, MessageValue.newBuilder().setValue(33).build()); | 81 3, MessageValue.newBuilder().setValue(33).build()); |
| 75 | 82 // |
| 76 builder.getMutableStringToInt32Field().put("1", 11); | 83 builder.getMutableStringToInt32Field().put("1", 11); |
| 77 builder.getMutableStringToInt32Field().put("2", 22); | 84 builder.getMutableStringToInt32Field().put("2", 22); |
| 78 builder.getMutableStringToInt32Field().put("3", 33); | 85 builder.getMutableStringToInt32Field().put("3", 33); |
| 79 } | 86 } |
| 80 | 87 |
| 88 private void setMapValuesUsingAccessors(TestMap.Builder builder) { |
| 89 builder |
| 90 .putInt32ToInt32Field(1, 11) |
| 91 .putInt32ToInt32Field(2, 22) |
| 92 .putInt32ToInt32Field(3, 33) |
| 93 |
| 94 .putInt32ToStringField(1, "11") |
| 95 .putInt32ToStringField(2, "22") |
| 96 .putInt32ToStringField(3, "33") |
| 97 |
| 98 .putInt32ToBytesField(1, TestUtil.toBytes("11")) |
| 99 .putInt32ToBytesField(2, TestUtil.toBytes("22")) |
| 100 .putInt32ToBytesField(3, TestUtil.toBytes("33")) |
| 101 |
| 102 .putInt32ToEnumField(1, TestMap.EnumValue.FOO) |
| 103 .putInt32ToEnumField(2, TestMap.EnumValue.BAR) |
| 104 .putInt32ToEnumField(3, TestMap.EnumValue.BAZ) |
| 105 |
| 106 .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(11).build(
)) |
| 107 .putInt32ToMessageField(2, MessageValue.newBuilder().setValue(22).build(
)) |
| 108 .putInt32ToMessageField(3, MessageValue.newBuilder().setValue(33).build(
)) |
| 109 |
| 110 .putStringToInt32Field("1", 11) |
| 111 .putStringToInt32Field("2", 22) |
| 112 .putStringToInt32Field("3", 33); |
| 113 } |
| 114 |
| 115 public void testSetMapValues() { |
| 116 TestMap.Builder usingMutableMapBuilder = TestMap.newBuilder(); |
| 117 setMapValuesUsingMutableMap(usingMutableMapBuilder); |
| 118 TestMap usingMutableMap = usingMutableMapBuilder.build(); |
| 119 assertMapValuesSet(usingMutableMap); |
| 120 |
| 121 TestMap.Builder usingAccessorsBuilder = TestMap.newBuilder(); |
| 122 setMapValuesUsingAccessors(usingAccessorsBuilder); |
| 123 TestMap usingAccessors = usingAccessorsBuilder.build(); |
| 124 assertMapValuesSet(usingAccessors); |
| 125 |
| 126 assertEquals(usingAccessors, usingMutableMap); |
| 127 } |
| 128 |
| 81 private void copyMapValues(TestMap source, TestMap.Builder destination) { | 129 private void copyMapValues(TestMap source, TestMap.Builder destination) { |
| 82 destination | 130 destination |
| 83 .putAllInt32ToInt32Field(source.getInt32ToInt32Field()) | 131 .putAllInt32ToInt32Field(source.getInt32ToInt32Field()) |
| 84 .putAllInt32ToStringField(source.getInt32ToStringField()) | 132 .putAllInt32ToStringField(source.getInt32ToStringField()) |
| 85 .putAllInt32ToBytesField(source.getInt32ToBytesField()) | 133 .putAllInt32ToBytesField(source.getInt32ToBytesField()) |
| 86 .putAllInt32ToEnumField(source.getInt32ToEnumField()) | 134 .putAllInt32ToEnumField(source.getInt32ToEnumField()) |
| 87 .putAllInt32ToMessageField(source.getInt32ToMessageField()) | 135 .putAllInt32ToMessageField(source.getInt32ToMessageField()) |
| 88 .putAllStringToInt32Field(source.getStringToInt32Field()); | 136 .putAllStringToInt32Field(source.getStringToInt32Field()); |
| 89 } | 137 } |
| 90 | 138 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 113 assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); | 161 assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); |
| 114 assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); | 162 assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); |
| 115 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); | 163 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); |
| 116 | 164 |
| 117 assertEquals(3, message.getStringToInt32Field().size()); | 165 assertEquals(3, message.getStringToInt32Field().size()); |
| 118 assertEquals(11, message.getStringToInt32Field().get("1").intValue()); | 166 assertEquals(11, message.getStringToInt32Field().get("1").intValue()); |
| 119 assertEquals(22, message.getStringToInt32Field().get("2").intValue()); | 167 assertEquals(22, message.getStringToInt32Field().get("2").intValue()); |
| 120 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); | 168 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); |
| 121 } | 169 } |
| 122 | 170 |
| 123 private void updateMapValues(TestMap.Builder builder) { | 171 private void updateMapValuesUsingMutableMap(TestMap.Builder builder) { |
| 124 builder.getMutableInt32ToInt32Field().put(1, 111); | 172 builder.getMutableInt32ToInt32Field().put(1, 111); |
| 125 builder.getMutableInt32ToInt32Field().remove(2); | 173 builder.getMutableInt32ToInt32Field().remove(2); |
| 126 builder.getMutableInt32ToInt32Field().put(4, 44); | 174 builder.getMutableInt32ToInt32Field().put(4, 44); |
| 127 | 175 // |
| 128 builder.getMutableInt32ToStringField().put(1, "111"); | 176 builder.getMutableInt32ToStringField().put(1, "111"); |
| 129 builder.getMutableInt32ToStringField().remove(2); | 177 builder.getMutableInt32ToStringField().remove(2); |
| 130 builder.getMutableInt32ToStringField().put(4, "44"); | 178 builder.getMutableInt32ToStringField().put(4, "44"); |
| 131 | 179 // |
| 132 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); | 180 builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); |
| 133 builder.getMutableInt32ToBytesField().remove(2); | 181 builder.getMutableInt32ToBytesField().remove(2); |
| 134 builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); | 182 builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); |
| 135 | 183 // |
| 136 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); | 184 builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); |
| 137 builder.getMutableInt32ToEnumField().remove(2); | 185 builder.getMutableInt32ToEnumField().remove(2); |
| 138 builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); | 186 builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); |
| 139 | 187 // |
| 140 builder.getMutableInt32ToMessageField().put( | 188 builder.getMutableInt32ToMessageField().put( |
| 141 1, MessageValue.newBuilder().setValue(111).build()); | 189 1, MessageValue.newBuilder().setValue(111).build()); |
| 142 builder.getMutableInt32ToMessageField().remove(2); | 190 builder.getMutableInt32ToMessageField().remove(2); |
| 143 builder.getMutableInt32ToMessageField().put( | 191 builder.getMutableInt32ToMessageField().put( |
| 144 4, MessageValue.newBuilder().setValue(44).build()); | 192 4, MessageValue.newBuilder().setValue(44).build()); |
| 145 | 193 // |
| 146 builder.getMutableStringToInt32Field().put("1", 111); | 194 builder.getMutableStringToInt32Field().put("1", 111); |
| 147 builder.getMutableStringToInt32Field().remove("2"); | 195 builder.getMutableStringToInt32Field().remove("2"); |
| 148 builder.getMutableStringToInt32Field().put("4", 44); | 196 builder.getMutableStringToInt32Field().put("4", 44); |
| 149 } | 197 } |
| 150 | 198 |
| 199 private void updateMapValuesUsingAccessors(TestMap.Builder builder) { |
| 200 builder |
| 201 .putInt32ToInt32Field(1, 111) |
| 202 .removeInt32ToInt32Field(2) |
| 203 .putInt32ToInt32Field(4, 44) |
| 204 |
| 205 .putInt32ToStringField(1, "111") |
| 206 .removeInt32ToStringField(2) |
| 207 .putInt32ToStringField(4, "44") |
| 208 |
| 209 .putInt32ToBytesField(1, TestUtil.toBytes("111")) |
| 210 .removeInt32ToBytesField(2) |
| 211 .putInt32ToBytesField(4, TestUtil.toBytes("44")) |
| 212 |
| 213 .putInt32ToEnumField(1, TestMap.EnumValue.BAR) |
| 214 .removeInt32ToEnumField(2) |
| 215 .putInt32ToEnumField(4, TestMap.EnumValue.QUX) |
| 216 |
| 217 .putInt32ToMessageField(1, MessageValue.newBuilder().setValue(111).build
()) |
| 218 .removeInt32ToMessageField(2) |
| 219 .putInt32ToMessageField(4, MessageValue.newBuilder().setValue(44).build(
)) |
| 220 |
| 221 .putStringToInt32Field("1", 111) |
| 222 .removeStringToInt32Field("2") |
| 223 .putStringToInt32Field("4", 44); |
| 224 } |
| 225 |
| 226 public void testUpdateMapValues() { |
| 227 TestMap.Builder usingMutableMapBuilder = TestMap.newBuilder(); |
| 228 setMapValuesUsingMutableMap(usingMutableMapBuilder); |
| 229 TestMap usingMutableMap = usingMutableMapBuilder.build(); |
| 230 assertMapValuesSet(usingMutableMap); |
| 231 |
| 232 TestMap.Builder usingAccessorsBuilder = TestMap.newBuilder(); |
| 233 setMapValuesUsingAccessors(usingAccessorsBuilder); |
| 234 TestMap usingAccessors = usingAccessorsBuilder.build(); |
| 235 assertMapValuesSet(usingAccessors); |
| 236 |
| 237 assertEquals(usingAccessors, usingMutableMap); |
| 238 // |
| 239 usingMutableMapBuilder = usingMutableMap.toBuilder(); |
| 240 updateMapValuesUsingMutableMap(usingMutableMapBuilder); |
| 241 usingMutableMap = usingMutableMapBuilder.build(); |
| 242 assertMapValuesUpdated(usingMutableMap); |
| 243 |
| 244 usingAccessorsBuilder = usingAccessors.toBuilder(); |
| 245 updateMapValuesUsingAccessors(usingAccessorsBuilder); |
| 246 usingAccessors = usingAccessorsBuilder.build(); |
| 247 assertMapValuesUpdated(usingAccessors); |
| 248 |
| 249 assertEquals(usingAccessors, usingMutableMap); |
| 250 } |
| 251 |
| 151 private void assertMapValuesUpdated(TestMap message) { | 252 private void assertMapValuesUpdated(TestMap message) { |
| 152 assertEquals(3, message.getInt32ToInt32Field().size()); | 253 assertEquals(3, message.getInt32ToInt32Field().size()); |
| 153 assertEquals(111, message.getInt32ToInt32Field().get(1).intValue()); | 254 assertEquals(111, message.getInt32ToInt32Field().get(1).intValue()); |
| 154 assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); | 255 assertEquals(33, message.getInt32ToInt32Field().get(3).intValue()); |
| 155 assertEquals(44, message.getInt32ToInt32Field().get(4).intValue()); | 256 assertEquals(44, message.getInt32ToInt32Field().get(4).intValue()); |
| 156 | 257 |
| 157 assertEquals(3, message.getInt32ToStringField().size()); | 258 assertEquals(3, message.getInt32ToStringField().size()); |
| 158 assertEquals("111", message.getInt32ToStringField().get(1)); | 259 assertEquals("111", message.getInt32ToStringField().get(1)); |
| 159 assertEquals("33", message.getInt32ToStringField().get(3)); | 260 assertEquals("33", message.getInt32ToStringField().get(3)); |
| 160 assertEquals("44", message.getInt32ToStringField().get(4)); | 261 assertEquals("44", message.getInt32ToStringField().get(4)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 173 assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); | 274 assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); |
| 174 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); | 275 assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); |
| 175 assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); | 276 assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); |
| 176 | 277 |
| 177 assertEquals(3, message.getStringToInt32Field().size()); | 278 assertEquals(3, message.getStringToInt32Field().size()); |
| 178 assertEquals(111, message.getStringToInt32Field().get("1").intValue()); | 279 assertEquals(111, message.getStringToInt32Field().get("1").intValue()); |
| 179 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); | 280 assertEquals(33, message.getStringToInt32Field().get("3").intValue()); |
| 180 assertEquals(44, message.getStringToInt32Field().get("4").intValue()); | 281 assertEquals(44, message.getStringToInt32Field().get("4").intValue()); |
| 181 } | 282 } |
| 182 | 283 |
| 183 private void assertMapValuesCleared(TestMap message) { | 284 private void assertMapValuesCleared(TestMapOrBuilder testMapOrBuilder) { |
| 184 assertEquals(0, message.getInt32ToInt32Field().size()); | 285 assertEquals(0, testMapOrBuilder.getInt32ToInt32Field().size()); |
| 185 assertEquals(0, message.getInt32ToStringField().size()); | 286 assertEquals(0, testMapOrBuilder.getInt32ToInt32FieldCount()); |
| 186 assertEquals(0, message.getInt32ToBytesField().size()); | 287 assertEquals(0, testMapOrBuilder.getInt32ToStringField().size()); |
| 187 assertEquals(0, message.getInt32ToEnumField().size()); | 288 assertEquals(0, testMapOrBuilder.getInt32ToStringFieldCount()); |
| 188 assertEquals(0, message.getInt32ToMessageField().size()); | 289 assertEquals(0, testMapOrBuilder.getInt32ToBytesField().size()); |
| 189 assertEquals(0, message.getStringToInt32Field().size()); | 290 assertEquals(0, testMapOrBuilder.getInt32ToBytesFieldCount()); |
| 291 assertEquals(0, testMapOrBuilder.getInt32ToEnumField().size()); |
| 292 assertEquals(0, testMapOrBuilder.getInt32ToEnumFieldCount()); |
| 293 assertEquals(0, testMapOrBuilder.getInt32ToMessageField().size()); |
| 294 assertEquals(0, testMapOrBuilder.getInt32ToMessageFieldCount()); |
| 295 assertEquals(0, testMapOrBuilder.getStringToInt32Field().size()); |
| 296 assertEquals(0, testMapOrBuilder.getStringToInt32FieldCount()); |
| 190 } | 297 } |
| 191 | 298 |
| 299 public void testGetMapIsImmutable() { |
| 300 TestMap.Builder builder = TestMap.newBuilder(); |
| 301 assertMapsAreImmutable(builder); |
| 302 assertMapsAreImmutable(builder.build()); |
| 303 |
| 304 setMapValuesUsingAccessors(builder); |
| 305 assertMapsAreImmutable(builder); |
| 306 assertMapsAreImmutable(builder.build()); |
| 307 } |
| 308 |
| 309 private void assertMapsAreImmutable(TestMapOrBuilder testMapOrBuilder) { |
| 310 assertImmutable(testMapOrBuilder.getInt32ToInt32Field(), 1, 2); |
| 311 assertImmutable(testMapOrBuilder.getInt32ToStringField(), 1, "2"); |
| 312 assertImmutable(testMapOrBuilder.getInt32ToBytesField(), 1, TestUtil.toBytes
("2")); |
| 313 assertImmutable(testMapOrBuilder.getInt32ToEnumField(), 1, TestMap.EnumValue
.FOO); |
| 314 assertImmutable( |
| 315 testMapOrBuilder.getInt32ToMessageField(), 1, MessageValue.getDefaultIns
tance()); |
| 316 assertImmutable(testMapOrBuilder.getStringToInt32Field(), "1", 2); |
| 317 } |
| 318 |
| 319 private <K, V> void assertImmutable(Map<K, V> map, K key, V value) { |
| 320 try { |
| 321 map.put(key, value); |
| 322 fail(); |
| 323 } catch (UnsupportedOperationException e) { |
| 324 // expected |
| 325 } |
| 326 } |
| 327 |
| 192 public void testMutableMapLifecycle() { | 328 public void testMutableMapLifecycle() { |
| 193 TestMap.Builder builder = TestMap.newBuilder(); | 329 TestMap.Builder builder = TestMap.newBuilder(); |
| 194 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); | 330 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); |
| 195 intMap.put(1, 2); | 331 intMap.put(1, 2); |
| 196 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); | 332 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); |
| 197 try { | 333 try { |
| 198 intMap.put(2, 3); | 334 intMap.put(2, 3); |
| 199 fail(); | 335 fail(); |
| 200 } catch (UnsupportedOperationException e) { | 336 } catch (UnsupportedOperationException e) { |
| 201 // expected | 337 // expected |
| 202 } | 338 } |
| 203 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); | 339 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); |
| 204 builder.getMutableInt32ToInt32Field().put(2, 3); | 340 builder.getMutableInt32ToInt32Field().put(2, 3); |
| 205 assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); | 341 assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); |
| 206 | 342 // |
| 207 Map<Integer, TestMap.EnumValue> enumMap = builder.getMutableInt32ToEnumField
(); | 343 Map<Integer, TestMap.EnumValue> enumMap = builder.getMutableInt32ToEnumField
(); |
| 208 enumMap.put(1, TestMap.EnumValue.BAR); | 344 enumMap.put(1, TestMap.EnumValue.BAR); |
| 209 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnu
mField()); | 345 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnu
mField()); |
| 210 try { | 346 try { |
| 211 enumMap.put(2, TestMap.EnumValue.FOO); | 347 enumMap.put(2, TestMap.EnumValue.FOO); |
| 212 fail(); | 348 fail(); |
| 213 } catch (UnsupportedOperationException e) { | 349 } catch (UnsupportedOperationException e) { |
| 214 // expected | 350 // expected |
| 215 } | 351 } |
| 216 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()
); | 352 assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.getInt32ToEnumField()
); |
| 217 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.FOO); | 353 builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.FOO); |
| 218 assertEquals( | 354 assertEquals( |
| 219 newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), | 355 newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), |
| 220 builder.getInt32ToEnumField()); | 356 builder.getInt32ToEnumField()); |
| 221 | 357 // |
| 222 Map<Integer, String> stringMap = builder.getMutableInt32ToStringField(); | 358 Map<Integer, String> stringMap = builder.getMutableInt32ToStringField(); |
| 223 stringMap.put(1, "1"); | 359 stringMap.put(1, "1"); |
| 224 assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); | 360 assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); |
| 225 try { | 361 try { |
| 226 stringMap.put(2, "2"); | 362 stringMap.put(2, "2"); |
| 227 fail(); | 363 fail(); |
| 228 } catch (UnsupportedOperationException e) { | 364 } catch (UnsupportedOperationException e) { |
| 229 // expected | 365 // expected |
| 230 } | 366 } |
| 231 assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); | 367 assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); |
| 232 builder.getMutableInt32ToStringField().put(2, "2"); | 368 builder.putInt32ToStringField(2, "2"); |
| 233 assertEquals( | 369 assertEquals( |
| 234 newMap(1, "1", 2, "2"), | 370 newMap(1, "1", 2, "2"), |
| 235 builder.getInt32ToStringField()); | 371 builder.getInt32ToStringField()); |
| 236 | 372 // |
| 237 Map<Integer, TestMap.MessageValue> messageMap = builder.getMutableInt32ToMes
sageField(); | 373 Map<Integer, TestMap.MessageValue> messageMap = builder.getMutableInt32ToMes
sageField(); |
| 238 messageMap.put(1, TestMap.MessageValue.getDefaultInstance()); | 374 messageMap.put(1, TestMap.MessageValue.getDefaultInstance()); |
| 239 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), | 375 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), |
| 240 builder.build().getInt32ToMessageField()); | 376 builder.build().getInt32ToMessageField()); |
| 241 try { | 377 try { |
| 242 messageMap.put(2, TestMap.MessageValue.getDefaultInstance()); | 378 messageMap.put(2, TestMap.MessageValue.getDefaultInstance()); |
| 243 fail(); | 379 fail(); |
| 244 } catch (UnsupportedOperationException e) { | 380 } catch (UnsupportedOperationException e) { |
| 245 // expected | 381 // expected |
| 246 } | 382 } |
| 247 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), | 383 assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), |
| 248 builder.getInt32ToMessageField()); | 384 builder.getInt32ToMessageField()); |
| 249 builder.getMutableInt32ToMessageField().put(2, TestMap.MessageValue.getDefau
ltInstance()); | 385 builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance())
; |
| 250 assertEquals( | 386 assertEquals( |
| 251 newMap(1, TestMap.MessageValue.getDefaultInstance(), | 387 newMap(1, TestMap.MessageValue.getDefaultInstance(), |
| 252 2, TestMap.MessageValue.getDefaultInstance()), | 388 2, TestMap.MessageValue.getDefaultInstance()), |
| 253 builder.getInt32ToMessageField()); | 389 builder.getInt32ToMessageField()); |
| 254 } | 390 } |
| 255 | 391 // |
| 256 public void testMutableMapLifecycle_collections() { | 392 public void testMutableMapLifecycle_collections() { |
| 257 TestMap.Builder builder = TestMap.newBuilder(); | 393 TestMap.Builder builder = TestMap.newBuilder(); |
| 258 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); | 394 Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); |
| 259 intMap.put(1, 2); | 395 intMap.put(1, 2); |
| 260 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); | 396 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); |
| 261 try { | 397 try { |
| 262 intMap.remove(2); | 398 intMap.remove(2); |
| 263 fail(); | 399 fail(); |
| 264 } catch (UnsupportedOperationException e) { | 400 } catch (UnsupportedOperationException e) { |
| 265 // expected | 401 // expected |
| (...skipping 25 matching lines...) Expand all Loading... |
| 291 try { | 427 try { |
| 292 intMap.values().iterator().remove(); | 428 intMap.values().iterator().remove(); |
| 293 fail(); | 429 fail(); |
| 294 } catch (UnsupportedOperationException e) { | 430 } catch (UnsupportedOperationException e) { |
| 295 // expected | 431 // expected |
| 296 } | 432 } |
| 297 assertEquals(newMap(1, 2), intMap); | 433 assertEquals(newMap(1, 2), intMap); |
| 298 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); | 434 assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); |
| 299 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); | 435 assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); |
| 300 } | 436 } |
| 301 | 437 |
| 438 |
| 302 public void testGettersAndSetters() throws Exception { | 439 public void testGettersAndSetters() throws Exception { |
| 303 TestMap.Builder builder = TestMap.newBuilder(); | 440 TestMap.Builder builder = TestMap.newBuilder(); |
| 304 TestMap message = builder.build(); | 441 TestMap message = builder.build(); |
| 305 assertMapValuesCleared(message); | 442 assertMapValuesCleared(message); |
| 306 | 443 |
| 307 builder = message.toBuilder(); | 444 builder = message.toBuilder(); |
| 308 setMapValues(builder); | 445 setMapValuesUsingAccessors(builder); |
| 309 message = builder.build(); | 446 message = builder.build(); |
| 310 assertMapValuesSet(message); | 447 assertMapValuesSet(message); |
| 311 | 448 |
| 312 builder = message.toBuilder(); | 449 builder = message.toBuilder(); |
| 313 updateMapValues(builder); | 450 updateMapValuesUsingAccessors(builder); |
| 314 message = builder.build(); | 451 message = builder.build(); |
| 315 assertMapValuesUpdated(message); | 452 assertMapValuesUpdated(message); |
| 316 | 453 |
| 317 builder = message.toBuilder(); | 454 builder = message.toBuilder(); |
| 318 builder.clear(); | 455 builder.clear(); |
| 456 assertMapValuesCleared(builder); |
| 319 message = builder.build(); | 457 message = builder.build(); |
| 320 assertMapValuesCleared(message); | 458 assertMapValuesCleared(message); |
| 321 } | 459 } |
| 322 | 460 |
| 323 public void testPutAll() throws Exception { | 461 public void testPutAll() throws Exception { |
| 324 TestMap.Builder sourceBuilder = TestMap.newBuilder(); | 462 TestMap.Builder sourceBuilder = TestMap.newBuilder(); |
| 325 setMapValues(sourceBuilder); | 463 setMapValuesUsingAccessors(sourceBuilder); |
| 326 TestMap source = sourceBuilder.build(); | 464 TestMap source = sourceBuilder.build(); |
| 465 assertMapValuesSet(source); |
| 327 | 466 |
| 328 TestMap.Builder destination = TestMap.newBuilder(); | 467 TestMap.Builder destination = TestMap.newBuilder(); |
| 329 copyMapValues(source, destination); | 468 copyMapValues(source, destination); |
| 330 assertMapValuesSet(destination.build()); | 469 assertMapValuesSet(destination.build()); |
| 331 } | 470 } |
| 332 | 471 |
| 333 public void testPutAllForUnknownEnumValues() throws Exception { | 472 public void testPutAllForUnknownEnumValues() throws Exception { |
| 334 TestMap.Builder sourceBuilder = TestMap.newBuilder(); | 473 TestMap source = TestMap.newBuilder() |
| 335 sourceBuilder.getMutableInt32ToEnumFieldValue().put(0, 0); | 474 .putAllInt32ToEnumFieldValue(newMap( |
| 336 sourceBuilder.getMutableInt32ToEnumFieldValue().put(1, 1); | 475 0, 0, |
| 337 sourceBuilder.getMutableInt32ToEnumFieldValue().put(2, 1000); // unknown va
lue. | 476 1, 1, |
| 338 TestMap source = sourceBuilder.build(); | 477 2, 1000)) // unknown value. |
| 478 .build(); |
| 339 | 479 |
| 340 TestMap.Builder destinationBuilder = TestMap.newBuilder(); | 480 TestMap destination = TestMap.newBuilder() |
| 341 destinationBuilder.putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldVal
ue()); | 481 .putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()) |
| 342 TestMap destination = destinationBuilder.build(); | 482 .build(); |
| 343 | 483 |
| 344 assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); | 484 assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); |
| 345 assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); | 485 assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); |
| 346 assertEquals(1000, destination.getInt32ToEnumFieldValue().get(2).intValue())
; | 486 assertEquals(1000, destination.getInt32ToEnumFieldValue().get(2).intValue())
; |
| 487 assertEquals(3, destination.getInt32ToEnumFieldCount()); |
| 488 } |
| 489 |
| 490 public void testPutForUnknownEnumValues() throws Exception { |
| 491 TestMap.Builder builder = TestMap.newBuilder() |
| 492 .putInt32ToEnumFieldValue(0, 0) |
| 493 .putInt32ToEnumFieldValue(1, 1); |
| 494 |
| 495 try { |
| 496 builder.putInt32ToEnumFieldValue(2, 1000); // unknown value. |
| 497 fail(); |
| 498 } catch (IllegalArgumentException e) { |
| 499 // expected |
| 500 } |
| 501 |
| 502 TestMap message = builder.build(); |
| 503 assertEquals(0, message.getInt32ToEnumFieldValueOrThrow(0)); |
| 504 assertEquals(1, message.getInt32ToEnumFieldValueOrThrow(1)); |
| 505 assertEquals(2, message.getInt32ToEnumFieldCount()); |
| 506 } |
| 507 |
| 508 public void testPutChecksNullKeysAndValues() throws Exception { |
| 509 TestMap.Builder builder = TestMap.newBuilder(); |
| 510 |
| 511 try { |
| 512 builder.putInt32ToStringField(1, null); |
| 513 fail(); |
| 514 } catch (NullPointerException e) { |
| 515 // expected. |
| 516 } |
| 517 |
| 518 try { |
| 519 builder.putInt32ToBytesField(1, null); |
| 520 fail(); |
| 521 } catch (NullPointerException e) { |
| 522 // expected. |
| 523 } |
| 524 |
| 525 try { |
| 526 builder.putInt32ToEnumField(1, null); |
| 527 fail(); |
| 528 } catch (NullPointerException e) { |
| 529 // expected. |
| 530 } |
| 531 |
| 532 try { |
| 533 builder.putInt32ToMessageField(1, null); |
| 534 fail(); |
| 535 } catch (NullPointerException e) { |
| 536 // expected. |
| 537 } |
| 538 |
| 539 try { |
| 540 builder.putStringToInt32Field(null, 1); |
| 541 fail(); |
| 542 } catch (NullPointerException e) { |
| 543 // expected. |
| 544 } |
| 347 } | 545 } |
| 348 | 546 |
| 349 public void testSerializeAndParse() throws Exception { | 547 public void testSerializeAndParse() throws Exception { |
| 350 TestMap.Builder builder = TestMap.newBuilder(); | 548 TestMap.Builder builder = TestMap.newBuilder(); |
| 351 setMapValues(builder); | 549 setMapValuesUsingAccessors(builder); |
| 352 TestMap message = builder.build(); | 550 TestMap message = builder.build(); |
| 353 assertEquals(message.getSerializedSize(), message.toByteString().size()); | 551 assertEquals(message.getSerializedSize(), message.toByteString().size()); |
| 354 message = TestMap.parser().parseFrom(message.toByteString()); | 552 message = TestMap.parser().parseFrom(message.toByteString()); |
| 355 assertMapValuesSet(message); | 553 assertMapValuesSet(message); |
| 356 | 554 |
| 357 builder = message.toBuilder(); | 555 builder = message.toBuilder(); |
| 358 updateMapValues(builder); | 556 updateMapValuesUsingAccessors(builder); |
| 359 message = builder.build(); | 557 message = builder.build(); |
| 360 assertEquals(message.getSerializedSize(), message.toByteString().size()); | 558 assertEquals(message.getSerializedSize(), message.toByteString().size()); |
| 361 message = TestMap.parser().parseFrom(message.toByteString()); | 559 message = TestMap.parser().parseFrom(message.toByteString()); |
| 362 assertMapValuesUpdated(message); | 560 assertMapValuesUpdated(message); |
| 363 | 561 |
| 364 builder = message.toBuilder(); | 562 builder = message.toBuilder(); |
| 365 builder.clear(); | 563 builder.clear(); |
| 366 message = builder.build(); | 564 message = builder.build(); |
| 367 assertEquals(message.getSerializedSize(), message.toByteString().size()); | 565 assertEquals(message.getSerializedSize(), message.toByteString().size()); |
| 368 message = TestMap.parser().parseFrom(message.toByteString()); | 566 message = TestMap.parser().parseFrom(message.toByteString()); |
| 369 assertMapValuesCleared(message); | 567 assertMapValuesCleared(message); |
| 370 } | 568 } |
| 371 | 569 |
| 570 private TestMap tryParseTestMap(BizarroTestMap bizarroMap) throws IOException
{ |
| 571 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 572 CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStre
am); |
| 573 bizarroMap.writeTo(output); |
| 574 output.flush(); |
| 575 return TestMap.parser().parseFrom(ByteString.copyFrom(byteArrayOutputStream.
toByteArray())); |
| 576 } |
| 577 |
| 578 public void testParseError() throws Exception { |
| 579 ByteString bytes = TestUtil.toBytes("SOME BYTES"); |
| 580 String stringKey = "a string key"; |
| 581 |
| 582 TestMap map = tryParseTestMap(BizarroTestMap.newBuilder() |
| 583 .putInt32ToInt32Field(5, bytes) |
| 584 .build()); |
| 585 assertEquals(map.getInt32ToInt32FieldOrDefault(5, -1), 0); |
| 586 |
| 587 map = tryParseTestMap(BizarroTestMap.newBuilder() |
| 588 .putInt32ToStringField(stringKey, 5) |
| 589 .build()); |
| 590 assertEquals(map.getInt32ToStringFieldOrDefault(0, null), ""); |
| 591 |
| 592 map = tryParseTestMap(BizarroTestMap.newBuilder() |
| 593 .putInt32ToBytesField(stringKey, 5) |
| 594 .build()); |
| 595 assertEquals(map.getInt32ToBytesFieldOrDefault(0, null), ByteString.EMPTY); |
| 596 |
| 597 map = tryParseTestMap(BizarroTestMap.newBuilder() |
| 598 .putInt32ToEnumField(stringKey, bytes) |
| 599 .build()); |
| 600 assertEquals(map.getInt32ToEnumFieldOrDefault(0, null), TestMap.EnumValue.FO
O); |
| 601 |
| 602 try { |
| 603 tryParseTestMap(BizarroTestMap.newBuilder() |
| 604 .putInt32ToMessageField(stringKey, bytes) |
| 605 .build()); |
| 606 fail(); |
| 607 } catch (InvalidProtocolBufferException expected) { |
| 608 assertTrue(expected.getUnfinishedMessage() instanceof TestMap); |
| 609 map = (TestMap) expected.getUnfinishedMessage(); |
| 610 assertTrue(map.getInt32ToMessageField().isEmpty()); |
| 611 } |
| 612 |
| 613 map = tryParseTestMap(BizarroTestMap.newBuilder() |
| 614 .putStringToInt32Field(stringKey, bytes) |
| 615 .build()); |
| 616 assertEquals(map.getStringToInt32FieldOrDefault(stringKey, -1), 0); |
| 617 } |
| 618 |
| 372 public void testMergeFrom() throws Exception { | 619 public void testMergeFrom() throws Exception { |
| 373 TestMap.Builder builder = TestMap.newBuilder(); | 620 TestMap.Builder builder = TestMap.newBuilder(); |
| 374 setMapValues(builder); | 621 setMapValuesUsingAccessors(builder); |
| 375 TestMap message = builder.build(); | 622 TestMap message = builder.build(); |
| 376 | 623 |
| 377 TestMap.Builder other = TestMap.newBuilder(); | 624 TestMap.Builder other = TestMap.newBuilder(); |
| 378 other.mergeFrom(message); | 625 other.mergeFrom(message); |
| 379 assertMapValuesSet(other.build()); | 626 assertMapValuesSet(other.build()); |
| 380 } | 627 } |
| 381 | 628 |
| 382 public void testEqualsAndHashCode() throws Exception { | 629 public void testEqualsAndHashCode() throws Exception { |
| 383 // Test that generated equals() and hashCode() will disregard the order | 630 // Test that generated equals() and hashCode() will disregard the order |
| 384 // of map entries when comparing/hashing map fields. | 631 // of map entries when comparing/hashing map fields. |
| 385 | 632 |
| 386 // We can't control the order of elements in a HashMap. The best we can do | 633 // We can't control the order of elements in a HashMap. The best we can do |
| 387 // here is to add elements in different order. | 634 // here is to add elements in different order. |
| 388 TestMap.Builder b1 = TestMap.newBuilder(); | 635 TestMap.Builder b1 = TestMap.newBuilder() |
| 389 b1.getMutableInt32ToInt32Field().put(1, 2); | 636 .putInt32ToInt32Field(1, 2) |
| 390 b1.getMutableInt32ToInt32Field().put(3, 4); | 637 .putInt32ToInt32Field(3, 4) |
| 391 b1.getMutableInt32ToInt32Field().put(5, 6); | 638 .putInt32ToInt32Field(5, 6); |
| 392 TestMap m1 = b1.build(); | 639 TestMap m1 = b1.build(); |
| 393 | 640 |
| 394 TestMap.Builder b2 = TestMap.newBuilder(); | 641 TestMap.Builder b2 = TestMap.newBuilder() |
| 395 b2.getMutableInt32ToInt32Field().put(5, 6); | 642 .putInt32ToInt32Field(5, 6) |
| 396 b2.getMutableInt32ToInt32Field().put(1, 2); | 643 .putInt32ToInt32Field(1, 2) |
| 397 b2.getMutableInt32ToInt32Field().put(3, 4); | 644 .putInt32ToInt32Field(3, 4); |
| 398 TestMap m2 = b2.build(); | 645 TestMap m2 = b2.build(); |
| 399 | 646 |
| 400 assertEquals(m1, m2); | 647 assertEquals(m1, m2); |
| 401 assertEquals(m1.hashCode(), m2.hashCode()); | 648 assertEquals(m1.hashCode(), m2.hashCode()); |
| 402 | 649 |
| 403 // Make sure we did compare map fields. | 650 // Make sure we did compare map fields. |
| 404 b2.getMutableInt32ToInt32Field().put(1, 0); | 651 b2.putInt32ToInt32Field(1, 0); |
| 405 m2 = b2.build(); | 652 m2 = b2.build(); |
| 406 assertFalse(m1.equals(m2)); | 653 assertFalse(m1.equals(m2)); |
| 407 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed | 654 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed |
| 408 // to be different. | 655 // to be different. |
| 409 | 656 |
| 410 // Regression test for b/18549190: if a map is a subset of the other map, | 657 // Regression test for b/18549190: if a map is a subset of the other map, |
| 411 // equals() should return false. | 658 // equals() should return false. |
| 412 b2.getMutableInt32ToInt32Field().remove(1); | 659 b2.removeInt32ToInt32Field(1); |
| 413 m2 = b2.build(); | 660 m2 = b2.build(); |
| 414 assertFalse(m1.equals(m2)); | 661 assertFalse(m1.equals(m2)); |
| 415 assertFalse(m2.equals(m1)); | 662 assertFalse(m2.equals(m1)); |
| 416 } | 663 } |
| 417 | 664 |
| 418 public void testNestedBuilderOnChangeEventPropagation() { | 665 public void testNestedBuilderOnChangeEventPropagation() { |
| 419 TestOnChangeEventPropagation.Builder parent = | 666 TestOnChangeEventPropagation.Builder parent = |
| 420 TestOnChangeEventPropagation.newBuilder(); | 667 TestOnChangeEventPropagation.newBuilder(); |
| 421 parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2); | 668 parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 2); |
| 422 TestOnChangeEventPropagation message = parent.build(); | 669 TestOnChangeEventPropagation message = parent.build(); |
| 423 assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); | 670 assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); |
| 424 | 671 |
| 425 // Make a change using nested builder. | 672 // Make a change using nested builder. |
| 426 parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3); | 673 parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 3); |
| 427 | 674 |
| 428 // Should be able to observe the change. | 675 // Should be able to observe the change. |
| 429 message = parent.build(); | 676 message = parent.build(); |
| 430 assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); | 677 assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); |
| 431 | 678 |
| 432 // Make another change using mergeFrom() | 679 // Make another change using mergeFrom() |
| 433 TestMap.Builder other = TestMap.newBuilder(); | 680 TestMap.Builder other = TestMap.newBuilder().putInt32ToInt32Field(1, 4); |
| 434 other.getMutableInt32ToInt32Field().put(1, 4); | |
| 435 parent.getOptionalMessageBuilder().mergeFrom(other.build()); | 681 parent.getOptionalMessageBuilder().mergeFrom(other.build()); |
| 436 | 682 |
| 437 // Should be able to observe the change. | 683 // Should be able to observe the change. |
| 438 message = parent.build(); | 684 message = parent.build(); |
| 439 assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); | 685 assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).i
ntValue()); |
| 440 | 686 |
| 441 // Make yet another change by clearing the nested builder. | 687 // Make yet another change by clearing the nested builder. |
| 442 parent.getOptionalMessageBuilder().clear(); | 688 parent.getOptionalMessageBuilder().clear(); |
| 443 | 689 |
| 444 // Should be able to observe the change. | 690 // Should be able to observe the change. |
| 445 message = parent.build(); | 691 message = parent.build(); |
| 446 assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size()); | 692 assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size()); |
| 447 } | 693 } |
| 448 | 694 |
| 449 public void testNestedBuilderOnChangeEventPropagationReflection() { | 695 public void testNestedBuilderOnChangeEventPropagationReflection() { |
| 450 FieldDescriptor intMapField = f("int32_to_int32_field"); | 696 FieldDescriptor intMapField = f("int32_to_int32_field"); |
| 451 // Create an outer message builder with nested builder. | 697 // Create an outer message builder with nested builder. |
| 452 TestOnChangeEventPropagation.Builder parentBuilder = | 698 TestOnChangeEventPropagation.Builder parentBuilder = |
| 453 TestOnChangeEventPropagation.newBuilder(); | 699 TestOnChangeEventPropagation.newBuilder(); |
| 454 TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder(); | 700 TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder(); |
| 455 | 701 |
| 456 // Create a map entry message. | 702 // Create a map entry message. |
| 457 TestMap.Builder entryBuilder = TestMap.newBuilder(); | 703 TestMap.Builder entryBuilder = TestMap.newBuilder().putInt32ToInt32Field(1,
1); |
| 458 entryBuilder.getMutableInt32ToInt32Field().put(1, 1); | |
| 459 | 704 |
| 460 // Put the entry into the nested builder. | 705 // Put the entry into the nested builder. |
| 461 testMapBuilder.addRepeatedField( | 706 testMapBuilder.addRepeatedField( |
| 462 intMapField, entryBuilder.getRepeatedField(intMapField, 0)); | 707 intMapField, entryBuilder.getRepeatedField(intMapField, 0)); |
| 463 | 708 |
| 464 // Should be able to observe the change. | 709 // Should be able to observe the change. |
| 465 TestOnChangeEventPropagation message = parentBuilder.build(); | 710 TestOnChangeEventPropagation message = parentBuilder.build(); |
| 466 assertEquals(1, message.getOptionalMessage().getInt32ToInt32Field().size()); | 711 assertEquals(1, message.getOptionalMessage().getInt32ToInt32Field().size()); |
| 467 | 712 |
| 468 // Change the entry value. | 713 // Change the entry value. |
| 469 entryBuilder.getMutableInt32ToInt32Field().put(1, 4); | 714 entryBuilder.putInt32ToInt32Field(1, 4); |
| 470 testMapBuilder = parentBuilder.getOptionalMessageBuilder(); | 715 testMapBuilder = parentBuilder.getOptionalMessageBuilder(); |
| 471 testMapBuilder.setRepeatedField( | 716 testMapBuilder.setRepeatedField( |
| 472 intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); | 717 intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); |
| 473 | 718 |
| 474 // Should be able to observe the change. | 719 // Should be able to observe the change. |
| 475 message = parentBuilder.build(); | 720 message = parentBuilder.build(); |
| 476 assertEquals(4, | 721 assertEquals(4, |
| 477 message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); | 722 message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); |
| 478 | 723 |
| 479 // Clear the nested builder. | 724 // Clear the nested builder. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 Map<KeyType, ValueType> mapForValues( | 791 Map<KeyType, ValueType> mapForValues( |
| 547 KeyType key1, ValueType value1, KeyType key2, ValueType value2) { | 792 KeyType key1, ValueType value1, KeyType key2, ValueType value2) { |
| 548 Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>(); | 793 Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>(); |
| 549 map.put(key1, value1); | 794 map.put(key1, value1); |
| 550 map.put(key2, value2); | 795 map.put(key2, value2); |
| 551 return map; | 796 return map; |
| 552 } | 797 } |
| 553 | 798 |
| 554 public void testReflectionApi() throws Exception { | 799 public void testReflectionApi() throws Exception { |
| 555 // In reflection API, map fields are just repeated message fields. | 800 // In reflection API, map fields are just repeated message fields. |
| 556 TestMap.Builder builder = TestMap.newBuilder(); | 801 TestMap.Builder builder = TestMap.newBuilder() |
| 557 builder.getMutableInt32ToInt32Field().put(1, 2); | 802 .putInt32ToInt32Field(1, 2) |
| 558 builder.getMutableInt32ToInt32Field().put(3, 4); | 803 .putInt32ToInt32Field(3, 4) |
| 559 builder.getMutableInt32ToMessageField().put( | 804 .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build
()) |
| 560 11, MessageValue.newBuilder().setValue(22).build()); | 805 .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build
()); |
| 561 builder.getMutableInt32ToMessageField().put( | |
| 562 33, MessageValue.newBuilder().setValue(44).build()); | |
| 563 TestMap message = builder.build(); | 806 TestMap message = builder.build(); |
| 564 | 807 |
| 565 // Test getField(), getRepeatedFieldCount(), getRepeatedField(). | 808 // Test getField(), getRepeatedFieldCount(), getRepeatedField(). |
| 566 assertHasMapValues(message, "int32_to_int32_field", | 809 assertHasMapValues(message, "int32_to_int32_field", |
| 567 mapForValues(1, 2, 3, 4)); | 810 mapForValues(1, 2, 3, 4)); |
| 568 assertHasMapValues(message, "int32_to_message_field", | 811 assertHasMapValues(message, "int32_to_message_field", |
| 569 mapForValues( | 812 mapForValues( |
| 570 11, MessageValue.newBuilder().setValue(22).build(), | 813 11, MessageValue.newBuilder().setValue(22).build(), |
| 571 33, MessageValue.newBuilder().setValue(44).build())); | 814 33, MessageValue.newBuilder().setValue(44).build())); |
| 572 | 815 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 builder.setRepeatedField(f("int32_to_int32_field"), i, mapEntryBuilder.bui
ld()); | 865 builder.setRepeatedField(f("int32_to_int32_field"), i, mapEntryBuilder.bui
ld()); |
| 623 } | 866 } |
| 624 message = builder.build(); | 867 message = builder.build(); |
| 625 assertEquals(11, message.getInt32ToInt32Field().get(22).intValue()); | 868 assertEquals(11, message.getInt32ToInt32Field().get(22).intValue()); |
| 626 assertEquals(33, message.getInt32ToInt32Field().get(44).intValue()); | 869 assertEquals(33, message.getInt32ToInt32Field().get(44).intValue()); |
| 627 assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); | 870 assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); |
| 628 } | 871 } |
| 629 | 872 |
| 630 public void testTextFormat() throws Exception { | 873 public void testTextFormat() throws Exception { |
| 631 TestMap.Builder builder = TestMap.newBuilder(); | 874 TestMap.Builder builder = TestMap.newBuilder(); |
| 632 setMapValues(builder); | 875 setMapValuesUsingAccessors(builder); |
| 633 TestMap message = builder.build(); | 876 TestMap message = builder.build(); |
| 634 | 877 |
| 635 String textData = TextFormat.printToString(message); | 878 String textData = TextFormat.printToString(message); |
| 636 | 879 |
| 637 builder = TestMap.newBuilder(); | 880 builder = TestMap.newBuilder(); |
| 638 TextFormat.merge(textData, builder); | 881 TextFormat.merge(textData, builder); |
| 639 message = builder.build(); | 882 message = builder.build(); |
| 640 | 883 |
| 641 assertMapValuesSet(message); | 884 assertMapValuesSet(message); |
| 642 } | 885 } |
| 643 | 886 |
| 644 public void testDynamicMessage() throws Exception { | 887 public void testDynamicMessage() throws Exception { |
| 645 TestMap.Builder builder = TestMap.newBuilder(); | 888 TestMap.Builder builder = TestMap.newBuilder(); |
| 646 setMapValues(builder); | 889 setMapValuesUsingAccessors(builder); |
| 647 TestMap message = builder.build(); | 890 TestMap message = builder.build(); |
| 648 | 891 |
| 649 Message dynamicDefaultInstance = | 892 Message dynamicDefaultInstance = |
| 650 DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); | 893 DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); |
| 651 Message dynamicMessage = dynamicDefaultInstance | 894 Message dynamicMessage = dynamicDefaultInstance |
| 652 .newBuilderForType().mergeFrom(message.toByteString()).build(); | 895 .newBuilderForType().mergeFrom(message.toByteString()).build(); |
| 653 | 896 |
| 654 assertEquals(message, dynamicMessage); | 897 assertEquals(message, dynamicMessage); |
| 655 assertEquals(message.hashCode(), dynamicMessage.hashCode()); | 898 assertEquals(message.hashCode(), dynamicMessage.hashCode()); |
| 656 } | 899 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 681 | 924 |
| 682 // Make sure we did compare map fields. | 925 // Make sure we did compare map fields. |
| 683 b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0))
; | 926 b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0))
; |
| 684 m2 = b2.build(); | 927 m2 = b2.build(); |
| 685 assertFalse(m1.equals(m2)); | 928 assertFalse(m1.equals(m2)); |
| 686 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed | 929 // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed |
| 687 // to be different. | 930 // to be different. |
| 688 } | 931 } |
| 689 | 932 |
| 690 public void testUnknownEnumValues() throws Exception { | 933 public void testUnknownEnumValues() throws Exception { |
| 691 TestMap.Builder builder = TestMap.newBuilder(); | 934 TestMap.Builder builder = TestMap.newBuilder() |
| 692 builder.getMutableInt32ToEnumFieldValue().put(0, 0); | 935 .putAllInt32ToEnumFieldValue(newMap( |
| 693 builder.getMutableInt32ToEnumFieldValue().put(1, 1); | 936 0, 0, |
| 694 builder.getMutableInt32ToEnumFieldValue().put(2, 1000); // unknown value. | 937 1, 1, |
| 938 2, 1000)); // unknown value. |
| 695 TestMap message = builder.build(); | 939 TestMap message = builder.build(); |
| 696 | 940 |
| 697 assertEquals(TestMap.EnumValue.FOO, | 941 assertEquals(TestMap.EnumValue.FOO, |
| 698 message.getInt32ToEnumField().get(0)); | 942 message.getInt32ToEnumField().get(0)); |
| 699 assertEquals(TestMap.EnumValue.BAR, | 943 assertEquals(TestMap.EnumValue.BAR, |
| 700 message.getInt32ToEnumField().get(1)); | 944 message.getInt32ToEnumField().get(1)); |
| 701 assertEquals(TestMap.EnumValue.UNRECOGNIZED, | 945 assertEquals(TestMap.EnumValue.UNRECOGNIZED, |
| 702 message.getInt32ToEnumField().get(2)); | 946 message.getInt32ToEnumField().get(2)); |
| 703 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); | 947 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); |
| 704 | 948 |
| 705 // Unknown enum values should be preserved after: | 949 // Unknown enum values should be preserved after: |
| 706 // 1. Serialization and parsing. | 950 // 1. Serialization and parsing. |
| 707 // 2. toBuild(). | 951 // 2. toBuild(). |
| 708 // 3. mergeFrom(). | 952 // 3. mergeFrom(). |
| 709 message = TestMap.parseFrom(message.toByteString()); | 953 message = TestMap.parseFrom(message.toByteString()); |
| 710 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); | 954 assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); |
| 711 builder = message.toBuilder(); | 955 builder = message.toBuilder(); |
| 712 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); | 956 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); |
| 713 builder = TestMap.newBuilder().mergeFrom(message); | 957 builder = TestMap.newBuilder().mergeFrom(message); |
| 714 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); | 958 assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); |
| 715 | 959 |
| 716 // hashCode()/equals() should take unknown enum values into account. | 960 // hashCode()/equals() should take unknown enum values into account. |
| 717 builder.getMutableInt32ToEnumFieldValue().put(2, 1001); | 961 builder.putAllInt32ToEnumFieldValue(newMap(2, 1001)); |
| 718 TestMap message2 = builder.build(); | 962 TestMap message2 = builder.build(); |
| 719 assertFalse(message.hashCode() == message2.hashCode()); | 963 assertFalse(message.hashCode() == message2.hashCode()); |
| 720 assertFalse(message.equals(message2)); | 964 assertFalse(message.equals(message2)); |
| 721 // Unknown values will be converted to UNRECOGNIZED so the resulted enum map | 965 // Unknown values will be converted to UNRECOGNIZED so the resulted enum map |
| 722 // should be the same. | 966 // should be the same. |
| 723 assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField
())); | 967 assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField
())); |
| 724 } | 968 } |
| 725 | 969 |
| 726 public void testUnknownEnumValuesInReflectionApi() throws Exception { | 970 public void testUnknownEnumValuesInReflectionApi() throws Exception { |
| 727 Descriptor descriptor = TestMap.getDescriptor(); | 971 Descriptor descriptor = TestMap.getDescriptor(); |
| 728 EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); | 972 EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); |
| 729 FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); | 973 FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); |
| 730 | 974 |
| 731 Map<Integer, Integer> data = new HashMap<Integer, Integer>(); | 975 Map<Integer, Integer> data = newMap( |
| 732 data.put(0, 0); | 976 0, 0, |
| 733 data.put(1, 1); | 977 1, 1, |
| 734 data.put(2, 1000); // unknown value. | 978 2, 1000); // unknown value |
| 735 | 979 |
| 736 TestMap.Builder builder = TestMap.newBuilder(); | 980 TestMap.Builder builder = TestMap.newBuilder() |
| 737 for (Map.Entry<Integer, Integer> entry : data.entrySet()) { | 981 .putAllInt32ToEnumFieldValue(data); |
| 738 builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getVal
ue()); | |
| 739 } | |
| 740 | 982 |
| 741 // Try to read unknown enum values using reflection API. | 983 // Try to read unknown enum values using reflection API. |
| 742 for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) { | 984 for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) { |
| 743 Message mapEntry = (Message) builder.getRepeatedField(field, i); | 985 Message mapEntry = (Message) builder.getRepeatedField(field, i); |
| 744 int key = ((Integer) getFieldValue(mapEntry, "key")).intValue(); | 986 int key = ((Integer) getFieldValue(mapEntry, "key")).intValue(); |
| 745 int value = ((EnumValueDescriptor) getFieldValue(mapEntry, "value")).getNu
mber(); | 987 int value = ((EnumValueDescriptor) getFieldValue(mapEntry, "value")).getNu
mber(); |
| 746 assertEquals(data.get(key).intValue(), value); | 988 assertEquals(data.get(key).intValue(), value); |
| 747 Message.Builder mapEntryBuilder = mapEntry.toBuilder(); | 989 Message.Builder mapEntryBuilder = mapEntry.toBuilder(); |
| 748 // Increase the value by 1. | 990 // Increase the value by 1. |
| 749 setFieldValue(mapEntryBuilder, "value", | 991 setFieldValue(mapEntryBuilder, "value", |
| 750 enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1)); | 992 enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1)); |
| 751 builder.setRepeatedField(field, i, mapEntryBuilder.build()); | 993 builder.setRepeatedField(field, i, mapEntryBuilder.build()); |
| 752 } | 994 } |
| 753 | 995 |
| 754 // Verify that enum values have been successfully updated. | 996 // Verify that enum values have been successfully updated. |
| 755 TestMap message = builder.build(); | 997 TestMap message = builder.build(); |
| 756 for (Map.Entry<Integer, Integer> entry : message.getInt32ToEnumFieldValue().
entrySet()) { | 998 for (Map.Entry<Integer, Integer> entry : message.getInt32ToEnumFieldValue().
entrySet()) { |
| 757 assertEquals(data.get(entry.getKey()) + 1, entry.getValue().intValue()); | 999 assertEquals(data.get(entry.getKey()) + 1, entry.getValue().intValue()); |
| 758 } | 1000 } |
| 759 } | 1001 } |
| 760 | 1002 |
| 761 public void testIterationOrder() throws Exception { | 1003 public void testIterationOrder() throws Exception { |
| 762 TestMap.Builder builder = TestMap.newBuilder(); | 1004 TestMap.Builder builder = TestMap.newBuilder(); |
| 763 setMapValues(builder); | 1005 setMapValuesUsingAccessors(builder); |
| 764 TestMap message = builder.build(); | 1006 TestMap message = builder.build(); |
| 765 | 1007 |
| 766 assertEquals(Arrays.asList("1", "2", "3"), | 1008 assertEquals(Arrays.asList("1", "2", "3"), |
| 767 new ArrayList<String>(message.getStringToInt32Field().keySet())); | 1009 new ArrayList<String>(message.getStringToInt32Field().keySet())); |
| 768 } | 1010 } |
| 769 | 1011 |
| 1012 public void testGetMap() { |
| 1013 TestMap.Builder builder = TestMap.newBuilder(); |
| 1014 setMapValuesUsingAccessors(builder); |
| 1015 TestMap message = builder.build(); |
| 1016 assertEquals( |
| 1017 message.getStringToInt32Field(), |
| 1018 message.getStringToInt32FieldMap()); |
| 1019 assertEquals( |
| 1020 message.getInt32ToBytesField(), |
| 1021 message.getInt32ToBytesFieldMap()); |
| 1022 assertEquals( |
| 1023 message.getInt32ToEnumField(), |
| 1024 message.getInt32ToEnumFieldMap()); |
| 1025 assertEquals( |
| 1026 message.getInt32ToEnumFieldValue(), |
| 1027 message.getInt32ToEnumFieldValueMap()); |
| 1028 assertEquals( |
| 1029 message.getInt32ToMessageField(), |
| 1030 message.getInt32ToMessageFieldMap()); |
| 1031 } |
| 1032 |
| 1033 public void testContains() { |
| 1034 TestMap.Builder builder = TestMap.newBuilder(); |
| 1035 setMapValuesUsingAccessors(builder); |
| 1036 assertMapContainsSetValues(builder); |
| 1037 assertMapContainsSetValues(builder.build()); |
| 1038 } |
| 1039 |
| 1040 private void assertMapContainsSetValues(TestMapOrBuilder testMapOrBuilder) { |
| 1041 assertTrue(testMapOrBuilder.containsInt32ToInt32Field(1)); |
| 1042 assertTrue(testMapOrBuilder.containsInt32ToInt32Field(2)); |
| 1043 assertTrue(testMapOrBuilder.containsInt32ToInt32Field(3)); |
| 1044 assertFalse(testMapOrBuilder.containsInt32ToInt32Field(-1)); |
| 1045 |
| 1046 assertTrue(testMapOrBuilder.containsInt32ToStringField(1)); |
| 1047 assertTrue(testMapOrBuilder.containsInt32ToStringField(2)); |
| 1048 assertTrue(testMapOrBuilder.containsInt32ToStringField(3)); |
| 1049 assertFalse(testMapOrBuilder.containsInt32ToStringField(-1)); |
| 1050 |
| 1051 assertTrue(testMapOrBuilder.containsInt32ToBytesField(1)); |
| 1052 assertTrue(testMapOrBuilder.containsInt32ToBytesField(2)); |
| 1053 assertTrue(testMapOrBuilder.containsInt32ToBytesField(3)); |
| 1054 assertFalse(testMapOrBuilder.containsInt32ToBytesField(-1)); |
| 1055 |
| 1056 assertTrue(testMapOrBuilder.containsInt32ToEnumField(1)); |
| 1057 assertTrue(testMapOrBuilder.containsInt32ToEnumField(2)); |
| 1058 assertTrue(testMapOrBuilder.containsInt32ToEnumField(3)); |
| 1059 assertFalse(testMapOrBuilder.containsInt32ToEnumField(-1)); |
| 1060 |
| 1061 assertTrue(testMapOrBuilder.containsInt32ToMessageField(1)); |
| 1062 assertTrue(testMapOrBuilder.containsInt32ToMessageField(2)); |
| 1063 assertTrue(testMapOrBuilder.containsInt32ToMessageField(3)); |
| 1064 assertFalse(testMapOrBuilder.containsInt32ToMessageField(-1)); |
| 1065 |
| 1066 assertTrue(testMapOrBuilder.containsStringToInt32Field("1")); |
| 1067 assertTrue(testMapOrBuilder.containsStringToInt32Field("2")); |
| 1068 assertTrue(testMapOrBuilder.containsStringToInt32Field("3")); |
| 1069 assertFalse(testMapOrBuilder.containsStringToInt32Field("-1")); |
| 1070 } |
| 1071 |
| 1072 public void testCount() { |
| 1073 TestMap.Builder builder = TestMap.newBuilder(); |
| 1074 assertMapCounts(0, builder); |
| 1075 |
| 1076 setMapValuesUsingAccessors(builder); |
| 1077 assertMapCounts(3, builder); |
| 1078 |
| 1079 TestMap message = builder.build(); |
| 1080 assertMapCounts(3, message); |
| 1081 |
| 1082 builder = message.toBuilder().putInt32ToInt32Field(4, 44); |
| 1083 assertEquals(4, builder.getInt32ToInt32FieldCount()); |
| 1084 assertEquals(4, builder.build().getInt32ToInt32FieldCount()); |
| 1085 |
| 1086 // already present - should be unchanged |
| 1087 builder.putInt32ToInt32Field(4, 44); |
| 1088 assertEquals(4, builder.getInt32ToInt32FieldCount()); |
| 1089 } |
| 1090 |
| 1091 private void assertMapCounts(int expectedCount, TestMapOrBuilder testMapOrBuil
der) { |
| 1092 assertEquals(expectedCount, testMapOrBuilder.getInt32ToInt32FieldCount()); |
| 1093 assertEquals(expectedCount, testMapOrBuilder.getInt32ToStringFieldCount()); |
| 1094 assertEquals(expectedCount, testMapOrBuilder.getInt32ToBytesFieldCount()); |
| 1095 assertEquals(expectedCount, testMapOrBuilder.getInt32ToEnumFieldCount()); |
| 1096 assertEquals(expectedCount, testMapOrBuilder.getInt32ToMessageFieldCount()); |
| 1097 assertEquals(expectedCount, testMapOrBuilder.getStringToInt32FieldCount()); |
| 1098 } |
| 1099 |
| 1100 public void testGetOrDefault() { |
| 1101 TestMap.Builder builder = TestMap.newBuilder(); |
| 1102 assertMapCounts(0, builder); |
| 1103 setMapValuesUsingAccessors(builder); |
| 1104 doTestGetOrDefault(builder); |
| 1105 doTestGetOrDefault(builder.build()); |
| 1106 } |
| 1107 |
| 1108 public void doTestGetOrDefault(TestMapOrBuilder testMapOrBuilder) { |
| 1109 assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(1, -11)); |
| 1110 assertEquals(-11, testMapOrBuilder.getInt32ToInt32FieldOrDefault(-1, -11)); |
| 1111 |
| 1112 assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrDefault(1, "-11")
); |
| 1113 assertNull("-11", testMapOrBuilder.getInt32ToStringFieldOrDefault(-1, null))
; |
| 1114 |
| 1115 assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOr
Default(1, null)); |
| 1116 assertNull(testMapOrBuilder.getInt32ToBytesFieldOrDefault(-1, null)); |
| 1117 |
| 1118 assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrDe
fault(1, null)); |
| 1119 assertNull(testMapOrBuilder.getInt32ToEnumFieldOrDefault(-1, null)); |
| 1120 |
| 1121 assertEquals( |
| 1122 TestMap.EnumValue.BAR.getNumber(), |
| 1123 (int) testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(2, -1)); |
| 1124 assertEquals(-1, testMapOrBuilder.getInt32ToEnumFieldValueOrDefault(-1000, -
1)); |
| 1125 |
| 1126 assertEquals(MessageValue.newBuilder().setValue(11).build(), |
| 1127 testMapOrBuilder.getInt32ToMessageFieldOrDefault(1, null)); |
| 1128 assertNull(testMapOrBuilder.getInt32ToMessageFieldOrDefault(-1, null)); |
| 1129 |
| 1130 assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrDefault("1", -11)); |
| 1131 assertEquals(-11, testMapOrBuilder.getStringToInt32FieldOrDefault("-1", -11)
); |
| 1132 |
| 1133 try { |
| 1134 testMapOrBuilder.getStringToInt32FieldOrDefault(null, -11); |
| 1135 fail(); |
| 1136 } catch (NullPointerException e) { |
| 1137 // expected |
| 1138 } |
| 1139 } |
| 1140 |
| 1141 public void testGetOrThrow() { |
| 1142 TestMap.Builder builder = TestMap.newBuilder(); |
| 1143 assertMapCounts(0, builder); |
| 1144 setMapValuesUsingAccessors(builder); |
| 1145 doTestGetOrDefault(builder); |
| 1146 doTestGetOrDefault(builder.build()); |
| 1147 } |
| 1148 |
| 1149 public void doTestGetOrThrow(TestMapOrBuilder testMapOrBuilder) { |
| 1150 assertEquals(11, testMapOrBuilder.getInt32ToInt32FieldOrThrow(1)); |
| 1151 try { |
| 1152 testMapOrBuilder.getInt32ToInt32FieldOrThrow(-1); |
| 1153 fail(); |
| 1154 } catch (IllegalArgumentException e) { |
| 1155 // expected |
| 1156 } |
| 1157 |
| 1158 assertEquals("11", testMapOrBuilder.getInt32ToStringFieldOrThrow(1)); |
| 1159 |
| 1160 try { |
| 1161 testMapOrBuilder.getInt32ToStringFieldOrThrow(-1); |
| 1162 fail(); |
| 1163 } catch (IllegalArgumentException e) { |
| 1164 // expected |
| 1165 } |
| 1166 |
| 1167 assertEquals(TestUtil.toBytes("11"), testMapOrBuilder.getInt32ToBytesFieldOr
Throw(1)); |
| 1168 |
| 1169 try { |
| 1170 testMapOrBuilder.getInt32ToBytesFieldOrThrow(-1); |
| 1171 fail(); |
| 1172 } catch (IllegalArgumentException e) { |
| 1173 // expected |
| 1174 } |
| 1175 |
| 1176 assertEquals(TestMap.EnumValue.FOO, testMapOrBuilder.getInt32ToEnumFieldOrTh
row(1)); |
| 1177 try { |
| 1178 testMapOrBuilder.getInt32ToEnumFieldOrThrow(-1); |
| 1179 fail(); |
| 1180 } catch (IllegalArgumentException e) { |
| 1181 // expected |
| 1182 } |
| 1183 |
| 1184 assertEquals( |
| 1185 TestMap.EnumValue.BAR.getNumber(), testMapOrBuilder.getInt32ToEnumFieldV
alueOrThrow(2)); |
| 1186 try { |
| 1187 testMapOrBuilder.getInt32ToEnumFieldValueOrThrow(-1); |
| 1188 fail(); |
| 1189 } catch (IllegalArgumentException e) { |
| 1190 // expected |
| 1191 } |
| 1192 |
| 1193 assertEquals(MessageValue.newBuilder().setValue(11).build(), |
| 1194 testMapOrBuilder.getInt32ToMessageFieldOrThrow(1)); |
| 1195 try { |
| 1196 testMapOrBuilder.getInt32ToMessageFieldOrThrow(-1); |
| 1197 fail(); |
| 1198 } catch (IllegalArgumentException e) { |
| 1199 // expected |
| 1200 } |
| 1201 |
| 1202 assertEquals(11, testMapOrBuilder.getStringToInt32FieldOrThrow("1")); |
| 1203 try { |
| 1204 testMapOrBuilder.getStringToInt32FieldOrThrow("-1"); |
| 1205 fail(); |
| 1206 } catch (IllegalArgumentException e) { |
| 1207 // expected |
| 1208 } |
| 1209 |
| 1210 try { |
| 1211 testMapOrBuilder.getStringToInt32FieldOrThrow(null); |
| 1212 fail(); |
| 1213 } catch (NullPointerException e) { |
| 1214 // expected |
| 1215 } |
| 1216 } |
| 1217 |
| 1218 public void testPut() { |
| 1219 TestMap.Builder builder = TestMap.newBuilder(); |
| 1220 builder.putInt32ToInt32Field(1, 11); |
| 1221 assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); |
| 1222 |
| 1223 builder.putInt32ToStringField(1, "a"); |
| 1224 assertEquals("a", builder.getInt32ToStringFieldOrThrow(1)); |
| 1225 try { |
| 1226 builder.putInt32ToStringField(1, null); |
| 1227 fail(); |
| 1228 } catch (NullPointerException e) { |
| 1229 // expected |
| 1230 } |
| 1231 |
| 1232 builder.putInt32ToBytesField(1, TestUtil.toBytes("11")); |
| 1233 assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1))
; |
| 1234 try { |
| 1235 builder.putInt32ToBytesField(1, null); |
| 1236 fail(); |
| 1237 } catch (NullPointerException e) { |
| 1238 // expected |
| 1239 } |
| 1240 |
| 1241 builder.putInt32ToEnumField(1, TestMap.EnumValue.FOO); |
| 1242 assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); |
| 1243 try { |
| 1244 builder.putInt32ToEnumField(1, null); |
| 1245 fail(); |
| 1246 } catch (NullPointerException e) { |
| 1247 // expected |
| 1248 } |
| 1249 |
| 1250 builder.putInt32ToEnumFieldValue(1, TestMap.EnumValue.BAR.getNumber()); |
| 1251 assertEquals( |
| 1252 TestMap.EnumValue.BAR.getNumber(), builder.getInt32ToEnumFieldValueOrThr
ow(1)); |
| 1253 try { |
| 1254 builder.putInt32ToEnumFieldValue(1, -1); |
| 1255 fail(); |
| 1256 } catch (IllegalArgumentException e) { |
| 1257 // expected |
| 1258 } |
| 1259 |
| 1260 builder.putStringToInt32Field("a", 1); |
| 1261 assertEquals(1, builder.getStringToInt32FieldOrThrow("a")); |
| 1262 try { |
| 1263 builder.putStringToInt32Field(null, -1); |
| 1264 } catch (NullPointerException e) { |
| 1265 // expected |
| 1266 } |
| 1267 } |
| 1268 |
| 1269 public void testRemove() { |
| 1270 TestMap.Builder builder = TestMap.newBuilder(); |
| 1271 setMapValuesUsingAccessors(builder); |
| 1272 assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); |
| 1273 for (int times = 0; times < 2; times++) { |
| 1274 builder.removeInt32ToInt32Field(1); |
| 1275 assertEquals(-1, builder.getInt32ToInt32FieldOrDefault(1, -1)); |
| 1276 } |
| 1277 |
| 1278 assertEquals("11", builder.getInt32ToStringFieldOrThrow(1)); |
| 1279 for (int times = 0; times < 2; times++) { |
| 1280 builder.removeInt32ToStringField(1); |
| 1281 assertNull(builder.getInt32ToStringFieldOrDefault(1, null)); |
| 1282 } |
| 1283 |
| 1284 assertEquals(TestUtil.toBytes("11"), builder.getInt32ToBytesFieldOrThrow(1))
; |
| 1285 for (int times = 0; times < 2; times++) { |
| 1286 builder.removeInt32ToBytesField(1); |
| 1287 assertNull(builder.getInt32ToBytesFieldOrDefault(1, null)); |
| 1288 } |
| 1289 |
| 1290 assertEquals(TestMap.EnumValue.FOO, builder.getInt32ToEnumFieldOrThrow(1)); |
| 1291 for (int times = 0; times < 2; times++) { |
| 1292 builder.removeInt32ToEnumField(1); |
| 1293 assertNull(builder.getInt32ToEnumFieldOrDefault(1, null)); |
| 1294 } |
| 1295 |
| 1296 assertEquals(11, builder.getStringToInt32FieldOrThrow("1")); |
| 1297 for (int times = 0; times < 2; times++) { |
| 1298 builder.removeStringToInt32Field("1"); |
| 1299 assertEquals(-1, builder.getStringToInt32FieldOrDefault("1", -1)); |
| 1300 } |
| 1301 |
| 1302 try { |
| 1303 builder.removeStringToInt32Field(null); |
| 1304 fail(); |
| 1305 } catch (NullPointerException e) { |
| 1306 // expected |
| 1307 } |
| 1308 } |
| 1309 |
| 1310 public void testReservedWordsFieldNames() { |
| 1311 ReservedAsMapField.newBuilder().build(); |
| 1312 ReservedAsMapFieldWithEnumValue.newBuilder().build(); |
| 1313 } |
| 1314 |
| 1315 public void testDeterministicSerialziation() throws Exception { |
| 1316 TestMap.Builder builder = TestMap.newBuilder(); |
| 1317 // int32->int32 |
| 1318 builder.putInt32ToInt32Field(5, 1); |
| 1319 builder.putInt32ToInt32Field(1, 1); |
| 1320 builder.putInt32ToInt32Field(4, 1); |
| 1321 builder.putInt32ToInt32Field(-2, 1); |
| 1322 builder.putInt32ToInt32Field(0, 1); |
| 1323 |
| 1324 // uint32->int32 |
| 1325 builder.putUint32ToInt32Field(5, 1); |
| 1326 builder.putUint32ToInt32Field(1, 1); |
| 1327 builder.putUint32ToInt32Field(4, 1); |
| 1328 builder.putUint32ToInt32Field(-2, 1); |
| 1329 builder.putUint32ToInt32Field(0, 1); |
| 1330 |
| 1331 // int64->int32 |
| 1332 builder.putInt64ToInt32Field(5L, 1); |
| 1333 builder.putInt64ToInt32Field(1L, 1); |
| 1334 builder.putInt64ToInt32Field(4L, 1); |
| 1335 builder.putInt64ToInt32Field(-2L, 1); |
| 1336 builder.putInt64ToInt32Field(0L, 1); |
| 1337 |
| 1338 // string->int32 |
| 1339 builder.putStringToInt32Field("baz", 1); |
| 1340 builder.putStringToInt32Field("foo", 1); |
| 1341 builder.putStringToInt32Field("bar", 1); |
| 1342 builder.putStringToInt32Field("", 1); |
| 1343 builder.putStringToInt32Field("hello", 1); |
| 1344 builder.putStringToInt32Field("world", 1); |
| 1345 |
| 1346 TestMap message = builder.build(); |
| 1347 byte[] serialized = new byte[message.getSerializedSize()]; |
| 1348 CodedOutputStream output = CodedOutputStream.newInstance(serialized); |
| 1349 output.useDeterministicSerialization(); |
| 1350 message.writeTo(output); |
| 1351 output.flush(); |
| 1352 |
| 1353 CodedInputStream input = CodedInputStream.newInstance(serialized); |
| 1354 List<Integer> int32Keys = new ArrayList<Integer>(); |
| 1355 List<Integer> uint32Keys = new ArrayList<Integer>(); |
| 1356 List<Long> int64Keys = new ArrayList<Long>(); |
| 1357 List<String> stringKeys = new ArrayList<String>(); |
| 1358 int tag; |
| 1359 while (true) { |
| 1360 tag = input.readTag(); |
| 1361 if (tag == 0) { |
| 1362 break; |
| 1363 } |
| 1364 int length = input.readRawVarint32(); |
| 1365 int oldLimit = input.pushLimit(length); |
| 1366 switch (WireFormat.getTagFieldNumber(tag)) { |
| 1367 case TestMap.STRING_TO_INT32_FIELD_FIELD_NUMBER: |
| 1368 stringKeys.add(readMapStringKey(input)); |
| 1369 break; |
| 1370 case TestMap.INT32_TO_INT32_FIELD_FIELD_NUMBER: |
| 1371 int32Keys.add(readMapIntegerKey(input)); |
| 1372 break; |
| 1373 case TestMap.UINT32_TO_INT32_FIELD_FIELD_NUMBER: |
| 1374 uint32Keys.add(readMapIntegerKey(input)); |
| 1375 break; |
| 1376 case TestMap.INT64_TO_INT32_FIELD_FIELD_NUMBER: |
| 1377 int64Keys.add(readMapLongKey(input)); |
| 1378 break; |
| 1379 default: |
| 1380 fail("Unexpected fields."); |
| 1381 } |
| 1382 input.popLimit(oldLimit); |
| 1383 } |
| 1384 assertEquals( |
| 1385 Arrays.asList(-2, 0, 1, 4, 5), |
| 1386 int32Keys); |
| 1387 assertEquals( |
| 1388 Arrays.asList(-2, 0, 1, 4, 5), |
| 1389 uint32Keys); |
| 1390 assertEquals( |
| 1391 Arrays.asList(-2L, 0L, 1L, 4L, 5L), |
| 1392 int64Keys); |
| 1393 assertEquals( |
| 1394 Arrays.asList("", "bar", "baz", "foo", "hello", "world"), |
| 1395 stringKeys); |
| 1396 } |
| 1397 |
| 1398 public void testInitFromPartialDynamicMessage() { |
| 1399 FieldDescriptor fieldDescriptor = |
| 1400 TestMap.getDescriptor().findFieldByNumber(TestMap.INT32_TO_MESSAGE_FIELD
_FIELD_NUMBER); |
| 1401 Descriptor mapEntryType = fieldDescriptor.getMessageType(); |
| 1402 FieldDescriptor keyField = mapEntryType.findFieldByNumber(1); |
| 1403 FieldDescriptor valueField = mapEntryType.findFieldByNumber(2); |
| 1404 DynamicMessage dynamicMessage = |
| 1405 DynamicMessage.newBuilder(TestMap.getDescriptor()) |
| 1406 .addRepeatedField( |
| 1407 fieldDescriptor, |
| 1408 DynamicMessage.newBuilder(mapEntryType) |
| 1409 .setField(keyField, 10) |
| 1410 .setField(valueField, TestMap.MessageValue.newBuilder().setV
alue(10).build()) |
| 1411 .build()) |
| 1412 .build(); |
| 1413 TestMap message = TestMap.newBuilder().mergeFrom(dynamicMessage).build(); |
| 1414 assertEquals( |
| 1415 TestMap.MessageValue.newBuilder().setValue(10).build(), |
| 1416 message.getInt32ToMessageFieldMap().get(10)); |
| 1417 } |
| 1418 |
| 1419 public void testInitFromFullyDynamicMessage() { |
| 1420 FieldDescriptor fieldDescriptor = |
| 1421 TestMap.getDescriptor().findFieldByNumber(TestMap.INT32_TO_MESSAGE_FIELD
_FIELD_NUMBER); |
| 1422 Descriptor mapEntryType = fieldDescriptor.getMessageType(); |
| 1423 FieldDescriptor keyField = mapEntryType.findFieldByNumber(1); |
| 1424 FieldDescriptor valueField = mapEntryType.findFieldByNumber(2); |
| 1425 DynamicMessage dynamicMessage = |
| 1426 DynamicMessage.newBuilder(TestMap.getDescriptor()) |
| 1427 .addRepeatedField( |
| 1428 fieldDescriptor, |
| 1429 DynamicMessage.newBuilder(mapEntryType) |
| 1430 .setField(keyField, 10) |
| 1431 .setField( |
| 1432 valueField, |
| 1433 DynamicMessage.newBuilder(TestMap.MessageValue.getDescri
ptor()) |
| 1434 .setField( |
| 1435 TestMap.MessageValue.getDescriptor().findFieldBy
Name("value"), 10) |
| 1436 .build()) |
| 1437 .build()) |
| 1438 .build(); |
| 1439 TestMap message = TestMap.newBuilder().mergeFrom(dynamicMessage).build(); |
| 1440 assertEquals( |
| 1441 TestMap.MessageValue.newBuilder().setValue(10).build(), |
| 1442 message.getInt32ToMessageFieldMap().get(10)); |
| 1443 } |
| 1444 |
| 1445 private int readMapIntegerKey(CodedInputStream input) throws IOException { |
| 1446 int tag = input.readTag(); |
| 1447 assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT), tag); |
| 1448 int ret = input.readInt32(); |
| 1449 // skip the value field. |
| 1450 input.skipField(input.readTag()); |
| 1451 assertTrue(input.isAtEnd()); |
| 1452 return ret; |
| 1453 } |
| 1454 |
| 1455 private long readMapLongKey(CodedInputStream input) throws IOException { |
| 1456 int tag = input.readTag(); |
| 1457 assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT), tag); |
| 1458 long ret = input.readInt64(); |
| 1459 // skip the value field. |
| 1460 input.skipField(input.readTag()); |
| 1461 assertTrue(input.isAtEnd()); |
| 1462 return ret; |
| 1463 } |
| 1464 |
| 1465 private String readMapStringKey(CodedInputStream input) throws IOException { |
| 1466 int tag = input.readTag(); |
| 1467 assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), ta
g); |
| 1468 String ret = input.readString(); |
| 1469 // skip the value field. |
| 1470 input.skipField(input.readTag()); |
| 1471 assertTrue(input.isAtEnd()); |
| 1472 return ret; |
| 1473 } |
| 1474 |
| 770 private static <K, V> Map<K, V> newMap(K key1, V value1) { | 1475 private static <K, V> Map<K, V> newMap(K key1, V value1) { |
| 771 Map<K, V> map = new HashMap<K, V>(); | 1476 Map<K, V> map = new HashMap<K, V>(); |
| 772 map.put(key1, value1); | 1477 map.put(key1, value1); |
| 773 return map; | 1478 return map; |
| 774 } | 1479 } |
| 775 | 1480 |
| 776 private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2) { | 1481 private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2) { |
| 777 Map<K, V> map = new HashMap<K, V>(); | 1482 Map<K, V> map = new HashMap<K, V>(); |
| 778 map.put(key1, value1); | 1483 map.put(key1, value1); |
| 779 map.put(key2, value2); | 1484 map.put(key2, value2); |
| 780 return map; | 1485 return map; |
| 781 } | 1486 } |
| 1487 |
| 1488 private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2, K k
ey3, V value3) { |
| 1489 Map<K, V> map = new HashMap<K, V>(); |
| 1490 map.put(key1, value1); |
| 1491 map.put(key2, value2); |
| 1492 map.put(key3, value3); |
| 1493 return map; |
| 1494 } |
| 782 } | 1495 } |
| OLD | NEW |