commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stevencasw...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/exception NestableDelegateTestCase.java NestableExceptionTestCase.java NestableRuntimeExceptionTestCase.java
Date Fri, 26 Jul 2002 20:30:58 GMT
stevencaswell    2002/07/26 13:30:58

  Modified:    lang/src/test/org/apache/commons/lang/exception
                        NestableDelegateTestCase.java
                        NestableExceptionTestCase.java
                        NestableRuntimeExceptionTestCase.java
  Log:
  updated tests to reflect the following changes:
  deprecated method getLength(), replaced with getThrowableCount();
  deprecated indexOfThrowable(int, Class), replaced with indexOfThrowable(Class, int) to make
signature consistent with other APIs;
  changed functionality of methods that receive an index argument to throw an IndexOutOfBoundsException
if the index is specified incorrectly, rather than absorbing invalid indices silently, to
bring in line with the String API
  
  Revision  Changes    Path
  1.2       +232 -46   jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java
  
  Index: NestableDelegateTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NestableDelegateTestCase.java	19 Jul 2002 03:35:55 -0000	1.1
  +++ NestableDelegateTestCase.java	26 Jul 2002 20:30:58 -0000	1.2
  @@ -153,36 +153,36 @@
               nd2.getMessage("base").equals("base: " + ne2.getCause().getMessage()));
       }
   
  -    public void testNestableDelegateGetLength()
  +    public void testNestableDelegateGetThrowableCount()
       {
           Nestable n = null;
           NestableDelegate d = null;
           
           n = new NestableDelegateTester1();
           d = new NestableDelegate(n);
  -        doNestableDelegateGetLength(d, 1);
  +        doNestableDelegateGetThrowableCount(d, 1);
           
           n = new NestableDelegateTester1("level 1");
           d = new NestableDelegate(n);
  -        doNestableDelegateGetLength(d, 1);
  +        doNestableDelegateGetThrowableCount(d, 1);
           
           n = new NestableDelegateTester1(new Exception());
           d = new NestableDelegate(n);
  -        doNestableDelegateGetLength(d, 2);
  +        doNestableDelegateGetThrowableCount(d, 2);
           
           n = new NestableDelegateTester1(new Exception("level 2"));
           d = new NestableDelegate(n);
  -        doNestableDelegateGetLength(d, 2);
  +        doNestableDelegateGetThrowableCount(d, 2);
           
           n = new NestableDelegateTester1("level 1", new NestableDelegateTester2("level 2",
new NestableDelegateTester1(new NestableDelegateTester2("level 4", new Exception("level 5")))));
           d = new NestableDelegate(n);
  -        doNestableDelegateGetLength(d, 5);
  +        doNestableDelegateGetThrowableCount(d, 5);
       }
   
  -    private void doNestableDelegateGetLength(NestableDelegate d, int len)
  +    private void doNestableDelegateGetThrowableCount(NestableDelegate d, int len)
       {
           // Compare the lengths
  -        assertEquals("delegate length", len, d.getLength());
  +        assertEquals("delegate length", len, d.getThrowableCount());
       }
       
       public void testNestableDelegateGetMessages()
  @@ -252,11 +252,27 @@
           {
               assertEquals("message " + i, msgs[i], d.getMessage(i));
           }
  -        assertEquals("message -1", msgs[0], d.getMessage(-1));
  -        assertEquals("message -1", msgs[msgs.length - 1], d.getMessage(msgs.length + 100));
  +        
  +        // Test for index out of bounds
  +        try
  +        {
  +            String msg = d.getMessage(-1);
  +            fail("getMessage(-1) should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException ioode)
  +        {
  +        }
  +        try
  +        {
  +            String msg = d.getMessage(msgs.length + 100);
  +            fail("getMessage(999) should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException ioode)
  +        {
  +        }
       }
   
  -    public void testNestableDelegateGetThrowable()
  +    public void testNestableDelegateGetThrowableN()
       {
           Nestable n = null;
           NestableDelegate d = null;
  @@ -271,7 +287,7 @@
           throwables[1] = Exception.class;
           n = new NestableDelegateTester1(new Exception(msgs[1]));
           d = new NestableDelegate(n);
  -        doNestableDelegateGetThrowable(d, throwables, msgs);
  +        doNestableDelegateGetThrowableN(d, throwables, msgs);
    
           msgs = new String[5];
           msgs[0] = "level 1";
  @@ -287,10 +303,10 @@
           throwables[4] = Exception.class;        
           n = new NestableDelegateTester1(msgs[0], new NestableDelegateTester2(msgs[1], new
NestableDelegateTester1(new NestableDelegateTester2(msgs[3], new Exception(msgs[4])))));
           d = new NestableDelegate(n);
  -        doNestableDelegateGetThrowable(d, throwables, msgs);
  +        doNestableDelegateGetThrowableN(d, throwables, msgs);
       }
   
  -    private void doNestableDelegateGetThrowable(NestableDelegate d, Class[] classes, String[]
msgs)
  +    private void doNestableDelegateGetThrowableN(NestableDelegate d, Class[] classes, String[]
msgs)
       {
           Throwable t = null;
           String msg = null;
  @@ -309,28 +325,24 @@
               }
               assertEquals("throwable message", msgs[i], msg);
           }
  -        t = d.getThrowable(-1);
  -        assertEquals("throwable(-1)", classes[0], t.getClass());
  -        if(Nestable.class.isInstance(t))
  +        
  +        // Test for index out of bounds
  +        try
           {
  -            msg = ((Nestable) t).getMessage(0);
  +            t = d.getThrowable(-1);
  +            fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
           }
  -        else
  +        catch(IndexOutOfBoundsException ioobe)
           {
  -            msg = t.getMessage();
           }
  -        assertEquals("throwable message", msgs[0], msg);
  -        t = d.getThrowable(999);
  -        assertEquals("throwable(999)", classes[classes.length - 1], t.getClass());
  -        if(Nestable.class.isInstance(t))
  +        try
           {
  -            msg = ((Nestable) t).getMessage(0);
  +            t = d.getThrowable(999);
  +            fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
           }
  -        else
  +        catch(IndexOutOfBoundsException ioobe)
           {
  -            msg = t.getMessage();
           }
  -        assertEquals("throwable message", msgs[msgs.length - 1], msg);
       }
   
       public void testNestableDelegateGetThrowables()
  @@ -418,28 +430,52 @@
           }
           doNestableDelegateIndexOfThrowable(d, NestableDelegateTester2.class, 2, 3, msgs[3]);
           doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 1, 2, msgs[2]);
  +        doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 3, -1, null);
  +        doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 4, -1, null);
  +        doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 4, msgs[4]);
           doNestableDelegateIndexOfThrowable(d, java.util.Date.class, 0, -1, null);
  +        
  +        // Test for index out of bounds
  +        try
  +        {
  +            int index = d.indexOfThrowable(NestableDelegateTester1.class, -1);
  +            fail("method should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException iooob)
  +        {
  +        }
  +        try
  +        {
  +            int index = d.indexOfThrowable(NestableDelegateTester1.class, 5);
  +            fail("method should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException iooob)
  +        {
  +        }
       }
   
  -    private void doNestableDelegateIndexOfThrowable(NestableDelegate d, Class type, int
pos, int expectedIndex, String expectedMsg)
  +    private void doNestableDelegateIndexOfThrowable(NestableDelegate d, Class type, int
fromIndex, int expectedIndex, String expectedMsg)
       {
           Throwable t = null;
           
  -        int index = d.indexOfThrowable(pos, type);
  +        int index = d.indexOfThrowable(type, fromIndex);
           assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  -        t = d.getThrowable(index);
  -        if(expectedMsg != null)
  +        if(expectedIndex > -1)
           {
  -            String msg = null;
  -            if(Nestable.class.isInstance(t))
  -            {
  -                msg = ((Nestable) t).getMessage(0);
  -            }
  -            else
  +            t = d.getThrowable(index);
  +            if(expectedMsg != null)
               {
  -                msg = t.getMessage();
  +                String msg = null;
  +                if(Nestable.class.isInstance(t))
  +                {
  +                    msg = ((Nestable) t).getMessage(0);
  +                }
  +                else
  +                {
  +                    msg = t.getMessage();
  +                }
  +                assertEquals("message of indexed throwable", expectedMsg, msg);
               }
  -            assertEquals("message of indexed throwable", expectedMsg, msg);
           }
       }
       
  @@ -571,7 +607,7 @@
        * Returns the number of nested <code>Throwable</code>s represented by
        * this <code>Nestable</code>, including this <code>Nestable</code>.
        */
  -    public int getLength()
  +    public int getThrowableCount()
       {
           return 1;
       }
  @@ -639,6 +675,36 @@
        * @return index of the first occurrence of the type in the chain, or -1 if
        * the type is not found
        */
  +    public int indexOfThrowable(Class type, int fromIndex)
  +    {
  +        return -1;
  +    }
  +    
  +    /** Returns the number of nested <code>Throwable</code>s represented by
  +     * this <code>Nestable</code>, including this <code>Nestable</code>.
  +     * @deprecated
  +     */
  +    public int getLength()
  +    {
  +        return 1;
  +    }
  +    
  +    /** Returns the index, numbered from 0, of the first <code>Throwable</code>
  +     * that matches the specified type in the chain of <code>Throwable</code>s
  +     * with an index greater than or equal to the specified position, or -1 if
  +     * the type is not found. If <code>pos</code> is negative, the effect is
the
  +     * same as if it were 0. If <code>pos</code> is greater than or equal to
the
  +     * length of the chain, the effect is the same as if it were the index of
  +     * the last element in the chain.
  +     *
  +     * @param pos index, numbered from 0, of the starting position in the chain
  +     * to be searched
  +     * @param type <code>Class</code> to be found
  +     *
  +     * @return index of the first occurrence of the type in the chain, or -1 if
  +     * the type is not found
  +     * @deprecated replaced by {@link #indexOfThrowable(Class, int)}
  +     */
       public int indexOfThrowable(int pos, Class type)
       {
           return -1;
  @@ -735,7 +801,7 @@
        * Returns the number of nested <code>Throwable</code>s represented by
        * this <code>Nestable</code>, including this <code>Nestable</code>.
        */
  -    public int getLength()
  +    public int getThrowableCount()
       {
           return 1;
       }
  @@ -803,6 +869,36 @@
        * @return index of the first occurrence of the type in the chain, or -1 if
        * the type is not found
        */
  +    public int indexOfThrowable(Class type, int fromIndex)
  +    {
  +        return -1;
  +    }
  +    
  +    /** Returns the number of nested <code>Throwable</code>s represented by
  +     * this <code>Nestable</code>, including this <code>Nestable</code>.
  +     * @deprecated
  +     */
  +    public int getLength()
  +    {
  +        return 1;
  +    }
  +    
  +    /** Returns the index, numbered from 0, of the first <code>Throwable</code>
  +     * that matches the specified type in the chain of <code>Throwable</code>s
  +     * with an index greater than or equal to the specified position, or -1 if
  +     * the type is not found. If <code>pos</code> is negative, the effect is
the
  +     * same as if it were 0. If <code>pos</code> is greater than or equal to
the
  +     * length of the chain, the effect is the same as if it were the index of
  +     * the last element in the chain.
  +     *
  +     * @param pos index, numbered from 0, of the starting position in the chain
  +     * to be searched
  +     * @param type <code>Class</code> to be found
  +     *
  +     * @return index of the first occurrence of the type in the chain, or -1 if
  +     * the type is not found
  +     * @deprecated replaced by {@link #indexOfThrowable(Class, int)}
  +     */
       public int indexOfThrowable(int pos, Class type)
       {
           return -1;
  @@ -818,7 +914,7 @@
        * Returns the number of nested <code>Throwable</code>s represented by
        * this <code>Nestable</code>, including this <code>Nestable</code>.
        */
  -    public int getLength()
  +    public int getThrowableCount()
       {
           return 1;
       }
  @@ -929,6 +1025,36 @@
        * @return index of the first occurrence of the type in the chain, or -1 if
        * the type is not found
        */
  +    public int indexOfThrowable(Class type, int fromIndex)
  +    {
  +        return indexOfThrowable(type);
  +    }
  +    
  +    /** Returns the number of nested <code>Throwable</code>s represented by
  +     * this <code>Nestable</code>, including this <code>Nestable</code>.
  +     * @deprecated
  +     */
  +    public int getLength()
  +    {
  +        return 1;
  +    }
  +    
  +    /** Returns the index, numbered from 0, of the first <code>Throwable</code>
  +     * that matches the specified type in the chain of <code>Throwable</code>s
  +     * with an index greater than or equal to the specified position, or -1 if
  +     * the type is not found. If <code>pos</code> is negative, the effect is
the
  +     * same as if it were 0. If <code>pos</code> is greater than or equal to
the
  +     * length of the chain, the effect is the same as if it were the index of
  +     * the last element in the chain.
  +     *
  +     * @param pos index, numbered from 0, of the starting position in the chain
  +     * to be searched
  +     * @param type <code>Class</code> to be found
  +     *
  +     * @return index of the first occurrence of the type in the chain, or -1 if
  +     * the type is not found
  +     * @deprecated replaced by {@link #indexOfThrowable(Class, int)}
  +     */
       public int indexOfThrowable(int pos, Class type)
       {
           return indexOfThrowable(type);
  @@ -945,7 +1071,7 @@
           this.cause = cause;
       }
       
  -    public int getLength()
  +    public int getThrowableCount()
       {
           return 1;
       }
  @@ -1046,6 +1172,36 @@
        * @return index of the first occurrence of the type in the chain, or -1 if
        * the type is not found
        */
  +    public int indexOfThrowable(Class type, int fromIndex)
  +    {
  +        return indexOfThrowable(type);
  +    }
  +    
  +    /** Returns the number of nested <code>Throwable</code>s represented by
  +     * this <code>Nestable</code>, including this <code>Nestable</code>.
  +     * @deprecated
  +     */
  +    public int getLength()
  +    {
  +        return 1;
  +    }
  +    
  +    /** Returns the index, numbered from 0, of the first <code>Throwable</code>
  +     * that matches the specified type in the chain of <code>Throwable</code>s
  +     * with an index greater than or equal to the specified position, or -1 if
  +     * the type is not found. If <code>pos</code> is negative, the effect is
the
  +     * same as if it were 0. If <code>pos</code> is greater than or equal to
the
  +     * length of the chain, the effect is the same as if it were the index of
  +     * the last element in the chain.
  +     *
  +     * @param pos index, numbered from 0, of the starting position in the chain
  +     * to be searched
  +     * @param type <code>Class</code> to be found
  +     *
  +     * @return index of the first occurrence of the type in the chain, or -1 if
  +     * the type is not found
  +     * @deprecated replaced by {@link #indexOfThrowable(Class, int)}
  +     */
       public int indexOfThrowable(int pos, Class type)
       {
           return indexOfThrowable(type);
  @@ -1055,7 +1211,7 @@
   
   class NonThrowableNestable implements Nestable
   {
  -    public int getLength()
  +    public int getThrowableCount()
       {
           return 1;
       }
  @@ -1147,6 +1303,36 @@
        * @param pos
        * @return index of the first occurrence of the type in the chain, or -1 if
        * the type is not found
  +     */
  +    public int indexOfThrowable(Class type, int fromIndex)
  +    {
  +        return -1;
  +    }
  +    
  +    /** Returns the number of nested <code>Throwable</code>s represented by
  +     * this <code>Nestable</code>, including this <code>Nestable</code>.
  +     * @deprecated
  +     */
  +    public int getLength()
  +    {
  +        return 1;
  +    }
  +    
  +    /** Returns the index, numbered from 0, of the first <code>Throwable</code>
  +     * that matches the specified type in the chain of <code>Throwable</code>s
  +     * with an index greater than or equal to the specified position, or -1 if
  +     * the type is not found. If <code>pos</code> is negative, the effect is
the
  +     * same as if it were 0. If <code>pos</code> is greater than or equal to
the
  +     * length of the chain, the effect is the same as if it were the index of
  +     * the last element in the chain.
  +     *
  +     * @param pos index, numbered from 0, of the starting position in the chain
  +     * to be searched
  +     * @param type <code>Class</code> to be found
  +     *
  +     * @return index of the first occurrence of the type in the chain, or -1 if
  +     * the type is not found
  +     * @deprecated replaced by {@link #indexOfThrowable(Class, int)}
        */
       public int indexOfThrowable(int pos, Class type)
       {
  
  
  
  1.2       +142 -47   jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java
  
  Index: NestableExceptionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NestableExceptionTestCase.java	19 Jul 2002 03:35:55 -0000	1.1
  +++ NestableExceptionTestCase.java	26 Jul 2002 20:30:58 -0000	1.2
  @@ -128,6 +128,7 @@
   
       public void testGetLength()
       {
  +        // test the deprecated method
           NestableException ne1 = new NestableException();
           assertEquals("ne1 length", 1, ne1.getLength());
   
  @@ -153,6 +154,34 @@
           assertEquals("ne 8 length", 5, ne8.getLength());
       }
       
  +    public void testGetThrowableCount()
  +    {
  +        NestableException ne1 = new NestableException();
  +        assertEquals("ne1 throwable count", 1, ne1.getThrowableCount());
  +
  +        NestableException ne2 = new NestableException("ne2");
  +        assertEquals("ne2 throwable count", 1, ne2.getThrowableCount());
  +        
  +        NestableException ne3 = new NestableException(new Exception("ne3 exception"));
  +        assertEquals("ne3 throwable count", 2, ne3.getThrowableCount());
  +        
  +        NestableException ne4 = new NestableException("ne4", new Exception("ne4 exception"));
  +        assertEquals("ne4 throwable count", 2, ne4.getThrowableCount());
  +        
  +        NestableException ne5 = new NestableException("ne5", null);
  +        assertEquals("ne 5 throwable count", 1, ne5.getThrowableCount());
  +        
  +        NestableException ne6 = new NestableException(null, new Exception("ne6 exception"));
  +        assertEquals("ne 6 throwable count", 2, ne6.getThrowableCount());
  +        
  +        NestableException ne7 = new NestableException("ne7o", new NestableException("ne7i",
new Exception("ne7 exception")));
  +        assertEquals("ne 7 throwable count", 3, ne7.getThrowableCount());
  +
  +        NestableException ne8 = new NestableException("level 1", new NestableException("level
2", new NestableException(new NestableException("level 4", new Exception("level 5")))));
  +        assertEquals("ne 8 throwable count", 5, ne8.getThrowableCount());
  +    }
  +    
  +
       public void testGetMessage()
       {
           NestableException ne1 = new NestableException();
  @@ -190,7 +219,7 @@
   
       }
   
  -    public void testGetMessageN()
  +    public void testGetMessageI()
       {
           String[] msgs = new String[5];
           msgs[0] = "level 1";
  @@ -203,8 +232,24 @@
           {
               assertEquals("message " + i, msgs[i], ne.getMessage(i));
           }
  -        assertEquals("message -1", msgs[0], ne.getMessage(-1));
  -        assertEquals("message 999", msgs[4], ne.getMessage(999));
  +        
  +        // Test for index out of bounds
  +        try
  +        {
  +            String msg = ne.getMessage(-1);
  +            fail("getMessage(-1) should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException ioode)
  +        {
  +        }
  +        try
  +        {
  +            String msg = ne.getMessage(msgs.length + 100);
  +            fail("getMessage(999) should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException ioode)
  +        {
  +        }
       }
       
       public void testGetMessages()
  @@ -224,7 +269,7 @@
           }
       }
   
  -    public void testGetThrowable()
  +    public void testGetThrowableI()
       {
           Nestable n = null;
           String msgs[] = null;
  @@ -237,7 +282,7 @@
           throwables[0] = NestableExceptionTester1.class;
           throwables[1] = Exception.class;
           n = new NestableExceptionTester1(new Exception(msgs[1]));
  -        doNestableExceptionGetThrowable(n, throwables, msgs);
  +        doNestableExceptionGetThrowableI(n, throwables, msgs);
    
           msgs = new String[5];
           msgs[0] = "level 1";
  @@ -252,10 +297,10 @@
           throwables[3] = NestableExceptionTester2.class;
           throwables[4] = Exception.class;        
           n = new NestableExceptionTester1(msgs[0], new NestableExceptionTester2(msgs[1],
new NestableExceptionTester1(new NestableExceptionTester2(msgs[3], new Exception(msgs[4])))));
  -        doNestableExceptionGetThrowable(n, throwables, msgs);
  +        doNestableExceptionGetThrowableI(n, throwables, msgs);
       }
       
  -    private void doNestableExceptionGetThrowable(Nestable n, Class[] classes, String[]
msgs)
  +    private void doNestableExceptionGetThrowableI(Nestable n, Class[] classes, String[]
msgs)
       {
           Throwable t = null;
           String msg = null;
  @@ -274,28 +319,24 @@
               }
               assertEquals("throwable message", msgs[i], msg);
           }
  -        t = n.getThrowable(-1);
  -        assertEquals("throwable(-1)", classes[0], t.getClass());
  -        if(Nestable.class.isInstance(t))
  +        
  +        // Test for index out of bounds
  +        try
           {
  -            msg = ((Nestable) t).getMessage(0);
  +            t = n.getThrowable(-1);
  +            fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
           }
  -        else
  +        catch(IndexOutOfBoundsException ioobe)
           {
  -            msg = t.getMessage();
           }
  -        assertEquals("throwable message", msgs[0], msg);
  -        t = n.getThrowable(999);
  -        assertEquals("throwable(999)", classes[classes.length - 1], t.getClass());
  -        if(Nestable.class.isInstance(t))
  +        try
           {
  -            msg = ((Nestable) t).getMessage(0);
  +            t = n.getThrowable(999);
  +            fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
           }
  -        else
  +        catch(IndexOutOfBoundsException ioobe)
           {
  -            msg = t.getMessage();
           }
  -        assertEquals("throwable message", msgs[msgs.length - 1], msg);
       }
       
       public void testGetThrowables()
  @@ -384,23 +425,26 @@
           
           int index = n.indexOfThrowable(type);
           assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  -        t = n.getThrowable(index);
  -        if(expectedMsg != null)
  +        if(expectedIndex > -1)
           {
  -            String msg = null;
  -            if(Nestable.class.isInstance(t))
  +            t = n.getThrowable(index);
  +            if(expectedMsg != null)
               {
  -                msg = ((Nestable) t).getMessage(0);
  +                String msg = null;
  +                if(Nestable.class.isInstance(t))
  +                {
  +                    msg = ((Nestable) t).getMessage(0);
  +                }
  +                else
  +                {
  +                    msg = t.getMessage();
  +                }
  +                assertEquals("message of indexed throwable", expectedMsg, msg);
               }
  -            else
  -            {
  -                msg = t.getMessage();
  -            }
  -            assertEquals("message of indexed throwable", expectedMsg, msg);
           }
       }
       
  -    public void testIndexOfThrowableN()
  +    public void testIndexOfThrowableI()
       {
           Nestable n = null;
           String msgs[] = null;
  @@ -422,32 +466,83 @@
           n = new NestableExceptionTester1(msgs[0], new NestableExceptionTester2(msgs[1],
new NestableExceptionTester1(new NestableExceptionTester2(msgs[3], new Exception(msgs[4])))));
           for(int i = 0; i < throwables.length; i++)
           {
  -            doNestableExceptionIndexOfThrowableN(n, throwables[i], 0, indexes[i], msgs[indexes[i]]);
  +            doNestableExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i], msgs[indexes[i]]);
  +        }
  +        doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester2.class, 2, 3, msgs[3]);
  +        doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester1.class, 1, 2, msgs[2]);
  +        doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester1.class, 3, -1,
null);
  +        doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester1.class, 4, -1,
null);
  +        doNestableExceptionIndexOfThrowableI(n, Exception.class, 2, 4, msgs[4]);
  +        doNestableExceptionIndexOfThrowableI(n, java.util.Date.class, 0, -1, null);
  +        
  +        // Test for index out of bounds
  +        try
  +        {
  +            int index = n.indexOfThrowable(NestableExceptionTester1.class, -1);
  +            fail("method should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException iooob)
  +        {
           }
  -        doNestableExceptionIndexOfThrowableN(n, NestableExceptionTester2.class, 2, 3, msgs[3]);
  -        doNestableExceptionIndexOfThrowableN(n, NestableExceptionTester1.class, 1, 2, msgs[2]);
  -        doNestableExceptionIndexOfThrowableN(n, java.util.Date.class, 0, -1, null);
  +        try
  +        {
  +            int index = n.indexOfThrowable(NestableExceptionTester1.class, 5);
  +            fail("method should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException iooob)
  +        {
  +        }
  +        
  +        // test the deprecated method
  +        int index = n.indexOfThrowable(-1, NestableExceptionTester1.class);
  +        assertEquals("deprecated method index", 0, index);
  +        index = n.indexOfThrowable(999, Exception.class);
  +        assertEquals("deprecated method index", 4, index);
       }
   
  -    private void doNestableExceptionIndexOfThrowableN(Nestable n, Class type, int pos,
int expectedIndex, String expectedMsg)
  +    private void doNestableExceptionIndexOfThrowableI(Nestable n, Class type, int fromIndex,
int expectedIndex, String expectedMsg)
       {
           Throwable t = null;
           
  -        int index = n.indexOfThrowable(pos, type);
  +        int index = n.indexOfThrowable(type, fromIndex);
           assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  -        t = n.getThrowable(index);
  -        if(expectedMsg != null)
  +        if(expectedIndex > -1)
           {
  -            String msg = null;
  -            if(Nestable.class.isInstance(t))
  +            t = n.getThrowable(index);
  +            if(expectedMsg != null)
               {
  -                msg = ((Nestable) t).getMessage(0);
  +                String msg = null;
  +                if(Nestable.class.isInstance(t))
  +                {
  +                    msg = ((Nestable) t).getMessage(0);
  +                }
  +                else
  +                {
  +                    msg = t.getMessage();
  +                }
  +                assertEquals("message of indexed throwable", expectedMsg, msg);
               }
  -            else
  +        }
  +        
  +        // test the deprecated method
  +        int index1 = n.indexOfThrowable(fromIndex, type);
  +        assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  +        if(expectedIndex > -1)
  +        {
  +            t = n.getThrowable(index1);
  +            if(expectedMsg != null)
               {
  -                msg = t.getMessage();
  +                String msg = null;
  +                if(Nestable.class.isInstance(t))
  +                {
  +                    msg = ((Nestable) t).getMessage(0);
  +                }
  +                else
  +                {
  +                    msg = t.getMessage();
  +                }
  +                assertEquals("message of indexed throwable", expectedMsg, msg);
               }
  -            assertEquals("message of indexed throwable", expectedMsg, msg);
           }
       }
       
  
  
  
  1.2       +128 -39   jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java
  
  Index: NestableRuntimeExceptionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NestableRuntimeExceptionTestCase.java	19 Jul 2002 03:35:56 -0000	1.1
  +++ NestableRuntimeExceptionTestCase.java	26 Jul 2002 20:30:58 -0000	1.2
  @@ -125,8 +125,36 @@
               ne6.getCause()); 
       }
       
  +    public void testGetThrowableCount()
  +    {
  +        NestableRuntimeException ne1 = new NestableRuntimeException();
  +        assertEquals("ne1 throwable count", 1, ne1.getThrowableCount());
  +
  +        NestableRuntimeException ne2 = new NestableRuntimeException("ne2");
  +        assertEquals("ne2 throwable count", 1, ne2.getThrowableCount());
  +        
  +        NestableRuntimeException ne3 = new NestableRuntimeException(new Exception("ne3
exception"));
  +        assertEquals("ne3 throwable count", 2, ne3.getThrowableCount());
  +        
  +        NestableRuntimeException ne4 = new NestableRuntimeException("ne4", new Exception("ne4
exception"));
  +        assertEquals("ne4 throwable count", 2, ne4.getThrowableCount());
  +        
  +        NestableRuntimeException ne5 = new NestableRuntimeException("ne5", null);
  +        assertEquals("ne 5 throwable count", 1, ne5.getThrowableCount());
  +        
  +        NestableRuntimeException ne6 = new NestableRuntimeException(null, new Exception("ne6
exception"));
  +        assertEquals("ne 6 throwable count", 2, ne6.getThrowableCount());
  +        
  +        NestableRuntimeException ne7 = new NestableRuntimeException("ne7o", new NestableRuntimeException("ne7i",
new Exception("ne7 exception")));
  +        assertEquals("ne 7 throwable count", 3, ne7.getThrowableCount());
  +
  +        NestableRuntimeException ne8 = new NestableRuntimeException("level 1", new NestableRuntimeException("level
2", new NestableRuntimeException(new NestableRuntimeException("level 4", new Exception("level
5")))));
  +        assertEquals("ne 8 throwable count", 5, ne8.getThrowableCount());
  +    }
  +    
       public void testGetLength()
       {
  +        // test the deprecated method
           NestableRuntimeException ne1 = new NestableRuntimeException();
           assertEquals("ne1 length", 1, ne1.getLength());
   
  @@ -151,7 +179,7 @@
           NestableRuntimeException ne8 = new NestableRuntimeException("level 1", new NestableRuntimeException("level
2", new NestableRuntimeException(new NestableRuntimeException("level 4", new Exception("level
5")))));
           assertEquals("ne 8 length", 5, ne8.getLength());
       }
  -    
  +
       public void testGetMessage()
       {
           NestableRuntimeException ne1 = new NestableRuntimeException();
  @@ -188,7 +216,7 @@
               ne7.getMessage().equals("ne7o: ne7i: ne7 exception")); 
       }
       
  -    public void testGetMessageN()
  +    public void testGetMessageI()
       {
           String[] msgs = new String[5];
           msgs[0] = "level 1";
  @@ -201,8 +229,24 @@
           {
               assertEquals("message " + i, msgs[i], ne.getMessage(i));
           }
  -        assertEquals("message -1", msgs[0], ne.getMessage(-1));
  -        assertEquals("message 999", msgs[4], ne.getMessage(999));
  +        
  +        // Test for index out of bounds
  +        try
  +        {
  +            String msg = ne.getMessage(-1);
  +            fail("getMessage(-1) should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException ioode)
  +        {
  +        }
  +        try
  +        {
  +            String msg = ne.getMessage(msgs.length + 100);
  +            fail("getMessage(999) should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException ioode)
  +        {
  +        }
       }
       
       public void testGetMessages()
  @@ -222,7 +266,7 @@
           }
       }
   
  -    public void testGetThrowable()
  +    public void testGetThrowableI()
       {
           Nestable n = null;
           String msgs[] = null;
  @@ -235,7 +279,7 @@
           throwables[0] = NestableRuntimeExceptionTester1.class;
           throwables[1] = Exception.class;
           n = new NestableRuntimeExceptionTester1(new Exception(msgs[1]));
  -        doNestableRuntimeExceptionGetThrowable(n, throwables, msgs);
  +        doNestableRuntimeExceptionGetThrowableI(n, throwables, msgs);
    
           msgs = new String[5];
           msgs[0] = "level 1";
  @@ -250,10 +294,10 @@
           throwables[3] = NestableRuntimeExceptionTester2.class;
           throwables[4] = Exception.class;        
           n = new NestableRuntimeExceptionTester1(msgs[0], new NestableRuntimeExceptionTester2(msgs[1],
new NestableRuntimeExceptionTester1(new NestableRuntimeExceptionTester2(msgs[3], new Exception(msgs[4])))));
  -        doNestableRuntimeExceptionGetThrowable(n, throwables, msgs);
  +        doNestableRuntimeExceptionGetThrowableI(n, throwables, msgs);
       }
       
  -    private void doNestableRuntimeExceptionGetThrowable(Nestable n, Class[] classes, String[]
msgs)
  +    private void doNestableRuntimeExceptionGetThrowableI(Nestable n, Class[] classes, String[]
msgs)
       {
           Throwable t = null;
           String msg = null;
  @@ -272,28 +316,24 @@
               }
               assertEquals("throwable message", msgs[i], msg);
           }
  -        t = n.getThrowable(-1);
  -        assertEquals("throwable(-1)", classes[0], t.getClass());
  -        if(Nestable.class.isInstance(t))
  +        
  +        // Test for index out of bounds
  +        try
           {
  -            msg = ((Nestable) t).getMessage(0);
  +            t = n.getThrowable(-1);
  +            fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
           }
  -        else
  +        catch(IndexOutOfBoundsException ioobe)
           {
  -            msg = t.getMessage();
           }
  -        assertEquals("throwable message", msgs[0], msg);
  -        t = n.getThrowable(999);
  -        assertEquals("throwable(999)", classes[classes.length - 1], t.getClass());
  -        if(Nestable.class.isInstance(t))
  +        try
           {
  -            msg = ((Nestable) t).getMessage(0);
  +            t = n.getThrowable(999);
  +            fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
           }
  -        else
  +        catch(IndexOutOfBoundsException ioobe)
           {
  -            msg = t.getMessage();
           }
  -        assertEquals("throwable message", msgs[msgs.length - 1], msg);
       }
       
       public void testGetThrowables()
  @@ -382,9 +422,9 @@
           
           int index = n.indexOfThrowable(type);
           assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  -        t = n.getThrowable(index);
           if(expectedMsg != null)
           {
  +            t = n.getThrowable(index);
               String msg = null;
               if(Nestable.class.isInstance(t))
               {
  @@ -398,7 +438,7 @@
           }
       }
       
  -    public void testIndexOfThrowableN()
  +    public void testIndexOfThrowableI()
       {
           Nestable n = null;
           String msgs[] = null;
  @@ -420,32 +460,81 @@
           n = new NestableRuntimeExceptionTester1(msgs[0], new NestableRuntimeExceptionTester2(msgs[1],
new NestableRuntimeExceptionTester1(new NestableRuntimeExceptionTester2(msgs[3], new Exception(msgs[4])))));
           for(int i = 0; i < throwables.length; i++)
           {
  -            doNestableRuntimeExceptionIndexOfThrowableN(n, throwables[i], 0, indexes[i],
msgs[indexes[i]]);
  +            doNestableRuntimeExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i],
msgs[indexes[i]]);
  +        }
  +        doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester2.class,
2, 3, msgs[3]);
  +        doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester1.class,
1, 2, msgs[2]);
  +        doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester1.class,
3, -1, null);
  +        doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester1.class,
4, -1, null);
  +        doNestableRuntimeExceptionIndexOfThrowableI(n, java.util.Date.class, 0, -1, null);
  +        
  +        try
  +        {
  +            int index = n.indexOfThrowable(NestableRuntimeExceptionTester1.class, -1);
  +            fail("method should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException iooob)
  +        {
  +        }
  +        try
  +        {
  +            int index = n.indexOfThrowable(NestableRuntimeExceptionTester1.class, 5);
  +            fail("method should have thrown IndexOutOfBoundsException");
  +        }
  +        catch(IndexOutOfBoundsException iooob)
  +        {
           }
  -        doNestableRuntimeExceptionIndexOfThrowableN(n, NestableRuntimeExceptionTester2.class,
2, 3, msgs[3]);
  -        doNestableRuntimeExceptionIndexOfThrowableN(n, NestableRuntimeExceptionTester1.class,
1, 2, msgs[2]);
  -        doNestableRuntimeExceptionIndexOfThrowableN(n, java.util.Date.class, 0, -1, null);
  +        
  +        // test the deprecated method
  +        int index = n.indexOfThrowable(-1, NestableRuntimeExceptionTester1.class);
  +        assertEquals("deprecated method index", 0, index);
  +        index = n.indexOfThrowable(999, Exception.class);
  +        assertEquals("deprecated method index", 4, index);
       }
   
  -    private void doNestableRuntimeExceptionIndexOfThrowableN(Nestable n, Class type, int
pos, int expectedIndex, String expectedMsg)
  +    private void doNestableRuntimeExceptionIndexOfThrowableI(Nestable n, Class type, int
fromIndex, int expectedIndex, String expectedMsg)
       {
           Throwable t = null;
           
  -        int index = n.indexOfThrowable(pos, type);
  +        int index = n.indexOfThrowable(type, fromIndex);
           assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  -        t = n.getThrowable(index);
  -        if(expectedMsg != null)
  +        if(expectedIndex > -1)
           {
  -            String msg = null;
  -            if(Nestable.class.isInstance(t))
  +            t = n.getThrowable(index);
  +            if(expectedMsg != null)
               {
  -                msg = ((Nestable) t).getMessage(0);
  +                String msg = null;
  +                if(Nestable.class.isInstance(t))
  +                {
  +                    msg = ((Nestable) t).getMessage(0);
  +                }
  +                else
  +                {
  +                    msg = t.getMessage();
  +                }
  +                assertEquals("message of indexed throwable", expectedMsg, msg);
               }
  -            else
  +        }
  +        
  +        // test the deprecated method
  +        int index1 = n.indexOfThrowable(fromIndex, type);
  +        assertEquals("index of throwable " + type.getName(), expectedIndex, index);
  +        if(expectedIndex > -1)
  +        {
  +            t = n.getThrowable(index);
  +            if(expectedMsg != null)
               {
  -                msg = t.getMessage();
  +                String msg = null;
  +                if(Nestable.class.isInstance(t))
  +                {
  +                    msg = ((Nestable) t).getMessage(0);
  +                }
  +                else
  +                {
  +                    msg = t.getMessage();
  +                }
  +                assertEquals("message of indexed throwable", expectedMsg, msg);
               }
  -            assertEquals("message of indexed throwable", expectedMsg, msg);
           }
       }
       
  
  
  

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


Mime
View raw message