@Test public void updateImageShouldDeleteOldImage() throws IOException { final File currentFile = File.createTempFile("avatar", "tests"); currentFile.deleteOnExit(); final Avatar currentAvatar = new Avatar(); currentAvatar.setId(1); currentAvatar.setAvatarType(AvatarType.AVATAR_GALLERY); currentAvatar.setFileName(currentFile.getName()); context.checking( new Expectations() { { one(repository).get(1); will(returnValue(currentAvatar)); atLeast(1).of(config).getApplicationPath(); will(returnValue(currentFile.getParent())); atLeast(1).of(config).getValue(ConfigKeys.AVATAR_GALLERY_DIR); will(returnValue("")); one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_GALLERY); will(returnValue(true)); one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_UPLOAD); will(returnValue(true)); one(config).getLong(ConfigKeys.AVATAR_MAX_SIZE); will(returnValue(10000l)); one(config).getInt(ConfigKeys.AVATAR_MAX_WIDTH); will(returnValue(800)); one(config).getInt(ConfigKeys.AVATAR_MAX_HEIGHT); will(returnValue(600)); one(config).getInt(ConfigKeys.AVATAR_MIN_WIDTH); will(returnValue(1)); one(config).getInt(ConfigKeys.AVATAR_MIN_HEIGHT); will(returnValue(1)); one(repository).update(currentAvatar); } }); File originalFile = new File(this.getClass().getResource("/smilies/smilie.gif").getFile()); File newFile = File.createTempFile("jforum", "tests"); TestCaseUtils.copyFile(originalFile, newFile); UploadedFile uploadedFile = new DefaultUploadedFile(new FileInputStream(newFile), newFile.getAbsolutePath(), ""); String oldDiskName = currentAvatar.getFileName(); Avatar newAvatar = new Avatar(); newAvatar.setId(1); newAvatar.setAvatarType(AvatarType.AVATAR_GALLERY); service.update(newAvatar, uploadedFile); context.assertIsSatisfied(); Assert.assertEquals(newAvatar.getAvatarType(), currentAvatar.getAvatarType()); Assert.assertFalse(currentFile.exists()); Assert.assertFalse(currentAvatar.getFileName().equals(oldDiskName)); new File(String.format("%s/%s", currentFile.getParent(), currentAvatar.getFileName())).delete(); }
@Test(expected = ValidationException.class) public void updateWithoutIdExpectException() { Avatar avatar = new Avatar(); avatar.setId(0); context.checking( new Expectations() { { one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_GALLERY); will(returnValue(true)); one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_UPLOAD); will(returnValue(true)); } }); service.update(avatar, null); }