1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 30 31 package com.google.protobuf; 32 33 import protobuf_unittest.UnittestProto.TestAllExtensions; 34 import protobuf_unittest.UnittestProto.TestAllTypes; 35 import junit.framework.TestCase; 36 37 /** 38 * Unit test for {@link LazyField}. 39 * 40 * @author xiangl@google.com (Xiang Li) 41 */ 42 public class LazyFieldTest extends TestCase { testHashCode()43 public void testHashCode() { 44 MessageLite message = TestUtil.getAllSet(); 45 LazyField lazyField = createLazyFieldFromMessage(message); 46 assertEquals(message.hashCode(), lazyField.hashCode()); 47 lazyField.getValue(); 48 assertEquals(message.hashCode(), lazyField.hashCode()); 49 changeValue(lazyField); 50 // make sure two messages have different hash code 51 assertNotEqual(message.hashCode(), lazyField.hashCode()); 52 } 53 testHashCodeEx()54 public void testHashCodeEx() throws Exception { 55 TestAllExtensions message = TestUtil.getAllExtensionsSet(); 56 LazyField lazyField = createLazyFieldFromMessage(message); 57 assertEquals(message.hashCode(), lazyField.hashCode()); 58 lazyField.getValue(); 59 assertEquals(message.hashCode(), lazyField.hashCode()); 60 changeValue(lazyField); 61 // make sure two messages have different hash code 62 assertNotEqual(message.hashCode(), lazyField.hashCode()); 63 } 64 testGetValue()65 public void testGetValue() { 66 MessageLite message = TestUtil.getAllSet(); 67 LazyField lazyField = createLazyFieldFromMessage(message); 68 assertEquals(message, lazyField.getValue()); 69 changeValue(lazyField); 70 assertNotEqual(message, lazyField.getValue()); 71 } 72 testGetValueEx()73 public void testGetValueEx() throws Exception { 74 TestAllExtensions message = TestUtil.getAllExtensionsSet(); 75 LazyField lazyField = createLazyFieldFromMessage(message); 76 assertEquals(message, lazyField.getValue()); 77 changeValue(lazyField); 78 assertNotEqual(message, lazyField.getValue()); 79 } 80 testEqualsObject()81 public void testEqualsObject() { 82 MessageLite message = TestUtil.getAllSet(); 83 LazyField lazyField = createLazyFieldFromMessage(message); 84 assertTrue(lazyField.equals(message)); 85 changeValue(lazyField); 86 assertFalse(lazyField.equals(message)); 87 assertFalse(message.equals(lazyField.getValue())); 88 } 89 90 @SuppressWarnings("EqualsIncompatibleType") // LazyField.equals() is not symmetric testEqualsObjectEx()91 public void testEqualsObjectEx() throws Exception { 92 TestAllExtensions message = TestUtil.getAllExtensionsSet(); 93 LazyField lazyField = createLazyFieldFromMessage(message); 94 assertTrue(lazyField.equals(message)); 95 changeValue(lazyField); 96 assertFalse(lazyField.equals(message)); 97 assertFalse(message.equals(lazyField.getValue())); 98 } 99 100 // Help methods. 101 createLazyFieldFromMessage(MessageLite message)102 private LazyField createLazyFieldFromMessage(MessageLite message) { 103 ByteString bytes = message.toByteString(); 104 return new LazyField( 105 message.getDefaultInstanceForType(), TestUtil.getExtensionRegistry(), bytes); 106 } 107 changeValue(LazyField lazyField)108 private void changeValue(LazyField lazyField) { 109 TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder(); 110 builder.addRepeatedBool(true); 111 MessageLite newMessage = builder.build(); 112 lazyField.setValue(newMessage); 113 } 114 assertNotEqual(Object unexpected, Object actual)115 private void assertNotEqual(Object unexpected, Object actual) { 116 assertFalse(unexpected == actual || (unexpected != null && unexpected.equals(actual))); 117 } 118 } 119