protobuf
java
core
src
test
java
com
google
protobuf
LazyFieldTest.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
.UnittestProto.TestAllExtensions;
34
import
protobuf_unittest
.UnittestProto.TestAllTypes;
35
import
junit.framework.TestCase;
36
42
public
class
LazyFieldTest
extends
TestCase
{
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
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
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
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
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
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
102
private
LazyField
createLazyFieldFromMessage
(
MessageLite
message
) {
103
ByteString
bytes
=
message
.toByteString();
104
return
new
LazyField
(
105
message
.getDefaultInstanceForType(),
TestUtil
.
getExtensionRegistry
(),
bytes
);
106
}
107
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
115
private
void
assertNotEqual
(Object unexpected, Object actual) {
116
assertFalse(unexpected == actual || (unexpected !=
null
&& unexpected.equals(actual)));
117
}
118
}
com.google.protobuf.LazyFieldTest.testEqualsObjectEx
void testEqualsObjectEx()
Definition:
LazyFieldTest.java:91
com.google.protobuf.TestUtil.getAllSet
static TestAllTypes getAllSet()
Definition:
core/src/test/java/com/google/protobuf/TestUtil.java:278
com.google.protobuf.LazyFieldTest.testEqualsObject
void testEqualsObject()
Definition:
LazyFieldTest.java:81
com.google.protobuf.LazyFieldLite.setValue
MessageLite setValue(MessageLite value)
Definition:
LazyFieldLite.java:233
com.google.protobuf.LazyFieldTest.testHashCode
void testHashCode()
Definition:
LazyFieldTest.java:43
com.google.protobuf.LazyField.getValue
MessageLite getValue()
Definition:
LazyField.java:66
com.google.protobuf.LazyFieldTest.testGetValueEx
void testGetValueEx()
Definition:
LazyFieldTest.java:73
com.google.protobuf.TestUtil.getAllExtensionsSet
static TestAllExtensions getAllExtensionsSet()
Definition:
core/src/test/java/com/google/protobuf/TestUtil.java:298
bytes
uint8 bytes[10]
Definition:
coded_stream_unittest.cc:153
com.google.protobuf.TestUtil.getExtensionRegistry
static ExtensionRegistryLite getExtensionRegistry()
Definition:
core/src/test/java/com/google/protobuf/TestUtil.java:1184
com.google.protobuf.LazyFieldTest.testGetValue
void testGetValue()
Definition:
LazyFieldTest.java:65
com.google.protobuf.TestUtil
Definition:
core/src/test/java/com/google/protobuf/TestUtil.java:254
com.google.protobuf.LazyFieldTest.createLazyFieldFromMessage
LazyField createLazyFieldFromMessage(MessageLite message)
Definition:
LazyFieldTest.java:102
com.google.protobuf.LazyFieldTest.testHashCodeEx
void testHashCodeEx()
Definition:
LazyFieldTest.java:54
com.google.protobuf.LazyField.hashCode
int hashCode()
Definition:
LazyField.java:71
com.google.protobuf.LazyFieldTest
Definition:
LazyFieldTest.java:42
com.google.protobuf.LazyField.equals
boolean equals(Object obj)
Definition:
LazyField.java:76
com.google.protobuf.LazyFieldTest.assertNotEqual
void assertNotEqual(Object unexpected, Object actual)
Definition:
LazyFieldTest.java:115
com.google.protobuf.LazyFieldTest.changeValue
void changeValue(LazyField lazyField)
Definition:
LazyFieldTest.java:108
protobuf_unittest
Definition:
map_test_util_impl.h:39
com.google.protobuf.LazyField
Definition:
LazyField.java:46
gmock_test_utils.TestCase
TestCase
Definition:
gmock_test_utils.py:97
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition:
glcorearb.h:2695
com.google.protobuf.MessageLite
Definition:
MessageLite.java:65
com.google.protobuf.ByteString
Definition:
ByteString.java:67
libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55