Skip to content
Snippets Groups Projects
Commit f3c58293 authored by Righi Andrea (Student Com17)'s avatar Righi Andrea (Student Com17)
Browse files

finished unit test class

parent 9dc27f55
No related branches found
Tags v1.0
No related merge requests found
Showing
with 22331 additions and 74 deletions
......@@ -8,8 +8,8 @@ public class Auction {
static int idCounter;
public static ArrayList<Auction> auctions = new ArrayList<Auction>();
private ArrayList<User> subscribedUsers;
private ArrayList<Bid> bids;
private ArrayList<User> subscribedUsers = new ArrayList<User>();
private ArrayList<Bid> bids = new ArrayList<Bid>();
private int id;
private Item item;
private Date start, end;
......@@ -40,7 +40,6 @@ public class Auction {
public static Auction getInstance() {
return new Auction();
}
......@@ -128,9 +127,9 @@ public class Auction {
public void setEnd(Date endDate) {
this.end = endDate;
}
public ArrayList<Bid> getBids() {
return bids;
}
......
package project;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import exceptions.BidBelowZeroException;
......@@ -31,23 +32,26 @@ public class AuctionManager {
}
public boolean placeBid(User user, int value) throws BidBelowZeroException, ParseException {
long millis = System.currentTimeMillis();
Timestamp t = new Timestamp(millis);
DateFormat df = DateFormat.getInstance();
Date d = df.parse((t.getTime() + ""));
LocalDateTime l = LocalDateTime.now();
Date d = AuctionManager.parseDate(l.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")));
if (value < 1) {
throw new BidBelowZeroException();
} else if (value > 1 && checkTime(d)) {
} else if (checkTime(d)) {
Bid b = new Bid(d, value, user, auction);
computePrice(b);
auction.getBids().add(b);
ArrayList<Bid> toSet = auction.getBids();
toSet.add(b);
auction.setBids(toSet);
auction.setCurrentLeader(user);
return true;
}
......@@ -55,26 +59,36 @@ public class AuctionManager {
return false;
}
public boolean checkTime(Date d) {
// Checks for existing bids with the same time value
for(Bid b : auction.getBids()){
if(d.equals(b.getDate())){
ArrayList<Bid> toCheck = auction.getBids();
for (Bid b : toCheck) {
if (d.equals(b.getDate())) {
return false;
}
}
if (d.before(auction.getEnd())) {
return true;
}
else return false;
return false;
}
public void computePrice(Bid bid) {
auction.setPrice(bid.getValue() + auction.getPrice());
auction.setPrice(bid.getValue() + auction.getPrice());
}
public static Date parseDate(String value) {
......
......@@ -4,7 +4,7 @@ import java.util.Date;
public class Bid {
private Date date;
private Date date = new Date();
private int value;
private User user;
private Auction auction;
......
......@@ -39,19 +39,4 @@ public class Category {
return null;
}
public static Category getCategory(int catId) {
for (Category c : categories) {
if (c.categoryId == catId) {
return c;
}
}
return null;
}
}
package fitNesseFixture;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import exceptions.BidBelowZeroException;
import project.Auction;
import project.AuctionManager;
import project.User;
......@@ -20,7 +23,14 @@ public class AuctionManagerFixture {
public String placeBid() {
Auction a = new Auction();
LocalDateTime l = LocalDateTime.of(2020, 12, 25, 12, 0, 0);
a.setEnd(AuctionManager.parseDate(l.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"))));
managerInstance = AuctionManager.getInstance();
managerInstance.setAuction(a);
try {
......@@ -35,7 +45,7 @@ public class AuctionManagerFixture {
return e.getMessage();
} catch (ParseException e) {
return e.getMessage();
}
......
......@@ -4,6 +4,7 @@ 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.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
......@@ -18,6 +19,8 @@ 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.BidBelowZeroException;
import exceptions.ImageInvalidException;
......@@ -47,37 +50,45 @@ public class AuctionManagerUnitTest {
AuctionManager am = AuctionManager.getInstance();
User user = User.getInstance();
Auction a = Auction.getInstance();
Date endDate = AuctionManager.parseDate("3/12/2019 10:00:00");
Date endDate = AuctionManager.parseDate("03/12/2019 10:00:00");
a.setEnd(endDate);
am.setAuction(a);
ArrayList<Bid> bidList = new ArrayList<Bid>();
for (int x = 0; x < 10; x++) {
Date d = AuctionManager.parseDate("3/12/2059 " + x + ":00:00");
Date d = AuctionManager.parseDate("03/12/2059 " + x + ":00:00");
Bid b = new Bid(d, 10, user, a);
bidList.add(b);
}
a.setBids(bidList);
a.setBids(bidList);
ArrayList<Date> rightList = new ArrayList<Date>();
for (int x = 0; x < 10; x++) {
Date d = AuctionManager.parseDate("3/12/2019 " + x + ":00:00");
String dateToParse = "03/12/2019 0" + x + ":00:00";
Date d = AuctionManager.parseDate(dateToParse);
rightList.add(d);
}
ArrayList<Date> wrongList = new ArrayList<Date>();
for (int x = 10; x < 20; x++) {
Date d = AuctionManager.parseDate("3/12/2019 " + x + ":00:00");
String dateToParse = "03/12/2019 " + x + ":00:00";
Date d = AuctionManager.parseDate(dateToParse);
wrongList.add(d);
}
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
for (int e = 0; e < rightList.size(); e++) {
Date toRemove = rightList.get(e);
Executable exec = () -> assertTrue(am.checkTime(toRemove));
DynamicTest dTest = DynamicTest.dynamicTest("Before end date", exec);
......@@ -99,66 +110,79 @@ public class AuctionManagerUnitTest {
@Test
public void checkTimeTwoBidsTest() {
Date commonDate = AuctionManager.parseDate("3/12/2019 10:00:00");
Date commonDate = AuctionManager.parseDate("03/12/2019 10:00:00");
User user = User.getInstance();
AuctionManager auctionManager = AuctionManager.getInstance();
Auction auction = Auction.getInstance();
auctionManager.setAuction(auction);
Bid a = new Bid(commonDate, 10, user, auction);
auction.getBids().add(a);
ArrayList<Bid> toSet = auction.getBids();
toSet.add(a);
auction.setBids(toSet);
Bid b = new Bid(commonDate, 10, user, auction);
assertFalse(auctionManager.checkTime(b.getDate()));
}
@TestFactory
public Collection<DynamicTest> placeBidTest() {
@ParameterizedTest
@ValueSource(ints = { -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 })
public void placeBidTestOpenAuction(int toCheck) throws BidBelowZeroException, ParseException {
AuctionManager auctionManager = AuctionManager.getInstance();
Auction auction = Auction.getInstance();
User user = User.getInstance();
Date rightEndDate = AuctionManager.parseDate("3/12/2029 10:00:00");
Date rightEndDate = AuctionManager.parseDate("03/12/2021 10:00:00");
// Sets the auction related to the AuctionManager with a valid date
auction.setEnd(rightEndDate);
auctionManager.setAuction(auction);
ArrayList<Integer> values = new ArrayList<Integer>();
if (toCheck < 1) {
assertThrows(BidBelowZeroException.class, () -> auctionManager.placeBid(user, toCheck));
} else {
assertTrue(auctionManager.placeBid(user, toCheck));
for (int e = -4; e < 6; e++) {
values.add(e);
}
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
}
for (int e = -4; e < 6; e++) {
@TestFactory
public Collection<DynamicTest> placeBidTestEndedAuction() {
DynamicTest dTest;
int toCheck = values.get(e + 4);
AuctionManager auctionManager = AuctionManager.getInstance();
Auction auction = Auction.getInstance();
User user = User.getInstance();
if (toCheck < 1) {
Executable exec = () -> assertThrows(BidBelowZeroException.class,
() -> auctionManager.placeBid(user, toCheck));
dTest = DynamicTest.dynamicTest("Value below zero", exec);
} else {
Executable exec = () -> assertTrue(auctionManager.placeBid(user, toCheck));
dTest = DynamicTest.dynamicTest("Value over zero", exec);
}
// Sets the auction related to the AuctionManager with a valid date
auctionManager.setAuction(auction);
ArrayList<Integer> values = new ArrayList<Integer>();
for (int e = -4; e < 6; e++) {
values.add(e);
dynamicTests.add(dTest);
}
Collection<DynamicTest> dynamicTests = new ArrayList<DynamicTest>();
// Change the date of the auction managed by the manager with a wrong date and
// re-run the test
Date wrongEndDate = AuctionManager.parseDate("3/12/2008 10:00:00");
Date wrongEndDate = AuctionManager.parseDate("03/12/2008 10:00:00");
auction.setEnd(wrongEndDate);
for (int e = -4; e < 6; e++) {
for (int e = 0; e < 10; e++) {
DynamicTest dTest;
int toCheck = values.get(e + 4);
int toCheck = values.get(e);
if (toCheck < 1) {
Executable exec = () -> assertThrows(BidBelowZeroException.class,
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment