]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/format/TimeUnitTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / format / TimeUnitTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2009, International Business Machines Corporation and         *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.format;\r
8 \r
9 import java.text.ParseException;\r
10 import java.text.ParsePosition;\r
11 import java.util.Locale;\r
12 \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
19 \r
20 /**\r
21  * @author markdavis\r
22  *\r
23  */\r
24 public class TimeUnitTest extends TestFmwk {\r
25     public static void main(String[] args) throws Exception{\r
26         new TimeUnitTest().run(args);\r
27     }\r
28 \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
36             };\r
37             for (int style = TimeUnitFormat.FULL_NAME;\r
38                  style <= TimeUnitFormat.ABBREVIATED_NAME;\r
39                  ++style) {\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
49                         try {\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
53                             }\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
58                             }\r
59                         } catch (ParseException e) {\r
60                             errln(e.getMessage());\r
61                         }\r
62                     }\r
63                 }\r
64             }\r
65         }\r
66     }\r
67 \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
77 \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
83         \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
91     }\r
92 \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
103                 try {\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
107                     }\r
108                 } catch (ParseException e) {\r
109                     errln(e.getMessage());\r
110                 }\r
111             }\r
112         }\r
113     }\r
114     \r
115     /*\r
116      * Tests the method public TimeUnitFormat(ULocale locale, int style), public TimeUnitFormat(Locale locale, int style)\r
117      */\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
124             try {\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
129             }\r
130         }\r
131         for (int i = 0; i < cases.length; i++) {\r
132             try {\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
137             }\r
138         }\r
139     }\r
140     \r
141     /*\r
142      * Tests the method public TimeUnitFormat setLocale(ULocale locale) public TimeUnitFormat setLocale(Locale locale)\r
143      */\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
150         }\r
151     }\r
152 \r
153     /*\r
154      * Tests the method public TimeUnitFormat setNumberFormat(NumberFormat format)\r
155      */\r
156     public void TestSetNumberFormat() {\r
157         TimeUnitFormat tuf = new TimeUnitFormat();\r
158 \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
162 \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
167         }\r
168 \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
174         }\r
175 \r
176         TimeUnitFormat tuf1 = new TimeUnitFormat(new ULocale("en_US"));\r
177 \r
178         // Tests when "if ( locale == null )" is false\r
179         tuf1.setNumberFormat(NumberFormat.getInstance());\r
180         tuf1.setNumberFormat(null);\r
181     }\r
182     \r
183     /*\r
184      * Tests the method public StringBuffer format(Object obj, ...\r
185      */\r
186     public void TestFormat() {\r
187         TimeUnitFormat tuf = new TimeUnitFormat();\r
188         try {\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
194         }\r
195     }\r
196     \r
197     /* Tests the method private void setup() from\r
198      * public Object parseObject(String source, ParsePosition pos)\r
199      * \r
200      */\r
201     public void TestSetup(){\r
202         TimeUnitFormat tuf = new TimeUnitFormat();\r
203         tuf.parseObject("", new ParsePosition(0));\r
204         \r
205         TimeUnitFormat tuf1 = new TimeUnitFormat();\r
206         tuf1.setNumberFormat(NumberFormat.getInstance());\r
207         tuf1.parseObject("", new ParsePosition(0));\r
208     }\r
209 }\r