| 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; |
| 34 | 35 |
| 35 import protobuf_unittest.UnittestProto.TestAllExtensions; | 36 import protobuf_unittest.UnittestProto.TestAllExtensions; |
| 36 import protobuf_unittest.UnittestProto.TestAllTypes; | 37 import protobuf_unittest.UnittestProto.TestAllTypes; |
| 38 |
| 39 import junit.framework.TestCase; |
| 40 |
| 37 import java.io.IOException; | 41 import java.io.IOException; |
| 38 import junit.framework.TestCase; | |
| 39 | 42 |
| 40 /** | 43 /** |
| 41 * Unit test for {@link LazyFieldLite}. | 44 * Unit test for {@link LazyFieldLite}. |
| 42 * | 45 * |
| 43 * @author xiangl@google.com (Xiang Li) | 46 * @author xiangl@google.com (Xiang Li) |
| 44 */ | 47 */ |
| 45 public class LazyFieldLiteTest extends TestCase { | 48 public class LazyFieldLiteTest extends TestCase { |
| 46 | 49 |
| 47 public void testGetValue() { | 50 public void testGetValue() { |
| 48 MessageLite message = TestUtil.getAllSet(); | 51 MessageLite message = TestUtil.getAllSet(); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); | 214 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); |
| 212 | 215 |
| 213 // And again reverse. | 216 // And again reverse. |
| 214 field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage); | 217 field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage); |
| 215 field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. | 218 field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing. |
| 216 other = LazyFieldLite.fromValue(messageWithExtensions); | 219 other = LazyFieldLite.fromValue(messageWithExtensions); |
| 217 field.merge(other); | 220 field.merge(other); |
| 218 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); | 221 assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefa
ultInstance())); |
| 219 } | 222 } |
| 220 | 223 |
| 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 |
| 221 | 247 |
| 222 // Help methods. | 248 // Help methods. |
| 223 | 249 |
| 224 private LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message) { | 250 private LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message) { |
| 225 return createLazyFieldLiteFromMessage(TestUtil.getExtensionRegistry(), messa
ge); | 251 return createLazyFieldLiteFromMessage(TestUtil.getExtensionRegistry(), messa
ge); |
| 226 } | 252 } |
| 227 | 253 |
| 228 private LazyFieldLite createLazyFieldLiteFromMessage( | 254 private LazyFieldLite createLazyFieldLiteFromMessage( |
| 229 ExtensionRegistryLite extensionRegistry, MessageLite message) { | 255 ExtensionRegistryLite extensionRegistry, MessageLite message) { |
| 230 ByteString bytes = message.toByteString(); | 256 ByteString bytes = message.toByteString(); |
| 231 return new LazyFieldLite(extensionRegistry, bytes); | 257 return new LazyFieldLite(extensionRegistry, bytes); |
| 232 } | 258 } |
| 233 | 259 |
| 234 private void changeValue(LazyFieldLite lazyField) { | 260 private void changeValue(LazyFieldLite lazyField) { |
| 235 TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder(); | 261 TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder(); |
| 236 builder.addRepeatedBool(true); | 262 builder.addRepeatedBool(true); |
| 237 MessageLite newMessage = builder.build(); | 263 MessageLite newMessage = builder.build(); |
| 238 lazyField.setValue(newMessage); | 264 lazyField.setValue(newMessage); |
| 239 } | 265 } |
| 240 | 266 |
| 241 private void assertNotEqual(Object unexpected, Object actual) { | 267 private void assertNotEqual(Object unexpected, Object actual) { |
| 242 assertFalse(unexpected == actual | 268 assertFalse(unexpected == actual |
| 243 || (unexpected != null && unexpected.equals(actual))); | 269 || (unexpected != null && unexpected.equals(actual))); |
| 244 } | 270 } |
| 245 | 271 |
| 246 } | 272 } |
| OLD | NEW |