Announcement Announcement Module
Collapse
No announcement yet.
DiskFileItem getSize() returns zero size Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DiskFileItem getSize() returns zero size

    Hello. I'm trying to run unit test for my controller which accepts two files for upload:
    Code:
    public class CreateQuestionForm extends AbstractForm {
        private CommonsMultipartFile imageA;
        private CommonsMultipartFile imageB;
    
        public void updateFromModel(Question model) {
        }
        
        public void updateModel(Question model) {
            model.setImageA(imageA.getOriginalFilename());
            model.setImageB(imageB.getOriginalFilename());
        }
    
        public CommonsMultipartFile getImageA() {
            return imageA;
        }
    
        public void setImageA(CommonsMultipartFile imageA) {
            this.imageA = imageA;
        }
    
        public CommonsMultipartFile getImageB() {
            return imageB;
        }
    
        public void setImageB(CommonsMultipartFile imageB) {
            this.imageB = imageB;
        }
    }
    Code:
    @RequestMapping(method = RequestMethod.POST, value = "/addQuestion", produces = "application/json")
    public @ResponseBody String addUserQuestion(@RequestBody CreateQuestionForm questionForm, Principal principal)  throws Exception {
    }
    (actually this is not the case for now).

    What I'm trying to do in test is:
    Code:
    @Test
        public void shouldCreateQuestion() throws Exception {
            final File fileA = new File("src/test/resources/images/imageA.jpg");
            final File fileB = new File("src/test/resources/images/imageB.jpg");
            assertTrue(fileA.exists());
            assertTrue(fileB.exists());
    
    //until now everything seems to be ok. Even evaluating fileA.length() works fine and returns correct file size.
    
            final DiskFileItem fileItemA = new DiskFileItem("imageA", "image/jpeg", true, fileA.getName(), 100000000, fileA.getParentFile());
            final DiskFileItem fileItemB = new DiskFileItem("imageB", "image/jpeg", true, fileB.getName(), 100000000, fileB.getParentFile());
            fileItemA.getOutputStream(); // workaround to get rid of NullPointerException at isInMemory() call
            fileItemB.getOutputStream();
    
    // at this step I have a trouble when evaluating fileItemA.getSize() or .get(). They return 0 and nothing.
    
            byte yo[] = fileItemA.get();
    
            final CommonsMultipartFile imageA = new CommonsMultipartFile(fileItemA);
            final CommonsMultipartFile imageB = new CommonsMultipartFile(fileItemB);
    ...
        }
    Any ideas how to read the file? As a final result I need to get test file (like it was uploaded) and then copy to another destination.
    Any example would be helpful too. Thanks
Working...
X