Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread musomesa

[The standard?diclaimer that it is not a popularity contest and nothing that 
does the job for you is wrong.]
Would you not agree that if you are going to mock up the framework the 
simulation of S2 of highest fidelity is S2 itself.
IMHO?you are digging a swimming pool by the ocean to test your yatch.
I am not familiar with Selenium but anything you can do with JUnit you can do 
in JWebUnit since it is just
JUnit with more libraries.

I took your route or instantiating parts of S2 in standard JUnit tests and run 
into trouble when I moved
from 2,0,x to 2,1,x due to changes in the framework. It is a bummer when you 
switch versions and find
the tests are dead.
Chris



-Original Message-
From: Greg Lindholm greg.lindh...@gmail.com
To: Struts Users Mailing List user@struts.apache.org
Sent: Sat, Jul 18, 2009 3:00 am
Subject: Re: unit testing Struts2 application (with Spring and Hibernate)




  
   IMO that's outside the purview of unit testing, though--by definition
   this describes integration testing: the testing of an action along with
   the framework.
  
   There's nothing *wrong* with doing that testing, I just don't think
 it's
   the same thing as unit testing: independently testing the smallest bits
   of functionality.
  
   JUnit can be used for that kind of testing too (and I do, sometimes),
   but once I'm at that point I generally figure I might as well just be
   doing client-focused testing and testing the output of my results. I
   also use Selenium, although I may switch back to using a layer I wrote
   on top of Watir.
  
   Dave
  
 
  Not to throw weight around, but it is sort of curious to me that the
 three
  struts committers who chimed in all agreed that tip-to-tail integration
  testing in JUnit is not worth the effort. I only bring it up because,
 IMO,
  struts 2 is one of the best-unit-tested products I've ever worked on. I
 think
  Dave, Musachy and myself are biased against tip-to-tail in JUnit because
 in
  Struts 2, we have a guideline to unit test all bugfixes and new
 functionality.
  That being so, all three of us have probably come across situations where
  writing the unit test is 500x harder than writing the fix :)
 
  Dave does a good job of making the point I tried to make earlier,
 tip-to-tail
  testing is better looked at as an integration test and it becomes much
 easier
  to deal with as an integration test. If you are unfamiliar with selenium,
 it
  is worth learning. One of the posters earlier mentioned that he didn't
 want to
  learn another testing framework when he already knows JUnit. Selenium is
 nice
  because it runs right in the browser (IE and Firefox) and runs though a
 set of
  VB-like instructions... Things like - open this url, look for this text,
 click
  this link and then make sure this text exists. IMO, if you want to make
 sure
  th
at your action renders the appropriate result, this is way better than
  trying to coax the framework by bootstrapping it with mocks then figuring
 out a
  way to retrieve the rendered result. As an added bonus, it is possible to
 get
  maven to launch selenium tests, so you can get full unit and integration
  testing out of your CI if you are willing to put forth the effort.
 
  To drive the point home further, I would add that the Dojo plugin
 probably
  would have been more stable if we had taken the selenium approach (that
 is
  being employed with the slowly moving jquery plugin).
 
  -Wes
 


Not to pick on anyone but this isn't really a popularity contest. Different
situations have different needs and there is no reason to suggest that one
solution will work best for everyone.

At a large shop naming something unit testing vs integration testing
maybe important as it can determined who's job it is to do the work.  But at
a small shop, like I'm at, it makes no difference, it's all just testing and
it's the developers job.  So for me, whatever way is easiest, quickest and
gets the job done wins.

It took some work at first to figure out how to tests actions with the full
stack with junit but now that I have the plumbing figured out it's very easy
to add tests as actions are added. With junit I can easily set the database
to a know state before each test, or use mocks to simulate hard to setup
edge conditions (how easy is that to do with selenium?)  Plus it's easy to
jun junit with code coverage so I can see code isn't being covered. And, as
another already pointed out, junit is fast and convienent, 2 clicks from
inside Eclipse.

I do think it is great to see that the industry (at least those on this
list) recognizes the importance of automated testing and that with Struts
you have ability to test at the isolated pojo detail level all the way
through full blow integration testing.



Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread Paweł Wielgus
Hi all,
[the standard disclaimer ... ;-) ]
migrating from one version of struts to another is not a trouble for
selenium or such a tool,
actually it even helps You to be sure that the migration didn't break anything.
But You will hit the same scale of problems when You will change
layout - all selenium tests are dead,
of course one may argue that he is able to write selenium tests that way
that it will stay alive but that will be extra effort to cope with that.

