/* ******************************************************************************* * Copyright (C) 2007-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ package com.ibm.icu.dev.test.duration; import java.text.FieldPosition; import java.util.Date; import java.util.MissingResourceException; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.Duration; 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); } } 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