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 16 matching lines...) Expand all Loading... |
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 import protobuf_unittest.LazyFieldsLite.LazyExtension; | 33 import protobuf_unittest.LazyFieldsLite.LazyExtension; |
34 import protobuf_unittest.LazyFieldsLite.LazyInnerMessageLite; | 34 import protobuf_unittest.LazyFieldsLite.LazyInnerMessageLite; |
35 import protobuf_unittest.LazyFieldsLite.LazyMessageLite; | 35 import protobuf_unittest.LazyFieldsLite.LazyMessageLite; |
36 import protobuf_unittest.LazyFieldsLite.LazyNestedInnerMessageLite; | 36 import protobuf_unittest.LazyFieldsLite.LazyNestedInnerMessageLite; |
37 | 37 import java.util.ArrayList; |
38 import junit.framework.TestCase; | 38 import junit.framework.TestCase; |
39 | 39 |
40 import java.util.ArrayList; | |
41 | |
42 /** | 40 /** |
43 * Unit test for messages with lazy fields. | 41 * Unit test for messages with lazy fields. |
44 * | 42 * |
45 * @author niwasaki@google.com (Naoki Iwasaki) | 43 * @author niwasaki@google.com (Naoki Iwasaki) |
46 */ | 44 */ |
47 public class LazyMessageLiteTest extends TestCase { | 45 public class LazyMessageLiteTest extends TestCase { |
48 | 46 |
49 private Parser<LazyInnerMessageLite> originalLazyInnerMessageLiteParser; | 47 private Parser<LazyInnerMessageLite> originalLazyInnerMessageLiteParser; |
50 | 48 |
51 @Override | 49 @Override |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 .setNum(1) | 94 .setNum(1) |
97 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) | 95 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) |
98 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) | 96 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) |
99 .build(); | 97 .build(); |
100 | 98 |
101 assertEquals(1, outer.getNum()); | 99 assertEquals(1, outer.getNum()); |
102 assertEquals(2, outer.getRepeatedInnerCount()); | 100 assertEquals(2, outer.getRepeatedInnerCount()); |
103 assertEquals(119, outer.getRepeatedInner(0).getNum()); | 101 assertEquals(119, outer.getRepeatedInner(0).getNum()); |
104 assertEquals(122, outer.getRepeatedInner(1).getNum()); | 102 assertEquals(122, outer.getRepeatedInner(1).getNum()); |
105 } | 103 } |
| 104 |
| 105 public void testRepeatedMutability() throws Exception { |
| 106 LazyMessageLite outer = LazyMessageLite.newBuilder() |
| 107 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) |
| 108 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) |
| 109 .build(); |
| 110 |
| 111 outer = LazyMessageLite.parseFrom(outer.toByteArray()); |
| 112 try { |
| 113 outer.getRepeatedInnerList().set(1, null); |
| 114 fail(); |
| 115 } catch (UnsupportedOperationException expected) {} |
| 116 } |
106 | 117 |
107 public void testAddAll() { | 118 public void testAddAll() { |
108 ArrayList<LazyInnerMessageLite> inners = new ArrayList<LazyInnerMessageLite>
(); | 119 ArrayList<LazyInnerMessageLite> inners = new ArrayList<LazyInnerMessageLite>
(); |
109 int count = 4; | 120 int count = 4; |
110 for (int i = 0; i < count; i++) { | 121 for (int i = 0; i < count; i++) { |
111 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() | 122 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() |
112 .setNum(i) | 123 .setNum(i) |
113 .build(); | 124 .build(); |
114 inners.add(inner); | 125 inners.add(inner); |
115 } | 126 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 .build(); | 255 .build(); |
245 // Merging default-instance shouldn't overwrite values in the base message. | 256 // Merging default-instance shouldn't overwrite values in the base message. |
246 assertEquals(119, merged.getNum()); | 257 assertEquals(119, merged.getNum()); |
247 assertEquals(122, merged.getNumWithDefault()); | 258 assertEquals(122, merged.getNumWithDefault()); |
248 assertEquals(115, merged.getInner().getNum()); | 259 assertEquals(115, merged.getInner().getNum()); |
249 assertEquals(42, merged.getInner().getNumWithDefault()); | 260 assertEquals(42, merged.getInner().getNumWithDefault()); |
250 assertEquals(115, merged.getOneofInner().getNum()); | 261 assertEquals(115, merged.getOneofInner().getNum()); |
251 assertEquals(42, merged.getOneofInner().getNumWithDefault()); | 262 assertEquals(42, merged.getOneofInner().getNumWithDefault()); |
252 } | 263 } |
253 | 264 |
| 265 // Regression test for b/28198805. |
| 266 public void testMergeOneofMessages() throws Exception { |
| 267 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder().build(); |
| 268 LazyMessageLite outer = LazyMessageLite.newBuilder().setOneofInner(inner).bu
ild(); |
| 269 ByteString data1 = outer.toByteString(); |
| 270 |
| 271 // The following should not alter the content of the 'outer' message. |
| 272 LazyMessageLite.Builder merged = LazyMessageLite.newBuilder().mergeFrom(oute
r); |
| 273 LazyInnerMessageLite anotherInner = LazyInnerMessageLite.newBuilder().setNum
(12345).build(); |
| 274 merged.setOneofInner(anotherInner); |
| 275 |
| 276 // Check that the 'outer' stays the same. |
| 277 ByteString data2 = outer.toByteString(); |
| 278 assertEquals(data1, data2); |
| 279 assertEquals(0, outer.getOneofInner().getNum()); |
| 280 } |
| 281 |
254 public void testSerialize() throws InvalidProtocolBufferException { | 282 public void testSerialize() throws InvalidProtocolBufferException { |
255 LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder() | 283 LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder() |
256 .setNum(3) | 284 .setNum(3) |
257 .build(); | 285 .build(); |
258 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() | 286 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() |
259 .setNum(2) | 287 .setNum(2) |
260 .setNested(nested) | 288 .setNested(nested) |
261 .build(); | 289 .build(); |
262 LazyMessageLite outer = LazyMessageLite.newBuilder() | 290 LazyMessageLite outer = LazyMessageLite.newBuilder() |
263 .setNum(1) | 291 .setNum(1) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 LazyInnerMessageLite innerMessage = innerBuilder.build(); | 326 LazyInnerMessageLite innerMessage = innerBuilder.build(); |
299 assertTrue(innerMessage.hasExtension(LazyExtension.extension)); | 327 assertTrue(innerMessage.hasExtension(LazyExtension.extension)); |
300 assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getN
ame()); | 328 assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getN
ame()); |
301 | 329 |
302 LazyMessageLite lite = LazyMessageLite.newBuilder() | 330 LazyMessageLite lite = LazyMessageLite.newBuilder() |
303 .setInner(innerMessage).build(); | 331 .setInner(innerMessage).build(); |
304 assertTrue(lite.getInner().hasExtension(LazyExtension.extension)); | 332 assertTrue(lite.getInner().hasExtension(LazyExtension.extension)); |
305 assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).g
etName()); | 333 assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).g
etName()); |
306 } | 334 } |
307 } | 335 } |
OLD | NEW |