Every solution have it's own set of troubles involved.
The is no better way, i think.
We can point out pros and cons of solutions we use,
so that others could make theirs decisions based on our experiences.

Best greetings,
Paweł Wielgus.


2009/7/19  musom...@aol.com:

 [The standard?diclaimer that it is not a popularity contest and nothing that 
 does the job for you is wrong.]
 Would you not agree that if you are going to mock up the framework the 
 simulation of S2 of highest fidelity is S2 itself.
 IMHO?you are digging a swimming pool by the ocean to test your yatch.
 I am not familiar with Selenium but anything you can do with JUnit you can do 
 in JWebUnit since it is just
 JUnit with more libraries.

 I took your route or instantiating parts of S2 in standard JUnit tests and 
 run into trouble when I moved
 from 2,0,x to 2,1,x due to changes in the framework. It is a bummer when you 
 switch versions and find
 the tests are dead.
 Chris



 -Original Message-
 From: Greg Lindholm greg.lindh...@gmail.com
 To: Struts Users Mailing List user@struts.apache.org
 Sent: Sat, Jul 18, 2009 3:00 am
 Subject: Re: unit testing Struts2 application (with Spring and Hibernate)




  
   IMO that's outside the purview of unit testing, though--by definition
   this describes integration testing: the testing of an action along with
   the framework.
  
   There's nothing *wrong* with doing that testing, I just don't think
 it's
   the same thing as unit testing: independently testing the smallest bits
   of functionality.
  
   JUnit can be used for that kind of testing too (and I do, sometimes),
   but once I'm at that point I generally figure I might as well just be
   doing client-focused testing and testing the output of my results. I
   also use Selenium, although I may switch back to using a layer I wrote
   on top of Watir.
  
   Dave
  
 
  Not to throw weight around, but it is sort of curious to me that the
 three
  struts committers who chimed in all agreed that tip-to-tail integration
  testing in JUnit is not worth the effort. I only bring it up because,
 IMO,
  struts 2 is one of the best-unit-tested products I've ever worked on. I
 think
  Dave, Musachy and myself are biased against tip-to-tail in JUnit because
 in
  Struts 2, we have a guideline to unit test all bugfixes and new
 functionality.
  That being so, all three of us have probably come across situations where
  writing the unit test is 500x harder than writing the fix :)
 
  Dave does a good job of making the point I tried to make earlier,
 tip-to-tail
  testing is better looked at as an integration test and it becomes much
 easier
  to deal with as an integration test. If you are unfamiliar with selenium,
 it
  is worth learning. One of the posters earlier mentioned that he didn't
 want to
  learn another testing framework when he already knows JUnit. Selenium is
 nice
  because it runs right in the browser (IE and Firefox) and runs though a
 set of
  VB-like instructions... Things like - open this url, look for this text,
 click
  this link and then make sure this text exists. IMO, if you want to make
 sure
  th
 at your action renders the appropriate result, this is way better than
  trying to coax the framework by bootstrapping it with mocks then figuring
 out a
  way to retrieve the rendered result. As an added bonus, it is possible to
 get
  maven to launch selenium tests, so you can get full unit and integration
  testing out of your CI if you are willing to put forth the effort.
 
  To drive the point home further, I would add that the Dojo plugin
 probably
  would have been more stable if we had taken the selenium approach (that
 is
  being employed with the slowly moving jquery plugin).
 
  -Wes
 


 Not to pick on anyone but this isn't really a popularity contest. Different
 situations have different needs and there is no reason to suggest that one
 solution will work best for everyone.

 At a large shop naming something unit testing vs integration testing
 maybe important as it can determined who's job it is to do the work.  But at
 a small shop, like I'm at, it makes no difference, it's all just testing and
 it's the developers job.  So for me, whatever way is easiest, quickest and
 gets the job done wins.

 It took some work at first to figure out how to tests actions with the full
 stack with junit but now that I have the plumbing figured out it's very easy
 to add tests as actions are added. With junit I can easily set the database
 to a know state before 

Re: Tags as template parameters?

2009-07-19 Thread Pascal
I just had a look at Sitemesh and it looks like what I want. There is one
thing that disturbs me, though, it looks as if sitemesh will build the page
every request, which causes quite some overhead, or am I mistaken there?

