3 *******************************************************************************
\r
4 * Copyright (C) 2007-2009, International Business Machines Corporation and *
\r
5 * others. All Rights Reserved. *
\r
6 *******************************************************************************
\r
8 package com.ibm.icu.dev.test.duration;
\r
10 import java.util.Date;
\r
11 import java.util.MissingResourceException;
\r
13 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
\r
15 import javax.xml.datatype.DatatypeConfigurationException;
\r
16 import javax.xml.datatype.DatatypeFactory;
\r
17 import javax.xml.datatype.Duration;
\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
29 public class ICUDurationTest extends TestFmwk {
\r
34 public ICUDurationTest() {
\r
40 public static void main(String[] args) {
\r
41 new ICUDurationTest().run(args);
\r
48 public void TestBasics() {
\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
59 logln("format duration -> " + formatted);
\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
69 logln("format date -> " + formatted);
\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
77 logln("format date from -> " + formatted);
\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
85 logln("format long obj -> " + formatted);
\r
89 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
\r
91 public void TestSimpleXMLDuration() {
\r
92 DatatypeFactory factory = null;
\r
94 factory = DatatypeFactory.newInstance();
\r
95 } catch (DatatypeConfigurationException e) {
\r
96 errln("Error instantiating XML DatatypeFactory.");
\r
97 e.printStackTrace();
\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
114 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\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
125 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\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
135 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\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
146 if(out.equals(expected2)){
\r
147 logln("WARNING: got " + out + " wanted " + expected + " from " + d);
\r
149 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\r
155 public void TestXMLDuration() {
\r
156 DatatypeFactory factory = null;
\r
158 factory = DatatypeFactory.newInstance();
\r
159 } catch (DatatypeConfigurationException e) {
\r
160 errln("Error instantiating XML DatatypeFactory.");
\r
161 e.printStackTrace();
\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
171 "en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",
\r
172 "it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",
\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
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
187 ULocale locale = new ULocale(loc);
\r
189 if(from.startsWith("#")) {
\r
190 d = factory.newDuration(Long.parseLong(from.substring(1)));
\r
192 d = factory.newDuration(from);
\r
195 DurationFormat df = DurationFormat.getInstance(locale);
\r
196 String output = df.format(d);
\r
198 if(output.equals(to)) {
\r
199 logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);
\r
201 logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);
\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
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
218 // try a second time, because it is a different code path for java < 1.5
\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
227 public void TestBadLocaleError() {
\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
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
243 errln("FAIL: null DurationFormat returned.");
\r