From 8a27be78d2541b13caba8985b888457a8a9fd8cf Mon Sep 17 00:00:00 2001 From: miferreiro Date: Sat, 2 Mar 2019 17:29:16 +0100 Subject: [PATCH] Adds tests to check the idOwner of the pet entity Now, it is checked whether the negative idOwner correctly throws the exception IllegalArgumentException. --- .../java/es/uvigo/esei/daa/dao/PetsDAO.java | 14 ++++++++++--- .../java/es/uvigo/esei/daa/entities/Pet.java | 6 +++++- .../es/uvigo/esei/daa/dao/PetsDAOTest.java | 15 ++++++++++++-- .../uvigo/esei/daa/dataset/PetsDataset.java | 4 ++++ .../uvigo/esei/daa/entities/PetUnitTest.java | 20 +++++++++---------- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java index e31c4ef..6e7fd66 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java @@ -92,8 +92,16 @@ public class PetsDAO extends DAO { */ public Pet add(String name, String specie, int idOwner) throws DAOException, IllegalArgumentException { - if (name == null || specie == null) { - throw new IllegalArgumentException("name and specie can't be null " + specie); + if (name == null) { + throw new IllegalArgumentException("name can't be null " + name); + } + + if ( specie == null) { + throw new IllegalArgumentException("specie can't be null " + specie); + } + + if(idOwner < 0) { + throw new IllegalArgumentException("idOwner can't be negative " + specie); } try (Connection conn = this.getConnection()) { @@ -197,7 +205,7 @@ public class PetsDAO extends DAO { public List getPets(int idOwner) throws DAOException, IllegalArgumentException { - if ( idOwner <= 0) { + if ( idOwner < 0) { throw new IllegalArgumentException("idOwner can't be negative"); } diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java index 84bc538..69b5d1e 100644 --- a/src/main/java/es/uvigo/esei/daa/entities/Pet.java +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -2,6 +2,7 @@ package es.uvigo.esei.daa.entities; import static java.util.Objects.requireNonNull; + /** * An entity that represents a pet. * @@ -89,7 +90,10 @@ public class Pet { * @throws NullPointerException if the {@code idOwner} is {@code null}. */ public void setIdOwner(int idOwner) { - this.idOwner = requireNonNull(idOwner, "idOwner can't be null"); + if(idOwner < 1) { + throw new IllegalArgumentException(idOwner + " can't be less than 0"); + } + this.idOwner = idOwner; } @Override diff --git a/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java b/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java index 713213c..0caf492 100644 --- a/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java +++ b/src/test/java/es/uvigo/esei/daa/dao/PetsDAOTest.java @@ -9,6 +9,7 @@ import static es.uvigo.esei.daa.dataset.PetsDataset.newSpecie; import static es.uvigo.esei.daa.dataset.PetsDataset.newIdOwner; import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentId; import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentIdOwner; +import static es.uvigo.esei.daa.dataset.PetsDataset.negativeIdOwner; import static es.uvigo.esei.daa.dataset.PetsDataset.nonExistentPet; import static es.uvigo.esei.daa.dataset.PetsDataset.pets; import static es.uvigo.esei.daa.dataset.PetsDataset.petsOwner; @@ -72,6 +73,11 @@ public class PetsDAOTest { assertThat(this.dao.getPets(existentIdOwner()), containsPetsInAnyOrder(petsOwner(existentIdOwner()))); } + @Test(expected = IllegalArgumentException.class) + public void testGetPetsNegativeIdOwner() throws DAOException { + this.dao.getPets(negativeIdOwner()); + } + @Test public void testGet() throws DAOException { final Pet pet = this.dao.get(existentId()); @@ -83,7 +89,7 @@ public class PetsDAOTest { public void testGetNonExistentId() throws DAOException { this.dao.get(nonExistentId()); } - + @Test @ExpectedDatabase("/datasets/dataset-delete-pets.xml") public void testDelete() throws DAOException { @@ -140,7 +146,12 @@ public class PetsDAOTest { } @Test(expected = IllegalArgumentException.class) - public void testAddNullSurname() throws DAOException { + public void testAddNullSpecie() throws DAOException { this.dao.add(newName(), null, newIdOwner()); } + + @Test(expected = IllegalArgumentException.class) + public void testAddNegativeIdOwner() throws DAOException { + this.dao.add(newName(), newSpecie(),negativeIdOwner()); + } } diff --git a/src/test/java/es/uvigo/esei/daa/dataset/PetsDataset.java b/src/test/java/es/uvigo/esei/daa/dataset/PetsDataset.java index b3d20f3..79f8fee 100644 --- a/src/test/java/es/uvigo/esei/daa/dataset/PetsDataset.java +++ b/src/test/java/es/uvigo/esei/daa/dataset/PetsDataset.java @@ -72,6 +72,10 @@ public class PetsDataset { return 1234; } + public static int negativeIdOwner() { + return -1; + } + public static Pet existentPet() { return pet(existentId()); } diff --git a/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java b/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java index e474528..a2f6453 100644 --- a/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java +++ b/src/test/java/es/uvigo/esei/daa/entities/PetUnitTest.java @@ -36,10 +36,10 @@ public class PetUnitTest { new Pet(1, "Rex", null, 1); } -// @Test(expected = Error.class) -// public void testPetIntStringStringIntNullidOwner() { -// new Pet(1, "Rex", "Dog", null); -// } + @Test(expected = IllegalArgumentException.class) + public void testPetIntStringStringIntNegativeIdOwner() { + new Pet(1, "Rex", "Dog", -1); + } @Test public void testSetName() { @@ -100,12 +100,12 @@ public class PetUnitTest { assertThat(pet.getIdOwner(), is(equalTo(1))); } -// @Test(expected = Error.class) -// public void testSetNullIdOwner() { -// final Pet pet = new Pet(1, "Rex", "Dog", null); -// -// pet.setSpecie(null); -// } + @Test(expected = IllegalArgumentException.class) + public void testSetNegativeIdOwner() { + final Pet pet = new Pet(1, "Rex", "Dog", 1); + + pet.setIdOwner(-1); + } @Test -- 2.18.1