-//##header\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 2007-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.dev.test.duration;\r
-\r
-import java.util.Date;\r
-import java.util.MissingResourceException;\r
-\r
-//#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)\r
-//#else\r
-import javax.xml.datatype.DatatypeConfigurationException;\r
-import javax.xml.datatype.DatatypeFactory;\r
-import javax.xml.datatype.Duration;\r
-//#endif\r
-\r
-import com.ibm.icu.dev.test.TestFmwk;\r
-import com.ibm.icu.text.DurationFormat;\r
-import com.ibm.icu.util.Calendar;\r
-import com.ibm.icu.util.ULocale;\r
-\r
-/**\r
- * @author srl\r
- *\r
- */\r
-public class ICUDurationTest extends TestFmwk {\r
-\r
- /**\r
- * \r
- */\r
- public ICUDurationTest() {\r
- }\r
-\r
- /**\r
- * @param args\r
- */\r
- public static void main(String[] args) {\r
- new ICUDurationTest().run(args);\r
- }\r
- \r
- \r
- /**\r
- * Basic test\r
- */\r
- public void TestBasics() {\r
- DurationFormat df;\r
- String expect;\r
- String formatted;\r
- \r
- df = DurationFormat.getInstance(new ULocale("it"));\r
- formatted = df.formatDurationFromNow(4096);\r
- expect = "fra quattro secondi";\r
- if(!expect.equals(formatted)) {\r
- errln("Expected " + expect + " but got " + formatted);\r
- } else {\r
- logln("format duration -> " + formatted);\r
- }\r
- \r
- formatted = df.formatDurationFromNowTo(new Date(0));\r
- Calendar cal = Calendar.getInstance();\r
- int years = cal.get(Calendar.YEAR) - 1970; // year of Date(0)\r
- expect = "fra " + years + " anni";\r
- if(!expect.equals(formatted)) {\r
- errln("Expected " + expect + " but got " + formatted);\r
- } else {\r
- logln("format date -> " + formatted);\r
- }\r
- \r
- formatted = df.formatDurationFrom(1000*3600*24, new Date(0).getTime());\r
- expect = "fra un giorno";\r
- if(!expect.equals(formatted)) {\r
- errln("Expected " + expect + " but got " + formatted);\r
- } else {\r
- logln("format date from -> " + formatted);\r
- }\r
-\r
- formatted = df.format(new Long(1000*3600*24*2));\r
- expect = "fra due giorni";\r
- if(!expect.equals(formatted)) {\r
- errln("Expected " + expect + " but got " + formatted);\r
- } else {\r
- logln("format long obj -> " + formatted);\r
- }\r
- }\r
-\r
-//#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)\r
-//#else\r
- public void TestSimpleXMLDuration() {\r
- DatatypeFactory factory = null;\r
- try {\r
- factory = DatatypeFactory.newInstance();\r
- } catch (DatatypeConfigurationException e) {\r
- errln("Error instantiating XML DatatypeFactory.");\r
- e.printStackTrace();\r
- }\r
- \r
- Duration d;\r
- DurationFormat df;\r
- String out;\r
- String expected;\r
- String expected2;\r
- \r
- // test 1\r
- d = factory.newDuration("PT2H46M40S");\r
- df = DurationFormat.getInstance(new ULocale("en"));\r
- expected = "2 hours, 46 minutes, and 40 seconds";\r
- out = df.format(d);\r
- if(out.equals(expected)) {\r
- logln("out=expected: " + expected + " from " + d);\r
- } else {\r
- errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
- }\r
- \r
- // test 2\r
- d = factory.newDuration(10000);\r
- df = DurationFormat.getInstance(new ULocale("en"));\r
- expected = "10 seconds";\r
- out = df.format(d);\r
- if(out.equals(expected)) {\r
- logln("out=expected: " + expected + " from " + d);\r
- } else {\r
- errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
- }\r
- // test 3\r
- d = factory.newDuration("P0DT0H0M10.0S");\r
- df = DurationFormat.getInstance(new ULocale("en"));\r
- expected = "10 seconds";\r
- out = df.format(d);\r
- if(out.equals(expected)) {\r
- logln("out=expected: " + expected + " from " + d);\r
- } else {\r
- errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
- }\r
- // test 4\r
- d = factory.newDuration(86400000);\r
- df = DurationFormat.getInstance(new ULocale("en"));\r
- expected = "1 day, 0 hours, 0 minutes, and 0 seconds";\r
- expected2 = "1 day and 0 seconds"; // This is the expected result for Windows with IBM JRE6\r
- out = df.format(d);\r
- if(out.equals(expected)) {\r
- logln("out=expected: " + expected + " from " + d);\r
- } else {\r
- if(out.equals(expected2)){\r
- logln("WARNING: got " + out + " wanted " + expected + " from " + d);\r
- } else{\r
- errln("FAIL: got " + out + " wanted " + expected + " from " + d);\r
- }\r
- }\r
- }\r
-\r
-\r
- public void TestXMLDuration() {\r
- DatatypeFactory factory = null;\r
- try {\r
- factory = DatatypeFactory.newInstance();\r
- } catch (DatatypeConfigurationException e) {\r
- errln("Error instantiating XML DatatypeFactory.");\r
- e.printStackTrace();\r
- }\r
- \r
- String cases[] = {\r
- "en", "PT10.00099S", "10 seconds",\r
- "en", "#10000", "10 seconds",\r
- "en", "-PT10.00099S", "10 seconds",\r
- "en", "#-10000", "10 seconds",\r
- \r
- // from BD req's\r
- "en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",\r
- "it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",\r
- \r
- // more cases\r
- "en", "PT10S", "10 seconds",\r
- "en", "PT88M70S", "88 minutes and 70 seconds",\r
- "en", "PT10.100S", "10 seconds and 100 milliseconds",\r
- "en", "-PT10S", "10 seconds",\r
- "en", "PT0H5M0S", "5 minutes and 0 seconds"\r
- };\r
- \r
- for(int n=0;n<cases.length;n+=3) {\r
- String loc = cases[n+0];\r
- String from = cases[n+1];\r
- String to = cases[n+2];\r
- \r
- ULocale locale = new ULocale(loc);\r
- Duration d;\r
- if(from.startsWith("#")) {\r
- d = factory.newDuration(Long.parseLong(from.substring(1)));\r
- } else {\r
- d = factory.newDuration(from);\r
- }\r
- \r
- DurationFormat df = DurationFormat.getInstance(locale);\r
- String output = df.format(d);\r
- \r
- if(output.equals(to)) {\r
- logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);\r
- } else {\r
- logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);\r
- }\r
- }\r
- }\r
-//#endif\r
-\r
-\r
- public void TestBadObjectError() {\r
- Runtime r = Runtime.getRuntime();\r
- DurationFormat df = DurationFormat.getInstance(new ULocale("en"));\r
- String output = null;\r
- try {\r
- output = df.format(r);\r
- errln("FAIL: did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");\r
- } catch (IllegalArgumentException iae) {\r
- logln("PASS: expected: Caught iae: " + iae.toString() );\r
- }\r
- // try a second time, because it is a different code path for java < 1.5\r
- try {\r
- output = df.format(r);\r
- errln("FAIL: [#2] did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");\r
- } catch (IllegalArgumentException iae) {\r
- logln("PASS: [#2] expected: Caught iae: " + iae.toString() );\r
- }\r
- }\r
-\r
- public void TestBadLocaleError() {\r
- try {\r
- DurationFormat df = DurationFormat.getInstance(new ULocale("und"));\r
- df.format(new Date());\r
- logln("Should have thrown err.");\r
- errln("failed, should have thrown err.");\r
- } catch(MissingResourceException mre) {\r
- logln("PASS: caught missing resource exception on locale 'und'");\r
- logln(mre.toString());\r
- }\r
- }\r
-\r
- public void TestResourceWithCalendar() {\r
- DurationFormat df = DurationFormat.getInstance(new ULocale("th@calendar=buddhist"));\r
- // should pass, but return a default formatter for th.\r
- if (df == null) {\r
- errln("FAIL: null DurationFormat returned.");\r
- }\r
- }\r
-}\r
+//##header J2SE15
+/*
+ *******************************************************************************
+ * Copyright (C) 2007-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+package com.ibm.icu.dev.test.duration;
+
+import java.util.Date;
+import java.util.MissingResourceException;
+
+//#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
+//#else
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.Duration;
+//#endif
+
+import com.ibm.icu.dev.test.TestFmwk;
+import com.ibm.icu.text.DurationFormat;
+import com.ibm.icu.util.Calendar;
+import com.ibm.icu.util.ULocale;
+
+/**
+ * @author srl
+ *
+ */
+public class ICUDurationTest extends TestFmwk {
+
+ /**
+ *
+ */
+ public ICUDurationTest() {
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ new ICUDurationTest().run(args);
+ }
+
+
+ /**
+ * Basic test
+ */
+ public void TestBasics() {
+ DurationFormat df;
+ String expect;
+ String formatted;
+
+ df = DurationFormat.getInstance(new ULocale("it"));
+ formatted = df.formatDurationFromNow(4096);
+ expect = "fra quattro secondi";
+ if(!expect.equals(formatted)) {
+ errln("Expected " + expect + " but got " + formatted);
+ } else {
+ logln("format duration -> " + formatted);
+ }
+
+ formatted = df.formatDurationFromNowTo(new Date(0));
+ Calendar cal = Calendar.getInstance();
+ int years = cal.get(Calendar.YEAR) - 1970; // year of Date(0)
+ expect = "fra " + years + " anni";
+ if(!expect.equals(formatted)) {
+ errln("Expected " + expect + " but got " + formatted);
+ } else {
+ logln("format date -> " + formatted);
+ }
+
+ formatted = df.formatDurationFrom(1000*3600*24, new Date(0).getTime());
+ expect = "fra un giorno";
+ if(!expect.equals(formatted)) {
+ errln("Expected " + expect + " but got " + formatted);
+ } else {
+ logln("format date from -> " + formatted);
+ }
+
+ formatted = df.format(new Long(1000*3600*24*2));
+ expect = "fra due giorni";
+ if(!expect.equals(formatted)) {
+ errln("Expected " + expect + " but got " + formatted);
+ } else {
+ logln("format long obj -> " + formatted);
+ }
+ }
+
+//#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
+//#else
+ public void TestSimpleXMLDuration() {
+ DatatypeFactory factory = null;
+ try {
+ factory = DatatypeFactory.newInstance();
+ } catch (DatatypeConfigurationException e) {
+ errln("Error instantiating XML DatatypeFactory.");
+ e.printStackTrace();
+ }
+
+ Duration d;
+ DurationFormat df;
+ String out;
+ String expected;
+ String expected2;
+
+ // test 1
+ d = factory.newDuration("PT2H46M40S");
+ df = DurationFormat.getInstance(new ULocale("en"));
+ expected = "2 hours, 46 minutes, and 40 seconds";
+ out = df.format(d);
+ if(out.equals(expected)) {
+ logln("out=expected: " + expected + " from " + d);
+ } else {
+ errln("FAIL: got " + out + " wanted " + expected + " from " + d);
+ }
+
+ // test 2
+ d = factory.newDuration(10000);
+ df = DurationFormat.getInstance(new ULocale("en"));
+ expected = "10 seconds";
+ out = df.format(d);
+ if(out.equals(expected)) {
+ logln("out=expected: " + expected + " from " + d);
+ } else {
+ errln("FAIL: got " + out + " wanted " + expected + " from " + d);
+ }
+ // test 3
+ d = factory.newDuration("P0DT0H0M10.0S");
+ df = DurationFormat.getInstance(new ULocale("en"));
+ expected = "10 seconds";
+ out = df.format(d);
+ if(out.equals(expected)) {
+ logln("out=expected: " + expected + " from " + d);
+ } else {
+ errln("FAIL: got " + out + " wanted " + expected + " from " + d);
+ }
+ // test 4
+ d = factory.newDuration(86400000);
+ df = DurationFormat.getInstance(new ULocale("en"));
+ expected = "1 day, 0 hours, 0 minutes, and 0 seconds";
+ expected2 = "1 day and 0 seconds"; // This is the expected result for Windows with IBM JRE6
+ out = df.format(d);
+ if(out.equals(expected)) {
+ logln("out=expected: " + expected + " from " + d);
+ } else {
+ if(out.equals(expected2)){
+ logln("WARNING: got " + out + " wanted " + expected + " from " + d);
+ } else{
+ errln("FAIL: got " + out + " wanted " + expected + " from " + d);
+ }
+ }
+ }
+
+
+ public void TestXMLDuration() {
+ DatatypeFactory factory = null;
+ try {
+ factory = DatatypeFactory.newInstance();
+ } catch (DatatypeConfigurationException e) {
+ errln("Error instantiating XML DatatypeFactory.");
+ e.printStackTrace();
+ }
+
+ String cases[] = {
+ "en", "PT10.00099S", "10 seconds",
+ "en", "#10000", "10 seconds",
+ "en", "-PT10.00099S", "10 seconds",
+ "en", "#-10000", "10 seconds",
+
+ // from BD req's
+ "en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",
+ "it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",
+
+ // more cases
+ "en", "PT10S", "10 seconds",
+ "en", "PT88M70S", "88 minutes and 70 seconds",
+ "en", "PT10.100S", "10 seconds and 100 milliseconds",
+ "en", "-PT10S", "10 seconds",
+ "en", "PT0H5M0S", "5 minutes and 0 seconds"
+ };
+
+ for(int n=0;n<cases.length;n+=3) {
+ String loc = cases[n+0];
+ String from = cases[n+1];
+ String to = cases[n+2];
+
+ ULocale locale = new ULocale(loc);
+ Duration d;
+ if(from.startsWith("#")) {
+ d = factory.newDuration(Long.parseLong(from.substring(1)));
+ } else {
+ d = factory.newDuration(from);
+ }
+
+ DurationFormat df = DurationFormat.getInstance(locale);
+ String output = df.format(d);
+
+ if(output.equals(to)) {
+ logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);
+ } else {
+ logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);
+ }
+ }
+ }
+//#endif
+
+
+ public void TestBadObjectError() {
+ Runtime r = Runtime.getRuntime();
+ DurationFormat df = DurationFormat.getInstance(new ULocale("en"));
+ String output = null;
+ try {
+ output = df.format(r);
+ errln("FAIL: did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
+ } catch (IllegalArgumentException iae) {
+ logln("PASS: expected: Caught iae: " + iae.toString() );
+ }
+ // try a second time, because it is a different code path for java < 1.5
+ try {
+ output = df.format(r);
+ errln("FAIL: [#2] did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
+ } catch (IllegalArgumentException iae) {
+ logln("PASS: [#2] expected: Caught iae: " + iae.toString() );
+ }
+ }
+
+ public void TestBadLocaleError() {
+ try {
+ DurationFormat df = DurationFormat.getInstance(new ULocale("und"));
+ df.format(new Date());
+ logln("Should have thrown err.");
+ errln("failed, should have thrown err.");
+ } catch(MissingResourceException mre) {
+ logln("PASS: caught missing resource exception on locale 'und'");
+ logln(mre.toString());
+ }
+ }
+
+ public void TestResourceWithCalendar() {
+ DurationFormat df = DurationFormat.getInstance(new ULocale("th@calendar=buddhist"));
+ // should pass, but return a default formatter for th.
+ if (df == null) {
+ errln("FAIL: null DurationFormat returned.");
+ }
+ }
+}