Skip to content

Commit 0965e96

Browse files
committed
test(spanner): replace fixed hashCode value assertions with contract-compliance tests in OptionsTest
`OptionsTest` contained four assertions that verified `Options#hashCode()` returns specific fixed integer values (31, 108027089, -2118248262). The Java `hashCode()` contract does not guarantee cross-JVM or cross-version stability of hash values, so testing against hardcoded numbers is brittle and misleads readers into thinking fixed values are part of the API contract. Signed-off-by: anish k <ak8686@princeton.edu>
1 parent 1e633d7 commit 0965e96

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

  • java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OptionsTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ public void allOptionsAbsent() {
152152
assertThat(options.equals(null)).isFalse();
153153
assertThat(options.equals(this)).isFalse();
154154
assertNull(options.isolationLevel());
155-
assertThat(options.hashCode()).isEqualTo(31);
155+
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
156+
Options options2 = Options.fromReadOptions();
157+
assertThat(options.equals(options2)).isTrue();
158+
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
159+
assertThat(options.hashCode()).isEqualTo(options.hashCode());
156160
}
157161

158162
@Test
@@ -175,7 +179,13 @@ public void listOptionsTest() {
175179
assertThat(options.pageSize()).isEqualTo(pageSize);
176180
assertThat(options.pageToken()).isEqualTo(pageToken);
177181
assertThat(options.filter()).isEqualTo(filter);
178-
assertThat(options.hashCode()).isEqualTo(108027089);
182+
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
183+
Options options2 =
184+
Options.fromListOptions(
185+
Options.pageSize(pageSize), Options.pageToken(pageToken), Options.filter(filter));
186+
assertThat(options.equals(options2)).isTrue();
187+
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
188+
assertThat(options.hashCode()).isEqualTo(options.hashCode());
179189
}
180190

181191
@Test
@@ -696,7 +706,11 @@ public void updateOptionsTest() {
696706
assertEquals("tag: " + tag + " ", options.toString());
697707
assertTrue(options.hasTag());
698708
assertThat(options.tag()).isEqualTo(tag);
699-
assertThat(options.hashCode()).isEqualTo(-2118248262);
709+
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
710+
Options options2 = Options.fromUpdateOptions(Options.tag(tag));
711+
assertThat(options.equals(options2)).isTrue();
712+
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
713+
assertThat(options.hashCode()).isEqualTo(options.hashCode());
700714
}
701715

702716
@Test
@@ -728,7 +742,11 @@ public void transactionOptionsTest() {
728742
assertEquals("tag: " + tag + " ", options.toString());
729743
assertTrue(options.hasTag());
730744
assertThat(options.tag()).isEqualTo(tag);
731-
assertThat(options.hashCode()).isEqualTo(-2118248262);
745+
// Verify hashCode contract: equal objects must have equal hashCodes, and hashCode is stable.
746+
Options options2 = Options.fromTransactionOptions(Options.tag(tag));
747+
assertThat(options.equals(options2)).isTrue();
748+
assertThat(options.hashCode()).isEqualTo(options2.hashCode());
749+
assertThat(options.hashCode()).isEqualTo(options.hashCode());
732750
}
733751

734752
@Test

0 commit comments

Comments
 (0)