Home » commons-lang-2.4-src » org.apache.commons » lang » exception » [javadoc | source]
    1   /*
    2    * Copyright 2002-2005 The Apache Software Foundation.
    3    * 
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    * 
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    * 
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   package org.apache.commons.lang.exception;
   17   
   18   import java.io.ByteArrayInputStream;
   19   import java.io.ByteArrayOutputStream;
   20   import java.io.EOFException;
   21   import java.io.ObjectInputStream;
   22   import java.io.ObjectOutputStream;
   23   import java.io.PrintStream;
   24   
   25   import junit.framework.Test;
   26   import junit.framework.TestSuite;
   27   import junit.textui.TestRunner;
   28   
   29   /**
   30    * Tests the org.apache.commons.lang.exception.NestableException class.
   31    *
   32    * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
   33    * @version $Id: NestableExceptionTestCase.java 161244 2005-04-14 06:16:36Z ggregory $
   34    */
   35   public class NestableExceptionTestCase extends AbstractNestableTestCase {
   36       
   37       /**
   38        * Construct a new instance of
   39        * <code>NestableExceptionTestCase</code>.
   40        *
   41        * @param name test case name
   42        */
   43       public NestableExceptionTestCase(String name)
   44       {
   45           super(name);
   46       }
   47   
   48       /**
   49        * Sets up instance variables required by this test case.
   50        */
   51       public void setUp()
   52       {
   53       }
   54       
   55       /**
   56        * Returns the test suite
   57        *
   58        * @return the test suite
   59        */
   60       public static Test suite()
   61       {
   62           return new TestSuite(NestableExceptionTestCase.class);
   63       }
   64       
   65       /**
   66        * Tears down instance variables required by this test case.
   67        */
   68       public void tearDown()
   69       {
   70       }
   71       
   72       /**
   73        * Command line entry point for running the test suite.
   74        *
   75        * @param args array of command line arguments
   76        */
   77       public static void main(String args[])
   78       {
   79           TestRunner.run(suite());
   80       }
   81       
   82       /**
   83        * @see AbstractNestableTestCase#getNestable()
   84        */
   85       public Nestable getNestable()
   86       {
   87           return new NestableException();
   88       }
   89       
   90       /**
   91        * @see AbstractNestableTestCase#getNestable(Nestable)
   92        */
   93       public Nestable getNestable(Nestable n)
   94       {
   95           return new NestableException((Throwable) n);
   96       }
   97       
   98       /**
   99        * @see AbstractNestableTestCase#getNestable(String)
  100        */
  101       public Nestable getNestable(String msg)
  102       {
  103           return new NestableException(msg);
  104       }
  105       
  106       /**
  107        * @see AbstractNestableTestCase#getNestable(Throwable)
  108        */
  109       public Nestable getNestable(Throwable t)
  110       {
  111           return new NestableException(t);
  112       }
  113       
  114       /**
  115        * @see AbstractNestableTestCase#getNestable(String, Throwable)
  116        */
  117       public Nestable getNestable(String msg, Throwable t)
  118       {
  119           return new NestableException(msg, t);
  120       }
  121       
  122       /**
  123        * @see AbstractNestableTestCase#getNestable(String, Nestable)
  124        */
  125       public Nestable getNestable(String msg, Nestable n)
  126       {
  127           return new NestableException(msg, (Throwable) n);
  128       }
  129       
  130       /**
  131        * @see AbstractNestableTestCase#getTester1(Throwable)
  132        */
  133       public Nestable getTester1(Throwable t)
  134       {
  135           return new NestableExceptionTester1(t);
  136       }
  137       
  138       /**
  139        * @see AbstractNestableTestCase#getTester1(Nestable)
  140        */
  141       public Nestable getTester1(Nestable n)
  142       {
  143           return new NestableExceptionTester1((Throwable) n);
  144       }
  145       
  146       /**
  147        * @see AbstractNestableTestCase#getTester1(String, Throwable)
  148        */
  149       public Nestable getTester1(String msg, Throwable t)
  150       {
  151           return new NestableExceptionTester1(msg, t);
  152       }
  153       
  154       /**
  155        * @see AbstractNestableTestCase#getTester1(String, Nestable)
  156        */
  157       public Nestable getTester1(String msg, Nestable n)
  158       {
  159           return new NestableExceptionTester1(msg, (Throwable) n);
  160       }
  161       
  162       /**
  163        * @see AbstractNestableTestCase#getTester1Class()
  164        */
  165       public Class getTester1Class()
  166       {
  167           return NestableExceptionTester1.class;
  168       }
  169       
  170       /**
  171        * @see AbstractNestableTestCase#getTester2(String, Throwable)
  172        */
  173       public Nestable getTester2(String msg, Throwable t)
  174       {
  175           return new NestableExceptionTester2(msg, t);
  176       }
  177       
  178       /**
  179        * @see AbstractNestableTestCase#getTester2(String, Nestable)
  180        */
  181       public Nestable getTester2(String msg, Nestable n)
  182       {
  183           return new NestableExceptionTester2(msg, (Throwable) n);
  184       }
  185       
  186       /**
  187        * @see AbstractNestableTestCase#getTester2Class()
  188        */
  189       public Class getTester2Class()
  190       {
  191           return NestableExceptionTester2.class;
  192       }
  193       
  194       /**
  195        * @see AbstractNestableTestCase#getThrowable(String)
  196        */
  197       public Throwable getThrowable(String msg)
  198       {
  199           return new EOFException(msg);
  200       }
  201       
  202       /**
  203        * @see AbstractNestableTestCase#getThrowableClass()
  204        */
  205       public Class getThrowableClass()
  206       {
  207           return EOFException.class;
  208       }
  209   
  210       /**
  211        * @see AbstractNestableTestCase#getBaseThrowableClass()
  212        */
  213       public Class getBaseThrowableClass()
  214       {
  215           return Exception.class;
  216       }
  217       
  218       public void testSpecificPrintStackTrace()
  219       {
  220           ByteArrayOutputStream baos = new ByteArrayOutputStream();
  221           PrintStream ps = new PrintStream(baos);
  222           NestableException ne = new NestableException("outer", new NestableException("inner", new Exception("another exception")));
  223           for(int i = 0; i < 2; i++)
  224           {
  225               if(i == 0)
  226               {
  227                   // Test printStackTrac()
  228                   // Replace System.err with our own PrintStream so that we can
  229                   // obtain and check the printStrackTrace output
  230                   PrintStream err = System.err;
  231                   System.setErr(ps);
  232                   ne.printStackTrace();
  233                   // Restore the System.err
  234                   System.setErr(err);
  235               }
  236               else
  237               {
  238                   // Test printStackTrace(PrintStream)
  239                   ne.printStackTrace(ps);
  240               }
  241           }
  242           String msg = baos.toString();
  243           assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableException: outer"));
  244           assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") >= 0);
  245           assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0);
  246           assertTrue( "printStackTrace() inner message after outer message", 
  247               msg.indexOf("org.apache.commons.lang.exception.NestableException: outer") <
  248               msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner"));
  249           assertTrue( "printStackTrace() cause message after inner message",
  250               msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") <
  251               msg.indexOf("Caused by: java.lang.Exception: another exception"));
  252       }
  253       
  254       public void testSerialization()
  255           throws java.io.IOException, ClassNotFoundException
  256       {
  257           RuntimeException nestedEx = new RuntimeException("nested exception message");
  258           NestableExceptionTester1 ex = new NestableExceptionTester1("serialization test", nestedEx);
  259   
  260           assertTrue( "implements java.io.Serializable", nestedEx instanceof java.io.Serializable);
  261           
  262           assertTrue( "implements java.io.Serializable", ex instanceof java.io.Serializable);
  263           
  264           ByteArrayOutputStream baos = new ByteArrayOutputStream();
  265           ByteArrayInputStream bais = null;
  266           ObjectOutputStream oos = null;
  267           ObjectInputStream ois = null;
  268   
  269           try
  270           {        
  271               oos = new ObjectOutputStream(baos);
  272               oos.writeObject(ex);
  273               oos.flush();
  274               bais = new ByteArrayInputStream(baos.toByteArray());
  275               ois = new ObjectInputStream(bais);
  276               NestableExceptionTester1 deserializedEx = (NestableExceptionTester1) ois.readObject();
  277               assertEquals( 
  278                       "getThrowableCount() return value",
  279                           ex.getThrowableCount(),
  280                           deserializedEx.getThrowableCount());
  281               
  282               for (int i = 0; i < ex.getThrowableCount(); i++)
  283               {
  284                   Throwable t = ex.getThrowable(i);
  285                   Throwable deserializedThrowable = deserializedEx.getThrowable(i);
  286                   
  287                   assertEquals( t.getClass(),
  288                           deserializedThrowable.getClass());
  289                           
  290                   assertEquals(
  291                       t.getMessage(),
  292                       deserializedThrowable.getMessage());
  293               }
  294           }
  295           finally
  296           {
  297               if (null != oos)
  298               {
  299                   try
  300                   {
  301                       oos.close();
  302                   }
  303                   catch (Exception ignored)
  304                   {
  305                       // intentionally empty
  306                   }
  307               }
  308           }
  309           
  310       }
  311   }
  312   
  313   /**
  314    * First nestable tester implementation for use in test cases.
  315    */
  316   class NestableExceptionTester1 extends NestableException
  317   {
  318       public NestableExceptionTester1()
  319       {
  320           super();
  321       }
  322   
  323       public NestableExceptionTester1(String reason, Throwable cause)
  324       {
  325           super(reason, cause);
  326       }
  327       
  328       public NestableExceptionTester1(String reason)
  329       {
  330           super(reason);
  331       }
  332       
  333       public NestableExceptionTester1(Throwable cause)
  334       {
  335           super(cause);
  336       }
  337       
  338   }
  339   
  340   /**
  341    * Second nestable tester implementation for use in test cases.
  342    */
  343   class NestableExceptionTester2 extends NestableException
  344   {
  345       public NestableExceptionTester2()
  346       {
  347           super();
  348       }
  349       
  350       public NestableExceptionTester2(String reason, Throwable cause)
  351       {
  352           super(reason, cause);
  353       }
  354       
  355       public NestableExceptionTester2(String reason)
  356       {
  357           super(reason);
  358       }
  359       
  360       public NestableExceptionTester2(Throwable cause)
  361       {
  362           super(cause);
  363       }
  364       
  365   }
  366   

Save This Page
Home » commons-lang-2.4-src » org.apache.commons » lang » exception » [javadoc | source]