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 |
| 38 import junit.framework.TestCase; |
| 39 |
37 import java.util.ArrayList; | 40 import java.util.ArrayList; |
38 import junit.framework.TestCase; | |
39 | 41 |
40 /** | 42 /** |
41 * Unit test for messages with lazy fields. | 43 * Unit test for messages with lazy fields. |
42 * | 44 * |
43 * @author niwasaki@google.com (Naoki Iwasaki) | 45 * @author niwasaki@google.com (Naoki Iwasaki) |
44 */ | 46 */ |
45 public class LazyMessageLiteTest extends TestCase { | 47 public class LazyMessageLiteTest extends TestCase { |
46 | 48 |
47 private Parser<LazyInnerMessageLite> originalLazyInnerMessageLiteParser; | 49 private Parser<LazyInnerMessageLite> originalLazyInnerMessageLiteParser; |
48 | 50 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 .setNum(1) | 96 .setNum(1) |
95 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) | 97 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) |
96 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) | 98 .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) |
97 .build(); | 99 .build(); |
98 | 100 |
99 assertEquals(1, outer.getNum()); | 101 assertEquals(1, outer.getNum()); |
100 assertEquals(2, outer.getRepeatedInnerCount()); | 102 assertEquals(2, outer.getRepeatedInnerCount()); |
101 assertEquals(119, outer.getRepeatedInner(0).getNum()); | 103 assertEquals(119, outer.getRepeatedInner(0).getNum()); |
102 assertEquals(122, outer.getRepeatedInner(1).getNum()); | 104 assertEquals(122, outer.getRepeatedInner(1).getNum()); |
103 } | 105 } |
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 } | |
117 | 106 |
118 public void testAddAll() { | 107 public void testAddAll() { |
119 ArrayList<LazyInnerMessageLite> inners = new ArrayList<LazyInnerMessageLite>
(); | 108 ArrayList<LazyInnerMessageLite> inners = new ArrayList<LazyInnerMessageLite>
(); |
120 int count = 4; | 109 int count = 4; |
121 for (int i = 0; i < count; i++) { | 110 for (int i = 0; i < count; i++) { |
122 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() | 111 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() |
123 .setNum(i) | 112 .setNum(i) |
124 .build(); | 113 .build(); |
125 inners.add(inner); | 114 inners.add(inner); |
126 } | 115 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 .build(); | 244 .build(); |
256 // Merging default-instance shouldn't overwrite values in the base message. | 245 // Merging default-instance shouldn't overwrite values in the base message. |
257 assertEquals(119, merged.getNum()); | 246 assertEquals(119, merged.getNum()); |
258 assertEquals(122, merged.getNumWithDefault()); | 247 assertEquals(122, merged.getNumWithDefault()); |
259 assertEquals(115, merged.getInner().getNum()); | 248 assertEquals(115, merged.getInner().getNum()); |
260 assertEquals(42, merged.getInner().getNumWithDefault()); | 249 assertEquals(42, merged.getInner().getNumWithDefault()); |
261 assertEquals(115, merged.getOneofInner().getNum()); | 250 assertEquals(115, merged.getOneofInner().getNum()); |
262 assertEquals(42, merged.getOneofInner().getNumWithDefault()); | 251 assertEquals(42, merged.getOneofInner().getNumWithDefault()); |
263 } | 252 } |
264 | 253 |
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 | |
282 public void testSerialize() throws InvalidProtocolBufferException { | 254 public void testSerialize() throws InvalidProtocolBufferException { |
283 LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder() | 255 LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder() |
284 .setNum(3) | 256 .setNum(3) |
285 .build(); | 257 .build(); |
286 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() | 258 LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder() |
287 .setNum(2) | 259 .setNum(2) |
288 .setNested(nested) | 260 .setNested(nested) |
289 .build(); | 261 .build(); |
290 LazyMessageLite outer = LazyMessageLite.newBuilder() | 262 LazyMessageLite outer = LazyMessageLite.newBuilder() |
291 .setNum(1) | 263 .setNum(1) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 LazyInnerMessageLite innerMessage = innerBuilder.build(); | 298 LazyInnerMessageLite innerMessage = innerBuilder.build(); |
327 assertTrue(innerMessage.hasExtension(LazyExtension.extension)); | 299 assertTrue(innerMessage.hasExtension(LazyExtension.extension)); |
328 assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getN
ame()); | 300 assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getN
ame()); |
329 | 301 |
330 LazyMessageLite lite = LazyMessageLite.newBuilder() | 302 LazyMessageLite lite = LazyMessageLite.newBuilder() |
331 .setInner(innerMessage).build(); | 303 .setInner(innerMessage).build(); |
332 assertTrue(lite.getInner().hasExtension(LazyExtension.extension)); | 304 assertTrue(lite.getInner().hasExtension(LazyExtension.extension)); |
333 assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).g
etName()); | 305 assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).g
etName()); |
334 } | 306 } |
335 } | 307 } |
OLD | NEW |