Pascal

On Fri, 17 Jul 2009 20:44:59 -0400, Wes Wannemacher w...@wantii.com
wrote:
 I'm on my phone so I only skimmed your message. If I read correctly,
 you should take a look at sitemesh.
 
 -W
 
 On 7/17/09, Pascal pas...@voidmage.net wrote:
 Hello list,

 in Struts2 it is possible to do something like

 html
 body
   s:myMainNavigationMenu /

   h1A page/h1
   pSome content/p
 /body
 /html

 This works ok, but my problem is that I have to change every single
 page, if i would like to have a custom footer for every page, for
 example.

 Now my question is: Is it possible to do something like this?

 s:useTemplate name=myCommonTemplate
 s:setTemplateTag name=body
 h1A page/h1
 pSome content/p
 /s:setTemplateTag
 /s:useTemplate

 And as template definition say

 !-- Main menu --
 table.../table

 s:insertTemplateTag name=body /

 !-- Footer --
 pThis is the footer/p

 I know it is possible to use parameters for templates, but they
 obviously have huge limitations as I can't just put the tags there as I
 did in my example.

 Also, I could do something like

 s:myMainTemplate
 s:param name=bodyPage value=page1.jsp /
 /s:myMainTemplate

 which I also don't want because I would have an extra (unnecessary)
page.

 So, is it possible to pass whole XML-Structures to templates? Maybe you
 have some reasons why I shouldn't want what I'm suggesting here, I would
 be eager to hear them! :-)

 Pascal



-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread Dave Newton

Paweł Wielgus wrote:

But You will hit the same scale of problems when You will change
layout - all selenium tests are dead,


I haven't really found that to be the case--I only rarely test deep 
structure with Selenium, but instead look for the presence of specific 
CSS selectors containing text etc.


That type of change is (generally) under my control, unlike a framework 
change that breaks *my* tests.


Dave

-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



RE: Struts w/Ajax with Struts again

2009-07-19 Thread cpanon
Hi Martin
Thanks, however I havnt migrate to Struts2 yet.  What I was hoping for was a 
technique to get all the session objects reprocessed upon return from the Ajax 
call.  I am unclear that if I do the mapping.getInputForward(), does that force 
tomcat to reprocess and the jsp reprocess all the session objects?  I have also 
tried returning from the Ajax call with a forward to the page and that doesnt 
force a redisplay of updated values.  That should, correct, it is no different 
than any other usage and with the json result being stored in the header it 
will not matter for that processing.  

--- On Sat, 7/18/09, Martin Gainty mgai...@hotmail.com wrote:

From: Martin Gainty mgai...@hotmail.com
Subject: RE: Struts w/Ajax with Struts again
To: Struts Users Mailing List user@struts.apache.org
Date: Saturday, July 18, 2009, 11:37 PM


better off setting it in the action

then access it thru ognl e.g.
#session.setSeeMe

http://struts.apache.org/2.0.14/docs/ognl-basics.html

 

if you want to see a bean described as

package package;
public class bean {
  
  private String BeanParamName;

  public void setBeanParamName(String BeanParamName){
    this.BeanParamName =BeanParamName ;
  }
  public String getBeanParamName(){
    return BeanParamName;
  }
} 

 in your jsp use s:bean as here

%@ taglib prefix=s uri=/struts-tags %

html

body 

  s:bean name=package.bean id=id
        s:param name=BeanParamNameBeanParam/s:param 
          s:property value=%{BeanParamName} /br
      /s:bean

/body

/html

http://struts.apache.org/2.0.6/struts2-core/apidocs/org/apache/struts2/components/Bean.html


