Commit d5dbf2a5 authored by Karel Frajták's avatar Karel Frajták

merge

parent 06cbc763
package archive;
import cz.cvut.eshop.archive.ItemPurchaseArchiveEntry;
import cz.cvut.eshop.archive.PurchasesArchive;
import cz.cvut.eshop.shop.Item;
import cz.cvut.eshop.shop.Order;
import cz.cvut.eshop.shop.ShoppingCart;
import cz.cvut.eshop.shop.StandardItem;
import org.junit.*;
import org.junit.runners.MethodSorters;
import java.util.HashMap;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.TestCase.assertEquals;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PurchasesArchiveTest {
@Test
public void getHowManyTimesHasBeenItemSold_itemWasNotSoldBefore()
{
// setup
PurchasesArchive archive = new PurchasesArchive();
Item item = new StandardItem(1, "name", 3.14f,
"category", 12);
// act
int actual = archive.getHowManyTimesHasBeenItemSold(item);
// assert
assertEquals(0, actual);
}
@Test
public void getHowManyTimesHasBeenItemSold_itemWasSoldBefore_v1()
{
// setup
PurchasesArchive archive = new PurchasesArchive();
Item item = new StandardItem(1, "name", 3.14f,
"category", 12);
ShoppingCart cart = new ShoppingCart();
cart.addItem(item);
Order order = new Order(cart);
// act
archive.putOrderToPurchasesArchive(order);
int actual = archive.getHowManyTimesHasBeenItemSold(item);
// assert
assertEquals(1, actual);
}
@Test
public void getHowManyTimesHasBeenItemSold_itemWasSoldBefore_v2()
{
// setup
Item item = new StandardItem(1, "name", 3.14f,
"category", 12);
HashMap<Integer, ItemPurchaseArchiveEntry> itemPurchaseArchiveEntryHashMap =
new HashMap<Integer, ItemPurchaseArchiveEntry>();
itemPurchaseArchiveEntryHashMap.put(1,
new ItemPurchaseArchiveEntry(item));
PurchasesArchive archive = new PurchasesArchive(
itemPurchaseArchiveEntryHashMap,
null
);
// act
int actual = archive.getHowManyTimesHasBeenItemSold(item);
// assert
assertEquals(1, actual);
}
@Test(expected = java.lang.NullPointerException.class)
public void getHowManyTimesHasBeenItemSold_itemIsNull()
{
// setup
PurchasesArchive archive = new PurchasesArchive();
// act
int actual = archive.getHowManyTimesHasBeenItemSold(null);
// assert
}
@BeforeClass
public static void beforeAll()
{
System.out.println("before all");
}
@AfterClass
public static void afterAll()
{
System.out.println("after all");
}
@Before
public void before()
{
System.out.println("before");
}
@After
public void after()
{
System.out.println("after");
}
}
......@@ -2,6 +2,7 @@ package cz.cvut.eshop.shop;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import static org.junit.Assert.*;
......@@ -59,4 +60,73 @@ public class ShoppingCartTest {
assertEquals(0f, shCart.getTotalPrice(),0.05f);
}
@Test
public void constructorTest(){
//setup
ShoppingCart shopCart = new ShoppingCart();
//assert
assertNotNull(shopCart);
}
@Test
public void howManyItemsAreInTheCart_noItemSupposedToBe(){
//setup
ShoppingCart shopCart = new ShoppingCart();
ArrayList<Item> actual = shopCart.getCartItems();
assertEquals(0, actual.size());
//act
shopCart.getCartItems();
}
@Test
public void howManyItemsInTheCart_oneItemIsSupposedToBe(){
//setup
ShoppingCart shopCart = new ShoppingCart();
Item item = new StandardItem(1, "name", 3.14f,"category",10);
//act
shopCart.addItem(item);
int actual = shopCart.getItemsCount();
//assert
assertEquals(1, actual);
}
@Test
public void whatIsThePriceOfCart_20(){
//setup
ShoppingCart shopCart = new ShoppingCart();
Item item1 = new StandardItem(1, "name1", 1,"category",10);
Item item2 = new StandardItem(2, "name2", 2,"category",20);
Item item3 = new StandardItem(3, "name3", 17,"category",30);
//act
shopCart.addItem(item1);
shopCart.addItem(item2);
shopCart.addItem(item3);
float price = shopCart.getTotalPrice();
//assert
assertEquals(20,price, 0);
}
@Test
public void isItemRemoved_2(){
//setup
ShoppingCart shopCart = new ShoppingCart();
Item item1 = new StandardItem(1, "name1", 1,"category",10);
Item item2 = new StandardItem(2, "name2", 2,"category",20);
Item item3 = new StandardItem(3, "name3", 17,"category",30);
//act
shopCart.addItem(item1);
shopCart.addItem(item2);
shopCart.addItem(item3);
int numberOfItems1 = shopCart.getItemsCount();
//assert
assertEquals(3,numberOfItems1);
//act
//BYLO POTREBA UPRAVIT PUVODNI FUNKCI removeItem(), JINAK NEFUNGOVALA A HAZELA CHYBY
shopCart.removeItem(2);
//assert
int numberOfItems2 = shopCart.getItemsCount();
assertEquals(2,numberOfItems2);
}
}
\ No newline at end of file
package shop;
import cz.cvut.eshop.shop.Customer;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class CustomerTest {
Customer customer;
@Before
public void setUp() throws Exception {
customer = new Customer(500);
}
@Test
public void getLoyaltyPoints() {
assertEquals(500,customer.getLoyaltyPoints());
}
@Test
public void getCustomerName() {
assertEquals(null,customer.getCustomerName());
}
@Test
public void setCustomerAddress() {
customer.setCustomerName("Vaclavak");
assertEquals("Vaclavak",customer.getCustomerName());
}
@Test
public void setCustomerName() {
customer.setCustomerName("Pepa");
assertEquals("Pepa",customer.getCustomerName());
}
@Test
public void getCustomerAddress() {
assertEquals(null,customer.getCustomerAddress());
}
}
\ No newline at end of file
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;
public class DiscountedItemTest {
@Test
public void setDiscountFrom_correctDateFormat() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
String newDateString = "15.04.2005";
discountedItem.setDiscountFrom(newDateString);
Date date = discountedItem.getDiscountFrom();
assertEquals(15, date.getDate());
assertEquals(3, date.getMonth());
assertEquals(105, date.getYear());
}
@Test
public void setDiscountFrom_wrongDateFormat() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
String newDateString = "abc";
discountedItem.setDiscountFrom(newDateString);
Date date = discountedItem.getDiscountFrom();
assertEquals(10, date.getDate());
assertEquals(4, date.getMonth());
assertEquals(117, date.getYear());
}
@Test
public void testSetDiscountToCorrectDateFormatString() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
String newDateString = "15.04.2020";
discountedItem.setDiscountTo(newDateString);
Date date = discountedItem.getDiscountTo();
assertEquals(15, date.getDate());
assertEquals(3, date.getMonth());
assertEquals(120, date.getYear());
}
@Test
public void testSetDiscountToWrongDateFormatString() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
String newDateString = "abcdefs";
discountedItem.setDiscountTo(newDateString);
Date date = discountedItem.getDiscountTo();
assertEquals(10, date.getDate());
assertEquals(4, date.getMonth());
assertEquals(118, date.getYear());
}
//TODO: see Ignore
@Ignore("function getDiscountedPrice() in class DiscountedItem does not divide percents by 100" +
" so the price result of multiplication is incorrect. " +
"Possible change replacement of line 114 is: return super.getPrice()*((100 - discount)/100.0f);")
@Test
public void testGetDiscountedPriceZeroDiscount() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",0, "10.05.2017", "10.05.2018");
float value = discountedItem.getDiscountedPrice();
assertEquals(1.6f, value, 0.001f);
}
//TODO: see Ignore
@Ignore("function getDiscountedPrice() in class DiscountedItem does not divide percents by 100" +
" so the price result of multiplication is incorrect. " +
"Possible change replacement of line 114 is: return super.getPrice()*((100 - discount)/100.0f);")
@Test
public void testGetDiscountedPriceNozeroDiscount() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
float value = discountedItem.getDiscountedPrice();
assertEquals(0.64f, value, 0.001f);
}
@Test
public void testparseDateCorrectDateFormat() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "abcd", "10.05.2018");
Date date = discountedItem.getDiscountFrom();
assertNull(date);
}
@Test
public void testparseDateWrongDateFormat() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
Date date = discountedItem.getDiscountFrom();
assertEquals(10, date.getDate());
assertEquals(4, date.getMonth());
assertEquals(117, date.getYear());
}
@Test
public void testSetDiscountToCorrectDateFormatDate() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
Date newDate = new Date(120,3,15);
discountedItem.setDiscountTo(newDate);
Date date = discountedItem.getDiscountTo();
assertEquals(15, date.getDate());
assertEquals(3, date.getMonth());
assertEquals(120, date.getYear());
}
@Test
public void testSetDiscountToNullDateFormatDate() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
Date newDate = null;
discountedItem.setDiscountTo(newDate);
Date date = discountedItem.getDiscountTo();
assertNull(date);
}
//TODO: see Ignore
@Ignore("function getDiscountedPrice() in class DiscountedItem does not divide percents by 100" +
" so the price result of multiplication is incorrect. " +
"Possible change replacement of line 114 is: return super.getPrice()*((100 - discount)/100.0f);")
@Test
public void testGetDiscountedPriceReflectsChangeOfDiscount() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
int newDiscount = 40;
discountedItem.setDiscount(newDiscount);
int actual = discountedItem.getDiscount();
float value = discountedItem.getDiscountedPrice();
assertEquals(0.96f, value, 0.001f);
assertEquals(newDiscount, actual);
}
@Test
public void testGetOriginalPricePriceIsCorrect() {
DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018");
float actual = discountedItem.getOriginalPrice();
assertEquals(1.6f, actual, 0.001f);
}
}
package shop;
import cz.cvut.eshop.shop.Customer;
import cz.cvut.eshop.shop.Order;
import cz.cvut.eshop.shop.ShoppingCart;
import cz.cvut.eshop.shop.StandardItem;
import cz.cvut.eshop.storage.NoItemInStorage;
import org.junit.Ignore;
import org.junit.Test;
import static cz.cvut.eshop.shop.EShopController.purchaseShoppingCart;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
public class OrderTest {
@Test
public void testCreateOrderWithNoItemsOrderedTest() {
// setup
ShoppingCart cart = new ShoppingCart();
Order order = new Order(cart);
// act
order.create();
int numberOfItems = order.getItems().size();
// assert
assertEquals(numberOfItems,0);
}
@Test
public void testOneItemAndDeleted() {
// setup
ShoppingCart cart = new ShoppingCart();
int num1 = 2122;
float num2 = 5411210;
int num3 = 4324;
String str1 = "PC";
String str2 = "PCs";
cart.addItem(new StandardItem(num1,str1,num2,str2,num3));
Order order = new Order(cart, 0);
order.getItems().remove(0);
// act
// assert
assertEquals(0, order.getItems().size());
}
@Test
public void testOneItemInserted() {
// setup
ShoppingCart cart = new ShoppingCart();
cart.addItem(new StandardItem(79987, "Generic laptop", 20000, "laptops", 200));
Order order = new Order(cart, 0);
order.create();
// act
// assert
assertEquals(1, order.getItems().size());
}
@Test
public void testMultipleItemsInserted() {
// setup
ShoppingCart cart = new ShoppingCart();
Order order = new Order(cart, 0);
order.create();
int numberOfItems = 100;
//act
for (int i = 0; i < numberOfItems*2; i = i + 2) {
int num1 = i;
int num2 = i + 1;
float num3 = (float) (num1 + num2 + 0.5);
String str1 = (char) num1 + "";
String str2 = (char) num1 + "";
cart.addItem(new StandardItem(num1,str1,num3,str2,num2));
}
assertEquals(numberOfItems, order.getItems().size());
}
//TODO: see Ignore
@Ignore("method order.getTotalAmount() has variable int totalAmount (line 91) where floats are added, variable totalAmount must use type float")
@Test
public void testGetTotalAmount() {
// setup
ShoppingCart cart = new ShoppingCart();
Order order = new Order(cart, 0);
order.create();
int numberOfItems = 100;
float price = 0;
//act
for (int i = 0; i < numberOfItems*2; i = i + 2) {
int num1 = i;
int num2 = i + 1;
float num3 = (float) (num1 + num2 + 0.31315);
price += num3;
String str1 = (char) num1 + "";
String str2 = (char) num1 + "";
cart.addItem(new StandardItem(num1,str1,num3,str2,num2));
}
assertEquals(price, order.getTotalAmount());
}
//TODO: see Ignore
@Ignore("applyDiscount can't work properly if getTotalAmount() does not work properly")
@Test
public void testApplyDiscount() {
// setup
ShoppingCart cart = new ShoppingCart();
Order order = new Order(cart, 0);
Customer cust = new Customer(20);
order.setCustomer(cust);
int discountFromItems = 0;
float price = 0;
//act
for (int i = 0; i < 10; i++) {
int num1 = i;
int num2 = i + 1;
float num3 = (float) (num1 + num2 + 0.31315);
price += num3;
discountFromItems += num2;
String str1 = (char) num1 + "";
String str2 = (char) num1 + "";
cart.addItem(new StandardItem(num1,str1,num3,str2,num2));
}
order.create();
float disc = (float) (20 * 0.2);
float disc2 = (float) (discountFromItems * 0.2);
price -= disc;
price -= disc2;
order.applyDiscount();
assertEquals(price,order.getTotalAmount());
}
}
package shop;
import cz.cvut.eshop.shop.DiscountedItem;
import cz.cvut.eshop.shop.Item;
import cz.cvut.eshop.shop.ShoppingCart;
import cz.cvut.eshop.shop.StandardItem;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ShoppingCartTest {
@Test
public void testRemoveItemItemDoesNotExist() {
ShoppingCart shoppingCart = new ShoppingCart();
Item item1 = new StandardItem(1, "name", 0.28f, "category", 1);
Item item2 = new DiscountedItem(2, "name2", 2.22f, "category2", 50, new Date(1996,12,20), new Date(2020,11,15));
shoppingCart.addItem(item1);
shoppingCart.addItem(item2);
shoppingCart.removeItem(5);
int items = shoppingCart.getItemsCount();
assertEquals(2, items);
}
//TODO: see Ignore
@Ignore("class ShoppingCart where both for-cycles (line 45 and 66) contains wrong stopping condition. Instead of \"i<=0\" should be \"i>=0\"")
@Test
public void testRemoveItemItemExists() {
ShoppingCart shoppingCart = new ShoppingCart();
Item item = new StandardItem(1, "name", 0.28f, "category", 1);
shoppingCart.addItem(item);
shoppingCart.removeItem(1);
int items = shoppingCart.getItemsCount();
assertEquals(0, items);
}
//TODO: see Ignore
@Ignore("function getTotalPrice() in ShoppingCart should probably return float value instead of integer as all of the item prices are float values")
@Test
public void testGetTotalPriceStandardItem() {
Item item1 = new StandardItem(2, "name1", 1.28f, "category1", 1);
Item item2 = new StandardItem(1, "name2", 2.01f, "category2", 2);