Re: How to assert forwards?

2005-08-15 Thread Niklas Lindholm
Hi!

Yes, separating the forwarding from the rest is definitely a solution. 
Thanks very much for the help Kazuhito and Nicolas!

/Niklas

On Mon, 15 Aug 2005, Kazuhito SUGURI wrote:

> Hi Niklas,
> 
> In article <[EMAIL PROTECTED]>,
> Sat, 13 Aug 2005 12:24:54 +0200 (CEST),
> Niklas Lindholm <[EMAIL PROTECTED]> wrote: 
> cactuslist> > response.getWebRequest().getURL();
> cactuslist> 
> cactuslist> I have done a simple test and in that test the getURL() method 
> returns 
> cactuslist> null. So I get this failure:
> cactuslist> 
> cactuslist> junit.framework.AssertionFailedError: expected: but 
> was:
> cactuslist> 
> cactuslist> I have a servlet that ends with this line:
> cactuslist> 
> cactuslist> req.getRequestDispatcher("unit.jsp").forward(req, res);
> cactuslist> 
> cactuslist> And in my endXXX() method I have this assertion:
> cactuslist> 
> cactuslist> assertEquals("unit.jsp", response.getWebRequest().getURL());
> cactuslist> 
> cactuslist> The endXXX() method is run on the client side. Will the client 
> side really 
> cactuslist> have information about what forward that has been done since this 
> typs of 
> cactuslist> forward is done entirely on the server side? I would imagine that 
> this 
> cactuslist> kind of assertion would have to go into the testXXX() method.
> 
> AFAIK, the getURL() method returns a simulated URL
> which is specified by WebRequest#setURL method.
> So, you cannot know a forwarded URL by using the method.
> 
> 
> cactuslist> > Why ?? It seems a great solution : you can check more ie that 
> there is
> cactuslist> > no error message in the page code...
> cactuslist> 
> cactuslist> Well.. in this case I don't want to verify the content of the 
> resulting 
> cactuslist> page. Only the logic of the servlet. And it could be pretty 
> difficult to 
> cactuslist> figure out from the resulting HTML which jsp page that actually 
> has been 
> cactuslist> invoked. (Unless I code some comment into it telling which jsp 
> page it 
> cactuslist> is.. hmm.. that might be a solution..)
> 
> If you are interested in the servlet logic, you might separate
> the logic into some methods to make your test easier. For your case:
> - a method returns String as a resource name to be forwarded
> - a method forward the request to the selected resource
> 
> For example, the code will be like as follows:
> public String getForwardTo() {
> // resource selection logic
> return resourceName;
> }
> ...
> req.getRequestDispatcher(getForwardTo()).forward(req, res);
> 
> You can write tests for the getForwardTo() method to verify
> the selection has been done properly.
> 
> If you want the forward is been done properly,
> you have to verify the resulting HTML in endXXX().
> To make it easier, you might insert ID string as a HTML comment
> in every JSPs.
> 
> Hope this helps,
> 
> Kazuhito SUGURI
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

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



Re: How to assert forwards?

2005-08-14 Thread Kazuhito SUGURI
Hi Niklas,

In article <[EMAIL PROTECTED]>,
Sat, 13 Aug 2005 12:24:54 +0200 (CEST),
Niklas Lindholm <[EMAIL PROTECTED]> wrote: 
cactuslist> > response.getWebRequest().getURL();
cactuslist> 
cactuslist> I have done a simple test and in that test the getURL() method 
returns 
cactuslist> null. So I get this failure:
cactuslist> 
cactuslist> junit.framework.AssertionFailedError: expected: but 
was:
cactuslist> 
cactuslist> I have a servlet that ends with this line:
cactuslist> 
cactuslist> req.getRequestDispatcher("unit.jsp").forward(req, res);
cactuslist> 
cactuslist> And in my endXXX() method I have this assertion:
cactuslist> 
cactuslist> assertEquals("unit.jsp", response.getWebRequest().getURL());
cactuslist> 
cactuslist> The endXXX() method is run on the client side. Will the client side 
really 
cactuslist> have information about what forward that has been done since this 
typs of 
cactuslist> forward is done entirely on the server side? I would imagine that 
this 
cactuslist> kind of assertion would have to go into the testXXX() method.

AFAIK, the getURL() method returns a simulated URL
which is specified by WebRequest#setURL method.
So, you cannot know a forwarded URL by using the method.


cactuslist> > Why ?? It seems a great solution : you can check more ie that 
there is
cactuslist> > no error message in the page code...
cactuslist> 
cactuslist> Well.. in this case I don't want to verify the content of the 
resulting 
cactuslist> page. Only the logic of the servlet. And it could be pretty 
difficult to 
cactuslist> figure out from the resulting HTML which jsp page that actually has 
been 
cactuslist> invoked. (Unless I code some comment into it telling which jsp page 
it 
cactuslist> is.. hmm.. that might be a solution..)

If you are interested in the servlet logic, you might separate
the logic into some methods to make your test easier. For your case:
- a method returns String as a resource name to be forwarded
- a method forward the request to the selected resource

For example, the code will be like as follows:
public String getForwardTo() {
// resource selection logic
return resourceName;
}
...
req.getRequestDispatcher(getForwardTo()).forward(req, res);

You can write tests for the getForwardTo() method to verify
the selection has been done properly.

If you want the forward is been done properly,
you have to verify the resulting HTML in endXXX().
To make it easier, you might insert ID string as a HTML comment
in every JSPs.

Hope this helps,

Kazuhito SUGURI

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



Re: How to assert forwards?

2005-08-13 Thread Niklas Lindholm


Hi Nicolas!

> response.getWebRequest().getURL();

I have done a simple test and in that test the getURL() method returns 
null. So I get this failure:

junit.framework.AssertionFailedError: expected: but was:

I have a servlet that ends with this line:

req.getRequestDispatcher("unit.jsp").forward(req, res);

And in my endXXX() method I have this assertion:

assertEquals("unit.jsp", response.getWebRequest().getURL());

The endXXX() method is run on the client side. Will the client side really 
have information about what forward that has been done since this typs of 
forward is done entirely on the server side? I would imagine that this 
kind of assertion would have to go into the testXXX() method.

> Why ?? It seems a great solution : you can check more ie that there is
> no error message in the page code...

Well.. in this case I don't want to verify the content of the resulting 
page. Only the logic of the servlet. And it could be pretty difficult to 
figure out from the resulting HTML which jsp page that actually has been 
invoked. (Unless I code some comment into it telling which jsp page it 
is.. hmm.. that might be a solution..)

/Niklas

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



Re: How to assert forwards?

2005-08-12 Thread Nicolas Chalumeau
Salut Niklas,

2005/8/11, Niklas Lindholm <[EMAIL PROTECTED]>:
> Hi!
> 
> I am writing a web application that is using an MVC design. This means
> that at the end of almost every servlet I do a
> 
> request.getRequestDispatcher("foo.jsp").forward(request, response)
> 
> 
> I some cases the servlet makes a choice between two (or more) different
> targets to forward() to. Something like:
> 
> if(somethingIsTrue)
>   request.getRequestDispatcher("foo.jsp").forward(request, response);
> else
>   request.getRequestDispatcher("bar.jsp").forward(request, response);
> 
> Is there a way to make a Cactus test case that asserts that the forward is
> being done to the correct target? I can't find any way to get information
> about what forward that has been done. 

response.getWebRequest().getURL();

> (Except trying to find out by
> looking at the resulting HTML is end(), but that doesn't seem like a
> good idea..)

Why ?? It seems a great solution : you can check more ie that there is
no error message in the page code...

Nicolas,

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

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