001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.format;
017:
018: import java.util.Locale;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import org.joda.time.Chronology;
024: import org.joda.time.DateTime;
025: import org.joda.time.DateTimeZone;
026: import org.joda.time.MutableDateTime;
027: import org.joda.time.chrono.ISOChronology;
028:
029: /**
030: * Makes sure that text fields are correct for English.
031: *
032: * @author Brian S O'Neill
033: */
034: public class TestTextFields extends TestCase {
035:
036: private static final DateTimeZone[] ZONES = { DateTimeZone.UTC,
037: DateTimeZone.forID("Europe/Paris"),
038: DateTimeZone.forID("Europe/London"),
039: DateTimeZone.forID("Asia/Tokyo"),
040: DateTimeZone.forID("America/Los_Angeles"), };
041:
042: private static final String[] MONTHS = { null, "January",
043: "February", "March", "April", "May", "June", "July",
044: "August", "September", "October", "November", "December" };
045:
046: private static final String[] WEEKDAYS = { null, "Monday",
047: "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
048: "Sunday" };
049:
050: private static final String[] HALFDAYS = { "AM", "PM" };
051:
052: private DateTimeZone originalDateTimeZone = null;
053: private Locale originalLocale = null;
054:
055: public static void main(String[] args) {
056: junit.textui.TestRunner.run(suite());
057: }
058:
059: public static TestSuite suite() {
060: return new TestSuite(TestTextFields.class);
061: }
062:
063: public TestTextFields(String name) {
064: super (name);
065: }
066:
067: protected void setUp() throws Exception {
068: originalDateTimeZone = DateTimeZone.getDefault();
069: originalLocale = Locale.getDefault();
070: DateTimeZone.setDefault(ZONES[0]);
071: Locale.setDefault(Locale.ENGLISH);
072: }
073:
074: protected void tearDown() throws Exception {
075: DateTimeZone.setDefault(originalDateTimeZone);
076: Locale.setDefault(originalLocale);
077: originalDateTimeZone = null;
078: originalLocale = null;
079: }
080:
081: //-----------------------------------------------------------------------
082: public void testMonthNames_monthStart() {
083: DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM");
084: for (int i = 0; i < ZONES.length; i++) {
085: for (int month = 1; month <= 12; month++) {
086: DateTime dt = new DateTime(2004, month, 1, 1, 20, 30,
087: 40, ZONES[i]);
088: String monthText = printer.print(dt);
089: assertEquals(MONTHS[month], monthText);
090: }
091: }
092: }
093:
094: public void testMonthNames_monthMiddle() {
095: DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM");
096: for (int i = 0; i < ZONES.length; i++) {
097: for (int month = 1; month <= 12; month++) {
098: DateTime dt = new DateTime(2004, month, 15, 12, 20, 30,
099: 40, ZONES[i]);
100: String monthText = printer.print(dt);
101: assertEquals(MONTHS[month], monthText);
102: }
103: }
104: }
105:
106: public void testMonthNames_monthEnd() {
107: DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM");
108: for (int i = 0; i < ZONES.length; i++) {
109: Chronology chrono = ISOChronology.getInstance(ZONES[i]);
110: for (int month = 1; month <= 12; month++) {
111: DateTime dt = new DateTime(2004, month, 1, 23, 20, 30,
112: 40, chrono);
113: int lastDay = chrono.dayOfMonth().getMaximumValue(
114: dt.getMillis());
115: dt = new DateTime(2004, month, lastDay, 23, 20, 30, 40,
116: chrono);
117: String monthText = printer.print(dt);
118: assertEquals(MONTHS[month], monthText);
119: }
120: }
121: }
122:
123: public void testWeekdayNames() {
124: DateTimeFormatter printer = DateTimeFormat.forPattern("EEEE");
125: for (int i = 0; i < ZONES.length; i++) {
126: MutableDateTime mdt = new MutableDateTime(2004, 1, 1, 1,
127: 20, 30, 40, ZONES[i]);
128: for (int day = 1; day <= 366; day++) {
129: mdt.setDayOfYear(day);
130: int weekday = mdt.getDayOfWeek();
131: String weekdayText = printer.print(mdt);
132: assertEquals(WEEKDAYS[weekday], weekdayText);
133: }
134: }
135: }
136:
137: public void testHalfdayNames() {
138: DateTimeFormatter printer = DateTimeFormat.forPattern("a");
139: for (int i = 0; i < ZONES.length; i++) {
140: Chronology chrono = ISOChronology.getInstance(ZONES[i]);
141: MutableDateTime mdt = new MutableDateTime(2004, 5, 30, 0,
142: 20, 30, 40, chrono);
143: for (int hour = 0; hour < 24; hour++) {
144: mdt.setHourOfDay(hour);
145: int halfday = mdt.get(chrono.halfdayOfDay());
146: String halfdayText = printer.print(mdt);
147: assertEquals(HALFDAYS[halfday], halfdayText);
148: }
149: }
150: }
151: }
|