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.lite_equals_and_hash.LiteEqualsAndHash.Bar; 34 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; 35 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; 36 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestOneofEquals; 37 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestRecursiveOneof; 38 39 import junit.framework.TestCase; 40 41 /** 42 * Test generate equal and hash methods for the lite runtime. 43 * 44 * @author pbogle@google.com Phil Bogle 45 */ 46 public class LiteEqualsAndHashTest extends TestCase { 47 testEquals()48 public void testEquals() throws Exception { 49 // Since the generated equals and hashCode methods for lite messages are a 50 // mostly complete subset of those for regular messages, we can mostly assume 51 // that the generated methods are already thoroughly tested by the regular tests. 52 53 // This test mostly just verifies is that a proto with 54 // optimize_for = LITE_RUNTIME and java_generates_equals_and_hash_compiles 55 // correctly when linked only against the lite library. 56 57 // We do however do some basic testing to make sure that equals is actually 58 // overriden to test for value equality rather than simple object equality. 59 60 // Check that two identical objs are equal. 61 Foo foo1a = Foo.newBuilder() 62 .setValue(1) 63 .addBar(Bar.newBuilder().setName("foo1")) 64 .build(); 65 Foo foo1b = Foo.newBuilder() 66 .setValue(1) 67 .addBar(Bar.newBuilder().setName("foo1")) 68 .build(); 69 Foo foo2 = Foo.newBuilder() 70 .setValue(1) 71 .addBar(Bar.newBuilder().setName("foo2")) 72 .build(); 73 74 // Check that equals is doing value rather than object equality. 75 assertEquals(foo1a, foo1b); 76 assertEquals(foo1a.hashCode(), foo1b.hashCode()); 77 78 // Check that a diffeent object is not equal. 79 assertFalse(foo1a.equals(foo2)); 80 81 // Check that two objects which have different types but the same field values are not 82 // considered to be equal. 83 Bar bar = Bar.newBuilder().setName("bar").build(); 84 BarPrime barPrime = BarPrime.newBuilder().setName("bar").build(); 85 assertFalse(bar.equals(barPrime)); 86 } 87 testOneofEquals()88 public void testOneofEquals() throws Exception { 89 TestOneofEquals.Builder builder = TestOneofEquals.newBuilder(); 90 TestOneofEquals message1 = builder.build(); 91 // Set message2's name field to default value. The two messages should be different when we 92 // check with the oneof case. 93 builder.setName(""); 94 TestOneofEquals message2 = builder.build(); 95 assertFalse(message1.equals(message2)); 96 } 97 testEqualsAndHashCodeWithUnknownFields()98 public void testEqualsAndHashCodeWithUnknownFields() throws InvalidProtocolBufferException { 99 Foo fooWithOnlyValue = Foo.newBuilder() 100 .setValue(1) 101 .build(); 102 103 Foo fooWithValueAndExtension = fooWithOnlyValue.toBuilder() 104 .setValue(1) 105 .setExtension(Bar.fooExt, Bar.newBuilder() 106 .setName("name") 107 .build()) 108 .build(); 109 110 Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.toByteArray()); 111 112 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFields); 113 assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnknownFields); 114 } 115 assertEqualsAndHashCodeAreFalse(Object o1, Object o2)116 private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) { 117 assertFalse(o1.equals(o2)); 118 assertFalse(o1.hashCode() == o2.hashCode()); 119 } 120 testRecursiveHashcode()121 public void testRecursiveHashcode() { 122 // This tests that we don't infinite loop. 123 TestRecursiveOneof.getDefaultInstance().hashCode(); 124 } 125 } 126