Browse Source

Fixing incorrect handling of berth offset

develop
LO Kam Tao Leo 3 years ago
parent
commit
3d930cd506
  1. 5
      src/main/java/org/leolo/nrdatad/model/Smart.java
  2. 17
      src/main/java/org/leolo/nrdatad/util/JSONUtil.java
  3. 2
      src/test/java/org/leolo/nrdatad/ReferenceDataParserTest.java
  4. 35
      src/test/java/org/leolo/nrdatad/UtilTest.java

5
src/main/java/org/leolo/nrdatad/model/Smart.java

@ -1,6 +1,7 @@
package org.leolo.nrdatad.model;
import org.json.JSONObject;
import org.leolo.nrdatad.util.JSONUtil;
public class Smart {
@ -142,7 +143,7 @@ public class Smart {
smart.toBerth = obj.optString("TOBERTH");
smart.fromLine = obj.optString("FROMLINE");
smart.toLine = obj.optString("TOLINE");
smart.berthOffset = obj.optInt("BERTHOFFSET");
smart.berthOffset = JSONUtil.parseInt(obj.optString("BERTHOFFSET"));
smart.platform = obj.optString("PLATFORM");
smart.event = SmartEvent.parseCode(obj.optString("EVENT"));
smart.route = obj.optString("ROUTE");
@ -152,4 +153,6 @@ public class Smart {
smart.comment = obj.optString("COMMENT");
return smart;
}
}

17
src/main/java/org/leolo/nrdatad/util/JSONUtil.java

@ -0,0 +1,17 @@
package org.leolo.nrdatad.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class JSONUtil {
static Logger log = LogManager.getLogger();
public static int parseInt(String val){
if (val.startsWith("+")) {
return Integer.parseInt(val.substring(1));
}
return Integer.parseInt(val);
}
}

2
src/test/java/org/leolo/nrdatad/ReferenceDataParserTest.java

@ -49,7 +49,7 @@ public class ReferenceDataParserTest {
assertEquals("", smart.getRoute());
assertEquals("", smart.getFromLine());
assertEquals("IH", smart.getTd());
assertEquals("01/12/2018", smart.getComment());
assertEquals("02/12/2018", smart.getComment());
assertEquals("NAIRN", smart.getStationName());
}
}

35
src/test/java/org/leolo/nrdatad/UtilTest.java

@ -0,0 +1,35 @@
package org.leolo.nrdatad;
import org.junit.Test;
import org.leolo.nrdatad.util.JSONUtil;
import static org.junit.Assert.*;
public class UtilTest {
@Test public void testJSONUtilParseIntNormal(){
assertEquals(10, JSONUtil.parseInt("10"));
assertEquals(10, JSONUtil.parseInt("+10"));
assertEquals(-10, JSONUtil.parseInt("-10"));
}
@Test(expected = NumberFormatException.class) public void testJSONUtilParseIntError1(){
JSONUtil.parseInt("abcd");
}
@Test(expected = NumberFormatException.class) public void testJSONUtilParseIntError2(){
JSONUtil.parseInt("");
}
@Test(expected = NullPointerException.class) public void testJSONUtilParseIntError3(){
JSONUtil.parseInt(null);
}
@Test(expected = NumberFormatException.class) public void testJSONUtilParseIntError4(){
JSONUtil.parseInt("+");
}
@Test(expected = NumberFormatException.class) public void testJSONUtilParseIntError5(){
JSONUtil.parseInt("+abcd");
}
@Test(expected = NumberFormatException.class) public void testJSONUtilParseIntError6(){
JSONUtil.parseInt("+2200000000");
}
}
Loading…
Cancel
Save