Martin  
__ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem 
Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. 
Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung 
fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est 
interdite. Ce message sert à l'information seulement et n'aura pas n'importe 
quel effet légalement obligatoire. Étant donné que les email peuvent facilement 
être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité 
pour le contenu fourni.



 

 Date: Sat, 18 Jul 2009 19:07:26 -0700
 From: cpa...@yahoo.com
 Subject: RE: Struts w/Ajax with Struts again
 To: user@struts.apache.org
 
 Hi Martin
 Thank you.  So I may be misunderstanding something.  If I return to the Ajax 
 call with mapping.getInputForward() for the defined input attribute of that 
 action processing the Ajax call, should I see changes in the session objects 
 displayed?  Because before I return I set an element in the 
 actionForm.setSeeMe(hi), set the session.setAttribute(actionForm, 
 modified) and I do not see it in the display.  I am accessing that element 
 with the ${seeMe} syntax.  I am seeing the json results, but not what I put 
 in the actionForm.  That is what I would organically expect to see.  Can 
 you clarify this?  tia.
 
 --- On Sat, 7/18/09, Martin Gainty mgai...@hotmail.com wrote:
 
 From: Martin Gainty mgai...@hotmail.com
 Subject: RE: Struts w/Ajax with Struts again
 To: Struts Users Mailing List user@struts.apache.org
 Date: Saturday, July 18, 2009, 9:32 PM
 
 
 ajax's responsibility is to return either json or html formatted text to 
 innerHtml attribute of div tag identified in ajax call
 
 
 
 other activity such as refreshing session attributes would need to take place
 
 in the action class..upon return from action all tags of the jsp would 
 re-display the content based on the refreshed state for those session 
 attributes
 
 
 
 another strategy is to create and/or modify a jsp in the action and on return 
 forwards to a result *which will forward to the just created or modified jsp* 
 
 
 you're welcome
 Martin 
 __ 
 Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
 Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
 sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
 oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich 
 dem Austausch von Informationen und entfaltet keine rechtliche 
 Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen 
 wir keine Haftung fuer den Inhalt uebernehmen.
 
 Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
 destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
 l'expéditeur. 

Re: Struts w/Ajax with Struts again

2009-07-19 Thread Nils-Helge Garli Hegvik
The form will not be updated unless you refresh the values or the part
of the page that displays the values you want to be updated. So you
would have to manually do this processing some returned updated
values in a json result, or by returning the result of a partial jsp
which re-renders the part of the page that you want to be updated, and
then replace the existing html (using innerHTML).

Nils-H

On Sat, Jul 18, 2009 at 3:45 AM, cpanoncpa...@yahoo.com wrote:
 Hello
 I am able to use an Ajax call to a struts action and with getInputForward() 
 get the get the data back.  (With prototpye and json, very, very elegant).  I 
 am now realizing that I have cases where I want to reprocess all components, 
 i.e. I want the actionForm to be updated and the jsp recalculated, I want the 
 session objects to be reprocessed so my iterate tags will show the updates, 
 etc.  I understand how with getInputForward it doesnt happen.  If I 
 setAttribute(), the original value that was processed when the form first 
 generated is all that ever shows.  A simple findForward() does not force the 
 reprocessing,ie still the original values of the actionsForm members.  Is 
 there a way to force a complete reprocessing the jsp from within an action?  
 tia.


-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



RE: Struts w/Ajax with Struts again

2009-07-19 Thread Martin Gainty

apologies as i thought you were uptodate on the latest version

i assume you are implementing 

public ActionForward execute(ActionMapping mapping,
ActionForm form,
ServletRequest request,
ServletResponse response)
throws Exception;

 

the ActionMapping mapping will call findForward which will

Return an appropriate ActionForward object that identifies the presentation 
page to be used to generate this response, based on the newly updated beans. 
Typically, you will acquire a reference to such an object by calling 
findForward on either the ActionMapping object you received (if you are using a 
logical name local to this mapping), or on the controller servlet itself (if 
you are using a logical name global to the application).

 

its important to declare local variables in Action specifically:
controller servlet creates only one instance of your Action class, and uses 
this one instance to service all requests. Thus, you need to write thread-safe 
Action classes. Follow the same guidelines you would use to write thread-safe 
Servlets.  


Only Use Local Variables - The most important principle that aids in 
thread-safe coding is to use only local variables, not instance variables , in 
your Action class. Local variables are created on a stack that is assigned (by 
your JVM) to each request thread, so there is no need to worry about sharing 
them. An Action can be factored into several local methods, so long as all 
variables needed are passed as method parameters. This assures thread safety, 
as the JVM handles such variables internally using the call stack which is 
associated with a single Thread. 
 

For each request made of the controller, the method process(HttpServletRequest, 
HttpServletResponse) will be called.

so keep the guts of the request processing in the Action process method

 

http://struts.apache.org/1.x/userGuide/building_controller.html

 

ActionMapping considerations:

use RequestActionMapping instead of SessionActionMapping
http://struts.apache.org/1.x/apidocs/org/apache/struts/action/RequestActionMapping.html

 

