[java-opensaml] branch master updated: OSJ-201 - fix RecordId equals

Daniel Fisher dfisher at vt.edu
Tue Apr 4 12:52:58 EDT 2017


This is an automated email from the git hooks/post-receive script.

dfisher pushed a commit to branch master
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=0f99293b3059b25a5fd7eeed0c09b2ee30646ec1

The following commit(s) were added to refs/heads/master by this push:
       new  0f99293   OSJ-201 - fix RecordId equals
0f99293 is described below

commit 0f99293b3059b25a5fd7eeed0c09b2ee30646ec1
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 363ad93..0fa502e 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
@@ -142,6 +142,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
@@ -154,4 +187,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