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 36 import java.io.IOException; 37 import junit.framework.TestCase; 38 39 /** 40 * Unit test for {@link LazyField}. 41 * 42 * @author xiangl@google.com (Xiang Li) 43 */ 44 public class LazyFieldTest extends TestCase { testHashCode()45 public void testHashCode() { 46 MessageLite message = TestUtil.getAllSet(); 47 LazyField lazyField = 48 createLazyFieldFromMessage(message); 49 assertEquals(message.hashCode(), lazyField.hashCode()); 50 lazyField.getValue(); 51 assertEquals(message.hashCode(), lazyField.hashCode()); 52 changeValue(lazyField); 53 // make sure two messages have different hash code 54 assertNotEqual(message.hashCode(), lazyField.hashCode()); 55 } 56 testHashCodeEx()57 public void testHashCodeEx() throws Exception { 58 TestAllExtensions message = TestUtil.getAllExtensionsSet(); 59 LazyField lazyField = createLazyFieldFromMessage(message); 60 assertEquals(message.hashCode(), lazyField.hashCode()); 61 lazyField.getValue(); 62 assertEquals(message.hashCode(), lazyField.hashCode()); 63 changeValue(lazyField); 64 // make sure two messages have different hash code 65 assertNotEqual(message.hashCode(), lazyField.hashCode()); 66 } 67 testGetValue()68 public void testGetValue() { 69 MessageLite message = TestUtil.getAllSet(); 70 LazyField lazyField = createLazyFieldFromMessage(message); 71 assertEquals(message, lazyField.getValue()); 72 changeValue(lazyField); 73 assertNotEqual(message, lazyField.getValue()); 74 } 75 testGetValueEx()76 public void testGetValueEx() throws Exception { 77 TestAllExtensions message = TestUtil.getAllExtensionsSet(); 78 LazyField lazyField = createLazyFieldFromMessage(message); 79 assertEquals(message, lazyField.getValue()); 80 changeValue(lazyField); 81 assertNotEqual(message, lazyField.getValue()); 82 } 83 testEqualsObject()84 public void testEqualsObject() { 85 MessageLite message = TestUtil.getAllSet(); 86 LazyField lazyField = createLazyFieldFromMessage(message); 87 assertTrue(lazyField.equals(message)); 88 changeValue(lazyField); 89 assertFalse(lazyField.equals(message)); 90 assertFalse(message.equals(lazyField.getValue())); 91 } 92 testEqualsObjectEx()93 public void testEqualsObjectEx() throws Exception { 94 TestAllExtensions message = TestUtil.getAllExtensionsSet(); 95 LazyField lazyField = createLazyFieldFromMessage(message); 96 assertTrue(lazyField.equals(message)); 97 changeValue(lazyField); 98 assertFalse(lazyField.equals(message)); 99 assertFalse(message.equals(lazyField.getValue())); 100 } 101 102 // Help methods. 103 createLazyFieldFromMessage(MessageLite message)104 private LazyField createLazyFieldFromMessage(MessageLite message) { 105 ByteString bytes = message.toByteString(); 106 return new LazyField(message.getDefaultInstanceForType(), 107 TestUtil.getExtensionRegistry(), bytes); 108 } 109 changeValue(LazyField lazyField)110 private void changeValue(LazyField lazyField) { 111 TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder(); 112 builder.addRepeatedBool(true); 113 MessageLite newMessage = builder.build(); 114 lazyField.setValue(newMessage); 115 } 116 assertNotEqual(Object unexpected, Object actual)117 private void assertNotEqual(Object unexpected, Object actual) { 118 assertFalse(unexpected == actual 119 || (unexpected != null && unexpected.equals(actual))); 120 } 121 } 122