/**
* Copyright 2013 the original author or authors.
*
* May 23,201 3
*/
Import java. Io. file;
Import javax. servlet. servletexception;
Import org. Apache. Struts. Action. actionservlet;
Import org. JUnit. After;
Import org. JUnit. before;
Import org. JUnit. test;
Import org. springframework. Beans. Factory. Support. beandefinitionreader;
Import org. springframework. Beans. Factory. xml. xmlbeandefinitionreader;
Import org. springframework. Context. annotation. annotationconfigutils;
Import org. springframework. Web. Context. webapplicationcontext;
Import org. springframework. Web. Context. Support. genericwebapplicationcontext;
Import servletunit. Struts. mockstrutstestcase;
Import com. ystech. Struts. Action. mybaseactionservlet;
/**
* @ Author A5
*
*/
// Inherit mockstrutstestcase -- strutstest-2.1.3.jar test struts action
Public class sendemailactiontest extends mockstrutstestcase {
@ Before
Public void setup () throws exception {
Super. Setup ();
// Load the struts configuration file
This. setcontextdirectory (new file ("webcontent "));
This. setservletconfigfile ("/WEB-INF/Web. xml ");
This. setconfigfile ("/WEB-INF/struts-config.xml ");
// Create a spring Context
Genericwebapplicationcontext webcontext = new genericwebapplicationcontext ();
Beandefinitionreader beanreader = new xmlbeandefinitionreader (webcontext );
Beanreader. loadbeandefinitions (this. choosespringconfigs ());
Annotationconfigutils. registerannotationconfigprocessors (webcontext );
Webcontext. Refresh ();
Webcontext. registershutdownhook ();
// Set spring context to webcontext
This. Context. setattribute (
Webapplicationcontext. root_web_application_context_attribute,
Webcontext );
Webcontext. setservletcontext (this. context );
}
@ After
Public void teardown () throws exception {
Super. teardown ();
}
Public void testsendemailcs203pass (string badchar) throws servletexception {
// Load the custom Servlet
Mybaseactionservlet = new mybaseactionservlet ();
Mybaseactionservlet. INIT (this. config );
This. actionservlet = (actionservlet) mybaseactionservlet;
// Test the action
This. setrequestpathinfo ("/sendemail ");
This. addrequestparameter ("article_date", "pm et 05/22/13 ");
This. addrequestparameter ("article_publisher", "Reuters ");
This. addrequestparameter ("sender", "test@fmr.com ");
This. addrequestparameter ("recipient", "test@fmr.com ");
This. addrequestparameter ("message", badchar );
// Execute action
This. actionperform ();
// Verify the test result
This. verifynoactionerrors ();
}
Public void testsendemailcs203faild (string badchar ){
This. setrequestpathinfo ("/sendemail ");
This. addrequestparameter ("article_date", "pm et 05/22/13 ");
This. addrequestparameter ("article_publisher", "Reuters ");
This. addrequestparameter ("sender", "test@fmr.com ");
This. addrequestparameter ("recipient", "test@fmr.com ");
This. addrequestparameter ("message", badchar );
This. actionperform ();
String [] errors = {"errors. cs203.knowngood "};
This. verifyactionerrors (errors );
}
@ Test
Public void testemailgoodcharacter1 () throws servletexception {
This. testsendemailcs203pass ("\ t ");
}
@ Test
Public void testemailbadcharacter1 (){
This. testsendemailcs203faild ("&");
}
/**
* {@ Inheritdoc}
*/
Public String [] choosespringconfigs (){
Return New String [] {"file: src/config/spring/propertiescontext. xml ",
"File: src/config/spring/cs203context. xml ",
"File: src/config/spring/cachecontext. xml ",
"File: src/config/spring/jcacontext. xml ",
"File: src/config/spring/taskmanagercontext. xml ",
"File: src/config/spring/servicecontext. xml ",
"File: src/config/spring/modelcontext. xml ",
"File: src/config/spring/daocontext. xml ",};
}
}