2 *******************************************************************************
\r
3 * Copyright (C) 2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.format;
\r
9 import java.text.ParseException;
\r
11 import com.ibm.icu.dev.test.TestFmwk;
\r
12 import com.ibm.icu.text.NumberFormat;
\r
13 import com.ibm.icu.text.TimeUnitFormat;
\r
14 import com.ibm.icu.util.TimeUnit;
\r
15 import com.ibm.icu.util.TimeUnitAmount;
\r
16 import com.ibm.icu.util.ULocale;
\r
22 public class TimeUnitTest extends TestFmwk {
\r
23 public static void main(String[] args) throws Exception{
\r
24 new TimeUnitTest().run(args);
\r
27 public void TestBasic() {
\r
28 String[] locales = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant"};
\r
29 for ( int locIndex = 0; locIndex < locales.length; ++locIndex ) {
\r
30 //System.out.println("locale: " + locales[locIndex]);
\r
31 Object[] formats = new Object[] {
\r
32 new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.FULL_NAME),
\r
33 new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.ABBREVIATED_NAME)
\r
35 for (int style = TimeUnitFormat.FULL_NAME;
\r
36 style <= TimeUnitFormat.ABBREVIATED_NAME;
\r
38 final TimeUnit[] values = TimeUnit.values();
\r
39 for (int j = 0; j < values.length; ++j) {
\r
40 final TimeUnit timeUnit = values[j];
\r
41 double[] tests = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35};
\r
42 for (int i = 0; i < tests.length; ++i) {
\r
43 TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
\r
44 String formatted = ((TimeUnitFormat)formats[style]).format(source);
\r
45 //System.out.println(formatted);
\r
46 logln(tests[i] + " => " + formatted);
\r
48 TimeUnitAmount result = (TimeUnitAmount) ((TimeUnitFormat)formats[style]).parseObject(formatted);
\r
49 if (result == null || !source.equals(result)) {
\r
50 errln("No round trip: " + source + " => " + formatted + " => " + result);
\r
52 // mix style parsing
\r
53 result = (TimeUnitAmount) ((TimeUnitFormat)formats[1 - style]).parseObject(formatted);
\r
54 if (result == null || !source.equals(result)) {
\r
55 errln("No round trip: " + source + " => " + formatted + " => " + result);
\r
57 } catch (ParseException e) {
\r
58 errln(e.getMessage());
\r
66 public void TestAPI() {
\r
67 TimeUnitFormat format = new TimeUnitFormat();
\r
68 format.setLocale(new ULocale("pt_BR"));
\r
69 formatParsing(format);
\r
70 format = new TimeUnitFormat(new ULocale("de"));
\r
71 formatParsing(format);
\r
72 format = new TimeUnitFormat(new ULocale("ja"));
\r
73 format.setNumberFormat(NumberFormat.getNumberInstance(new ULocale("en")));
\r
74 formatParsing(format);
\r
76 format = new TimeUnitFormat();
\r
77 ULocale es = new ULocale("es");
\r
78 format.setNumberFormat(NumberFormat.getNumberInstance(es));
\r
79 format.setLocale(es);
\r
80 formatParsing(format);
\r
84 private void formatParsing(TimeUnitFormat format) {
\r
85 final TimeUnit[] values = TimeUnit.values();
\r
86 for (int j = 0; j < values.length; ++j) {
\r
87 final TimeUnit timeUnit = values[j];
\r
88 double[] tests = {0, 0.5, 1, 2, 3, 5};
\r
89 for (int i = 0; i < tests.length; ++i) {
\r
90 TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
\r
91 String formatted = format.format(source);
\r
92 //System.out.println(formatted);
\r
93 logln(tests[i] + " => " + formatted);
\r
95 TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
\r
96 if (result == null || !source.equals(result)) {
\r
97 errln("No round trip: " + source + " => " + formatted + " => " + result);
\r
99 } catch (ParseException e) {
\r
100 errln(e.getMessage());
\r