From e86ce341de55cd55d69c4545e4d1c19a0a7776f4 Mon Sep 17 00:00:00 2001 From: Georg Reisinger Date: Mon, 19 Oct 2020 12:34:56 +0200 Subject: [PATCH] JUnit verbessert --- src/main/java/Card.java | 2 +- src/main/java/Cards.java | 24 ++++----- src/main/java/Credentials.java | 28 +++++----- src/main/java/DBConnection.java | 34 ++++++------ src/main/java/Stack.java | 14 ++--- src/main/java/TradingDeal.java | 13 +++++ src/main/java/User.java | 14 ++--- src/test/java/CardTest.java | 6 +-- src/test/java/CardsTest.java | 42 ++++++++++++--- src/test/java/StackTest.java | 2 +- src/test/java/StoreTest.java | 95 ++++++++++++++++++++------------- src/test/java/UserTest.java | 12 ++--- 12 files changed, 178 insertions(+), 108 deletions(-) diff --git a/src/main/java/Card.java b/src/main/java/Card.java index d74cfbf..ca787b6 100644 --- a/src/main/java/Card.java +++ b/src/main/java/Card.java @@ -83,7 +83,7 @@ public class Card { * @param neuerName Name der Card */ public void setName(String neuerName) { - this.name = name; + this.name = neuerName; } /** diff --git a/src/main/java/Cards.java b/src/main/java/Cards.java index 65f2447..1a2bbec 100644 --- a/src/main/java/Cards.java +++ b/src/main/java/Cards.java @@ -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; + } } } diff --git a/src/main/java/Credentials.java b/src/main/java/Credentials.java index 01818c8..8a9e7e0 100644 --- a/src/main/java/Credentials.java +++ b/src/main/java/Credentials.java @@ -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; +// } } diff --git a/src/main/java/DBConnection.java b/src/main/java/DBConnection.java index 3e734b1..3c69e29 100644 --- a/src/main/java/DBConnection.java +++ b/src/main/java/DBConnection.java @@ -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 deckArrList = new ArrayList(); + ArrayList stackArrList = new ArrayList(); + 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(); + this.packages = new ArrayList(); + 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); +// } } diff --git a/src/main/java/Stack.java b/src/main/java/Stack.java index af4f858..6b0be43 100644 --- a/src/main/java/Stack.java +++ b/src/main/java/Stack.java @@ -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); +// } } diff --git a/src/main/java/TradingDeal.java b/src/main/java/TradingDeal.java index 80e377f..4f0c8a5 100644 --- a/src/main/java/TradingDeal.java +++ b/src/main/java/TradingDeal.java @@ -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; } } diff --git a/src/main/java/User.java b/src/main/java/User.java index 6edcfae..bf37bc9 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -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 buyPackage(Package mypackage) throws NegativAmountException { - ArrayList availablePackages = db.getAllPackages(); + ArrayList 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 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 getPossibleTradingDeals(Card toTrade) { - ArrayList storeList = db.getAllTradingDeals(); + ArrayList storeList = new DBConnection().getAllTradingDeals(); ArrayList possible = new ArrayList(); storeList.forEach(item -> { if(item.cardOk(toTrade)){ + System.out.println("ADDED ein item"); possible.add(item); } }); diff --git a/src/test/java/CardTest.java b/src/test/java/CardTest.java index 9784c07..3e000f2 100644 --- a/src/test/java/CardTest.java +++ b/src/test/java/CardTest.java @@ -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() { diff --git a/src/test/java/CardsTest.java b/src/test/java/CardsTest.java index 6584e35..578f8e8 100644 --- a/src/test/java/CardsTest.java +++ b/src/test/java/CardsTest.java @@ -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 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(); cardsArrayList2 = new ArrayList(); cardsArrayList3 = new ArrayList(); 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 diff --git a/src/test/java/StackTest.java b/src/test/java/StackTest.java index 1ad402b..c81b096 100644 --- a/src/test/java/StackTest.java +++ b/src/test/java/StackTest.java @@ -42,6 +42,6 @@ public class StackTest { @Test void test_getDeck() { Cards result = stack.getDeck(); - assertTrue(result.equals(cards)); + assertTrue(result.equals(nochNewCards)); } } diff --git a/src/test/java/StoreTest.java b/src/test/java/StoreTest.java index 0df2404..64df1b1 100644 --- a/src/test/java/StoreTest.java +++ b/src/test/java/StoreTest.java @@ -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(); cardsArrayList2 = new ArrayList(); @@ -48,28 +48,41 @@ public class StoreTest { @Test void test_addTradingDeal() { - user.addTradingDeal(tradingDeal); - ArrayList expected = new ArrayList(); - 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 deckArrList = new ArrayList(); + ArrayList stackArrList = new ArrayList(); + 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 tradingDeals = new ArrayList(); + ArrayList packages = new ArrayList(); + 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 result = user.getTradingDeals(); ArrayList expected = new ArrayList(); + 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 deckArrList = new ArrayList(); + ArrayList stackArrList = new ArrayList(); + 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 tradingDeals = new ArrayList(); + ArrayList packages = new ArrayList(); + 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 result = user.getPossibleTradingDeals(testCard); ArrayList expected = new ArrayList(); 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 deckArrList = new ArrayList(); + ArrayList stackArrList = new ArrayList(); + 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())); } } diff --git a/src/test/java/UserTest.java b/src/test/java/UserTest.java index bb81ea5..2d8b613 100644 --- a/src/test/java/UserTest.java +++ b/src/test/java/UserTest.java @@ -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(){