Here's a test I use to verify a csv upload:

public void testCSVUpload()
{
log.debug("testing csv upload...");
MockHttpServletRequest request = new
MockHttpServletRequest("POST","/importData.html");

List<MockFileItem> fileItems = new ArrayList<MockFileItem>();
//Create the test upload (Type, content-type, filename, content of the file)
MockFileItem fileItem = new MockFileItem("file","text/plain","test.csv
","1;1");
fileItems.add(fileItem);
MockCommonsMultipartResolver resolver = new
MockCommonsMultipartResolver(fileItems );
request.setContentType("multipart/form-data");
request.addHeader("Content-type", "multipart/form-data");

//Add any required parameters, e.g.
request.addParameter("importType", "EH");
assertTrue(resolver.isMultipart(request));
MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart
(request);
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = importDataController.handleRequest
(multipartRequest,response);
//Do the asserts...
assertNotNull("Fetched no ModelAndView ["+mav+"]",mav);
//....
}


Bye,

Peter


2007/9/20, Matt Raible <[EMAIL PROTECTED]>:
>
> I would suggest using Canoo WebTest to test file-upload. There's a
> test demonstrating how to do it in web-tests.xml.
>
> Matt
>
> P.S. I like the lingo. ;-)
>
> On 9/13/07, Yopy <[EMAIL PROTECTED]> wrote:
> >
> > Y halo thar,
> >
> > I've been working on a project using AppFuse, so far so good. I copied
> the
> > existing file upload sources into my own project, and have done some
> > adaptations (so that all the files are put into a single directory and
> given
> > an unique name), and I managed to get it to work - no problems there.
> >
> > Now, for completeness and such, I'm trying to write a unit test to test
> the
> > functionality. I've been Googling for an example (seeing that I'm too
> > inexperienced to come up with a test of my own), first looking for the
> > source of the file upload test in AppFuse itself (if any), but I didn't
> find
> > anything. So, next up a generic file upload test, and I found the
> following
> > thread
> >
> > http://forum.springframework.org/showthread.php?t=10154
> >
> > from three years~ish ago, a possible solution being posted by mr. Raible
> > himself. I've copypasta'd the solution and did some tweaks (i.e. remove
> the
> > email bit), and it shoudl work largely now. However, when I try to run
> the
> > test, I get an error from
> > org.apache.commons.fileupload.FileUploadBase$UnknownSizeException: the
> > request was rejected because its size is unknown.
> >
> > Logically speaking, it would be a matter of adding a content-length to
> the
> > request, like so:
> >
> > request.addHeader("Content-length", 1234);
> >
> > buuut that don't work. So, I'd like to ask two questions:
> >
> > 1. How would I be able to solve this problem?
> > 2. Probably easier, where can I find the source for the file that tests
> the
> > default file upload controller in AppFuse?
> >
> > The test I have so far:
> >
> > public class XmlFileUploadControllerTest extends TestCase {
> >
> >
> >     private XmlFileUploadController fileUpload = null;
> >
> >     private static Log log =
> > LogFactory.getLog(XmlFileUploadControllerTest.class);
> >
> >     public void setUp() {
> >
> >         fileUpload = (XmlFileUploadController) new
> > XmlFileUploadController();
> >
> >     }
> >
> >     public void testUpload() throws Exception {
> >         log.debug("testing upload...");
> >         MockHttpServletRequest request =
> >             new MockHttpServletRequest("POST", "/xmlfileupload.html");
> >
> >         MockCommonsMultipartResolver resolver = new
> > MockCommonsMultipartResolver();
> >
> >
> >         request.setContentType("multipart/form-data");
> >         request.addHeader("Content-type", "multipart/form-data");
> >         request.addHeader("Content-length", new Integer(0));
> >
> >         assertTrue(resolver.isMultipart(request));
> >         MultipartHttpServletRequest multipartRequest =
> > resolver.resolveMultipart(request);
> >
> >                 ModelAndView mav =
> >             fileUpload.handleRequest(multipartRequest, new
> > MockHttpServletResponse());
> >
> >         log.debug("model: " + mav.getModel());
> >
> >         assertNotNull(request.getSession().getAttribute("message"));
> >
> >
> > }
> >
> > followed by some mocks, copypasta'd directly from the thread I posted
> > earlier.
> >
> > Any help, pointers (non-C plz), or pimpslaps would be appreciated.
> >
> >
> > --
> > View this message in context:
> http://www.nabble.com/Testing-File-Upload-tf4435990s2369.html#a12655704
> > Sent from the AppFuse - User mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
>
> --
> http://raibledesigns.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to