incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r164176 - /incubator/graffito/trunk/components/src/test/org/apache/portals/graffito/security/impl/TestPermissions.java
Date Fri, 22 Apr 2005 05:28:01 GMT
Author: clombart
Date: Thu Apr 21 22:28:00 2005
New Revision: 164176

URL: http://svn.apache.org/viewcvs?rev=164176&view=rev
Log:
UPdate unit tests

Added:
    incubator/graffito/trunk/components/src/test/org/apache/portals/graffito/security/impl/TestPermissions.java

Added: incubator/graffito/trunk/components/src/test/org/apache/portals/graffito/security/impl/TestPermissions.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/components/src/test/org/apache/portals/graffito/security/impl/TestPermissions.java?rev=164176&view=auto
==============================================================================
--- incubator/graffito/trunk/components/src/test/org/apache/portals/graffito/security/impl/TestPermissions.java
(added)
+++ incubator/graffito/trunk/components/src/test/org/apache/portals/graffito/security/impl/TestPermissions.java
Thu Apr 21 22:28:00 2005
@@ -0,0 +1,212 @@
+/* Copyright 2004 Apache Software Foundation
+ *
+ * Licensed 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.portals.graffito.security.impl;
+
+
+
+import java.security.Permission;
+import java.security.Permissions;
+import java.util.Enumeration;
+import java.util.Iterator;
+
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.jetspeed.security.SecurityException;
+import org.apache.jetspeed.security.UserPrincipal;
+
+import org.apache.jetspeed.security.impl.UserPrincipalImpl;
+import org.apache.portals.graffito.context.CmsRequestContext;
+import org.apache.portals.graffito.model.CmsPermission;
+import org.apache.portals.graffito.security.CmsAccessController;
+import org.apache.portals.graffito.security.impl.AbstractSecurityTestcase;
+import org.apache.portals.graffito.security.impl.CmsPermissionImpl;
+
+/**
+ * This unit test comes from Jetspeed 2. It used to check if the J2 security
+ * components are well integrated with Graffito
+ *  
+ */
+
+public class TestPermissions extends AbstractSecurityTestcase
+{
+
+    private CmsAccessController cmsAccessController;
+
+    /**
+     * @see junit.framework.TestCase#setUp()
+     */
+    public void setUp() throws Exception
+    {
+        super.setUp();        
+        try
+        {
+            cmsAccessController = (CmsAccessController) ctx.getBean("org.apache.portals.graffito.security.CmsAccessController");
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Fail to run due to an exception : " + e);
+        }
+
+    }
+
+    /**
+     * @see junit.framework.TestCase#tearDown()
+     */
+    public void tearDown() throws Exception
+    {
+
+        // Logout
+        try
+        {
+            loginContext.logout();
+        }
+        catch (LoginException le)
+        {
+            le.printStackTrace();
+            assertTrue("\t\t[TestRdbmsPolicy] Failed to tear down test.", false);
+        }
+        destroyUser();
+        super.tearDown();
+    }
+
+    /**
+     * Start test suite
+     * @return test suite
+     */
+    public static Test suite()
+    {
+        // All methods starting with "test" will be executed in the test suite.
+        return new TestSuite(TestPermissions.class);
+    }
+
+    /**
+     * Test getPermissions
+     */
+    public void testgetPermission()
+    {
+                
+        Permissions permissions = pms.getPermissions("/files/subfolder1");
+        
+        Enumeration enum = permissions.elements();
+        int count = 0;
+        while (enum.hasMoreElements())
+        {
+            CmsPermission permission = (CmsPermission) enum.nextElement();            
+            System.out.println("Permission found - actions : " + permission.getActions()
+ " name : " + permission.getName());
+            Iterator fullPathIterator = permission.getPrincipalFullPaths().iterator();
+            while (fullPathIterator.hasNext())
+            {
+                String fullPath = (String) fullPathIterator.next();
+                System.out.println("          fullPath : " + fullPath);                
+            }
+            
+            count++;
+        }
+        assertTrue("Invalid number of permissions for '/files/subfolder1", count == 2);
+       
+    }  
+    
+    /**
+     * Test isRecursive
+     */
+    public void testisRecursive()
+    {
+                
+        CmsPermissionImpl perm = new CmsPermissionImpl("/files/subfolder1/-", "view");
+        assertTrue("Permission /files/subfolder1/- is not recursive", perm.isRecursive()
);
+        
+        perm = new CmsPermissionImpl("/files/subfolder1", "view");
+        assertFalse("Permission /files/subfolder1 is  recursive", perm.isRecursive() );
+        
+        perm = new CmsPermissionImpl("/files/subfolder1/*", "view");
+        assertFalse("Permission  /files/subfolder1/* is  recursive", perm.isRecursive() );
+       
+    }     
+    /**
+     * <p>
+     * Initialize user test object.
+     * </p>
+     */
+    protected void initUser()
+    {
+        try
+        {
+            ums.addUser("anon", "password");
+            ums.addUser("christophe", "password");
+        }
+        catch (SecurityException sex)
+        {
+        }
+
+        UserPrincipal user = new UserPrincipalImpl("anon");
+        UserPrincipal user2 = new UserPrincipalImpl("christophe");
+        
+        CmsPermissionImpl perm1 = new CmsPermissionImpl("/files/test.xml", "edit");
+        CmsPermissionImpl perm2 = new CmsPermissionImpl("/files/subfolder1/-", "view");
+        CmsPermissionImpl perm3 = new CmsPermissionImpl("/files/subfolder1/*", "edit");
+        CmsPermissionImpl perm4 = new CmsPermissionImpl("/files/subfolder2/-", "view");
+        CmsPermissionImpl perm5 = new CmsPermissionImpl("/files/subfolder3/*", "view,insert");
+        
+        try
+        {
+            
+            pms.addPermission(perm1);
+            pms.addPermission(perm2);
+            pms.addPermission(perm3);
+            pms.addPermission(perm4);
+            pms.addPermission(perm5);
+            pms.grantPermission(user, perm1);
+            pms.grantPermission(user, perm2);
+            pms.grantPermission(user, perm3);
+            pms.grantPermission(user, perm4);
+            pms.grantPermission(user, perm5);
+            pms.grantPermission(user2, perm2);
+            
+        }
+        catch (SecurityException sex)
+        {
+            sex.printStackTrace();
+        }
+    }
+
+    /**
+     * 
+     * Destroy user test object.
+     * 
+     * @throws Exception when it is not possible to clean up
+     */
+    protected void destroyUser() throws Exception
+    {
+        ums.removeUser("anon");
+        ums.removeUser("christophe");
+
+        CmsPermissionImpl perm1 = new CmsPermissionImpl("/files/test.xml", "edit");
+        CmsPermissionImpl perm2 = new CmsPermissionImpl("/files/subfolder1/*", "view");
+        CmsPermissionImpl perm3 = new CmsPermissionImpl("/files/subfolder1/-", "view");
+        CmsPermissionImpl perm4 = new CmsPermissionImpl("/files/subfolder2/-", "view");
+        CmsPermissionImpl perm5 = new CmsPermissionImpl("/files/subfolder3/-", "view,insert");
+        pms.removePermission(perm1);
+        pms.removePermission(perm2);
+        pms.removePermission(perm3);
+        pms.removePermission(perm4);
+        pms.removePermission(perm5);
+    }
+
+}
\ No newline at end of file



Mime
View raw message