Commit 9fcbe54a authored by Jan Novák's avatar Jan Novák
Browse files

cover equals() an copy() methods, some defects found, so a few are ignored for now

parent 9db57a1f
Pipeline #4093 passed with stage
in 29 seconds
......@@ -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;
}
......
......@@ -64,7 +64,11 @@ public abstract class Item {
Item zbozi = (Item) object;
if( id == zbozi.getID()
&& name.equals(zbozi.getName())
// TODO: tester's note: doing equals() on any floating point number representation is very bad idea! :(
&& price == zbozi.getPrice()
&& category.equals(zbozi.getCategory())
) {
return true;
......
package cz.cvut.eshop.shop;
import org.junit.*;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.Date;
import static org.junit.Assert.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DiscountedItemTest {
private Date fromDate;
private Date toDate;
private int id;
private float price;
private int discountValid, discountInvalidSmall, discountInvalidLarge;
private String itemName, itemCategory;
private static Date fromDate, toDate;
private static int id;
private static float price;
private static int discountValid, discountInvalidSmall, discountInvalidLarge;
private static String itemName, itemCategory;
private static String fromDateString, toDateString;
@BeforeClass
public static void setUpClass() {
}
@Before
public void setUp() {
/*
Creating Date object using different constructor than Date() and Date(long milisSinceEpochBegining)
is deprecated!
*/
fromDate = new Date(1538352000L); // Oct-01-2018
toDate = new Date(1541030399L); // Oct-30-2018
long fromDateRaw = 1538344800000L;
long toDateRaw = 1541030399000L;
fromDate = new Date(fromDateRaw); // Oct-01-2018
toDate = new Date(toDateRaw); // Oct-30-2018
// expected valid date format is dd.mm.yyyy
fromDateString = "01.10.2018";
toDateString = "30.10.2018";
id = 1;
price = 50.0f;
......@@ -41,76 +49,157 @@ public class DiscountedItemTest {
itemName = "TestName";
itemCategory = "TestCategory";
}
@Test
public void Test001CreateDiscountedItemWithDateRangeByDateObject() {
public void TestCreateDiscountedItemWithDateRangeByDateObject() {
/*
Creating Date object using different constructor than Date() and Date(long milisSinceEpochBegining)
is deprecated!
*/
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
assertNotNull(d);
}
@Test
public void TestCreateDiscountedItemWithDateRangeByDateResultsInNonNullDateTo() {
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
Assert.assertNotNull(d);
assertNotNull(d.getDiscountTo());
}
@Test
public void Test002CreateDiscountedItemWithDateRangeByValidDateString() {
/*
Creating Date object using different constructor than Date() and Date(long milisSinceEpochBegining)
is deprecated!
*/
String fromDate = "03.05.2002";
String toDate = "03.05.2003";
public void TestCreateDiscountedItemWithDateRangeByDateResultsInNonNullDateFrom() {
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
assertNotNull(d.getDiscountFrom());
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountInvalidLarge, fromDate, toDate);
Assert.assertNotNull(d);
}
@Test
public void Test003CreateDiscountedItemWithDateRangeByInvalidFromDateString() {
/*
Creating Date object using different constructor than Date() and Date(long milisSinceEpochBegining)
is deprecated!
*/
public void TestCreateDiscountedItemWithDateRangeByValidDateString() {
// expected valid date format is dd.mm.yyyy, let's do an invalid one and await null upon object creation attempt
String fromDate = "3.2002";
String toDate = "03.05.2003";
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountInvalidLarge, fromDateString, toDateString);
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountInvalidSmall, fromDate, toDate);
Assert.assertNull(d.getDiscountFrom());
assertNotNull(d);
}
@Test
public void Test004CreateDiscountedItemWithDateRangeByInvalidFromDateString() {
/*
Creating Date object using different constructor than Date() and Date(long milisSinceEpochBegining)
is deprecated!
*/
public void TestCreateDiscountedItemWithDateRangeByInvalidFromDateString() {
// expected valid date format is dd.mm.yyyy, let's do an invalid one and await null upon object creation attempt
String fromDate = "3.5.2002";
String toDate = "03..2003";
String fromDateStringInvalid = "3.2018";
int id = 2;
float price = 105.0f;
int discount = 200;
String name = "TestName";
String category = "TestCategory";
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountInvalidSmall, fromDateStringInvalid, toDateString);
assertNull(d.getDiscountFrom());
}
@Test
public void TestCreateDiscountedItemWithDateRangeByInvalidToDateString() {
// expected valid date format is dd.mm.yyyy, let's do an invalid one and await null upon object creation attempt
String toDateStringInvalid = "1.2017";
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountInvalidSmall, fromDateString, toDateStringInvalid);
assertNull(d.getDiscountTo());
}
// TODO: ignored because of error in Item.equals() method, which needs fixing and possible redesign of the interface
@Ignore("Ignored because of error in Item.equals() method, which needs fixing and possible redesign of the interface")
@Test
public void TestDiscountedItemsAreEqual() {
DiscountedItem d1 = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
DiscountedItem d2 = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
assertEquals(d1, d2);
DiscountedItem d = new DiscountedItem(id, name, price, category, discount, fromDate, toDate);
Assert.assertNull(d.getDiscountTo());
}
@Test
public void Test0053DiscountedItemsAreEqual() {
public void TestDiscountedItemsAreNotEqual() {
DiscountedItem d1 = new DiscountedItem(id + 2, itemName, price, itemCategory, discountValid, fromDate, toDate);
DiscountedItem d2 = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
assertNotEquals(d1, d2);
}
@Test
public void Test006DiscountedItemsAreNotEqual() {
public void TestSetDiscountToValidDate() {
DiscountedItem d = new DiscountedItem(id, itemName, price, itemCategory, discountValid, fromDate, toDate);
Date changedDate = new Date(toDate.getTime() + 1000);
d.setDiscountTo(changedDate);
assertEquals(changedDate.getTime(), d.getDiscountTo().getTime());
}
@Test
public void TestSetDiscountToByInvalidDateIsIgnored() {
DiscountedItem d = new DiscountedItem(
id,
itemName,
price,
itemCategory,
discountValid,
fromDate,
toDate
);
String toDateStringInvalid = "3.2018";
d.setDiscountTo(toDateStringInvalid);
Date toDateInItem = d.getDiscountTo();
assertEquals(toDate, toDateInItem);
}
@Test
public void TestSetDiscountFromByInvalidDateIsIgnored() {
DiscountedItem d = new DiscountedItem(
id,
itemName,
price,
itemCategory,
discountValid,
fromDate,
toDate
);
String fromDateStringInvalid = "1.2018";
d.setDiscountFrom(fromDateStringInvalid);
Date fromDateInItem = d.getDiscountFrom();
assertEquals(fromDate, fromDateInItem);
}
// TODO: ignored because of incorrect handling of price -> discounted price and also because of defects in Item.equals()
@Ignore("Ignored because of incorrect handling of price -> discounted price and also because of defects in Item.equals()")
@Test
public void TestDeepCopyOfDiscountedItem() {
DiscountedItem orig, cop;
orig = new DiscountedItem(
id,
itemName,
price,
itemCategory,
discountValid,
fromDate,
toDate
);
cop = orig.copy();
assertEquals(orig, cop);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment