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 37 import junit.framework.TestCase; 38 39 /** 40 * Test generate equal and hash methods for the lite runtime. 41 * 42 * @author pbogle@google.com Phil Bogle 43 */ 44 public class LiteEqualsAndHashTest extends TestCase { 45 testEquals()46 public void testEquals() throws Exception { 47 // Since the generated equals and hashCode methods for lite messages are a 48 // mostly complete subset of those for regular messages, we can mostly assume 49 // that the generated methods are already thoroughly tested by the regular tests. 50 51 // This test mostly just verifies is that a proto with 52 // optimize_for = LITE_RUNTIME and java_generates_equals_and_hash_compiles 53 // correctly when linked only against the lite library. 54 55 // We do however do some basic testing to make sure that equals is actually 56 // overriden to test for value equality rather than simple object equality. 57 58 // Check that two identical objs are equal. 59 Foo foo1a = Foo.newBuilder() 60 .setValue(1) 61 .addBar(Bar.newBuilder().setName("foo1")) 62 .build(); 63 Foo foo1b = Foo.newBuilder() 64 .setValue(1) 65 .addBar(Bar.newBuilder().setName("foo1")) 66 .build(); 67 Foo foo2 = Foo.newBuilder() 68 .setValue(1) 69 .addBar(Bar.newBuilder().setName("foo2")) 70 .build(); 71 72 // Check that equals is doing value rather than object equality. 73 assertEquals(foo1a, foo1b); 74 assertEquals(foo1a.hashCode(), foo1b.hashCode()); 75 76 // Check that a diffeent object is not equal. 77 assertFalse(foo1a.equals(foo2)); 78 79 // Check that two objects which have different types but the same field values are not 80 // considered to be equal. 81 Bar bar = Bar.newBuilder().setName("bar").build(); 82 BarPrime barPrime = BarPrime.newBuilder().setName("bar").build(); 83 assertFalse(bar.equals(barPrime)); 84 } 85 } 86