Example #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();
  }
Example #2
0
  @Test
  public void addExpectSuccess() throws IOException {
    final Avatar avatar = new Avatar();
    File tempFile = File.createTempFile("jforum", "tests");
    tempFile.deleteOnExit();
    final String tempDir = tempFile.getParent();

    File file = new File(this.getClass().getResource("/smilies/smilie.gif").getFile());
    TestCaseUtils.copyFile(file, tempFile);

    UploadedFile uploadedFile =
        new DefaultUploadedFile(new FileInputStream(file), file.getAbsolutePath(), "");

    context.checking(
        new Expectations() {
          {
            one(config).getApplicationPath();
            will(returnValue(tempDir));
            one(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).add(avatar);
          }
        });

    service.add(avatar, uploadedFile);
    context.assertIsSatisfied();
    Assert.assertNotNull(avatar.getFileName());

    File expectedFile = new File(String.format("%s/%s/%s", tempDir, "", avatar.getFileName()));
    expectedFile.deleteOnExit();

    Assert.assertTrue(expectedFile.exists());
  }