LiteEqualsAndHashTest.java
Go to the documentation of this file.
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 
46 public class LiteEqualsAndHashTest extends TestCase {
47 
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  // overridden 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 
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 
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 
116  private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) {
117  assertFalse(o1.equals(o2));
118  assertFalse(o1.hashCode() == o2.hashCode());
119  }
120 
121  public void testRecursiveHashcode() {
122  // This tests that we don't infinite loop.
123  TestRecursiveOneof.getDefaultInstance().hashCode();
124  }
125 }
com.google.protobuf.LiteEqualsAndHashTest.testOneofEquals
void testOneofEquals()
Definition: LiteEqualsAndHashTest.java:88
bar
Definition: googletest-output-test_.cc:550
com.google.protobuf.LiteEqualsAndHashTest.testEquals
void testEquals()
Definition: LiteEqualsAndHashTest.java:48
com.google.protobuf.LiteEqualsAndHashTest.testEqualsAndHashCodeWithUnknownFields
void testEqualsAndHashCodeWithUnknownFields()
Definition: LiteEqualsAndHashTest.java:98
com.google.protobuf.LiteEqualsAndHashTest.assertEqualsAndHashCodeAreFalse
void assertEqualsAndHashCodeAreFalse(Object o1, Object o2)
Definition: LiteEqualsAndHashTest.java:116
benchmark::o1
@ o1
Definition: benchmark.h:375
com.google.protobuf.LiteEqualsAndHashTest.testRecursiveHashcode
void testRecursiveHashcode()
Definition: LiteEqualsAndHashTest.java:121
protobuf_unittest
Definition: map_test_util_impl.h:39
com.google.protobuf.LiteEqualsAndHashTest
Definition: LiteEqualsAndHashTest.java:46
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55