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.AbstractPartial;
022: import org.joda.time.chrono.BuddhistChronology;
023: import org.joda.time.chrono.ISOChronology;
024: import org.joda.time.field.AbstractPartialFieldProperty;
025:
026: /**
027: * This class is a Junit unit test for YearMonthDay.
028: *
029: * @author Stephen Colebourne
030: */
031: public class TestAbstractPartial extends TestCase {
032:
033: private static final DateTimeZone PARIS = DateTimeZone
034: .forID("Europe/Paris");
035:
036: private long TEST_TIME_NOW = (31L + 28L + 31L + 30L + 31L + 9L - 1L)
037: * DateTimeConstants.MILLIS_PER_DAY;
038:
039: private long TEST_TIME1 = (31L + 28L + 31L + 6L - 1L)
040: * DateTimeConstants.MILLIS_PER_DAY + 12L
041: * DateTimeConstants.MILLIS_PER_HOUR + 24L
042: * DateTimeConstants.MILLIS_PER_MINUTE;
043:
044: private long TEST_TIME2 = (365L + 31L + 28L + 31L + 30L + 7L - 1L)
045: * DateTimeConstants.MILLIS_PER_DAY + 14L
046: * DateTimeConstants.MILLIS_PER_HOUR + 28L
047: * DateTimeConstants.MILLIS_PER_MINUTE;
048:
049: private DateTimeZone zone = null;
050:
051: public static void main(String[] args) {
052: junit.textui.TestRunner.run(suite());
053: }
054:
055: public static TestSuite suite() {
056: return new TestSuite(TestAbstractPartial.class);
057: }
058:
059: public TestAbstractPartial(String name) {
060: super (name);
061: }
062:
063: protected void setUp() throws Exception {
064: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
065: zone = DateTimeZone.getDefault();
066: DateTimeZone.setDefault(DateTimeZone.UTC);
067: }
068:
069: protected void tearDown() throws Exception {
070: DateTimeUtils.setCurrentMillisSystem();
071: DateTimeZone.setDefault(zone);
072: zone = null;
073: }
074:
075: //-----------------------------------------------------------------------
076: public void testGetValue() throws Throwable {
077: MockPartial mock = new MockPartial();
078: assertEquals(1970, mock.getValue(0));
079: assertEquals(1, mock.getValue(1));
080:
081: try {
082: mock.getValue(-1);
083: fail();
084: } catch (IndexOutOfBoundsException ex) {
085: }
086: try {
087: mock.getValue(2);
088: fail();
089: } catch (IndexOutOfBoundsException ex) {
090: }
091: }
092:
093: public void testGetValues() throws Throwable {
094: MockPartial mock = new MockPartial();
095: int[] vals = mock.getValues();
096: assertEquals(2, vals.length);
097: assertEquals(1970, vals[0]);
098: assertEquals(1, vals[1]);
099: }
100:
101: public void testGetField() throws Throwable {
102: MockPartial mock = new MockPartial();
103: assertEquals(BuddhistChronology.getInstanceUTC().year(), mock
104: .getField(0));
105: assertEquals(BuddhistChronology.getInstanceUTC().monthOfYear(),
106: mock.getField(1));
107:
108: try {
109: mock.getField(-1);
110: fail();
111: } catch (IndexOutOfBoundsException ex) {
112: }
113: try {
114: mock.getField(2);
115: fail();
116: } catch (IndexOutOfBoundsException ex) {
117: }
118: }
119:
120: public void testGetFieldType() throws Throwable {
121: MockPartial mock = new MockPartial();
122: assertEquals(DateTimeFieldType.year(), mock.getFieldType(0));
123: assertEquals(DateTimeFieldType.monthOfYear(), mock
124: .getFieldType(1));
125:
126: try {
127: mock.getFieldType(-1);
128: fail();
129: } catch (IndexOutOfBoundsException ex) {
130: }
131: try {
132: mock.getFieldType(2);
133: fail();
134: } catch (IndexOutOfBoundsException ex) {
135: }
136: }
137:
138: public void testGetFieldTypes() throws Throwable {
139: MockPartial mock = new MockPartial();
140: DateTimeFieldType[] vals = mock.getFieldTypes();
141: assertEquals(2, vals.length);
142: assertEquals(DateTimeFieldType.year(), vals[0]);
143: assertEquals(DateTimeFieldType.monthOfYear(), vals[1]);
144: }
145:
146: public void testGetPropertyEquals() throws Throwable {
147: MockPartial mock = new MockPartial();
148: YearMonthDay ymd = new YearMonthDay(1970, 2, 1,
149: BuddhistChronology.getInstance());
150:
151: MockProperty0 prop0 = new MockProperty0();
152: assertEquals(true, prop0.equals(prop0));
153: assertEquals(true, prop0.equals(new MockProperty0()));
154: assertEquals(false, prop0.equals(new MockProperty1()));
155: assertEquals(false, prop0.equals(new MockProperty0Val()));
156: assertEquals(false, prop0.equals(new MockProperty0Field()));
157: assertEquals(false, prop0.equals(new MockProperty0Chrono()));
158: assertEquals(false, prop0.equals(""));
159: assertEquals(false, prop0.equals(null));
160: }
161:
162: //-----------------------------------------------------------------------
163: static class MockPartial extends AbstractPartial {
164:
165: int[] val = new int[] { 1970, 1 };
166:
167: MockPartial() {
168: super ();
169: }
170:
171: protected DateTimeField getField(int index, Chronology chrono) {
172: switch (index) {
173: case 0:
174: return chrono.year();
175: case 1:
176: return chrono.monthOfYear();
177: default:
178: throw new IndexOutOfBoundsException();
179: }
180: }
181:
182: public int size() {
183: return 2;
184: }
185:
186: public int getValue(int index) {
187: return val[index];
188: }
189:
190: public void setValue(int index, int value) {
191: val[index] = value;
192: }
193:
194: public Chronology getChronology() {
195: return BuddhistChronology.getInstanceUTC();
196: }
197: }
198:
199: static class MockProperty0 extends AbstractPartialFieldProperty {
200: MockPartial partial = new MockPartial();
201:
202: public DateTimeField getField() {
203: return partial.getField(0);
204: }
205:
206: public ReadablePartial getReadablePartial() {
207: return partial;
208: }
209:
210: public int get() {
211: return partial.getValue(0);
212: }
213: }
214:
215: static class MockProperty1 extends AbstractPartialFieldProperty {
216: MockPartial partial = new MockPartial();
217:
218: public DateTimeField getField() {
219: return partial.getField(1);
220: }
221:
222: public ReadablePartial getReadablePartial() {
223: return partial;
224: }
225:
226: public int get() {
227: return partial.getValue(1);
228: }
229: }
230:
231: static class MockProperty0Field extends MockProperty0 {
232: public DateTimeField getField() {
233: return BuddhistChronology.getInstanceUTC().hourOfDay();
234: }
235: }
236:
237: static class MockProperty0Val extends MockProperty0 {
238: public int get() {
239: return 99;
240: }
241: }
242:
243: static class MockProperty0Chrono extends MockProperty0 {
244: public ReadablePartial getReadablePartial() {
245: return new MockPartial() {
246: public Chronology getChronology() {
247: return ISOChronology.getInstanceUTC();
248: }
249: };
250: }
251: }
252: }
|