mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Finished config parser to read race info from external xml file
- created config parser unit test. - modified config.xml file - write unit test for config parser #story[422]
This commit is contained in:
@@ -4,5 +4,6 @@
|
|||||||
<race-name>AC35</race-name>
|
<race-name>AC35</race-name>
|
||||||
<race-size>6</race-size>
|
<race-size>6</race-size>
|
||||||
<time-scale>1.0</time-scale>
|
<time-scale>1.0</time-scale>
|
||||||
|
<wind-direction>135</wind-direction>
|
||||||
</configurations>
|
</configurations>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package seng302.models.parsers;
|
||||||
|
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
|
import java.util.DoubleSummaryStatistics;
|
||||||
|
|
||||||
|
public class ConfigParser extends FileParser {
|
||||||
|
|
||||||
|
private Document doc;
|
||||||
|
|
||||||
|
public ConfigParser(String path) {
|
||||||
|
super(path);
|
||||||
|
this.doc = this.parseFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets wind direction from config file.
|
||||||
|
*
|
||||||
|
* @return a double type degree, or 0 if no value or invalid value is found
|
||||||
|
*/
|
||||||
|
public double getWindDirection() {
|
||||||
|
return getDoubleByTagName("wind-direction", 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a non negative time scale for the race
|
||||||
|
*
|
||||||
|
* @return a double type scale, or 0 if no scale or invalid scale is found
|
||||||
|
*/
|
||||||
|
public double getTimeScale() {
|
||||||
|
return getDoubleByTagName("time-scale", 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a double type number by given tag name found in xml file
|
||||||
|
*
|
||||||
|
* @param tagName a string of tag name
|
||||||
|
* @param defaultVal value returned if no value or invalid value is found
|
||||||
|
* @return value found
|
||||||
|
*/
|
||||||
|
public double getDoubleByTagName(String tagName, double defaultVal) {
|
||||||
|
double val = defaultVal;
|
||||||
|
try {
|
||||||
|
Node node = this.doc.getElementsByTagName(tagName).item(0);
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element element = (Element) node;
|
||||||
|
val = Double.valueOf(element.getTextContent());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a string by given tag name found in xml file
|
||||||
|
*
|
||||||
|
* @param tagName a string of tag name
|
||||||
|
* @param defaultVal a string returned if no value or invalid value is found
|
||||||
|
* @return string found
|
||||||
|
*/
|
||||||
|
public String getStringByTagName(String tagName, String defaultVal) {
|
||||||
|
String string = defaultVal;
|
||||||
|
try {
|
||||||
|
Node node = this.doc.getElementsByTagName(tagName).item(0);
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element element = (Element) node;
|
||||||
|
string = element.getTextContent();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package seng302.models.parsers;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Haoming on 23/03/17.
|
||||||
|
*/
|
||||||
|
public class ConfigParserTest {
|
||||||
|
|
||||||
|
private ConfigParser cp;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void initializeParser() throws Exception {
|
||||||
|
cp = new ConfigParser("doc/examples/config.xml");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getWindDirection() throws Exception {
|
||||||
|
assertEquals(135, cp.getWindDirection(), 1e-10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTimeScale() throws Exception {
|
||||||
|
assertEquals(1.0, cp.getTimeScale(), 1e-10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getDoubleByTagName() throws Exception {
|
||||||
|
assertEquals(6, cp.getDoubleByTagName("race-size", 0), 1e-10);
|
||||||
|
assertEquals(100, cp.getDoubleByTagName("noTag", 100), 1e-10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getStringByTagName() throws Exception {
|
||||||
|
assertEquals("AC35", cp.getStringByTagName("race-name", "11"));
|
||||||
|
assertEquals("oops", cp.getStringByTagName("noTag", "oops"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user