click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r922837 - in /click/trunk/click: documentation/docs/roadmap-changes.html mock/src/org/apache/click/servlet/MockPrincipal.java mock/src/org/apache/click/servlet/MockRequest.java mock/test/org/apache/click/MockPrincipalTest.java
Date Sun, 14 Mar 2010 13:20:49 GMT
Author: sabob
Date: Sun Mar 14 13:20:48 2010
New Revision: 922837

URL: http://svn.apache.org/viewvc?rev=922837&view=rev
Log:
added support for mock principals and roles. CLK-585

Added:
    click/trunk/click/mock/src/org/apache/click/servlet/MockPrincipal.java
    click/trunk/click/mock/test/org/apache/click/MockPrincipalTest.java
Modified:
    click/trunk/click/documentation/docs/roadmap-changes.html
    click/trunk/click/mock/src/org/apache/click/servlet/MockRequest.java

Modified: click/trunk/click/documentation/docs/roadmap-changes.html
URL: http://svn.apache.org/viewvc/click/trunk/click/documentation/docs/roadmap-changes.html?rev=922837&r1=922836&r2=922837&view=diff
==============================================================================
--- click/trunk/click/documentation/docs/roadmap-changes.html (original)
+++ click/trunk/click/documentation/docs/roadmap-changes.html Sun Mar 14 13:20:48 2010
@@ -154,7 +154,12 @@ includes improved Ajax support and @Bind
         and <a href="click-api/org/apache/click/Context.html#hasRequestAttribute(java.lang.String)">Context.hasRequestAttribute(String)</a>.
       </li>
       <li class="change">
