You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ck...@apache.org on 2009/05/27 09:02:43 UTC

svn commit: r779032 - in /jackrabbit/trunk/jackrabbit-jca/src: main/java/org/apache/jackrabbit/jca/JCAConnectionRequestInfo.java test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java

Author: ckoell
Date: Wed May 27 07:02:42 2009
New Revision: 779032

URL: http://svn.apache.org/viewvc?rev=779032&view=rev
Log:
JCR-1665: In JCAConnectionRequestInfo, equals() and hashCode() implementations are inconsistent

Added:
    jackrabbit/trunk/jackrabbit-jca/src/test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java   (with props)
Modified:
    jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAConnectionRequestInfo.java

Modified: jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAConnectionRequestInfo.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAConnectionRequestInfo.java?rev=779032&r1=779031&r2=779032&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAConnectionRequestInfo.java (original)
+++ jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAConnectionRequestInfo.java Wed May 27 07:02:42 2009
@@ -72,7 +72,7 @@
      */
     public int hashCode() {
         int hash1 = workspace != null ? workspace.hashCode() : 0;
-        int hash2 = creds != null ? creds.hashCode() : 0;
+        int hash2 = creds != null ? computeCredsHashCode(creds) : 0;
         return hash1 ^ hash2;
     }
 
@@ -170,4 +170,33 @@
 
         return map;
     }
+
+    /**
+     * Returns Credentials instance hash code. Handles instances of
+     * SimpleCredentials in a special way.
+     */
+    private int computeCredsHashCode(Credentials c) {
+        if (c instanceof SimpleCredentials) {
+            return computeSimpleCredsHashCode((SimpleCredentials) c);
+        }
+        return c.hashCode();
+    }
+
+    /**
+     * Computes hash code of a SimpleCredentials instance. Ignores its own
+     * hashCode() method because it's not overridden in SimpleCredentials.
+     */
+    private int computeSimpleCredsHashCode(SimpleCredentials c) {
+        String userID = c.getUserID();
+        char[] password = c.getPassword();
+        Map m = getAttributeMap(c);
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((userID == null) ? 0 : userID.hashCode());
+        for (int i = 0; i < password.length; i++) {
+            result = prime * result + password[i];
+        }
+        result = prime * result + ((m == null) ? 0 : m.hashCode());
+        return result;
+    }
 }

Added: jackrabbit/trunk/jackrabbit-jca/src/test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jca/src/test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java?rev=779032&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jca/src/test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java (added)
+++ jackrabbit/trunk/jackrabbit-jca/src/test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java Wed May 27 07:02:42 2009
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.jca.test;
+
+import org.apache.jackrabbit.jca.JCAConnectionRequestInfo;
+
+import javax.jcr.SimpleCredentials;
+
+import java.util.HashMap;
+
+/**
+ * This case executes tests on the connection request info.
+ */
+public final class ConnectionRequestInfoTest
+        extends AbstractTestCase {
+
+    private SimpleCredentials creds1 = new SimpleCredentials("user", "password".toCharArray());
+    private SimpleCredentials creds2 = new SimpleCredentials("user", "password".toCharArray());
+    private SimpleCredentials creds3 = new SimpleCredentials("another_user", "password".toCharArray());
+    private JCAConnectionRequestInfo info1 = new JCAConnectionRequestInfo(creds1, "default");
+    private JCAConnectionRequestInfo info2 = new JCAConnectionRequestInfo(creds2, "default");
+    private JCAConnectionRequestInfo info3 = new JCAConnectionRequestInfo(creds3, "default");
+
+    /**
+     * Test the JCAConnectionRequestInfo equals() method.
+     */
+    public void testEquals() throws Exception {
+        assertEquals("Object must be equal to itself", info1, info1);
+        assertEquals("Infos with the same auth data must be equal", info1, info2);
+        assertTrue("Infos with different auth data must not be equal", !info1.equals(info3));
+    }
+
+    /**
+     * Test the JCAConnectionRequestInfo hashCode() method.
+     */
+    public void testHashCode() throws Exception {
+        assertEquals("Object must be equal to itself", info1.hashCode(), info1.hashCode());
+        assertEquals("Infos with the same auth data must have same hashCode", info1.hashCode(), info2.hashCode());
+        assertTrue("Infos with different auth data must not have same hashCode", info1.hashCode() != info3.hashCode());
+    }
+
+    /**
+     * Tests that JCAConnectionRequestInfo works as a HashMap key correctly.
+     */
+    public void testPutToHashMap() throws Exception {
+        HashMap map = new HashMap();
+        map.put(info1, new Object());
+        assertTrue("Map must contain the info", map.containsKey(info2));
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-jca/src/test/java/org/apache/jackrabbit/jca/test/ConnectionRequestInfoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native