[java-opensaml] 13/24: OSJ-201 - fix RecordId equals
Brent Putman
putmanb at georgetown.edu
Wed Sep 27 16:46:07 EDT 2017
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch maint-3.3
in repository java-opensaml.
View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=c2d305e540f2d3f3e2592137116558301ac076aa
commit c2d305e540f2d3f3e2592137116558301ac076aa
Author: Daniel Fisher <dfisher at vt.edu>
AuthorDate: Tue Apr 4 12:50:24 2017 -0400
OSJ-201 - fix RecordId equals
Use Java 7 Objects#hash for calculating hashCode.
Add unit test for sanity check.
---
.../opensaml/storage/impl/JPAStorageRecord.java | 15 ++++++----
.../storage/impl/JPAStorageServiceTest.java | 35 +++++++++++++++++++++-
2 files changed, 44 insertions(+), 6 deletions(-)
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JPAStorageRecord.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JPAStorageRecord.java
index 5965b23..884f2a4 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JPAStorageRecord.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/JPAStorageRecord.java
@@ -19,6 +19,7 @@ package org.opensaml.storage.impl;
import java.io.Serializable;
+import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.Column;
@@ -212,16 +213,20 @@ public class JPAStorageRecord extends MutableStorageRecord {
/** {@inheritDoc} */
@Override
public int hashCode() {
- int hc = 31;
- hc += context != null ? context.hashCode() : 0;
- hc += key != null ? key.hashCode() : 0;
- return hc;
+ return Objects.hash(context, key);
}
/** {@inheritDoc} */
@Override
public boolean equals(final Object o) {
- return o != null && (this == o || getClass() == o.getClass() && hashCode() == o.hashCode());
+ if (o == this) {
+ return true;
+ }
+ if (o instanceof RecordId) {
+ final RecordId id = (RecordId) o;
+ return context.equals(id.context) && key.equals(id.key);
+ }
+ return false;
}
/** {@inheritDoc} */
diff --git a/opensaml-storage-impl/src/test/java/org/opensaml/storage/impl/JPAStorageServiceTest.java b/opensaml-storage-impl/src/test/java/org/opensaml/storage/impl/JPAStorageServiceTest.java
index 029c09a..e73a211 100644
--- a/opensaml-storage-impl/src/test/java/org/opensaml/storage/impl/JPAStorageServiceTest.java
+++ b/opensaml-storage-impl/src/test/java/org/opensaml/storage/impl/JPAStorageServiceTest.java
@@ -144,6 +144,39 @@ public class JPAStorageServiceTest extends StorageServiceTest {
Assert.assertFalse(result, "createString should have failed");
}
+ @Test
+ public void keyCollision() throws IOException {
+ shared.create("unit_test", "dlo1", "value", null);
+ shared.create("unit_test", "dn11", "value", null);
+ StorageRecord rec1 = shared.read("unit_test", "dlo1");
+ StorageRecord rec2 = shared.read("unit_test", "dn11");
+ Assert.assertNotNull(rec1);
+ Assert.assertNotNull(rec2);
+ Assert.assertNotEquals(rec1, rec2);
+
+ shared.update("unit_test", "dlo1", "value2", null);
+ shared.update("unit_test", "dn11", "value2", null);
+ rec1 = shared.read("unit_test", "dlo1");
+ rec2 = shared.read("unit_test", "dn11");
+ Assert.assertNotNull(rec1);
+ Assert.assertNotNull(rec2);
+ Assert.assertNotEquals(rec1, rec2);
+
+ Assert.assertEquals(2, storageService.readAll().size());
+ Assert.assertEquals(2, storageService.readAll("unit_test").size());
+
+ shared.delete("unit_test", "dlo1");
+ rec1 = shared.read("unit_test", "dlo1");
+ rec2 = shared.read("unit_test", "dn11");
+ Assert.assertNull(rec1);
+ Assert.assertNotNull(rec2);
+ shared.delete("unit_test", "dn11");
+ rec1 = shared.read("unit_test", "dlo1");
+ rec2 = shared.read("unit_test", "dn11");
+ Assert.assertNull(rec1);
+ Assert.assertNull(rec2);
+ }
+
@Test(enabled = false)
public void largeValue() throws IOException {
// hsqldb defaults LOB length to 255 chars; disabled for now
@@ -156,4 +189,4 @@ public class JPAStorageServiceTest extends StorageServiceTest {
Assert.assertNotNull(rec);
Assert.assertEquals(sb.toString(), rec.getValue());
}
-}
\ No newline at end of file
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list