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