hth
Martin Gainty 
__ 
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und 
Vertraulichkeitanmerkung/Note de déni et de confidentialité


 
Ez az üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy 
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának készítése 
nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és semmiféle jogi 
alkalmazhatósága sincs.  Mivel az electronikus üzenetek könnyen 
megváltoztathatóak, ezért minket semmi felelöség nem terhelhet ezen üzenet 
tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem 
Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. 
Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung 
fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est 
interdite. Ce message sert à l'information seulement et n'aura pas n'importe 
quel effet légalement obligatoire. Étant donné que les email peuvent facilement 
être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité 
pour le contenu fourni.



 

 Date: Sun, 19 Jul 2009 05:34:00 -0700
 From: cpa...@yahoo.com
 Subject: RE: Struts w/Ajax with Struts again
 To: user@struts.apache.org
 
 Hi Martin
 Thanks, however I havnt migrate to Struts2 yet.  What I was hoping for was a 
 technique to get all the session objects reprocessed upon return from the 
 Ajax call.  I am unclear that if I do the mapping.getInputForward(), does 
 that force tomcat to reprocess and the jsp reprocess all the session objects? 
  I have also tried returning from the Ajax call with a forward to the page 
 and that doesnt force a redisplay of updated values.  That should, correct, 
 it is no different than any other usage and with the json result being stored 
 in the header it will not matter for that processing.  
 
 --- On Sat, 7/18/09, Martin Gainty mgai...@hotmail.com wrote:
 
 From: Martin Gainty mgai...@hotmail.com
 Subject: RE: Struts w/Ajax with Struts again
 To: Struts Users Mailing List user@struts.apache.org
 Date: Saturday, July 18, 2009, 11:37 PM
 
 
 better off setting it in the action
 
 then access it thru ognl e.g.
 #session.setSeeMe
 
 http://struts.apache.org/2.0.14/docs/ognl-basics.html
 
 
 
 if you want to see a bean described as
 
 package package;
 public class bean {
   
   private String BeanParamName;
 
   public void setBeanParamName(String BeanParamName){
 

Login mechanism - preserve Action parameters

2009-07-19 Thread mathias-ewald

Hi,

a few days ago I implemented a login mechanism into my web application.
Therefore I use an abstract BaseAction, that asks the implementing class
wheter it want to be password protected or not. If it does and there's no
object named user available the Login.jsp is shown. When the Login form
returns the user object is placed into session scope. 

The Problem is, that after the Login.jsp has returned to the BaseAction all
parameters that were passed to the implementing Action are lost. 

What can I do?

Here' my code:

BaseAction.java
---
public abstract class BaseAction {

public static final Integer ALLOWED = 0;

public static final Integer DENIED = 1;

public static final Integer DENIED_GROUP = 2;


private String logout = false;

private String username;

private String password;

protected Log log;


public BaseAction() {}


public String execute() {
if(log == null) {
log = LogFactory.getLog(getClass());
}

MapString, Object session = 
ActionContext.getContext().getSession();

/*
 * if the user wants to logout, delete the object
 * from session scope.
 */
if(true.equals(logout)) {
Object userObj = session.get(user);
if(userObj != null) {
session.put(user, null);
log.info(User  + 
((UserAccount)userObj).getName() +  logged out.);
}
} 

/*
 * in case the username and password values are set, perform 
 * the login process.
 */
if (username != null  password != null) {
Session s = 
HibernateUtil.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();

UserAccount user = 
(UserAccount)s.createCriteria(UserAccount.class)
.add(Restrictions.eq(name, username))
.uniqueResult();

tx.commit();
s.close();

if(user == null) {
log.info(Error authenticating user  + 
username);
return loginError;
}

String dbHash = user.getPasswordhash().toLowerCase();
String formHash = MD5Util.md5(password).toLowerCase();

if(dbHash.equals(formHash)) {
session.put(user, user);  
log.info(User  + user.getName() +  logged 
in.);
} else {
log.info(Password mismatch for user  + 
username);
return loginError;
}
}

/*
 * If we get this far, userObject is either successfully logged
 * in or null, so get the UserAccount object or set it null.
 */
Object userObject = session.get(user);
UserAccount user = null;
if(userObject != null  userObject instanceof UserAccount) {
user = (UserAccount)userObject;
}

/*
 * Now ask the real action if access is allowed.
 */
int retVal = isAllowed(user);
if(retVal == ALLOWED) {
return executeAction();
} else if(retVal == DENIED_GROUP) {
return permissionError;
} else {
return login;
}
}

public abstract String executeAction();

public abstract Integer isAllowed(UserAccount user);

   // getter and setter methods

}
---

Login.jsp
---
html
head
jsp:include page=/common/Head.jsp/
/head
body

div id=container
jsp:include page=/common/Header.jsp/

div id=navi
Main gt; Login
/div

div id=body
brbrbrbrbr
div style=width: 40%; margin: 0 auto;

Re: Login mechanism - preserve Action parameters

2009-07-19 Thread Dave Newton
Have you considered using an interceptor for determining whether or not 
the user is logged in? Actions requiring login can be marked with an 
interface, annotation, etc. IMO this is a substantially cleaner 
architecture.


I'd also *strongly* discourage tying your actions to Hibernate like 
this; it makes testing more difficult than necessary and introduces an 
unnecessary level of coupling.


All that said, I'm not really sure which parameters aren't being 
preserved--are you doing a redirect?


Dave

mathias-ewald wrote:

Hi,

a few days ago I implemented a login mechanism into my web application.
Therefore I use an abstract BaseAction, that asks the implementing class
wheter it want to be password protected or not. If it does and there's no
object named user available the Login.jsp is shown. When the Login form
returns the user object is placed into session scope. 


The Problem is, that after the Login.jsp has returned to the BaseAction all
parameters that were passed to the implementing Action are lost. 


What can I do?

Here' my code:

BaseAction.java
---
public abstract class BaseAction {

public static final Integer ALLOWED = 0;

public static final Integer DENIED = 1;

public static final Integer DENIED_GROUP = 2;


private String logout = false;

private String username;

private String password;

protected Log log;


public BaseAction() {}


public String execute() {
if(log == null) {
log = LogFactory.getLog(getClass());
}

MapString, Object session = 
ActionContext.getContext().getSession();

/*
 * if the user wants to logout, delete the object
 * from session scope.
 */
if(true.equals(logout)) {
Object userObj = session.get(user);
if(userObj != null) {
session.put(user, null);
log.info(User  + ((UserAccount)userObj).getName() + 
 logged out.);
}
		} 
		

/*
		 * in case the username and password values are set, perform 
		 * the login process.

 */
if (username != null  password != null) {
Session s = 
HibernateUtil.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();

UserAccount user = 
(UserAccount)s.createCriteria(UserAccount.class)
.add(Restrictions.eq(name, username))
.uniqueResult();

tx.commit();
s.close();

if(user == null) {
log.info(Error authenticating user  + 
username);
return loginError;
}

String dbHash = user.getPasswordhash().toLowerCase();
String formHash = MD5Util.md5(password).toLowerCase();

if(dbHash.equals(formHash)) {
session.put(user, user);
log.info(User  + user.getName() +  logged 
in.);
} else {
log.info(Password mismatch for user  + 
username);
return loginError;
}
}

/*
 * If we get this far, userObject is either successfully logged
 * in or null, so get the UserAccount object or set it null.
 */
Object userObject = session.get(user);
UserAccount user = null;
if(userObject != null  userObject instanceof UserAccount) {
user = (UserAccount)userObject;
}

/*
 * Now ask the real action if access is allowed.
 */
int retVal = isAllowed(user);
if(retVal == ALLOWED) {
return executeAction();
} else if(retVal == DENIED_GROUP) {
return permissionError;
} else {
return login;
}
}

public abstract String executeAction();

public abstract Integer isAllowed(UserAccount user);

   // getter and setter methods

}

Re: Login mechanism - preserve Action parameters

2009-07-19 Thread mathias-ewald

Hi,


newton.dave wrote:
 
 Have you considered using an interceptor for determining whether or not 
 the user is logged in? Actions requiring login can be marked with an 
 interface, annotation, etc. IMO this is a substantially cleaner 
 architecture.
 

Okay - I didn't really care about interceptors yet. Maybe its time to do
that now.


newton.dave wrote:
 
 I'd also *strongly* discourage tying your actions to Hibernate like 
 this; it makes testing more difficult than necessary and introduces an 
 unnecessary level of coupling.
 

What do you suggest? Abstracting Hibernate from the Actions placing a data
access object in between? This is a university project I have to finish
untill 22nd July - I am definitly not going to do any testing ;)


newton.dave wrote:
 
 All that said, I'm not really sure which parameters aren't being 
 preserved--are you doing a redirect?
 

One Action, for example, receives a parameter with the URL. The BaseAction
the shows the Login and returns to the same Action. After that the parameter
is gone.

cu
mathias
-- 
View this message in context: 
http://www.nabble.com/Login-mechanism---preserve-Action-parameters-tp24559907p24560162.html
Sent from the Struts - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Re: Struts w/Ajax with Struts again

2009-07-19 Thread cpanon
Hi Nils  Martin
Thanks and I am sorry about being obtuse and I hope you will continue to make 
this an edifying moment for me.  

When I initially put up the form I code a value in the actionForm, myFormBean 
as myFormBean.setSeeMe(firstTime) and put into the session with 
session.setAttribute(fb00, myFormBean)  I display this value on the form page 
within a html:text with ...value=${sessionScope.fb00.seeMe}, and it shows 
firstTime on the form.  Good.  I process the Ajax call in another Action, fed 
with the same class of actionForm.  No matter whether I remove the action form 
session.removeAttirbute(fb00) or if I set it a different value, 
myFormBean2.setSeeMe(fromAjax);session.setAttribute(fb00,myFormBean2).  I 
still see firstTime.  

I am looking for the simplest technique that would reprocess the session 
objects from the new values, regenerate the full jsp, ancilliary to the json 
object I am setting, in the action processing the Ajax.  Specifically I am 
using the iterate tags to display a series of records and that is what I want 
to be reprocessed and redrawn to the user with the new values that will be in 
the session object set in the Ajax processing action.  

I understand that actions should/must be thread-safe, I just want to reprocess 
objects that are unique to state maintained by the session object.  I am 
suspecting this is not possible.

--- On Sun, 7/19/09, Nils-Helge Garli Hegvik nil...@gmail.com wrote:

From: Nils-Helge Garli Hegvik nil...@gmail.com
Subject: Re: Struts w/Ajax with Struts again
To: Struts Users Mailing List user@struts.apache.org
Date: Sunday, July 19, 2009, 9:56 AM

The form will not be updated unless you refresh the values or the part
of the page that displays the values you want to be updated. So you
would have to manually do this processing some returned updated
values in a json result, or by returning the result of a partial jsp
which re-renders the part of the page that you want to be updated, and
then replace the existing html (using innerHTML).

Nils-H

On Sat, Jul 18, 2009 at 3:45 AM, cpanoncpa...@yahoo.com wrote:
 Hello
 I am able to use an Ajax call to a struts action and with getInputForward() 
 get the get the data back.  (With prototpye and json, very, very elegant).  I 
 am now realizing that I have cases where I want to reprocess all components, 
 i.e. I want the actionForm to be updated and the jsp recalculated, I want the 
 session objects to be reprocessed so my iterate tags will show the updates, 
 etc.  I understand how with getInputForward it doesnt happen.  If I 
 setAttribute(), the original value that was processed when the form first 
 generated is all that ever shows.  A simple findForward() does not force the 
 reprocessing,ie still the original values of the actionsForm members.  Is 
 there a way to force a complete reprocessing the jsp from within an action?  
 tia.


-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread Paweł Wielgus
Hi Dave,
when i record my tests with selenium ide,
all click or assert alements takes various loactor addresses,
very often they contain DOM paths,
so when layout is changed from tables to divs,
all these addresses are no longer valid.

To present one simple example,
when i record logout click on one of my sites the recorded command i see is
clickAndWait //strong
instead of clickAndWait link=logout
i know this is plain wrong and i could manualy correct it but it
ilustrates the problem.
So when the layout will change and this logout will not be the first
element that is strong,
which is very probable, this test will fail to click the logout link.

I have done about 3 such huge layout changes and every time it
involved tests update.

Still, i use and promote selenium over junit for that kind of job.

Best greetings,
Paweł Wielgus.



2009/7/19 Dave Newton newton.d...@yahoo.com:
 Paweł Wielgus wrote:

 But You will hit the same scale of problems when You will change
 layout - all selenium tests are dead,

 I haven't really found that to be the case--I only rarely test deep
 structure with Selenium, but instead look for the presence of specific CSS
 selectors containing text etc.

 That type of change is (generally) under my control, unlike a framework
 change that breaks *my* tests.

 Dave

 -
 To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
 For additional commands, e-mail: user-h...@struts.apache.org



-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread Dave Newton

Paweł Wielgus wrote:

Hi Dave,
when i record my tests with selenium ide,
all click or assert alements takes various loactor addresses,
very often they contain DOM paths,
so when layout is changed from tables to divs,
all these addresses are no longer valid.


On the rare occasions I use the IDE to generate the script I find I 
always modify it pretty heavily--since I am pretty good about marking up 
my HTML it's almost easier to just write the tests by hand so I can 
target only the most-specific elements I'm looking for.


I'm rarely bitten by layout changes since the important stuff doesn't 
change much regardless of its surroundings.


YMMV :)

