001 /* 002 * Copyright 2001-2009 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 YearMonth. 032 * 033 * @author Stephen Colebourne 034 */ 035 public class TestYearMonth_Constructors extends TestCase { 036 037 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 038 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 039 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 040 private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC(); 041 private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS); 042 043 private long TEST_TIME_NOW = 044 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 045 046 private long TEST_TIME1 = 047 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 048 + 12L * DateTimeConstants.MILLIS_PER_HOUR 049 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 050 051 private long TEST_TIME2 = 052 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 053 + 14L * DateTimeConstants.MILLIS_PER_HOUR 054 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 055 056 private DateTimeZone zone = null; 057 058 public static void main(String[] args) { 059 junit.textui.TestRunner.run(suite()); 060 } 061 062 public static TestSuite suite() { 063 return new TestSuite(TestYearMonth_Constructors.class); 064 } 065 066 public TestYearMonth_Constructors(String name) { 067 super(name); 068 } 069 070 protected void setUp() throws Exception { 071 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 072 zone = DateTimeZone.getDefault(); 073 DateTimeZone.setDefault(LONDON); 074 } 075 076 protected void tearDown() throws Exception { 077 DateTimeUtils.setCurrentMillisSystem(); 078 DateTimeZone.setDefault(zone); 079 zone = null; 080 } 081 082 //----------------------------------------------------------------------- 083 public void testParse_noFormatter() throws Throwable { 084 assertEquals(new YearMonth(2010, 6), YearMonth.parse("2010-06-30")); 085 assertEquals(new YearMonth(2010, 1), YearMonth.parse("2010-002")); 086 } 087 088 public void testParse_formatter() throws Throwable { 089 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--MM").withChronology(ISOChronology.getInstance(PARIS)); 090 assertEquals(new YearMonth(2010, 6), YearMonth.parse("2010--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 YearMonth expected = new YearMonth(1970, 2); 098 assertEquals(expected, YearMonth.fromCalendarFields(cal)); 099 try { 100 YearMonth.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 YearMonth expected = new YearMonth(1970, 2); 110 assertEquals(expected, YearMonth.fromDateFields(cal.getTime())); 111 try { 112 YearMonth.fromDateFields(null); 113 fail(); 114 } catch (IllegalArgumentException ex) {} 115 } 116 117 //----------------------------------------------------------------------- 118 /** 119 * Test constructor () 120 */ 121 public void testConstructor() throws Throwable { 122 YearMonth test = new YearMonth(); 123 assertEquals(ISO_UTC, test.getChronology()); 124 assertEquals(1970, test.getYear()); 125 assertEquals(6, test.getMonthOfYear()); 126 assertEquals(test, YearMonth.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 YearMonth test = new YearMonth(LONDON); 138 assertEquals(ISO_UTC, test.getChronology()); 139 assertEquals(2005, test.getYear()); 140 assertEquals(6, test.getMonthOfYear()); 141 assertEquals(test, YearMonth.now(LONDON)); 142 143 test = new YearMonth(PARIS); 144 assertEquals(ISO_UTC, test.getChronology()); 145 assertEquals(2005, test.getYear()); 146 assertEquals(7, test.getMonthOfYear()); 147 assertEquals(test, YearMonth.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 YearMonth test = new YearMonth((DateTimeZone) null); 159 assertEquals(ISO_UTC, test.getChronology()); 160 assertEquals(2005, test.getYear()); 161 assertEquals(6, test.getMonthOfYear()); 162 } 163 164 /** 165 * Test constructor (Chronology) 166 */ 167 public void testConstructor_Chronology() throws Throwable { 168 YearMonth test = new YearMonth(GREGORIAN_PARIS); 169 assertEquals(GREGORIAN_UTC, test.getChronology()); 170 assertEquals(1970, test.getYear()); 171 assertEquals(6, test.getMonthOfYear()); 172 assertEquals(test, YearMonth.now(GREGORIAN_PARIS)); 173 } 174 175 /** 176 * Test constructor (Chronology=null) 177 */ 178 public void testConstructor_nullChronology() throws Throwable { 179 YearMonth test = new YearMonth((Chronology) null); 180 assertEquals(ISO_UTC, test.getChronology()); 181 assertEquals(1970, test.getYear()); 182 assertEquals(6, test.getMonthOfYear()); 183 } 184 185 //----------------------------------------------------------------------- 186 /** 187 * Test constructor (long) 188 */ 189 public void testConstructor_long1() throws Throwable { 190 YearMonth test = new YearMonth(TEST_TIME1); 191 assertEquals(ISO_UTC, test.getChronology()); 192 assertEquals(1970, test.getYear()); 193 assertEquals(4, test.getMonthOfYear()); 194 } 195 196 /** 197 * Test constructor (long) 198 */ 199 public void testConstructor_long2() throws Throwable { 200 YearMonth test = new YearMonth(TEST_TIME2); 201 assertEquals(ISO_UTC, test.getChronology()); 202 assertEquals(1971, test.getYear()); 203 assertEquals(5, test.getMonthOfYear()); 204 } 205 206 /** 207 * Test constructor (long, Chronology) 208 */ 209 public void testConstructor_long1_Chronology() throws Throwable { 210 YearMonth test = new YearMonth(TEST_TIME1, GREGORIAN_PARIS); 211 assertEquals(GREGORIAN_UTC, test.getChronology()); 212 assertEquals(1970, test.getYear()); 213 assertEquals(4, test.getMonthOfYear()); 214 } 215 216 /** 217 * Test constructor (long, Chronology) 218 */ 219 public void testConstructor_long2_Chronology() throws Throwable { 220 YearMonth test = new YearMonth(TEST_TIME2, GREGORIAN_PARIS); 221 assertEquals(GREGORIAN_UTC, test.getChronology()); 222 assertEquals(1971, test.getYear()); 223 assertEquals(5, test.getMonthOfYear()); 224 } 225 226 /** 227 * Test constructor (long, Chronology=null) 228 */ 229 public void testConstructor_long_nullChronology() throws Throwable { 230 YearMonth test = new YearMonth(TEST_TIME1, null); 231 assertEquals(ISO_UTC, test.getChronology()); 232 assertEquals(1970, test.getYear()); 233 assertEquals(4, test.getMonthOfYear()); 234 } 235 236 //----------------------------------------------------------------------- 237 public void testConstructor_Object() throws Throwable { 238 Date date = new Date(TEST_TIME1); 239 YearMonth test = new YearMonth(date); 240 assertEquals(ISO_UTC, test.getChronology()); 241 assertEquals(1970, test.getYear()); 242 assertEquals(4, test.getMonthOfYear()); 243 } 244 245 public void testConstructor_nullObject() throws Throwable { 246 YearMonth test = new YearMonth((Object) null); 247 assertEquals(ISO_UTC, test.getChronology()); 248 assertEquals(1970, test.getYear()); 249 assertEquals(6, test.getMonthOfYear()); 250 } 251 252 public void testConstructor_ObjectString1() throws Throwable { 253 YearMonth test = new YearMonth("1972-12"); 254 assertEquals(ISO_UTC, test.getChronology()); 255 assertEquals(1972, test.getYear()); 256 assertEquals(12, test.getMonthOfYear()); 257 } 258 259 public void testConstructor_ObjectString5() throws Throwable { 260 YearMonth test = new YearMonth("10"); 261 assertEquals(ISO_UTC, test.getChronology()); 262 assertEquals(10, test.getYear()); 263 assertEquals(1, test.getMonthOfYear()); 264 } 265 266 public void testConstructor_ObjectStringEx1() throws Throwable { 267 try { 268 new YearMonth("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 YearMonth("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 YearMonth("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 YearMonth("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 YearMonth test = new YearMonth(date, GREGORIAN_PARIS); 309 assertEquals(GREGORIAN_UTC, test.getChronology()); 310 assertEquals(1970, test.getYear()); 311 assertEquals(4, test.getMonthOfYear()); 312 } 313 314 /** 315 * Test constructor (Object=null, Chronology) 316 */ 317 public void testConstructor_nullObject_Chronology() throws Throwable { 318 YearMonth test = new YearMonth((Object) null, GREGORIAN_PARIS); 319 assertEquals(GREGORIAN_UTC, test.getChronology()); 320 assertEquals(1970, test.getYear()); 321 assertEquals(6, test.getMonthOfYear()); 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 YearMonth test = new YearMonth(date, null); 330 assertEquals(ISO_UTC, test.getChronology()); 331 assertEquals(1970, test.getYear()); 332 assertEquals(4, test.getMonthOfYear()); 333 } 334 335 /** 336 * Test constructor (Object=null, Chronology=null) 337 */ 338 public void testConstructor_nullObject_nullChronology() throws Throwable { 339 YearMonth test = new YearMonth((Object) null, null); 340 assertEquals(ISO_UTC, test.getChronology()); 341 assertEquals(1970, test.getYear()); 342 assertEquals(6, test.getMonthOfYear()); 343 } 344 345 //----------------------------------------------------------------------- 346 /** 347 * Test constructor (int, int) 348 */ 349 public void testConstructor_int_int() throws Throwable { 350 YearMonth test = new YearMonth(1970, 6); 351 assertEquals(ISO_UTC, test.getChronology()); 352 assertEquals(1970, test.getYear()); 353 assertEquals(6, test.getMonthOfYear()); 354 try { 355 new YearMonth(Integer.MIN_VALUE, 6); 356 fail(); 357 } catch (IllegalArgumentException ex) {} 358 try { 359 new YearMonth(Integer.MAX_VALUE, 6); 360 fail(); 361 } catch (IllegalArgumentException ex) {} 362 try { 363 new YearMonth(1970, 0); 364 fail(); 365 } catch (IllegalArgumentException ex) {} 366 try { 367 new YearMonth(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 YearMonth test = new YearMonth(1970, 6, GREGORIAN_PARIS); 377 assertEquals(GREGORIAN_UTC, test.getChronology()); 378 assertEquals(1970, test.getYear()); 379 assertEquals(6, test.getMonthOfYear()); 380 try { 381 new YearMonth(Integer.MIN_VALUE, 6, GREGORIAN_PARIS); 382 fail(); 383 } catch (IllegalArgumentException ex) {} 384 try { 385 new YearMonth(Integer.MAX_VALUE, 6, GREGORIAN_PARIS); 386 fail(); 387 } catch (IllegalArgumentException ex) {} 388 try { 389 new YearMonth(1970, 0, GREGORIAN_PARIS); 390 fail(); 391 } catch (IllegalArgumentException ex) {} 392 try { 393 new YearMonth(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 YearMonth test = new YearMonth(1970, 6, null); 403 assertEquals(ISO_UTC, test.getChronology()); 404 assertEquals(1970, test.getYear()); 405 assertEquals(6, test.getMonthOfYear()); 406 } 407 408 }