You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by gi...@apache.org on 2003/01/06 20:07:37 UTC

cvs commit: xml-xindice/java/scratchpad/tests/src/org/apache/xindice/core/security XindiceUserManagerTest.java

gianugo     2003/01/06 11:07:36

  Modified:    java/scratchpad/src/org/apache/xindice/core/security
                        UserManager.java XindiceUserManager.java
               java/scratchpad/tests/src/org/apache/xindice/core/security
                        XindiceUserManagerTest.java
  Log:
  A few bugfixes, more tests added and a small API change: now getRoles() returns a java.util.Collection instead than a String[]
  
  Revision  Changes    Path
  1.2       +6 -4      xml-xindice/java/scratchpad/src/org/apache/xindice/core/security/UserManager.java
  
  Index: UserManager.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/scratchpad/src/org/apache/xindice/core/security/UserManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UserManager.java	3 Jan 2003 16:58:31 -0000	1.1
  +++ UserManager.java	6 Jan 2003 19:07:36 -0000	1.2
  @@ -1,5 +1,7 @@
   package org.apache.xindice.core.security;
   
  +import java.util.Collection;
  +
   /*
    * The Apache Software License, Version 1.1
    *
  @@ -103,10 +105,10 @@
       boolean checkPassword(String name, String password);
   
       /**
  -     * Returns an array of Object representing the roles
  +     * Returns a collection of Strings representing the roles
        * for a given user or null if the user does not exist.
        * 
  -     * @return the role array
  +     * @return a Collection holding the roles
        */ 
  -    String[] getRoles(String user);
  +    Collection getRoles(String user);
   }
  
  
  
  1.2       +14 -15    xml-xindice/java/scratchpad/src/org/apache/xindice/core/security/XindiceUserManager.java
  
  Index: XindiceUserManager.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/scratchpad/src/org/apache/xindice/core/security/XindiceUserManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XindiceUserManager.java	3 Jan 2003 16:58:31 -0000	1.1
  +++ XindiceUserManager.java	6 Jan 2003 19:07:36 -0000	1.2
  @@ -59,6 +59,7 @@
    */
   
   import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Collections;
   import java.util.HashMap;
   import java.util.Iterator;
  @@ -171,12 +172,14 @@
               userElement.setAttribute(USERNAME, currentUser);
               userElement.setAttribute(PASSWORD, currentPassword);
               
  -            String[] roles = getRoles(currentUser);
  -            for (int j = 0; j < roles.length; j++) {
  -                 Element roleElement = doc.createElement(ROLE);
  -                 roleElement.setAttribute(USERNAME, roles[j]);
  -                 System.err.println("Added " + roles[j] + " to user " + currentUser);
  -                 userElement.appendChild(roleElement);    				
  +            Collection roles = getRoles(currentUser);
  +            
  +            for (Iterator iter = roles.iterator(); iter.hasNext();) {
  +				String role = (String) iter.next();
  +                Element roleElement = doc.createElement(ROLE);
  +                roleElement.setAttribute(USERNAME, role);
  +                userElement.appendChild(roleElement);                  
  +				
   			}
               
               root.appendChild(userElement);
  @@ -237,18 +240,14 @@
       }
       
       /**
  -     * Returns an array of String representing the roles
  +     * Returns a Collection of Strings representing the roles
        * for a given user or null if the user does not exist.
        * 
  -     * @return the role array
  +     * @return the role Collection
        */ 
  -    public String[] getRoles(String user) {
  -        ArrayList roleList = (ArrayList) roles.get(user);
  -        
  -        if (roleList != null)
  -            return (String[]) roleList.toArray(new String[1]);        
  -        
  -        return null;
  +    public Collection getRoles(String user) {
  +        return (Collection) roles.get(user);
  +
       }    
   
   }
  
  
  
  1.2       +25 -31    xml-xindice/java/scratchpad/tests/src/org/apache/xindice/core/security/XindiceUserManagerTest.java
  
  Index: XindiceUserManagerTest.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/scratchpad/tests/src/org/apache/xindice/core/security/XindiceUserManagerTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XindiceUserManagerTest.java	3 Jan 2003 16:58:30 -0000	1.1
  +++ XindiceUserManagerTest.java	6 Jan 2003 19:07:36 -0000	1.2
  @@ -1,5 +1,6 @@
   package org.apache.xindice.core.security;
   
  +import java.util.Collection;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
   import junit.framework.TestCase;
  @@ -126,37 +127,16 @@
               fail("Unable to create a DOM object" + e.getMessage());
           }
           
  -        Element element = usermanager.streamToXML(doc);
  -        
  -        assertEquals(element.getNodeName(), XindiceUserManager.CONFIGURATION);
  -        
  -        NodeList nl = element.getChildNodes();
  -        
  -        assertTrue(nl.getLength() > 0);
           
  -        for (int i = 0; i < nl.getLength(); i++) {
  -            assertTrue(nl.item(i).getNodeType() == Node.ELEMENT_NODE);
  -            assertEquals(((Element)nl.item(i)).getNodeName(), 
  -                XindiceUserManager.USERTAG);
  -            assertTrue(((Element)nl.item(i)).hasAttribute(XindiceUserManager.USERNAME));
  -            assertTrue(((Element)nl.item(i)).hasAttribute(XindiceUserManager.PASSWORD));
  -            
  -            NodeList rolesNl = nl.item(i).getChildNodes();
  -            assertTrue(rolesNl.getLength() > 0);
  -            
  -            for (int ii = 0; ii < nl.getLength(); ii++) {
  -                 assertTrue(rolesNl.item(ii).getNodeType() == Node.ELEMENT_NODE);
  -                 assertEquals(((Element)rolesNl.item(ii)).getNodeName(), XindiceUserManager.ROLE);
  -                 assertTrue(((Element)rolesNl.item(ii)).hasAttribute(XindiceUserManager.USERNAME));  
  -            } 
  -                
  -        }
  +        Element element = usermanager.streamToXML(doc);
           
           XindiceUserManager newUserManager = new XindiceUserManager();
           
           newUserManager.streamFromXML(element);
           try {
               assertTrue(newUserManager.checkPassword("test", "test"));
  +            Collection roles = newUserManager.getRoles("manyroles");
  +            assertTrue(roles.size() == 7);
           } catch (Exception e) {
               fail("The serialized UserManager is not the same as the current one ");
           }
  @@ -165,17 +145,31 @@
   
       public void testRoles() {
           
  -        String[] result = (String[])usermanager.getRoles("test");
  -        assertEquals(1, result.length);
  -        assertEquals("root", result[0]);
  +        Collection result = usermanager.getRoles("test");
  +        assertEquals(1, result.size());
  +        assertEquals("root", (String)result.iterator().next());
           
       }   
       
       public void testManyRoles() {
  +        Collection result = usermanager.getRoles("manyroles");
  +        assertEquals(7, result.size());
  +        assertEquals("root", (String)result.iterator().next());
  +    } 
  +    
  +    public void testNonExistingUser() {
           
  -        String[] result = (String[])usermanager.getRoles("manyroles");
  -        assertEquals(7, result.length);
  -        assertEquals("root", result[0]);
  +        assertNull(usermanager.getRoles("nonexisting"));
           
  -    }  
  +        try {
  +			usermanager.deleteUser("nonexisting");
  +            fail("Successful deletion of a non existing user");
  +		} catch (UserManagerException e) {
  +		}
  +        try {
  +			usermanager.changePassword("nonexisting", "nonexisting");
  +            fail("Successful password change of a non existing user");
  +		} catch (UserManagerException e) {
  +		}   
  +    }    
   }