TestFormatterpublic class TestFormatter extends TestCase
Methods Summary |
---|
private java.util.Formatter[] | getFormatters()creates an array containing all the supported Formatters and 'null'
for locale-neutral formatting
String[] suppLocales = Formatter.getSupportedLocales();
Formatter[] ret = new Formatter[suppLocales.length + 1];
int i;
try {
for (i = 0; i < suppLocales.length; i++) {
ret[i] = new Formatter(suppLocales[i]);
}
ret[i++] = new Formatter(null);
} catch (Exception e) {
return null;
}
return ret;
| public void | runTests()fill suite with test methods
declare("testMessageFormat");
testMessageFormat();
declare("testDateTime");
testDateTime();
declare("testPercent");
testPercent();
declare("testNumber");
testNumber();
declare("testCurrencyWithCode");
testCurrencyWithCode();
declare("testUnsupportedLocale");
testUnsupportedLocale();
declare("testFormatSupportedCurrency");
testFormatSupportedCurrency();
declare("testCurrencyWithNonSupportedCode");
testCurrencyWithNonSupportedCode();
declare("testRoundingCurrencyDown");
testRoundingCurrencyDown();
declare("testRoundingCurrencyUp");
testRoundingCurrencyUp();
declare("testNaN");
testNaN();
| public void | testCurrencyWithCode()Format Currency with code
Formatter f = new Formatter();
String result = f.formatCurrency(12.34, "USD");
assertEquals("$12.34", result);
result = f.formatCurrency(-12.34, "USD");
assertEquals("($12.34)", result);
| public void | testCurrencyWithNonSupportedCode()Format Currency with nonsupported code
Formatter f = new Formatter();
String result = f.formatCurrency(12.34, "LIT");
assertEquals("LIT12.34", result);
result = f.formatCurrency(-12.34, "LIT");
assertEquals("(LIT12.34)", result);
| public void | testDateTime()Format Date & Time
long constTime = 1128522923951l;
String result;
String month_en = "October";
String month_sk = "okt\u00f3ber";
String month_cs = "\u0159\u00edjna";
Formatter f = new Formatter("en-US");
Formatter f1 = new Formatter("sk-SK");
Formatter f2 = new Formatter("cs-CZ");
Calendar c = Calendar.getInstance();
Date date = c.getTime();
c.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));
date.setTime(constTime);
c.setTime(date);
result = f.formatDateTime(c, Formatter.DATE_SHORT);
assertEquals("10/5/05", result);
result = f1.formatDateTime(c, Formatter.DATE_SHORT);
assertEquals("5.10.2005", result);
result = f2.formatDateTime(c, Formatter.DATE_SHORT);
assertEquals("5.10.05", result);
result = f.formatDateTime(c, Formatter.DATE_LONG);
assertEquals(month_en + " 5, 2005", result);
result = f1.formatDateTime(c, Formatter.DATE_LONG);
assertEquals("5. " + month_sk + " 2005", result);
result = f2.formatDateTime(c, Formatter.DATE_LONG);
assertEquals("5. " + month_cs + " 2005", result);
result = f.formatDateTime(c, Formatter.TIME_SHORT);
assertEquals("9:35 AM", result);
result = f1.formatDateTime(c, Formatter.TIME_SHORT);
assertEquals("9:35", result);
result = f2.formatDateTime(c, Formatter.TIME_SHORT);
assertEquals("9:35", result);
result = f.formatDateTime(c, Formatter.TIME_LONG);
assertEquals("9:35:23 AM -04:00", result);
result = f1.formatDateTime(c, Formatter.TIME_LONG);
assertEquals("9:35:23 -04:00", result);
result = f2.formatDateTime(c, Formatter.TIME_LONG);
assertEquals("9:35:23 -04:00", result);
result = f.formatDateTime(c, Formatter.DATETIME_SHORT);
assertEquals("9:35 AM 10/5/05", result);
result = f1.formatDateTime(c, Formatter.DATETIME_SHORT);
assertEquals("9:35 5.10.2005", result);
result = f2.formatDateTime(c, Formatter.DATETIME_SHORT);
assertEquals("9:35 5.10.05", result);
result = f.formatDateTime(c, Formatter.DATETIME_LONG);
assertEquals("9:35:23 AM -04:00 " + month_en + " 5, 2005",
result);
result = f1.formatDateTime(c, Formatter.DATETIME_LONG);
assertEquals("9:35:23 -04:00 5. " + month_sk + " 2005",
result);
result = f2.formatDateTime(c, Formatter.DATETIME_LONG);
assertEquals("9:35:23 -04:00 5. " + month_cs + " 2005",
result);
| public void | testFormatSupportedCurrency()Format supported Currency
try {
Formatter f = new Formatter("sk-SK");
String result = f.formatCurrency(12.50);
assertEquals("12.50 Sk", result);
Formatter f1 = new Formatter("en-US");
result = f1.formatCurrency(12.50);
assertEquals("$12.50", result);
result = f1.formatCurrency(-12.50);
assertEquals("($12.50)", result);
Formatter f2 = new Formatter("en");
result = f2.formatCurrency(12.50);
assertEquals("US$12.50", result);
result = f2.formatCurrency(-12.50);
assertEquals("-US$12.50", result);
} catch (UnsupportedLocaleException ue) {
ue.printStackTrace();
fail();
}
| public void | testMessageFormat()Format Message
Formatter f = new Formatter("en-US");
String result;
/* Format message with just one parameter */
result = f.formatMessage("This is {0} parameter",
new String[] {"first"});
assertEquals("This is first parameter", result);
/* Format message with more than one parameter */
result = f.formatMessage("{0} {1} {2} {3} {4} {5} {6} {7}",
new String[] {"first", "second", "third",
"fourth", "fifth", "sixth",
"seventh", "eighth"});
assertEquals("first second third fourth fifth sixth seventh eighth",
result);
/* Format message containing escaped parenthesis */
result = f.formatMessage("This is {{ {0} parenthesis",
new String[] {"left"});
assertEquals("This is { left parenthesis", result);
/* Format message containing right escaped parenthesis */
result = f.formatMessage("This is } {0} parenthesis",
new String[] {"right"});
assertEquals("This is } right parenthesis", result);
/* Test when placeholders doesn't match number of parameters */
try {
result = f.formatMessage("{0} {1} {2}", new String[] {"first"});
fail("IllegalArgumentException wasn't thrown.");
} catch (IllegalArgumentException e) {
assertTrue(true);
}
/* Test when there are more parameters than placeholders */
result = null;
try {
result = f.formatMessage("{0}",
new String[] {"first", "second", "third"});
} catch (IllegalArgumentException e) {
fail();
}
assertEquals("first", result);
/* Placeholders can repeat */
result = f.formatMessage("{0} {1} {0} {0}",
new String[] {"first", "second"});
assertEquals("first second first first", result);
/* Test leading zero example: <code>{05}</code> */
result = f.formatMessage("{00} {01}",
new String[] {"first", "second"});
assertEquals("first second", result);
/*
* If a placeholder doesn't contain number, then
* IllegalArgumentException is thrown
*/
try {
result = f.formatMessage("{ciao}", new String[] {"first"});
fail("IllegalArgumentException wasn't thrown.");
} catch (IllegalArgumentException e) {
assertTrue(true);
}
/* Check if NPE is thrown when parameters == null */
String[] params = null;
try {
result = f.formatMessage("{0} {1}", params);
fail("NullPointerException wasn't thrown.");
} catch (NullPointerException npe) {
assertTrue(true);
}
/* Check if NPE is thrown when template == null */
String template = null;
try {
result = f.formatMessage(template, new String[] {"first"});
fail("NullPointerException wasn't thrown.");
} catch (NullPointerException npe) {
assertTrue(true);
}
| public void | testNaN()Format NAN, infinity value for Currency
double[] val = {Double.NaN, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY };
String[] currencyCode = { "XXX", "YYY", "ZZZ" };
Formatter[] formatter = getFormatters();
for (int i = 0; i < formatter.length; i++) {
for (int j = 0; j < val.length; j++) {
String result = formatter[i].formatCurrency(val[j],
currencyCode[j]);
if (result.indexOf(currencyCode[j]) != -1) {
assertTrue(false);
} else {
assertTrue(true);
}
}
}
| public void | testNumber()Format Numbers
Formatter f = new Formatter("en-US");
String result = f.formatNumber(1234567890);
assertEquals("1,234,567,890", result);
result = f.formatNumber(12345.67890);
assertEquals("12,345.68", result);
result = f.formatNumber(12345.67890, 4);
assertEquals("12,345.6789", result);
Formatter f1 = new Formatter("zh-CN");
result = f1.formatNumber(1234567890);
assertEquals("1,234,567,890", result);
result = f1.formatNumber(12345.67890);
assertEquals("12,345.68", result);
result = f1.formatNumber(12345.67890, 4);
assertEquals("12,345.6789", result);
Formatter f2 = new Formatter();
result = f2.formatNumber(1234567890);
assertEquals("1,234,567,890", result);
result = f2.formatNumber(12345.67890);
assertEquals("12,345.68", result);
result = f2.formatNumber(12345.67890, 4);
assertEquals("12,345.6789", result);
| public void | testPercent()Format Percents
Formatter f = new Formatter("en-US");
float fl_value = 0.123456f;
String result = f.formatPercentage(123);
assertEquals("123%", result);
result = f.formatPercentage(fl_value, 3);
assertEquals("12.346%", result);
result = f.formatPercentage(1234567890);
assertEquals("1,234,567,890%", result);
Formatter f1 = new Formatter("zh-CN");
result = f1.formatPercentage(1234567890);
assertEquals("1,234,567,890%", result);
result = f1.formatPercentage(fl_value, 4);
assertEquals("12.3456%", result);
result = f1.formatPercentage(-fl_value, 4);
assertEquals("-12.3456%", result);
| public void | testRoundingCurrencyDown()Format Rounding Currency Down
try {
Formatter f = new Formatter("sk-SK");
String result = f.formatCurrency(12.502);
assertEquals("12.50 Sk", result);
} catch (UnsupportedLocaleException ue) {
ue.printStackTrace();
fail();
}
| public void | testRoundingCurrencyUp()Format Rounding Currency Up
try {
Formatter f = new Formatter("sk-SK");
String result = f.formatCurrency(12.597);
assertEquals("12.60 Sk", result);
} catch (UnsupportedLocaleException ue) {
ue.printStackTrace();
fail();
}
| public void | testUnsupportedLocale()Test unsupported locale
try {
Formatter f = new Formatter("uk-UK");
} catch (UnsupportedLocaleException ue) {
assertTrue(true);
return;
}
fail("Exception wasn't thrown");
|
|