📄 testconversions.java
字号:
package net.sf.irunninglog.util;
import java.math.BigDecimal;
import java.util.Date;
import net.sf.irunninglog.junit.BaseTestCase;
/**
* @to.do v2.x Tests for Locales other than the default
*/
public class TestConversions extends BaseTestCase {
public TestConversions(String name) {
super(name);
}
public void setUp() {
super.setUp();
}
public void testStringToBoolean() {
String [] trueValues = new String [] {"1", "true", "TRUE", "TrUe", "t", "T", "yes", "YEs"};
String [] falseValues = new String [] {"0", "false", "FALSE", "FaLSe", "f", "F", "no", "NO"};
try {
assertNull(Conversions.stringToBoolean(null));
} catch (ConversionException ex) {
failTest();
}
for (int i=0;i<trueValues.length;i++) {
try {
assertTrue(Conversions.stringToBoolean(trueValues[i]).booleanValue());
} catch (ConversionException ex) {
failTest();
}
}
for (int i=0;i<falseValues.length; i++) {
try {
assertFalse(Conversions.stringToBoolean(falseValues[i]).booleanValue());
} catch (ConversionException ex) {
failTest();
}
}
String [] errorConditions = new String [] {"", " ", "foo", "avasvasv", "@#F@##@ESDG"};
for (int i=0;i<errorConditions.length;i++) {
String value = errorConditions[i];
try {
Conversions.stringToBoolean(value);
failTest();
} catch (ConversionException ex) {
assertTrue(true);
}
}
}
public void testBooleanToString1() {
Boolean bTrue = new Boolean(true);
Boolean bFalse = new Boolean(false);
Boolean bGarbage = new Boolean("foo");
assertNull(Conversions.booleanToString(null));
assertEquals(ConstantValues.STRING_TRUE, Conversions.booleanToString(bTrue));
assertEquals(ConstantValues.STRING_FALSE, Conversions.booleanToString(bFalse));
assertEquals(ConstantValues.STRING_FALSE, Conversions.booleanToString(bGarbage));
}
public void testBooleanToString2() {
assertEquals(ConstantValues.STRING_TRUE, Conversions.booleanToString(true));
assertEquals(ConstantValues.STRING_FALSE, Conversions.booleanToString(false));
}
public void testStringToDecimal() {
String [] validValues = new String [] {"0", "-2", "212.2124124", "123121231231", "0.000000000021312", "-23423423423.34234234"};
String [] invalidValues = new String [] {"sdasf", "--1222", "123123.123123.21312", "1,000.00", "", " ", "2,3"};
try {
Conversions.stringToDecimal(null);
} catch (ConversionException ex) {
failTest();
}
for (int i=0; i<validValues.length; i++) {
String value = validValues[i];
try{
assertNotNull(Conversions.stringToDecimal(value));
} catch (ConversionException ex) {
LOG.error("Unable to convert '"+value+"' to a Decimal");
failTest();
}
}
for (int i=0; i<invalidValues.length; i++) {
String value = invalidValues[i];
try{
Conversions.stringToDecimal(value);
LOG.error("Should not have been able to convert '"+value+"' to a Decimal");
failTest();
} catch (ConversionException ex) {
assertTrue(true);
}
}
}
public void testDecimalToString() {
assertNull(Conversions.decimalToString(null));
assertNotNull(Conversions.decimalToString(new BigDecimal(0)));
}
public void testStringToDate() {
String [] validValues = new String [] {"2/2/2005", "2/2/05", "2/30/05", "1/1/34", "15/1/3043"};
String [] invalidValues = new String [] {"ascasc", "", " ", "2.2.2005", "2-2-05", "2/2", "2"};
try {
assertNull(Conversions.stringToDate(null));
} catch (ConversionException ex) {
LOG.error("Unable to convert 'null' to a Date");
failTest();
}
for (int i=0; i<validValues.length; i++) {
String value = validValues[i];
try {
assertNotNull(Conversions.stringToDate(value));
} catch (Exception ex) {
LOG.error("Unable to convert '"+value+"' to a Date");
failTest();
}
}
for (int i=0;i<invalidValues.length;i++) {
String value = invalidValues[i];
try {
Conversions.stringToDate(value);
LOG.error("Should not have been able to convert '"+value+"' to a Date");
failTest();
} catch (ConversionException e) {
assertTrue(true);
}
}
}
public void testDateToString() {
assertNull(Conversions.dateToString(null));
assertNotNull(Conversions.dateToString(new Date()));
}
public void testStringToTime() {
String [] validValues = new String [] {"1:2:3.4", "2:3.4", "1:2:3", "1:2", "1"};
String [] invalidValues = new String [] {ConstantValues.STRING_BLANK, "1:2:", "1.2", ":1:2", "1:2:3:4", "1:2:3:4:5", "a:b", "1:2:3.d", "1:2:3:4.4.4", "24:00:00.000", "23:61:00", "50:100:2000.800", "-1:-20:-300", "1:-2:4.500", "1:2:3.", "12:23.34."};
try {
assertNull(Conversions.stringToTime(null));
} catch (ConversionException ex) {
LOG.error("Unable to convert 'null' to a time");
failTest();
}
for (int i=0; i<validValues.length; i++) {
String value = validValues[i];
try {
assertNotNull(Conversions.stringToTime(value));
} catch (Exception ex) {
LOG.error("Unable to convert '"+value+"' to a time");
failTest(ex);
}
}
for (int i=0;i<invalidValues.length;i++) {
String value = invalidValues[i];
try {
Conversions.stringToTime(value);
LOG.error("Should not have been able to convert '"+value+"' to a time");
failTest();
} catch (ConversionException ex) {
assertTrue(true);
}
}
}
public void testTimeToString() throws ConversionException {
long l = (long) Integer.MAX_VALUE;
long [] valid = new long [] {0, 763524, 1000000, 86399999};
long [] invalid = new long [] {-1, -1000, 86400000, l+1};
assertNull(Conversions.timeToString(null));
for (int i=0; i<valid.length; i++) {
assertNotNull(Conversions.timeToString(new Long(valid[i])));
}
for (int i=0; i<invalid.length; i++) {
try {
Conversions.timeToString(new Long(invalid[i]));
failTest();
} catch (ConversionException ex) {
assertTrue(true);
}
}
}
public void testAndVerifyStringToTime() throws ConversionException {
String [] input = {"23:59:59.999", "1:00:00", "1:0:0", "3:15:30.981", "59:59", "59:59.999", "5:02", "10:27.5", "10:30.050", "3", "0", "60", "80:20.001","1:61:00.000","3:5:2.001", "1:2:3.4"};
long [] output = {86399999, 3600000, 3600000, 11730981, 3599000, 3599999, 302000, 627500, 630050, 180000, 0, 3600000, 4820001, 7260000, 11102001, 3723400};
for (int i=0; i<input.length; i++) {
assertEquals(output[i], Conversions.stringToTime(input[i]).longValue());
}
}
public void testAndVerifyTimeToString() throws ConversionException {
long [] input = new long [] {86399999, 3600000, 11730981, 60000, 67000, 67512, 3599999, 3599000, 1260000, 30000, 1012, 1002};
String [] output = new String [] {"23:59:59.999", "01:00:00", "03:15:30.981", "01:00", "01:07", "01:07.512", "59:59.999", "59:59", "21:00", "00:30", "00:01.012", "00:01.002"};
for (int i=0; i< input.length; i++) {
assertEquals(output[i], Conversions.timeToString(new Long(input[i])));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -