james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject james-project git commit: JAMES-1839 SimpleGroupMembershipResolver should not fail badly on null user
Date Wed, 09 Nov 2016 15:50:22 GMT
Repository: james-project
Updated Branches:
  refs/heads/master e20b50b47 -> aec24c4e9


JAMES-1839 SimpleGroupMembershipResolver should not fail badly on null user


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/aec24c4e
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/aec24c4e
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/aec24c4e

Branch: refs/heads/master
Commit: aec24c4e98a78af4d41dc7557f321205fd621e49
Parents: e20b50b
Author: Laura Royet <lroyet@linagora.com>
Authored: Thu Nov 3 11:08:33 2016 +0100
Committer: Laura Royet <lroyet@linagora.com>
Committed: Wed Nov 9 10:53:21 2016 +0100

----------------------------------------------------------------------
 .../acl/SimpleGroupMembershipResolver.java      | 23 +++---
 .../acl/SimpleGroupMembershipResolverTest.java  | 75 ++++++++++++++++++++
 2 files changed, 87 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/aec24c4e/mailbox/api/src/main/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolver.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolver.java
b/mailbox/api/src/main/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolver.java
index 04dc45a..6924757 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolver.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolver.java
@@ -23,6 +23,9 @@ package org.apache.james.mailbox.acl;
 import java.util.HashSet;
 import java.util.Set;
 
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+
 
 /**
  * In memory {@link GroupMembershipResolver} implementation. There is no
@@ -31,38 +34,36 @@ import java.util.Set;
  */
 public class SimpleGroupMembershipResolver implements GroupMembershipResolver {
 
-    private static class Membership {
+    public static class Membership {
         private final String group;
-        private final int hash;
         private final String user;
 
         public Membership(String user, String group) {
-            super();
             this.group = group;
             this.user = user;
-
-            final int PRIME = 31;
-            this.hash = PRIME * this.group.hashCode() + this.user.hashCode();
         }
 
         @Override
         public boolean equals(Object o) {
             if (o instanceof Membership) {
-                Membership other = (Membership) o;
-                return this.group == other.group || (this.group != null && this.group.equals(other.group))
&& this.user == other.user || (this.user != null && this.user.equals(other.user));
-
+                Membership that = (Membership) o;
+                
+                return Objects.equal(this.user, that.user) && Objects.equal(this.group,
that.group);
             }
             return false;
         }
 
         @Override
         public int hashCode() {
-            return hash;
+            return Objects.hashCode(group, user);
         }
 
         @Override
         public String toString() {
-            return group + ": " + user;
+            return MoreObjects.toStringHelper(this)
+                .add("group", group)
+                .add("user", user)
+                .toString();
         }
 
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/aec24c4e/mailbox/api/src/test/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolverTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolverTest.java
b/mailbox/api/src/test/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolverTest.java
new file mode 100644
index 0000000..f77dbbb
--- /dev/null
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/acl/SimpleGroupMembershipResolverTest.java
@@ -0,0 +1,75 @@
+/*
+ *   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.james.mailbox.acl;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class SimpleGroupMembershipResolverTest {
+
+    private SimpleGroupMembershipResolver simpleGroupMembershipResolver;
+
+    @Before
+    public void setUp() {
+       simpleGroupMembershipResolver = new SimpleGroupMembershipResolver();
+    }
+
+    @Test
+    public void isMemberShouldReturnFalseWhenEmptyResolver() throws Exception {
+        //When
+        boolean actual = simpleGroupMembershipResolver.isMember("user", "group");
+        //Then
+        assertThat(actual).isFalse();
+    }
+
+    @Test
+    public void isMemberShouldReturnTrueWhenTheSearchedMembershipIsPresent() throws Exception
{
+        //Given
+        simpleGroupMembershipResolver.addMembership("group", "user");
+        //When
+        boolean actual = simpleGroupMembershipResolver.isMember("user", "group");
+        //Then
+        assertThat(actual).isTrue();
+    }
+
+    @Test
+    public void addMembershipShouldAddAMembershipWhenNonNullUser() throws Exception {
+        //When
+        simpleGroupMembershipResolver.addMembership("group", "user");
+        boolean actual = simpleGroupMembershipResolver.isMember("user", "group");
+        //Then
+        assertThat(actual).isTrue();
+    }
+
+    @Test
+    public void addMembershipShouldAddAMembershipWithANullUser() throws Exception {
+        //Given
+        String userAdded = null;
+        //When
+        simpleGroupMembershipResolver.addMembership("group", userAdded);
+        boolean actual = simpleGroupMembershipResolver.isMember(userAdded, "group");
+        //Then
+        assertThat(actual).isTrue();
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message