4 changed files with 57 additions and 2 deletions
@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -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…
Reference in new issue