예제 #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
  @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());
  }
/** @author Rafael Steil */
public class TopicWatchTopicEventTestCase {
  private Mockery context = TestCaseUtils.newMockery();
  private TopicWatchDao repository = context.mock(TopicWatchDao.class);
  private TopicWatchTopicEvent event = new TopicWatchTopicEvent(repository);

  @Test
  public void deleted() {
    final Topic topic = new Topic();
    topic.setId(2);

    context.checking(
        new Expectations() {
          {
            one(repository).removeSubscription(topic);
          }
        });

    event.deleted(topic);
    context.assertIsSatisfied();
  }
}
예제 #4
0
/** @author Rafael Steil */
public class AvatarServiceTestCase {
  private Mockery context = TestCaseUtils.newMockery();
  private AvatarRepository repository = context.mock(AvatarRepository.class);
  private JForumConfig config = context.mock(JForumConfig.class);
  private AvatarService service = new AvatarService(config, repository);

  @Test(expected = NullPointerException.class)
  public void addNullExpectException() {
    service.add(null, null);
  }

  @Test(expected = ValidationException.class)
  public void addWithIdExpectException() {
    Avatar avatar = new Avatar();
    avatar.setId(1);

    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.add(avatar);
  }

  @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);
  }

  @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());
  }

  @Test(expected = NullPointerException.class)
  public void updateNullExpectException() {
    service.update(null, null);
  }

  @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
  public void deleteUsingNullShouldIgnore() {
    context.checking(
        new Expectations() {
          {
          }
        });
    Avatar avatar = null;
    service.delete(avatar);
  }

  @Test
  public void deleteExpectSuccess() {
    context.checking(
        new Expectations() {
          {
            Avatar s1 = new Avatar();
            s1.setId(1);
            s1.setFileName(Long.toString(System.currentTimeMillis()));
            Avatar s2 = new Avatar();
            s2.setId(2);
            s2.setFileName(Long.toString(System.currentTimeMillis()));

            String applicationPath =
                new File(this.getClass().getResource("").getFile()).getParent();

            atLeast(1).of(config).getApplicationPath();
            will(returnValue(applicationPath));
            atLeast(1).of(config).getValue(ConfigKeys.AVATAR_GALLERY_DIR);
            will(returnValue(""));

            one(repository).get(1);
            will(returnValue(s1));
            one(repository).remove(s1);

            one(repository).get(2);
            will(returnValue(s2));
            one(repository).remove(s2);
          }
        });

    service.delete(1, 2);
    context.assertIsSatisfied();
  }
}
/** @author Rafael Steil */
public class GroupAdminControllerTestCase extends AdminTestCase {
  private Mockery context = TestCaseUtils.newMockery();
  private GroupAdminController controller;
  private GroupRepository repository = context.mock(GroupRepository.class);
  private GroupService service = context.mock(GroupService.class);
  private CategoryRepository categoryRepository = context.mock(CategoryRepository.class);
  private UserSession userSession = context.mock(UserSession.class);
  private RoleManager roleManager = context.mock(RoleManager.class);
  private Result mockResult = context.mock(MockResult.class);
  private HttpServletRequest mockRequest = context.mock(HttpServletRequest.class);
  private GroupAdminController mockGroupAdminController = context.mock(GroupAdminController.class);

  public GroupAdminControllerTestCase() {
    super(GroupAdminController.class);
  }

  @Test
  public void permissions() {
    context.checking(
        new Expectations() {
          {
            one(repository).get(1);
            will(returnValue(new Group()));
            one(roleManager).isAdministrator();
            will(returnValue(true));
            one(categoryRepository).getAllCategories();
            will(returnValue(new ArrayList<Category>()));
            one(repository).getAllGroups();
            will(returnValue(new ArrayList<Group>()));

            one(mockResult).include("group", new Group());
            one(mockResult).include("groups", new ArrayList<Group>());
            one(mockResult).include("categories", new ArrayList<Category>());
            // TODO: fix PermOption				one(mockResult).include("permissions", new
            // PermissionOptions());
          }
        });

    controller.permissions(1);
    context.assertIsSatisfied();
  }

  @Test
  public void permissionsSave() {
    // TODO: fix PermOption		final PermissionOptions permissions = new PermissionOptions();

    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(true));
            // TODO: fix PermOption		one(service).savePermissions(1, permissions);
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    // TODO: fix PermOption	controller.permissionsSave(1, permissions);
    context.assertIsSatisfied();
  }

  @Test
  public void deleteIsFullAdministratorShouldAllow() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(true));
            one(service).delete(1, 2);
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.delete(1, 2);
    context.assertIsSatisfied();
  }

  @Test
  public void deleteIsNotFullAdministratorShouldIgnore() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(false));
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.delete(1, 2);
    context.assertIsSatisfied();
  }

  @Test
  public void list() {
    context.checking(
        new Expectations() {
          {
            one(repository).getAllGroups();
            will(returnValue(new ArrayList<Group>()));
            one(mockResult).include("groups", new ArrayList<Group>());
          }
        });

    controller.list();
    context.assertIsSatisfied();
  }

  @Test
  public void editExpectsAGroup() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(true));
            one(repository).get(2);
            will(returnValue(new Group()));
            one(mockResult).include("group", new Group());
            one(mockResult).forwardTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).add();
          }
        });

    controller.edit(2);
    context.assertIsSatisfied();
  }

  @Test
  public void editSaveIsFullAdministratorExpectsSuccess() {
    final Group group = new Group();

    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(true));

            one(service).update(group);
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.editSave(group);
    context.assertIsSatisfied();
  }

  @Test
  public void editSaveIsGroupManagerExpectsSuccess() {
    final Group group = new Group();

    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(false));
            one(roleManager).isGroupManager(group.getId());
            will(returnValue(true));

            one(service).update(group);
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.editSave(group);
    context.assertIsSatisfied();
  }

  @Test
  public void editSaveIsNotFullAdministratorAndNotGroupManagerShouldIgnore() {
    final Group group = new Group();

    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(false));
            one(roleManager).isGroupManager(group.getId());
            will(returnValue(false));

            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.editSave(group);
    context.assertIsSatisfied();
  }

  @Test
  public void addSaveIsFullAdministratorShouldAllow() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(true));
            one(service).add(with(aNonNull(Group.class)));
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.addSave(new Group());
    context.assertIsSatisfied();
  }

  @Test
  public void addSaveIsNotFullAdministratorShouldIgnore() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(false));
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.addSave(new Group());
    context.assertIsSatisfied();
  }

  @Test
  public void addIsNotFullAdministratorShouldIgnore() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(false));
            one(mockResult).redirectTo(controller);
            will(returnValue(mockGroupAdminController));
            one(mockGroupAdminController).list();
          }
        });

    controller.add();
    context.assertIsSatisfied();
  }

  @Test
  public void addIsFullAdministratorShouldAllow() {
    context.checking(
        new Expectations() {
          {
            one(roleManager).isAdministrator();
            will(returnValue(true));
          }
        });

    controller.add();
    context.assertIsSatisfied();
  }

  @Before
  public void setup() {
    controller =
        new GroupAdminController(
            service, repository, categoryRepository, mockResult, userSession, mockRequest);

    context.checking(
        new Expectations() {
          {
            allowing(userSession).getRoleManager();
            will(returnValue(roleManager));
          }
        });
  }
}