コード例 #1
0
  @Test
  public void existReturnsWhetherAnEntityCanBeLoaded() throws Exception {

    flushTestUsers();
    assertTrue(userDao.exists(id));
    assertFalse(userDao.exists(id * 27));
  }
コード例 #2
0
  private void assertDeleteCallDoesNotDeleteAnything(List<User> collection) {

    flushTestUsers();
    Long count = userDao.count();

    userDao.delete(collection);
    assertEquals(count, userDao.count());
  }
コード例 #3
0
  /** Tests deleting a user. */
  @Test
  public void testDelete() {

    flushTestUsers();

    userDao.delete(firstUser);
    assertNull(userDao.readByPrimaryKey(id));
  }
コード例 #4
0
  @Test
  public void returnsSamePageIfNoSpecGiven() throws Exception {

    Pageable pageable = new PageRequest(0, 1);

    flushTestUsers();
    assertEquals(userDao.readAll(pageable), userDao.readAll(null, pageable));
  }
コード例 #5
0
  @Test
  public void executesManipulatingQuery() throws Exception {

    flushTestUsers();
    userDao.renameAllUsersTo("newLastname");

    assertEquals(userDao.count().intValue(), userDao.findByLastname("newLastname").size());
  }
コード例 #6
0
  /**
   * Tests that all users get deleted by triggering {@link UserDao#deleteAll()}.
   *
   * @throws Exception
   */
  @Test
  public void deleteAll() throws Exception {

    flushTestUsers();

    userDao.deleteAll();

    assertEquals((Long) 0L, userDao.count());
  }
コード例 #7
0
  @Test
  public void removeDetachedObject() throws Exception {

    flushTestUsers();

    em.detach(firstUser);
    userDao.delete(firstUser);

    assertThat(userDao.count(), is(2L));
  }
コード例 #8
0
  @Test
  public void deleteColletionOfEntities() {

    flushTestUsers();

    long before = userDao.count();

    userDao.delete(Arrays.asList(firstUser, secondUser));
    assertThat(userDao.count(), is(before - 2));
  }
コード例 #9
0
  /** Tests, that the generic dao implements count correctly. */
  @Test
  public void testCountsCorrectly() {

    Long count = userDao.count();

    User user = new User();
    user.setEmailAddress("*****@*****.**");
    userDao.save(user);

    assertTrue(userDao.count().equals(count + 1));
  }
コード例 #10
0
  /** Tests updating a user. */
  @Test
  public void testUpdate() {

    flushTestUsers();

    User foundPerson = userDao.readByPrimaryKey(id);
    foundPerson.setLastname("Schlicht");

    User updatedPerson = userDao.readByPrimaryKey(id);
    assertEquals(foundPerson.getFirstname(), updatedPerson.getFirstname());
  }
コード例 #11
0
  @Test
  public void executesMethodWithNamedParametersCorrectly() throws Exception {

    firstUser = userDao.save(firstUser);
    secondUser = userDao.save(secondUser);

    assertTrue(
        userDao
            .findByLastnameOrFirstname("Oliver", "Arrasz")
            .containsAll(Arrays.asList(firstUser, secondUser)));
  }
コード例 #12
0
  /** Tests cascading on {@literal merge} operation. */
  @Test
  public void testMergingCascadesCollegueas() {

    firstUser.addColleague(secondUser);
    flushTestUsers();

    firstUser.addColleague(new User("Florian", "Hopf", "*****@*****.**"));
    firstUser = userDao.save(firstUser);

    User reference = userDao.readByPrimaryKey(firstUser.getId());
    Set<User> colleagues = reference.getColleagues();

    assertNotNull(colleagues);
    assertEquals(2, colleagues.size());
  }
コード例 #13
0
  @Test
  public void executesCombinedSpecificationsCorrectly() {

    flushTestUsers();
    Specification<User> spec = where(userHasFirstname("Oliver")).or(userHasLastname("Arrasz"));
    assertThat(userDao.readAll(spec).size(), is(2));
  }
コード例 #14
0
  /**
   * Asserts, that a call to {@code UserDao#readByPrimaryKey(Integer)} returns {@code null} for
   * invalid not {@code null} ids.
   */
  @Test
  public void testReadByPrimaryKeyReturnsNullForNotFoundEntities() {

    flushTestUsers();

    assertNull(userDao.readByPrimaryKey(id * 27));
  }
コード例 #15
0
  @Test
  public void savingNullCollectionIsNoOp() throws Exception {

    List<User> result = userDao.save((Collection<User>) null);
    assertNotNull(result);
    assertTrue(result.isEmpty());
  }
コード例 #16
0
  @Test
  public void savingEmptyCollectionIsNoOp() throws Exception {

    List<User> result = userDao.save(new ArrayList<User>());
    assertNotNull(result);
    assertTrue(result.isEmpty());
  }
コード例 #17
0
  /**
   * Make sure no {@link NullPointerException} is being thrown.
   *
   * @see Ticket #110
   */
  @Test
  public void testFinderInvocationWithNullParameter() {

    flushTestUsers();

    userDao.findByLastname(null);
  }
