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;
017
018 import junit.framework.TestCase;
019 import junit.framework.TestSuite;
020
021 import org.joda.time.base.BasePartial;
022
023 /**
024 * This class is a Junit unit test for YearMonthDay.
025 *
026 * @author Stephen Colebourne
027 */
028 public class TestBasePartial extends TestCase {
029
030 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
031
032 private long TEST_TIME_NOW =
033 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
034
035 private long TEST_TIME1 =
036 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
037 + 12L * DateTimeConstants.MILLIS_PER_HOUR
038 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
039
040 private long TEST_TIME2 =
041 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
042 + 14L * DateTimeConstants.MILLIS_PER_HOUR
043 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
044
045 private DateTimeZone zone = null;
046
047 public static void main(String[] args) {
048 junit.textui.TestRunner.run(suite());
049 }
050
051 public static TestSuite suite() {
052 return new TestSuite(TestBasePartial.class);
053 }
054
055 public TestBasePartial(String name) {
056 super(name);
057 }
058
059 protected void setUp() throws Exception {
060 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
061 zone = DateTimeZone.getDefault();
062 DateTimeZone.setDefault(DateTimeZone.UTC);
063 }
064
065 protected void tearDown() throws Exception {
066 DateTimeUtils.setCurrentMillisSystem();
067 DateTimeZone.setDefault(zone);
068 zone = null;
069 }
070
071 //-----------------------------------------------------------------------
072 public void testSetMethods() throws Throwable {
073 MockPartial mock = new MockPartial();
074 assertEquals(1970, mock.getYear());
075 assertEquals(1, mock.getMonthOfYear());
076
077 mock.setYear(2004);
078 assertEquals(2004, mock.getYear());
079 assertEquals(1, mock.getMonthOfYear());
080
081 mock.setMonthOfYear(6);
082 assertEquals(2004, mock.getYear());
083 assertEquals(6, mock.getMonthOfYear());
084
085 mock.set(2005, 5);
086 assertEquals(2005, mock.getYear());
087 assertEquals(5, mock.getMonthOfYear());
088
089 try {
090 mock.setMonthOfYear(0);
091 fail();
092 } catch (IllegalArgumentException ex) {}
093 assertEquals(2005, mock.getYear());
094 assertEquals(5, mock.getMonthOfYear());
095
096 try {
097 mock.setMonthOfYear(13);
098 fail();
099 } catch (IllegalArgumentException ex) {}
100 assertEquals(2005, mock.getYear());
101 assertEquals(5, mock.getMonthOfYear());
102 }
103
104 static class MockPartial extends BasePartial {
105
106 MockPartial() {
107 super(new int[] {1970, 1}, null);
108 }
109
110 protected DateTimeField getField(int index, Chronology chrono) {
111 switch (index) {
112 case 0:
113 return chrono.year();
114 case 1:
115 return chrono.monthOfYear();
116 default:
117 throw new IndexOutOfBoundsException();
118 }
119 }
120
121 public int size() {
122 return 2;
123 }
124
125 public int getYear() {
126 return getValue(0);
127 }
128
129 public void setYear(int year) {
130 setValue(0, year);
131 }
132
133 public int getMonthOfYear() {
134 return getValue(1);
135 }
136
137 public void setMonthOfYear(int month) {
138 setValue(1, month);
139 }
140
141 public void set(int year, int month) {
142 setValues(new int[] {year, month});
143 }
144 }
145 }