From a5f9bd9e4130f12a786fdc27b9e60a53c40419b5 Mon Sep 17 00:00:00 2001 From: hacklego Date: Sun, 12 Mar 2017 00:43:54 +0100 Subject: [PATCH] Add PetDAO tests --- .../es/uvigo/esei/daa/dao/PetsDAOTest.java | 125 ++++++++++++ .../uvigo/esei/daa/dao/PetsDAOUnitTest.java | 193 ++++++++++++++++++ 2 files changed, 318 insertions(+) create mode 100644 src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java create mode 100644 src/test/java/es/uvigo/esei/daa/dao/PetsDAOUnitTest.java diff --git a/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java b/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java new file mode 100644 index 0000000..39bde3d --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java @@ -0,0 +1,125 @@ +package es.uvigo.esei.daa.dao; + +import static es.uvigo.esei.daa.dataset.PetsDataset.*; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.*; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import javax.sql.DataSource; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.github.springtestdbunit.DbUnitTestExecutionListener; +import com.github.springtestdbunit.annotation.DatabaseSetup; +import com.github.springtestdbunit.annotation.ExpectedDatabase; + +import es.uvigo.esei.daa.entities.Pet; +import es.uvigo.esei.daa.listeners.ApplicationContextBinding; +import es.uvigo.esei.daa.listeners.ApplicationContextJndiBindingTestExecutionListener; +import es.uvigo.esei.daa.listeners.DbManagement; +import es.uvigo.esei.daa.listeners.DbManagementTestExecutionListener; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:contexts/mem-context.xml") +@TestExecutionListeners({ + DbUnitTestExecutionListener.class, + DbManagementTestExecutionListener.class, + ApplicationContextJndiBindingTestExecutionListener.class +}) +@ApplicationContextBinding( + jndiUrl = "java:/comp/env/jdbc/daaexample", + type = DataSource.class +) +@DbManagement( + create = "classpath:db/hsqldb.sql", + drop = "classpath:db/hsqldb-drop.sql" +) +@DatabaseSetup("/datasets/dataset.xml") +@ExpectedDatabase("/datasets/dataset.xml") +public class PetsDAOTest { + private PetsDAO dao; + + @Before + public void setUp() throws Exception { + this.dao = new PetsDAO(); + } + + @Test + public void testList() throws DAOException { + assertThat(this.dao.list(), containsPetsInAnyOrder(pets())); + } + + @Test + public void testGet() throws DAOException { + final Pet pet = this.dao.get(existentId()); + + assertThat(pet, is(equalsToPet(existentPet()))); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetNonExistentId() throws DAOException { + this.dao.get(nonExistentId()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-delete.xml") + public void testDelete() throws DAOException { + this.dao.delete(existentId()); + + assertThat(this.dao.list(), containsPetsInAnyOrder(petWithout(existentId()))); + } + + @Test(expected = IllegalArgumentException.class) + public void testDeleteNonExistentId() throws DAOException { + this.dao.delete(nonExistentId()); + } + + @Test + @ExpectedDatabase("/datasets/dataset-modify.xml") + public void testModify() throws DAOException { + final Pet pet = existentPet(); + pet.setId(newId()); + pet.setName(newName()); + pet.setKind(newKind()); + pet.setBreed(newBreed()); + pet.setOwner(newOwner()); + + this.dao.modify(pet); + + final Pet persistentPet = this.dao.get(pet.getId()); + + assertThat(persistentPet, is(equalsToPet(pet))); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyNonExistentId() throws DAOException { + this.dao.modify(nonExistentPet()); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyNullPet() throws DAOException { + this.dao.modify(null); + } + + @Test + @ExpectedDatabase("/datasets/dataset-add.xml") + public void testAdd() throws DAOException { + final Pet pet = this.dao.add(new Pet(newId(), newName(), newKind(), newBreed(), newOwner())); + + assertThat(pet, is(equalsToPet(newPet()))); + + final Pet persistentPet = this.dao.get(pet.getId()); + + assertThat(persistentPet, is(equalsToPet(newPet()))); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddNullPet() throws DAOException { + this.dao.add(null); + } +} diff --git a/src/test/java/es/uvigo/esei/daa/dao/PetsDAOUnitTest.java b/src/test/java/es/uvigo/esei/daa/dao/PetsDAOUnitTest.java new file mode 100644 index 0000000..46a50ec --- /dev/null +++ b/src/test/java/es/uvigo/esei/daa/dao/PetsDAOUnitTest.java @@ -0,0 +1,193 @@ +package es.uvigo.esei.daa.dao; + +import static es.uvigo.esei.daa.dataset.PetsDataset.*; +import static es.uvigo.esei.daa.matchers.IsEqualToPet.*; +import static org.easymock.EasyMock.anyString; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.reset; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.sql.SQLException; + +import org.junit.Test; + +import com.mysql.jdbc.Statement; + +import es.uvigo.esei.daa.entities.Pet; +import es.uvigo.esei.daa.util.DatabaseQueryUnitTest; + +public class PetsDAOUnitTest extends DatabaseQueryUnitTest { + @Test + public void testList() throws Exception { + final Pet[] pets = pets(); + + for (Pet pet : pets) { + expectPetRow(pet); + } + expect(result.next()).andReturn(false); + result.close(); + + replayAll(); + final PetsDAO petsDAO = new PetsDAO(); + + assertThat(petsDAO.list(), containsPetsInAnyOrder(pets)); + } + + @Test(expected = DAOException.class) + public void testListUnexpectedException() throws Exception { + expect(result.next()).andThrow(new SQLException()); + result.close(); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.list(); + } + + @Test + public void testGet() throws Exception { + final Pet existentPet = existentPet(); + + expectPetRow(existentPet); + result.close(); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + + assertThat(petsDAO.get(existentId()), is(equalTo(existentPet))); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetMissing() throws Exception { + expect(result.next()).andReturn(false); + result.close(); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.get(existentId()); + } + + @Test(expected = DAOException.class) + public void testGetUnexpectedException() throws Exception { + expect(result.next()).andThrow(new SQLException()); + result.close(); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.get(existentId()); + } + + @Test + public void testAdd() throws Exception { + final Pet pet = newPet(); + reset(connection); + expect(connection.prepareStatement(anyString(), eq(Statement.RETURN_GENERATED_KEYS))) + .andReturn(statement); + expect(statement.executeUpdate()).andReturn(1); + connection.close(); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + final Pet newPet = petsDAO.add(new Pet(pet.getId(), pet.getName(), pet.getKind(), pet.getBreed(), pet.getOwner())); + + assertThat(newPet, is(equalsToPet(pet))); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddNullPet() throws Exception { + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + + resetAll(); // No expectations + + petsDAO.add(null); + } + + @Test + public void testDelete() throws Exception { + expect(statement.executeUpdate()).andReturn(1); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.delete(existentId()); + } + + @Test(expected = IllegalArgumentException.class) + public void testDeleteInvalidId() throws Exception { + expect(statement.executeUpdate()).andReturn(0); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.delete(existentId()); + } + + @Test(expected = DAOException.class) + public void testDeleteUnexpectedException() throws Exception { + expect(statement.executeUpdate()).andThrow(new SQLException()); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.delete(existentId()); + } + + @Test + public void testModify() throws Exception { + expect(statement.executeUpdate()).andReturn(1); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.modify(existentPet()); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyNullPet() throws Exception { + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + + resetAll(); // No expectations + + petsDAO.modify(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testModifyZeroUpdatedRows() throws Exception { + expect(statement.executeUpdate()).andReturn(0); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.modify(existentPet()); + } + + @Test(expected = DAOException.class) + public void testModifyUnexpectedException() throws Exception { + expect(statement.executeUpdate()).andThrow(new SQLException()); + + replayAll(); + + final PetsDAO petsDAO = new PetsDAO(); + petsDAO.modify(existentPet()); + } + + private void expectPetRow(Pet pet) throws SQLException { + expect(result.next()).andReturn(true); + expect(result.getInt("id")).andReturn(pet.getId()); + expect(result.getString("name")).andReturn(pet.getName()); + expect(result.getString("kind")).andReturn(pet.getKind()); + expect(result.getString("breed")).andReturn(pet.getBreed()); + expect(result.getInt("owner")).andReturn(pet.getOwner()); + } +} -- 2.18.1