From 3cb8dad3917cad4df95340253db5f5b743844f58 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 24 Jul 2015 22:29:10 +0200 Subject: Did many CLI tests, have to rethink the MTGUniverse thing to make it more testable --- src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 52 +++++++++++++---------- src/eu/equalparts/cardbase/utils/MTGUniverse.java | 7 ++- 2 files changed, 32 insertions(+), 27 deletions(-) (limited to 'src/eu') diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index e621bb5..0b2c869 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -38,7 +38,7 @@ public final class CardbaseCLI { /** * Location of the help file. */ - static final String HELP_FILE_PATH = "/help"; + static final String HELP_FILE_PATH = "/help_en"; /** * Program version. */ @@ -79,7 +79,26 @@ public final class CardbaseCLI { * @param args the first argument is the cardbase file. Further arguments are ignored. */ public static void main(String... args) { - new CardbaseCLI(args).startInterface(); + try { + new CardbaseCLI(args).startInterface(); + } catch (JsonParseException e) { + System.out.println("Error: poorly formatted cardbase, check the syntax and try again."); + // although the problem could also be with the upstream CardSetList json. + if (Cardbase.DEBUG) e.printStackTrace(); + System.exit(1); + } catch (JsonMappingException e) { + System.out.println("Error: unexpected fields found in cardbase, it may be from an old version?"); + if (Cardbase.DEBUG) e.printStackTrace(); + System.exit(1); + } catch (IOException e) { + System.out.println("Error: something went wrong reading cardbase file, abort..."); + if (Cardbase.DEBUG) e.printStackTrace(); + System.exit(1); + } catch (IllegalArgumentException e) { + System.out.println("Error: the provided file is invalid."); + if (Cardbase.DEBUG) e.printStackTrace(); + System.exit(1); + } } /** @@ -88,37 +107,24 @@ public final class CardbaseCLI { * on the constructed object. * * @param args a list of arguments. Only the first argument is used, as a cardbase JSON. + * @throws IOException if something goes wrong while reading the provided file. + * @throws JsonMappingException if the provided json did not correspond to the expected format. + * @throws JsonParseException if the provided file did not contain valid json. */ - CardbaseCLI(String... args) { + CardbaseCLI(String... args) throws JsonParseException, JsonMappingException, IOException { System.out.println("Welcome to Cardbase CLI!"); // set debug flag if we are debugging if (Cardbase.DEBUG) System.out.println("Debug mode is on."); // make the Cardbase - if (args != null && args.length > 0) { + if (args != null && args.length > 0 && !args[0].isEmpty()) { cardbaseFile = new File(args[0]); if (cardbaseFile.exists() && cardbaseFile.isFile() && cardbaseFile.canRead()) { System.out.println("Loading cardbase from \"" + args[0] + "\"."); - try { - cardbase = new Cardbase(cardbaseFile); - } catch (JsonParseException e) { - System.out.println("Error: poorly formatted cardbase, check the syntax and try again."); - // although the problem could also be with the upstream CardSetList json. - if (Cardbase.DEBUG) e.printStackTrace(); - System.exit(1); - } catch (JsonMappingException e) { - System.out.println("Error: unexpected fields found in cardbase, it may be from an old version?"); - if (Cardbase.DEBUG) e.printStackTrace(); - System.exit(1); - } catch (IOException e) { - System.out.println("Error: something went wrong reading cardbase file, abort..."); - if (Cardbase.DEBUG) e.printStackTrace(); - System.exit(1); - } + cardbase = new Cardbase(cardbaseFile); } else { - System.out.println(args[0] + " appears to be an invalid path."); - System.exit(0); + throw new IllegalArgumentException(); } } else { System.out.println("No cardbase file was provided, creating a clean cardbase."); @@ -465,7 +471,7 @@ public final class CardbaseCLI { */ String sanitiseFileName(String name) { // POSIX-compliant valid filename characters - name = name.replaceAll("[^-_.A-Za-z0-9]", ""); + name = name.replaceAll("[^-_./A-Za-z0-9]", ""); // extension is not indispensable, but good practice if (!name.endsWith(".cb")) { name = name.concat(".cb"); diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java index 7a58c1c..b40e518 100644 --- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java +++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java @@ -40,8 +40,7 @@ public final class MTGUniverse { /** * A cache of CardSets to avoid querying the server many times for the same information. */ - private static ArrayList cardSets; - + private static List cardSets; /** * A cache of {@code FullCardSets} to avoid querying the server many times for the same information. */ @@ -110,13 +109,13 @@ public final class MTGUniverse { /** * @return a list of all card sets in the form of {@code CardSet} objects. */ - public static ArrayList getCardSetList() { + public static List getCardSetList() { // if the list isn't cached, fetch and cache it if (cardSets == null) { try { cardSets = JSON.mapper.readValue(new URL(BASE_DATA_URL + "SetList.json"), new TypeReference>() {}); } catch (Exception e) { - System.out.println("Error: could not fetch/parse set code list from upstream, loading fallback json..."); + System.out.println("Error: could not fetch or parse set code list from upstream, using fallback json..."); e.printStackTrace(); try { -- cgit v1.2.3