コード例 #18
0
  /** Tests reading all users. */
  @Test
  public void testReadAll() {

    flushTestUsers();

    List<User> reference = Arrays.asList(firstUser, secondUser);
    assertTrue(userDao.readAll().containsAll(reference));
  }
コード例 #19
0
  @Test
  public void savesCollectionCorrectly() throws Exception {

    List<User> result = userDao.save(Arrays.asList(firstUser, secondUser, thirdUser));
    assertNotNull(result);
    assertThat(result.size(), is(3));
    assertThat(result, hasItems(firstUser, secondUser, thirdUser));
  }
コード例 #20
0
  /**
   * Tests reading a single user.
   *
   * @throws Exception
   */
  @Test
  public void testRead() throws Exception {

    flushTestUsers();

    User foundPerson = userDao.readByPrimaryKey(id);
    assertEquals(firstUser.getFirstname(), foundPerson.getFirstname());
  }
コード例 #21
0
  @Test
  public void executesSimpleNotCorrectly() throws Exception {

    flushTestUsers();

    List<User> result = userDao.findByLastnameNot("Gierke");
    assertThat(result.size(), is(2));
    assertThat(result, hasItems(secondUser, thirdUser));
  }
コード例 #22
0
  @Test
  public void executesLikeAndOrderByCorrectly() throws Exception {

    flushTestUsers();

    List<User> result = userDao.findByLastnameLikeOrderByFirstnameDesc("%r%");
    assertEquals(firstUser, result.get(0));
    assertEquals(secondUser, result.get(1));
  }
コード例 #23
0
  private Page<User> executeSpecWithSort(Sort sort) {

    flushTestUsers();

    Specification<User> spec = where(userHasFirstname("Oliver")).or(userHasLastname("Matthews"));

    Page<User> result = userDao.readAll(spec, new PageRequest(0, 1, sort));
    assertThat(result.getTotalElements(), is(2L));
    return result;
  }
コード例 #24
0
  /**
   * Tests, that searching by the lastname of the reference user returns exactly that instance.
   *
   * @throws Exception
   */
  @Test
  public void testFindByLastname() throws Exception {

    flushTestUsers();

    List<User> byName = userDao.findByLastname("Gierke");

    assertTrue(byName.size() == 1);
    assertEquals(firstUser, byName.get(0));
  }
コード例 #25
0
  /**
   * Tests, that searching by the email address of the reference user returns exactly that instance.
   *
   * @throws Exception
   */
  @Test
  public void testFindByEmailAddress() throws Exception {

    flushTestUsers();

    User byName = userDao.findByEmailAddress("*****@*****.**");

    assertNotNull(byName);
    assertEquals(firstUser, byName);
  }
コード例 #26
0
  @Test
  public void returnsAllSortedCorrectly() throws Exception {

    flushTestUsers();
    List<User> result = userDao.readAll(new Sort(Order.ASCENDING, "lastname"));
    assertNotNull(result);
    assertThat(result.size(), is(3));
    assertThat(result.get(0), is(secondUser));
    assertThat(result.get(1), is(firstUser));
    assertThat(result.get(2), is(thirdUser));
  }
コード例 #27
0
  @Test
  public void executesCombinedSpecificationsWithPageableCorrectly() {

    flushTestUsers();
    Specification<User> spec = where(userHasFirstname("Oliver")).or(userHasLastname("Arrasz"));

    Page<User> users = userDao.readAll(spec, new PageRequest(0, 1));
    assertThat(users.getSize(), is(1));
    assertThat(users.hasPreviousPage(), is(false));
    assertThat(users.getTotalElements(), is(2L));
  }
コード例 #28
0
  /** Tests cascading persistence. */
  @Test
  public void testCascadesPersisting() {

    // Create link prior to persisting
    firstUser.addColleague(secondUser);

    // Persist
    flushTestUsers();

    // Fetches first user from .. bdatabase
    User firstReferenceUser = userDao.readByPrimaryKey(firstUser.getId());
    assertEquals(firstUser, firstReferenceUser);

    // Fetch colleagues and assert link
    Set<User> colleagues = firstReferenceUser.getColleagues();
    assertEquals(1, colleagues.size());
    assertTrue(colleagues.contains(secondUser));
  }
コード例 #29
0
  /** Flushes test users to the database. */
  private void flushTestUsers() {

    firstUser = userDao.save(firstUser);
    secondUser = userDao.save(secondUser);
    thirdUser = userDao.save(thirdUser);

    userDao.flush();

    id = firstUser.getId();

    assertThat(id, is(notNullValue()));
    assertThat(secondUser.getId(), is(notNullValue()));
    assertThat(thirdUser.getId(), is(notNullValue()));

    assertThat(userDao.exists(id), is(true));
    assertThat(userDao.exists(secondUser.getId()), is(true));
    assertThat(userDao.exists(thirdUser.getId()), is(true));
  }
コード例 #30
0
  @Test
  public void returnsAllAsPageIfNoPageableIsGiven() throws Exception {

    flushTestUsers();
    assertEquals(new PageImpl<User>(userDao.readAll()), userDao.readAll((Pageable) null));
  }