jakarta-cactus-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cml...@apache.org
Subject cvs commit: jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment TestWebXml.java
Date Tue, 10 Jun 2003 12:56:58 GMT
cmlenz      2003/06/10 05:56:58

  Modified:    integration/ant/src/java/org/apache/cactus/integration/ant/deployment
                        WebXmlTag.java WebXml.java
               integration/ant/src/test/org/apache/cactus/integration/ant/deployment
                        TestWebXml.java
  Log:
  Initial support for security constraint manipulation and access.
  
  Revision  Changes    Path
  1.4       +19 -1     jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlTag.java
  
  Index: WebXmlTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WebXmlTag.java	10 Jun 2003 09:20:31 -0000	1.3
  +++ WebXmlTag.java	10 Jun 2003 12:56:58 -0000	1.4
  @@ -233,6 +233,24 @@
           new WebXmlTag("security-constraint");
       
       /**
  +     * Element name 'web-resource-collection',
  +     */
  +    public static final WebXmlTag WEB_RESOURCE_COLLECTION =
  +        new WebXmlTag("web-resource-collection");
  +    
  +    /**
  +     * Element name 'web-resource-name',
  +     */
  +    public static final WebXmlTag WEB_RESOURCE_NAME =
  +        new WebXmlTag("web-resource-name");
  +    
  +    /**
  +     * Element name 'auth-constraint',
  +     */
  +    public static final WebXmlTag AUTH_CONSTRAINT =
  +        new WebXmlTag("auth-constraint");
  +    
  +    /**
        * Element name 'login-config',
        */
       public static final WebXmlTag LOGIN_CONFIG =
  
  
  
  1.4       +117 -2    jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXml.java
  
  Index: WebXml.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXml.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WebXml.java	10 Jun 2003 09:20:32 -0000	1.3
  +++ WebXml.java	10 Jun 2003 12:56:58 -0000	1.4
  @@ -706,6 +706,98 @@
       }
       
       /**
  +     * Creates and adds a security-constraint to the descriptor.
  +     * 
  +     * @param theWebResourceName The name of the web resource collection to
  +     *        protect
  +     * @param theUrlPattern The URL pattern to apply the constraint to
  +     * @param theRoles The list of authorized roles
  +     */
  +    public final void addSecurityConstraint(String theWebResourceName,
  +        String theUrlPattern, List theRoles)
  +    {
  +        if ((theWebResourceName == null) || (theUrlPattern == null)
  +         || (theRoles == null))
  +        {
  +            throw new NullPointerException();
  +        }
  +        if (hasSecurityConstraint(theUrlPattern))
  +        {
  +            throw new IllegalStateException("Security constraint for URL "
  +                + "pattern " + theUrlPattern + " already defined");
  +        }
  +        Element securityConstraintElement =
  +            this.document.createElement(
  +                WebXmlTag.SECURITY_CONSTRAINT.getTagName());
  +        Element webResourceCollectionElement =
  +            this.document.createElement(
  +                WebXmlTag.WEB_RESOURCE_COLLECTION.getTagName());
  +        webResourceCollectionElement.appendChild(
  +            createNestedText(WebXmlTag.WEB_RESOURCE_NAME, theWebResourceName));
  +        webResourceCollectionElement.appendChild(
  +            createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern));
  +        securityConstraintElement.appendChild(webResourceCollectionElement);
  +        Element authConstraintElement =
  +            this.document.createElement(WebXmlTag.AUTH_CONSTRAINT.getTagName());
  +        for (Iterator i = theRoles.iterator(); i.hasNext();)
  +        {
  +            authConstraintElement.appendChild(
  +                createNestedText(WebXmlTag.ROLE_NAME, (String) i.next()));
  +        }
  +        securityConstraintElement.appendChild(authConstraintElement);
  +        addElement(WebXmlTag.SECURITY_CONSTRAINT, securityConstraintElement);
  +    }
  +
  +    /**
  +     * Returns the element that contains the security constraint defined for the
  +     * specified URL pattern.
  +     * 
  +     * @param therUrlPattern The URL pattern
  +     * @return The DOM element representing the security constraint
  +     */
  +    public final Element getSecurityConstraint(String theUrlPattern)
  +    {
  +        if (theUrlPattern == null)
  +        {
  +            throw new NullPointerException();
  +        }
  +        Iterator securityConstraintElements =
  +            getElements(WebXmlTag.SECURITY_CONSTRAINT);
  +        while (securityConstraintElements.hasNext())
  +        {
  +            Element securityConstraintElement = (Element)
  +                securityConstraintElements.next();
  +            Iterator webResourceCollectionElements = 
  +                getNestedElements(securityConstraintElement,
  +                    WebXmlTag.WEB_RESOURCE_COLLECTION);
  +            if (webResourceCollectionElements.hasNext())
  +            {
  +                Element webResourceCollectionElement = (Element)
  +                    webResourceCollectionElements.next();
  +                if (theUrlPattern.equals(getNestedText(
  +                    webResourceCollectionElement, WebXmlTag.URL_PATTERN)))
  +                {
  +                    return securityConstraintElement;
  +                }
  +            }
  +        }
  +        return null;
  +    }
  +    
  +    /**
  +     * Returns whether a security constraint has been mapped to the specified
  +     * URL pattern.
  +     * 
  +     * @param theUrlPattern The URL patterm
  +     * @return <code>true</code> if a security constraint is defined,
  +     *         <code>false</code> otherwise
  +     */
  +    public final boolean hasSecurityConstraint(String theUrlPattern)
  +    {
  +        return (getSecurityConstraint(theUrlPattern) != null);
  +    }
  +    
  +    /**
        * Adds a new security role to the descriptor.
        * 
        * @param theRoleName The name of the role to add
  @@ -779,7 +871,7 @@
       }
       
       /**
  -     * Returns whether a specific security role has been defined
  +     * Returns whether a specific security role has been defined.
        * 
        * @param theRoleName The name of the role
        * @return <code>true</code> if the security role is defined,
  @@ -885,6 +977,29 @@
           }
       }
       
  +    /**
  +     * Returns an iterator over the child elements of the specified element that
  +     * match the specified tag.
  +     *  
  +     * @param theParent The element of which the nested elements should be
  +     *        retrieved
  +     * @param theTag The descriptor tag of which the elements should be
  +     *        returned
  +     * @return An iterator over the elements matching the tag, in the order 
  +     *         they occur in the descriptor
  +     */
  +    private Iterator getNestedElements(Element theParent,
  +        WebXmlTag theTag)
  +    {
  +        List elements = new ArrayList();
  +        NodeList nodeList = theParent.getElementsByTagName(theTag.getTagName());
  +        for (int i = 0; i < nodeList.getLength(); i++)
  +        {
  +            elements.add(nodeList.item(i));
  +        }
  +        return elements.iterator();
  +    }
  +
       /**
        * Creates an element that contains nested text.
        * 
  
  
  
  1.4       +66 -11    jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXml.java
  
  Index: TestWebXml.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXml.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestWebXml.java	10 Jun 2003 09:20:32 -0000	1.3
  +++ TestWebXml.java	10 Jun 2003 12:56:58 -0000	1.4
  @@ -58,7 +58,10 @@
   
   import java.io.ByteArrayInputStream;
   import java.io.StringReader;
  +import java.util.ArrayList;
  +import java.util.Collections;
   import java.util.Iterator;
  +import java.util.List;
   
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
  @@ -1023,20 +1026,65 @@
       }
   
       /**
  -     * Tests whether retrieving security-constraint elements from an empty
  -     * descriptor results in an empty iterator.
  +     * TODO
        * 
        * @throws Exception If an unexpected error occurs
        */
  -    public void testGetSecurityConstraintEmpty()
  +    public void testAddSecurityConstraint()
           throws Exception
       {
           String xml = "<web-app></web-app>";
           Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
           WebXml webXml = new WebXml(doc);
  -        Iterator securityConstraints =
  -            webXml.getElements(WebXmlTag.SECURITY_CONSTRAINT);
  -        assertTrue(!securityConstraints.hasNext());
  +        webXml.addSecurityConstraint("wrn", "/url", Collections.EMPTY_LIST);
  +        assertTrue(webXml.hasSecurityConstraint("/url"));
  +    }
  +
  +    /**
  +     * TODO
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testAddSecurityConstraintWithRoles()
  +        throws Exception
  +    {
  +        String xml = "<web-app></web-app>";
  +        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
  +        WebXml webXml = new WebXml(doc);
  +        List roles = new ArrayList();
  +        roles.add("role1");
  +        roles.add("role2");
  +        webXml.addSecurityConstraint("wrn", "/url", roles);
  +        assertTrue(webXml.hasSecurityConstraint("/url"));
  +        Element securityConstraintElement =
  +            webXml.getSecurityConstraint("/url");
  +        assertNotNull(securityConstraintElement);
  +        Element authConstraintElement = (Element)
  +            securityConstraintElement.getElementsByTagName(
  +                "auth-constraint").item(0);
  +        assertNotNull(authConstraintElement);
  +        NodeList roleNameElements =
  +            authConstraintElement.getElementsByTagName("role-name");
  +        assertEquals(2, roleNameElements.getLength());
  +        assertEquals("role1",
  +            roleNameElements.item(0).getChildNodes().item(0).getNodeValue());
  +        assertEquals("role2",
  +            roleNameElements.item(1).getChildNodes().item(0).getNodeValue());
  +    }
  +
  +    /**
  +     * Tests whether checking an empty descriptor for some security constraint
  +     * results in <code>false</code>.
  +     * 
  +     * @throws Exception If an unexpected error occurs
  +     */
  +    public void testHasSecurityConstraintEmpty()
  +        throws Exception
  +    {
  +        String xml = "<web-app></web-app>";
  +        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
  +        WebXml webXml = new WebXml(doc);
  +        assertTrue(!webXml.hasSecurityConstraint("/TestUrl"));
       }
   
       /**
  @@ -1052,15 +1100,16 @@
               + "  <security-constraint>"
               + "    <web-resource-collection>"
               + "      <web-resource-name>wr1</web-resource-name>"
  +            + "      <url-pattern>/url1</url-pattern>"
               + "    </web-resource-collection>"
               + "  </security-constraint>"
               + "</web-app>";
           Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
           WebXml webXml = new WebXml(doc);
  -        Iterator securityConstraints =
  -            webXml.getElements(WebXmlTag.SECURITY_CONSTRAINT);
  -        assertNotNull(securityConstraints.next());
  -        assertTrue(!securityConstraints.hasNext());
  +        assertTrue(webXml.hasSecurityConstraint("/url1"));
  +        Element securityConstraintElement =
  +            webXml.getSecurityConstraint("/url1");
  +        assertNotNull(securityConstraintElement);
       }
   
       /**
  @@ -1076,21 +1125,27 @@
               + "  <security-constraint>"
               + "    <web-resource-collection>"
               + "      <web-resource-name>wr1</web-resource-name>"
  +            + "      <url-pattern>/url1</url-pattern>"
               + "    </web-resource-collection>"
               + "  </security-constraint>"
               + "  <security-constraint>"
               + "    <web-resource-collection>"
               + "      <web-resource-name>wr2</web-resource-name>"
  +            + "      <url-pattern>/url2</url-pattern>"
               + "    </web-resource-collection>"
               + "  </security-constraint>"
               + "  <security-constraint>"
               + "    <web-resource-collection>"
               + "      <web-resource-name>wr3</web-resource-name>"
  +            + "      <url-pattern>/url3</url-pattern>"
               + "    </web-resource-collection>"
               + "  </security-constraint>"
               + "</web-app>";
           Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
           WebXml webXml = new WebXml(doc);
  +        assertTrue(webXml.hasSecurityConstraint("/url1"));
  +        assertTrue(webXml.hasSecurityConstraint("/url2"));
  +        assertTrue(webXml.hasSecurityConstraint("/url3"));
           Iterator securityConstraints =
               webXml.getElements(WebXmlTag.SECURITY_CONSTRAINT);
           assertNotNull(securityConstraints.next());
  
  
  

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


Mime
View raw message