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 13 matching lines...) Expand all Loading... |
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 import static protobuf_unittest.UnittestProto.optionalInt32Extension; | 33 import static protobuf_unittest.UnittestProto.optionalInt32Extension; |
34 import static protobuf_unittest.UnittestProto.optionalInt64Extension; | |
35 | 34 |
36 import protobuf_unittest.UnittestProto.TestAllExtensions; | 35 import protobuf_unittest.UnittestProto.TestAllExtensions; |
37 import protobuf_unittest.UnittestProto.TestAllTypes; | 36 import protobuf_unittest.UnittestProto.TestAllTypes; |
38 | 37 import java.io.IOException; |
39 import junit.framework.TestCase; | 38 import junit.framework.TestCase; |
40 | 39 |
41 import java.io.IOException; | |
42 | |
43 /** | 40 /** |
44 * Unit test for {@link LazyFieldLite}. | 41 * Unit test for {@link LazyFieldLite}. |
45 * | 42 * |
46 * @author xiangl@google.com (Xiang Li) | 43 * @author xiangl@google.com (Xiang Li) |
47 */ | 44 */ |
48 public class LazyFieldLiteTest extends TestCase { | 45 public class LazyFieldLiteTest extends TestCase { |
49 | 46 |
50 public void testGetValue() { | 47 public void testGetValue() { |
51 MessageLite message = TestUtil.getAllSet(); | 48 MessageLite message = TestUtil.getAllSet(); |
52 LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message); | 49 LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); | 211 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); |
215 | 212 |
216 // And again reverse. | 213 // And again reverse. |
217 field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage); | 214 field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage); |
218 field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. | 215 field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. |
219 other = LazyFieldLite.fromValue(messageWithExtensions); | 216 other = LazyFieldLite.fromValue(messageWithExtensions); |
220 field.merge(other); | 217 field.merge(other); |
221 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); | 218 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); |
222 } | 219 } |
223 | 220 |
224 public void testMergeMightLoseExtensions() throws Exception { | |
225 // Test that we don't know about the extensions when parsing. | |
226 TestAllExtensions message1 = | |
227 TestAllExtensions.newBuilder().setExtension(optionalInt32Extension, 1).b
uild(); | |
228 TestAllExtensions message2 = | |
229 TestAllExtensions.newBuilder().setExtension(optionalInt64Extension, 2L).
build(); | |
230 | |
231 LazyFieldLite field = LazyFieldLite.fromValue(message1); | |
232 field.merge(LazyFieldLite.fromValue(message2)); | |
233 | |
234 // We lose the extensions from message 2 because we have to serialize it and
then parse it | |
235 // again, using the empty registry this time. | |
236 TestAllExtensions value = | |
237 (TestAllExtensions) field.getValue(TestAllExtensions.getDefaultInstance(
)); | |
238 assertTrue(value.hasExtension(optionalInt32Extension)); | |
239 assertEquals(Integer.valueOf(1), value.getExtension(optionalInt32Extension))
; | |
240 assertFalse(value.hasExtension(optionalInt64Extension)); | |
241 | |
242 // The field is still there, it is just unknown. | |
243 assertTrue(value.getUnknownFields() | |
244 .hasField(optionalInt64Extension.getDescriptor().getNumber())); | |
245 } | |
246 | |
247 | 221 |
248 // Help methods. | 222 // Help methods. |
249 | 223 |
250 private LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message) { | 224 private LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message) { |
251 return createLazyFieldLiteFromMessage(TestUtil.getExtensionRegistry(), messa
ge); | 225 return createLazyFieldLiteFromMessage(TestUtil.getExtensionRegistry(), messa
ge); |
252 } | 226 } |
253 | 227 |
254 private LazyFieldLite createLazyFieldLiteFromMessage( | 228 private LazyFieldLite createLazyFieldLiteFromMessage( |
255 ExtensionRegistryLite extensionRegistry, MessageLite message) { | 229 ExtensionRegistryLite extensionRegistry, MessageLite message) { |
256 ByteString bytes = message.toByteString(); | 230 ByteString bytes = message.toByteString(); |
257 return new LazyFieldLite(extensionRegistry, bytes); | 231 return new LazyFieldLite(extensionRegistry, bytes); |
258 } | 232 } |
259 | 233 |
260 private void changeValue(LazyFieldLite lazyField) { | 234 private void changeValue(LazyFieldLite lazyField) { |
261 TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder(); | 235 TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder(); |
262 builder.addRepeatedBool(true); | 236 builder.addRepeatedBool(true); |
263 MessageLite newMessage = builder.build(); | 237 MessageLite newMessage = builder.build(); |
264 lazyField.setValue(newMessage); | 238 lazyField.setValue(newMessage); |
265 } | 239 } |
266 | 240 |
267 private void assertNotEqual(Object unexpected, Object actual) { | 241 private void assertNotEqual(Object unexpected, Object actual) { |
268 assertFalse(unexpected == actual | 242 assertFalse(unexpected == actual |
269 || (unexpected != null && unexpected.equals(actual))); | 243 || (unexpected != null && unexpected.equals(actual))); |
270 } | 244 } |
271 | 245 |
272 } | 246 } |
OLD | NEW |