3 *******************************************************************************
4 * Copyright (C) 2007-2009, International Business Machines Corporation and *
5 * others. All Rights Reserved. *
6 *******************************************************************************
8 package com.ibm.icu.dev.test.duration;
10 import java.util.Date;
11 import java.util.MissingResourceException;
13 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
15 import javax.xml.datatype.DatatypeConfigurationException;
16 import javax.xml.datatype.DatatypeFactory;
17 import javax.xml.datatype.Duration;
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;
29 public class ICUDurationTest extends TestFmwk {
34 public ICUDurationTest() {
40 public static void main(String[] args) {
41 new ICUDurationTest().run(args);
48 public void TestBasics() {
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);
59 logln("format duration -> " + formatted);
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);
69 logln("format date -> " + formatted);
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);
77 logln("format date from -> " + formatted);
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);
85 logln("format long obj -> " + formatted);
89 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
91 public void TestSimpleXMLDuration() {
92 DatatypeFactory factory = null;
94 factory = DatatypeFactory.newInstance();
95 } catch (DatatypeConfigurationException e) {
96 errln("Error instantiating XML DatatypeFactory.");
107 d = factory.newDuration("PT2H46M40S");
108 df = DurationFormat.getInstance(new ULocale("en"));
109 expected = "2 hours, 46 minutes, and 40 seconds";
111 if(out.equals(expected)) {
112 logln("out=expected: " + expected + " from " + d);
114 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
118 d = factory.newDuration(10000);
119 df = DurationFormat.getInstance(new ULocale("en"));
120 expected = "10 seconds";
122 if(out.equals(expected)) {
123 logln("out=expected: " + expected + " from " + d);
125 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
128 d = factory.newDuration("P0DT0H0M10.0S");
129 df = DurationFormat.getInstance(new ULocale("en"));
130 expected = "10 seconds";
132 if(out.equals(expected)) {
133 logln("out=expected: " + expected + " from " + d);
135 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
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
143 if(out.equals(expected)) {
144 logln("out=expected: " + expected + " from " + d);
146 if(out.equals(expected2)){
147 logln("WARNING: got " + out + " wanted " + expected + " from " + d);
149 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
155 public void TestXMLDuration() {
156 DatatypeFactory factory = null;
158 factory = DatatypeFactory.newInstance();
159 } catch (DatatypeConfigurationException e) {
160 errln("Error instantiating XML DatatypeFactory.");
165 "en", "PT10.00099S", "10 seconds",
166 "en", "#10000", "10 seconds",
167 "en", "-PT10.00099S", "10 seconds",
168 "en", "#-10000", "10 seconds",
171 "en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",
172 "it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",
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"
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];
187 ULocale locale = new ULocale(loc);
189 if(from.startsWith("#")) {
190 d = factory.newDuration(Long.parseLong(from.substring(1)));
192 d = factory.newDuration(from);
195 DurationFormat df = DurationFormat.getInstance(locale);
196 String output = df.format(d);
198 if(output.equals(to)) {
199 logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);
201 logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);
208 public void TestBadObjectError() {
209 Runtime r = Runtime.getRuntime();
210 DurationFormat df = DurationFormat.getInstance(new ULocale("en"));
211 String output = null;
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() );
218 // try a second time, because it is a different code path for java < 1.5
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() );
227 public void TestBadLocaleError() {
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());
239 public void TestResourceWithCalendar() {
240 DurationFormat df = DurationFormat.getInstance(new ULocale("th@calendar=buddhist"));
241 // should pass, but return a default formatter for th.
243 errln("FAIL: null DurationFormat returned.");