Menu

#70 customize exception rethrowing policy in ComponentTestFixtur

closed
Framework (21)
5
2005-12-03
2005-11-15
No

Environment: Abbot-1.0.0RC1, JUnit-3.8.1

We encountered the need to customize exception
rethrowing policy in ComponentTestFixture. For
achieving this, we patched the ComponentTestFixture as
follows, with a new ruleOut method.
Feature Request 1357210 ("tearDown in ResolverFeature
may mask previous exception") needs to be implemented
to make this work in all cases.

Here is how we modified Abbot code (while keeping
current rethrowing policy unchanged):

/**

* Override this method to choose the
<code>Throwable</code> which should
* appear as test failure cause in the test report.

* This method is useful when two exception occur
while a test is ran,
* one in EDT (Event Dispatch Thread) and one in
the main thread (the
* one running the code of the test case).

* This method may be called with both
<code>exception</code> and
* <code>edtException</code> set to null. When it
does return null,
* the exception(s) is (are) swallowed.

*

* @param exception the exception which occured in
test case thread.
* May be null.

* @param exceptionTime the time at which
<code>exception</code>
* was caught by the fixture. Meaningless if
<code>exception</code>
* is null.

* @param edtException the exception which occured
in Event Dispatch Thread.
* May be null.

* @param edtExceptionTime the time at which
<code>edtException</code>
* was caught by the fixture. Meaningless if
<code>edtException</code>
* is null.

* @return a possibly null <code>Throwable</code>.

*/

protected Throwable ruleOut(
Throwable exception,
long exceptionTime,
Throwable edtException,
long edtExceptionTime
) {
if (edtException != null
&& (exception == null
|| edtExceptionTime < exceptionTime)) {
// TODO wrap this somehow with an
additional message that
// says "this was thrown on the EDT"
return edtException;
}
return exception;
}

public void runBare() throws Throwable {
Throwable exception = null;
long exceptionTime = -1;
try {
super.runBare();
}
catch(Throwable e) {
exceptionTime = System.currentTimeMillis();
exception = e;
}
finally {
// Cf. StepRunner.runStep()
// Any EDT exception which occurred *prior*
to when the
// exception on the main thread was thrown
should be used
// instead.
exception = ruleOut( exception,
exceptionTime, edtException, edtExceptionTime );
}
if (exception != null) {
throw exception;
}
}

We found useful to use this strategy as default strategy:

protected Throwable ruleOut(
Throwable exception,
long exceptionTime,
Throwable edtException,
long edtExceptionTime
) {
if ( null != edtException ) {
if ( null == exception ) {
exception = edtException;
} else if ( null == exception.getCause() ) {
exception.initCause( edtException );
} else {
if ( edtExceptionTime < exceptionTime ) {
exception.printStackTrace();
exception = edtException;
} else {
edtException.printStackTrace();
}
}
}
return exception;
}

Discussion

  • Laurent Caillette

    • summary: feature request: tearDown in ResolverFeature may mask previo --> customize exception rethrowing policy in ComponentTestFixtur
     
  • Timothy Wall

    Timothy Wall - 2005-11-16
    • status: open --> pending
     
  • Timothy Wall

    Timothy Wall - 2005-11-16

    Logged In: YES
    user_id=54098

    It's not clear to me what you're trying to accomplish with the "ruleOut" method.

    It would be inappropriate to arbitrarily set the EDT exception as the "cause" of a
    different exception. It also introduces a 1.5JRE dependency.

     
  • Laurent Caillette

    • status: pending --> open
     
  • Laurent Caillette

    Logged In: YES
    user_id=386643

    > It's not clear to me what you're trying to accomplish
    > with the "ruleOut" method.

    Abbot strategy is to keep first exception (regarding
    throwing time). While testing some kind of worker (code
    interacting with EDT but running in another thread than EDT
    and main thread), the exception in the worker was the real
    cause of the crash but it was trapped after EDT exploded. So
    we found useful to customize which exception should appear
    in the JUnit report in the code of the test case.

     
  • Timothy Wall

    Timothy Wall - 2005-11-18

    Logged In: YES
    user_id=54098

    If you want to override the EDT exception, use
    abbot.util.EDTExceptionCatcher.clear() when your worker thread exception is
    caught/thrown (or not, if you prefer to see the EDT exception).

     
  • Timothy Wall

    Timothy Wall - 2005-11-18
    • status: open --> pending
     
  • SourceForge Robot

    • status: pending --> closed
     
  • SourceForge Robot

    Logged In: YES
    user_id=1312539

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).