Hosein,

I enclose a code sample.

As you see, I favour convenience over orthodoxy. The crucial point testing
Struts is the instantiation of the action with all its "armour", specially
in actions with validation (Struts is not an example of inversion of
control).

You can find different solutions to that problem: at least a couple of
specific frameworks freely available or your own ad-hoc solution. I opt for
the simplest one: I instantiate an action in my container letting Struts
take charge of all the complexities.

This solution does not forbid you to test quite a lot of things: the
validation of the parameteres of the form, the integration of your action
with your model classes, the messages you get with the different parameters
set and the internals of your action.

Two ingredients are also necessary: the logic of your action must be simple,
so that you can control the invisible corners with java 1.4 assertions
inside the code of your action; the testing of your model classes must be
done separately, so the only thing you test here is the integration of the
different parts.
Regards.


public class SubscriptAlDiaActionTest extends ServletTestCase {

    // Dispatcher.
    private static RequestDispatcher dispatcher;

    // Usuario.
    protected Usuario usuario;

    // WebSites.
    protected WebSite web1;

    // Servicio.
    protected Servicio servicio1;


    // ...................... M�TODOS DE INICIALIZACI�N
.................................................

    public void begin(WebRequest request) {

        request.addParameter("password", "hola", WebRequest.POST_METHOD);
        request.addParameter("password2", "hola2", WebRequest.POST_METHOD);
        request.addParameter("recuerdoPassword", "saludo",
WebRequest.POST_METHOD);
        request.addParameter("nombre1", "name", WebRequest.POST_METHOD);
        request.addParameter("nombre2", "name2", WebRequest.POST_METHOD);
        request.addParameter("nombre3", "name3", WebRequest.POST_METHOD);
        request.addParameter("periodo", "99", WebRequest.POST_METHOD);

        int[] idWebSites = new int[]{1, 2};
        request.addParameter("idWebSites", String.valueOf(idWebSites[0]),
WebRequest.POST_METHOD);
        request.addParameter("idWebSites", String.valueOf(idWebSites[1]),
WebRequest.POST_METHOD);

        request.setAutomaticSession(false);
    }

    protected void setUp() throws IOException, ServletException {

        // Creaci�n de usuario.
        String usuarioStr = TestConstants.USUARIO_NAME_1;
        String password = "password";
        Set subscripciones = new HashSet();
        usuario = new Usuario(usuarioStr, password);
        usuario.setSubscripciones(subscripciones);
        usuario.setEmail(new Email(new
StringBuffer().append(TestConstants.USUARIO_NAME_1).append("@javato.com").to
String()));
        assert usuario.getIdUsuario() == 0 : "idUsuario = 0;";

        // Creaci�n de webSites.
        web1 = new WebSite();
        web1.setIdWebSite(1);
        // Creaci�n de servicios
        servicio1 = new Servicio(TestConstants.ID_SERVICIO_1);

        // RequestDispatcher.
        dispatcher =
request.getRequestDispatcher(TestConstants.SUBSCRPIPTFORM_ALDIA_SUBMIT);
        assert dispatcher != null : "dispatcher should not be null";
    }

    protected void tearDown() throws HibernateException,
IllegalAccessException, InstantiationException,
            ClassNotFoundException, DaoException {
        TestUtils.deleteUsuarioTest(TestConstants.USUARIO_NAME_1);
    }

    // ............................... TEST CHECK USUARIO Y EMAIL - one
......................................

    public void beginCheckUsuarioYmail_1(WebRequest request) {
        request.addParameter("usuario", TestConstants.USUARIO_NAME_1,
WebRequest.POST_METHOD);
        request.addParameter("eMailAddress", "[EMAIL PROTECTED]",
WebRequest.POST_METHOD);
    }

    public void testCheckUsuarioYmail_1() throws IllegalAccessException,
ClassNotFoundException, InstantiationException,
            DaoException, IOException, ServletException {

        // Nueva subscripci�n, con mismo usuario y email que figura en el
par�metro del request.
        Subscripcion subscripcionNew = TestUtils.doSubscripcion(usuario, new
WebSite[]{web1}, servicio1, null);
        usuario.addNewSubscripcion(subscripcionNew);

        SubscripcionDaoIf subscripcionDao =
SubscripcionDaoFactory.SUBSCRIPCION_FACTORY_INST.getInstance();
        boolean isSubscribed = subscripcionDao.subscribe(usuario);
        assertTrue("isSubscribed is true", isSubscribed);

        dispatcher.forward(request, response);
        boolean isMessage = TestUtils.checkMessage(request,
Constants.USUARIO_DUPLICADO_MSG, null);
        assertTrue("Usuario duplicado", isMessage);
    }

    public void endCheckUsuarioYmail_1(WebResponse response) {
        String jspText = response.getText();
        assertTrue("Reenv�o a alDia_subscript.jsp",
jspText.indexOf(TestConstants.ALDIA_SUBCRIPT_TEXT) >= 0);
    }



-----Mensaje original-----
De: hossein rahmani [mailto:[EMAIL PROTECTED]
Enviado el: jueves, 09 de diciembre de 2004 8:58
Para: Cactus Users List
Asunto: RE: cactus+hibernate+ejb


Pedro,
 Ok,plz mail me those samples if possible.
bye


Pedro Nevado <[EMAIL PROTECTED]> wrote:
Hossein,

I can not mail you my project. However, I can send you a couple of samples
of tests (a Struts action, a Hibernate dao object), if you want. I insist
that my architecture does not contemplate JBoss as the application server,
just plain Tomcat.

Regards.
-----Mensaje original-----
De: hossein rahmani [mailto:[EMAIL PROTECTED]
Enviado el: jueves, 09 de diciembre de 2004 6:39
Para: [EMAIL PROTECTED]
Asunto: cactus+hibernate+ejb


Dear pedro
Hi,I will be so happy if you be more specific ,
and if possible please mail me your project,and if you wanna i can mail
you the whole project.
Thanks Inadvane.
Regards.
Bye





----------------------------------------------------------------------------
--
Find local movie times and trailers on Yahoo! Movies.



---------------------------------
Find local movie times and trailers on Yahoo! Movies.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to