jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r1469259 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
Date Thu, 18 Apr 2013 10:02:28 GMT
Author: angela
Date: Thu Apr 18 10:02:28 2013
New Revision: 1469259

URL: http://svn.apache.org/r1469259
Log:
OAK-523 : AuthorizableImpl: add equals/hashCode implementation

Added:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java?rev=1469259&r1=1469258&r2=1469259&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
Thu Apr 18 10:02:28 2013
@@ -149,11 +149,12 @@ abstract class AuthorizableImpl implemen
     @Override
     public int hashCode() {
         if (hashCode == 0) {
-            // FIXME OAK-523: add proper hash-code generation taking repo/workspace/tree-identifier
into account
             StringBuilder sb = new StringBuilder();
             sb.append(isGroup() ? "group:" : "user:");
             sb.append(':');
             sb.append(id);
+            sb.append('_');
+            sb.append(userManager.hashCode());
             hashCode = sb.toString().hashCode();
         }
         return hashCode;
@@ -166,8 +167,7 @@ abstract class AuthorizableImpl implemen
         }
         if (obj instanceof AuthorizableImpl) {
             AuthorizableImpl otherAuth = (AuthorizableImpl) obj;
-            // FIXME OAK-523: make sure 2 authorizables are based on the same tree/node object
-            return isGroup() == otherAuth.isGroup() && id.equals(otherAuth.id);
+            return isGroup() == otherAuth.isGroup() && id.equals(otherAuth.id) &&
userManager.equals(otherAuth.userManager);
         }
         return false;
     }

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java?rev=1469259&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/AuthorizableImplTest.java
Thu Apr 18 10:02:28 2013
@@ -0,0 +1,136 @@
+/*
+ * 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.oak.security.user;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.Group;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.api.security.user.UserManager;
+import org.apache.jackrabbit.oak.AbstractSecurityTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+/**
+ * Implementation specific tests for {@code AuthorizableImpl} and subclasses.
+ */
+public class AuthorizableImplTest extends AbstractSecurityTest {
+
+    private UserManager userMgr;
+    private User testUser;
+    private Group testGroup;
+
+    @Override
+    @Before
+    public void before() throws Exception {
+        super.before();
+
+        userMgr = getUserManager();
+        testUser = userMgr.createUser("testUser", "pw");
+        testGroup = userMgr.createGroup("testGroup");
+        root.commit();
+    }
+
+    @Override
+    public void after() throws Exception {
+        try {
+            if (testUser != null) {
+                testUser.remove();
+            }
+            if (testGroup != null) {
+                testGroup.remove();
+            }
+            root.commit();
+        } finally {
+            super.after();
+        }
+    }
+
+    @Test
+    public void testEqualAuthorizables() throws Exception {
+        Authorizable user = userMgr.getAuthorizable(testUser.getID());
+        Authorizable group = userMgr.getAuthorizable(testGroup.getID());
+
+        Map<Authorizable, Authorizable> equalAuthorizables = new HashMap<Authorizable,
Authorizable>();
+        equalAuthorizables.put(testUser, testUser);
+        equalAuthorizables.put(testGroup, testGroup);
+        equalAuthorizables.put(user, user);
+        equalAuthorizables.put(group, group);
+        equalAuthorizables.put(testUser, user);
+        equalAuthorizables.put(testGroup, group);
+
+        for (Map.Entry entry : equalAuthorizables.entrySet()) {
+            assertEquals(entry.getKey(), entry.getValue());
+            assertEquals(entry.getValue(), entry.getKey());
+        }
+    }
+
+    @Test
+    public void testNotEqualAuthorizables() throws Exception {
+        UserManager otherUserManager = getSecurityProvider().getUserConfiguration().getUserManager(root,
getNamePathMapper());
+        Authorizable user = otherUserManager.getAuthorizable(testUser.getID());
+        Authorizable group = otherUserManager.getAuthorizable(testGroup.getID());
+
+        Map<Authorizable, Authorizable> notEqual = new HashMap<Authorizable, Authorizable>();
+        notEqual.put(testUser, testGroup);
+        notEqual.put(user, group);
+        notEqual.put(testUser, user);
+        notEqual.put(testGroup, group);
+
+        for (Map.Entry entry : notEqual.entrySet()) {
+            assertFalse(entry.getKey().equals(entry.getValue()));
+            assertFalse(entry.getValue().equals(entry.getKey()));
+        }
+    }
+
+    @Test
+    public void testHashCode() throws Exception {
+        Authorizable user = userMgr.getAuthorizable(testUser.getID());
+        Authorizable group = userMgr.getAuthorizable(testGroup.getID());
+
+        Map<Authorizable, Authorizable> sameHashCode = new HashMap<Authorizable,
Authorizable>();
+        sameHashCode.put(testUser, testUser);
+        sameHashCode.put(testGroup, testGroup);
+        sameHashCode.put(user, user);
+        sameHashCode.put(group, group);
+        sameHashCode.put(testUser, user);
+        sameHashCode.put(testGroup, group);
+
+        for (Map.Entry entry : sameHashCode.entrySet()) {
+            assertEquals(entry.getKey().hashCode(), entry.getValue().hashCode());
+        }
+
+        UserManager otherUserManager = getSecurityProvider().getUserConfiguration().getUserManager(root,
getNamePathMapper());
+        user = otherUserManager.getAuthorizable(testUser.getID());
+        group = otherUserManager.getAuthorizable(testGroup.getID());
+
+        Map<Authorizable, Authorizable> notSameHashCode = new HashMap<Authorizable,
Authorizable>();
+        notSameHashCode.put(testUser, testGroup);
+        notSameHashCode.put(user, group);
+        notSameHashCode.put(testUser, user);
+        notSameHashCode.put(testGroup, group);
+
+        for (Map.Entry entry : notSameHashCode.entrySet()) {
+            assertFalse(entry.getKey().hashCode() == entry.getValue().hashCode());
+        }
+    }
+}
\ No newline at end of file



Mime
View raw message