Skip to content
Snippets Groups Projects
Commit a38909c9 authored by cristiano's avatar cristiano
Browse files

Item unit test + ItemPicture unit test

parent f597ea17
No related branches found
No related tags found
No related merge requests found
......@@ -38,11 +38,11 @@
## Item
| Method | Static Test | Dynamic Test | Parameterized test |
| :--------- | ----------- | -------------------------------------------------- | ----------------------------------- |
| delete | | random ID (error if they do not exist) | |
| modify | | random ID and strings (error if they do not exist) | empty case, wrong format |
| addPicture | | | 0 < images <= 10 and item existence |
| Method | Static Test | Dynamic Test | Parameterized test |
| :--------- | ----------- | -------------------------------------------------- | ------------------------ |
| delete | | random ID (error if they do not exist) | |
| modify | | random ID and strings (error if they do not exist) | empty case, wrong format |
| addPicture | | | 0 < images <= 10 |
......
......@@ -184,4 +184,8 @@ public class Item {
}
public ArrayList<ItemPicture> getImages() {
return images;
}
}
......@@ -16,6 +16,11 @@ public class ItemPicture {
this.id = idCounter++;
}
public ItemPicture(int id) {
pictures.add(this);
this.id = id;
}
public void setId(int id) {
this.id = id;
......@@ -24,17 +29,17 @@ public class ItemPicture {
public int getId() {
return id;
}
/*
public boolean remove(int i) {
public static boolean remove(int i, Item item) {
if (item.getImage(i) != null) {
int index = item.images.indexOf(item.getImage(i));
item.images.remove(index);
int index = item.getImages().indexOf(item.getImage(i));
item.getImages().remove(index);
return true;
} else
return false;
}
*/
public static ItemPicture getItemPicture(int itemPicture) {
for (ItemPicture ip : pictures) {
......@@ -55,4 +60,9 @@ public class ItemPicture {
this.item = item;
}
public static ItemPicture getInstance() {
return new ItemPicture();
}
}
package unitTest;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingConsumer;
import exceptions.ImageInvalidException;
import project.Auction;
import project.Category;
import project.Item;
import project.ItemPicture;
import project.User;
public class ItemPictureUnitTest {
/*@TestFactory
public Stream<DynamicTest> ItemPictureUnitTest() {
ItemPicture a = ItemPicture.getInstance();
Iterator<Integer> inputGenerator = new Iterator<Integer>() {
int picId;
Random random = new Random();
@Override
public boolean hasNext() {
return random.nextInt(100) % 10 != 0;
}
@Override
public Integer next() {
@BeforeEach
public void reset() {
picId = (int) (random.nextDouble() * 10);
Item.items = new ArrayList<Item>();
User.users = new ArrayList<User>();
Auction.auctions = new ArrayList<Auction>();
Category.categories = new ArrayList<Category>();
return picId;
}
};
}
Function<Integer, String> displayNameGenerator = (input) -> "input: " + input;
@TestFactory
public Collection<DynamicTest> ItemPictureUnitTest() throws ImageInvalidException {
ThrowingConsumer<Integer> testExecutor = (input) -> assertTrue(a.remove(input));
Item i = Item.getInstance();
ArrayList<ItemPicture> rightList = new ArrayList<ItemPicture>();
for(int x = 0; x < 10; x++) {
rightList.add(new ItemPicture(x));
}
i.addPicture(rightList);
ArrayList<ItemPicture> wrongList = new ArrayList<ItemPicture>();
for(int x = 10; x < 20; x++) {
wrongList.add(new ItemPicture(x));
}
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
for(int e = 0; e < rightList.size(); e++) {
ItemPicture toRemove = rightList.get(e);
Executable exec = () -> assertTrue(ItemPicture.remove(toRemove.getId(), i));
DynamicTest dTest = DynamicTest.dynamicTest("Existing images", exec);
dynamicTests.add(dTest);
}
for(int e = 0; e < wrongList.size(); e++) {
ItemPicture toRemove = wrongList.get(e);
Executable exec = () -> assertFalse(ItemPicture.remove(toRemove.getId(), i));
DynamicTest dTest = DynamicTest.dynamicTest("Not existen images", exec);
dynamicTests.add(dTest);
}
return dynamicTests;
}
*/}
}
......@@ -12,19 +12,37 @@ import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingConsumer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import exceptions.CategoryInvalidException;
import exceptions.DescriptionInvalidException;
import exceptions.ImageInvalidException;
import exceptions.PriceInvalidException;
import exceptions.TitleInvalidException;
import project.Auction;
import project.Category;
import project.Item;
import project.ItemPicture;
import project.User;
public class ItemUnitTest {
@BeforeEach
public void reset() {
Item.items = new ArrayList<Item>();
User.users = new ArrayList<User>();
Auction.auctions = new ArrayList<Auction>();
Category.categories = new ArrayList<Category>();
}
@TestFactory
public Stream<DynamicTest> deleteTestTrue() {
......@@ -33,7 +51,8 @@ public class ItemUnitTest {
int itemId;
Random random = new Random();
@Override
public boolean hasNext() {
......@@ -107,18 +126,18 @@ public class ItemUnitTest {
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"
+ "aaaaaaaaaaaaaaaa"));
ArrayList<String> category = new ArrayList<String>(
Arrays.asList("bike"));
ArrayList<String> category = new ArrayList<String>(Arrays.asList("bike"));
ArrayList<String> titleTestNames = new ArrayList<String>(Arrays.asList("title empty", "numerical title",
"symbols present in the title", "title longer than 30 characters", "cateogry not correct"));
ArrayList<String> descriptionTestNames = new ArrayList<String>(Arrays.asList("description empty", "numerical description",
"title longer than 500 characters"));
ArrayList<String> descriptionTestNames = new ArrayList<String>(
Arrays.asList("description empty", "numerical description", "title longer than 500 characters"));
ArrayList<String> categoryTestNames = new ArrayList<String>(Arrays.asList("cateogry not correct"));
ArrayList<Class<? extends Exception>> titleExceptions = new ArrayList<Class<? extends Exception>>(
Arrays.asList(TitleInvalidException.class, TitleInvalidException.class, TitleInvalidException.class,
TitleInvalidException.class));
ArrayList<Class<? extends Exception>> descriptionExceptions = new ArrayList<Class<? extends Exception>>(
Arrays.asList(DescriptionInvalidException.class, DescriptionInvalidException.class, DescriptionInvalidException.class));
Arrays.asList(DescriptionInvalidException.class, DescriptionInvalidException.class,
DescriptionInvalidException.class));
ArrayList<Class<? extends Exception>> categoryExceptions = new ArrayList<Class<? extends Exception>>(
Arrays.asList(CategoryInvalidException.class));
......@@ -165,8 +184,42 @@ public class ItemUnitTest {
}
Executable exec = () -> assertTrue(item.modify("title", "description", "camera"));
DynamicTest dTest = DynamicTest.dynamicTest("Working test", exec);
dynamicTests.add(dTest);
return dynamicTests;
}
@ParameterizedTest
@ValueSource(ints = { -1, 20 })
public void addPictureOutOfBoundTest(int arrayLength) {
ArrayList<ItemPicture> list = new ArrayList<ItemPicture>();
for (int e = 0; e < arrayLength; e++) {
list.add(new ItemPicture());
}
Item i = Item.getInstance();
assertThrows(ImageInvalidException.class, () -> i.addPicture(list));
}
@Test
public void addPictureWorkingTest() throws ImageInvalidException {
ArrayList<ItemPicture> list = new ArrayList<ItemPicture>();
int value = 5;
for (int e = 0; e < value; e++) {
list.add(new ItemPicture());
}
Item i = Item.getInstance();
assertTrue(i.addPicture(list));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment