From 8761458a9a98efaa767110ac66dcd1c2f86be965 Mon Sep 17 00:00:00 2001 From: "petr.poliak" <poliape2@fel.cvut.cz> Date: Mon, 29 Oct 2018 21:44:14 +0100 Subject: [PATCH 1/3] added equals null test for standard item --- src/test/java/cz/cvut/eshop/shop/StandardItemTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java b/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java index 7fcf7d3..26d606b 100644 --- a/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java +++ b/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java @@ -13,4 +13,11 @@ public class StandardItemTest { assertEquals(item, copyItem); } + + + @Test + public void equalsShouldReturnFalseForNull() { + StandardItem item = new StandardItem(1, "Name", 9.68f, "Vegetable", 5); + assertFalse(item.equals(null)); + } } \ No newline at end of file -- GitLab From 87d8090088ed9bd947ed5a36c3f33660866b8e1d Mon Sep 17 00:00:00 2001 From: "petr.poliak" <poliape2@fel.cvut.cz> Date: Mon, 29 Oct 2018 21:44:40 +0100 Subject: [PATCH 2/3] added discount item test and fixed copy method --- .../cz/cvut/eshop/shop/DiscountedItem.java | 9 +-- src/main/java/cz/cvut/eshop/shop/Item.java | 8 +-- src/test/java/shop/DiscountedItemTest.java | 64 ++++++++++++++++++- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java b/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java index 404c1c1..9a8e3d4 100644 --- a/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java +++ b/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java @@ -51,11 +51,12 @@ public class DiscountedItem extends Item { @Override public boolean equals(Object object) { - if(object instanceof DiscountedItem){ + if (object instanceof DiscountedItem) { DiscountedItem zbozi = (DiscountedItem) object; return (super.equals(zbozi) - && discount == zbozi.getDiscount() - && discountTo.equals(zbozi.getDiscountTo())); + && discount == zbozi.getDiscount() + && discountTo.equals(zbozi.getDiscountTo()) + && discountFrom.equals(zbozi.getDiscountFrom())); } return false; } @@ -124,6 +125,6 @@ public class DiscountedItem extends Item { public DiscountedItem copy() { - return new DiscountedItem(getID(), getName(), getPrice(), getCategory(), discount, discountFrom, discountTo); + return new DiscountedItem(getID(), getName(), getOriginalPrice(), getCategory(), discount, discountFrom, discountTo); } } diff --git a/src/main/java/cz/cvut/eshop/shop/Item.java b/src/main/java/cz/cvut/eshop/shop/Item.java index a16f1f7..f729060 100644 --- a/src/main/java/cz/cvut/eshop/shop/Item.java +++ b/src/main/java/cz/cvut/eshop/shop/Item.java @@ -62,10 +62,10 @@ public abstract class Item { public boolean equals(Object object){ if(object instanceof Item){ Item zbozi = (Item) object; - if( id == zbozi.getID() - && name.equals(zbozi.getName()) - && price == zbozi.getPrice() - && category.equals(zbozi.getCategory()) + if( id == zbozi.id + && name.equals(zbozi.name) + && price == zbozi.price + && category.equals(zbozi.category) ) { return true; } diff --git a/src/test/java/shop/DiscountedItemTest.java b/src/test/java/shop/DiscountedItemTest.java index a19259c..61a2032 100644 --- a/src/test/java/shop/DiscountedItemTest.java +++ b/src/test/java/shop/DiscountedItemTest.java @@ -3,10 +3,10 @@ package shop; import cz.cvut.eshop.shop.DiscountedItem; import org.junit.Ignore; import org.junit.Test; + import java.util.Date; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; public class DiscountedItemTest { @Test @@ -157,4 +157,64 @@ public class DiscountedItemTest { assertEquals(1.6f, actual, 0.001f); } + + @Test + public void copyCreatesEqualCopy() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "10.05.2017", "10.05.2018"); + DiscountedItem copy = discountedItem.copy(); + + assertEquals(discountedItem.getDiscount(), copy.getDiscount()); + assertEquals(discountedItem.getPrice(), copy.getPrice(), Double.MIN_VALUE); + assertEquals(discountedItem.getOriginalPrice(), copy.getOriginalPrice(), Double.MIN_VALUE); + assertEquals(discountedItem.getDiscountedPrice(), copy.getDiscountedPrice(), + Double.MIN_VALUE); + assertEquals(discountedItem.getID(), copy.getID()); + assertEquals(discountedItem.getCategory(), copy.getCategory()); + assertEquals(discountedItem.getName(), copy.getName()); + + + } + + @Test + public void modifyingCopyDoesntChangeOriginal() { + int originalDiscount = 60; + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1", + originalDiscount, "10.05.2017", "10.05.2018"); + + DiscountedItem copy = discountedItem.copy(); + int newDiscount = 20; + copy.setDiscount(newDiscount); + + assertEquals("Setting discount wasnt successful", newDiscount, copy.getDiscount()); + assertEquals("Modifying copy changed original", originalDiscount, + discountedItem.getDiscount()); + } + + @Test + public void equalsWorksOnSameObject() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "10.05.2017", "10.05.2018"); + DiscountedItem copy = discountedItem.copy(); + + assertNotSame("Copy returned the same object", copy, discountedItem); + assertTrue(discountedItem.equals(copy)); + } + + + @Test + public void equalsCheckAllArgs() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "10.05.2017", "11.05.2018"); + DiscountedItem changedFrom = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "11.05.2017", "11.05.2018"); + DiscountedItem changedTo = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "10.05.2017", "12.05.2018"); + DiscountedItem changedDiscount = new DiscountedItem(1, "name1", 1.6f, "category1", + 40, "10.05.2017", "11.05.2018"); + + assertNotEquals(changedFrom, discountedItem); + assertNotEquals(changedTo, discountedItem); + assertNotEquals(changedDiscount, discountedItem); + } } -- GitLab From 78d78153155760cf254b1f043e7abe3e511b7c0e Mon Sep 17 00:00:00 2001 From: "petr.poliak" <poliape2@fel.cvut.cz> Date: Mon, 29 Oct 2018 21:54:37 +0100 Subject: [PATCH 3/3] added copy equals test --- .../java/cz/cvut/eshop/shop/DiscountedItem.java | 2 +- src/test/java/shop/DiscountedItemTest.java | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java b/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java index 9a8e3d4..c22d096 100644 --- a/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java +++ b/src/main/java/cz/cvut/eshop/shop/DiscountedItem.java @@ -19,7 +19,7 @@ public class DiscountedItem extends Item { public DiscountedItem(int id, String name, float price, String category, int discount, Date discountFrom, Date discountTo) { super(id, name, price, category); this.discount = discount > 100 ? 0 : discount; - this.discountTo = discountFrom; + this.discountFrom = discountFrom; this.discountTo = discountTo; } diff --git a/src/test/java/shop/DiscountedItemTest.java b/src/test/java/shop/DiscountedItemTest.java index 61a2032..78c5cde 100644 --- a/src/test/java/shop/DiscountedItemTest.java +++ b/src/test/java/shop/DiscountedItemTest.java @@ -193,12 +193,12 @@ public class DiscountedItemTest { @Test public void equalsWorksOnSameObject() { - DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1", + DiscountedItem discountedItem1 = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "10.05.2017", "10.05.2018"); + DiscountedItem discountedItem2 = new DiscountedItem(1, "name1", 1.6f, "category1", 60, "10.05.2017", "10.05.2018"); - DiscountedItem copy = discountedItem.copy(); - assertNotSame("Copy returned the same object", copy, discountedItem); - assertTrue(discountedItem.equals(copy)); + assertTrue(discountedItem1.equals(discountedItem2)); } @@ -217,4 +217,12 @@ public class DiscountedItemTest { assertNotEquals(changedTo, discountedItem); assertNotEquals(changedDiscount, discountedItem); } + + @Test + public void copyEquals(){ + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1", + 60, "10.05.2017", "11.05.2018"); + DiscountedItem copy = discountedItem.copy(); + assertTrue(discountedItem.equals(copy)); + } } -- GitLab