From 62fe8a3e017430b0f2a09ee1a678143ba5ccfcab Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Sun, 5 Mar 2017 18:01:37 +1300 Subject: [PATCH 1/3] Added Json-simple dependency to Maven --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 09ce924d..0be33f00 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,11 @@ 4.12 test + + com.googlecode.json-simple + json-simple + 1.1.1 + From b9b34095913b44f8e690ff7331f8da8726a9e8cd Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Sun, 5 Mar 2017 21:21:22 +1300 Subject: [PATCH 2/3] Added tests for FileParser class, as well as test files --- src/test/java/seng302/FileParserTest.java | 53 +++++++++++++++++++++++ src/test/java/seng302/invalid.json | 5 +++ src/test/java/seng302/valid.json | 5 +++ 3 files changed, 63 insertions(+) create mode 100644 src/test/java/seng302/FileParserTest.java create mode 100644 src/test/java/seng302/invalid.json create mode 100644 src/test/java/seng302/valid.json diff --git a/src/test/java/seng302/FileParserTest.java b/src/test/java/seng302/FileParserTest.java new file mode 100644 index 00000000..05c71a9e --- /dev/null +++ b/src/test/java/seng302/FileParserTest.java @@ -0,0 +1,53 @@ +package seng302; + +import org.junit.Test; + +import java.io.FileNotFoundException; +import java.util.ArrayList; + +import static org.junit.Assert.*; + +/** Unit test for FileParser class + * Created by Haoming on 5/03/17. + */ +public class FileParserTest { + + /* + test if it fails from reading non existed file + */ + @Test (expected = FileNotFoundException.class) + public void readNonExistedFile() throws Exception { + FileParser fileParser = new FileParser("test/java/seng302/non-existed.json"); + } + + /* + test a valid json file with valid content. + */ + @Test + public void readValidFile() throws Exception{ + FileParser fileParser = new FileParser("src/test/java/seng302/valid.json"); + + assertEquals(fileParser.getRaceName(), "IDK"); + + ArrayList teams = new ArrayList<>(); + teams.add("team1"); + teams.add("team2"); + teams.add("team3"); + assertTrue(teams.equals(fileParser.getTeams())); + } + + /* + test an invalid json file within wrong type value and misnamed + variable name. + */ + @Test + public void readInvaldFile() throws Exception { + FileParser fileParser = new FileParser("src/test/java/seng302/invalid.json"); + + assertEquals(fileParser.getRaceName(), null); + assertEquals(fileParser.getTeams(), null); + assertEquals(fileParser.getTimeScale(), -1); + assertEquals(fileParser.getTeams(), null); + } + +} \ No newline at end of file diff --git a/src/test/java/seng302/invalid.json b/src/test/java/seng302/invalid.json new file mode 100644 index 00000000..43331785 --- /dev/null +++ b/src/test/java/seng302/invalid.json @@ -0,0 +1,5 @@ +{ + "time-scale": "abc", + "race-name": 123, + "teams-with-wrong-name":["team1","team2","team3"] +} \ No newline at end of file diff --git a/src/test/java/seng302/valid.json b/src/test/java/seng302/valid.json new file mode 100644 index 00000000..21b0c2dc --- /dev/null +++ b/src/test/java/seng302/valid.json @@ -0,0 +1,5 @@ +{ + "time-scale": 100, + "race-name": "IDK", + "teams":["team1","team2","team3"] +} \ No newline at end of file From c197083d7c1d2ba601a69452c66bf5933d645284 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Sun, 5 Mar 2017 21:41:50 +1300 Subject: [PATCH 3/3] Created FileParser class - Created FileParser class which processes team setting file in Json format. - Implement get** methods, which extract information from setting file more efficient. - Took more time than expected because need to learn Json-simple dependency first. \#story[3] --- src/main/java/seng302/FileParser.java | 92 +++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/main/java/seng302/FileParser.java diff --git a/src/main/java/seng302/FileParser.java b/src/main/java/seng302/FileParser.java new file mode 100644 index 00000000..2fe26d0f --- /dev/null +++ b/src/main/java/seng302/FileParser.java @@ -0,0 +1,92 @@ +package seng302; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.FileReader; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.util.ArrayList; + + +/** + * Read team name from a given Json file. So that user can extract information + * efficiently from external files. + */ + +public class FileParser { + + private String filePath; + private JSONObject content; + /** used to construct an instance of file parser + * + * @param filePath a string like path to show location of desired file to + * be parsed + */ + public FileParser(String filePath) throws Exception { + this.filePath = filePath; + this.readFile(); + } + + /** + * Reads content from a given file, and return the content as JSONObject. + * Throws FileNotFoundException, if the given file cannot be found. + */ + private void readFile() throws FileNotFoundException{ + JSONParser parser = new JSONParser(); + try { + Object obj = parser.parse(new FileReader(filePath)); + + JSONObject jsonObject = (JSONObject) obj; + this.content = jsonObject; + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + /** + * Gets time scale setting parameter. + * @return long time scale. -1 if parameter is invalid (eg. scale is + * negative number, or containing non numeric character) or cannot be found. + */ + public long getTimeScale() { + try { + long timeScale = (long) this.content.get("time-scale"); + return timeScale >= 0 ? timeScale : -1; + } catch (Exception e) { + return -1; + } + } + + /** + * Gets race name in the setting file. + * @return a string of race name. null if race name is invalid or cannot + * be found. + */ + public String getRaceName() { + try { + return (String) this.content.get("race-name"); + } catch (Exception e) { + return null; + } + } + + /** + * Gets an array of teams who participate the race. + * @return an ArrayList containing strings of team names. null if teams + * setting is invalid or there is no team. + */ + public ArrayList getTeams() { + try { + return (ArrayList) this.content.get("teams"); + } catch (Exception e) { + return null; + } + } + +} \ No newline at end of file