2 *******************************************************************************
\r
3 * Copyright (C) 2007-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.duration;
\r
9 import java.text.FieldPosition;
\r
10 import java.util.Date;
\r
11 import java.util.MissingResourceException;
\r
13 import javax.xml.datatype.DatatypeConfigurationException;
\r
14 import javax.xml.datatype.DatatypeFactory;
\r
15 import javax.xml.datatype.Duration;
\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
26 public class ICUDurationTest extends TestFmwk {
\r
31 public ICUDurationTest() {
\r
37 public static void main(String[] args) {
\r
38 new ICUDurationTest().run(args);
\r
45 public void TestBasics() {
\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
56 logln("format duration -> " + formatted);
\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
66 logln("format date -> " + formatted);
\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
74 logln("format date from -> " + formatted);
\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
82 logln("format long obj -> " + formatted);
\r
86 public void TestSimpleXMLDuration() {
\r
87 DatatypeFactory factory = null;
\r
89 factory = DatatypeFactory.newInstance();
\r
90 } catch (DatatypeConfigurationException e) {
\r
91 errln("Error instantiating XML DatatypeFactory.");
\r
92 e.printStackTrace();
\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
109 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\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
120 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\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
130 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\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
141 if(out.equals(expected2)){
\r
142 logln("WARNING: got " + out + " wanted " + expected + " from " + d);
\r
144 errln("FAIL: got " + out + " wanted " + expected + " from " + d);
\r
150 public void TestXMLDuration() {
\r
151 DatatypeFactory factory = null;
\r
153 factory = DatatypeFactory.newInstance();
\r
154 } catch (DatatypeConfigurationException e) {
\r
155 errln("Error instantiating XML DatatypeFactory.");
\r
156 e.printStackTrace();
\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
166 "en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",
\r
167 "it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",
\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
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
182 ULocale locale = new ULocale(loc);
\r
184 if(from.startsWith("#")) {
\r
185 d = factory.newDuration(Long.parseLong(from.substring(1)));
\r
187 d = factory.newDuration(from);
\r
190 DurationFormat df = DurationFormat.getInstance(locale);
\r
191 String output = df.format(d);
\r
193 if(output.equals(to)) {
\r
194 logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);
\r
196 logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);
\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
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
212 // try a second time, because it is a different code path for java < 1.5
\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
221 public void TestBadLocaleError() {
\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
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
237 errln("FAIL: null DurationFormat returned.");
\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
255 // Tests the constructor and the following method
\r
256 // public Object parseObject(String source, ParsePosition pos)
\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