JUnit verbessert

This commit is contained in:
Georg Reisinger 2020-10-19 12:34:56 +02:00
parent 95873d7e6d
commit e86ce341de
12 changed files with 178 additions and 108 deletions

View File

@ -83,7 +83,7 @@ public class Card {
* @param neuerName Name der Card
*/
public void setName(String neuerName) {
this.name = name;
this.name = neuerName;
}
/**

View File

@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@ -15,12 +16,7 @@ public class Cards {
this.cards = cardsArrayList;
}
/**
* Erstellt ein neues Obj ohne Cards
*/
public Cards() {
}
/**
* Fügt eine neue Card hinzu
@ -56,6 +52,8 @@ public class Cards {
this.cards.forEach(item -> {
if(item.getElementTyp().equals(toCeck.getElementTyp()) && item.getCardType().equals(toCeck.getCardType()) && item.getName().equals(toCeck.getName()) && item.getDamage() == toCeck.getDamage()){
returnval.set(true);
}else{
returnval.set(false);
}
});
return returnval.get();
@ -67,12 +65,14 @@ public class Cards {
* @return True wenn es aus den selben Cards besteht
*/
public boolean equals(Cards toCompare){
AtomicBoolean returnval = new AtomicBoolean(false);
this.cards.forEach(item -> {
if(toCompare.containsCard(item)){
returnval.set(true);
}
});
return returnval.get();
if (this.cards == null && toCompare.getCards() == null){
return true;
}else if ((this.cards == null && toCompare.getCards() != null) || (this.cards != null && toCompare.getCards() == null)){
return false;
}else if(this.cards.containsAll(toCompare.getCards()) && toCompare.getCards().containsAll(this.cards)){
return true;
}else {
return false;
}
}
}

View File

@ -22,13 +22,13 @@ public class Credentials {
return passwort;
}
/**
* Setzt das User Passwort
* @param passwort Neues Passwort
*/
public void setPasswort(String passwort) {
this.passwort = passwort;
}
// /**
// * Setzt das User Passwort
// * @param passwort Neues Passwort
// */
// public void setPasswort(String passwort) {
// this.passwort = passwort;
// }
/**
* Holt den Usernamen des Users
@ -38,11 +38,11 @@ public class Credentials {
return username;
}
/**
* Setzt einen neuen Usernamen
* @param username Neuer Username
*/
public void setUsername(String username) {
this.username = username;
}
// /**
// * Setzt einen neuen Usernamen
// * @param username Neuer Username
// */
// public void setUsername(String username) {
// this.username = username;
// }
}

View File

@ -18,15 +18,19 @@ public class DBConnection {
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
Cards deck = new Cards();
Cards stack = new Cards();
deck.addCard(c1);
deck.addCard(c2);
stack.addCard(c3);
stack.addCard(c4);
stack.addCard(c5);
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
this.tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.SPELL));
this.tradingDeals = new ArrayList<TradingDeal>();
this.packages = new ArrayList<Package>();
this.tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
this.packages.add(new Package(stack, "Test Package", 100));
}
@ -54,11 +58,11 @@ public class DBConnection {
this.tradingDeals.add(tradingDeal);
}
/**
* Fügt ein neues Package zu dem Store hinzu
* @param packageDeal Das neue Package
*/
public void addPackage(Package packageDeal){
this.packages.add(packageDeal);
}
// /**
// * Fügt ein neues Package zu dem Store hinzu
// * @param packageDeal Das neue Package
// */
// public void addPackage(Package packageDeal){
// this.packages.add(packageDeal);
// }
}

View File

@ -38,11 +38,11 @@ public class Stack extends Cards{
this.deck.delCard(toDel);
}
/**
* Fügt dem deck eine neue Card ein
* @param toAdd Neue Card
*/
public void addDeckCard(Card toAdd){
this.deck.addCard(toAdd);
}
// /**
// * Fügt dem deck eine neue Card ein
// * @param toAdd Neue Card
// */
// public void addDeckCard(Card toAdd){
// this.deck.addCard(toAdd);
// }
}

View File

