]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/duration/ICUDurationTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / duration / ICUDurationTest.java
1 //##header\r
2 /*\r
3  *******************************************************************************\r
4  * Copyright (C) 2007-2009, International Business Machines Corporation and    *\r
5  * others. All Rights Reserved.                                                *\r
6  *******************************************************************************\r
7  */\r
8 package com.ibm.icu.dev.test.duration;\r
9 \r
10 import java.util.Date;\r
11 import java.util.MissingResourceException;\r
12 \r
13 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)\r
14 //#else\r
15 import javax.xml.datatype.DatatypeConfigurationException;\r
16 import javax.xml.datatype.DatatypeFactory;\r
17 import javax.xml.datatype.Duration;\r
18 //#endif\r
19 \r
20 import com.ibm.icu.dev.test.TestFmwk;\r
21 import com.ibm.icu.text.DurationFormat;\r
22 import com.ibm.icu.util.Calendar;\r
23 import com.ibm.icu.util.ULocale;\r
24 \r
25 /**\r
26  * @author srl\r
27  *\r
28  */\r
29 public class ICUDurationTest extends TestFmwk {\r
30 \r
31     /**\r
32      * \r
33      */\r
34     public ICUDurationTest() {\r
35     }\r
36 \r
37     /**\r
38      * @param args\r
39      */\r
40     public static void main(String[] args) {\r
41         new ICUDurationTest().run(args);\r
42     }\r
43     \r
44     \r
45     /**\r
46      * Basic test\r
47      */\r
48     public void TestBasics() {\r
49         DurationFormat df;\r
50         String expect;\r
51         String formatted;\r
52         \r
53         df = DurationFormat.getInstance(new ULocale("it"));\r
54         formatted = df.formatDurationFromNow(4096);\r
55         expect = "fra quattro secondi";\r
56         if(!expect.equals(formatted)) {\r
57             errln("Expected " + expect + " but got " + formatted);\r
58         } else {\r
59             logln("format duration -> " + formatted);\r
60         }\r
61         \r
62         formatted = df.formatDurationFromNowTo(new Date(0));\r
63         Calendar cal = Calendar.getInstance();\r
64         int years = cal.get(Calendar.YEAR) - 1970; // year of Date(0)\r
65         expect = "fra " + years + " anni";\r
66         if(!expect.equals(formatted)) {\r
67             errln("Expected " + expect + " but got " + formatted);\r
68         } else {\r
69             logln("format date  -> " + formatted);\r
70         }\r
71         \r
72         formatted = df.formatDurationFrom(1000*3600*24, new Date(0).getTime());\r
73         expect = "fra un giorno";\r
74         if(!expect.equals(formatted)) {\r
75             errln("Expected " + expect + " but got " + formatted);\r
76         } else {\r
77             logln("format date from -> " + formatted);\r
78         }\r
79 \r
80         formatted = df.format(new Long(1000*3600*24*2));\r
81         expect = "fra due giorni";\r
82         if(!expect.equals(formatted)) {\r
83             errln("Expected " + expect + " but got " + formatted);\r
84         } else {\r
85             logln("format long obj -> " + formatted);\r
86         }\r
87     }\r
88 \r
89 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)\r
90 //#else\r
91     public void TestSimpleXMLDuration() {\r
92         DatatypeFactory factory = null;\r
93         try {\r
94             factory = DatatypeFactory.newInstance();\r
95         } catch (DatatypeConfigurationException e) {\r
96             errln("Error instantiating XML DatatypeFactory.");\r
97             e.printStackTrace();\r
98         }\r
99         \r
100         Duration d;\r
101         DurationFormat df;\r
102         String out;\r
103         String expected;\r
104         String expected2;\r
105         \r
106         // test 1\r
107         d = factory.newDuration("PT2H46M40S");\r
108         df = DurationFormat.getInstance(new ULocale("en"));\r
109         expected = "2 hours, 46 minutes, and 40 seconds";\r
110         out = df.format(d);\r
111         if(out.equals(expected)) {\r
112             logln("out=expected: " + expected + " from " + d);\r
113         } else {\r
114             errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
115         }\r
116         \r
117         // test 2\r
118         d = factory.newDuration(10000);\r
119         df = DurationFormat.getInstance(new ULocale("en"));\r
120         expected = "10 seconds";\r
121         out = df.format(d);\r
122         if(out.equals(expected)) {\r
123             logln("out=expected: " + expected + " from " + d);\r
124         } else {\r
125             errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
126         }\r
127         // test 3\r
128         d = factory.newDuration("P0DT0H0M10.0S");\r
129         df = DurationFormat.getInstance(new ULocale("en"));\r
130         expected = "10 seconds";\r
131         out = df.format(d);\r
132         if(out.equals(expected)) {\r
133             logln("out=expected: " + expected + " from " + d);\r
134         } else {\r
135             errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
136         }\r
137         // test 4\r
138         d = factory.newDuration(86400000);\r
139         df = DurationFormat.getInstance(new ULocale("en"));\r
140         expected = "1 day, 0 hours, 0 minutes, and 0 seconds";\r
141         expected2 = "1 day and 0 seconds"; // This is the expected result for Windows with IBM JRE6\r
142         out = df.format(d);\r
143         if(out.equals(expected)) {\r
144             logln("out=expected: " + expected + " from " + d);\r
145         } else {\r
146             if(out.equals(expected2)){\r
147                 logln("WARNING: got " + out + " wanted " + expected + " from " + d);\r
148             } else{\r
149                 errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
150             }\r
151         }\r
152     }\r
153 \r
154 \r
155     public void TestXMLDuration() {\r
156         DatatypeFactory factory = null;\r
157         try {\r
158             factory = DatatypeFactory.newInstance();\r
159         } catch (DatatypeConfigurationException e) {\r
160             errln("Error instantiating XML DatatypeFactory.");\r
161             e.printStackTrace();\r
162         }\r
163         \r
164         String cases[] = {\r
165                 "en",   "PT10.00099S",   "10 seconds",\r
166                 "en",   "#10000",   "10 seconds",\r
167                 "en",   "-PT10.00099S",   "10 seconds",\r
168                 "en",   "#-10000",   "10 seconds",\r
169                 \r
170                 // from BD req's\r
171                 "en",   "PT2H46M40S",   "2 hours, 46 minutes, and 40 seconds",\r
172                 "it",   "PT2H46M40S",   "due ore, 46 minuti e 40 secondi",\r
173                 \r
174                 // more cases\r
175                 "en",   "PT10S",        "10 seconds",\r
176                 "en",   "PT88M70S",        "88 minutes and 70 seconds",\r
177                 "en",   "PT10.100S",    "10 seconds and 100 milliseconds",\r
178                 "en",   "-PT10S",       "10 seconds",\r
179                 "en",   "PT0H5M0S",     "5 minutes and 0 seconds"\r
180         };\r
181         \r
182         for(int n=0;n<cases.length;n+=3) {\r
183             String loc = cases[n+0];\r
184             String from = cases[n+1];\r
185             String to = cases[n+2];\r
186             \r
187             ULocale locale = new ULocale(loc);\r
188             Duration d;\r
189             if(from.startsWith("#")) {\r
190                 d = factory.newDuration(Long.parseLong(from.substring(1)));\r
191             } else {\r
192                 d = factory.newDuration(from);\r
193             }\r
194             \r
195             DurationFormat df = DurationFormat.getInstance(locale);\r
196             String output = df.format(d);\r
197             \r
198             if(output.equals(to)) {\r
199                 logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);\r
200             } else {\r
201                 logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);\r
202             }\r
203         }\r
204     }\r
205 //#endif\r
206 \r
207 \r
208     public void TestBadObjectError() {\r
209         Runtime r = Runtime.getRuntime();\r
210         DurationFormat df = DurationFormat.getInstance(new ULocale("en"));\r
211         String output = null;\r
212         try {\r
213             output = df.format(r);\r
214             errln("FAIL: did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");\r
215         } catch (IllegalArgumentException iae) {\r
216             logln("PASS: expected: Caught iae: " + iae.toString() );\r
217         }\r
218         // try a second time, because it is a different code path for java < 1.5\r
219         try {\r
220             output = df.format(r);\r
221             errln("FAIL: [#2] did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");\r
222         } catch (IllegalArgumentException iae) {\r
223             logln("PASS: [#2] expected: Caught iae: " + iae.toString() );\r
224         }\r
225     }\r
226 \r
227     public void TestBadLocaleError() {\r
228         try {\r
229             DurationFormat df = DurationFormat.getInstance(new ULocale("und"));\r
230             df.format(new Date());\r
231             logln("Should have thrown err.");\r
232             errln("failed, should have thrown err.");\r
233         } catch(MissingResourceException mre) {\r
234             logln("PASS: caught missing resource exception on locale 'und'");\r
235             logln(mre.toString());\r
236         }\r
237     }\r
238 \r
239     public void TestResourceWithCalendar() {\r
240         DurationFormat df = DurationFormat.getInstance(new ULocale("th@calendar=buddhist"));\r
241         // should pass, but return a default formatter for th.\r
242         if (df == null) {\r
243             errln("FAIL: null DurationFormat returned.");\r
244         }\r
245     }\r
246 }\r