Merge branch 'main'
This commit is contained in:
79
src/test/java/oving7/abstractaccount/CreditAccountTest.java
Normal file
79
src/test/java/oving7/abstractaccount/CreditAccountTest.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package oving7.abstractaccount;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CreditAccountTest {
|
||||
|
||||
private static final double epsilon = 0.0005;
|
||||
|
||||
private CreditAccount sub;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
sub = new CreditAccount(10_000.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that deposits work as expected")
|
||||
public void testDeposit() {
|
||||
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
sub.deposit(10_000.0);
|
||||
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.deposit(-10_000.0);
|
||||
}, "Negative deposit should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that withdrawals work as expected")
|
||||
public void testWithdraw() {
|
||||
sub.deposit(20_000.0);
|
||||
sub.withdraw(5000.0);
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(-10_000.0);
|
||||
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
sub.withdraw(20_000.0);
|
||||
assertEquals(-5000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(20_000.0);
|
||||
}, "Withdrawal exceeding the credit limit should have triggered an IllegalArgumentException");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the credit limit works as expected")
|
||||
public void testCreditLine() {
|
||||
assertEquals(10_000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
||||
|
||||
sub.setCreditLine(5000.0);
|
||||
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.setCreditLine(-5000.0);
|
||||
}, "Cannot have a negative credit limit");
|
||||
|
||||
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
||||
|
||||
sub.withdraw(4000.0);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
sub.setCreditLine(3000.0);
|
||||
}, "Cannot set a credit limit that would result in an invalid balance");
|
||||
|
||||
assertEquals(-4000.0, sub.getBalance(), epsilon, "The balance was incorrect");
|
||||
assertEquals(5000.0, sub.getCreditLine(), epsilon, "The credit limit was incorrect");
|
||||
}
|
||||
}
|
||||
52
src/test/java/oving7/abstractaccount/DebitAccountTest.java
Normal file
52
src/test/java/oving7/abstractaccount/DebitAccountTest.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package oving7.abstractaccount;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DebitAccountTest {
|
||||
|
||||
private static final double epsilon = 0.0005;
|
||||
|
||||
private DebitAccount sub;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
sub = new DebitAccount();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that deposits work as expected")
|
||||
public void testDeposit() {
|
||||
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
sub.deposit(10_000.0);
|
||||
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.deposit(-10_000.0);
|
||||
}, "Negative deposit should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that withdrawals work as expected")
|
||||
public void testWithdraw() {
|
||||
sub.deposit(20_000.0);
|
||||
sub.withdraw(5000.0);
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(-10_000.0);
|
||||
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(20_000.0);
|
||||
}, "Withdrawal exceeding the balance should have triggered an IllegalArgumentException");
|
||||
}
|
||||
}
|
||||
65
src/test/java/oving7/abstractaccount/SavingsAccountTest.java
Normal file
65
src/test/java/oving7/abstractaccount/SavingsAccountTest.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package oving7.abstractaccount;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SavingsAccountTest {
|
||||
|
||||
private static final double epsilon = 0.0005;
|
||||
|
||||
private SavingsAccount sub;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
sub = new SavingsAccount(1, 50.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that deposits work as expected")
|
||||
public void testDeposit() {
|
||||
assertEquals(0.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
sub.deposit(10_000.0);
|
||||
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.deposit(-10_000.0);
|
||||
}, "Negative deposit should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(10_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that withdrawals work as expected")
|
||||
public void testWithdraw() {
|
||||
sub.deposit(20_000.0);
|
||||
sub.withdraw(5000.0);
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(-10_000.0);
|
||||
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(20_000.0);
|
||||
}, "Withdrawal exceeding the balance should have triggered an IllegalArgumentException");
|
||||
|
||||
assertEquals(15_000.0, sub.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
sub.withdraw(10_000.0);
|
||||
assertEquals(4950.0, sub.getBalance(), epsilon,
|
||||
"The account balance was incorrect after the fee was deducted");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
sub.withdraw(4930.0);
|
||||
}, "Withdrawal exceeding the balance + fee should have triggered an IllegalArgumentException");
|
||||
|
||||
assertEquals(4950.0, sub.getBalance(), epsilon,
|
||||
"The account balance was incorrect after the fee was deducted");
|
||||
}
|
||||
}
|
||||
87
src/test/java/oving7/card/CardDeckTest.java
Normal file
87
src/test/java/oving7/card/CardDeckTest.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package oving7.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardDeckTest {
|
||||
|
||||
private Card s1;
|
||||
private Card s2;
|
||||
private Card h1;
|
||||
private Card h2;
|
||||
private Card d1;
|
||||
private Card d2;
|
||||
private Card c1;
|
||||
private Card c2;
|
||||
private CardDeck deck;
|
||||
private List<Card> expected;
|
||||
|
||||
private static void testCards(CardContainer it, List<Card> expected) {
|
||||
assertEquals(expected.size(), it.getCardCount());
|
||||
|
||||
for (int i = 0; i < expected.size(); i++) {
|
||||
Card expectedCard = expected.get(i);
|
||||
Card actualCard = it.getCard(i);
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void testCards(Iterator<Card> actual, Iterator<Card> expected) {
|
||||
while (expected.hasNext()) {
|
||||
assertTrue(actual.hasNext());
|
||||
|
||||
Card expectedCard = expected.next();
|
||||
Card actualCard = actual.next();
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
deck = new CardDeck(2);
|
||||
s1 = new Card('S', 1);
|
||||
s2 = new Card('S', 2);
|
||||
h1 = new Card('H', 1);
|
||||
h2 = new Card('H', 2);
|
||||
d1 = new Card('D', 1);
|
||||
d2 = new Card('D', 2);
|
||||
c1 = new Card('C', 1);
|
||||
c2 = new Card('C', 2);
|
||||
expected = new LinkedList<>(List.of(s1, s2, h1, h2, d1, d2, c1, c2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test maxCardCount")
|
||||
public void testMaxCardCount() {
|
||||
assertTrue(deck instanceof CardContainerImpl);
|
||||
assertEquals(52, deck.getMaxCardCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that CardDeckImpl implements CardContainer")
|
||||
public void testCardContainer() {
|
||||
CardDeckTest.testCards(deck, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that CardDeckImpl implements Iterable")
|
||||
public void testDeckIterator() {
|
||||
CardDeckTest.testCards(deck.iterator(), expected.iterator());
|
||||
}
|
||||
}
|
||||
82
src/test/java/oving7/card/CardHandTest.java
Normal file
82
src/test/java/oving7/card/CardHandTest.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package oving7.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardHandTest {
|
||||
|
||||
private Card c2;
|
||||
private Card s1;
|
||||
private CardHand hand;
|
||||
private List<Card> expected;
|
||||
|
||||
private static void testCards(CardContainer it, List<Card> expected) {
|
||||
assertEquals(expected.size(), it.getCardCount());
|
||||
|
||||
for (int i = 0; i < expected.size(); i++) {
|
||||
Card expectedCard = expected.get(i);
|
||||
Card actualCard = it.getCard(i);
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void testCards(Iterator<Card> actual, Iterator<Card> expected) {
|
||||
while (expected.hasNext()) {
|
||||
assertTrue(actual.hasNext());
|
||||
|
||||
Card expectedCard = expected.next();
|
||||
Card actualCard = actual.next();
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
hand = new CardHand(2);
|
||||
s1 = new Card('S', 1);
|
||||
c2 = new Card('C', 2);
|
||||
expected = List.of(s1, c2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test cardCount")
|
||||
public void testCardCount() {
|
||||
assertTrue(hand instanceof CardContainerImpl);
|
||||
assertEquals(0, hand.getCardCount(), "CardCount should have been 0 at the start");
|
||||
|
||||
hand.addCard(new Card('S', 1));
|
||||
hand.addCard(new Card('S', 2));
|
||||
assertEquals(2, hand.getCardCount(), "CardCount should have been 2 at the start");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that cardDeckImpl implements cardContainer")
|
||||
public void testCardContainer() {
|
||||
hand.addCard(new Card('S', 1));
|
||||
hand.addCard(new Card('C', 2));
|
||||
CardHandTest.testCards(hand, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that cardDeckImpl implements iterable")
|
||||
public void testDeckIterator() {
|
||||
hand.addCard(new Card('S', 1));
|
||||
hand.addCard(new Card('C', 2));
|
||||
CardHandTest.testCards(hand.iterator(), expected.iterator());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package oving7.observablelist;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ObservableHighscoreListTest {
|
||||
|
||||
private int pos1;
|
||||
private int pos2;
|
||||
private ObservableHighscoreList highscoreList;
|
||||
|
||||
private static void checkHighscoreList(String contextMessage, ObservableHighscoreList list,
|
||||
List<Integer> elements) {
|
||||
assertEquals(elements.size(), list.size(),
|
||||
contextMessage + " -> Testing the length of the highscore list");
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (int element : elements) {
|
||||
assertEquals(element, list.getElement(i),
|
||||
contextMessage + " -> Testing that the element at position " + i + " matches");
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private void addResultWithListener(int pos, int element) {
|
||||
pos1 = pos;
|
||||
highscoreList.addResult(element);
|
||||
|
||||
// Check that the position that was changed is the same as the one sent to the listener
|
||||
assertEquals(pos1, pos2, "Added " + element + " at position " + pos
|
||||
+ " -> Testing the position received by the listener");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
highscoreList = new ObservableHighscoreList(3);
|
||||
pos1 = -1;
|
||||
pos2 = -1;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test constructor")
|
||||
public void testConstructor() {
|
||||
assertEquals(0, highscoreList.size(), "Testing initialization of the highscore list");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Add results (simple)")
|
||||
public void testAddElementSimple() {
|
||||
highscoreList.addResult(5);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 5 to an empty list", highscoreList,
|
||||
List.of(5));
|
||||
|
||||
highscoreList.addResult(6);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 6 to the list [5]", highscoreList,
|
||||
List.of(5, 6));
|
||||
|
||||
highscoreList.addResult(2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [5, 6]", highscoreList,
|
||||
List.of(2, 5, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Add results - list becomes too long")
|
||||
public void testAddElementMoreThanMax() {
|
||||
highscoreList.addResult(5);
|
||||
highscoreList.addResult(6);
|
||||
highscoreList.addResult(2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
|
||||
highscoreList, List.of(2, 5, 6));
|
||||
|
||||
highscoreList.addResult(3);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 3 to the list [2, 5, 6]",
|
||||
highscoreList, List.of(2, 3, 5));
|
||||
|
||||
highscoreList.addResult(7);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 7 to the list [2, 3, 5]",
|
||||
highscoreList, List.of(2, 3, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Add two identical elements")
|
||||
public void testAddElementDuplicate() {
|
||||
highscoreList.addResult(5);
|
||||
highscoreList.addResult(6);
|
||||
highscoreList.addResult(2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
|
||||
highscoreList, List.of(2, 5, 6));
|
||||
|
||||
highscoreList.addResult(2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [2, 5, 6]",
|
||||
highscoreList, List.of(2, 2, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test listeners (simple)")
|
||||
public void testListListenersSimple() {
|
||||
// Mock a listener
|
||||
ObservableListListener listener = (list, pos) -> pos2 = pos;
|
||||
highscoreList.addObservableListListener(listener);
|
||||
|
||||
this.addResultWithListener(0, 5);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 5 to the list []", highscoreList,
|
||||
List.of(5));
|
||||
|
||||
this.addResultWithListener(1, 6);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 6 to the list [5]", highscoreList,
|
||||
List.of(5, 6));
|
||||
|
||||
this.addResultWithListener(0, 2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [5, 6]", highscoreList,
|
||||
List.of(2, 5, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("With listener - list becomes too long")
|
||||
public void testListListenerMoreThanMax() {
|
||||
// Mock a listener
|
||||
ObservableListListener listener = (list, pos) -> pos2 = pos;
|
||||
highscoreList.addObservableListListener(listener);
|
||||
|
||||
highscoreList.addResult(5);
|
||||
highscoreList.addResult(6);
|
||||
highscoreList.addResult(2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
|
||||
highscoreList, List.of(2, 5, 6));
|
||||
|
||||
this.addResultWithListener(1, 3);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 3 to the list [2, 5, 6]",
|
||||
highscoreList, List.of(2, 3, 5));
|
||||
|
||||
// Reset pos2 since the next element falls outside the list and is therefore not updated by
|
||||
// itself and sent to the listener
|
||||
pos2 = -1;
|
||||
this.addResultWithListener(-1, 7);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 7 to the list [2, 3, 5]",
|
||||
highscoreList, List.of(2, 3, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("With listener - two identical elements")
|
||||
public void testListListenerDuplicate() {
|
||||
// Mock a listener
|
||||
ObservableListListener listener = (list, pos) -> pos2 = pos;
|
||||
highscoreList.addObservableListListener(listener);
|
||||
|
||||
highscoreList.addResult(5);
|
||||
highscoreList.addResult(6);
|
||||
highscoreList.addResult(2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 5, 6, and 2 to the list",
|
||||
highscoreList, List.of(2, 5, 6));
|
||||
|
||||
this.addResultWithListener(1, 2);
|
||||
ObservableHighscoreListTest.checkHighscoreList("Added 2 to the list [2, 5, 6]",
|
||||
highscoreList, List.of(2, 2, 5));
|
||||
}
|
||||
}
|
||||
106
src/test/java/oving7/observablelist/ObservableListTest.java
Normal file
106
src/test/java/oving7/observablelist/ObservableListTest.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package oving7.observablelist;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
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.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ObservableListTest {
|
||||
|
||||
private int pos1;
|
||||
private int pos2;
|
||||
private ObservableList observableList;
|
||||
|
||||
private static void checkObservableList(ObservableList list, List<Integer> elements,
|
||||
String contextMessage) {
|
||||
assertEquals(elements.size(), list.size(),
|
||||
contextMessage + " -> Testing the length of observableList");
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (int element : elements) {
|
||||
assertEquals(element, list.getElement(i),
|
||||
contextMessage + " -> Testing that the element at position " + i + " matches");
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private void addElementWithListener(int pos, int element) {
|
||||
pos1 = pos;
|
||||
observableList.addElement(pos, element);
|
||||
|
||||
// Check that the position that was changed is the same as the one sent to the listener
|
||||
assertEquals(pos1, pos2, "Added " + element + " at position " + pos
|
||||
+ " -> Testing the position received by the listener");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
observableList = new ObservableList() {
|
||||
@Override
|
||||
public boolean acceptsElement(Object element) {
|
||||
return element instanceof Integer;
|
||||
}
|
||||
};
|
||||
pos1 = -1;
|
||||
pos2 = -1;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test constructor")
|
||||
public void testConstructor() {
|
||||
assertEquals(0, observableList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test acceptance of elements")
|
||||
public void testAcceptsElement() {
|
||||
assertTrue(observableList.acceptsElement(5), "Testing that the list accepts integers");
|
||||
assertFalse(observableList.acceptsElement("5"),
|
||||
"Testing that the list does not accept strings");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
observableList.addElement("5");
|
||||
}, "Testing that the list cannot accept elements of type string");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test adding elements")
|
||||
public void testAddElement() {
|
||||
observableList.addElement(5);
|
||||
ObservableListTest.checkObservableList(observableList, List.of(5),
|
||||
"Added 5 to an empty list");
|
||||
|
||||
observableList.addElement(6);
|
||||
ObservableListTest.checkObservableList(observableList, List.of(5, 6),
|
||||
"Added 6 to the list [5]");
|
||||
|
||||
observableList.addElement(0, 2);
|
||||
ObservableListTest.checkObservableList(observableList, List.of(2, 5, 6),
|
||||
"Added 2 at position 0 in the list [5, 6]");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test listener")
|
||||
public void testListListener() {
|
||||
ObservableListListener listener = (list, pos) -> pos2 = pos;
|
||||
observableList.addObservableListListener(listener);
|
||||
|
||||
this.addElementWithListener(0, 5);
|
||||
ObservableListTest.checkObservableList(observableList, List.of(5),
|
||||
"Added 5 to the list []");
|
||||
|
||||
this.addElementWithListener(1, 6);
|
||||
ObservableListTest.checkObservableList(observableList, List.of(5, 6),
|
||||
"Added 6 to the list [5]");
|
||||
|
||||
this.addElementWithListener(0, 2);
|
||||
ObservableListTest.checkObservableList(observableList, List.of(2, 5, 6),
|
||||
"Added 2 to the list [5, 6]");
|
||||
}
|
||||
}
|
||||
84
src/test/java/oving7/savingsaccount/BSUTest.java
Normal file
84
src/test/java/oving7/savingsaccount/BSUTest.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BSUTest {
|
||||
|
||||
private static final double epsilon = 0.001;
|
||||
|
||||
private BSU bsu;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
bsu = new BSU(0.05, 25_000.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that deposits work as expected")
|
||||
public void testDeposit() {
|
||||
bsu.deposit(10_000.0);
|
||||
assertEquals(10_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
bsu.deposit(-100.0);
|
||||
}, "Negative deposit should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
bsu.deposit(20_000.0);
|
||||
}, "Should not be able to deposit more money than the deposit limit");
|
||||
|
||||
bsu.endYearUpdate();
|
||||
bsu.deposit(20_000.0);
|
||||
assertEquals(10_000.0 * (1 + 0.05) + 20_000.0, bsu.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that withdrawals work as expected")
|
||||
public void testWithdraw() {
|
||||
bsu.deposit(20_000.0);
|
||||
bsu.withdraw(5000.0);
|
||||
assertEquals(15_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
bsu.withdraw(-10_000.0);
|
||||
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(15_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
bsu.withdraw(20_000);
|
||||
}, "Should not be able to withdraw more money than deposited this year");
|
||||
|
||||
assertEquals(15_000.0, bsu.getBalance(), epsilon, "The account balance was incorrect");
|
||||
|
||||
bsu.endYearUpdate();
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
bsu.withdraw(10_000);
|
||||
}, "Should not be able to withdraw more money than deposited this year");
|
||||
|
||||
assertEquals(15_000 * (1 + 0.05), bsu.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the tax deduction is correct")
|
||||
public void testTaxDeduction() {
|
||||
bsu.deposit(20_000.0);
|
||||
assertEquals(20_000.0 * 0.20, bsu.getTaxDeduction(), epsilon,
|
||||
"The tax deduction was incorrect");
|
||||
|
||||
bsu.endYearUpdate();
|
||||
bsu.deposit(10_000.0);
|
||||
assertEquals(10_000.0 * 0.20, bsu.getTaxDeduction(), epsilon,
|
||||
"The tax deduction was incorrect");
|
||||
|
||||
bsu.endYearUpdate();
|
||||
assertEquals(0.0, bsu.getTaxDeduction(), epsilon, "The tax deduction was incorrect");
|
||||
}
|
||||
}
|
||||
72
src/test/java/oving7/savingsaccount/ForeldreSparTest.java
Normal file
72
src/test/java/oving7/savingsaccount/ForeldreSparTest.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ForeldreSparTest {
|
||||
|
||||
private static final double epsilon = 0.001;
|
||||
|
||||
private ForeldreSpar foreldreSpar;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
foreldreSpar = new ForeldreSpar(0.04, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that withdraw works as expected")
|
||||
public void testWithdraw() {
|
||||
foreldreSpar.deposit(10_000.0);
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
assertEquals(9000.0, foreldreSpar.getBalance(), epsilon,
|
||||
"The account balance is incorrect");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
foreldreSpar.withdraw(-10_000.0);
|
||||
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(9000, foreldreSpar.getBalance(), epsilon, "The account balance is incorrect");
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
foreldreSpar.withdraw(10_000);
|
||||
}, "Should not be able to withdraw more money than is in the account");
|
||||
|
||||
assertEquals(9000.0, foreldreSpar.getBalance(), epsilon,
|
||||
"The account balance is incorrect");
|
||||
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
}, "Should not be able to make more withdrawals than the set limit");
|
||||
|
||||
foreldreSpar.endYearUpdate();
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
assertEquals(7000.0 * (1 + 0.04) - 1000.0, foreldreSpar.getBalance(), epsilon,
|
||||
"The account balance is incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that remaining withdrawals are always correct")
|
||||
public void testRemainingWithdrawals() {
|
||||
foreldreSpar.deposit(10_000.0);
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
assertEquals(2, foreldreSpar.getRemainingWithdrawals());
|
||||
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
assertEquals(0, foreldreSpar.getRemainingWithdrawals());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
foreldreSpar.withdraw(1000.0);
|
||||
}, "Should not be able to make more withdrawals than the set limit");
|
||||
|
||||
foreldreSpar.endYearUpdate();
|
||||
assertEquals(3, foreldreSpar.getRemainingWithdrawals());
|
||||
}
|
||||
}
|
||||
76
src/test/java/oving7/savingsaccount/SavingsAccountTest.java
Normal file
76
src/test/java/oving7/savingsaccount/SavingsAccountTest.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package oving7.savingsaccount;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SavingsAccountTest {
|
||||
|
||||
private static final double epsilon = 0.001;
|
||||
|
||||
private SavingsAccount savingsAccount;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
savingsAccount = new SavingsAccount(0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the balance is correct after deposit")
|
||||
public void testBalance() {
|
||||
savingsAccount.deposit(100.0);
|
||||
assertEquals(100.0, savingsAccount.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test deposit and withdraw")
|
||||
public void testDepositAndWithdraw() {
|
||||
savingsAccount.deposit(100.0);
|
||||
savingsAccount.withdraw(40.0);
|
||||
assertEquals(60.0, savingsAccount.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test deposit and withdraw with illegal input")
|
||||
public void testDepositAndWithdrawIllegalInput() {
|
||||
savingsAccount.deposit(10.0);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
savingsAccount.deposit(-100.0);
|
||||
}, "Negative deposit should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(10.0, savingsAccount.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
|
||||
savingsAccount.deposit(10.0);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
savingsAccount.withdraw(-100.0);
|
||||
}, "Negative withdrawal should have triggered an IllegalArgumentException!");
|
||||
|
||||
assertEquals(20.0, savingsAccount.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
|
||||
savingsAccount.deposit(10.0);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
savingsAccount.withdraw(40.0);
|
||||
}, "Withdrawal of more than the balance should have triggered an IllegalStateException");
|
||||
|
||||
assertEquals(30.0, savingsAccount.getBalance(), epsilon,
|
||||
"The account balance was incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that the interest is calculated correctly")
|
||||
public void endYearUpdate() {
|
||||
savingsAccount.deposit(100.0);
|
||||
savingsAccount.endYearUpdate();
|
||||
assertEquals(100.0 * (1 + 0.10), savingsAccount.getBalance(), epsilon,
|
||||
"The account balance was incorrect after interest was added");
|
||||
}
|
||||
}
|
||||
28
src/test/java/oving7/train/CargoCarTest.java
Normal file
28
src/test/java/oving7/train/CargoCarTest.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package oving7.train;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CargoCarTest {
|
||||
|
||||
private CargoCar cargoCar;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
cargoCar = new CargoCar(3000, 2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check total weight")
|
||||
public void testWeight() {
|
||||
assertEquals(5000, cargoCar.getTotalWeight(), "Test total weight after initialization");
|
||||
|
||||
cargoCar.setCargoWeight(4000);
|
||||
assertEquals(7000, cargoCar.getTotalWeight(),
|
||||
"Test total weight after changing cargo weight");
|
||||
assertEquals(4000, cargoCar.getCargoWeight(),
|
||||
"Test cargo weight after changing the weight");
|
||||
}
|
||||
}
|
||||
29
src/test/java/oving7/train/PassengerCarTest.java
Normal file
29
src/test/java/oving7/train/PassengerCarTest.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package oving7.train;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PassengerCarTest {
|
||||
|
||||
private PassengerCar passengerCar;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
passengerCar = new PassengerCar(3000, 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check total weight")
|
||||
public void testWeight() {
|
||||
assertEquals(3000 + (200 * 80), passengerCar.getTotalWeight(),
|
||||
"Test total weight after initialization");
|
||||
|
||||
passengerCar.setPassengerCount(100);
|
||||
assertEquals(3000 + (100 * 80), passengerCar.getTotalWeight(),
|
||||
"Test total weight after changing the number of passengers");
|
||||
assertEquals(100, passengerCar.getPassengerCount(),
|
||||
"Test passenger count after changing the number");
|
||||
}
|
||||
}
|
||||
26
src/test/java/oving7/train/TrainCarTest.java
Normal file
26
src/test/java/oving7/train/TrainCarTest.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package oving7.train;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TrainCarTest {
|
||||
|
||||
private TrainCar trainCar;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
trainCar = new TrainCar(3000);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dead weight equals total weight")
|
||||
public void testDeadWeight() {
|
||||
assertEquals(3000, trainCar.getTotalWeight(), "Test initialization of dead weight");
|
||||
|
||||
trainCar.setDeadWeight(5000);
|
||||
assertEquals(5000, trainCar.getTotalWeight(),
|
||||
"Test that total weight equals set dead weight");
|
||||
}
|
||||
}
|
||||
85
src/test/java/oving7/train/TrainTest.java
Normal file
85
src/test/java/oving7/train/TrainTest.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package oving7.train;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
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 org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TrainTest {
|
||||
|
||||
private CargoCar cc1;
|
||||
private CargoCar cc2;
|
||||
private PassengerCar pc1;
|
||||
private PassengerCar pc2;
|
||||
private Train train;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
train = new Train();
|
||||
pc1 = new PassengerCar(2000, 200);
|
||||
pc2 = new PassengerCar(1500, 100);
|
||||
cc1 = new CargoCar(3000, 5000);
|
||||
cc2 = new CargoCar(2500, 7000);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Add cars to train")
|
||||
public void testAddCarToTrain() {
|
||||
train.addTrainCar(pc1);
|
||||
train.addTrainCar(pc2);
|
||||
train.addTrainCar(cc1);
|
||||
assertTrue(train.contains(pc1),
|
||||
"Test if the train contains passenger car 1 after it has been added");
|
||||
assertTrue(train.contains(pc2),
|
||||
"Test if the train contains passenger car 2 after it has been added");
|
||||
assertTrue(train.contains(cc1),
|
||||
"Test if the train contains cargo car 1 after it has been added");
|
||||
assertFalse(train.contains(cc2),
|
||||
"Test if the train contains cargo car 2 without it being added");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
train.addTrainCar(null);
|
||||
}, "Test if an IllegalArgumentException is thrown when adding a null car");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check total weight of the train")
|
||||
public void testTotalTrainWeight() {
|
||||
train.addTrainCar(pc1);
|
||||
train.addTrainCar(cc1);
|
||||
assertEquals(8000 + (2000 + (200 * 80)), train.getTotalWeight(),
|
||||
"Test the train's total weight after adding a passenger car and a cargo car");
|
||||
|
||||
train.addTrainCar(pc2);
|
||||
assertEquals(8000 + (2000 + (200 * 80)) + (1500 + (100 * 80)), train.getTotalWeight(),
|
||||
"Test the train's total weight after adding another passenger car");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check passenger count on the train")
|
||||
public void testPassengerCount() {
|
||||
train.addTrainCar(pc1);
|
||||
train.addTrainCar(pc2);
|
||||
assertEquals(300, train.getPassengerCount(),
|
||||
"Test passenger count after adding passenger cars");
|
||||
|
||||
train.addTrainCar(cc1);
|
||||
assertEquals(300, train.getPassengerCount(),
|
||||
"Test passenger count after adding a cargo car");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check cargo weight on the train")
|
||||
public void testCargoWeight() {
|
||||
train.addTrainCar(cc1);
|
||||
train.addTrainCar(cc2);
|
||||
assertEquals(12_000, train.getCargoWeight(), "Test cargo weight after adding cargo cars");
|
||||
|
||||
train.addTrainCar(pc1);
|
||||
assertEquals(12_000, train.getCargoWeight(),
|
||||
"Test cargo weight after adding a passenger car");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user