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