@ -119,9 +119,22 @@ public class TradingDeal {
* @return True wenn der Deal passt
*/
public boolean cardOk(Card checkCard){
System.out.println(checkCard.getCardType().equals(this.requiredCardType));
System.out.println(checkCard.getCardType().toString());
System.out.println(this.requiredCardType.toString());
System.out.println(" ");
System.out.println(checkCard.getElementTyp().equals(this.requiredElementType));
System.out.println(checkCard.getElementTyp().toString());
System.out.println(this.requiredElementType);
System.out.println(" ");
System.out.println(checkCard.getDamage() >= this.requiredMinDamage);
System.out.println(checkCard.getDamage());
System.out.println(this.requiredMinDamage);
if(checkCard.getCardType().equals(this.requiredCardType) && checkCard.getElementTyp().equals(this.requiredElementType) && checkCard.getDamage() >= this.requiredMinDamage){
System.out.println("Da simma true");
return true;
}else {
System.out.println("da simma false");
return false;
}
}

View File

@ -26,7 +26,7 @@ public class User implements Store{
this.email = email;
this.stack = stack;
this.coins = coins;
this.db = new DBConnection();
//this.db = new DBConnection();
}
/**
@ -50,7 +50,7 @@ public class User implements Store{
* @return Das DB Obj
*/
public DBConnection getDb() {
return db;
return new DBConnection();
}
/**
@ -150,7 +150,7 @@ public class User implements Store{
* @throws NegativAmountException Wenn ein negativer Preis im Package gesetzt wird.
*/
public ArrayList<Card> buyPackage(Package mypackage) throws NegativAmountException {
ArrayList<Package> availablePackages = db.getAllPackages();
ArrayList<Package> availablePackages = new DBConnection().getAllPackages();
availablePackages.removeIf(obj -> obj.equals(mypackage));
this.coins.removeCoin(mypackage.getPrice());
return mypackage.getCards();
@ -161,7 +161,7 @@ public class User implements Store{
* @param tradingDeal Der neue Tradingdeal
*/
public void addTradingDeal(TradingDeal tradingDeal) {
db.addTradingDeal(tradingDeal);
new DBConnection().addTradingDeal(tradingDeal);
}
/**
@ -169,7 +169,7 @@ public class User implements Store{
* @return Alle verfügbaren Deals
*/
public ArrayList<TradingDeal> getTradingDeals() {
return db.getAllTradingDeals();
return new DBConnection().getAllTradingDeals();
}
/**
@ -182,6 +182,7 @@ public class User implements Store{
if (storeTradingDeal.dealOk(myTradingDeal)){
stack.delCard(myTradingDeal.getCardToTrade());
stack.addCard(storeTradingDeal.getCardToTrade());
return true;
}
return false;
}
@ -193,10 +194,11 @@ public class User implements Store{
*/
@Override
public ArrayList<TradingDeal> getPossibleTradingDeals(Card toTrade) {
ArrayList<TradingDeal> storeList = db.getAllTradingDeals();
ArrayList<TradingDeal> storeList = new DBConnection().getAllTradingDeals();
ArrayList<TradingDeal> possible = new ArrayList<TradingDeal>();
storeList.forEach(item -> {
if(item.cardOk(toTrade)){
System.out.println("ADDED ein item");
possible.add(item);
}
});

View File

@ -18,20 +18,20 @@ public class CardTest {
public void test_getElementType() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
ElementTyp elementTyp = card.getElementTyp();
assertTrue(elementTyp != ElementTyp.WATER);
assertTrue(elementTyp == ElementTyp.WATER);
}
@Test
public void test_getCardType() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
CardType cardType = card.getCardType();
assertTrue(cardType != CardType.SPELL);
assertTrue(cardType == CardType.SPELL);
}
@Test
public void test_getEffectivenessAgainst() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
Card card2 = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
EffectivnessType effectivnessType = card.getEffectivenessAgainst(card2);
assertTrue(effectivnessType != EffectivnessType.NOEFFECT);
assertTrue(effectivnessType == EffectivnessType.NOEFFECT);
}
@Test
public void test_setName() {

View File

@ -2,38 +2,66 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CardsTest {
private Cards newCards, cards, nochNewCards;
private Card newCard, card;
private Cards cards, cards2, cards3;
private Card newCard, card, newerCard;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
private Stack stack;
@BeforeEach
void setUp() {
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
newerCard = new Card("NameNewerer", 100, ElementTyp.FIRE, CardType.MONSTER);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cardsArrayList2.add(card);
cardsArrayList3.add(newerCard);
cards = new Cards(cardsArrayList);
newCards = new Cards(cardsArrayList2);
nochNewCards = new Cards(cardsArrayList3);
stack = new Stack(cards, nochNewCards);
cards2 = new Cards(cardsArrayList2);
cards3 = new Cards(cardsArrayList3);
}
@Test
void test_addCard() {
System.out.println(cards.getCards().toString());
cards.addCard(newCard);
assertTrue(cards.getCards().equals(cardsArrayList2));
System.out.println(cards.getCards().toString());
System.out.println(cards2.getCards().toString());
assertTrue(cards.equals(cards2));
}
@Test
void test_addCard2() {
cards.addCard(newCard);
assertFalse(cards.equals(cards3));
}
@Test
void test_equals() {
assertFalse(cards.equals(cards2));
}
@Test
void test_contains() {
assertTrue(cards2.containsCard(card));
}
@Test
void test_contains2() {
assertFalse(cards.containsCard(newCard));
}
@Test
void test_delCard() {
cards.addCard(newCard);
cards.delCard(newCard);
assertTrue(cards.getCards().equals(cardsArrayList));
}
@Test

View File

@ -42,6 +42,6 @@ public class StackTest {
@Test
void test_getDeck() {
Cards result = stack.getDeck();
assertTrue(result.equals(cards));
assertTrue(result.equals(nochNewCards));
}
}

View File

@ -18,7 +18,7 @@ public class StoreTest {
@BeforeEach
void setUp() {
//Stack
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
card = new Card("Name", 100, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
@ -48,28 +48,41 @@ public class StoreTest {
@Test
void test_addTradingDeal() {
user.addTradingDeal(tradingDeal);
ArrayList<TradingDeal> expected = new ArrayList<TradingDeal>();
expected.add(tradingDeal);
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
Cards deck = new Cards();
Cards stack = new Cards();
deck.addCard(c1);
deck.addCard(c2);
stack.addCard(c3);
stack.addCard(c4);
stack.addCard(c5);
TradingDeal vonDB = new TradingDeal(new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000)), c1, ElementTyp.FIRE, 10, CardType.SPELL);
expected.add(vonDB);
assertTrue(user.getTradingDeals().equals(expected));
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
ArrayList<TradingDeal> tradingDeals = new ArrayList<TradingDeal>();
ArrayList<Package> packages = new ArrayList<Package>();
tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
packages.add(new Package(stack, "Test Package", 100));
System.out.println(user.getTradingDeals().toString());
user.addTradingDeal(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
System.out.println(user.getTradingDeals().toString());
System.out.println(tradingDeals.toString());
assertTrue(user.getTradingDeals().get(0).dealOk(tradingDeals.get(0)));
}
@Test
void test_doTrade() {
Card cardtest = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card cardtest = new Card("Name", 100, ElementTyp.FIRE, CardType.MONSTER);
TradingDeal tradingDealtest = new TradingDeal(user, cardtest, ElementTyp.WATER, 100, CardType.MONSTER);
boolean result = user.doTrading(tradingDeal, tradingDealtest);
assertTrue(result);
@ -78,27 +91,33 @@ public class StoreTest {
void test_getTradingDeals() {
ArrayList<TradingDeal> result = user.getTradingDeals();
ArrayList<TradingDeal> expected = new ArrayList<TradingDeal>();
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
Cards deck = new Cards();
Cards stack = new Cards();
deck.addCard(c1);
deck.addCard(c2);
stack.addCard(c3);
stack.addCard(c4);
stack.addCard(c5);
TradingDeal vonDB = new TradingDeal(new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000)), c1, ElementTyp.FIRE, 10, CardType.SPELL);
expected.add(vonDB);
expected.add(tradingDeal);
assertTrue(result.equals(expected));
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
ArrayList<TradingDeal> tradingDeals = new ArrayList<TradingDeal>();
ArrayList<Package> packages = new ArrayList<Package>();
tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
packages.add(new Package(stack, "Test Package", 100));
assertTrue(result.get(0).dealOk(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER)));
}
@Test
void test_getPossibleTradingDeals() {
Card testCard = new Card("Test", 15, ElementTyp.FIRE, CardType.SPELL);
Card testCard = new Card("Test", 10, ElementTyp.FIRE, CardType.MONSTER);
ArrayList<TradingDeal> result = user.getPossibleTradingDeals(testCard);
ArrayList<TradingDeal> expected = new ArrayList<TradingDeal>();
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
@ -106,15 +125,19 @@ public class StoreTest {
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
Cards deck = new Cards();
Cards stack = new Cards();
deck.addCard(c1);
deck.addCard(c2);
stack.addCard(c3);
stack.addCard(c4);
stack.addCard(c5);
TradingDeal vonDB = new TradingDeal(new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000)), c1, ElementTyp.FIRE, 10, CardType.SPELL);
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
TradingDeal vonDB = new TradingDeal(new User(new Credentials("username", "passwort"), "DerName", "Nachname", "Email", new Stack(stack, deck), new Coins(1000)), c1, ElementTyp.FIRE, 10, CardType.SPELL);
expected.add(vonDB);
assertTrue(result.equals(expected));
System.out.println(result.get(0).getCardToTrade().getName());
System.out.println(expected.get(0).getCardToTrade().getName());
assertTrue(result.get(0).getCardToTrade().equals(expected.get(0).getCardToTrade()));
}
}

View File

@ -41,17 +41,17 @@ public class UserTest {
@Test
public void test_getName() {
String result = user.getName();
assertTrue(result != "name");
assertTrue(result.equals("name"));
}
@Test
public void test_getNachname() {
String result = user.getNachname();
assertTrue(result != "nachname");
assertTrue(result.equals("nachname"));
}
@Test
public void test_getEmail() {
String result = user.getEmail();
assertTrue(result != "email");
assertTrue(result.equals("email"));
}
@Test
@ -59,21 +59,21 @@ public class UserTest {
String newstring = "new";
user.setName(newstring);
String result = user.getName();
assertTrue(result != newstring);
assertTrue(result.equals(newstring));
}
@Test
public void test_setNachname() {
String newstring = "new";
user.setNachname(newstring);
String result = user.getNachname();
assertTrue(result != newstring);
assertTrue(result.equals(newstring));
}
@Test
public void test_setEmail() {
String newstring = "new";
user.setEmail(newstring);
String result = user.getEmail();
assertTrue(result != newstring);
assertTrue(result.equals(newstring));
}
@Test
public void test_buyPackage(){