All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.exc.test.SysExceptionTest Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*
 * /*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 * @(#) $Id:  $
 *******************************************************************************/
package at.spardat.enterprise.exc.test;

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

import at.spardat.enterprise.exc.BaseException;
import at.spardat.enterprise.exc.INotification;
import at.spardat.enterprise.exc.SysException;
import junit.framework.Assert;
import junit.framework.TestCase;

public class SysExceptionTest extends TestCase {

    public void testSysExceptionStringObjectArray() {
        SysException exc = new SysException("test with parameters: p1={0} p2={1}",new Object[]{"one","two"});
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two"));
        Assert.assertNull(exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
    }

    public void testSysExceptionStringObjectObject() {
        SysException exc = new SysException("test with parameters: p1={0} p2={1}","one","two");
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two"));
        Assert.assertNull(exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
    }

    public void testSysExceptionStringObject() {
        SysException exc = new SysException("test with parameters: p1={0}","one");
        Assert.assertEquals("test with parameters: p1=one", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one"));
        Assert.assertNull(exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
    }

    public void testSysExceptionString() {
        Exception rt = new RuntimeException("test without parameters");
        SysException exc = new SysException("test without parameters");
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test without parameters", message);
        message = exc.getMessage();
        String expected = rt.getMessage();
        Assert.assertEquals(expected,message);
        message = exc.getLocalizedMessage();
        expected = rt.getLocalizedMessage();
        Assert.assertEquals(expected,message);

        String trace = stackTraceToString(exc);
        String rttrace = stackTraceToString(rt);
        rttrace = rttrace.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        rttrace = fixLineNumber(rttrace, trace);
        Assert.assertEquals(rttrace, trace);

        Assert.assertNull(exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());

    }

    public void testSysExceptionThrowableString() {
        Exception detail = new Exception("testdetail");
        Exception rt = new RuntimeException("outher message",detail);
        SysException exc = new SysException(detail,"outher message");
        String message = exc.toString();
        String expected = rt.toString();
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        Assert.assertEquals(expected, message);
        message = exc.getMessage();
        expected = rt.getMessage();
        Assert.assertEquals(expected,message);
        message = exc.getLocalizedMessage();
        expected = rt.getLocalizedMessage();
        Assert.assertEquals(expected,message);

        String trace = stackTraceToString(exc);
        String rttrace = stackTraceToString(rt);
        rttrace = rttrace.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        rttrace = fixLineNumber(rttrace, trace);
        Assert.assertEquals(rttrace, trace);

        Assert.assertEquals(detail,exc.getDetail());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(),exc.getCause());
}


    // message handled differently:
    // java uses detail.toString()
    // sysException uses ""
//    public void testSysExceptionThrowable() {
//        Exception detail = new Exception("testdetail");
//        Exception rt = new RuntimeException(detail);
//        Exception exc = new SysException(detail);
//        String message = exc.toString();
//        String expected = rt.toString();
//        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
//        Assert.assertEquals(expected, message);
//        message = exc.getMessage();
//        expected = rt.getMessage();
//        Assert.assertEquals(expected,message);
//        message = exc.getLocalizedMessage();
//        expected = rt.getLocalizedMessage();
//        Assert.assertEquals(expected,message);
//
//        String trace = stackTraceToString(exc);
//        String rttrace = stackTraceToString(rt);
//        rttrace = rttrace.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
//        rttrace = fixLineNumber(rttrace, trace);
//        Assert.assertEquals(rttrace, trace);
//    }
    public void testSysExceptionThrowable() {
        Exception detail = new Exception("testdetail");
        SysException exc = new SysException(detail);
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: ", message);
        message = exc.getMessage();
        Assert.assertEquals("",message);
        message = exc.getLocalizedMessage();
        Assert.assertEquals("",message);

        Assert.assertEquals(detail,exc.getDetail());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
}

    public void testSysExceptionThrowableStringObjectObject() {
        Exception detail = new Exception("testdetail");
        Exception rt = new RuntimeException("test with parameters: p1=one p2=two",detail);
        SysException exc = new SysException(detail,"test with parameters: p1={0} p2={1}","one","two");
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two"));

        String message = exc.toString();
        String expected = rt.toString();
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        Assert.assertEquals(expected, message);
        message = exc.getMessage();
        expected = rt.getMessage();
        Assert.assertEquals(expected,message);
        message = exc.getLocalizedMessage();
        expected = rt.getLocalizedMessage();
        Assert.assertEquals(expected,message);

        trace = stackTraceToString(exc);
        String rttrace = stackTraceToString(rt);
        rttrace = rttrace.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        rttrace = fixLineNumber(rttrace, trace);
        Assert.assertEquals(rttrace, trace);

        Assert.assertEquals(detail,exc.getDetail());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(),exc.getCause());

    }

    public void testSysExceptionThrowableStringObject() {
        Exception detail = new Exception("testdetail");
        Exception rt = new RuntimeException("test with parameters: p1=one",detail);
        SysException exc = new SysException(detail,"test with parameters: p1={0}","one");
        Assert.assertEquals("test with parameters: p1=one", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one"));

        String message = exc.toString();
        String expected = rt.toString();
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        Assert.assertEquals(expected, message);
        message = exc.getMessage();
        expected = rt.getMessage();
        Assert.assertEquals(expected,message);
        message = exc.getLocalizedMessage();
        expected = rt.getLocalizedMessage();
        Assert.assertEquals(expected,message);

        trace = stackTraceToString(exc);
        String rttrace = stackTraceToString(rt);
        rttrace = rttrace.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        rttrace = fixLineNumber(rttrace, trace);
        Assert.assertEquals(rttrace, trace);

        Assert.assertEquals(detail,exc.getDetail());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(),exc.getCause());
    }

    public void testSysExceptionThrowableStringObjectArray() {
        Exception detail = new Exception("testdetail");
        Exception rt = new RuntimeException("test with parameters: p1=one p2=two",detail);
        SysException exc = new SysException(detail,"test with parameters: p1={0} p2={1}",new Object[]{"one","two"});
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two"));

        String message = exc.toString();
        String expected = rt.toString();
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        Assert.assertEquals(expected, message);
        message = exc.getMessage();
        expected = rt.getMessage();
        Assert.assertEquals(expected,message);
        message = exc.getLocalizedMessage();
        expected = rt.getLocalizedMessage();
        Assert.assertEquals(expected,message);

        trace = stackTraceToString(exc);
        String rttrace = stackTraceToString(rt);
        rttrace = rttrace.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        rttrace = fixLineNumber(rttrace, trace);
        Assert.assertEquals(rttrace, trace);

        Assert.assertEquals(detail,exc.getDetail());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(),exc.getCause());
    }

    // shown in toString, not shown in getMessage
    public void testSetCode() {
        SysException exc = new SysException("bla bla");
        exc.setCode(4711);
        Assert.assertEquals(4711,exc.getCode());
        Assert.assertTrue(exc.containsCode(4711));
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException [4711]: bla bla", message);
        message = exc.getMessage();
        Assert.assertEquals("bla bla",message);
        message = exc.getLocalizedMessage();
        Assert.assertEquals("bla bla",message);
    }

    // not shown in toString and getMessage
    public void testSetReaction() {
        SysException exc = new SysException("bla bla");
        exc.setReaction(INotification.R_YES_NO_CANCEL);
        Assert.assertEquals(INotification.R_YES_NO_CANCEL,exc.getReaction());
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: bla bla", message);
        message = exc.getMessage();
        Assert.assertEquals("bla bla",message);
        message = exc.getLocalizedMessage();
        Assert.assertEquals("bla bla",message);
    }

    // not shown in toString and getMessage
    public void testSetShortMessage() {
        SysException exc = new SysException("bla bla");
        exc.setReaction(INotification.R_YES_NO_CANCEL);
        Assert.assertEquals(INotification.R_YES_NO_CANCEL,exc.getReaction());
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: bla bla", message);
        message = exc.getMessage();
        Assert.assertEquals("bla bla",message);
        message = exc.getLocalizedMessage();
        Assert.assertEquals("bla bla",message);
    }

    // not shown in toString and getMessage
    public void testSetType() {
        SysException exc = new SysException("bla bla");
        exc.setType(INotification.T_ERROR);
        Assert.assertEquals(INotification.T_ERROR,exc.getType());
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: bla bla", message);
        message = exc.getMessage();
        Assert.assertEquals("bla bla",message);
        message = exc.getLocalizedMessage();
        Assert.assertEquals("bla bla",message);
    }

    // not shown in toString and getMessage
    public void testSetShowToEndUser() {
        SysException exc = new SysException("bla bla");
        Assert.assertEquals(false,exc.showToEndUser());
        exc.setShowToEndUser(true);
        Assert.assertEquals(true,exc.showToEndUser());
        String message = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: bla bla", message);
        message = exc.getMessage();
        Assert.assertEquals("bla bla",message);
        message = exc.getLocalizedMessage();
        Assert.assertEquals("bla bla",message);
        exc.setShowToEndUser(false);
        Assert.assertEquals(false,exc.showToEndUser());
    }


    public void testGetCode() {
        SysException detail = new SysException("testdetail");
        SysException exc = new SysException("bla bla");
        Assert.assertEquals(0,exc.getCode());
        exc.setCode(1);
        Assert.assertEquals(1,exc.getCode());
        exc.setCode(Integer.MAX_VALUE);
        Assert.assertEquals(Integer.MAX_VALUE,exc.getCode());

        SysException nest = new SysException(detail,"outhermessage");
        nest.setCode(13);
        detail.setCode(24);
        Assert.assertEquals(24,detail.getCode());
        Assert.assertEquals(13,nest.getCode());
    }

    public void testContainsCode() {
        SysException detail = new SysException("testdetail");
        SysException exc = new SysException("bla bla");
        Assert.assertTrue(exc.containsCode(0));
        exc.setCode(5);
        Assert.assertTrue(exc.containsCode(5));
        exc.setCode(Integer.MAX_VALUE);
        Assert.assertTrue(exc.containsCode(Integer.MAX_VALUE));

        SysException nest = new SysException(detail,"outhermessage");
        nest.setCode(13);
        detail.setCode(24);
        Assert.assertTrue(nest.containsCode(13));
        Assert.assertTrue(nest.containsCode(24));
        Assert.assertTrue(detail.containsCode(24));
        Assert.assertFalse(detail.containsCode(13));
    }

    public void testGetType() {
        SysException detail = new SysException("testdetail");
        SysException exc = new SysException("bla bla");
        Assert.assertEquals(0,exc.getCode());
        exc.setType(1);
        Assert.assertEquals(1,exc.getType());
        exc.setType(Integer.MAX_VALUE);
        Assert.assertEquals(Integer.MAX_VALUE,exc.getType());

        SysException nest = new SysException(detail,"outhermessage");
        nest.setType(13);
        detail.setType(24);
        Assert.assertEquals(24,detail.getType());
        Assert.assertEquals(13,nest.getType());
    }

    public void testGetReaction() {
        SysException detail = new SysException("testdetail");
        SysException exc = new SysException("bla bla");
        Assert.assertEquals(0,exc.getCode());
        exc.setReaction(1);
        Assert.assertEquals(1,exc.getReaction());
        exc.setReaction(Integer.MAX_VALUE);
        Assert.assertEquals(Integer.MAX_VALUE,exc.getReaction());

        SysException nest = new SysException(detail,"outhermessage");
        nest.setReaction(13);
        detail.setReaction(24);
        Assert.assertEquals(24,detail.getReaction());
        Assert.assertEquals(13,nest.getReaction());
    }

    public void testGetShortMessage() {
        SysException detail = new SysException("testdetail");
        SysException exc = new SysException("bla bla");
        Assert.assertEquals("",exc.getShortMessage());
        exc.setShortMessage("one");
        Assert.assertEquals("one",exc.getShortMessage());
        exc.setShortMessage(""+Integer.MAX_VALUE);
        Assert.assertEquals(""+Integer.MAX_VALUE,exc.getShortMessage());

        SysException nest = new SysException(detail,"outhermessage");
        nest.setShortMessage("13");
        detail.setShortMessage("24");
        Assert.assertEquals("24",detail.getShortMessage());
        Assert.assertEquals("13",nest.getShortMessage());
    }


    public void testGetFirstNonEmptyMessage() {
        Exception detail = new SysException("testdetail");
        SysException exc = new SysException("bla bla");
        assertEquals("bla bla", exc.getFirstNonEmptyMessage(false));
        assertEquals("at.spardat.enterprise.exc.SysException: bla bla", exc.getFirstNonEmptyMessage(true));
        SysException nest = new SysException(detail);
        assertEquals("testdetail", nest.getFirstNonEmptyMessage(false));
        assertEquals("at.spardat.enterprise.exc.SysException: testdetail", nest.getFirstNonEmptyMessage(true));
        detail = new Exception("testdetail");
        nest = new SysException(detail);
        assertEquals("testdetail", nest.getFirstNonEmptyMessage(false));
        assertEquals("java.lang.Exception: testdetail", nest.getFirstNonEmptyMessage(true));
        detail = new Exception();
        nest = new SysException(detail);
        assertEquals("", nest.getFirstNonEmptyMessage(false));
        assertEquals("java.lang.Exception", nest.getFirstNonEmptyMessage(true));

        detail = new Exception("testdetail");
        exc = new SysException(detail,"bla bla");
        nest = new SysException(exc);
        assertEquals("bla bla", nest.getFirstNonEmptyMessage(false));
        assertEquals("at.spardat.enterprise.exc.SysException: bla bla", nest.getFirstNonEmptyMessage(true));

        detail = new Exception("testdetail");
        exc = new SysException(detail);
        nest = new SysException(exc);
        assertEquals("testdetail", nest.getFirstNonEmptyMessage(false));
        assertEquals("java.lang.Exception: testdetail", nest.getFirstNonEmptyMessage(true));

        // java.lang exceptions allways return something for getMessage()!
        detail = new Exception("testdetail");
        Exception exc2 = new RuntimeException(detail);
        nest = new SysException(exc2);
        assertEquals("java.lang.Exception: testdetail", nest.getFirstNonEmptyMessage(false));
        assertEquals("java.lang.RuntimeException: java.lang.Exception: testdetail", nest.getFirstNonEmptyMessage(true));
    }

    public void testGetMessageBoolean() {
        Exception rt = new RuntimeException("message");
        SysException exc = new SysException("message");
        String message = exc.getMessage(false);
        String expected = rt.getMessage();
        Assert.assertEquals(expected, message);
        Assert.assertEquals("message", message);
        message = exc.getMessage(true);
        expected = rt.toString();
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        Assert.assertEquals(expected, message);
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: message", message);
    }

    public void testGetDetail() {
        SysException detail = new SysException("testdetail");
        SysException exc = new SysException(detail,"bla bla");
        assertNull(detail.getDetail());
        assertEquals(detail, exc.getDetail());
        assertEquals(exc.getDetail(),exc.getCause());

        exc = new SysException("later");
        exc.initCause(detail);
        assertEquals(exc.getDetail(),exc.getCause());
    }

    public void testPrepareMigration() {
        SysException exc = new SysException("bla bla");
        assertNull(exc.getDetail()); // no detail before
        exc.prepareMigration(); // does nothing
        assertNull(exc.getDetail()); // no detail after
        assertNull(exc.getCause());

        Exception detail = new SysException("testdetail");
        exc = new SysException(detail,"bla bla");
        assertEquals(detail, exc.getDetail());
        exc.prepareMigration(); // keep sysexception
        assertEquals(detail, exc.getDetail());
        assertEquals(detail, exc.getCause());

        detail = new Exception("testdetail");
        exc = new SysException(detail,"bla bla");
        String before = stackTraceToString(exc);
        assertEquals(detail, exc.getDetail());
        exc.prepareMigration(); // strip exception
        assertNull(exc.getDetail()); // no detail after
        assertNull(exc.getCause());
        String after = stackTraceToString(exc);
        assertEquals(before,after); // detail still in printstacktrace

        // deeper nested
        detail = new Exception("testdetail");
        Exception nest = new Exception("nest",detail);
        exc = new SysException(nest,"bla bla");
        before = stackTraceToString(exc);
        assertEquals(nest, exc.getDetail());
        assertEquals(detail, nest.getCause());
        exc.prepareMigration(); // strip exception
        assertNull(exc.getDetail()); // no detail after
        assertNull(exc.getCause());
        after = stackTraceToString(exc);
        assertEquals(before,after); // detail still in printstacktrace
    }

    public void testToString() {
        SysException exc = new SysException("message");
        String result = exc.toString();
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: message", result);
        String expected = exc.getClass().getName() + ": " + exc.getMessage();
        Assert.assertEquals(expected, result);

        exc.setCode(15);
        Assert.assertEquals("at.spardat.enterprise.exc.SysException [15]: message", exc.toString());

    }

    public void testGetOwnStackTrace() {
        Exception rt = new RuntimeException("message");
        SysException exc = new SysException("message");
        String trace = exc.getOwnStackTrace();
        String expected = stackTraceToString(rt);
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        expected = fixLineNumber(expected, trace);
        Assert.assertEquals(expected, trace);

        Exception detail = new Exception("detail");
        rt = new RuntimeException("message"); // without detail
        exc = new SysException(detail,"message");
        trace = exc.getOwnStackTrace(); // contains no detail
        expected = stackTraceToString(rt);
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        expected = fixLineNumber(expected, trace);
        Assert.assertEquals(expected, trace);
    }

    // in baseexceptio the stacktrace is created in the constructor
    // getStackOfThis fixes the header lines if the message changes later on
    public void testGetStackOfThis() {
        SysException exc = new SysException("message");
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: message"));
        exc.setMessage("changed");
        trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: changed"));

        SysException detail = new SysException("detail");
        exc = new SysException(detail,"message");
        trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: message"));
        exc.setMessage("changed");
        trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: changed"));
        int start = trace.indexOf("Caused by");
        Assert.assertTrue(trace.substring(start).startsWith("Caused by: at.spardat.enterprise.exc.SysException: detail"));
        detail.setMessage("something completly different");
        trace = stackTraceToString(exc);
        start = trace.indexOf("Caused by");
        Assert.assertTrue(trace.substring(start).startsWith("Caused by: at.spardat.enterprise.exc.SysException: something completly different"));
    }

    public void testTruncateSubclasses() {
        SysException exc = new SysException("message");
        BaseException my = new MySysException("message");
        Assert.assertEquals(MySysException.class, my.getClass());
        my = my.truncateSubclasses();
        Assert.assertEquals(SysException.class, my.getClass());

        String trace = stackTraceToString(my);
        String expected = stackTraceToString(exc);
        expected = fixLineNumber(expected, trace);
        Assert.assertEquals(expected, trace);

        // both deails must be in same line for the stacktrace to be identical
        Exception detail = new SysException("detail");   Exception myDetail = new MySysException("detail");
        exc = new SysException(detail,"message");
        my = new MySysException(myDetail,"message");
        Assert.assertEquals(MySysException.class, my.getClass());
        Assert.assertEquals(MySysException.class, my.getDetail().getClass());
        my = my.truncateSubclasses();
        Assert.assertEquals(SysException.class, my.getClass());
        Assert.assertEquals(SysException.class, my.getDetail().getClass());

        trace = stackTraceToString(my);
        expected = stackTraceToString(exc);
        expected = fixLineNumber(expected, trace);
        Assert.assertEquals(expected, trace);
    }

    public void testSetMessageStringObjectObject() {
        Exception detail = new Exception("detail");
        SysException exc = new SysException(detail);
        exc.setMessage("test with parameters: p1={0} p2={1}","one","two");
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one p2=two", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one p2=two"));
    }

    public void testSetMessageStringObject() {
        Exception detail = new Exception("detail");
        SysException exc = new SysException(detail);
        exc.setMessage("test with parameters: p1={0}","one");
        Assert.assertEquals("test with parameters: p1=one", exc.getMessage());
        Assert.assertEquals("test with parameters: p1=one", exc.getLocalizedMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: test with parameters: p1=one", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: test with parameters: p1=one"));
    }

    public void testSetMessageString() {
        SysException exc = new SysException("message");
        Assert.assertEquals("message", exc.getMessage());
        exc.setMessage("bla bla");
        Assert.assertEquals("bla bla", exc.getMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: bla bla", exc.toString());
        String trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: bla bla"));

        Exception detail = new Exception("detail");
        exc = new SysException(detail);
        Assert.assertEquals("", exc.getMessage());
        exc.setMessage("bla bla");
        Assert.assertEquals("bla bla", exc.getMessage());
        Assert.assertEquals("at.spardat.enterprise.exc.SysException: bla bla", exc.toString());
        trace = stackTraceToString(exc);
        Assert.assertTrue(trace.startsWith("at.spardat.enterprise.exc.SysException: bla bla"));

    }

    public void testGetStackTrace() {
        Exception rt = new RuntimeException("message");
        SysException exc = new SysException("message");

        StackTraceElement[] trace = exc.getStackTrace();
        StackTraceElement[] expected = rt.getStackTrace();
        Assert.assertTrue(equalsStackTrace(expected, trace));

        Exception detail = new Exception("detail");
        rt = new RuntimeException("message",detail);
        exc = new SysException(detail,"message");

        trace = exc.getStackTrace();
        expected = rt.getStackTrace();
        Assert.assertTrue(equalsStackTrace(expected, trace));

        // works only with new implementation
        trace = exc.getCause().getStackTrace();
        expected = rt.getCause().getStackTrace();
        Assert.assertTrue(equalsStackTrace(expected, trace));
        // works with both implementations
        trace = exc.getDetail().getStackTrace();
        expected = rt.getCause().getStackTrace();
        Assert.assertTrue(equalsStackTrace(expected, trace));

    }

    // setStackTrace not supported in SysException and AppException
//    public void testSetStackTrace() {
//        Exception rt = new RuntimeException("message");
//        Exception exc = new SysException("message");
//        String oldTrace = stackTraceToString(exc);
//        String expected = stackTraceToString(rt);
//        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
//        Assert.assertFalse(expected.equals(oldTrace)); // different line numbers
//        exc.setStackTrace(rt.getStackTrace());
//        String newTrace = stackTraceToString(exc);
//        // new behaviour
//        Assert.assertEquals(expected, newTrace); // now same line numbers
//    }

//    public void testFillInStackTrace() {
//        fail("Not yet implemented");
//    }

    public void testGetCause() {
        Exception rt = new RuntimeException("message");
        SysException exc = new SysException("message");
        Assert.assertNull(exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(), exc.getCause());

        Exception detail = new Exception("detail");
        rt = new RuntimeException(detail);
        exc = new SysException(detail);
        Assert.assertEquals(detail, exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(), exc.getCause());


        rt = new RuntimeException("message");
        rt.initCause(detail);
        exc = new SysException("message");
        exc.initCause(detail);
        Assert.assertEquals(detail, exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(), exc.getCause());
        String trace = stackTraceToString(exc);
        String expected = stackTraceToString(rt);
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        expected = fixLineNumber(expected, trace);
        Assert.assertEquals(expected, trace);
    }

    public void testInitCause() {
        Exception detail = new Exception("detail");
        Exception rt = new RuntimeException("message");
        SysException exc = new SysException("message");
        Assert.assertNull(exc.getCause());
        exc.initCause(detail);
        rt.initCause(detail);
        Assert.assertEquals(detail, exc.getCause());
        Assert.assertEquals(exc.getDetail(), exc.getCause());
        Assert.assertEquals(rt.getCause(), exc.getCause());
        String trace = stackTraceToString(exc);
        String expected = stackTraceToString(rt);
        expected = expected.replaceAll("java\\.lang\\.RuntimeException", "at.spardat.enterprise.exc.SysException");
        expected = fixLineNumber(expected, trace);
        Assert.assertEquals(expected, trace);

        Exception newdetail = new RuntimeException("newdetail");
        try {
            exc.initCause(newdetail);
            Assert.assertTrue("missing exception", false);
        } catch(Exception eise) {
            Assert.assertTrue(eise instanceof IllegalStateException);
            Assert.assertEquals("java.lang.IllegalStateException: Can't overwrite cause", eise.toString());
        }

        exc = new SysException(detail,"message");
        try {
            exc.initCause(newdetail);
            Assert.assertTrue("missing exception", false);
        } catch(Exception eise) {
            Assert.assertTrue(eise instanceof IllegalStateException);
            Assert.assertEquals("java.lang.IllegalStateException: Can't overwrite cause", eise.toString());
        }

        exc = new SysException("message");
        try {
            exc.initCause(exc);
            Assert.assertTrue("missing exception", false);
        } catch(Exception eise) {
            Assert.assertTrue(eise instanceof IllegalArgumentException);
            Assert.assertEquals("java.lang.IllegalArgumentException: Self-causation not permitted", eise.toString());
        }
    }

    /**
     * Converts the stacktrace of exc to a string using both
     * printStackTrace(PrintStream) and printStackTrace(PrintWriter).
     * It asserts, that both method give the same result.
     * @returns the result
     */
    private String stackTraceToString(Throwable exc) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(bout);
        exc.printStackTrace(out);
        out.close();
        String first = bout.toString();

        CharArrayWriter cout = new CharArrayWriter();
        PrintWriter pout = new PrintWriter(cout);
        exc.printStackTrace(pout);
        pout.close();
        String second = cout.toString();

        Assert.assertEquals(first, second);
        return first;
    }

    private String fixLineNumber(String toChange,String template) {
        String filename = this.getClass().getName();
        filename = filename.substring(filename.lastIndexOf('.')+1)+".java";

        // find line number in template
        int start = template.indexOf(filename)+filename.length();
        int end = template.indexOf(')', start);
        String rightNumber = template.substring(start, end);

        // find lin number in toChange
        start = toChange.indexOf(filename)+filename.length();
        end = toChange.indexOf(')', start);

        // convert
        StringBuffer out = new StringBuffer(toChange.length()+10);
        out.append(toChange.substring(0, start));
        out.append(rightNumber);
        out.append(toChange.substring(end));

        return out.toString();
    }

    boolean equalsStackTrace(StackTraceElement[] expected,StackTraceElement[] actual) {
        assertEquals(expected.length, actual.length);
        // className and lineNumber will differ in first entry
        assertEquals(expected[0].getFileName(), actual[0].getFileName());
        assertEquals(expected[0].getMethodName(), actual[0].getMethodName());
        for(int i=1;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy