001 /* 002 * Copyright 2001-2010 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; 017 018 import java.util.Calendar; 019 import java.util.Date; 020 import java.util.GregorianCalendar; 021 022 import junit.framework.TestCase; 023 import junit.framework.TestSuite; 024 025 import org.joda.time.chrono.GregorianChronology; 026 import org.joda.time.chrono.ISOChronology; 027 import org.joda.time.format.DateTimeFormat; 028 import org.joda.time.format.DateTimeFormatter; 029 030 /** 031 * This class is a Junit unit test for MonthDay. Based on {@link TestYearMonth_Constuctors} 032 */ 033 public class TestMonthDay_Constructors extends TestCase { 034 035 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 036 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 037 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 038 private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC(); 039 private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS); 040 041 private long TEST_TIME_NOW = 042 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 043 044 private long TEST_TIME1 = 045 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 046 + 12L * DateTimeConstants.MILLIS_PER_HOUR 047 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 048 049 private long TEST_TIME2 = 050 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 051 + 14L * DateTimeConstants.MILLIS_PER_HOUR 052 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 053 054 private DateTimeZone zone = null; 055 056 public static void main(String[] args) { 057 junit.textui.TestRunner.run(suite()); 058 } 059 060 public static TestSuite suite() { 061 return new TestSuite(TestMonthDay_Constructors.class); 062 } 063 064 public TestMonthDay_Constructors(String name) { 065 super(name); 066 } 067 068 protected void setUp() throws Exception { 069 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 070 zone = DateTimeZone.getDefault(); 071 DateTimeZone.setDefault(LONDON); 072 } 073 074 protected void tearDown() throws Exception { 075 DateTimeUtils.setCurrentMillisSystem(); 076 DateTimeZone.setDefault(zone); 077 zone = null; 078 } 079 080 //----------------------------------------------------------------------- 081 public void testParse_noFormatter() throws Throwable { 082 assertEquals(new MonthDay(6, 30), MonthDay.parse("--06-30")); 083 assertEquals(new MonthDay(2, 29), MonthDay.parse("--02-29")); 084 assertEquals(new MonthDay(6, 30), MonthDay.parse("2010-06-30")); 085 assertEquals(new MonthDay(1, 2), MonthDay.parse("2010-002")); 086 } 087 088 public void testParse_formatter() throws Throwable { 089 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--dd MM").withChronology(ISOChronology.getInstance(PARIS)); 090 assertEquals(new MonthDay(6, 30), MonthDay.parse("2010--30 06", f)); 091 } 092 093 //----------------------------------------------------------------------- 094 public void testFactory_FromCalendarFields() throws Exception { 095 GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6); 096 cal.set(Calendar.MILLISECOND, 7); 097 MonthDay expected = new MonthDay(2, 3); 098 assertEquals(expected, MonthDay.fromCalendarFields(cal)); 099 try { 100 MonthDay.fromCalendarFields(null); 101 fail(); 102 } catch (IllegalArgumentException ex) {} 103 } 104 105 //----------------------------------------------------------------------- 106 public void testFactory_FromDateFields() throws Exception { 107 GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6); 108 cal.set(Calendar.MILLISECOND, 7); 109 MonthDay expected = new MonthDay(2, 3); 110 assertEquals(expected, MonthDay.fromDateFields(cal.getTime())); 111 try { 112 MonthDay.fromDateFields(null); 113 fail(); 114 } catch (IllegalArgumentException ex) {} 115 } 116 117 //----------------------------------------------------------------------- 118 /** 119 * Test constructor () 120 */ 121 public void testConstructor() throws Throwable { 122 MonthDay test = new MonthDay(); 123 assertEquals(ISO_UTC, test.getChronology()); 124 assertEquals(6, test.getMonthOfYear()); 125 assertEquals(9, test.getDayOfMonth()); 126 assertEquals(test, MonthDay.now()); 127 } 128 129 /** 130 * Test constructor (DateTimeZone) 131 */ 132 public void testConstructor_DateTimeZone() throws Throwable { 133 DateTime dt = new DateTime(2005, 6, 30, 23, 59, 0, 0, LONDON); 134 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 135 // 23:59 in London is 00:59 the following day in Paris 136 137 MonthDay test = new MonthDay(LONDON); 138 assertEquals(ISO_UTC, test.getChronology()); 139 assertEquals(6, test.getMonthOfYear()); 140 assertEquals(30, test.getDayOfMonth()); 141 assertEquals(test, MonthDay.now(LONDON)); 142 143 test = new MonthDay(PARIS); 144 assertEquals(ISO_UTC, test.getChronology()); 145 assertEquals(7, test.getMonthOfYear()); 146 assertEquals(1, test.getDayOfMonth()); 147 assertEquals(test, MonthDay.now(PARIS)); 148 } 149 150 /** 151 * Test constructor (DateTimeZone=null) 152 */ 153 public void testConstructor_nullDateTimeZone() throws Throwable { 154 DateTime dt = new DateTime(2005, 6, 30, 23, 59, 0, 0, LONDON); 155 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 156 // 23:59 in London is 00:59 the following day in Paris 157 158 MonthDay test = new MonthDay((DateTimeZone) null); 159 assertEquals(ISO_UTC, test.getChronology()); 160 assertEquals(6, test.getMonthOfYear()); 161 assertEquals(30, test.getDayOfMonth()); 162 } 163 164 /** 165 * Test constructor (Chronology) 166 */ 167 public void testConstructor_Chronology() throws Throwable { 168 MonthDay test = new MonthDay(GREGORIAN_PARIS); 169 assertEquals(GREGORIAN_UTC, test.getChronology()); 170 assertEquals(6, test.getMonthOfYear()); 171 assertEquals(9, test.getDayOfMonth()); 172 assertEquals(test, MonthDay.now(GREGORIAN_PARIS)); 173 } 174 175 /** 176 * Test constructor (Chronology=null) 177 */ 178 public void testConstructor_nullChronology() throws Throwable { 179 MonthDay test = new MonthDay((Chronology) null); 180 assertEquals(ISO_UTC, test.getChronology()); 181 assertEquals(6, test.getMonthOfYear()); 182 assertEquals(9, test.getDayOfMonth()); 183 } 184 185 //----------------------------------------------------------------------- 186 /** 187 * Test constructor (long) 188 */ 189 public void testConstructor_long1() throws Throwable { 190 MonthDay test = new MonthDay(TEST_TIME1); 191 assertEquals(ISO_UTC, test.getChronology()); 192 assertEquals(4, test.getMonthOfYear()); 193 assertEquals(6, test.getDayOfMonth()); 194 } 195 196 /** 197 * Test constructor (long) 198 */ 199 public void testConstructor_long2() throws Throwable { 200 MonthDay test = new MonthDay(TEST_TIME2); 201 assertEquals(ISO_UTC, test.getChronology()); 202 assertEquals(5, test.getMonthOfYear()); 203 assertEquals(7, test.getDayOfMonth()); 204 } 205 206 /** 207 * Test constructor (long, Chronology) 208 */ 209 public void testConstructor_long1_Chronology() throws Throwable { 210 MonthDay test = new MonthDay(TEST_TIME1, GREGORIAN_PARIS); 211 assertEquals(GREGORIAN_UTC, test.getChronology()); 212 assertEquals(4, test.getMonthOfYear()); 213 assertEquals(6, test.getDayOfMonth()); 214 } 215 216 /** 217 * Test constructor (long, Chronology) 218 */ 219 public void testConstructor_long2_Chronology() throws Throwable { 220 MonthDay test = new MonthDay(TEST_TIME2, GREGORIAN_PARIS); 221 assertEquals(GREGORIAN_UTC, test.getChronology()); 222 assertEquals(5, test.getMonthOfYear()); 223 assertEquals(7, test.getDayOfMonth()); 224 } 225 226 /** 227 * Test constructor (long, Chronology=null) 228 */ 229 public void testConstructor_long_nullChronology() throws Throwable { 230 MonthDay test = new MonthDay(TEST_TIME1, null); 231 assertEquals(ISO_UTC, test.getChronology()); 232 assertEquals(4, test.getMonthOfYear()); 233 assertEquals(6, test.getDayOfMonth()); 234 } 235 236 //----------------------------------------------------------------------- 237 public void testConstructor_Object() throws Throwable { 238 Date date = new Date(TEST_TIME1); 239 MonthDay test = new MonthDay(date); 240 assertEquals(ISO_UTC, test.getChronology()); 241 assertEquals(4, test.getMonthOfYear()); 242 assertEquals(6, test.getDayOfMonth()); 243 } 244 245 public void testConstructor_nullObject() throws Throwable { 246 MonthDay test = new MonthDay((Object) null); 247 assertEquals(ISO_UTC, test.getChronology()); 248 assertEquals(6, test.getMonthOfYear()); 249 assertEquals(9, test.getDayOfMonth()); 250 } 251 252 public void testConstructor_ObjectString1() throws Throwable { 253 MonthDay test = new MonthDay("1972-12"); 254 assertEquals(ISO_UTC, test.getChronology()); 255 assertEquals(12, test.getMonthOfYear()); 256 assertEquals(1, test.getDayOfMonth()); 257 } 258 259 public void testConstructor_ObjectString5() throws Throwable { 260 MonthDay test = new MonthDay("10"); 261 assertEquals(ISO_UTC, test.getChronology()); 262 assertEquals(1, test.getMonthOfYear()); 263 assertEquals(1, test.getDayOfMonth()); 264 } 265 266 public void testConstructor_ObjectStringEx1() throws Throwable { 267 try { 268 new MonthDay("T10:20:30.040"); 269 fail(); 270 } catch (IllegalArgumentException ex) { 271 // expected 272 } 273 } 274 275 public void testConstructor_ObjectStringEx2() throws Throwable { 276 try { 277 new MonthDay("T10:20:30.040+14:00"); 278 fail(); 279 } catch (IllegalArgumentException ex) { 280 // expected 281 } 282 } 283 284 public void testConstructor_ObjectStringEx3() throws Throwable { 285 try { 286 new MonthDay("10:20:30.040"); 287 fail(); 288 } catch (IllegalArgumentException ex) { 289 // expected 290 } 291 } 292 293 public void testConstructor_ObjectStringEx4() throws Throwable { 294 try { 295 new MonthDay("10:20:30.040+14:00"); 296 fail(); 297 } catch (IllegalArgumentException ex) { 298 // expected 299 } 300 } 301 302 //----------------------------------------------------------------------- 303 /** 304 * Test constructor (Object, Chronology) 305 */ 306 public void testConstructor_Object_Chronology() throws Throwable { 307 Date date = new Date(TEST_TIME1); 308 MonthDay test = new MonthDay(date, GREGORIAN_PARIS); 309 assertEquals(GREGORIAN_UTC, test.getChronology()); 310 assertEquals(4, test.getMonthOfYear()); 311 assertEquals(6, test.getDayOfMonth()); 312 } 313 314 /** 315 * Test constructor (Object=null, Chronology) 316 */ 317 public void testConstructor_nullObject_Chronology() throws Throwable { 318 MonthDay test = new MonthDay((Object) null, GREGORIAN_PARIS); 319 assertEquals(GREGORIAN_UTC, test.getChronology()); 320 assertEquals(6, test.getMonthOfYear()); 321 assertEquals(9, test.getDayOfMonth()); 322 } 323 324 /** 325 * Test constructor (Object, Chronology=null) 326 */ 327 public void testConstructor_Object_nullChronology() throws Throwable { 328 Date date = new Date(TEST_TIME1); 329 MonthDay test = new MonthDay(date, null); 330 assertEquals(ISO_UTC, test.getChronology()); 331 assertEquals(4, test.getMonthOfYear()); 332 assertEquals(6, test.getDayOfMonth()); 333 } 334 335 /** 336 * Test constructor (Object=null, Chronology=null) 337 */ 338 public void testConstructor_nullObject_nullChronology() throws Throwable { 339 MonthDay test = new MonthDay((Object) null, null); 340 assertEquals(ISO_UTC, test.getChronology()); 341 assertEquals(6, test.getMonthOfYear()); 342 assertEquals(9, test.getDayOfMonth()); 343 } 344 345 //----------------------------------------------------------------------- 346 /** 347 * Test constructor (int, int) 348 */ 349 public void testConstructor_int_int() throws Throwable { 350 MonthDay test = new MonthDay(6, 30); 351 assertEquals(ISO_UTC, test.getChronology()); 352 assertEquals(6, test.getMonthOfYear()); 353 assertEquals(30, test.getDayOfMonth()); 354 try { 355 new MonthDay(Integer.MIN_VALUE, 6); 356 fail(); 357 } catch (IllegalArgumentException ex) {} 358 try { 359 new MonthDay(Integer.MAX_VALUE, 6); 360 fail(); 361 } catch (IllegalArgumentException ex) {} 362 try { 363 new MonthDay(1970, 0); 364 fail(); 365 } catch (IllegalArgumentException ex) {} 366 try { 367 new MonthDay(1970, 13); 368 fail(); 369 } catch (IllegalArgumentException ex) {} 370 } 371 372 /** 373 * Test constructor (int, int, Chronology) 374 */ 375 public void testConstructor_int_int_Chronology() throws Throwable { 376 MonthDay test = new MonthDay(6, 30, GREGORIAN_PARIS); 377 assertEquals(GREGORIAN_UTC, test.getChronology()); 378 assertEquals(6, test.getMonthOfYear()); 379 assertEquals(30, test.getDayOfMonth()); 380 try { 381 new MonthDay(Integer.MIN_VALUE, 6, GREGORIAN_PARIS); 382 fail(); 383 } catch (IllegalArgumentException ex) {} 384 try { 385 new MonthDay(Integer.MAX_VALUE, 6, GREGORIAN_PARIS); 386 fail(); 387 } catch (IllegalArgumentException ex) {} 388 try { 389 new MonthDay(1970, 0, GREGORIAN_PARIS); 390 fail(); 391 } catch (IllegalArgumentException ex) {} 392 try { 393 new MonthDay(1970, 13, GREGORIAN_PARIS); 394 fail(); 395 } catch (IllegalArgumentException ex) {} 396 } 397 398 /** 399 * Test constructor (int, int, Chronology=null) 400 */ 401 public void testConstructor_int_int_nullChronology() throws Throwable { 402 MonthDay test = new MonthDay(6, 30, null); 403 assertEquals(ISO_UTC, test.getChronology()); 404 assertEquals(6, test.getMonthOfYear()); 405 assertEquals(30, test.getDayOfMonth()); 406 } 407 408 }