use of java.text.ChoiceFormat in project j2objc by google.
the class MessageFormatTest method test_getFormatsByArgumentIndex.
public void test_getFormatsByArgumentIndex() {
// test with repeating formats and max argument index < max offset
Format[] formats = format1.getFormatsByArgumentIndex();
Format[] correctFormats = new Format[] { DateFormat.getDateInstance(), new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), NumberFormat.getCurrencyInstance(), null };
assertEquals("Test1:Returned wrong number of formats:", correctFormats.length, formats.length);
for (int i = 0; i < correctFormats.length; i++) {
assertEquals("Test1:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
}
// test with max argument index > max offset
formats = format2.getFormatsByArgumentIndex();
correctFormats = new Format[] { DateFormat.getDateInstance(), new ChoiceFormat("0#off|1#on"), null, NumberFormat.getCurrencyInstance(), null, null, null, null, DateFormat.getTimeInstance() };
assertEquals("Test2:Returned wrong number of formats:", correctFormats.length, formats.length);
for (int i = 0; i < correctFormats.length; i++) {
assertEquals("Test2:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
}
// test with argument number being zero
formats = format3.getFormatsByArgumentIndex();
assertEquals("Test3: Returned wrong number of formats:", 0, formats.length);
}
use of java.text.ChoiceFormat in project j2objc by google.
the class MessageFormatTest method test_ConstructorLjava_lang_String.
public void test_ConstructorLjava_lang_String() {
MessageFormat format = new MessageFormat("abc {4,time} def {3,date} ghi {2,number} jkl {1,choice,0#low|1#high} mnop {0}");
assertTrue("Not a MessageFormat", format.getClass() == MessageFormat.class);
Format[] formats = format.getFormats();
assertNotNull("null formats", formats);
assertTrue("Wrong format count: " + formats.length, formats.length >= 5);
assertTrue("Wrong time format", formats[0].equals(DateFormat.getTimeInstance()));
assertTrue("Wrong date format", formats[1].equals(DateFormat.getDateInstance()));
assertTrue("Wrong number format", formats[2].equals(NumberFormat.getInstance()));
assertTrue("Wrong choice format", formats[3].equals(new ChoiceFormat("0.0#low|1.0#high")));
assertNull("Wrong string format", formats[4]);
Date date = new Date();
FieldPosition pos = new FieldPosition(-1);
StringBuffer buffer = new StringBuffer();
format.format(new Object[] { "123", new Double(1.6), new Double(7.2), date, date }, buffer, pos);
String result = buffer.toString();
buffer.setLength(0);
buffer.append("abc ");
buffer.append(DateFormat.getTimeInstance().format(date));
buffer.append(" def ");
buffer.append(DateFormat.getDateInstance().format(date));
buffer.append(" ghi ");
buffer.append(NumberFormat.getInstance().format(new Double(7.2)));
buffer.append(" jkl high mnop 123");
assertTrue("Wrong answer:\n" + result + "\n" + buffer, result.equals(buffer.toString()));
assertEquals("Simple string", "Test message", new MessageFormat("Test message").format(new Object[0]));
result = new MessageFormat("Don't").format(new Object[0]);
assertTrue("Should not throw IllegalArgumentException: " + result, "Dont".equals(result));
try {
new MessageFormat("Invalid {1,foobar} format descriptor!");
fail("Expected test_ConstructorLjava_lang_String to throw IAE.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
new MessageFormat("Invalid {1,date,invalid-spec} format descriptor!");
} catch (IllegalArgumentException ex) {
// expected
}
checkSerialization(new MessageFormat(""));
checkSerialization(new MessageFormat("noargs"));
checkSerialization(new MessageFormat("{0}"));
checkSerialization(new MessageFormat("a{0}"));
checkSerialization(new MessageFormat("{0}b"));
checkSerialization(new MessageFormat("a{0}b"));
// Regression for HARMONY-65
try {
new MessageFormat("{0,number,integer");
fail("Assert 0: Failed to detect unmatched brackets.");
} catch (IllegalArgumentException e) {
// expected
}
}
use of java.text.ChoiceFormat in project j2objc by google.
the class MessageFormatTest method test_setFormats$Ljava_text_Format.
public void test_setFormats$Ljava_text_Format() throws Exception {
MessageFormat f1 = (MessageFormat) format1.clone();
// case 1: Test with repeating formats and max argument index < max
// offset
// compare getFormats() results after calls to setFormats(Format[])
Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(), new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), NumberFormat.getCurrencyInstance(), new ChoiceFormat("1#few|2#ok|3#a lot") };
f1.setFormats(correctFormats);
Format[] formats = f1.getFormats();
assertTrue("Test1A:Returned wrong number of formats:", correctFormats.length <= formats.length);
for (int i = 0; i < correctFormats.length; i++) {
assertEquals("Test1B:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
}
// case 2: Try to pass null argument to setFormats().
try {
f1.setFormats(null);
fail();
} catch (NullPointerException expected) {
}
}
use of java.text.ChoiceFormat in project j2objc by google.
the class MessageFormatTest method test_setFormatILjava_text_Format.
public void test_setFormatILjava_text_Format() throws Exception {
// case 1: Compare getFormats() results after calls to setFormat()
MessageFormat f1 = (MessageFormat) format1.clone();
f1.setFormat(0, DateFormat.getTimeInstance());
f1.setFormat(1, DateFormat.getTimeInstance());
f1.setFormat(2, NumberFormat.getInstance());
f1.setFormat(3, new ChoiceFormat("0#off|1#on"));
f1.setFormat(4, new ChoiceFormat("1#few|2#ok|3#a lot"));
f1.setFormat(5, DateFormat.getTimeInstance());
Format[] formats = f1.getFormats();
formats = f1.getFormats();
Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(), DateFormat.getTimeInstance(), NumberFormat.getInstance(), new ChoiceFormat("0#off|1#on"), new ChoiceFormat("1#few|2#ok|3#a lot"), DateFormat.getTimeInstance() };
assertEquals(correctFormats.length, formats.length);
for (int i = 0; i < correctFormats.length; i++) {
assertEquals("Test1B:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
}
// case 2: Try to setFormat using incorrect index
try {
f1.setFormat(-1, DateFormat.getDateInstance());
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
}
try {
f1.setFormat(f1.getFormats().length, DateFormat.getDateInstance());
fail();
} catch (ArrayIndexOutOfBoundsException expected) {
}
}
use of java.text.ChoiceFormat in project j2objc by google.
the class MessageFormatTest method test_getFormats.
public void test_getFormats() {
// test with repeating formats and max argument index < max offset
Format[] formats = format1.getFormats();
Format[] correctFormats = new Format[] { NumberFormat.getCurrencyInstance(), DateFormat.getTimeInstance(), NumberFormat.getPercentInstance(), null, new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance() };
assertEquals("Test1:Returned wrong number of formats:", correctFormats.length, formats.length);
for (int i = 0; i < correctFormats.length; i++) {
assertEquals("Test1:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
}
// test with max argument index > max offset
formats = format2.getFormats();
correctFormats = new Format[] { NumberFormat.getCurrencyInstance(), DateFormat.getTimeInstance(), NumberFormat.getPercentInstance(), null, new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance() };
assertEquals("Test2:Returned wrong number of formats:", correctFormats.length, formats.length);
for (int i = 0; i < correctFormats.length; i++) {
assertEquals("Test2:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
}
// test with argument number being zero
formats = format3.getFormats();
assertEquals("Test3: Returned wrong number of formats:", 0, formats.length);
}
Aggregations