From aba67c6fc58ccab4703de86d2d76e4e2dc80b7c0 Mon Sep 17 00:00:00 2001 From: Manny Date: Fri, 9 Jul 2021 15:39:05 -0400 Subject: [PATCH] Manny finished TDD Animal Factory --- .../java/rocks/zipcodewilmington/Food.java | 11 +- .../rocks/zipcodewilmington/animals/Cat.java | 5 + .../rocks/zipcodewilmington/animals/Dog.java | 4 + .../zipcodewilmington/animals/Mammal.java | 11 ++ .../zipcodewilmington/AnimalFactoryTest.java | 48 ++++++++ .../rocks/zipcodewilmington/CatHouseTest.java | 104 ++++++++++++++++++ .../java/rocks/zipcodewilmington/CatTest.java | 93 ++++++++++++++++ .../rocks/zipcodewilmington/DogHouseTest.java | 96 ++++++++++++++++ .../java/rocks/zipcodewilmington/DogTest.java | 101 +++++++++++++++++ 9 files changed, 472 insertions(+), 1 deletion(-) diff --git a/src/main/java/rocks/zipcodewilmington/Food.java b/src/main/java/rocks/zipcodewilmington/Food.java index 2e5056b..e79fbd3 100644 --- a/src/main/java/rocks/zipcodewilmington/Food.java +++ b/src/main/java/rocks/zipcodewilmington/Food.java @@ -3,5 +3,14 @@ /** * @author leon on 4/19/18. */ -public class Food { +public class Food { // we need to make up the Food variables + private String brand; + private int ounces; + private boolean isCanned; + + public Food (String brand, int ounces, boolean isCanned) { + this.brand = brand; + this.ounces = ounces; + this.isCanned = isCanned; + } } diff --git a/src/main/java/rocks/zipcodewilmington/animals/Cat.java b/src/main/java/rocks/zipcodewilmington/animals/Cat.java index e703c36..85cc5c5 100644 --- a/src/main/java/rocks/zipcodewilmington/animals/Cat.java +++ b/src/main/java/rocks/zipcodewilmington/animals/Cat.java @@ -10,6 +10,11 @@ public Cat(String name, Date birthDate, Integer id) { super(name, birthDate, id); } + public Cat(String name) { + super(name); + } + + public String speak() { return "meow!"; } diff --git a/src/main/java/rocks/zipcodewilmington/animals/Dog.java b/src/main/java/rocks/zipcodewilmington/animals/Dog.java index 7ed6dc0..f8ef39e 100644 --- a/src/main/java/rocks/zipcodewilmington/animals/Dog.java +++ b/src/main/java/rocks/zipcodewilmington/animals/Dog.java @@ -10,6 +10,10 @@ public Dog(String name, Date birthDate, Integer id) { super(name, birthDate, id); } + public Dog(String name) { + super(name); + } + public String speak() { return "bark!"; } diff --git a/src/main/java/rocks/zipcodewilmington/animals/Mammal.java b/src/main/java/rocks/zipcodewilmington/animals/Mammal.java index eec2aaa..9dc4448 100644 --- a/src/main/java/rocks/zipcodewilmington/animals/Mammal.java +++ b/src/main/java/rocks/zipcodewilmington/animals/Mammal.java @@ -14,6 +14,10 @@ public abstract class Mammal implements Animal { private String name; private Date birthDate; + + + + public Mammal(String name, Date birthDate, Integer id) { this.name = name; this.birthDate = birthDate; @@ -21,6 +25,13 @@ public Mammal(String name, Date birthDate, Integer id) { this.id = id; } + public Mammal(String name) { + this.name = name; + this.birthDate = null; + this.id = 0; + + } + public String getName() { return name; } diff --git a/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java b/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java index 7522ce3..7aa3ab4 100644 --- a/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java +++ b/src/test/java/rocks/zipcodewilmington/AnimalFactoryTest.java @@ -1,9 +1,57 @@ package rocks.zipcodewilmington; +import org.junit.Assert; +import org.junit.Test; +import rocks.zipcodewilmington.animals.Cat; +import rocks.zipcodewilmington.animals.Dog; +import rocks.zipcodewilmington.animals.animal_creation.AnimalFactory; +import rocks.zipcodewilmington.animals.animal_storage.CatHouse; +import rocks.zipcodewilmington.animals.animal_storage.DogHouse; + +import java.util.Date; + /** * @author leon on 4/19/18. */ public class AnimalFactoryTest { //TODO - Create Test for `Animal createDog(String name, Date birthDate)` //TODO - Create Test for `Animal createCat(String name, Date birthDate)` + + @Test + public void createDogTest(){ + // Given + String givenName = "Mochi"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + + // When + DogHouse.clear(); + Dog dog = AnimalFactory.createDog(givenName, givenBirthDate); + + + // Then + Assert.assertEquals(givenName, dog.getName()); + + + } + + + @Test + public void createCatTest(){ + // Given + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + + // When + CatHouse.clear(); + Cat cat = AnimalFactory.createCat(givenName, givenBirthDate); + + + // Then + Assert.assertEquals(givenName, cat.getName()); + + + } + } diff --git a/src/test/java/rocks/zipcodewilmington/CatHouseTest.java b/src/test/java/rocks/zipcodewilmington/CatHouseTest.java index f756b34..bda0f6c 100644 --- a/src/test/java/rocks/zipcodewilmington/CatHouseTest.java +++ b/src/test/java/rocks/zipcodewilmington/CatHouseTest.java @@ -1,5 +1,12 @@ package rocks.zipcodewilmington; +import org.junit.Assert; +import org.junit.Test; +import rocks.zipcodewilmington.animals.Cat; +import rocks.zipcodewilmington.animals.animal_storage.CatHouse; + +import java.util.Date; + /** * @author leon on 4/19/18. */ @@ -9,4 +16,101 @@ public class CatHouseTest { // TODO - Create tests for `void remove(Cat cat)` // TODO - Create tests for `Cat getCatById(Integer id)` // TODO - Create tests for `Integer getNumberOfCats()` + + @Test + public void addCatTest(){ + // Given + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + CatHouse.clear(); + + // When + Cat cat = new Cat(givenName, givenBirthDate, givenId); + CatHouse.add(cat); + int actual = CatHouse.getNumberOfCats(); + + // Then + Assert.assertEquals(1, actual); + +} + + @Test + public void removeCatByIdTest() { + // Given + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + CatHouse.clear(); + + // When + Cat cat = new Cat(givenName, givenBirthDate, givenId); + CatHouse.remove(0); + + + int actual = CatHouse.getNumberOfCats(); + + // Then + Assert.assertEquals(0, actual); + Assert.assertNull(CatHouse.getCatById(0)); + + } + + @Test + public void removeCatTest() { + // Given + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + CatHouse.clear(); + + // When + Cat cat = new Cat(givenName, givenBirthDate, givenId); + CatHouse.remove(cat); + + int actual = CatHouse.getNumberOfCats(); + + // Then + Assert.assertEquals(0, actual); + } + + @Test + public void getCatByIdTest() { + // Given + CatHouse.clear(); + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 2; + Cat cat = new Cat(givenName, givenBirthDate, givenId); + CatHouse.add(cat); + + + // When + Cat actual = CatHouse.getCatById(givenId); + + + // Then + Assert.assertEquals(cat, actual); + + } + + @Test + public void getNumberOfCatsTest () { + // Given + CatHouse.clear(); + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 2; + Cat cat = new Cat(givenName, givenBirthDate, givenId); + CatHouse.add(cat); + + // When + int actual = CatHouse.getNumberOfCats(); + + // Then + Assert.assertEquals(1, actual); + + } + + } diff --git a/src/test/java/rocks/zipcodewilmington/CatTest.java b/src/test/java/rocks/zipcodewilmington/CatTest.java index 4bd465f..8a0e546 100644 --- a/src/test/java/rocks/zipcodewilmington/CatTest.java +++ b/src/test/java/rocks/zipcodewilmington/CatTest.java @@ -2,7 +2,9 @@ import org.junit.Assert; import org.junit.Test; +import rocks.zipcodewilmington.animals.Animal; import rocks.zipcodewilmington.animals.Cat; +import rocks.zipcodewilmington.animals.Mammal; import java.util.Date; @@ -40,4 +42,95 @@ public void constructorTest() { Assert.assertEquals(givenId, retrievedId); } + @Test + public void setNameTest() { + // Given + String givenName = "Charlie"; + //Date givenBirthDate = new Date(); + //Integer givenId = 0; + String expectedName = "Garfield"; + + // When + Cat test = new Cat(givenName, null, null); + test.setName(expectedName); + + // Then + String actual = test.getName(); + Assert.assertEquals(expectedName, actual); + } + + @Test + public void speakTest() { + // Given + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + String expectedSpeak = "meow!"; + + // When + Cat test = new Cat(givenName, givenBirthDate, givenId); + String actual = test.speak(); + + // Then + Assert.assertEquals(expectedSpeak,actual); + } + + @Test + public void setBirthDateTest() { + // Given + Date givenBirthDate = new Date(); + Date expectedBirthDate = new Date(19-7-1978); + + // When + Cat test = new Cat(null, givenBirthDate, null); + test.setBirthDate(expectedBirthDate); + + // Then + Date actual = test.getBirthDate(); + Assert.assertEquals(expectedBirthDate, actual); + } + + @Test + public void eatTest() { + // Given + Integer expectedNumOfMeals = 1; // ate food 1 times + Food eatFood = new Food("Fancy Feast", 3, true); + + // When + Cat test = new Cat(null, null, null); + test.eat(eatFood); // ate food 1 times + + // Then + Integer actual = test.getNumberOfMealsEaten(); + Assert.assertEquals(expectedNumOfMeals, actual); + } + + @Test + public void getIdTest() { + // Given + String givenName = "Garfield"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + // String expectedSpeak = "meow!"; + + // When + Cat test = new Cat(givenName, givenBirthDate, givenId); + Integer actual = test.getId(); + + // Then + Assert.assertEquals(givenId,actual); + } + + @Test + public void testInheritanceMammal() { + Mammal cat = new Cat ("Garfield"); + Assert.assertTrue(cat instanceof Mammal); + } + + @Test + public void testInheritanceAnimal() { + Animal cat = new Cat("Garfield Junior"); + Assert.assertTrue(cat instanceof Animal); + + } } diff --git a/src/test/java/rocks/zipcodewilmington/DogHouseTest.java b/src/test/java/rocks/zipcodewilmington/DogHouseTest.java index b88b30b..2d81c51 100644 --- a/src/test/java/rocks/zipcodewilmington/DogHouseTest.java +++ b/src/test/java/rocks/zipcodewilmington/DogHouseTest.java @@ -1,8 +1,11 @@ package rocks.zipcodewilmington; +import org.junit.Assert; import org.junit.Test; +import rocks.zipcodewilmington.animals.Cat; import rocks.zipcodewilmington.animals.Dog; import rocks.zipcodewilmington.animals.animal_creation.AnimalFactory; +import rocks.zipcodewilmington.animals.animal_storage.CatHouse; import rocks.zipcodewilmington.animals.animal_storage.DogHouse; import java.util.Date; @@ -17,6 +20,99 @@ public class DogHouseTest { // TODO - Create tests for `Dog getDogById(Integer id)` // TODO - Create tests for `Integer getNumberOfDogs()` + @Test + public void addDogTest(){ + // Given + String givenName = "Mochi"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + DogHouse.clear(); + + // When + Dog dog = new Dog(givenName, givenBirthDate, givenId); + DogHouse.add(dog); + int actual = DogHouse.getNumberOfDogs(); + + // Then + Assert.assertEquals(1, actual); + + } + + @Test + public void removeDogByIdTest() { + // Given + String givenName = "Mochi Junior"; + Date givenBirthDate = new Date(); + Integer givenId = 1; + + + // When + Dog dog = new Dog(givenName, givenBirthDate, givenId); + DogHouse.remove(1); + + + int actual = DogHouse.getNumberOfDogs(); + + // Then + Assert.assertEquals(0, actual); + Assert.assertNull(DogHouse.getDogById(0)); + + } + + @Test + public void removeDogTest() { + // Given + String givenName = "Macha"; + Date givenBirthDate = new Date(); + Integer givenId = 2; + DogHouse.clear(); + + // When + Dog dog = new Dog(givenName, givenBirthDate, givenId); + DogHouse.remove(dog); + + int actual = DogHouse.getNumberOfDogs(); + + // Then + Assert.assertEquals(0, actual); + } + + +// @Test +// public void getNumberOfDogsTest () { +// // Given +// String givenName = "Macha"; +// Date givenBirthDate = new Date(); +// Integer givenId = 2; +// Dog dog = new Dog(givenName, givenBirthDate, givenId); +// DogHouse.add(dog); +// +// // When +// int actual = DogHouse.getNumberOfDogs(); +// +// // Then +// Assert.assertEquals(1, actual); +// +// } + + @Test + public void getDogByIdTest() { + // Given + String givenName = "Macha"; + Date givenBirthDate = new Date(); + Integer givenId = 2; + Dog dog = new Dog(givenName, givenBirthDate, givenId); + DogHouse.add(dog); + + // When + Dog actual = DogHouse.getDogById(givenId); + + + // Then + Assert.assertEquals(dog, actual); + + } + @Test public void testGetNumberOfDogs() { // Given (some diff --git a/src/test/java/rocks/zipcodewilmington/DogTest.java b/src/test/java/rocks/zipcodewilmington/DogTest.java index 34a15bd..476a226 100644 --- a/src/test/java/rocks/zipcodewilmington/DogTest.java +++ b/src/test/java/rocks/zipcodewilmington/DogTest.java @@ -2,7 +2,12 @@ import org.junit.Assert; import org.junit.Test; +import rocks.zipcodewilmington.animals.Animal; +import rocks.zipcodewilmington.animals.Cat; import rocks.zipcodewilmington.animals.Dog; +import rocks.zipcodewilmington.animals.Mammal; + +import java.util.Date; /** * @author leon on 4/19/18. @@ -15,6 +20,7 @@ public class DogTest { // TODO - Create tests for `Integer getId()` // TODO - Create test to check Animal inheritance; google search `java instanceof keyword` // TODO - Create test to check Mammal inheritance; google search `java instanceof keyword` + @Test public void setNameTest() { // Given (a name exists and a dog exists) @@ -28,4 +34,99 @@ public void setNameTest() { String dogName = dog.getName(); Assert.assertEquals(dogName, givenName); } + + @Test + public void constructorTest() { + // Given (dog data) + String givenName = "Mochi"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + + // When (a dog is constructed) + Dog dog = new Dog(givenName, givenBirthDate, givenId); + + // When (we retrieve data from the dog) + String retrievedName = dog.getName(); + Date retrievedBirthDate = dog.getBirthDate(); + Integer retrievedId = dog.getId(); + + // Then (we expect the given data, to match the retrieved data) + Assert.assertEquals(givenName, retrievedName); + Assert.assertEquals(givenBirthDate, retrievedBirthDate); + Assert.assertEquals(givenId, retrievedId); + } + + @Test + public void speakTest() { + // Given + String givenName = "Mochi"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + String expectedSpeak = "bark!"; + + // When + Dog test = new Dog(givenName, givenBirthDate, givenId); + String actual = test.speak(); + + // Then + Assert.assertEquals(expectedSpeak,actual); + } + + @Test + public void setBirthDateTest() { + // Given + Date givenBirthDate = new Date(); + Date expectedBirthDate = new Date(20-6-2021); + + // When + Dog test = new Dog(null, givenBirthDate, null); + test.setBirthDate(expectedBirthDate); + + // Then + Date actual = test.getBirthDate(); + Assert.assertEquals(expectedBirthDate, actual); + } + + @Test + public void eatTest() { + // Given + Integer expectedNumOfMeals = 1; // ate food 1 times + Food eatFood = new Food("Hungry Hippo", 5, true); + + // When + Dog test = new Dog(null, null, null); + test.eat(eatFood); // ate food 1 times + + // Then + Integer actual = test.getNumberOfMealsEaten(); + Assert.assertEquals(expectedNumOfMeals, actual); + } + + @Test + public void getIdTest() { + // Given + String givenName = "Mochi"; + Date givenBirthDate = new Date(); + Integer givenId = 0; + // String expectedSpeak = "bark!"; + + // When + Dog test = new Dog(givenName, givenBirthDate, givenId); + Integer actual = test.getId(); + + // Then + Assert.assertEquals(givenId,actual); + } + + @Test + public void testInheritanceMammal() { + Mammal dog = new Dog ("Mochi"); + Assert.assertTrue(dog instanceof Mammal); + } + + @Test + public void testInheritanceAnimal() { + Animal dog = new Dog("Mochi Junior"); + Assert.assertTrue(dog instanceof Animal); + } }