prajapativijay
Jun 11th, 2008, 08:05 AM
Dear All,
I have implemented FileUploadController as per spring guide line. Now I want to write Junit test for this controler. In which i just wanted to test, whether file is successfully uploaded or not?
Here I am providing onSubmit() of FileUploadController class.
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws MaxUploadSizeExceededException, Exception {
// fileMaxSize value should be coming from some configuration file
List errorList = new ArrayList();
Map map = new HashMap();
long fileSize = 0;
List campaign = new ArrayList();
FileUploadBean bean = (FileUploadBean) command;
try {
if (bean.getFile() == null) {
errorList.add("Please select a file to upload");
}
CommonsMultipartFile file = (CommonsMultipartFile) bean.getFile();
InputStream stream = file.getInputStream();
fileSize = stream.available();
}
catch (SpreadsheetReaderException srex) {
if (fileSize == 0) {
errorList.add("File is empty");
}
else {
errorList.add("Error occured while reading spreadsheet, not valid file type");
}
}
catch (NullPointerException npex) {
errorList.add("Empty or no file uploaded");
}
finally {
if (errorList.size() > 0) {
map.put("errorList", errorList);
}
map.put("campaign", campaign);
}
return new ModelAndView("displayhome", "homeView", map);
}
Please help me to develop this test.
I have implemented FileUploadController as per spring guide line. Now I want to write Junit test for this controler. In which i just wanted to test, whether file is successfully uploaded or not?
Here I am providing onSubmit() of FileUploadController class.
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws MaxUploadSizeExceededException, Exception {
// fileMaxSize value should be coming from some configuration file
List errorList = new ArrayList();
Map map = new HashMap();
long fileSize = 0;
List campaign = new ArrayList();
FileUploadBean bean = (FileUploadBean) command;
try {
if (bean.getFile() == null) {
errorList.add("Please select a file to upload");
}
CommonsMultipartFile file = (CommonsMultipartFile) bean.getFile();
InputStream stream = file.getInputStream();
fileSize = stream.available();
}
catch (SpreadsheetReaderException srex) {
if (fileSize == 0) {
errorList.add("File is empty");
}
else {
errorList.add("Error occured while reading spreadsheet, not valid file type");
}
}
catch (NullPointerException npex) {
errorList.add("Empty or no file uploaded");
}
finally {
if (errorList.size() > 0) {
map.put("errorList", errorList);
}
map.put("campaign", campaign);
}
return new ModelAndView("displayhome", "homeView", map);
}
Please help me to develop this test.