174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes/*
2994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more
3994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * contributor license agreements.  See the NOTICE file distributed with
4994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * this work for additional information regarding copyright ownership.
5994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0
6994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * (the "License"); you may not use this file except in compliance with
7994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * the License.  You may obtain a copy of the License at
874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes *
9994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
1074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes *
11994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * Unless required by applicable law or agreed to in writing, software
12994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
13994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * See the License for the specific language governing permissions and
15994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes * limitations under the License.
16994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes */
17994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
18994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughespackage org.apache.harmony.tests.java.text;
19994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
20994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.DateFormat;
21994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.MessageFormat;
22994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.NumberFormat;
23994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.text.MessageFormat.Field;
24994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.util.Date;
25994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.util.GregorianCalendar;
26994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.util.Locale;
27994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughesimport java.util.Vector;
28994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
29994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
30994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughespublic class Support_MessageFormat extends Support_Format {
31994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
32994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	public Support_MessageFormat(String p1) {
33994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		super(p1);
34994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	}
35994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
36994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	@Override
37994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes    public void runTest() {
38994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_formatToCharacterIterator();
39994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_format_with_FieldPosition();
40994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	}
41994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
42994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	public static void main(String[] args) {
43994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		new Support_MessageFormat("").runTest();
44994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	}
45994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
46994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	public void t_format_with_FieldPosition() {
4738a0b85afd4231f46fac59e59ee38729bd5154c4Neil Fuller		// This test assumes a default DateFormat.is24Hour setting.
4838a0b85afd4231f46fac59e59ee38729bd5154c4Neil Fuller		DateFormat.is24Hour = null;
49994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
5074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes		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!";
51994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		MessageFormat format = new MessageFormat(pattern, Locale.US);
52994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
53994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime();
54994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		Integer hamburgers = new Integer(8);
55994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		Object[] objects = new Object[] { hamburgers, new Double(3.5),
56994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes				hamburgers, date, date };
57994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
5874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes		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!";
59994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
60994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		// test with MessageFormat.Field.ARGUMENT
61994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_FormatWithField(1, format, objects, null, Field.ARGUMENT, 3, 15);
62994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
63994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		// test other format fields that are included in the formatted text
64994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_FormatWithField(2, format, objects, null, DateFormat.Field.AM_PM, 0,
65994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes				0);
66994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_FormatWithField(3, format, objects, null,
67994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes				NumberFormat.Field.FRACTION, 0, 0);
68994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
69994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		// test fields that are not included in the formatted text
70994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_FormatWithField(4, format, objects, null, DateFormat.Field.ERA, 0, 0);
71994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_FormatWithField(5, format, objects, null,
72994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes				NumberFormat.Field.EXPONENT_SIGN, 0, 0);
73994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	}
74994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
75994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	public void t_formatToCharacterIterator() {
7638a0b85afd4231f46fac59e59ee38729bd5154c4Neil Fuller		// This test assumes a default DateFormat.is24Hour setting.
7738a0b85afd4231f46fac59e59ee38729bd5154c4Neil Fuller		DateFormat.is24Hour = null;
7838a0b85afd4231f46fac59e59ee38729bd5154c4Neil Fuller
7974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes		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!";
80994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		MessageFormat format = new MessageFormat(pattern, Locale.US);
81994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
82994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		Date date = new GregorianCalendar(2005, 1, 28, 14, 20, 16).getTime();
83994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		Integer hamburgers = new Integer(8);
8474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes		Object[] objects = new Object[] { hamburgers, new Double(3.5), hamburgers, date, date };
85994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
86994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes		t_Format(1, objects, format, getMessageVector1());
87994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes	}
88994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
8974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  private Vector<FieldContainer> getMessageVector1() {
9074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    Vector<FieldContainer> v = new Vector<FieldContainer>();
9174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(3, 6, DateFormat.Field.MONTH));
9274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(3, 6, Field.ARGUMENT, 4));
9374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(6, 7, Field.ARGUMENT, 4));
9474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(7, 9, DateFormat.Field.DAY_OF_MONTH));
9574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(7, 9, Field.ARGUMENT, 4));
9674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(9, 11, Field.ARGUMENT, 4));
9774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(11, 15, DateFormat.Field.YEAR));
9874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(11, 15, Field.ARGUMENT, 4));
9974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(19, 20, DateFormat.Field.HOUR1));
10074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(19, 20, Field.ARGUMENT, 3));
10174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(20, 21, Field.ARGUMENT, 3));
10274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(21, 23, DateFormat.Field.MINUTE));
10374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(21, 23, Field.ARGUMENT, 3));
10474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(23, 24, Field.ARGUMENT, 3));
10574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(24, 26, DateFormat.Field.SECOND));
10674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(24, 26, Field.ARGUMENT, 3));
10774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(26, 27, Field.ARGUMENT, 3));
10874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(27, 29, DateFormat.Field.AM_PM));
10974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(27, 29, Field.ARGUMENT, 3));
11074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(38, 39, NumberFormat.Field.INTEGER));
11174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(38, 39, Field.ARGUMENT, 2));
11274473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(49, 50, Field.ARGUMENT, 2));
11374473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(61, 62, NumberFormat.Field.INTEGER));
11474473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(61, 62, Field.ARGUMENT, 1));
11574473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(62, 63, NumberFormat.Field.DECIMAL_SEPARATOR));
11674473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(62, 63, Field.ARGUMENT, 1));
11774473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(63, 64, NumberFormat.Field.FRACTION));
11874473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(63, 64, Field.ARGUMENT, 1));
11974473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    v.add(new FieldContainer(90, 106, Field.ARGUMENT, 0));
12074473971cc9d960376295fbcc430320c9ed62991Elliott Hughes    return v;
12174473971cc9d960376295fbcc430320c9ed62991Elliott Hughes  }
122994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes
123994e4e5ded616a100ca42b16cffa36aa9f595f64Elliott Hughes}
124