Dave

-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread Dimitrios Christodoulakis
Primarily for the sake of learning the inner mechanics of the struts2
framework, and unit testing, I took some time to study and experiment
with the code published at:
http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/

When testing (junit 4) an action implementing the Sessionaware
interface (my login and register classes) I noticed that the session
object is set to null by BaseStrutsTestCase. This was mentioned before
in the author's blog, but not sure if ever addressed. I added a new
comment over there but the discussion could be inactive.

I was wondering if anyone who is using the BaseStrutsTestCase, or used
it in the past, came across this issue and if by any chance managed to
resolve it. Perhaps Haroon might have a comment on this?

Also, a couple of more general questions:

1) Is there a recommended way to check during testing which
interceptors are firing and when?

2) If one with general knowledge of servlets  jsp wants to dive into
the struts2 source code, to get better understanding of the basic
mechanics, what would be the starting point? So should I start lets
say with the struts.core package?, which would be the entry point
class Despatcher, then ActionProxy? -- To the untrained eye (myself),
when looking the code from a distance
(http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/), it
looks somewhat like a ball of twine, so where should I look for the
piece of string that sticks out and will help me untangle it?

Well, the primary objective is testing our struts2 application, so I
will probably try other approaches mentioned in this discussion. So, I
would like to take a deeper look at Selenium next.

