001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.format;
032:
033: import java.text.*;
034: import java.util.*;
035: import net.sf.retrotranslator.tests.TestCaseBase;
036:
037: /**
038: * @author Taras Puchko
039: */
040: public class DateTimeConversionTestCase extends TestCaseBase {
041:
042: public void testFormat_Flags() throws Exception {
043: assertFormat(" null null", "%7tH %1tT", null, null);
044: assertDate(" 05", "%4tC", "0567-01-04");
045: assertTime("00:07:08 ", "%-10tT", "00:07:08.093");
046: assertTime(" 00:07:08", "%10tT", "00:07:08.093");
047: assertFormatException(MissingFormatWidthException.class,
048: "%-tT", "x");
049: assertFormatException(MissingFormatWidthException.class,
050: "%-tT", new Date());
051: assertFormatException(
052: FormatFlagsConversionMismatchException.class, "%#tT",
053: new Date());
054: assertFormatException(IllegalFormatPrecisionException.class,
055: "%10.2tT", new Date());
056: assertFormatException(IllegalFormatPrecisionException.class,
057: "%#-10.1tT", "x");
058: }
059:
060: public void testFormat_Time() throws Exception {
061: assertTime("00", "%tH", "00:07:08.093");
062: assertTime("14", "%tH", "14:15:45.178");
063:
064: assertTime("12", "%tI", "00:07:08.093");
065: assertTime("02", "%tI", "14:15:45.178");
066:
067: assertTime("0", "%tk", "00:07:08.093");
068: assertTime("14", "%tk", "14:15:45.178");
069:
070: assertTime("12", "%tl", "00:07:08.093");
071: assertTime("2", "%tl", "14:15:45.178");
072:
073: assertTime("07", "%tM", "00:07:08.093");
074: assertTime("15", "%tM", "14:15:45.178");
075:
076: assertTime("08", "%tS", "00:07:08.093");
077: assertTime("45", "%tS", "14:15:45.178");
078:
079: assertTime("093", "%tL", "00:07:08.093");
080: assertTime("178", "%tL", "14:15:45.178");
081:
082: assertTime("093000000", "%tN", "00:07:08.093");
083: assertTime("178000000", "%tN", "14:15:45.178");
084:
085: assertTime("am", "%tp", "00:07:08.093");
086: assertTime("pm", "%tp", "14:15:45.178");
087:
088: assertTime("AM", "%Tp", "00:07:08.093");
089: assertTime("PM", "%Tp", "14:15:45.178");
090:
091: assertDateTime("1234567", "%ts", new Date(1234567890));
092: assertDateTime("1234567890", "%tQ", new Date(1234567890));
093:
094: assertTime("00:07", "%tR", "00:07:08.093");
095: assertTime("14:15", "%tR", "14:15:45.178");
096:
097: assertTime("00:07:08", "%tT", "00:07:08.093");
098: assertTime("14:15:45", "%tT", "14:15:45.178");
099:
100: assertTime("12:07:08 AM", "%tr", "00:07:08.093");
101: assertTime("02:15:45 PM", "%tr", "14:15:45.178");
102: }
103:
104: public void testFormat_Date() throws Exception {
105: assertPatternDate("MMMM", "%tB", "2006-01-03", false);
106: assertPatternDate("MMMM", "%tB", "2006-07-07", false);
107:
108: assertDate("JANVIER", "%TB", "2006-01-03");
109: assertDate("JUILLET", "%TB", "2006-07-07");
110:
111: assertPatternDate("MMM", "%tb", "2006-01-05", false);
112: assertPatternDate("MMM", "%tb", "2006-07-09", false);
113:
114: assertPatternDate("MMM", "%Tb", "2006-01-05", true);
115: assertPatternDate("MMM", "%Tb", "2006-07-09", true);
116:
117: assertPatternDate("MMM", "%th", "2006-04-05", false);
118: assertPatternDate("MMM", "%th", "2006-09-09", false);
119:
120: assertPatternDate("MMM", "%Th", "2006-04-05", true);
121: assertPatternDate("MMM", "%Th", "2006-09-09", true);
122:
123: assertPatternDate("EEEE", "%tA", "2006-01-03", false);
124: assertPatternDate("EEEE", "%tA", "2006-07-07", false);
125:
126: assertDate("MARDI", "%TA", "2006-01-03");
127: assertDate("VENDREDI", "%TA", "2006-07-07");
128:
129: assertPatternDate("EEE", "%ta", "2006-01-04", false);
130: assertPatternDate("EEE", "%ta", "2006-07-08", false);
131:
132: assertPatternDate("EEE", "%Ta", "2006-01-04", true);
133: assertPatternDate("EEE", "%Ta", "2006-07-08", true);
134:
135: assertDate("20", "%tC", "2006-01-04");
136: assertDate("01", "%tC", "0123-01-04");
137: assertDate("123", "%tC", "12345-07-08");
138:
139: assertDate("2006", "%tY", "2006-01-04");
140: assertDate("0123", "%tY", "0123-01-04");
141: assertDate("12345", "%tY", "12345-07-08");
142:
143: assertDate("06", "%ty", "2006-01-04");
144: assertDate("03", "%ty", "0003-01-04");
145: assertDate("45", "%ty", "12345-07-08");
146:
147: assertDate("004", "%tj", "2006-01-04");
148: assertDate("076", "%tj", "0003-03-17");
149: assertDate("366", "%tj", "2000-12-31");
150:
151: assertDate("01", "%tm", "2006-01-04");
152: assertDate("03", "%tm", "0003-03-17");
153: assertDate("12", "%tm", "2000-12-31");
154:
155: assertDate("04", "%td", "2006-01-04");
156: assertDate("17", "%td", "0003-03-17");
157: assertDate("31", "%td", "2000-12-31");
158:
159: assertDate("4", "%te", "2006-01-04");
160: assertDate("17", "%te", "0003-03-17");
161: assertDate("31", "%te", "2000-12-31");
162:
163: assertDate("11/12/04", "%tD", "0004-11-12");
164: assertDate("01/03/06", "%tD", "2006-01-03");
165: assertDate("07/08/05", "%tD", "12005-07-08");
166:
167: assertDate("0004-11-12", "%tF", "0004-11-12");
168: assertDate("2006-01-03", "%tF", "2006-01-03");
169: assertDate("12005-07-08", "%tF", "12005-07-08");
170: }
171:
172: public void testFormat_TimeZone() throws Exception {
173: TimeZone defaultTimeZone = TimeZone.getDefault();
174: try {
175: if (isJava14AtLeast()) {
176: assertTimeZone("Asia/Katmandu", "+0545", "NPT", "NPT");
177: assertTimeZone("Europe/Helsinki", "+0200", "EET",
178: "EEST");
179: assertTimeZone("Africa/Casablanca", "+0000", "WET",
180: "WET");
181: }
182: assertTimeZone("GMT+02:00", "+0200", "GMT+02:00",
183: "GMT+02:00");
184: assertTimeZone("America/Los_Angeles", "-0800", "PST", "PDT");
185: assertTimeZone("America/New_York", "-0500", "EST", "EDT");
186: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
187: SimpleDateFormat format = new SimpleDateFormat(
188: "yyyy-MM-dd HH:mm:ss");
189: format
190: .setTimeZone(TimeZone
191: .getTimeZone("America/New_York"));
192: Calendar calendar = new GregorianCalendar(TimeZone
193: .getTimeZone("America/Los_Angeles"));
194: calendar.setTime(format.parse("2006-05-08 02:25:45"));
195: assertFormat("2006-05-07 23:25:45", "%tF %tT", calendar,
196: calendar);
197: } finally {
198: TimeZone.setDefault(defaultTimeZone);
199: }
200: }
201:
202: private void assertTime(String expected, String format, String time)
203: throws Exception {
204: assertDateTime(expected, format, parse("2006-01-03 " + time));
205: }
206:
207: private void assertDate(String expected, String format, String date)
208: throws Exception {
209: assertDateTime(expected, format, parseDate(date));
210: }
211:
212: private void assertPatternDate(String pattern, String format,
213: String date, boolean upperCase) throws Exception {
214: Date parsedDate = parseDate(date);
215: String expected = format(parsedDate, pattern);
216: if (upperCase) {
217: expected = expected.toUpperCase();
218: }
219: assertDateTime(expected, format, parsedDate);
220: }
221:
222: private void assertTimeZone(String id, String numeric,
223: String standard, String daylight) throws Exception {
224: TimeZone.setDefault(TimeZone.getTimeZone(id));
225: Date winter = parse("2006-01-03 00:07:08.093");
226: Date summer = parse("2006-04-07 14:15:45.178");
227: assertDateTime(numeric, "%tz", winter);
228: assertDateTime(numeric, "%tz", summer);
229: assertDateTime(standard, "%tZ", winter);
230: assertDateTime(daylight, "%tZ", summer);
231: assertDateTime(format(winter, "EEE MMM") + " 03 00:07:08 "
232: + standard + " 2006", "%tc", winter);
233: assertDateTime(format(summer, "EEE MMM") + " 07 14:15:45 "
234: + daylight + " 2006", "%tc", summer);
235: }
236:
237: private void assertDateTime(String expected, String format,
238: Date date) {
239: Calendar calendar = new GregorianCalendar();
240: calendar.setTime(date);
241: for (Object argument : new Object[] { date.getTime(), date,
242: calendar }) {
243: assertFormat(expected, format, argument);
244: }
245: }
246:
247: private static Date parseDate(String date) throws ParseException {
248: return parse(date + " 00:07:08.093");
249: }
250:
251: private static Date parse(String dateTime) throws ParseException {
252: return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
253: .parse(dateTime);
254: }
255:
256: private static String format(Date date, String pattern) {
257: return new SimpleDateFormat(pattern, Locale.FRANCE)
258: .format(date);
259: }
260:
261: }
|