@Test public void delete() { UserMapper sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); final User user = sut.find(2); assertThat(user.getId(), is(2)); assertThat(user.getLoginName(), is("Bar")); assertThat(user.getHashedUserData(), is("043bd227eaa879d438e7c1dfea568bc9")); sut.delete(user); try { sut.find(2); fail("Expected exception not thrown!"); } catch (DomainModelException ex) { assertThat(ex.getMessage(), is("There is no record set whith primary key '2'!")); } // avoid cache sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); try { sut.find(2); fail("Expected exception not thrown!"); } catch (DomainModelException ex) { assertThat(ex.getMessage(), is("There is no record set whith primary key '2'!")); } }
@Test public void findUserById() { final UserMapper sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); User user = sut.find(1); assertThat(user.getId(), is(1)); assertThat(user.getLoginName(), is("Foo")); assertThat(user.getHashedUserData(), is("b9f46238b289f23ba807973840655032")); user = sut.find(2); assertThat(user.getId(), is(2)); assertThat(user.getLoginName(), is("Bar")); assertThat(user.getHashedUserData(), is("043bd227eaa879d438e7c1dfea568bc9")); }
@Test public void update() { UserMapper sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); User user = sut.find(2); assertThat(user.getId(), is(2)); assertThat(user.getLoginName(), is("Bar")); assertThat(user.getHashedUserData(), is("043bd227eaa879d438e7c1dfea568bc9")); user.setLoginName("snafu"); user.setHashedUserData("snafupw"); sut.update(user); user = sut.find(2); // get from cache assertThat(user.getId(), is(2)); assertThat(user.getLoginName(), is("snafu")); assertThat(user.getHashedUserData(), is("snafupw")); sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); user = sut.find(2); assertThat(user.getId(), is(2)); assertThat(user.getLoginName(), is("snafu")); assertThat(user.getHashedUserData(), is("snafupw")); }
@Test public void insert() { final String loginName = "snafu"; final String hashedUserData = "snafupw"; User user = new User(loginName, hashedUserData); UserMapper sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); final int id = sut.insert(user); // avoid cache sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); user = sut.find(id); assertThat(user.getLoginName(), is(loginName)); assertThat(user.getHashedUserData(), is(hashedUserData)); }
@Test public void findUserById_caches() { final UserMapper sut = new UserMapper(dataSource(), new IntegerIdentityMap<User>()); final User user = sut.find(1); assertThat(user, is(sameInstance(sut.find(1)))); }