package org.apache.commons.lang.exception;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *    "Apache Turbine" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    "Apache Turbine", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

import junit.framework.*;
import junit.textui.TestRunner;
/**
 * Tests the org.apache.commons.lang.exception.NestableRuntimeException class.
 *
 * @author <a href="mailto:stevencaswell@yahoo.com">Steven Caswell</a>
 */
public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
{
    /**
     * Construct a new instance of NestableRuntimeExceptionTestCase with the specified name
     */
    public NestableRuntimeExceptionTestCase(String name)
    {
        super(name);
    }

    /**
     * Set up instance variables required by this test case.
     */
    public void setUp()
    {
    }
    
    public static Test suite()
    {
        return new TestSuite(NestableRuntimeExceptionTestCase.class);
    }
    
    /**
     * Tear down instance variables required by this test case.
     */
    public void tearDown()
    {
    }
    
    /**
     * Test the implementation
     */
    public void testNestableRuntimeException()
    {
        NestableRuntimeException ne1 = new NestableRuntimeException();
        assertNull("nestable runtime exception() cause is null", ne1.getCause()); 
        assertNull("nestable runtime exception() message is null", ne1.getMessage()); 

        NestableRuntimeException ne2 = new NestableRuntimeException("ne2");
        assertNull("nestable runtime exception(\"ne2\") cause is null", ne2.getCause());
        assertNotNull("nestable runtime exception(\"ne2\") message is not null", ne2.getMessage()); 
        assertTrue("nestable runtime exception(\"ne2\") message == ne2", ne2.getMessage().equals("ne2")); 

        NestableRuntimeException ne3 = new NestableRuntimeException(new Exception("ne3 exception"));
        assertNotNull("nestable runtime exception(new Exception(\"ne3 exception\") cause is not null",
            ne3.getCause()); 
        assertTrue("nestable runtime exception(new Exception(\"ne3 exception\") cause message == ne3 exception",
            ne3.getCause().getMessage().equals("ne3 exception")); 
        assertNotNull("nestable runtime exception(new Exception(\"ne3 exception\") message is not null",
            ne3.getMessage());
        assertTrue("nestable runtime exception(new Exception(\"ne3 exception\") message == cause message",
            ne3.getMessage().equals(ne3.getCause().getMessage())); 
        
        NestableRuntimeException ne4 = new NestableRuntimeException("ne4", new Exception("ne4 exception"));
        assertNotNull("nestable runtime exception(\"ne4\", new Exception(\"ne4 exception\") cause is not null", 
            ne4.getCause()); 
        assertNotNull("nestable runtime exception(\"ne4\", new Exception(\"ne4 exception\") message is not null", 
            ne4.getMessage()); 
        assertTrue("nestable runtime exception(\"ne4\", new Exception(\"ne4 exception\") message == ne4: ne4 exception", 
            ne4.getMessage().equals("ne4: ne4 exception")); 

        NestableRuntimeException ne5 = new NestableRuntimeException("ne5", null);
        assertNull("nestable runtime exception(\"ne5\", null) cause is null", 
            ne5.getCause());
        assertNotNull("nestable runtime exception(\"ne5\", new Exception(\"ne5 exception\") message is not null", 
            ne5.getMessage()); 
        assertTrue("nestable runtime exception(\"ne5\", null) message == ne5", 
            ne5.getMessage().equals("ne5")); 
        
        NestableRuntimeException ne6 = new NestableRuntimeException(null, new Exception("ne6 exception"));
        assertNotNull("nestable runtime exception(null, new Exception(\"ne6 exception\") cause is not null", 
            ne6.getCause()); 
        assertTrue("nestable runtime exception(null, new Exception(\"ne6 exception\") cause == ne6 exception", 
            ne6.getMessage().equals("ne6 exception")); 
        
        NestableRuntimeException ne7 = new NestableRuntimeException("ne7o", new NestableRuntimeException("ne7i", new Exception("ne7 exception")));
        assertTrue("nextable exception(\"ne7o\", new NestableRuntimeException(\"ne7i\", new Exception(\"ne7 exception\"))) message is ne7o: ne7i: ne7 exception",
            ne7.getMessage().equals("ne7o: ne7i: ne7 exception")); 

        NestableRuntimeException ne8 = new NestableRuntimeException("ne8", new Exception("ne8 exception"));
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
        PrintStream ps1 = new PrintStream(baos1);
        PrintWriter pw1 = new PrintWriter(ps1, true);
        ne8.printStackTrace(ps1);
        String stack1 = baos1.toString();
        assertTrue("stack trace startsWith == java.lang.Exception: ne8 exception",
            stack1.startsWith("java.lang.Exception: ne8 exception")); 
        assertTrue("stack trace indexOf org.apache.commons.lang.exception.NestableRuntimeException: ne8: ne8 exception > -1",
            stack1.indexOf("org.apache.commons.lang.exception.NestableRuntimeException: ne8: ne8 exception") > -1); 

        NestableRuntimeException ne9 = new NestableRuntimeException("ne9", new Exception("ne9 exception"));
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
        PrintStream ps2 = new PrintStream(baos2);
        PrintWriter pw2 = new PrintWriter(ps2, true);
        ne9.printPartialStackTrace(pw2);
        String stack2 = baos2.toString();
        assertTrue("stack trace startsWith == org.apache.commons.lang.exception.NestableRuntimeException: ne9: ne9 exception",
            stack2.startsWith("org.apache.commons.lang.exception.NestableRuntimeException: ne9: ne9 exception"));
        assertEquals("stack trace indexOf rethrown == -1",
            stack2.indexOf("rethrown"), -1);
    }

    public static void main(String args[])
    {
        TestRunner.run(suite());
    }
}
