Add oving 4
This commit is contained in:
74
src/test/java/oving4/PartnerTest.java
Normal file
74
src/test/java/oving4/PartnerTest.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package oving4;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
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 PartnerTest {
|
||||
|
||||
private Partner p1;
|
||||
private Partner p2;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
p1 = new Partner("P1");
|
||||
p2 = new Partner("P2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the constructor initializes correctly")
|
||||
public void testConstructor() {
|
||||
assertNull(p1.getPartner());
|
||||
assertNull(p2.getPartner());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Partner(null);
|
||||
}, "Name cannot be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that P1 and P2 are partners after p1.setPartner(p2)")
|
||||
public void simplePartnership() {
|
||||
assertNull(p1.getPartner());
|
||||
assertNull(p2.getPartner());
|
||||
|
||||
p1.setPartner(p2);
|
||||
assertEquals(p1.getPartner(), p2, "P1 should be partner to P2");
|
||||
assertEquals(p2.getPartner(), p1, "P2 should be partner to P1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that one can split up a partnership")
|
||||
public void partnershipWithDivorce() {
|
||||
p1.setPartner(p2);
|
||||
assertEquals(p1.getPartner(), p2, "P1 should be partner to P2");
|
||||
assertEquals(p2.getPartner(), p1, "P2 should be partner to P1");
|
||||
|
||||
p1.setPartner(null);
|
||||
assertNull(p1.getPartner());
|
||||
assertNull(p2.getPartner());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that combined breakup followed by the creation of a new partnership works")
|
||||
public void swinger() {
|
||||
Partner p3 = new Partner("P3");
|
||||
Partner p4 = new Partner("P4");
|
||||
|
||||
p1.setPartner(p2);
|
||||
p3.setPartner(p4);
|
||||
assertEquals(p1.getPartner(), p2, "P1 should be the partner of P2");
|
||||
assertEquals(p2.getPartner(), p1, "P2 should be the partner of P1");
|
||||
assertEquals(p3.getPartner(), p4, "P3 should be the partner of P4");
|
||||
assertEquals(p4.getPartner(), p3, "P4 should be the partner of P3");
|
||||
|
||||
p1.setPartner(p4);
|
||||
assertEquals(p1.getPartner(), p4, "P4 should be the partner of P1");
|
||||
assertEquals(p4.getPartner(), p1, "P1 should be the partner of P4");
|
||||
assertNull(p2.getPartner());
|
||||
assertNull(p3.getPartner());
|
||||
}
|
||||
}
|
||||
333
src/test/java/oving4/PersonTest.java
Normal file
333
src/test/java/oving4/PersonTest.java
Normal file
@@ -0,0 +1,333 @@
|
||||
package oving4;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PersonTest {
|
||||
|
||||
private Person anne;
|
||||
private Person hallvard;
|
||||
private Person jens;
|
||||
private Person marit;
|
||||
|
||||
private static void hasChildren(Person person, Collection<Person> children) {
|
||||
assertEquals(children.size(), person.getChildCount());
|
||||
|
||||
for (Person child : children) {
|
||||
boolean found = false;
|
||||
int i = 0;
|
||||
|
||||
while (i < person.getChildCount()) {
|
||||
if (child == person.getChild(i)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
assertTrue(found);
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
anne = new Person("Anne", 'F');
|
||||
hallvard = new Person("Hallvard", 'M');
|
||||
jens = new Person("Jens", 'M');
|
||||
marit = new Person("Marit", 'F');
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor")
|
||||
public void testConstructor() {
|
||||
assertEquals("Anne", anne.getName());
|
||||
assertEquals('F', anne.getGender());
|
||||
assertEquals(0, anne.getChildCount());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Person(null, 'M');
|
||||
}, "Name cannot be null");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Person("Anne", 'X');
|
||||
}, "X is not a valid gender");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Child cannot be null")
|
||||
public void testAddChildException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
anne.addChild(null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Woman cannot be father")
|
||||
public void testFatherException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
jens.setFather(marit);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
anne.setFather(marit);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Man cannot be mother")
|
||||
public void testMotherException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
jens.setMother(hallvard);
|
||||
});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
anne.setMother(hallvard);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Man cannot be his own father")
|
||||
public void testSelfFatherException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
jens.setFather(jens);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Woman cannot be her own mother")
|
||||
public void testSelfMotherException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
anne.setMother(anne);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Setting father with setFather")
|
||||
public void testSetFather() {
|
||||
jens.setFather(hallvard);
|
||||
|
||||
// Check state of Hallvard
|
||||
assertEquals(null, hallvard.getFather());
|
||||
assertEquals(null, hallvard.getMother());
|
||||
PersonTest.hasChildren(hallvard, List.of(jens));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(hallvard, jens.getFather());
|
||||
assertEquals(null, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
anne.setFather(hallvard);
|
||||
|
||||
// Check state of Hallvard
|
||||
assertEquals(null, hallvard.getFather());
|
||||
assertEquals(null, hallvard.getMother());
|
||||
PersonTest.hasChildren(hallvard, List.of(jens, anne));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(hallvard, jens.getFather());
|
||||
assertEquals(null, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
// Check state of Anne
|
||||
assertEquals(hallvard, anne.getFather());
|
||||
assertEquals(null, anne.getMother());
|
||||
assertEquals(0, anne.getChildCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Setting father with addChild")
|
||||
public void testFatherAddChild() {
|
||||
hallvard.addChild(jens);
|
||||
|
||||
// Check state of Hallvard
|
||||
assertEquals(null, hallvard.getFather());
|
||||
assertEquals(null, hallvard.getMother());
|
||||
PersonTest.hasChildren(hallvard, List.of(jens));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(hallvard, jens.getFather());
|
||||
assertEquals(null, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
hallvard.addChild(anne);
|
||||
|
||||
// Check state of Hallvard
|
||||
assertEquals(null, hallvard.getFather());
|
||||
assertEquals(null, hallvard.getMother());
|
||||
PersonTest.hasChildren(hallvard, List.of(jens, anne));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(hallvard, jens.getFather());
|
||||
assertEquals(null, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
// Check state of Anne
|
||||
assertEquals(hallvard, anne.getFather());
|
||||
assertEquals(null, anne.getMother());
|
||||
assertEquals(0, anne.getChildCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Setting mother with setMother")
|
||||
public void testSetMother() {
|
||||
jens.setMother(marit);
|
||||
|
||||
// Check state of Marit
|
||||
assertEquals(null, marit.getFather());
|
||||
assertEquals(null, marit.getMother());
|
||||
PersonTest.hasChildren(marit, List.of(jens));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(null, jens.getFather());
|
||||
assertEquals(marit, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
anne.setMother(marit);
|
||||
|
||||
// Check state of Marit
|
||||
assertEquals(null, marit.getFather());
|
||||
assertEquals(null, marit.getMother());
|
||||
PersonTest.hasChildren(marit, List.of(jens, anne));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(null, jens.getFather());
|
||||
assertEquals(marit, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
// Check state of Anne
|
||||
assertEquals(null, anne.getFather());
|
||||
assertEquals(marit, anne.getMother());
|
||||
assertEquals(0, anne.getChildCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Setting mother with addChild")
|
||||
public void testMotherAddChild() {
|
||||
marit.addChild(jens);
|
||||
|
||||
// Check state of Marit
|
||||
assertEquals(null, marit.getFather());
|
||||
assertEquals(null, marit.getMother());
|
||||
PersonTest.hasChildren(marit, List.of(jens));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(null, jens.getFather());
|
||||
assertEquals(marit, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
marit.addChild(anne);
|
||||
|
||||
// Check state of Marit
|
||||
assertEquals(null, marit.getFather());
|
||||
assertEquals(null, marit.getMother());
|
||||
PersonTest.hasChildren(marit, List.of(jens, anne));
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(null, jens.getFather());
|
||||
assertEquals(marit, jens.getMother());
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
// Check state of Anne
|
||||
assertEquals(null, anne.getFather());
|
||||
assertEquals(marit, anne.getMother());
|
||||
assertEquals(0, anne.getChildCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Change father with setFather")
|
||||
public void testChangeFatherSetFather() {
|
||||
anne.setFather(jens);
|
||||
// Check state of Anne
|
||||
assertEquals(jens, anne.getFather());
|
||||
|
||||
// Check state of Jens
|
||||
PersonTest.hasChildren(jens, List.of(anne));
|
||||
|
||||
anne.setFather(hallvard);
|
||||
|
||||
// Check state of Anne
|
||||
assertEquals(hallvard, anne.getFather());
|
||||
|
||||
// Check state of Jens
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
// Check state of Hallvard
|
||||
PersonTest.hasChildren(hallvard, List.of(anne));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Change father with addChild")
|
||||
public void testChangeFatherAddChild() {
|
||||
jens.addChild(anne);
|
||||
|
||||
// Check state of anne
|
||||
assertEquals(jens, anne.getFather());
|
||||
|
||||
// Check state of jens
|
||||
PersonTest.hasChildren(jens, List.of(anne));
|
||||
|
||||
hallvard.addChild(anne);
|
||||
|
||||
// Check state of anne
|
||||
assertEquals(hallvard, anne.getFather());
|
||||
|
||||
// Check state of jens
|
||||
assertEquals(0, jens.getChildCount());
|
||||
|
||||
// Check state of hallvard
|
||||
PersonTest.hasChildren(hallvard, List.of(anne));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Change mother with setMother")
|
||||
public void testChangeMotherSetMother() {
|
||||
jens.setMother(anne);
|
||||
|
||||
// Check state of jens
|
||||
assertEquals(anne, jens.getMother());
|
||||
|
||||
// Check state of anne
|
||||
PersonTest.hasChildren(anne, List.of(jens));
|
||||
|
||||
jens.setMother(marit);
|
||||
|
||||
// Check state of jens
|
||||
assertEquals(marit, jens.getMother());
|
||||
|
||||
// Check state of anne
|
||||
assertEquals(0, anne.getChildCount());
|
||||
|
||||
// Check state of marit
|
||||
PersonTest.hasChildren(marit, List.of(jens));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Change mother with addChild")
|
||||
public void testChangeMotherAddChild() {
|
||||
anne.addChild(jens);
|
||||
|
||||
// Check state of jens
|
||||
assertEquals(anne, jens.getMother());
|
||||
|
||||
// Check state of anne
|
||||
PersonTest.hasChildren(anne, List.of(jens));
|
||||
|
||||
marit.addChild(jens);
|
||||
|
||||
// Check state of jens
|
||||
assertEquals(marit, jens.getMother());
|
||||
|
||||
// Check state of anne
|
||||
assertEquals(0, anne.getChildCount());
|
||||
|
||||
// Check state of marit
|
||||
PersonTest.hasChildren(marit, List.of(jens));
|
||||
}
|
||||
}
|
||||
59
src/test/java/oving4/card/CardDeckTest.java
Normal file
59
src/test/java/oving4/card/CardDeckTest.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package oving4.card;
|
||||
|
||||
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 CardDeckTest {
|
||||
|
||||
private CardDeck cardDeck;
|
||||
|
||||
private static void checkDeck(CardDeck deck, String deckAsString) {
|
||||
String[] toStrings = deckAsString.split(",");
|
||||
assertEquals(toStrings.length, deck.getCardCount(),
|
||||
"CardDeck does not have the correct size");
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (String toString : toStrings) {
|
||||
Card card = deck.getCard(i);
|
||||
String cardString = String.valueOf(card.getSuit()) + card.getFace();
|
||||
assertEquals(toString, cardString,
|
||||
String.format("Card at position %d was incorrect. CardDeck should have been %s",
|
||||
i + 1, toStrings));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
cardDeck = new CardDeck(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that CardDeck is initialized to S1,S2,H1,H2,D1,D2,C1,C2")
|
||||
public void testConstructor() {
|
||||
CardDeckTest.checkDeck(cardDeck, "S1,S2,H1,H2,D1,D2,C1,C2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that CardDeck is shuffled to S1,D1,S2,D2,H1,C1,H2,C2")
|
||||
public void testShufflePerfectly() {
|
||||
cardDeck.shufflePerfectly();
|
||||
CardDeckTest.checkDeck(cardDeck, "S1,D1,S2,D2,H1,C1,H2,C2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that deal gives out the last three cards")
|
||||
public void testDeal() {
|
||||
CardHand hand = new CardHand();
|
||||
cardDeck.deal(hand, 3);
|
||||
CardDeckTest.checkDeck(cardDeck, "S1,S2,H1,H2,D1");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
cardDeck.deal(null, 1);
|
||||
}, "Cannot deal to null");
|
||||
}
|
||||
}
|
||||
75
src/test/java/oving4/card/CardHandTest.java
Normal file
75
src/test/java/oving4/card/CardHandTest.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package oving4.card;
|
||||
|
||||
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 CardHandTest {
|
||||
|
||||
private CardHand cardHand;
|
||||
|
||||
private static void checkHand(CardHand hand, String deckAsString) {
|
||||
String[] toStrings = deckAsString.split(",");
|
||||
assertEquals(toStrings.length, hand.getCardCount(),
|
||||
"The number of cards in the hand was incorrect");
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (String toString : toStrings) {
|
||||
Card card = hand.getCard(i);
|
||||
String cardString = String.valueOf(card.getSuit()) + card.getFace();
|
||||
assertEquals(toString, cardString, String.format(
|
||||
"The card at position %d was incorrect. The hand should have contained %s",
|
||||
i + 1, toStrings));
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
cardHand = new CardHand();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that CardHand is initialized to empty")
|
||||
public void testConstructor() {
|
||||
CardDeck deck = new CardDeck(2);
|
||||
deck.deal(cardHand, 3);
|
||||
CardHandTest.checkHand(cardHand, "C2,C1,D2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test the addCard method")
|
||||
public void testAddCard() {
|
||||
CardDeck deck = new CardDeck(2);
|
||||
deck.deal(cardHand, 3);
|
||||
CardHandTest.checkHand(cardHand, "C2,C1,D2");
|
||||
|
||||
cardHand.addCard(new Card('H', 1));
|
||||
CardHandTest.checkHand(cardHand, "C2,C1,D2,H1");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
cardHand.addCard(null);
|
||||
}, "Cannot add null card");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test the deal and play methods")
|
||||
public void testDealPlay() {
|
||||
CardDeck deck = new CardDeck(2);
|
||||
deck.deal(cardHand, 3);
|
||||
CardHandTest.checkHand(cardHand, "C2,C1,D2");
|
||||
|
||||
cardHand.play(1);
|
||||
CardHandTest.checkHand(cardHand, "C2,D2");
|
||||
|
||||
cardHand.play(0);
|
||||
CardHandTest.checkHand(cardHand, "D2");
|
||||
|
||||
cardHand.play(0);
|
||||
assertEquals(cardHand.getCardCount(), 0);
|
||||
}
|
||||
}
|
||||
46
src/test/java/oving4/card/CardTest.java
Normal file
46
src/test/java/oving4/card/CardTest.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package oving4.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardTest {
|
||||
|
||||
private static void checkCard(Card card, char suit, int face) {
|
||||
assertEquals(card.getSuit(), suit);
|
||||
assertEquals(card.getFace(), face);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the constructor creates Card objects with correct values")
|
||||
public void testConstructor() {
|
||||
CardTest.checkCard(new Card('S', 1), 'S', 1);
|
||||
CardTest.checkCard(new Card('S', 13), 'S', 13);
|
||||
CardTest.checkCard(new Card('H', 1), 'H', 1);
|
||||
CardTest.checkCard(new Card('H', 13), 'H', 13);
|
||||
CardTest.checkCard(new Card('D', 1), 'D', 1);
|
||||
CardTest.checkCard(new Card('D', 13), 'D', 13);
|
||||
CardTest.checkCard(new Card('C', 1), 'C', 1);
|
||||
CardTest.checkCard(new Card('C', 13), 'C', 13);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Card('X', 1);
|
||||
}, "Should not be able to create a card of type X");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Card('S', 0);
|
||||
}, "Should not be able to create a card with value 0");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Card('C', 14);
|
||||
}, "Should not be able to create a card with value 14");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that toString works as expected")
|
||||
public void testToString() {
|
||||
assertEquals("S1", new Card('S', 1).toString());
|
||||
assertEquals("H13", new Card('H', 13).toString());
|
||||
}
|
||||
}
|
||||
111
src/test/java/oving4/stopwatch/StopWatchManagerTest.java
Normal file
111
src/test/java/oving4/stopwatch/StopWatchManagerTest.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package oving4.stopwatch;
|
||||
|
||||
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 StopWatchManagerTest {
|
||||
|
||||
private StopWatchManager manager;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
manager = new StopWatchManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Create new StopWatch")
|
||||
public void testNewStopWatch() {
|
||||
StopWatch sw1 = manager.newStopWatch("SW1");
|
||||
StopWatch sw2 = manager.newStopWatch("SW2");
|
||||
assertEquals(sw1, manager.getStopWatch("SW1"));
|
||||
assertEquals(sw2, manager.getStopWatch("SW2"));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
manager.newStopWatch(null);
|
||||
}, "Name cannot be null");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
manager.newStopWatch("SW1");
|
||||
}, "Name already exists");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Ticker")
|
||||
public void testTicks() {
|
||||
StopWatch sw1 = manager.newStopWatch("SW1");
|
||||
StopWatch sw2 = manager.newStopWatch("SW2");
|
||||
|
||||
manager.tick(1);
|
||||
assertEquals(1, sw1.getTicks());
|
||||
assertEquals(1, sw2.getTicks());
|
||||
|
||||
manager.tick(4);
|
||||
assertEquals(5, sw1.getTicks());
|
||||
assertEquals(5, sw2.getTicks());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove StopWatches")
|
||||
public void testRemoveStopWatches() {
|
||||
assertEquals(0, manager.getAllWatches().size());
|
||||
|
||||
StopWatch sw1 = manager.newStopWatch("SW1");
|
||||
assertEquals(1, manager.getAllWatches().size());
|
||||
assertEquals(sw1, manager.getStopWatch("SW1"));
|
||||
|
||||
StopWatch sw2 = manager.newStopWatch("SW2");
|
||||
assertEquals(2, manager.getAllWatches().size());
|
||||
assertEquals(sw1, manager.getStopWatch("SW1"));
|
||||
assertEquals(sw2, manager.getStopWatch("SW2"));
|
||||
|
||||
manager.removeStopWatch("SW1");
|
||||
assertEquals(1, manager.getAllWatches().size());
|
||||
assertEquals(null, manager.getStopWatch("SW1"));
|
||||
|
||||
manager.removeStopWatch("SW2");
|
||||
assertEquals(0, manager.getAllWatches().size());
|
||||
assertEquals(null, manager.getStopWatch("SW1"));
|
||||
assertEquals(null, manager.getStopWatch("SW2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Starting and stopping StopWatches")
|
||||
public void testStartedStoppedWatches() {
|
||||
assertEquals(0, manager.getStartedWatches().size());
|
||||
|
||||
manager.newStopWatch("SW1").start();
|
||||
assertEquals(1, manager.getStartedWatches().size());
|
||||
assertEquals(0, manager.getStoppedWatches().size());
|
||||
assertTrue(manager.getStartedWatches().contains(manager.getStopWatch("SW1")));
|
||||
assertTrue(manager.getStopWatch("SW1").isStarted());
|
||||
|
||||
manager.newStopWatch("SW2").start();
|
||||
assertEquals(2, manager.getStartedWatches().size());
|
||||
assertEquals(0, manager.getStoppedWatches().size());
|
||||
assertTrue(manager.getStartedWatches().contains(manager.getStopWatch("SW1")));
|
||||
assertTrue(manager.getStopWatch("SW1").isStarted());
|
||||
assertFalse(manager.getStopWatch("SW1").isStopped());
|
||||
assertTrue(manager.getStartedWatches().contains(manager.getStopWatch("SW2")));
|
||||
assertTrue(manager.getStopWatch("SW2").isStarted());
|
||||
assertFalse(manager.getStopWatch("SW2").isStopped());
|
||||
|
||||
manager.getStopWatch("SW2").stop();
|
||||
assertEquals(1, manager.getStoppedWatches().size());
|
||||
assertFalse(manager.getStoppedWatches().contains(manager.getStopWatch("SW1")));
|
||||
assertFalse(manager.getStopWatch("SW1").isStopped());
|
||||
assertTrue(manager.getStoppedWatches().contains(manager.getStopWatch("SW2")));
|
||||
assertTrue(manager.getStopWatch("SW2").isStopped());
|
||||
|
||||
manager.getStopWatch("SW1").stop();
|
||||
assertEquals(2, manager.getStoppedWatches().size());
|
||||
assertTrue(manager.getStoppedWatches().contains(manager.getStopWatch("SW1")));
|
||||
assertTrue(manager.getStopWatch("SW1").isStopped());
|
||||
assertTrue(manager.getStoppedWatches().contains(manager.getStopWatch("SW2")));
|
||||
assertTrue(manager.getStopWatch("SW2").isStopped());
|
||||
}
|
||||
}
|
||||
159
src/test/java/oving4/stopwatch/StopWatchTest.java
Normal file
159
src/test/java/oving4/stopwatch/StopWatchTest.java
Normal file
@@ -0,0 +1,159 @@
|
||||
package oving4.stopwatch;
|
||||
|
||||
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 StopWatchTest {
|
||||
|
||||
private StopWatch stopWatch;
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() {
|
||||
stopWatch = new StopWatch();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that a newly created StopWatch object has correct values")
|
||||
public void testConstructor() {
|
||||
assertFalse(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
assertEquals(0, stopWatch.getTicks());
|
||||
assertEquals(-1, stopWatch.getTime());
|
||||
assertEquals(-1, stopWatch.getLapTime());
|
||||
assertEquals(-1, stopWatch.getLastLapTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that tick() without start does not change the time")
|
||||
public void testTicksWithoutStart() {
|
||||
stopWatch.tick(1);
|
||||
assertEquals(-1, stopWatch.getTime());
|
||||
assertEquals(1, stopWatch.getTicks());
|
||||
|
||||
stopWatch.tick(4);
|
||||
assertEquals(-1, stopWatch.getTime());
|
||||
assertEquals(5, stopWatch.getTicks());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Start and stop the StopWatch and check that the time is correct")
|
||||
public void testStartTickStop() {
|
||||
stopWatch.start();
|
||||
assertEquals(0, stopWatch.getTime());
|
||||
assertEquals(0, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
stopWatch.start();
|
||||
}, "Cannot start an already running StopWatch");
|
||||
|
||||
stopWatch.tick(3);
|
||||
assertEquals(3, stopWatch.getTime());
|
||||
assertEquals(3, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
stopWatch.tick(5);
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(8, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
stopWatch.stop();
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(8, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertTrue(stopWatch.isStopped());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
stopWatch.stop();
|
||||
}, "Cannot stop a StopWatch that is already stopped");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Start and stop the StopWatch, and call tick() while it is not started")
|
||||
public void testTickStartTickStopTick() {
|
||||
stopWatch.tick(2);
|
||||
assertEquals(-1, stopWatch.getTime());
|
||||
assertEquals(2, stopWatch.getTicks());
|
||||
assertFalse(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
stopWatch.start();
|
||||
assertEquals(0, stopWatch.getTime());
|
||||
assertEquals(2, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
stopWatch.tick(3);
|
||||
assertEquals(3, stopWatch.getTime());
|
||||
assertEquals(5, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
stopWatch.tick(5);
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(10, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertFalse(stopWatch.isStopped());
|
||||
|
||||
stopWatch.stop();
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(10, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertTrue(stopWatch.isStopped());
|
||||
|
||||
stopWatch.tick(3);
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(13, stopWatch.getTicks());
|
||||
assertTrue(stopWatch.isStarted());
|
||||
assertTrue(stopWatch.isStopped());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
stopWatch.tick(-1);
|
||||
}, "Time should not be able to go backward");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that laps work as expected")
|
||||
public void testLaps() {
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
stopWatch.lap();
|
||||
}, "Should not be able to start a new lap without starting the StopWatch");
|
||||
|
||||
stopWatch.start();
|
||||
assertEquals(0, stopWatch.getTime());
|
||||
assertEquals(0, stopWatch.getLapTime());
|
||||
assertEquals(-1, stopWatch.getLastLapTime());
|
||||
|
||||
stopWatch.tick(3);
|
||||
assertEquals(3, stopWatch.getTime());
|
||||
assertEquals(3, stopWatch.getLapTime());
|
||||
assertEquals(-1, stopWatch.getLastLapTime());
|
||||
|
||||
stopWatch.lap();
|
||||
assertEquals(3, stopWatch.getTime());
|
||||
assertEquals(0, stopWatch.getLapTime());
|
||||
assertEquals(3, stopWatch.getLastLapTime());
|
||||
|
||||
stopWatch.tick(5);
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(5, stopWatch.getLapTime());
|
||||
assertEquals(3, stopWatch.getLastLapTime());
|
||||
|
||||
stopWatch.stop();
|
||||
assertEquals(8, stopWatch.getTime());
|
||||
assertEquals(0, stopWatch.getLapTime());
|
||||
assertEquals(5, stopWatch.getLastLapTime());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
stopWatch.lap();
|
||||
}, "Should not be able to start a new lap with a stopped StopWatch");
|
||||
}
|
||||
}
|
||||
0
src/test/java/oving4/testing/.gitkeep
Normal file
0
src/test/java/oving4/testing/.gitkeep
Normal file
93
src/test/java/oving4/twitter/TweetTest.java
Normal file
93
src/test/java/oving4/twitter/TweetTest.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package oving4.twitter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
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 TweetTest {
|
||||
|
||||
private Tweet retweet1;
|
||||
private Tweet tweet;
|
||||
private TwitterAccount kari;
|
||||
private TwitterAccount nils;
|
||||
private TwitterAccount ole;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
nils = new TwitterAccount("Nils");
|
||||
ole = new TwitterAccount("Ole");
|
||||
kari = new TwitterAccount("Kari");
|
||||
tweet = new Tweet(nils, "Kvitre!");
|
||||
retweet1 = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the constructor initializes correctly")
|
||||
public void testNullInConstructors() {
|
||||
assertEquals("Kvitre!", tweet.getText());
|
||||
assertEquals(nils, tweet.getOwner());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Tweet(null, "Kvitre!");
|
||||
}, "The tweet must have an owner");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Tweet(nils, (String) null);
|
||||
}, "The tweet must have text");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Tweet(null, tweet);
|
||||
}, "The tweet must have an owner");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Tweet(nils, (Tweet) null);
|
||||
}, "The tweet must have an original tweet");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructorNewTweet() {
|
||||
assertEquals("Kvitre!", tweet.getText(),
|
||||
"The constructor initialized the tweet with incorrect text");
|
||||
assertEquals(nils, tweet.getOwner(),
|
||||
"The constructor initialized the tweet with the wrong owner");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that retweet has the same text but a different owner")
|
||||
public void constructorRetweet() {
|
||||
retweet1 = new Tweet(ole, tweet);
|
||||
assertEquals("Kvitre!", retweet1.getText());
|
||||
assertEquals(ole, retweet1.getOwner());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
new Tweet(nils, tweet);
|
||||
}, "A person should not be able to retweet themselves");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the original tweet is always correct")
|
||||
public void getOriginalTweet() {
|
||||
assertNull(tweet.getOriginalTweet());
|
||||
|
||||
retweet1 = new Tweet(ole, tweet);
|
||||
assertEquals(tweet, retweet1.getOriginalTweet());
|
||||
assertEquals(retweet1.getOriginalTweet().getText(), retweet1.getText());
|
||||
assertEquals(tweet, retweet1.getOriginalTweet());
|
||||
assertEquals(retweet1.getOriginalTweet().getText(), retweet1.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that retweet count increases when a tweet is retweeted")
|
||||
public void getRetweetCount() {
|
||||
assertEquals(0, tweet.getRetweetCount());
|
||||
|
||||
new Tweet(ole, tweet);
|
||||
assertEquals(1, tweet.getRetweetCount());
|
||||
|
||||
new Tweet(kari, tweet);
|
||||
assertEquals(2, tweet.getRetweetCount());
|
||||
}
|
||||
}
|
||||
152
src/test/java/oving4/twitter/TwitterAccountTest.java
Normal file
152
src/test/java/oving4/twitter/TwitterAccountTest.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package oving4.twitter;
|
||||
|
||||
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 TwitterAccountTest {
|
||||
|
||||
private TwitterAccount nils;
|
||||
private TwitterAccount ole;
|
||||
|
||||
private static void checkFollow(TwitterAccount accountA, TwitterAccount accountB,
|
||||
boolean AfollowsB, boolean BfollowsA) {
|
||||
if (AfollowsB) {
|
||||
assertTrue(accountA.isFollowing(accountB), String.format("%s should have followed %s",
|
||||
accountA.getUserName(), accountB.getUserName()));
|
||||
assertTrue(accountB.isFollowedBy(accountA),
|
||||
String.format("%s should have been followed by %s", accountB.getUserName(),
|
||||
accountA.getUserName()));
|
||||
} else {
|
||||
assertFalse(accountA.isFollowing(accountB),
|
||||
String.format("%s should not have followed %s", accountA.getUserName(),
|
||||
accountB.getUserName()));
|
||||
assertFalse(accountB.isFollowedBy(accountA),
|
||||
String.format("%s should not have been followed by %s", accountB.getUserName(),
|
||||
accountA.getUserName()));
|
||||
}
|
||||
if (BfollowsA) {
|
||||
assertTrue(accountB.isFollowing(accountA), String.format("%s should have followed %s",
|
||||
accountB.getUserName(), accountA.getUserName()));
|
||||
assertTrue(accountA.isFollowedBy(accountB),
|
||||
String.format("%s should have been followed by %s", accountA.getUserName(),
|
||||
accountB.getUserName()));
|
||||
} else {
|
||||
assertFalse(accountB.isFollowing(accountA),
|
||||
String.format("%s should not have followed %s", accountB.getUserName(),
|
||||
accountA.getUserName()));
|
||||
assertFalse(accountA.isFollowedBy(accountB),
|
||||
String.format("%s should not have been followed by %s", accountA.getUserName(),
|
||||
accountB.getUserName()));
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
nils = new TwitterAccount("Nils");
|
||||
ole = new TwitterAccount("Ole");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the constructor initializes correctly")
|
||||
public void testConstructor() {
|
||||
assertEquals("Nils", nils.getUserName());
|
||||
assertEquals(0, nils.getTweetCount());
|
||||
assertEquals("Ole", ole.getUserName());
|
||||
assertEquals(0, ole.getTweetCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that follow is implemented correctly")
|
||||
public void testFollow() {
|
||||
nils.follow(ole);
|
||||
TwitterAccountTest.checkFollow(nils, ole, true, false);
|
||||
|
||||
ole.follow(nils);
|
||||
TwitterAccountTest.checkFollow(nils, ole, true, true);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
nils.follow(nils);
|
||||
}, "Should not be able to follow oneself");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
nils.follow(null);
|
||||
}, "Should not be able to follow null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnfollow() {
|
||||
TwitterAccountTest.checkFollow(nils, ole, false, false);
|
||||
|
||||
nils.follow(ole);
|
||||
TwitterAccountTest.checkFollow(nils, ole, true, false);
|
||||
|
||||
nils.unfollow(ole);
|
||||
TwitterAccountTest.checkFollow(nils, ole, false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewTweet() {
|
||||
nils.tweet("Kvitre!");
|
||||
assertEquals(1, nils.getTweetCount(), "The tweet count of Nils should be 1");
|
||||
assertEquals("Kvitre!", nils.getTweet(1).getText(), "The text should have been 'Kvitre'");
|
||||
|
||||
nils.tweet("Kvitre igjen!");
|
||||
assertEquals(2, nils.getTweetCount());
|
||||
assertEquals("Kvitre igjen!", nils.getTweet(1).getText());
|
||||
assertEquals("Kvitre!", nils.getTweet(2).getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIllegalTweet() {
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
nils.getTweet(1);
|
||||
}, "Should not be able to get a tweet that does not exist");
|
||||
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
nils.getTweet(-1);
|
||||
}, "Should not be able to get a tweet that does not exist");
|
||||
|
||||
nils.tweet("Kvitre!");
|
||||
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
nils.getTweet(2);
|
||||
}, "Should not be able to get a tweet that does not exist");
|
||||
|
||||
assertThrows(RuntimeException.class, () -> {
|
||||
nils.getTweet(-1);
|
||||
}, "Should not be able to get a tweet that does not exist");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the retweet count is correct, also when retweeting a retweet")
|
||||
public void testRetweet() {
|
||||
TwitterAccount kari = new TwitterAccount("Kari");
|
||||
|
||||
nils.tweet("Kvitre!");
|
||||
assertEquals(1, nils.getTweetCount());
|
||||
assertEquals("Kvitre!", nils.getTweet(1).getText());
|
||||
|
||||
ole.retweet(nils.getTweet(1));
|
||||
assertEquals(1, nils.getTweetCount());
|
||||
assertEquals(1, nils.getRetweetCount());
|
||||
assertEquals(1, ole.getTweetCount());
|
||||
assertEquals(0, ole.getRetweetCount());
|
||||
assertEquals("Kvitre!", ole.getTweet(1).getText());
|
||||
assertEquals(nils.getTweet(1), ole.getTweet(1).getOriginalTweet());
|
||||
|
||||
kari.retweet(ole.getTweet(1));
|
||||
assertEquals(1, nils.getTweetCount());
|
||||
assertEquals(2, nils.getRetweetCount());
|
||||
assertEquals(1, ole.getTweetCount());
|
||||
assertEquals(0, ole.getRetweetCount());
|
||||
assertEquals(1, kari.getTweetCount());
|
||||
assertEquals(0, kari.getRetweetCount());
|
||||
assertEquals("Kvitre!", kari.getTweet(1).getText());
|
||||
assertEquals(nils.getTweet(1), kari.getTweet(1).getOriginalTweet());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user