Home | History | Annotate | Download | only in support
      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 
     18 package tests.support;
     19 
     20 import java.text.DateFormat;
     21 import java.text.MessageFormat;
     22 import java.text.NumberFormat;
     23 import java.text.MessageFormat.Field;
     24 import java.util.Date;
     25 import java.util.GregorianCalendar;
     26 import java.util.Locale;
     27 import java.util.Vector;
     28 
     29 public class Support_MessageFormat extends Support_Format {
     30 
     31     public Support_MessageFormat(String p1) {
     32         super(p1);
     33     }
     34 
     35     @Override
     36     public void runTest() {
     37         t_formatToCharacterIterator();
     38         t_format_with_FieldPosition();
     39     }
     40 
     41     public static void main(String[] args) {
     42         new Support_MessageFormat("").runTest();
     43     }
     44 
     45     public void t_format_with_FieldPosition() {
     46 
     47         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!";
     48         MessageFormat format = new MessageFormat(pattern, Locale.US);
     49 
     50         Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime();
     51         Integer hamburgers = new Integer(8);
     52         Object[] objects = new Object[] { hamburgers, new Double(3.5),
     53                 hamburgers, date, date };
     54 
     55         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!";
     56 
     57         // test with MessageFormat.Field.ARGUMENT
     58         t_FormatWithField(1, format, objects, null, Field.ARGUMENT, 3, 15);
     59 
     60         // test other format fields that are included in the formatted text
     61         t_FormatWithField(2, format, objects, null, DateFormat.Field.AM_PM, 0,
     62                 0);
     63         t_FormatWithField(3, format, objects, null,
     64                 NumberFormat.Field.FRACTION, 0, 0);
     65 
     66         // test fields that are not included in the formatted text
     67         t_FormatWithField(4, format, objects, null, DateFormat.Field.ERA, 0, 0);
     68         t_FormatWithField(5, format, objects, null,
     69                 NumberFormat.Field.EXPONENT_SIGN, 0, 0);
     70     }
     71 
     72     public void t_formatToCharacterIterator() {
     73 
     74         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!";
     75         MessageFormat format = new MessageFormat(pattern, Locale.US);
     76 
     77         Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime();
     78         Integer hamburgers = new Integer(8);
     79         Object[] objects = new Object[] { hamburgers, new Double(3.5),
     80                 hamburgers, date, date };
     81 
     82         t_Format(1, objects, format, getMessageVector1());
     83     }
     84 
     85     private Vector<FieldContainer> getMessageVector1() {
     86         Vector<FieldContainer> v = new Vector<FieldContainer>();
     87         v.add(new FieldContainer(3, 6, Field.ARGUMENT, 4));
     88         v.add(new FieldContainer(3, 6, DateFormat.Field.MONTH));
     89         v.add(new FieldContainer(6, 7, Field.ARGUMENT, 4));
     90         v.add(new FieldContainer(7, 9, Field.ARGUMENT, 4));
     91         v.add(new FieldContainer(7, 9, DateFormat.Field.DAY_OF_MONTH));
     92         v.add(new FieldContainer(9, 11, Field.ARGUMENT, 4));
     93         v.add(new FieldContainer(11, 15, Field.ARGUMENT, 4));
     94         v.add(new FieldContainer(11, 15, DateFormat.Field.YEAR));
     95         v.add(new FieldContainer(19, 20, Field.ARGUMENT, 3));
     96         v.add(new FieldContainer(19, 20, DateFormat.Field.HOUR1));
     97         v.add(new FieldContainer(20, 21, Field.ARGUMENT, 3));
     98         v.add(new FieldContainer(21, 23, Field.ARGUMENT, 3));
     99         v.add(new FieldContainer(21, 23, DateFormat.Field.MINUTE));
    100         v.add(new FieldContainer(23, 24, Field.ARGUMENT, 3));
    101         v.add(new FieldContainer(24, 26, Field.ARGUMENT, 3));
    102         v.add(new FieldContainer(24, 26, DateFormat.Field.SECOND));
    103         v.add(new FieldContainer(26, 27, Field.ARGUMENT, 3));
    104         v.add(new FieldContainer(27, 29, Field.ARGUMENT, 3));
    105         v.add(new FieldContainer(27, 29, DateFormat.Field.AM_PM));
    106         v.add(new FieldContainer(38, 39, Field.ARGUMENT, 2));
    107         v.add(new FieldContainer(38, 39, NumberFormat.Field.INTEGER));
    108         v.add(new FieldContainer(49, 50, Field.ARGUMENT, 2));
    109         v.add(new FieldContainer(61, 62, Field.ARGUMENT, 1));
    110         v.add(new FieldContainer(61, 62, NumberFormat.Field.INTEGER));
    111         v.add(new FieldContainer(62, 63, Field.ARGUMENT, 1));
    112         v.add(new FieldContainer(62, 63, NumberFormat.Field.DECIMAL_SEPARATOR));
    113         v.add(new FieldContainer(63, 64, Field.ARGUMENT, 1));
    114         v.add(new FieldContainer(63, 64, NumberFormat.Field.FRACTION));
    115         v.add(new FieldContainer(90, 106, Field.ARGUMENT, 0));
    116         return v;
    117     }
    118 
    119 }
    120