2 *******************************************************************************
\r
3 * Copyright (C) 2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.format;
\r
9 import java.text.ParseException;
\r
10 import java.text.ParsePosition;
\r
11 import java.util.Locale;
\r
13 import com.ibm.icu.dev.test.TestFmwk;
\r
14 import com.ibm.icu.text.NumberFormat;
\r
15 import com.ibm.icu.text.TimeUnitFormat;
\r
16 import com.ibm.icu.util.TimeUnit;
\r
17 import com.ibm.icu.util.TimeUnitAmount;
\r
18 import com.ibm.icu.util.ULocale;
\r
24 public class TimeUnitTest extends TestFmwk {
\r
25 public static void main(String[] args) throws Exception{
\r
26 new TimeUnitTest().run(args);
\r
29 public void TestBasic() {
\r
30 String[] locales = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant"};
\r
31 for ( int locIndex = 0; locIndex < locales.length; ++locIndex ) {
\r
32 //System.out.println("locale: " + locales[locIndex]);
\r
33 Object[] formats = new Object[] {
\r
34 new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.FULL_NAME),
\r
35 new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.ABBREVIATED_NAME)
\r
37 for (int style = TimeUnitFormat.FULL_NAME;
\r
38 style <= TimeUnitFormat.ABBREVIATED_NAME;
\r
40 final TimeUnit[] values = TimeUnit.values();
\r
41 for (int j = 0; j < values.length; ++j) {
\r
42 final TimeUnit timeUnit = values[j];
\r
43 double[] tests = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35};
\r
44 for (int i = 0; i < tests.length; ++i) {
\r
45 TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
\r
46 String formatted = ((TimeUnitFormat)formats[style]).format(source);
\r
47 //System.out.println(formatted);
\r
48 logln(tests[i] + " => " + formatted);
\r
50 TimeUnitAmount result = (TimeUnitAmount) ((TimeUnitFormat)formats[style]).parseObject(formatted);
\r
51 if (result == null || !source.equals(result)) {
\r
52 errln("No round trip: " + source + " => " + formatted + " => " + result);
\r
54 // mix style parsing
\r
55 result = (TimeUnitAmount) ((TimeUnitFormat)formats[1 - style]).parseObject(formatted);
\r
56 if (result == null || !source.equals(result)) {
\r
57 errln("No round trip: " + source + " => " + formatted + " => " + result);
\r
59 } catch (ParseException e) {
\r
60 errln(e.getMessage());
\r
68 public void TestAPI() {
\r
69 TimeUnitFormat format = new TimeUnitFormat();
\r
70 format.setLocale(new ULocale("pt_BR"));
\r
71 formatParsing(format);
\r
72 format = new TimeUnitFormat(new ULocale("de"));
\r
73 formatParsing(format);
\r
74 format = new TimeUnitFormat(new ULocale("ja"));
\r
75 format.setNumberFormat(NumberFormat.getNumberInstance(new ULocale("en")));
\r
76 formatParsing(format);
\r
78 format = new TimeUnitFormat();
\r
79 ULocale es = new ULocale("es");
\r
80 format.setNumberFormat(NumberFormat.getNumberInstance(es));
\r
81 format.setLocale(es);
\r
82 formatParsing(format);
\r
84 format.setLocale(new Locale("pt_BR"));
\r
85 formatParsing(format);
\r
86 format = new TimeUnitFormat(new Locale("de"));
\r
87 formatParsing(format);
\r
88 format = new TimeUnitFormat(new Locale("ja"));
\r
89 format.setNumberFormat(NumberFormat.getNumberInstance(new Locale("en")));
\r
90 formatParsing(format);
\r
93 private void formatParsing(TimeUnitFormat format) {
\r
94 final TimeUnit[] values = TimeUnit.values();
\r
95 for (int j = 0; j < values.length; ++j) {
\r
96 final TimeUnit timeUnit = values[j];
\r
97 double[] tests = {0, 0.5, 1, 2, 3, 5};
\r
98 for (int i = 0; i < tests.length; ++i) {
\r
99 TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
\r
100 String formatted = format.format(source);
\r
101 //System.out.println(formatted);
\r
102 logln(tests[i] + " => " + formatted);
\r
104 TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
\r
105 if (result == null || !source.equals(result)) {
\r
106 errln("No round trip: " + source + " => " + formatted + " => " + result);
\r
108 } catch (ParseException e) {
\r
109 errln(e.getMessage());
\r
116 * Tests the method public TimeUnitFormat(ULocale locale, int style), public TimeUnitFormat(Locale locale, int style)
\r
118 @SuppressWarnings("unused")
\r
119 public void TestTimeUnitFormat() {
\r
120 // Tests when "if (style < FULL_NAME || style >= TOTAL_STYLES)" is true
\r
121 // TOTAL_STYLES is 2
\r
122 int[] cases = { TimeUnitFormat.FULL_NAME - 1, TimeUnitFormat.FULL_NAME - 2, 2, 3 };
\r
123 for (int i = 0; i < cases.length; i++) {
\r
125 TimeUnitFormat tuf = new TimeUnitFormat(new ULocale("en_US"), cases[i]);
\r
126 errln("TimeUnitFormat(ULocale,int) was suppose to return an " + "exception for a style value of "
\r
127 + cases[i] + "passed into the constructor.");
\r
128 } catch (Exception e) {
\r
131 for (int i = 0; i < cases.length; i++) {
\r
133 TimeUnitFormat tuf = new TimeUnitFormat(new Locale("en_US"), cases[i]);
\r
134 errln("TimeUnitFormat(ULocale,int) was suppose to return an " + "exception for a style value of "
\r
135 + cases[i] + "passed into the constructor.");
\r
136 } catch (Exception e) {
\r
142 * Tests the method public TimeUnitFormat setLocale(ULocale locale) public TimeUnitFormat setLocale(Locale locale)
\r
144 public void TestSetLocale() {
\r
145 // Tests when "if ( locale != this.locale )" is false
\r
146 TimeUnitFormat tuf = new TimeUnitFormat(new ULocale("en_US"));
\r
147 if (!tuf.setLocale(new ULocale("en_US")).equals(tuf) && !tuf.setLocale(new Locale("en_US")).equals(tuf)) {
\r
148 errln("TimeUnitFormat.setLocale(ULocale) was suppose to "
\r
149 + "return the same TimeUnitFormat object if the same " + "ULocale is entered as a parameter.");
\r
154 * Tests the method public TimeUnitFormat setNumberFormat(NumberFormat format)
\r
156 public void TestSetNumberFormat() {
\r
157 TimeUnitFormat tuf = new TimeUnitFormat();
\r
159 // Tests when "if (format == this.format)" is false
\r
160 // Tests when "if ( format == null )" is false
\r
161 tuf.setNumberFormat(NumberFormat.getInstance());
\r
163 // Tests when "if (format == this.format)" is true
\r
164 if (!tuf.setNumberFormat(NumberFormat.getInstance()).equals(tuf)) {
\r
165 errln("TimeUnitFormat.setNumberFormat(NumberFormat) was suppose to "
\r
166 + "return the same object when the same NumberFormat is passed.");
\r
169 // Tests when "if ( format == null )" is true
\r
170 // Tests when "if ( locale == null )" is true
\r
171 if (!tuf.setNumberFormat(null).equals(tuf)) {
\r
172 errln("TimeUnitFormat.setNumberFormat(NumberFormat) was suppose to "
\r
173 + "return the same object when null is passed.");
\r
176 TimeUnitFormat tuf1 = new TimeUnitFormat(new ULocale("en_US"));
\r
178 // Tests when "if ( locale == null )" is false
\r
179 tuf1.setNumberFormat(NumberFormat.getInstance());
\r
180 tuf1.setNumberFormat(null);
\r
184 * Tests the method public StringBuffer format(Object obj, ...
\r
186 public void TestFormat() {
\r
187 TimeUnitFormat tuf = new TimeUnitFormat();
\r
189 tuf.format(new Integer("1"), null, null);
\r
190 errln("TimeUnitFormat.format(Object,StringBuffer,FieldPosition) "
\r
191 + "was suppose to return an exception because the Object "
\r
192 + "parameter was not of type TimeUnitAmount.");
\r
193 } catch (Exception e) {
\r
197 /* Tests the method private void setup() from
\r
198 * public Object parseObject(String source, ParsePosition pos)
\r
201 public void TestSetup(){
\r
202 TimeUnitFormat tuf = new TimeUnitFormat();
\r
203 tuf.parseObject("", new ParsePosition(0));
\r
205 TimeUnitFormat tuf1 = new TimeUnitFormat();
\r
206 tuf1.setNumberFormat(NumberFormat.getInstance());
\r
207 tuf1.parseObject("", new ParsePosition(0));
\r