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.text.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 48 String pattern = "On {4,date} at {3,time}, he ate {2,number, integer} hamburger{2,choice,1#|1<s} and drank {1, number} litres of coke. That was {0,choice,1#just enough|1<more than enough} food!"; 49 MessageFormat format = new MessageFormat(pattern, Locale.US); 50 51 Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime(); 52 Integer hamburgers = new Integer(8); 53 Object[] objects = new Object[] { hamburgers, new Double(3.5), 54 hamburgers, date, date }; 55 56 super.text = "On Feb 28, 2005 at 2:20:16 PM, he ate 8 hamburgers and drank 3.5 litres of coke. That was more than enough food!"; 57 58 // test with MessageFormat.Field.ARGUMENT 59 t_FormatWithField(1, format, objects, null, Field.ARGUMENT, 3, 15); 60 61 // test other format fields that are included in the formatted text 62 t_FormatWithField(2, format, objects, null, DateFormat.Field.AM_PM, 0, 63 0); 64 t_FormatWithField(3, format, objects, null, 65 NumberFormat.Field.FRACTION, 0, 0); 66 67 // test fields that are not included in the formatted text 68 t_FormatWithField(4, format, objects, null, DateFormat.Field.ERA, 0, 0); 69 t_FormatWithField(5, format, objects, null, 70 NumberFormat.Field.EXPONENT_SIGN, 0, 0); 71 } 72 73 public void t_formatToCharacterIterator() { 74 75 String pattern = "On {4,date} at {3,time}, he ate {2,number, integer} hamburger{2,choice,1#|1<s} and drank {1, number} litres of coke. That was {0,choice,1#just enough|1<more than enough} food!"; 76 MessageFormat format = new MessageFormat(pattern, Locale.US); 77 78 Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime(); 79 Integer hamburgers = new Integer(8); 80 Object[] objects = new Object[] { hamburgers, new Double(3.5), 81 hamburgers, date, date }; 82 83 t_Format(1, objects, format, getMessageVector1()); 84 } 85 86 private Vector<FieldContainer> getMessageVector1() { 87 Vector<FieldContainer> v = new Vector<FieldContainer>(); 88 v.add(new FieldContainer(3, 6, Field.ARGUMENT, 4)); 89 v.add(new FieldContainer(3, 6, DateFormat.Field.MONTH)); 90 v.add(new FieldContainer(6, 7, Field.ARGUMENT, 4)); 91 v.add(new FieldContainer(7, 9, Field.ARGUMENT, 4)); 92 v.add(new FieldContainer(7, 9, DateFormat.Field.DAY_OF_MONTH)); 93 v.add(new FieldContainer(9, 11, Field.ARGUMENT, 4)); 94 v.add(new FieldContainer(11, 15, Field.ARGUMENT, 4)); 95 v.add(new FieldContainer(11, 15, DateFormat.Field.YEAR)); 96 v.add(new FieldContainer(19, 20, Field.ARGUMENT, 3)); 97 v.add(new FieldContainer(19, 20, DateFormat.Field.HOUR1)); 98 v.add(new FieldContainer(20, 21, Field.ARGUMENT, 3)); 99 v.add(new FieldContainer(21, 23, Field.ARGUMENT, 3)); 100 v.add(new FieldContainer(21, 23, DateFormat.Field.MINUTE)); 101 v.add(new FieldContainer(23, 24, Field.ARGUMENT, 3)); 102 v.add(new FieldContainer(24, 26, Field.ARGUMENT, 3)); 103 v.add(new FieldContainer(24, 26, DateFormat.Field.SECOND)); 104 v.add(new FieldContainer(26, 27, Field.ARGUMENT, 3)); 105 v.add(new FieldContainer(27, 29, Field.ARGUMENT, 3)); 106 v.add(new FieldContainer(27, 29, DateFormat.Field.AM_PM)); 107 v.add(new FieldContainer(38, 39, Field.ARGUMENT, 2)); 108 v.add(new FieldContainer(38, 39, NumberFormat.Field.INTEGER)); 109 v.add(new FieldContainer(49, 50, Field.ARGUMENT, 2)); 110 v.add(new FieldContainer(61, 62, Field.ARGUMENT, 1)); 111 v.add(new FieldContainer(61, 62, NumberFormat.Field.INTEGER)); 112 v.add(new FieldContainer(62, 63, Field.ARGUMENT, 1)); 113 v.add(new FieldContainer(62, 63, NumberFormat.Field.DECIMAL_SEPARATOR)); 114 v.add(new FieldContainer(63, 64, Field.ARGUMENT, 1)); 115 v.add(new FieldContainer(63, 64, NumberFormat.Field.FRACTION)); 116 v.add(new FieldContainer(90, 106, Field.ARGUMENT, 0)); 117 return v; 118 } 119 120} 121