Kind regards and I appreciate all the input.


On Sun, Jul 19, 2009 at 4:35 PM, Dave Newtonnewton.d...@yahoo.com wrote:
 Paweł Wielgus wrote:

 Hi Dave,
 when i record my tests with selenium ide,
 all click or assert alements takes various loactor addresses,
 very often they contain DOM paths,
 so when layout is changed from tables to divs,
 all these addresses are no longer valid.

 On the rare occasions I use the IDE to generate the script I find I always
 modify it pretty heavily--since I am pretty good about marking up my HTML
 it's almost easier to just write the tests by hand so I can target only the
 most-specific elements I'm looking for.

 I'm rarely bitten by layout changes since the important stuff doesn't change
 much regardless of its surroundings.

 YMMV :)

 Dave

 -
 To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
 For additional commands, e-mail: user-h...@struts.apache.org



-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



Re: unit testing Struts2 application (with Spring and Hibernate)

2009-07-19 Thread Wes Wannemacher
On Sunday 19 July 2009 10:16:59 pm Dimitrios Christodoulakis wrote:

 2) If one with general knowledge of servlets  jsp wants to dive into
 the struts2 source code, to get better understanding of the basic
 mechanics, what would be the starting point? So should I start lets
 say with the struts.core package?, which would be the entry point
 class Despatcher, then ActionProxy? -- To the untrained eye (myself),
 when looking the code from a distance
 (http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/), it
 looks somewhat like a ball of twine, so where should I look for the
 piece of string that sticks out and will help me untangle it?


Depending on your goals, I think one place to start might be the xwork core. 
Xwork is an implementation of the command design pattern... 

http://en.wikipedia.org/wiki/Command_Pattern

Once you have a solid understanding of what is going on within xwork, then you 
can start with the Dispatcher. 

That being said, Struts is really the combination of many things. For 
instance, it lets xwork drive the core flow of request processing, but there is 
also the tag library. The tag library does a good job of breaking up 
processing into models, templating and jsp tag specific stuff. If you are 
interested in the tag library, start with Component and take a look at a few 
of the easier to follow tags (s:if, s:url, etc.). Part of understanding the 
tag library means learning freemarker, but freemarker is easy to learn as you 
go. 

-Wes

-- 
Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher

-
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org