commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester DigesterTestCase.java
Date Mon, 15 Mar 2004 21:44:53 GMT
rdonkin     2004/03/15 13:44:53

  Modified:    digester/src/java/org/apache/commons/digester Digester.java
               digester/src/test/org/apache/commons/digester
                        DigesterTestCase.java
  Log:
  Revised the named stacks API to take account of Martin's comments. Now the named stacks
throw EmptyStackException's (rather than returning null). This should bring Digester abck
into line with the standard way that stacks work in Java.
  
  Revision  Changes    Path
  1.96      +44 -6     jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java
  
  Index: Digester.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
  retrieving revision 1.95
  retrieving revision 1.96
  diff -u -r1.95 -r1.96
  --- Digester.java	7 Mar 2004 19:37:07 -0000	1.95
  +++ Digester.java	15 Mar 2004 21:44:53 -0000	1.96
  @@ -2476,39 +2476,77 @@
       }
   
       /**
  -     * Pops (gets and removes) the top object from the stack with the given name.
  +     * <p>Pops (gets and removes) the top object from the stack with the given name.</p>
  +     *
  +     * <p><strong>Note:</strong> a stack is considered empty
  +     * if no objects have been pushed onto it yet.</p>
        * 
        * @param stackName the name of the stack from which the top value is to be popped
        * @return the top <code>Object</code> on the stack or or null if the stack
is either 
        * empty or has not been created yet
  +     * @throws EmptyStackException if the named stack is empty
        */
       public Object pop(String stackName) {
           Object result = null;
           ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName);
  -        if (namedStack != null && !namedStack.isEmpty()) {
  +        if (namedStack == null) {
  +            if (log.isDebugEnabled()) {
  +                log.debug("Stack '" + stackName + "' is empty");
  +            }
  +            throw new EmptyStackException();
  +            
  +        } else {
  +        
               result = namedStack.pop();
           }
           return result;
       }
       
       /**
  -     * Gets the top object from the stack with the given name.
  +     * <p>Gets the top object from the stack with the given name.
        * This method does not remove the object from the stack.
  +     * </p>
  +     * <p><strong>Note:</strong> a stack is considered empty
  +     * if no objects have been pushed onto it yet.</p>
        *
        * @param stackName the name of the stack to be peeked
        * @return the top <code>Object</code> on the stack or null if the stack
is either 
        * empty or has not been created yet
  +     * @throws EmptyStackException if the named stack is empty 
        */
       public Object peek(String stackName) {
           Object result = null;
           ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName);
  -        if (namedStack != null && !namedStack.isEmpty()) {
  +        if (namedStack == null ) {
  +            if (log.isDebugEnabled()) {
  +                log.debug("Stack '" + stackName + "' is empty");
  +            }        
  +            throw new EmptyStackException();
  +        
  +        } else {
  +        
               result = namedStack.peek();
           }
           return result;
       }
   
  -
  +    /**
  +     * <p>Is the stack with the given name empty?</p>
  +     * <p><strong>Note:</strong> a stack is considered empty
  +     * if no objects have been pushed onto it yet.</p>
  +     * @param stackName the name of the stack whose emptiness 
  +     * should be evaluated
  +     * @return true if the given stack if empty 
  +     */
  +    public boolean isEmpty(String stackName) {
  +        boolean result = true;
  +        ArrayStack namedStack = (ArrayStack) stacksByName.get(stackName);
  +        if (namedStack != null ) {
  +            result = namedStack.isEmpty();
  +        }
  +        return result;
  +    }
  +    
       /**
        * When the Digester is being used as a SAXContentHandler, 
        * this method allows you to access the root object that has been
  
  
  
  1.19      +45 -4     jakarta-commons/digester/src/test/org/apache/commons/digester/DigesterTestCase.java
  
  Index: DigesterTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/DigesterTestCase.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DigesterTestCase.java	7 Mar 2004 19:37:08 -0000	1.18
  +++ DigesterTestCase.java	15 Mar 2004 21:44:53 -0000	1.19
  @@ -23,6 +23,7 @@
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  +import java.util.EmptyStackException;
   
   import junit.framework.Test;
   import junit.framework.TestCase;
  @@ -386,11 +387,11 @@
           BigDecimal archimedesAveragePi = new BigDecimal("3.1418");
           String testStackName = "org.apache.commons.digester.tests.testNamedStackPushPeekPop";
           Digester digester = new Digester();
  -        assertNull("Stack starts empty:", digester.peek(testStackName));
  +        assertTrue("Stack starts empty:", digester.isEmpty(testStackName));
           digester.push(testStackName, archimedesAveragePi);
           assertEquals("Peeked value:", archimedesAveragePi, digester.peek(testStackName));
           assertEquals("Popped value:", archimedesAveragePi, digester.pop(testStackName));
  -        assertNull("Stack ends empty:", digester.peek(testStackName));
  +        assertTrue("Stack ends empty:", digester.isEmpty(testStackName));
       }
       
       /** Tests that values are stored independently */
  @@ -410,8 +411,48 @@
       {
           String testStackName = "org.apache.commons.digester.tests.testPopNamedStackNotPushed";
           Digester digester = new Digester();
  -        digester.pop(testStackName);
  +        try {
  +        
  +            digester.pop(testStackName);
  +            fail("Expected an EmptyStackException");
  +            
  +        } catch (EmptyStackException e) {
  +            // expected
  +        }
  +        
  +        try {
  +        
  +            digester.peek(testStackName);
  +            fail("Expected an EmptyStackException");
  +            
  +        } catch (EmptyStackException e) {
  +            // expected
  +        }
  +    }
  +    
  +    /** Tests for isEmpty */
  +    public void testNamedStackIsEmpty()
  +    {
  +        String testStackName = "org.apache.commons.digester.tests.testNamedStackIsEmpty";
  +        Digester digester = new Digester();
  +        assertTrue(
  +            "A named stack that has no object pushed onto it yet should be empty", 
  +            digester.isEmpty(testStackName));
  +            
  +        digester.push(testStackName, "Some test value");
  +        assertFalse(
  +            "A named stack that has an object pushed onto it should be not empty",
  +            digester.isEmpty(testStackName));
  +            
           digester.peek(testStackName);
  +        assertFalse(
  +            "Peek should not effect whether the stack is empty",
  +            digester.isEmpty(testStackName));
  +        
  +        digester.pop(testStackName);
  +        assertTrue(
  +            "A named stack that has it's last object popped is empty", 
  +            digester.isEmpty(testStackName));
       }
       
   }
  
  
  

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


Mime
View raw message