Пример #1
0
  @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();
  }
Пример #2
0
  /**
   * Updates a existing avatar
   *
   * @param avatar
   * @param image
   */
  public void update(Avatar avatar, UploadedFile uploadedFile) {
    this.isAllowed(avatar);

    if (avatar.getId() == 0) {
      throw new ValidationException("update() expects a avatar with an existing id");
    }

    Avatar current = this.repository.get(avatar.getId());
    avatar.setAvatarType(current.getAvatarType());

    // upload the img and get the upload img info
    String imageDiskName = this.processImageUpload(avatar, uploadedFile);

    if (imageDiskName != null) {
      this.deleteImage(current);

      current.setFileName(imageDiskName);
      current.setHeight(avatar.getHeight());
      current.setWidth(avatar.getWidth());
    }

    this.repository.update(current);
  }