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