You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by dl...@apache.org on 2004/09/18 21:35:08 UTC

cvs commit: jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/spi TestRoleSecurityHandler.java TestGroupSecurityHandler.java TestCredentialHandler.java TestUserSecurityHandler.java

dlestrat    2004/09/18 12:35:08

  Added:       components/security/src/test/org/apache/jetspeed/security/spi
                        TestRoleSecurityHandler.java
                        TestGroupSecurityHandler.java
                        TestCredentialHandler.java
                        TestUserSecurityHandler.java
  Log:
  http://nagoya.apache.org/jira/browse/JS2-114#action_53185
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/spi/TestRoleSecurityHandler.java
  
  Index: TestRoleSecurityHandler.java
  ===================================================================
  /* 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.jetspeed.security.spi;
  
  import java.util.Set;
  
  import org.apache.jetspeed.security.AbstractSecurityTestcase;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * <p>
   * Unit testing for {@link RoleSecurityHandler}.
   * </p>
   * 
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
   */
  public class TestRoleSecurityHandler extends AbstractSecurityTestcase
  {
  
      /**
       * <p>
       * Defines the test case name for junit.
       * </p>
       * 
       * @param testName The test case name.
       */
      public TestRoleSecurityHandler(String testName)
      {
          super(testName);
      }
  
      /**
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception
      {
          super.setUp();
      }
  
      /**
       * @see junit.framework.TestCase#tearDown()
       */
      public void tearDown() throws Exception
      {
          super.tearDown();
      }
  
      /**
       * <p>
       * Constructs the suite.
       * </p>
       * 
       * @return The {@Test}.
       */
      public static Test suite()
      {
          return new TestSuite(TestRoleSecurityHandler.class);
      }
  
      /**
       * <p>
       * Test <code>getUserPrincipal</code>.
       * </p>
       */
      public void testGetRolePrincipals() throws Exception
      {
          initUser();
          Set principals = rsh.getRolePrincipals("testuser");
          assertNotNull(principals);
          // Hierarchy by generalization should return 3 roles.
          assertEquals(3, principals.size());
          destroyUser();
      }
  
      /**
       * <p>
       * Initialize user test object.
       * </p>
       */
      protected void initUser() throws Exception
      {
          ums.addUser("testuser", "password");
          rms.addRole("testusertorole1");
          rms.addRole("testusertorole2.role1");
          rms.addRoleToUser("testuser", "testusertorole1");
          rms.addRoleToUser("testuser", "testusertorole2.role1");
      }
  
      /**
       * <p>
       * Destroy user test object.
       * </p>
       */
      protected void destroyUser() throws Exception
      {
          ums.removeUser("testuser");
          rms.removeRole("testusertorole1");
          rms.removeRole("testusertorole2");
      }
  
  }
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/spi/TestGroupSecurityHandler.java
  
  Index: TestGroupSecurityHandler.java
  ===================================================================
  /* 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.jetspeed.security.spi;
  
  import java.util.Set;
  
  import org.apache.jetspeed.security.AbstractSecurityTestcase;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * <p>
   * Unit testing for {@link GroupSecurityHandler}.
   * </p>
   * 
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
   */
  public class TestGroupSecurityHandler extends AbstractSecurityTestcase
  {
  
      /**
       * <p>
       * Defines the test case name for junit.
       * </p>
       * 
       * @param testName The test case name.
       */
      public TestGroupSecurityHandler(String testName)
      {
          super(testName);
      }
  
      /**
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception
      {
          super.setUp();
      }
  
      /**
       * @see junit.framework.TestCase#tearDown()
       */
      public void tearDown() throws Exception
      {
          super.tearDown();
      }
  
      /**
       * <p>
       * Constructs the suite.
       * </p>
       * 
       * @return The {@Test}.
       */
      public static Test suite()
      {
          return new TestSuite(TestGroupSecurityHandler.class);
      }
  
      /**
       * <p>
       * Test <code>getUserPrincipal</code>.
       * </p>
       */
      public void testGetGroupPrincipals() throws Exception
      {
          initUser();
          Set principals = gsh.getGroupPrincipals("testuser");
          assertNotNull(principals);
          // Hierarchy by generalization should return 3 roles.
          assertEquals(3, principals.size());
          destroyUser();
      }
  
      /**
       * <p>
       * Initialize user test object.
       * </p>
       */
      protected void initUser() throws Exception
      {
          ums.addUser("testuser", "password");
          gms.addGroup("testusertogroup1");
          gms.addGroup("testusertogroup2.group1");
          gms.addUserToGroup("testuser", "testusertogroup1");
          gms.addUserToGroup("testuser", "testusertogroup2.group1");
      }
  
      /**
       * <p>
       * Destroy user test object.
       * </p>
       */
      protected void destroyUser() throws Exception
      {
          ums.removeUser("testuser");
          gms.removeGroup("testusertogroup1");
          gms.removeGroup("testusertogroup2");
      }
  
  }
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/spi/TestCredentialHandler.java
  
  Index: TestCredentialHandler.java
  ===================================================================
  /* 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.jetspeed.security.spi;
  
  import java.util.Set;
  
  import org.apache.jetspeed.security.AbstractSecurityTestcase;
  import org.apache.jetspeed.security.PasswordCredential;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * <p>
   * Unit testing for {@link UserManager}.
   * </p>
   * 
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
   */
  public class TestCredentialHandler extends AbstractSecurityTestcase
  {
  
      /**
       * <p>
       * Defines the test case name for junit.
       * </p>
       * 
       * @param testName The test case name.
       */
      public TestCredentialHandler(String testName)
      {
          super(testName);
      }
      
      /**
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception
      {
          super.setUp();
      }
  
      /**
       * @see junit.framework.TestCase#tearDown()
       */
      public void tearDown() throws Exception
      {
          super.tearDown();
      }
  
      /**
       * <p>
       * Constructs the suite.
       * </p>
       * 
       * @return The {@Test}.
       */
      public static Test suite()
      {
          return new TestSuite(TestCredentialHandler.class);
      }
  
      /**
       * <p>
       * Test <code>getPrivateCredentials</code>..
       * </p>
       */
      public void testGetPrivateCredentials() throws Exception
      {
          initUser();
          Set privateCredentials = ch.getPrivateCredentials("testcred");
          assertNotNull(privateCredentials);
          assertEquals(1, privateCredentials.size());
          PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
          assertEquals("testcred", pwdCreds[0].getUserName());
          assertEquals("password", new String(pwdCreds[0].getPassword()));
          destroyUser();
      }
      
      /**
       * <p>
       * Test <code>getPublicCredentials</code>..
       * </p>
       */
      public void testGetPublicCredentials() throws Exception
      {
          initUser();
          Set publicCredentials = ch.getPublicCredentials("testcred");
          assertNotNull(publicCredentials);
          assertEquals(0, publicCredentials.size());
          destroyUser();
      }
  
      /**
       * <p>
       * Test <code>setPrivatePasswordCredential</code>..
       * </p>
       */
      public void testSetPrivatePasswordCredential() throws Exception
      {
          initUser();
          // Replace existing password credential.
          PasswordCredential oldPwdCred = new PasswordCredential("testcred", ("password").toCharArray());
          PasswordCredential newPwdCred = new PasswordCredential("testcred", ("newpassword").toCharArray());
          ch.setPrivatePasswordCredential(oldPwdCred, newPwdCred);
          // Test that the credential was properly set.
          Set privateCredentials = ch.getPrivateCredentials("testcred");
          assertNotNull(privateCredentials);
          assertEquals(1, privateCredentials.size());
          PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
          assertEquals("testcred", pwdCreds[0].getUserName());
          assertEquals("newpassword", new String(pwdCreds[0].getPassword()));
          // Add password credential.
          oldPwdCred = null;
          newPwdCred = new PasswordCredential("testcred", ("anotherpassword").toCharArray());
          ch.setPrivatePasswordCredential(oldPwdCred, newPwdCred);
          // Test that the credential was properly set.
          privateCredentials = ch.getPrivateCredentials("testcred");
          assertNotNull(privateCredentials);
          assertEquals(2, privateCredentials.size());
          destroyUser();
      }
      
      /**
       * <p>
       * Initialize user test object.
       * </p>
       */
      protected void initUser() throws Exception
      {
          ums.addUser("testcred", "password");
      }
  
      /**
       * <p>
       * Destroy user test object.
       * </p>
       */
      protected void destroyUser() throws Exception
      {
          ums.removeUser("testcred");
      }
  
  }
  
  
  1.1                  jakarta-jetspeed-2/components/security/src/test/org/apache/jetspeed/security/spi/TestUserSecurityHandler.java
  
  Index: TestUserSecurityHandler.java
  ===================================================================
  /* 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.jetspeed.security.spi;
  
  import java.security.Principal;
  import java.util.Iterator;
  
  import org.apache.jetspeed.security.AbstractSecurityTestcase;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * <p>
   * Unit testing for {@link UserSecurityHandler}.
   * </p>
   * 
   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
   */
  public class TestUserSecurityHandler extends AbstractSecurityTestcase
  {
  
      /**
       * <p>
       * Defines the test case name for junit.
       * </p>
       * 
       * @param testName The test case name.
       */
      public TestUserSecurityHandler(String testName)
      {
          super(testName);
      }
  
      /**
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception
      {
          super.setUp();
      }
  
      /**
       * @see junit.framework.TestCase#tearDown()
       */
      public void tearDown() throws Exception
      {
          super.tearDown();
      }
  
      /**
       * <p>
       * Constructs the suite.
       * </p>
       * 
       * @return The {@Test}.
       */
      public static Test suite()
      {
          return new TestSuite(TestUserSecurityHandler.class);
      }
  
      /**
       * <p>
       * Test <code>getUserPrincipal</code>.
       * </p>
       */
      public void testGetUserPrincipal() throws Exception
      {
          initUsers();
          Principal principal = ush.getUserPrincipal("testuser1");
          assertNotNull(principal);
          assertEquals("testuser1", principal.getName());
          destroyUsers();
      }
      
      /**
       * <p>
       * Test <code>getUserPrincipals</code>.
       * </p>
       */
      public void testGetUserPrincipals() throws Exception
      {
          initUsers();
          Iterator principals = ush.getUserPrincipals("");
          int count = 0;
          while (principals.hasNext())
          {
              Principal principal = (Principal) principals.next();
              if (0 == count)
              {
                  assertNotNull(principal);
                  assertEquals("testuser1", principal.getName());
              }
              else if (1 == count)
              {
                  assertNotNull(principal);
                  assertEquals("testuser2", principal.getName());
              }
              count ++;
          }
          destroyUsers();
      }
  
      /**
       * <p>
       * Initialize user test object.
       * </p>
       */
      protected void initUsers() throws Exception
      {
          ums.addUser("testuser1", "password");
          ums.addUser("testuser2", "password");
      }
  
      /**
       * <p>
       * Destroy user test object.
       * </p>
       */
      protected void destroyUsers() throws Exception
      {
          ums.removeUser("testuser1");
          ums.removeUser("testuser2");
      }
  
  }
  
  

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