001: /*****************************************************************************************
002: *
003: * Copyright (C) 1996-2004, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **/
006:
007: /**
008: * Port From: JDK 1.4b1 : java.text.Format.IntlTestDateFormatAPI
009: * Source File: java/text/format/IntlTestDateFormatAPI.java
010: **/package com.ibm.icu.dev.test.format;
011:
012: import com.ibm.icu.util.*;
013: import com.ibm.icu.text.*;
014: import java.util.Locale;
015: import java.util.Date;
016: import java.text.ParsePosition;
017: import java.text.FieldPosition;
018: import java.text.ParseException;
019:
020: public class IntlTestDateFormatAPI extends
021: com.ibm.icu.dev.test.TestFmwk {
022: public static void main(String[] args) throws Exception {
023: new IntlTestDateFormatAPI().run(args);
024: }
025:
026: // Test that the equals method works correctly.
027: public void TestEquals() {
028: // Create two objects at different system times
029: DateFormat a = DateFormat.getInstance();
030: Date start = Calendar.getInstance().getTime();
031: while (true) {
032: // changed to remove compiler warnings.
033: if (!start.equals(Calendar.getInstance().getTime())) {
034: break; // Wait for time to change
035: }
036: }
037: DateFormat b = DateFormat.getInstance();
038:
039: if (!(a.equals(b)))
040: errln("FAIL: DateFormat objects created at different times are unequal.");
041:
042: // Why has this test been disabled??? - aliu
043: // if (b instanceof SimpleDateFormat)
044: // {
045: // //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used
046: // try {
047: // ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
048: // if (a.equals(b))
049: // errln("FAIL: DateFormat objects with different two digit start dates are equal.");
050: // }
051: // catch (Exception e) {
052: // errln("FAIL: setTwoDigitStartDate failed.");
053: // }
054: // }
055: }
056:
057: // This test checks various generic API methods in DateFormat to achieve 100% API coverage.
058: public void TestAPI() {
059: logln("DateFormat API test---");
060: logln("");
061: Locale.setDefault(Locale.ENGLISH);
062:
063: // ======= Test constructors
064:
065: logln("Testing DateFormat constructors");
066:
067: DateFormat def = DateFormat.getInstance();
068: DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL,
069: Locale.FRENCH);
070: DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM,
071: Locale.ITALIAN);
072: DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG,
073: DateFormat.LONG, Locale.GERMAN);
074:
075: // ======= Test equality
076:
077: logln("Testing equality operator");
078:
079: if (fr.equals(it)) {
080: errln("ERROR: equals failed");
081: }
082:
083: // ======= Test various format() methods
084:
085: logln("Testing various format() methods");
086:
087: Date d = new Date((long) 837039928046.0);
088:
089: StringBuffer res1 = new StringBuffer();
090: StringBuffer res2 = new StringBuffer();
091: String res3 = new String();
092: FieldPosition pos1 = new FieldPosition(0);
093: FieldPosition pos2 = new FieldPosition(0);
094:
095: res1 = fr.format(d, res1, pos1);
096: logln("" + d.getTime() + " formatted to " + res1);
097:
098: res2 = it.format(d, res2, pos2);
099: logln("" + d.getTime() + " formatted to " + res2);
100:
101: res3 = de.format(d);
102: logln("" + d.getTime() + " formatted to " + res3);
103:
104: // ======= Test parse()
105:
106: logln("Testing parse()");
107:
108: String text = new String("02/03/76 2:50 AM, CST");
109: Object result1 = new Date();
110: Date result2 = new Date();
111: Date result3 = new Date();
112: ParsePosition pos = new ParsePosition(0);
113: ParsePosition pos01 = new ParsePosition(0);
114:
115: result1 = def.parseObject(text, pos);
116: if (result1 == null) {
117: errln("ERROR: parseObject() failed for " + text);
118: }
119: logln(text + " parsed into " + ((Date) result1).getTime());
120:
121: try {
122: result2 = def.parse(text);
123: } catch (ParseException e) {
124: errln("ERROR: parse() failed");
125: }
126: logln(text + " parsed into " + result2.getTime());
127:
128: result3 = def.parse(text, pos01);
129: if (result3 == null) {
130: errln("ERROR: parse() failed for " + text);
131: }
132: logln(text + " parsed into " + result3.getTime());
133:
134: // ======= Test getters and setters
135:
136: logln("Testing getters and setters");
137:
138: final Locale[] locales = DateFormat.getAvailableLocales();
139: long count = locales.length;
140: logln("Got " + count + " locales");
141: for (int i = 0; i < count; i++) {
142: String name;
143: name = locales[i].getDisplayName();
144: logln(name);
145: }
146:
147: fr.setLenient(it.isLenient());
148: if (fr.isLenient() != it.isLenient()) {
149: errln("ERROR: setLenient() failed");
150: }
151:
152: final Calendar cal = def.getCalendar();
153: Calendar newCal = (Calendar) cal.clone();
154: de.setCalendar(newCal);
155: it.setCalendar(newCal);
156: if (!de.getCalendar().equals(it.getCalendar())) {
157: errln("ERROR: set Calendar() failed");
158: }
159:
160: final NumberFormat nf = def.getNumberFormat();
161: NumberFormat newNf = (NumberFormat) nf.clone();
162: de.setNumberFormat(newNf);
163: it.setNumberFormat(newNf);
164: if (!de.getNumberFormat().equals(it.getNumberFormat())) {
165: errln("ERROR: set NumberFormat() failed");
166: }
167:
168: final TimeZone tz = def.getTimeZone();
169: TimeZone newTz = (TimeZone) tz.clone();
170: de.setTimeZone(newTz);
171: it.setTimeZone(newTz);
172: if (!de.getTimeZone().equals(it.getTimeZone())) {
173: errln("ERROR: set TimeZone() failed");
174: }
175:
176: // ======= Test getStaticClassID()
177:
178: // logln("Testing instanceof()");
179:
180: // try {
181: // DateFormat test = new SimpleDateFormat();
182:
183: // if (! (test instanceof SimpleDateFormat)) {
184: // errln("ERROR: instanceof failed");
185: // }
186: // }
187: // catch (Exception e) {
188: // errln("ERROR: Couldn't create a DateFormat");
189: // }
190: }
191: }
|