mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Merge branch 'file-parser' into 'master'
File parser See merge request !4
This commit is contained in:
@@ -20,6 +20,11 @@
|
|||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.json-simple</groupId>
|
||||||
|
<artifactId>json-simple</artifactId>
|
||||||
|
<version>1.1.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -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<String> getTeams() {
|
||||||
|
try {
|
||||||
|
return (ArrayList<String>) this.content.get("teams");
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<String> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"time-scale": "abc",
|
||||||
|
"race-name": 123,
|
||||||
|
"teams-with-wrong-name":["team1","team2","team3"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"time-scale": 100,
|
||||||
|
"race-name": "IDK",
|
||||||
|
"teams":["team1","team2","team3"]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user