@Test public void existReturnsWhetherAnEntityCanBeLoaded() throws Exception { flushTestUsers(); assertTrue(userDao.exists(id)); assertFalse(userDao.exists(id * 27)); }
private void assertDeleteCallDoesNotDeleteAnything(List<User> collection) { flushTestUsers(); Long count = userDao.count(); userDao.delete(collection); assertEquals(count, userDao.count()); }
/** Tests deleting a user. */ @Test public void testDelete() { flushTestUsers(); userDao.delete(firstUser); assertNull(userDao.readByPrimaryKey(id)); }
@Test public void returnsSamePageIfNoSpecGiven() throws Exception { Pageable pageable = new PageRequest(0, 1); flushTestUsers(); assertEquals(userDao.readAll(pageable), userDao.readAll(null, pageable)); }
@Test public void executesManipulatingQuery() throws Exception { flushTestUsers(); userDao.renameAllUsersTo("newLastname"); assertEquals(userDao.count().intValue(), userDao.findByLastname("newLastname").size()); }
/** * 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()); }
@Test public void removeDetachedObject() throws Exception { flushTestUsers(); em.detach(firstUser); userDao.delete(firstUser); assertThat(userDao.count(), is(2L)); }
@Test public void deleteColletionOfEntities() { flushTestUsers(); long before = userDao.count(); userDao.delete(Arrays.asList(firstUser, secondUser)); assertThat(userDao.count(), is(before - 2)); }
/** 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)); }
/** 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()); }
@Test public void executesMethodWithNamedParametersCorrectly() throws Exception { firstUser = userDao.save(firstUser); secondUser = userDao.save(secondUser); assertTrue( userDao .findByLastnameOrFirstname("Oliver", "Arrasz") .containsAll(Arrays.asList(firstUser, secondUser))); }
/** 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()); }
@Test public void executesCombinedSpecificationsCorrectly() { flushTestUsers(); Specification<User> spec = where(userHasFirstname("Oliver")).or(userHasLastname("Arrasz")); assertThat(userDao.readAll(spec).size(), is(2)); }
/** * 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)); }
@Test public void savingNullCollectionIsNoOp() throws Exception { List<User> result = userDao.save((Collection<User>) null); assertNotNull(result); assertTrue(result.isEmpty()); }
@Test public void savingEmptyCollectionIsNoOp() throws Exception { List<User> result = userDao.save(new ArrayList<User>()); assertNotNull(result); assertTrue(result.isEmpty()); }
/** * Make sure no {@link NullPointerException} is being thrown. * * @see Ticket #110 */ @Test public void testFinderInvocationWithNullParameter() { flushTestUsers(); userDao.findByLastname(null); }
/** Tests reading all users. */ @Test public void testReadAll() { flushTestUsers(); List<User> reference = Arrays.asList(firstUser, secondUser); assertTrue(userDao.readAll().containsAll(reference)); }
@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)); }
/** * Tests reading a single user. * * @throws Exception */ @Test public void testRead() throws Exception { flushTestUsers(); User foundPerson = userDao.readByPrimaryKey(id); assertEquals(firstUser.getFirstname(), foundPerson.getFirstname()); }
@Test public void executesSimpleNotCorrectly() throws Exception { flushTestUsers(); List<User> result = userDao.findByLastnameNot("Gierke"); assertThat(result.size(), is(2)); assertThat(result, hasItems(secondUser, thirdUser)); }
@Test public void executesLikeAndOrderByCorrectly() throws Exception { flushTestUsers(); List<User> result = userDao.findByLastnameLikeOrderByFirstnameDesc("%r%"); assertEquals(firstUser, result.get(0)); assertEquals(secondUser, result.get(1)); }
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; }
/** * 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)); }
/** * 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); }
@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)); }
@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)); }
/** 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)); }
/** 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)); }
@Test public void returnsAllAsPageIfNoPageableIsGiven() throws Exception { flushTestUsers(); assertEquals(new PageImpl<User>(userDao.readAll()), userDao.readAll((Pageable) null)); }