001: /*****************************************************************************************
002: * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
003: * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved
004: *
005: * The original version of this source code and documentation is copyrighted and
006: * owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
007: * provided under terms of a License Agreement between Taligent and Sun. This
008: * technology is protected by multiple US and International patents. This notice and
009: * attribution to Taligent may not be removed.
010: * Taligent is a registered trademark of Taligent, Inc.
011: **/
012:
013: /**
014: * Port From: JDK 1.4b1 : java.text.Format.IntlTestSimpleDateFormatAPI
015: * Source File: java/text/format/IntlTestSimpleDateFormatAPI.java
016: **/package com.ibm.icu.dev.test.format;
017:
018: import com.ibm.icu.text.*;
019: import java.util.Locale;
020: import java.util.Date;
021: import java.text.ParsePosition;
022: import java.text.Format;
023: import java.text.FieldPosition;
024: import java.text.ParseException;
025:
026: public class IntlTestSimpleDateFormatAPI extends
027: com.ibm.icu.dev.test.TestFmwk {
028: public static void main(String[] args) throws Exception {
029: new IntlTestSimpleDateFormatAPI().run(args);
030: }
031:
032: // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
033: public void TestAPI() {
034: logln("SimpleDateFormat API test---");
035: logln("");
036:
037: Locale.setDefault(Locale.ENGLISH);
038:
039: // ======= Test constructors
040:
041: logln("Testing SimpleDateFormat constructors");
042:
043: SimpleDateFormat def = new SimpleDateFormat();
044:
045: final String pattern = new String(
046: "yyyy.MM.dd G 'at' hh:mm:ss z");
047: SimpleDateFormat pat = new SimpleDateFormat(pattern);
048:
049: SimpleDateFormat pat_fr = new SimpleDateFormat(pattern,
050: Locale.FRENCH);
051:
052: DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
053:
054: SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
055:
056: // ======= Test clone() and equality
057:
058: logln("Testing clone(), assignment and equality operators");
059:
060: Format clone = (Format) def.clone();
061: if (!clone.equals(def)) {
062: errln("ERROR: Format clone or equals failed");
063: }
064:
065: // ======= Test various format() methods
066:
067: logln("Testing various format() methods");
068:
069: Date d = new Date((long) 837039928046.0);
070:
071: StringBuffer res1 = new StringBuffer();
072: StringBuffer res2 = new StringBuffer();
073: FieldPosition pos1 = new FieldPosition(0);
074: FieldPosition pos2 = new FieldPosition(0);
075:
076: res1 = def.format(d, res1, pos1);
077: logln("" + d.getTime() + " formatted to " + res1);
078:
079: res2 = cust1.format(d, res2, pos2);
080: logln("" + d.getTime() + " formatted to " + res2);
081:
082: // ======= Test parse()
083:
084: logln("Testing parse()");
085:
086: String text = new String("02/03/76 2:50 AM, CST");
087: Date result1 = new Date();
088: Date result2 = new Date();
089: ParsePosition pos = new ParsePosition(0);
090: result1 = def.parse(text, pos);
091: logln(text + " parsed into " + result1);
092:
093: try {
094: result2 = def.parse(text);
095: } catch (ParseException e) {
096: errln("ERROR: parse() failed");
097: }
098: logln(text + " parsed into " + result2);
099:
100: // ======= Test getters and setters
101:
102: logln("Testing getters and setters");
103:
104: final DateFormatSymbols syms = pat.getDateFormatSymbols();
105: def.setDateFormatSymbols(syms);
106: pat_fr.setDateFormatSymbols(syms);
107: if (!pat.getDateFormatSymbols().equals(
108: def.getDateFormatSymbols())) {
109: errln("ERROR: set DateFormatSymbols() failed");
110: }
111:
112: /*
113: DateFormatSymbols has not the method getTwoDigitStartDate();
114: //Date startDate = null; //The variable is never used
115: try {
116: // startDate = pat.getTwoDigitStartDate();
117: }
118: catch (Exception e) {
119: errln("ERROR: getTwoDigitStartDate() failed");
120: }
121:
122: try {
123: // pat_fr.setTwoDigitStartDate(startDate);
124: }
125: catch (Exception e) {
126: errln("ERROR: setTwoDigitStartDate() failed");
127: }*/
128:
129: // ======= Test applyPattern()
130: logln("Testing applyPattern()");
131:
132: String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
133: logln("Applying pattern " + p1);
134: pat.applyPattern(p1);
135:
136: String s2 = pat.toPattern();
137: logln("Extracted pattern is " + s2);
138: if (!s2.equals(p1)) {
139: errln("ERROR: toPattern() result did not match pattern applied");
140: }
141:
142: logln("Applying pattern " + p1);
143: pat.applyLocalizedPattern(p1);
144: String s3 = pat.toLocalizedPattern();
145: logln("Extracted pattern is " + s3);
146: if (!s3.equals(p1)) {
147: errln("ERROR: toLocalizedPattern() result did not match pattern applied");
148: }
149:
150: // ======= Test getStaticClassID()
151:
152: // logln("Testing instanceof");
153:
154: // try {
155: // DateFormat test = new SimpleDateFormat();
156:
157: // if (! (test instanceof SimpleDateFormat)) {
158: // errln("ERROR: instanceof failed");
159: // }
160: // }
161: // catch (Exception e) {
162: // errln("ERROR: Couldn't create a SimpleDateFormat");
163: // }
164: }
165:
166: // Jitterbug 4451, for coverage
167: public void TestCoverage() {
168: class StubDateFormat extends SimpleDateFormat {
169: public void run() {
170: if (!zeroPaddingNumber(12, 4, 6).equals("0012")) {
171: errln("SimpleDateFormat(zeroPaddingNumber(long , int , int )");
172: }
173: }
174: }
175: new StubDateFormat().run();
176: }
177: }
|