-        Removed Click cores dependency on Velocity. This issue was raised by by Andrey Rybin
+        Added mock support for user <a href="mock-api/org/apache/click/servlet/MockPrincipal.html">principals</a>
+        and roles. This issue was raised and fixed by Sven Pfeiffer
+        [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-585">CLK-585</a>].
+      </li>
+      <li class="change">
+        Removed Click core dependency on Velocity. This issue was raised by by Andrey Rybin
         [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-606">CLK-606</a>].
       </li>
       <li class="change">

Added: click/trunk/click/mock/src/org/apache/click/servlet/MockPrincipal.java
URL: http://svn.apache.org/viewvc/click/trunk/click/mock/src/org/apache/click/servlet/MockPrincipal.java?rev=922837&view=auto
==============================================================================
--- click/trunk/click/mock/src/org/apache/click/servlet/MockPrincipal.java (added)
+++ click/trunk/click/mock/src/org/apache/click/servlet/MockPrincipal.java Sun Mar 14 13:20:48
2010
@@ -0,0 +1,152 @@
+/*
+ * 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.click.servlet;
+
+import java.security.Principal;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Mock implementation of a user {@link java.security.Principal principal}.
+ * This class also provides convenient methods for specifying the user principal
+ * roles.
+ * <p/>
+ * Example usage with MockContext:
+ * <pre class="prettyprint">
+ * MockContext mockContext = MockContext.initContext();
+ *
+ * // Create a new user principal with the roles "user" and "manager"
+ * MockPrincipal principal = new MockPrincipal("Bob", "user", "manager");
+ *
+ * // Set the user principal on the request object
+ * mockContext.getMockRequest().setUserPrincipal(principal); </pre>
+ *
+ * <p/>
+ * Example usage with MockContainer:
+ * <pre class="prettyprint">
+ * MockContainer container = new MockContainer("c:/dev/myapp/web");
+ *
+ * // Create a new user principal with the roles "user" and "manager"
+ * MockPrincipal principal = new MockPrincipal("Bob", "user", "manager");
+ *
+ * // Set the user principal on the request object
+ * container.getRequest().setUserPrincipal(principal); </pre>
+ */
+public class MockPrincipal implements Principal {
+
+    // Variables --------------------------------------------------------------
+
+    /** The principal name. */
+    private String name;
+
+    /** The principal roles. */
+    private Set<String> roles;
+
+    // Constructors -----------------------------------------------------------
+
+    /**
+     * Constructs a new MockPrincipal instance.
+     */
+    public MockPrincipal() {
+    }
+
+    /**
+     * Constructs a new MockPrincipal instance for the given name.
+     *
+     * @param name the name of the principal
+     */
+    public MockPrincipal(String name) {
+        setName(name);
+    }
+
+    /**
+     * Constructs a new MockPrincipal instance for the given name and roles.
+     *
+     * @param name the name of the principal
+     * @param roles the principal roles
+     */
+    public MockPrincipal(String name, Set<String> roles) {
+        setName(name);
+        setRoles(roles);
+    }
+
+    /**
+     * Constructs a new MockPrincipal instance for the given name and roles.
+     *
+     * @param name the name of the principal
+     * @param roles the principal roles
+     */
+    public MockPrincipal(String name, String... roles) {
+        setName(name);
+        addRoles(roles);
+    }
+
+    // Public methods ---------------------------------------------------------
+
+    /**
+     * Returns the name of this principal.
+     *
+     * @return the name of this principal.
+     */
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Set the name of this principal.
+     *
+     * @param name the name of the princpal
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * Returns the roles of this principal.
+     *
+     * @return the roles of this principal.
+     */
+    public Set<String> getRoles() {
+        if (roles == null) {
+             roles = new HashSet<String>();
+        }
+        return roles;
+    }
+
+    /**
+     * Sets the roles of this principal.
+     *
+     * @param roles set the roles of this principal.
+     */
+    public void setRoles(Set<String> roles) {
+        this.roles = roles;
+    }
+
+    /**
+     * Add the roles of this principal.
+     *
+     * @param roles set the roles of this principal.
+     */
+    public void addRoles(String... roles) {
+        for (String role : roles) {
+            getRoles().add(role);
+        }
+    }
+}

Modified: click/trunk/click/mock/src/org/apache/click/servlet/MockRequest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/mock/src/org/apache/click/servlet/MockRequest.java?rev=922837&r1=922836&r2=922837&view=diff
==============================================================================
--- click/trunk/click/mock/src/org/apache/click/servlet/MockRequest.java (original)
+++ click/trunk/click/mock/src/org/apache/click/servlet/MockRequest.java Sun Mar 14 13:20:48
2010
@@ -70,6 +70,9 @@ public class MockRequest implements Http
     /** File attachment boundary indicator. */
     private static final String BOUNDARY = "--abcdefgABCDEFG";
 
+    /** The REMOTE_USER header. */
+    public static final String REMOTE_USER = "REMOTE_USER";
+
     // -------------------------------------------------------- Variables
 
     /** The request default locale. */
@@ -106,8 +109,7 @@ public class MockRequest implements Http
      * Map of uploaded files, where the fieldname is the key and uploaded file
      * is the value.
      */
-    private Map /*<String, UploadedFile>*/ uploadedFiles =
-        new HashMap /*<String, UploadedFile>*/();
+    private Map <String, UploadedFile> uploadedFiles = new HashMap <String, UploadedFile>();
 
     /**
      * Indicates if this request is multipart (contains binary attachment) or
@@ -142,6 +144,9 @@ public class MockRequest implements Http
     /** A random number generator to create unique session id's. */
     private Random random = new Random();
 
+    /** The user principal. */
+    private Principal userPrincipal;
+
     /**
      * Create new MockRequest.
      */
@@ -278,8 +283,8 @@ public class MockRequest implements Http
     /**
      * Add a header to the request.
      *
-     * @param name The name of the header to add
-     * @param value The value
+     * @param name the name of the header to add
+     * @param value the value
      */
     public void addHeader(String name, String value) {
         List list = (List) headers.get(name);
@@ -291,6 +296,30 @@ public class MockRequest implements Http
     }
 
     /**
+     * Set request header value. The existing header value will be replaced.
+     *
+     * @param name the name of the header to set
+     * @param value the header value
+     */
+    public void setHeader(String name, String value) {
+        setHeader(name, new String[] {value});
+    }
+
+    /**
+     * Set request header values. The existing header values will be replaced.
+     *
+     * @param name the name of the header to set
+     * @param values the header values
+     */
+    public void setHeader(String name, String... values) {
+        List list = new ArrayList(values.length);
+        headers.put(name, list);
+        for (String value : values) {
+            list.add(value);
+        }
+    }
+
+    /**
      * Get an attribute.
      *
      * @param name The attribute name
@@ -698,12 +727,20 @@ public class MockRequest implements Http
     }
 
     /**
-     * Get the name of the remote user from the REMOTE_USER header.
+     * Return the name of the {@link #userPrincipal} if set, otherwise
+     * the value of the {@value #REMOTE_USER} header.
+     * <p/>
+     * To set the remote user, create an instance of a {@link MockPrincipal}
+     * and set it on the request through the method
+     * {@link #setUserPrincipal(java.security.Principal)}.
      *
-     * @return The name of the remote user
+     * @return the name of the remote user
      */
     public String getRemoteUser() {
-        return getHeader("REMOTE_USER");
+        if (userPrincipal != null) {
+            return userPrincipal.getName();
+        }
+        return getHeader(REMOTE_USER);
     }
 
     /**
@@ -962,27 +999,42 @@ public class MockRequest implements Http
     }
 
     /**
-     * Get the user principal.
+     * Get the user principal. If no user principal was set this method will
+     * create a user principal for the {@link #getRemoteUser()}.
      *
-     * @return A user principal
+     * @return the user principal
      */
     public Principal getUserPrincipal() {
-        final String user = getRemoteUser();
-        if (user == null) {
-            return null;
-        } else {
-            return new Principal() {
+        if (userPrincipal == null) {
+            final String user = getRemoteUser();
 
-                public String getName() {
-                    return user;
-                }
-            };
+            if (user == null) {
+                return null;
+            } else {
+                userPrincipal = new MockPrincipal() {
+
+                    @Override
+                    public String getName() {
+                        return user;
+                    }
+                };
+            }
         }
+        return userPrincipal;
+    }
+
+    /**
+     * Set the user principal.
+     *
+     * @param userPrincipal the user principal
+     */
+    public void setUserPrincipal(Principal userPrincipal) {
+        this.userPrincipal = userPrincipal;
     }
 
     /**
      * @return True if there has been added files to this request using
-     * {@link #addFile(String, File, String)}
+     * {@link #addFile(String, File, String)}.
      */
     public boolean hasUploadedFiles() {
         return uploadedFiles != null;
@@ -1055,12 +1107,21 @@ public class MockRequest implements Http
     }
 
     /**
-     * NOT IMPLEMENTED.
+     * Returns true if the {@link #getUserPrincipal() authenticated user} is
+     * included in the given role, false otherwise.
+     * <p/>
+     * To mock up roles for a user, create a {@link MockPrincipal user principal}
+     * and set the necessary roles. See {@link MockPrincipal} for an example.
      *
-     * @param name The role name
-     * @return Always false
+     * @param role the role name
+     * @return true if the user is included in the specified role, false
+     * otherwise
      */
-    public boolean isUserInRole(String name) {
+    public boolean isUserInRole(String role) {
+        Principal principal = getUserPrincipal();
+        if (principal instanceof MockPrincipal) {
+            return ((MockPrincipal) principal).getRoles().contains(role);
+        }
         return false;
     }
 

Added: click/trunk/click/mock/test/org/apache/click/MockPrincipalTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/mock/test/org/apache/click/MockPrincipalTest.java?rev=922837&view=auto
==============================================================================
--- click/trunk/click/mock/test/org/apache/click/MockPrincipalTest.java (added)
+++ click/trunk/click/mock/test/org/apache/click/MockPrincipalTest.java Sun Mar 14 13:20:48
2010
@@ -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.click;
+
+import junit.framework.TestCase;
+import org.apache.click.servlet.MockPrincipal;
+import org.apache.click.servlet.MockRequest;
+
+/**
+ * MockPrincipal tests.
+ */
+public class MockPrincipalTest extends TestCase {
+
+    /**
+     * Check that request's getRemoteUser lists the MockPrincipal name
+     */
+    public void testGetRemoteUser() {
+        String user = "Bob";
+
+        MockContext mockContext = MockContext.initContext();
+        MockRequest mockRequest = mockContext.getMockRequest();
+
+        // Create a new user principal with the roles "user" and "manager"
+        MockPrincipal principal = new MockPrincipal(user);
+
+        // Set the user principal on the request object
+        mockRequest.setUserPrincipal(principal);
+
+        // Check user is in "user" and "manager" roles
+        assertEquals(user, mockRequest.getRemoteUser());
+    }
+
+    /**
+     * Check that request's isUserInRole works properly.
+     *
+     * CLK-585
+     */
+    public void testUserInRole() {
+
+        MockContext mockContext = MockContext.initContext();
+        MockRequest mockRequest = mockContext.getMockRequest();
+
+        // Create a new user principal with the roles "user" and "manager"
+        MockPrincipal principal = new MockPrincipal("Bob", "user", "manager");
+
+        // Set the user principal on the request object
+        mockRequest.setUserPrincipal(principal);
+
+        // Check user is in "user" and "manager" roles
+        assertTrue(mockRequest.isUserInRole("user"));
+        assertTrue(mockRequest.isUserInRole("manager"));
+
+        // Check user is not in "Manager" role (check is case sensitivity)
+        assertFalse(mockRequest.isUserInRole("Manager"));
+
+        // Check user not in QA role
+        assertFalse(mockRequest.isUserInRole("QA"));
+    }
+}



Mime
View raw message