From 8cb35b358563e5a81ad9e4aceb123b85222cf4cc Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 14 Aug 2016 18:09:22 +0200 Subject: Implemented some more filtering tests, sorted out validation and exceptions --- src/eu/equalparts/cardbase/card/Card.java | 1 - .../equalparts/cardbase/cardfield/CardField.java | 4 +- .../cardbase/cardfield/IntegerCardField.java | 21 +- .../cardbase/cardfield/StringCardField.java | 14 +- src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 14 +- .../cardbase/comparator/CardComparator.java | 12 +- .../cardbase/filtering/CardFiltering.java | 53 ++- src/eu/equalparts/cardbase/filtering/Filter.java | 16 + .../equalparts/cardbase/json/CardDeserializer.java | 1 - .../equalparts/cardbase/json/CardSerializer.java | 6 - src/eu/equalparts/cardbase/utils/InputSanity.java | 27 -- src/eu/equalparts/cardbase/utils/Utils.java | 15 + test/eu/equalparts/cardbase/CardbaseTest.java | 5 +- .../equalparts/cardbase/cli/CardbaseCLITest.java | 58 ++++ .../containers/StandaloneCardContainerTest.java | 2 +- .../cardbase/filtering/CardFilteringTest.java | 386 +++++++++++++++++++-- .../cardbase/sorting/CardSortingTest.java | 21 -- 17 files changed, 510 insertions(+), 146 deletions(-) create mode 100644 src/eu/equalparts/cardbase/filtering/Filter.java delete mode 100644 src/eu/equalparts/cardbase/utils/InputSanity.java create mode 100644 src/eu/equalparts/cardbase/utils/Utils.java diff --git a/src/eu/equalparts/cardbase/card/Card.java b/src/eu/equalparts/cardbase/card/Card.java index 11cfb77..2c896e8 100644 --- a/src/eu/equalparts/cardbase/card/Card.java +++ b/src/eu/equalparts/cardbase/card/Card.java @@ -25,7 +25,6 @@ public class Card { public IntegerCardField loyalty; public IntegerCardField multiverseid; public StringCardField imageName; - public StringCardField watermark; // Not part of upstream JSON public StringCardField setCode; diff --git a/src/eu/equalparts/cardbase/cardfield/CardField.java b/src/eu/equalparts/cardbase/cardfield/CardField.java index 9af775b..e088577 100644 --- a/src/eu/equalparts/cardbase/cardfield/CardField.java +++ b/src/eu/equalparts/cardbase/cardfield/CardField.java @@ -1,5 +1,5 @@ package eu.equalparts.cardbase.cardfield; -import eu.equalparts.cardbase.filtering.CardFiltering.Filter; +import eu.equalparts.cardbase.filtering.Filter; public abstract class CardField> implements Comparable> { private String name; @@ -27,5 +27,5 @@ public abstract class CardField> implements Comparable { @@ -9,22 +8,18 @@ public class IntegerCardField extends CardField { } @Override - public boolean filter(Filter filter, String s) { - switch (filter) { + public boolean filter(Filter filter) throws NumberFormatException { + switch (filter.type) { case CONTAINS: - return get().toString().contains(s); + return get().toString().contains(filter.value); case EQUALS: - return get().toString().equalsIgnoreCase(s); + return get().toString().equalsIgnoreCase(filter.value); case REGEX: - return get().toString().matches(s); + return get().toString().matches(filter.value); case GREATER_THAN: - if (InputSanity.isInteger(s)) { - return get() > Integer.valueOf(s); - } + return get() > Integer.parseInt(filter.value); case SMALLER_THAN: - if (InputSanity.isInteger(s)) { - return get() < Integer.valueOf(s); - } + return get() < Integer.parseInt(filter.value); default: return false; } diff --git a/src/eu/equalparts/cardbase/cardfield/StringCardField.java b/src/eu/equalparts/cardbase/cardfield/StringCardField.java index 059a45d..8239138 100644 --- a/src/eu/equalparts/cardbase/cardfield/StringCardField.java +++ b/src/eu/equalparts/cardbase/cardfield/StringCardField.java @@ -1,5 +1,5 @@ package eu.equalparts.cardbase.cardfield; -import eu.equalparts.cardbase.filtering.CardFiltering.Filter; +import eu.equalparts.cardbase.filtering.Filter; public class StringCardField extends CardField { @@ -8,16 +8,16 @@ public class StringCardField extends CardField { } @Override - public boolean filter(Filter filter, String s) { - switch (filter) { + public boolean filter(Filter filter) { + switch (filter.type) { case CONTAINS: - return get().toLowerCase().contains(s.toLowerCase()); + return get().toLowerCase().contains(filter.value.toLowerCase()); case EQUALS: - return get().equalsIgnoreCase(s); + return get().equalsIgnoreCase(filter.value); case REGEX: - return get().matches(s); + return get().matches(filter.value); default: - return false; + throw new IllegalArgumentException(); } } } diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index f86365d..5738f5d 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -389,14 +389,14 @@ public final class CardbaseCLI { if (cardToRemove != null) { String count = args.length > 1 ? args[1] : "1"; - if (count.matches("[-]?[0-9]+")) { - Integer intCount = Integer.valueOf(count); + try { + Integer intCount = Integer.parseInt(count); if (intCount > 0) { removeCard(cardToRemove, intCount); } else { System.out.println("Cannot remove " + count + " cards."); } - } else { + } catch (NumberFormatException e) { System.out.println(count + " is not a valid number of cards."); } } else { @@ -430,16 +430,14 @@ public final class CardbaseCLI { if (cardToAdd != null) { String count = args != null && args.length > 0 ? args[0] : "1"; - if (count.matches("[-]?[0-9]+")) { - - Integer intCount = Integer.valueOf(count); - + try { + Integer intCount = Integer.parseInt(count); if (intCount > 0) { addCard(cardToAdd, intCount); } else { System.out.println("Cannot add " + intCount + " cards."); } - } else { + } catch (NumberFormatException e) { System.out.println(count + " is not a valid number of cards."); } } else { diff --git a/src/eu/equalparts/cardbase/comparator/CardComparator.java b/src/eu/equalparts/cardbase/comparator/CardComparator.java index d2cd0c1..31d42ad 100644 --- a/src/eu/equalparts/cardbase/comparator/CardComparator.java +++ b/src/eu/equalparts/cardbase/comparator/CardComparator.java @@ -1,6 +1,5 @@ package eu.equalparts.cardbase.comparator; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Comparator; import java.util.function.BiFunction; @@ -9,6 +8,7 @@ import eu.equalparts.cardbase.card.Card; import eu.equalparts.cardbase.cardfield.CardField; import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; +import eu.equalparts.cardbase.utils.Utils; /** * I'm new to this reflection business, so bear with me. @@ -56,12 +56,10 @@ public class CardComparator implements Comparator { this.fieldToCompare = fieldToCompare; // if annotated with a special comparator, set the comparison delegate here - for (Annotation annotation : fieldToCompare.getAnnotations()) { - if (annotation.annotationType() == DirtyNumber.class) { - this.comparisonDelegate = ComparatorDelegates::compareDirtyNumber; - } else if (annotation.annotationType() == Rarity.class) { - this.comparisonDelegate = ComparatorDelegates::compareRarity; - } + if (Utils.hasAnnotation(fieldToCompare, DirtyNumber.class)) { + this.comparisonDelegate = ComparatorDelegates::compareDirtyNumber; + } else if (Utils.hasAnnotation(fieldToCompare, Rarity.class)) { + this.comparisonDelegate = ComparatorDelegates::compareRarity; } } else { throw new IllegalArgumentException("The field provided is not valid."); diff --git a/src/eu/equalparts/cardbase/filtering/CardFiltering.java b/src/eu/equalparts/cardbase/filtering/CardFiltering.java index 081951f..8da9912 100644 --- a/src/eu/equalparts/cardbase/filtering/CardFiltering.java +++ b/src/eu/equalparts/cardbase/filtering/CardFiltering.java @@ -6,6 +6,10 @@ import java.util.List; import eu.equalparts.cardbase.card.Card; import eu.equalparts.cardbase.cardfield.CardField; +import eu.equalparts.cardbase.cardfield.IntegerCardField; +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; +import eu.equalparts.cardbase.filtering.Filter.FilterType; +import eu.equalparts.cardbase.utils.Utils; /** * Contains and equals are not case sensitive. Regex is. @@ -14,26 +18,51 @@ import eu.equalparts.cardbase.cardfield.CardField; * */ public class CardFiltering { - - public enum Filter { - EQUALS, CONTAINS, REGEX, GREATER_THAN, SMALLER_THAN; - } - - public static void filterByField(List cards, String fieldName, Filter filterType, String filterValue) throws NoSuchFieldException { - Field fieldToFilter = Card.class.getDeclaredField(fieldName); + + public static void filterByField(List cards, Filter filter) throws NoSuchFieldException { + Field fieldToFilter = Card.class.getDeclaredField(filter.fieldName); try { for (Iterator iterator = cards.iterator(); iterator.hasNext();) { - if (!((CardField) fieldToFilter.get(iterator.next())).filter(filterType, filterValue)) { + if (!((CardField) fieldToFilter.get(iterator.next())).filter(filter)) { iterator.remove(); } } - } catch (IllegalArgumentException e) { - System.out.println("Error: class Card does not define field \"" + fieldToFilter.getName() + "\"."); - e.printStackTrace(); } catch (IllegalAccessException e) { System.out.println("Error: field " + fieldToFilter.getName() + " in Card is not visible."); e.printStackTrace(); } } - + + public static boolean isFilterValid(Filter filter) { + // Check the integrity of the filter object + if (filter == null || filter.fieldName == null || filter.value == null) { + return false; + } + + // Check that the specified field exists + Field fieldToFilter; + try { + fieldToFilter = Card.class.getDeclaredField(filter.fieldName); + } catch (NoSuchFieldException | SecurityException e) { + return false; + } + + // If filter is numeric, check that the field and value are valid + if (filter.type == FilterType.GREATER_THAN || filter.type == FilterType.SMALLER_THAN) { + // Check that the field is of type integer + if (!fieldToFilter.getType().equals(IntegerCardField.class) + && !Utils.hasAnnotation(fieldToFilter, DirtyNumber.class)) { + return false; + } + + // Check that the value is an integer + try { + Integer.parseInt(filter.value); + } catch (NumberFormatException e) { + return false; + } + } + + return true; + } } diff --git a/src/eu/equalparts/cardbase/filtering/Filter.java b/src/eu/equalparts/cardbase/filtering/Filter.java new file mode 100644 index 0000000..7beaa09 --- /dev/null +++ b/src/eu/equalparts/cardbase/filtering/Filter.java @@ -0,0 +1,16 @@ +package eu.equalparts.cardbase.filtering; + +public class Filter { + + public enum FilterType { EQUALS, CONTAINS, REGEX, GREATER_THAN, SMALLER_THAN } + + public FilterType type; + public String fieldName, value; + + public Filter(FilterType type, String fieldName, String value) { + this.type = type; + this.fieldName = fieldName; + this.value = value; + } + +} diff --git a/src/eu/equalparts/cardbase/json/CardDeserializer.java b/src/eu/equalparts/cardbase/json/CardDeserializer.java index 7193c53..e140a5f 100644 --- a/src/eu/equalparts/cardbase/json/CardDeserializer.java +++ b/src/eu/equalparts/cardbase/json/CardDeserializer.java @@ -50,7 +50,6 @@ public class CardDeserializer extends StdDeserializer { newCard.loyalty = new IntegerCardField("loyalty", node.hasNonNull("loyalty") ? node.get("loyalty").asInt() : null); newCard.multiverseid = new IntegerCardField("multiverseid", node.hasNonNull("multiverseid") ? node.get("multiverseid").asInt() : null); newCard.imageName = new StringCardField("imageName", node.hasNonNull("imageName") ? node.get("imageName").asText() : null); - newCard.watermark = new StringCardField("watermark", node.hasNonNull("watermark") ? node.get("watermark").asText() : null); newCard.setCode = new StringCardField("setCode", node.hasNonNull("setCode") ? node.get("setCode").asText() : null); return newCard; diff --git a/src/eu/equalparts/cardbase/json/CardSerializer.java b/src/eu/equalparts/cardbase/json/CardSerializer.java index 5699061..7eaa0cb 100644 --- a/src/eu/equalparts/cardbase/json/CardSerializer.java +++ b/src/eu/equalparts/cardbase/json/CardSerializer.java @@ -103,12 +103,6 @@ public class CardSerializer extends JsonSerializer { jGen.writeNullField("imageName"); } - if (card.watermark.get() != null) { - jGen.writeStringField("watermark", card.watermark.get()); - } else { - jGen.writeNullField("watermark"); - } - if (card.setCode.get() != null) { jGen.writeStringField("setCode", card.setCode.get()); } else { diff --git a/src/eu/equalparts/cardbase/utils/InputSanity.java b/src/eu/equalparts/cardbase/utils/InputSanity.java deleted file mode 100644 index 0571c73..0000000 --- a/src/eu/equalparts/cardbase/utils/InputSanity.java +++ /dev/null @@ -1,27 +0,0 @@ -package eu.equalparts.cardbase.utils; - -public class InputSanity { - public static boolean isInteger(String str) { - if (str == null) { - return false; - } - int length = str.length(); - if (length == 0) { - return false; - } - int i = 0; - if (str.charAt(0) == '-') { - if (length == 1) { - return false; - } - i = 1; - } - for (; i < length; i++) { - char c = str.charAt(i); - if (c < '0' || c > '9') { - return false; - } - } - return true; - } -} diff --git a/src/eu/equalparts/cardbase/utils/Utils.java b/src/eu/equalparts/cardbase/utils/Utils.java new file mode 100644 index 0000000..bb88397 --- /dev/null +++ b/src/eu/equalparts/cardbase/utils/Utils.java @@ -0,0 +1,15 @@ +package eu.equalparts.cardbase.utils; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + +public class Utils { + public static boolean hasAnnotation(Field fieldToAnalyse, Class annotation) { + for (Annotation a : fieldToAnalyse.getAnnotations()) { + if (a.annotationType().equals(annotation)) { + return true; + } + } + return false; + } +} diff --git a/test/eu/equalparts/cardbase/CardbaseTest.java b/test/eu/equalparts/cardbase/CardbaseTest.java index 12c4842..f9ca0d8 100644 --- a/test/eu/equalparts/cardbase/CardbaseTest.java +++ b/test/eu/equalparts/cardbase/CardbaseTest.java @@ -184,8 +184,5 @@ public class CardbaseTest { /*********************************************************************************** * Deck tests ***********************************************************************************/ - @Test - public void test() throws Exception { - - } + } diff --git a/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java b/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java index 9a98469..87755db 100644 --- a/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java +++ b/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java @@ -860,6 +860,36 @@ public class CardbaseCLITest { assertEquals("Please select a set before removing cards." + EOL, testOutput.toString()); } + @Test + public void removeIntegerOverflowAmount() throws Exception { + uut = new CardbaseCLI(TEST_REMOTE, path("/testbase.cb")); + uut.interpretInput("set FRF"); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("remove 129 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"); + } finally { + System.setOut(console); + } + + assertEquals("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 is not a valid number of cards." + EOL, testOutput.toString()); + } + + @Test + public void removeNonIntAmount() throws Exception { + uut = new CardbaseCLI(TEST_REMOTE, path("/testbase.cb")); + uut.interpretInput("set FRF"); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("remove 129 1OO"); + } finally { + System.setOut(console); + } + + assertEquals("1OO is not a valid number of cards." + EOL, testOutput.toString()); + } + /*********************************************************************************** * add() tests, happy path ***********************************************************************************/ @@ -1252,6 +1282,34 @@ public class CardbaseCLITest { assertEquals("Total: 0" + EOL, testOutput.toString()); } + @Test + public void addIntegerOverflowAmount() throws Exception { + uut.interpretInput("set FRF"); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("129 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"); + } finally { + System.setOut(console); + } + + assertEquals("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 is not a valid number of cards." + EOL, testOutput.toString()); + } + + @Test + public void addNonIntAmount() throws Exception { + uut.interpretInput("set FRF"); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("129 1OO"); + } finally { + System.setOut(console); + } + + assertEquals("1OO is not a valid number of cards." + EOL, testOutput.toString()); + } + /*********************************************************************************** * undo() tests, happy path ***********************************************************************************/ diff --git a/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java index c420ee1..62f8dd7 100644 --- a/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java +++ b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java @@ -68,7 +68,7 @@ public class StandaloneCardContainerTest { exception.expect(NullPointerException.class); uut.addCard(null, 0); } - + /*********************************************************************************** * Removing card tests, happy path ***********************************************************************************/ diff --git a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java index 8e9d00b..ed3d3f2 100644 --- a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java +++ b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java @@ -15,7 +15,8 @@ import org.junit.rules.ExpectedException; import com.fasterxml.jackson.core.type.TypeReference; import eu.equalparts.cardbase.card.Card; -import eu.equalparts.cardbase.filtering.CardFiltering.Filter; +import eu.equalparts.cardbase.filtering.Filter; +import eu.equalparts.cardbase.filtering.Filter.FilterType; import eu.equalparts.cardbase.json.JSON; public class CardFilteringTest { @@ -34,9 +35,14 @@ public class CardFilteringTest { testCards = new LinkedList<>(allTestCards); } + /* + * Filter tests, happy path + */ @Test public void filterByNameEquals() throws Exception { - CardFiltering.filterByField(testCards, "name", Filter.EQUALS, "Callow jushi"); + Filter filter = new Filter(FilterType.EQUALS, "name", "Callow jushi"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name.get()); @@ -44,7 +50,9 @@ public class CardFilteringTest { @Test public void filterByNameContains() throws Exception { - CardFiltering.filterByField(testCards, "name", Filter.CONTAINS, "sh"); + Filter filter = new Filter(FilterType.CONTAINS, "name", "sh"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; @@ -61,7 +69,9 @@ public class CardFilteringTest { @Test public void filterByNameRegex() throws Exception { - CardFiltering.filterByField(testCards, "name", Filter.REGEX, ".+?n"); + Filter filter = new Filter(FilterType.REGEX, "name", ".+?n"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 2, testCards.size()); int i = 0; @@ -77,29 +87,25 @@ public class CardFilteringTest { @Test public void filterByNameGreaterThan() throws Exception { - CardFiltering.filterByField(testCards, "name", Filter.GREATER_THAN, "5"); + Filter filter = new Filter(FilterType.GREATER_THAN, "name", "0"); + exception.expect(IllegalArgumentException.class); - assertEquals("Wrong list size.", 8, testCards.size()); - int i = 0; - String[] names = { - "Callow Jushi", - "Coerced Confession", - "Khalni Hydra", - "Nightmare", - "Shivan Dragon", - "Disrupting Shoal", - "Sorin Markov", - "Ugin's Construct", - }; - for (Card card : testCards) { - assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); - i++; - } + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByNameSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "name", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); } @Test public void filterByLayoutEquals() throws Exception { - CardFiltering.filterByField(testCards, "layout", Filter.EQUALS, "flip"); + Filter filter = new Filter(FilterType.EQUALS, "layout", "flip"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name.get()); @@ -107,7 +113,9 @@ public class CardFilteringTest { @Test public void filterByLayoutContains() throws Exception { - CardFiltering.filterByField(testCards, "layout", Filter.CONTAINS, "l"); + Filter filter = new Filter(FilterType.CONTAINS, "layout", "l"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 8, testCards.size()); int i = 0; @@ -129,15 +137,35 @@ public class CardFilteringTest { @Test public void filterByLayoutRegex() throws Exception { - CardFiltering.filterByField(testCards, "layout", Filter.REGEX, "fl[a-z]p"); + Filter filter = new Filter(FilterType.REGEX, "layout", "fl[a-z]p"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name.get()); } + @Test + public void filterByLayoutGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "layout", "5"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByLayoutSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "layout", "5"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + @Test public void filterByManaCostEquals() throws Exception { - CardFiltering.filterByField(testCards, "manaCost", Filter.EQUALS, "{X}{U}{U}"); + Filter filter = new Filter(FilterType.EQUALS, "manaCost", "{X}{U}{U}"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Disrupting Shoal", testCards.get(0).name.get()); @@ -145,7 +173,9 @@ public class CardFilteringTest { @Test public void filterByManaCostContains() throws Exception { - CardFiltering.filterByField(testCards, "manaCost", Filter.CONTAINS, "B"); + Filter filter = new Filter(FilterType.CONTAINS, "manaCost", "B"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; @@ -162,15 +192,35 @@ public class CardFilteringTest { @Test public void filterByManaCostRegex() throws Exception { - CardFiltering.filterByField(testCards, "manaCost", Filter.REGEX, "(\\{G\\}){8}"); + Filter filter = new Filter(FilterType.REGEX, "manaCost", "(\\{G\\}){8}"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Khalni Hydra", testCards.get(0).name.get()); } + @Test + public void filterByManaCostGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "manaCost", "5"); + + exception.expect(IllegalArgumentException.class); + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByManaCostSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "manaCost", "5"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + @Test public void filterByCMCEquals() throws Exception { - CardFiltering.filterByField(testCards, "cmc", Filter.EQUALS, "5"); + Filter filter = new Filter(FilterType.EQUALS, "cmc", "5"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Coerced Confession", testCards.get(0).name.get()); @@ -178,7 +228,9 @@ public class CardFilteringTest { @Test public void filterByCMCContains() throws Exception { - CardFiltering.filterByField(testCards, "cmc", Filter.CONTAINS, "5"); + Filter filter = new Filter(FilterType.CONTAINS, "cmc", "5"); + + CardFiltering.filterByField(testCards, filter); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Coerced Confession", testCards.get(0).name.get()); @@ -186,17 +238,194 @@ public class CardFilteringTest { @Test public void filterByCMCRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "cmc", "5"); + CardFiltering.filterByField(testCards, filter); } - + @Test - public void filterByType() throws Exception { - + public void filterByCMCGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "cmc", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 4, testCards.size()); + int i = 0; + String[] names = { + "Khalni Hydra", + "Nightmare", + "Shivan Dragon", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByCMCSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "cmc", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Disrupting Shoal", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } } @Test - public void filterByRarity() throws Exception { + public void filterByTypeEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "type", "Sorcery"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Coerced Confession", testCards.get(0).name.get()); + } + + @Test + public void filterByTypeContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "type", "Creature"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 5, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Khalni Hydra", + "Nightmare", + "Shivan Dragon", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByTypeRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "type", "[^ ]+? —.*"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 6, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Khalni Hydra", + "Nightmare", + "Shivan Dragon", + "Disrupting Shoal", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByTypeGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "type", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByTypeSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "name", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + @Test + public void filterByRarityEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "rarity", "Mythic Rare"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Khalni Hydra", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByRarityContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "rarity", "Rare"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 5, testCards.size()); + int i = 0; + String[] names = { + "Khalni Hydra", + "Nightmare", + "Shivan Dragon", + "Disrupting Shoal", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByRarityRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "rarity", "[^ ]+"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 6, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Coerced Confession", + "Nightmare", + "Shivan Dragon", + "Disrupting Shoal", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByRarityGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "rarity", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByRaritySmallerThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "rarity", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); } @Test @@ -245,12 +474,97 @@ public class CardFilteringTest { } @Test - public void filterByWatermark() throws Exception { + public void filterBySetCode() throws Exception { } + + /* + * Filter validation tests, happy path + */ + @Test + public void validateNameEqualsSomethingFilter() throws Exception { + Filter testFilter = new Filter(FilterType.EQUALS, "name", "Card name"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } @Test - public void filterBySetCode() throws Exception { - + public void validateFlavourEqualsNothingFilter() throws Exception { + Filter testFilter = new Filter(FilterType.EQUALS, "flavor", ""); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateTextContainsVigilanceFilter() throws Exception { + Filter testFilter = new Filter(FilterType.CONTAINS, "text", "vigilance"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateCMCContainsNumberFilter() throws Exception { + Filter testFilter = new Filter(FilterType.CONTAINS, "cmc", "5"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateTypeRegexFilter() throws Exception { + Filter testFilter = new Filter(FilterType.REGEX, "type", "Legendary [A-z]+? Creature.*"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateManaCostRegexFilter() throws Exception { + Filter testFilter = new Filter(FilterType.REGEX, "manaCost", ".*?{./.}.*?"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateCMCGreaterThanFilter() throws Exception { + Filter testFilter = new Filter(FilterType.GREATER_THAN, "cmc", "5"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validatePowerGreaterThanFilter() throws Exception { + Filter testFilter = new Filter(FilterType.GREATER_THAN, "power", "3"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateLoyaltySmallerThanFilter() throws Exception { + Filter testFilter = new Filter(FilterType.SMALLER_THAN, "loyalty", "3"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateToughnessSmallerThanFilter() throws Exception { + Filter testFilter = new Filter(FilterType.SMALLER_THAN, "toughness", "5"); + + assertTrue(CardFiltering.isFilterValid(testFilter)); + } + + /* + * Edge cases + */ + @Test + public void validateNonIntegerFieldGreaterThanFilter() throws Exception { + Filter testFilter = new Filter(FilterType.GREATER_THAN, "name", "5"); + + assertTrue(!CardFiltering.isFilterValid(testFilter)); + } + + @Test + public void validateNonIntegerValueGreaterThanFilter() throws Exception { + Filter testFilter = new Filter(FilterType.GREATER_THAN, "cmc", "test"); + + assertTrue(!CardFiltering.isFilterValid(testFilter)); } } diff --git a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java index 2e4f0e6..d576726 100644 --- a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java +++ b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java @@ -309,27 +309,6 @@ public class CardSortingTest { } } - @Test - public void sortByWatermark() throws Exception { - CardSorting.sortByField(testCards, "watermark"); - int i = 0; - String[] watermarks = { - "", - "", - "", - "", - "", - "", - "", - "Dimir", - }; - for (Card card : testCards) { - String watermark = card.watermark.get() != null ? card.watermark.get() : ""; - assertTrue(watermark + " should have been " + watermarks[i] + ", i = " + i, watermark.equals(watermarks[i])); - i++; - } - } - @Test public void sortBySetCode() throws Exception { CardSorting.sortByField(testCards, "setCode"); -- cgit v1.2.3