1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.text; 19 20import java.text.DateFormat; 21import java.text.MessageFormat; 22import java.text.NumberFormat; 23import java.text.MessageFormat.Field; 24import java.util.Date; 25import java.util.GregorianCalendar; 26import java.util.Locale; 27import java.util.Vector; 28 29 30public class Support_MessageFormat extends Support_Format { 31 32 public Support_MessageFormat(String p1) { 33 super(p1); 34 } 35 36 @Override 37 public void runTest() { 38 t_formatToCharacterIterator(); 39 t_format_with_FieldPosition(); 40 } 41 42 public static void main(String[] args) { 43 new Support_MessageFormat("").runTest(); 44 } 45 46 public void t_format_with_FieldPosition() { 47 // This test assumes a default DateFormat.is24Hour setting. 48 DateFormat.is24Hour = null; 49 50 String pattern = "On {4,date} at {3,time}, he ate {2,number, integer} hamburger{2,choice,1#|1<s} and drank {1, number} liters of coke. That was {0,choice,1#just enough|1<more than enough} food!"; 51 MessageFormat format = new MessageFormat(pattern, Locale.US); 52 53 Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime(); 54 Integer hamburgers = new Integer(8); 55 Object[] objects = new Object[] { hamburgers, new Double(3.5), 56 hamburgers, date, date }; 57 58 super.text = "On Feb 28, 2005 at 2:20:16 PM, he ate 8 hamburgers and drank 3.5 liters of coke. That was more than enough food!"; 59 60 // test with MessageFormat.Field.ARGUMENT 61 t_FormatWithField(1, format, objects, null, Field.ARGUMENT, 3, 15); 62 63 // test other format fields that are included in the formatted text 64 t_FormatWithField(2, format, objects, null, DateFormat.Field.AM_PM, 0, 65 0); 66 t_FormatWithField(3, format, objects, null, 67 NumberFormat.Field.FRACTION, 0, 0); 68 69 // test fields that are not included in the formatted text 70 t_FormatWithField(4, format, objects, null, DateFormat.Field.ERA, 0, 0); 71 t_FormatWithField(5, format, objects, null, 72 NumberFormat.Field.EXPONENT_SIGN, 0, 0); 73 } 74 75 public void t_formatToCharacterIterator() { 76 // This test assumes a default DateFormat.is24Hour setting. 77 DateFormat.is24Hour = null; 78 79 String pattern = "On {4,date} at {3,time}, he ate {2,number, integer} hamburger{2,choice,1#|1<s} and drank {1, number} liters of coke. That was {0,choice,1#just enough|1<more than enough} food!"; 80 MessageFormat format = new MessageFormat(pattern, Locale.US); 81 82 Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime(); 83 Integer hamburgers = new Integer(8); 84 Object[] objects = new Object[] { hamburgers, new Double(3.5), hamburgers, date, date }; 85 86 t_Format(1, objects, format, getMessageVector1()); 87 } 88 89 private Vector<FieldContainer> getMessageVector1() { 90 Vector<FieldContainer> v = new Vector<FieldContainer>(); 91 v.add(new FieldContainer(3, 6, DateFormat.Field.MONTH)); 92 v.add(new FieldContainer(3, 6, Field.ARGUMENT, 4)); 93 v.add(new FieldContainer(6, 7, Field.ARGUMENT, 4)); 94 v.add(new FieldContainer(7, 9, DateFormat.Field.DAY_OF_MONTH)); 95 v.add(new FieldContainer(7, 9, Field.ARGUMENT, 4)); 96 v.add(new FieldContainer(9, 11, Field.ARGUMENT, 4)); 97 v.add(new FieldContainer(11, 15, DateFormat.Field.YEAR)); 98 v.add(new FieldContainer(11, 15, Field.ARGUMENT, 4)); 99 v.add(new FieldContainer(19, 20, DateFormat.Field.HOUR1)); 100 v.add(new FieldContainer(19, 20, Field.ARGUMENT, 3)); 101 v.add(new FieldContainer(20, 21, Field.ARGUMENT, 3)); 102 v.add(new FieldContainer(21, 23, DateFormat.Field.MINUTE)); 103 v.add(new FieldContainer(21, 23, Field.ARGUMENT, 3)); 104 v.add(new FieldContainer(23, 24, Field.ARGUMENT, 3)); 105 v.add(new FieldContainer(24, 26, DateFormat.Field.SECOND)); 106 v.add(new FieldContainer(24, 26, Field.ARGUMENT, 3)); 107 v.add(new FieldContainer(26, 27, Field.ARGUMENT, 3)); 108 v.add(new FieldContainer(27, 29, DateFormat.Field.AM_PM)); 109 v.add(new FieldContainer(27, 29, Field.ARGUMENT, 3)); 110 v.add(new FieldContainer(38, 39, NumberFormat.Field.INTEGER)); 111 v.add(new FieldContainer(38, 39, Field.ARGUMENT, 2)); 112 v.add(new FieldContainer(49, 50, Field.ARGUMENT, 2)); 113 v.add(new FieldContainer(61, 62, NumberFormat.Field.INTEGER)); 114 v.add(new FieldContainer(61, 62, Field.ARGUMENT, 1)); 115 v.add(new FieldContainer(62, 63, NumberFormat.Field.DECIMAL_SEPARATOR)); 116 v.add(new FieldContainer(62, 63, Field.ARGUMENT, 1)); 117 v.add(new FieldContainer(63, 64, NumberFormat.Field.FRACTION)); 118 v.add(new FieldContainer(63, 64, Field.ARGUMENT, 1)); 119 v.add(new FieldContainer(90, 106, Field.ARGUMENT, 0)); 120 return v; 121 } 122 123} 124