View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.format;
17  
18  import java.util.Locale;
19  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.joda.time.Chronology;
24  import org.joda.time.DateTime;
25  import org.joda.time.DateTimeZone;
26  import org.joda.time.MutableDateTime;
27  import org.joda.time.chrono.ISOChronology;
28  
29  /**
30   * Makes sure that text fields are correct for English.
31   *
32   * @author Brian S O'Neill
33   */
34  public class TestTextFields extends TestCase {
35  
36      private static final DateTimeZone[] ZONES = {
37          DateTimeZone.UTC,
38          DateTimeZone.forID("Europe/Paris"),
39          DateTimeZone.forID("Europe/London"),
40          DateTimeZone.forID("Asia/Tokyo"),
41          DateTimeZone.forID("America/Los_Angeles"),
42      };
43  
44      private static final String[] MONTHS = {
45          null,
46          "January", "February", "March", "April", "May", "June",
47          "July", "August", "September", "October", "November", "December"
48      };
49  
50      private static final String[] WEEKDAYS = {
51          null,
52          "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
53      };
54  
55      private static final String[] HALFDAYS = {
56          "AM", "PM"
57      };
58  
59      private DateTimeZone originalDateTimeZone = null;
60      private Locale originalLocale = null;
61  
62      public static void main(String[] args) {
63          junit.textui.TestRunner.run(suite());
64      }
65  
66      public static TestSuite suite() {
67          return new TestSuite(TestTextFields.class);
68      }
69  
70      public TestTextFields(String name) {
71          super(name);
72      }
73  
74      protected void setUp() throws Exception {
75          originalDateTimeZone = DateTimeZone.getDefault();
76          originalLocale = Locale.getDefault();
77          DateTimeZone.setDefault(ZONES[0]);
78          Locale.setDefault(Locale.ENGLISH);
79      }
80  
81      protected void tearDown() throws Exception {
82          DateTimeZone.setDefault(originalDateTimeZone);
83          Locale.setDefault(originalLocale);
84          originalDateTimeZone = null;
85          originalLocale = null;
86      }
87  
88      //-----------------------------------------------------------------------
89      public void testMonthNames_monthStart() {
90          DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM");
91          for (int i=0; i<ZONES.length; i++) {
92              for (int month=1; month<=12; month++) {
93                  DateTime dt = new DateTime(2004, month, 1, 1, 20, 30, 40, ZONES[i]);
94                  String monthText = printer.print(dt);
95                  assertEquals(MONTHS[month], monthText);
96              }
97          }
98      }
99  
100     public void testMonthNames_monthMiddle() {
101         DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM");
102         for (int i=0; i<ZONES.length; i++) {
103             for (int month=1; month<=12; month++) {
104                 DateTime dt = new DateTime(2004, month, 15, 12, 20, 30, 40, ZONES[i]);
105                 String monthText = printer.print(dt);
106                 assertEquals(MONTHS[month], monthText);
107             }
108         }
109     }
110 
111     public void testMonthNames_monthEnd() {
112         DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM");
113         for (int i=0; i<ZONES.length; i++) {
114             Chronology chrono = ISOChronology.getInstance(ZONES[i]);
115             for (int month=1; month<=12; month++) {
116                 DateTime dt = new DateTime(2004, month, 1, 23, 20, 30, 40, chrono);
117                 int lastDay = chrono.dayOfMonth().getMaximumValue(dt.getMillis());
118                 dt = new DateTime(2004, month, lastDay, 23, 20, 30, 40, chrono);
119                 String monthText = printer.print(dt);
120                 assertEquals(MONTHS[month], monthText);
121             }
122         }
123     }
124 
125     public void testWeekdayNames() {
126         DateTimeFormatter printer = DateTimeFormat.forPattern("EEEE");
127         for (int i=0; i<ZONES.length; i++) {
128             MutableDateTime mdt = new MutableDateTime(2004, 1, 1, 1, 20, 30, 40, ZONES[i]);
129             for (int day=1; day<=366; day++) {
130                 mdt.setDayOfYear(day);
131                 int weekday = mdt.getDayOfWeek();
132                 String weekdayText = printer.print(mdt);
133                 assertEquals(WEEKDAYS[weekday], weekdayText);
134             }
135         }
136     }
137 
138     public void testHalfdayNames() {
139         DateTimeFormatter printer = DateTimeFormat.forPattern("a");
140         for (int i=0; i<ZONES.length; i++) {
141             Chronology chrono = ISOChronology.getInstance(ZONES[i]);
142             MutableDateTime mdt = new MutableDateTime(2004, 5, 30, 0, 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 }