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 = { 037 DateTimeZone.UTC, 038 DateTimeZone.forID("Europe/Paris"), 039 DateTimeZone.forID("Europe/London"), 040 DateTimeZone.forID("Asia/Tokyo"), 041 DateTimeZone.forID("America/Los_Angeles"), 042 }; 043 044 private static final String[] MONTHS = { 045 null, 046 "January", "February", "March", "April", "May", "June", 047 "July", "August", "September", "October", "November", "December" 048 }; 049 050 private static final String[] WEEKDAYS = { 051 null, 052 "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" 053 }; 054 055 private static final String[] HALFDAYS = { 056 "AM", "PM" 057 }; 058 059 private DateTimeZone originalDateTimeZone = null; 060 private Locale originalLocale = null; 061 062 public static void main(String[] args) { 063 junit.textui.TestRunner.run(suite()); 064 } 065 066 public static TestSuite suite() { 067 return new TestSuite(TestTextFields.class); 068 } 069 070 public TestTextFields(String name) { 071 super(name); 072 } 073 074 protected void setUp() throws Exception { 075 originalDateTimeZone = DateTimeZone.getDefault(); 076 originalLocale = Locale.getDefault(); 077 DateTimeZone.setDefault(ZONES[0]); 078 Locale.setDefault(Locale.ENGLISH); 079 } 080 081 protected void tearDown() throws Exception { 082 DateTimeZone.setDefault(originalDateTimeZone); 083 Locale.setDefault(originalLocale); 084 originalDateTimeZone = null; 085 originalLocale = null; 086 } 087 088 //----------------------------------------------------------------------- 089 public void testMonthNames_monthStart() { 090 DateTimeFormatter printer = DateTimeFormat.forPattern("MMMM"); 091 for (int i=0; i<ZONES.length; i++) { 092 for (int month=1; month<=12; month++) { 093 DateTime dt = new DateTime(2004, month, 1, 1, 20, 30, 40, ZONES[i]); 094 String monthText = printer.print(dt); 095 assertEquals(MONTHS[month], monthText); 096 } 097 } 098 } 099 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 }