cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wojciech Gdela <>
Subject Unit test: more usable AbstractCompositeTestCase (or SitemapComponentTestCase)
Date Mon, 26 Jan 2004 23:57:04 GMT

I think that it will be a good idea to allow test cases catch exceptions
throwed during excecution of actions, matches, generators, etc. Now
every exception is catched in AbstractCompositeTestCase and test fails,
even if we don't want it to fail.

Suppose that I have an action:

public class MyAction extends ServiceableAction implements ThreadSafe {
    public Map act(Redirector redirector, SourceResolver resolver,
            Map objectModel, String src, Parameters par)
            throws Exception {
        if (!par.hasParameter("foo")) {
            throw new FooExpected();

        return EMPTY_MAP;

With current implementation of AbstractCompositeTestCase I can't test if
my action behaves correctly when there is no foo parameter. Method act()
from this class catches FooException and calls fail(). Then the test
fails and I can't do anything about it.

I propose to change implementation of methods act(), match(),
generate(), serialize() and transform() in following way:

public final Map act(String type, String source, Parameters parameters)
        throws AbstractCompositeTestException {

    Action action = null;
    SourceResolver resolver = null;

    if (type == null) {
        throw new AbstractCompositeTestException("Action name is null");

    Map result = null;
    try {
        resolver = (SourceResolver) lookup(SourceResolver.ROLE);
        if (resolver == null) {
            throw new AbstractCompositeTestException("Lookup of source  
                resolver failed");

        action = (Action) lookup(Action.ROLE + "/" + type);
        if (resolver == null) {
            throw new AbstractCompositeTestException("Lookup of action  

        result = action.act(redirector, new
            objectmodel, source, parameters);

    } catch (ServiceException ce) {
        getLogger().error("Could not retrieve action", ce);
        throw new AbstractCompositeTestException("Could not retrieve
            action", ce);
    } catch (Exception e) {
        getLogger().error("Could not execute test", e);
        throw new AbstractCompositeTestException("Could not execute
            test", e);
    } finally {
        if (action != null) {
    return result;

Now i can write following test:

public class MyActionTest extends AbstractCompositeTestCase {
    public MyActionTest(String name) {

    public void testCorrectParams()
            throws AbstractCompositeTestException {
        Parameters params = new Parameters();
        params.setParameter("foo", "bla");
        Map map = super.act("my-action", null, params);
    public void testWrongParams() {
        try {
            Parameters params = new Parameters();
            Map map = super.act("my-action", null, params);
            fail("Action should throw exception");
        } catch (AbstractCompositeTestException e) {
            if (e.getCause() instanceof FooException) {
                // ok - expected behaviour
            } else {

Methods match(), generate(), serialize() and transform() should be
changed in the same way as act(). This changes would need modifications
in every currently implemented test case, it would be as easy as adding
"throws AbstractCompositeTestException" clause in method signatures.

The questions are:

0. Is it a good idea to make this changes?

1. In which versions of cocoon should it be made?

2. Should I provide some patches? (It would be easier to someone who has
cvs access)

Note: The class AbstractCompositeTestCase above is from latest CVS in
Cocoon 2.2 branch. Code in this post wasn't tested, it even hasn't been


View raw message