use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method TestAdopt.
/**
* Of course, in Java there is no adopt, but we retain the same
* method name. [alan]
*/
@Test
public void TestAdopt() {
String formatStr = "{0,date},{1},{2,number}";
String formatStrChange = "{0,number},{1,number},{2,date}";
MessageFormat msg = new MessageFormat(formatStr);
MessageFormat msgCmp = new MessageFormat(formatStr);
Format[] formats = msg.getFormats();
Format[] formatsCmp = msgCmp.getFormats();
Format[] formatsChg = null;
Format[] formatsAct = null;
Format a = null;
Format b = null;
Format[] formatsToAdopt = null;
if (formats == null || formatsCmp == null || (formats.length <= 0) || (formats.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
int i;
for (i = 0; i < formats.length; i++) {
a = formats[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("a != b");
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
// set msg formats to something different
msg.applyPattern(formatStrChange);
// tested function
formatsChg = msg.getFormats();
if (formatsChg == null || (formatsChg.length != formats.length)) {
errln("Error getting Formats");
return;
}
boolean diff;
diff = true;
for (i = 0; i < formats.length; i++) {
a = formatsChg[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (a.equals(b)) {
logln("formatsChg == formatsCmp at index " + i);
diff = false;
}
}
}
if (!diff) {
errln("*** MSG getFormats diff err.");
return;
}
logln("MSG getFormats tested.");
// tested function
msg.setFormats(formatsCmp);
formatsAct = msg.getFormats();
if (formatsAct == null || (formatsAct.length <= 0) || (formatsAct.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern());
// assertEquals("msg.toPattern()", formatStr, msg.toPattern());
try {
msg.toPattern();
errln("msg.setFormat().toPattern() does not throw an IllegalStateException");
} catch (IllegalStateException e) {
// ok
}
for (i = 0; i < formatsAct.length; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("formatsAct != formatsCmp at index " + i);
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
logln("MSG setFormats tested.");
// ----
// set msg formats to something different
msg.applyPattern(formatStrChange);
formatsToAdopt = new Format[formatsCmp.length];
if (formatsToAdopt == null) {
errln("memory allocation error");
return;
}
for (i = 0; i < formatsCmp.length; i++) {
if (formatsCmp[i] == null) {
formatsToAdopt[i] = null;
} else {
formatsToAdopt[i] = (Format) formatsCmp[i].clone();
if (formatsToAdopt[i] == null) {
errln("Can't clone format at index " + i);
return;
}
}
}
// function to test
msg.setFormats(formatsToAdopt);
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern());
// ICU 4.8 does not support toPattern() when there are custom formats (from setFormat() etc.).
// assertEquals("msg.toPattern()", formatStr, msg.toPattern());
formatsAct = msg.getFormats();
if (formatsAct == null || (formatsAct.length <= 0) || (formatsAct.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
for (i = 0; i < formatsAct.length; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("a != b");
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
logln("MSG adoptFormats tested.");
// ---- adoptFormat
// set msg formats to something different
msg.applyPattern(formatStrChange);
formatsToAdopt = new Format[formatsCmp.length];
if (formatsToAdopt == null) {
errln("memory allocation error");
return;
}
for (i = 0; i < formatsCmp.length; i++) {
if (formatsCmp[i] == null) {
formatsToAdopt[i] = null;
} else {
formatsToAdopt[i] = (Format) formatsCmp[i].clone();
if (formatsToAdopt[i] == null) {
errln("Can't clone format at index " + i);
return;
}
}
}
for (i = 0; i < formatsCmp.length; i++) {
// function to test
msg.setFormat(i, formatsToAdopt[i]);
}
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern());
// ICU 4.8 does not support toPattern() when there are custom formats (from setFormat() etc.).
// assertEquals("msg.toPattern()", formatStr, msg.toPattern());
formatsAct = msg.getFormats();
if (formatsAct == null || (formatsAct.length <= 0) || (formatsAct.length != formatsCmp.length)) {
errln("Error getting Formats");
return;
}
for (i = 0; i < formatsAct.length; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != null) && (b != null)) {
if (!a.equals(b)) {
errln("a != b");
return;
}
} else if ((a != null) || (b != null)) {
errln("(a != null) || (b != null)");
return;
}
}
logln("MSG adoptFormat tested.");
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method testSelectFormatToPattern.
/**
* Test toPattern when there is a SelectFormat
*/
@Test
public void testSelectFormatToPattern() {
String[] patterns = { // Pattern with some text at start and at end
"{0} est {1,select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.", // Pattern with some text at start
"{0} est {1,select, female {all\\u00E9e} other {all\\u00E9}}", // Pattern with some text at end
"{1, select,female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.", // Pattern with no text at any end
"{1, select,female {all\\u00E9e} other {all\\u00E9}}.", // Quoted French pattern
"{0} est {1,select, female {all\\u00E9e c''est} other {all\\u00E9 c''est}} \\u00E0 Paris." };
for (int i = 0; i < patterns.length; ++i) {
String pattern = patterns[i];
MessageFormat mf = new MessageFormat(pattern);
MessageFormat mf2 = new MessageFormat(mf.toPattern());
if (!mf.equals(mf2)) {
errln("message formats not equal for pattern:\n*** '" + pattern + "'\n*** '" + mf.toPattern() + "'");
}
}
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method testNumericOnlyMethods.
// Ensure that methods designed for numeric arguments only, will throw
// an exception when called on MessageFormat objects created with
// named arguments.
@Test
public void testNumericOnlyMethods() {
MessageFormat msg = new MessageFormat("Number of files: {numfiles}");
boolean gotException = false;
try {
Format[] fmts = { new DecimalFormat() };
msg.setFormatsByArgumentIndex(fmts);
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
errln("MessageFormat.setFormatsByArgumentIndex() should throw an " + "IllegalArgumentException when called on formats with " + "named arguments but did not!");
}
gotException = false;
try {
msg.setFormatByArgumentIndex(0, new DecimalFormat());
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
errln("MessageFormat.setFormatByArgumentIndex() should throw an " + "IllegalArgumentException when called on formats with " + "named arguments but did not!");
}
gotException = false;
try {
msg.getFormatsByArgumentIndex();
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
errln("MessageFormat.getFormatsByArgumentIndex() should throw an " + "IllegalArgumentException when called on formats with " + "named arguments but did not!");
}
gotException = false;
try {
Object[] args = { new Long(42) };
msg.format(args, new StringBuffer(), new FieldPosition(0));
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
errln("MessageFormat.format(Object[], StringBuffer, FieldPosition) " + "should throw an IllegalArgumentException when called on " + "formats with named arguments but did not!");
}
gotException = false;
try {
Object[] args = { new Long(42) };
msg.format((Object) args, new StringBuffer(), new FieldPosition(0));
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
errln("MessageFormat.format(Object, StringBuffer, FieldPosition) " + "should throw an IllegalArgumentException when called with " + "non-Map object as argument on formats with named " + "arguments but did not!");
}
gotException = false;
try {
msg.parse("Number of files: 5", new ParsePosition(0));
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
errln("MessageFormat.parse(String, ParsePosition) " + "should throw an IllegalArgumentException when called with " + "non-Map object as argument on formats with named " + "arguments but did not!");
}
gotException = false;
try {
msg.parse("Number of files: 5");
} catch (IllegalArgumentException e) {
gotException = true;
} catch (ParseException e) {
errln("Wrong exception thrown.");
}
if (!gotException) {
errln("MessageFormat.parse(String) " + "should throw an IllegalArgumentException when called with " + "non-Map object as argument on formats with named " + "arguments but did not!");
}
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method TestSetFormat.
@Test
public void TestSetFormat() {
MessageFormat ms = new MessageFormat("{number} {date}", ULocale.ENGLISH);
final DecimalFormat decimalFormat = new DecimalFormat("000.000", DecimalFormatSymbols.getInstance(ULocale.ENGLISH));
ms.setFormatByArgumentName("number", decimalFormat);
final SimpleDateFormat dateFormat = new SimpleDateFormat("'year:'yy 'month:'MM 'day:'dd");
dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
ms.setFormatByArgumentName("date", dateFormat);
Map map = new HashMap();
map.put("number", new Integer(1234));
map.put("date", new Date(0, 0, 0));
String result = ms.format(map);
assertEquals("setFormatByArgumentName", "1234.000 year:99 month:12 day:31", result);
Set formatNames = ms.getArgumentNames();
assertEquals("Format Names match", formatNames, map.keySet());
assertEquals("Decimal", decimalFormat, ms.getFormatByArgumentName("number"));
assertEquals("Date", dateFormat, ms.getFormatByArgumentName("date"));
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method TestArgIsPrefixOfAnother.
@Test
public void TestArgIsPrefixOfAnother() {
// Ticket #11952
MessageFormat mf1 = new MessageFormat("{0,select,a{A}ab{AB}abc{ABC}other{?}}", ULocale.ENGLISH);
assertEquals("a", "A", mf1.format(new Object[] { "a" }));
assertEquals("ab", "AB", mf1.format(new Object[] { "ab" }));
assertEquals("abc", "ABC", mf1.format(new Object[] { "abc" }));
// Ticket #12172
MessageFormat mf2 = new MessageFormat("{a} {aa} {aaa}", ULocale.ENGLISH);
Map<String, Object> args = new TreeMap<String, Object>();
args.put("a", "A");
args.put("aa", "AB");
args.put("aaa", "ABC");
assertEquals("a aa aaa", "A AB ABC", mf2.format(args, new StringBuffer(), null).toString());
// Ticket #12172
MessageFormat mf3 = new MessageFormat("{aa} {aaa}", ULocale.ENGLISH);
assertEquals("aa aaa", "AB ABC", mf3.format(args, new StringBuffer(), null).toString());
}
Aggregations