Commit b1216afd authored by Tomáš Hamsa's avatar Tomáš Hamsa
Browse files

Added tests for package 1 - archive

parent 2e59c4d0
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");
}
}
package cz.cvut.eshop;
import java.util.ArrayList;
import java.util.Arrays;
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;
public class TestData {
private TestData() throws IllegalAccessException {
throw new IllegalAccessException("Single instance protection");
}
public static StandardItem createStandardItem() {
return new StandardItem(1, "testItem", 3.5f, "testCategory", 10);
}
public static PurchasesArchive createEmptyPurchaseArchive() {
return new PurchasesArchive();
}
public static Order createOrder(Item... items) {
ShoppingCart shoppingCart;
if (items == null) {
shoppingCart = new ShoppingCart();
} else {
ArrayList<Item> cartItems = new ArrayList<>(Arrays.asList(items));
shoppingCart = new ShoppingCart(cartItems);
}
return new Order(shoppingCart);
}
}
\ No newline at end of file
package cz.cvut.eshop.archive;
import org.junit.Before;
import cz.cvut.eshop.TestData;
import cz.cvut.eshop.shop.Item;
abstract class AbstractArchiveTest {
Item item;
ItemPurchaseArchiveEntry itemArchive;
@Before
public void setUp() {
item = TestData.createStandardItem();
itemArchive = new ItemPurchaseArchiveEntry(item);
}
}
package cz.cvut.eshop.archive;
import org.junit.Assert;
import org.junit.Test;
public class ItemPurchaseArchiveEntryTest extends AbstractArchiveTest {
@Test
public void testGetCountHowManyTimesHasBeenSold() {
Assert.assertEquals("Incorrect sold count in empty storage", 1, itemArchive.getCountHowManyTimesHasBeenSold());
}
@Test
public void testGetRefItem() {
Assert.assertEquals("Constructor and getter items are different", item, itemArchive.getRefItem());
}
@Test
public void testIncreaseCountHowManyTimesHasBeenSoldZero() {
compareIncrement(0);
}
@Test
public void testIncreaseCountHowManyTimesHasBeenSoldPositive() {
compareIncrement(10);
}
@Test
public void testIncreaseCountHowManyTimesHasBeenSoldNegative() {
compareIncrement(-10);
}
private void compareIncrement(int difference) {
int soldCount = itemArchive.getCountHowManyTimesHasBeenSold();
itemArchive.increaseCountHowManyTimesHasBeenSold(difference);
Assert.assertEquals("Different sold count for " + difference + " increment", soldCount + difference, itemArchive.getCountHowManyTimesHasBeenSold());
}
}
\ No newline at end of file
package cz.cvut.eshop.archive;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import cz.cvut.eshop.TestData;
import cz.cvut.eshop.shop.Order;
public class PurchasesArchiveTest extends AbstractArchiveTest {
private PurchasesArchive emptyArchive;
private PurchasesArchive notEmptyArchive;
private Order order;
@Override
@Before
public void setUp() {
super.setUp();
emptyArchive = TestData.createEmptyPurchaseArchive();
order = TestData.createOrder(item);
HashMap<Integer, ItemPurchaseArchiveEntry> items = new HashMap<>(Collections.singletonMap(item.getID(), itemArchive));
notEmptyArchive = new PurchasesArchive(items, new ArrayList<>(1));
}
@Test
public void testPrintItemPurchaseStatistics() throws IOException {
PrintStream originalStdout = System.out;
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PrintStream newStdout = new PrintStream(new BufferedOutputStream(buffer), true)
) {
System.setOut(newStdout);
emptyArchive.printItemPurchaseStatistics();
Assert.assertTrue("Empty archive purchase statistic", buffer.size() > 0);
} finally {
System.setOut(originalStdout);
}
}
@Test(expected = NullPointerException.class)
public void testGetHowManyTimesHasBeenItemSoldNull() {
emptyArchive.getHowManyTimesHasBeenItemSold(null);
}
@Test
public void testGetHowManyTimesHasBeenItemSoldEmpty() {
int soldCount = emptyArchive.getHowManyTimesHasBeenItemSold(item);
Assert.assertEquals("Not present item sold multiple times", 0, soldCount);
}
@Test
public void testGetHowManyTimesHasBeenItemSold() {
int soldCount = notEmptyArchive.getHowManyTimesHasBeenItemSold(item);
Assert.assertEquals("Item sold incorrect number of times", itemArchive.getCountHowManyTimesHasBeenSold(), soldCount);
}
@Test(expected = NullPointerException.class)
public void testPutOrderToPurchasesArchiveNull() {
TestData.createOrder();
emptyArchive.putOrderToPurchasesArchive(null);
}
@Test
public void testPutOrderToPurchasesArchiveEmpty() {
emptyArchive.putOrderToPurchasesArchive(order);
int soldCount = emptyArchive.getHowManyTimesHasBeenItemSold(item);
Assert.assertEquals("Item sold multiple times in empty archive", 1, soldCount);
}
@Test
public void testPutOrderToPurchasesArchive() {
int previousSoldCount = notEmptyArchive.getHowManyTimesHasBeenItemSold(item);
notEmptyArchive.putOrderToPurchasesArchive(order);
int newSoldCount = notEmptyArchive.getHowManyTimesHasBeenItemSold(item);
Assert.assertEquals("Item sold incorrect number of times", previousSoldCount + 1, newSoldCount);
}
}
\ No newline at end of file
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