cocoon-dev mailing list archives

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

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  
                failed");
        }

        result = action.act(redirector, new
            SourceResolverAdapter(resolver),
            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) {
            release(action);
        }
        release(resolver);
    }
    return result;
}

Now i can write following test:

public class MyActionTest extends AbstractCompositeTestCase {
    
    public MyActionTest(String name) {
        super(name);
    }

    public void testCorrectParams()
            throws AbstractCompositeTestException {
        Parameters params = new Parameters();
        params.setParameter("foo", "bla");
        Map map = super.act("my-action", null, params);
        assertNotNull(map);
    }
    
    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 {
                e.printStackTrace();
                fail(e.getMessage());
            }
        }
    }
}

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
compiled.

-- 
Wojtek


Mime
View raw message