001: /*
002: * Copyright 2001-2006 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.BaseSingleFieldPeriod;
022:
023: /**
024: * This class is a Junit unit test for BaseSingleFieldPeriod.
025: *
026: * @author Stephen Colebourne
027: */
028: public class TestBaseSingleFieldPeriod extends TestCase {
029: // Test in 2002/03 as time zones are more well known
030: // (before the late 90's they were all over the place)
031: private static final DateTimeZone PARIS = DateTimeZone
032: .forID("Europe/Paris");
033:
034: public static void main(String[] args) {
035: junit.textui.TestRunner.run(suite());
036: }
037:
038: public static TestSuite suite() {
039: return new TestSuite(TestBaseSingleFieldPeriod.class);
040: }
041:
042: public TestBaseSingleFieldPeriod(String name) {
043: super (name);
044: }
045:
046: protected void setUp() throws Exception {
047: }
048:
049: protected void tearDown() throws Exception {
050: }
051:
052: //-----------------------------------------------------------------------
053: public void testFactory_between_RInstant() {
054: // test using Days
055: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
056: DateTime end1 = new DateTime(2006, 6, 12, 12, 0, 0, 0, PARIS);
057: DateTime end2 = new DateTime(2006, 6, 15, 18, 0, 0, 0, PARIS);
058:
059: assertEquals(3, Single.between(start, end1, DurationFieldType
060: .days()));
061: assertEquals(0, Single.between(start, start, DurationFieldType
062: .days()));
063: assertEquals(0, Single.between(end1, end1, DurationFieldType
064: .days()));
065: assertEquals(-3, Single.between(end1, start, DurationFieldType
066: .days()));
067: assertEquals(6, Single.between(start, end2, DurationFieldType
068: .days()));
069: try {
070: Single.between(start, (ReadableInstant) null,
071: DurationFieldType.days());
072: fail();
073: } catch (IllegalArgumentException ex) {
074: // expected
075: }
076: try {
077: Single.between((ReadableInstant) null, end1,
078: DurationFieldType.days());
079: fail();
080: } catch (IllegalArgumentException ex) {
081: // expected
082: }
083: try {
084: Single.between((ReadableInstant) null,
085: (ReadableInstant) null, DurationFieldType.days());
086: fail();
087: } catch (IllegalArgumentException ex) {
088: // expected
089: }
090: }
091:
092: public void testFactory_between_RPartial() {
093: LocalDate start = new LocalDate(2006, 6, 9);
094: LocalDate end1 = new LocalDate(2006, 6, 12);
095: YearMonthDay end2 = new YearMonthDay(2006, 6, 15);
096:
097: Single zero = new Single(0);
098: assertEquals(3, Single.between(start, end1, zero));
099: assertEquals(0, Single.between(start, start, zero));
100: assertEquals(0, Single.between(end1, end1, zero));
101: assertEquals(-3, Single.between(end1, start, zero));
102: assertEquals(6, Single.between(start, end2, zero));
103: try {
104: Single.between(start, (ReadablePartial) null, zero);
105: fail();
106: } catch (IllegalArgumentException ex) {
107: // expected
108: }
109: try {
110: Single.between((ReadablePartial) null, end1, zero);
111: fail();
112: } catch (IllegalArgumentException ex) {
113: // expected
114: }
115: try {
116: Single.between((ReadablePartial) null,
117: (ReadablePartial) null, zero);
118: fail();
119: } catch (IllegalArgumentException ex) {
120: // expected
121: }
122: try {
123: Single.between(start, new TimeOfDay(), zero);
124: fail();
125: } catch (IllegalArgumentException ex) {
126: // expected
127: }
128: try {
129: Single.between(
130: new Partial(DateTimeFieldType.dayOfWeek(), 2),
131: new Partial(DateTimeFieldType.dayOfMonth(), 3),
132: zero);
133: fail();
134: } catch (IllegalArgumentException ex) {
135: // expected
136: }
137: Partial p = new Partial(
138: new DateTimeFieldType[] { DateTimeFieldType.year(),
139: DateTimeFieldType.hourOfDay() }, new int[] { 1,
140: 2 });
141: try {
142: Single.between(p, p, zero);
143: fail();
144: } catch (IllegalArgumentException ex) {
145: // expected
146: }
147: }
148:
149: public void testFactory_standardPeriodIn_RPeriod() {
150: assertEquals(0, Single.standardPeriodIn((ReadablePeriod) null,
151: DateTimeConstants.MILLIS_PER_DAY));
152: assertEquals(0, Single.standardPeriodIn(Period.ZERO,
153: DateTimeConstants.MILLIS_PER_DAY));
154: assertEquals(1, Single.standardPeriodIn(new Period(0, 0, 0, 1,
155: 0, 0, 0, 0), DateTimeConstants.MILLIS_PER_DAY));
156: assertEquals(123, Single.standardPeriodIn(Period.days(123),
157: DateTimeConstants.MILLIS_PER_DAY));
158: assertEquals(-987, Single.standardPeriodIn(Period.days(-987),
159: DateTimeConstants.MILLIS_PER_DAY));
160: assertEquals(1, Single.standardPeriodIn(Period.hours(47),
161: DateTimeConstants.MILLIS_PER_DAY));
162: assertEquals(2, Single.standardPeriodIn(Period.hours(48),
163: DateTimeConstants.MILLIS_PER_DAY));
164: assertEquals(2, Single.standardPeriodIn(Period.hours(49),
165: DateTimeConstants.MILLIS_PER_DAY));
166: assertEquals(14, Single.standardPeriodIn(Period.weeks(2),
167: DateTimeConstants.MILLIS_PER_DAY));
168: try {
169: Single.standardPeriodIn(Period.months(1),
170: DateTimeConstants.MILLIS_PER_DAY);
171: fail();
172: } catch (IllegalArgumentException ex) {
173: // expeceted
174: }
175: }
176:
177: //-----------------------------------------------------------------------
178: public void testValueIndexMethods() {
179: Single test = new Single(20);
180: assertEquals(1, test.size());
181: assertEquals(20, test.getValue(0));
182: try {
183: test.getValue(1);
184: fail();
185: } catch (IndexOutOfBoundsException ex) {
186: // expected
187: }
188: }
189:
190: public void testFieldTypeIndexMethods() {
191: Single test = new Single(20);
192: assertEquals(1, test.size());
193: assertEquals(DurationFieldType.days(), test.getFieldType(0));
194: try {
195: test.getFieldType(1);
196: fail();
197: } catch (IndexOutOfBoundsException ex) {
198: // expected
199: }
200: }
201:
202: public void testIsSupported() {
203: Single test = new Single(20);
204: assertEquals(false, test.isSupported(DurationFieldType.years()));
205: assertEquals(false, test
206: .isSupported(DurationFieldType.months()));
207: assertEquals(false, test.isSupported(DurationFieldType.weeks()));
208: assertEquals(true, test.isSupported(DurationFieldType.days()));
209: assertEquals(false, test.isSupported(DurationFieldType.hours()));
210: assertEquals(false, test.isSupported(DurationFieldType
211: .minutes()));
212: assertEquals(false, test.isSupported(DurationFieldType
213: .seconds()));
214: assertEquals(false, test
215: .isSupported(DurationFieldType.millis()));
216: }
217:
218: public void testGet() {
219: Single test = new Single(20);
220: assertEquals(0, test.get(DurationFieldType.years()));
221: assertEquals(0, test.get(DurationFieldType.months()));
222: assertEquals(0, test.get(DurationFieldType.weeks()));
223: assertEquals(20, test.get(DurationFieldType.days()));
224: assertEquals(0, test.get(DurationFieldType.hours()));
225: assertEquals(0, test.get(DurationFieldType.minutes()));
226: assertEquals(0, test.get(DurationFieldType.seconds()));
227: assertEquals(0, test.get(DurationFieldType.millis()));
228: }
229:
230: //-----------------------------------------------------------------------
231: public void testEqualsHashCode() {
232: Single testA = new Single(20);
233: Single testB = new Single(20);
234: assertEquals(true, testA.equals(testB));
235: assertEquals(true, testB.equals(testA));
236: assertEquals(true, testA.equals(testA));
237: assertEquals(true, testB.equals(testB));
238: assertEquals(true, testA.hashCode() == testB.hashCode());
239: assertEquals(true, testA.hashCode() == testA.hashCode());
240: assertEquals(true, testB.hashCode() == testB.hashCode());
241:
242: Single testC = new Single(30);
243: assertEquals(false, testA.equals(testC));
244: assertEquals(false, testB.equals(testC));
245: assertEquals(false, testC.equals(testA));
246: assertEquals(false, testC.equals(testB));
247: assertEquals(false, testA.hashCode() == testC.hashCode());
248: assertEquals(false, testB.hashCode() == testC.hashCode());
249:
250: assertEquals(true, testA.equals(Days.days(20)));
251: assertEquals(true, testA.equals(new Period(0, 0, 0, 20, 0, 0,
252: 0, 0, PeriodType.days())));
253: assertEquals(false, testA.equals(Period.days(2)));
254: assertEquals(false, testA.equals("Hello"));
255: assertEquals(false, testA.equals(Hours.hours(2)));
256: assertEquals(false, testA.equals(null));
257: }
258:
259: public void testCompareTo() {
260: Single test1 = new Single(21);
261: Single test2 = new Single(22);
262: Single test3 = new Single(23);
263: assertEquals(true, test1.compareTo(test1) == 0);
264: assertEquals(true, test1.compareTo(test2) < 0);
265: assertEquals(true, test1.compareTo(test3) < 0);
266: assertEquals(true, test2.compareTo(test1) > 0);
267: assertEquals(true, test2.compareTo(test2) == 0);
268: assertEquals(true, test2.compareTo(test3) < 0);
269: assertEquals(true, test3.compareTo(test1) > 0);
270: assertEquals(true, test3.compareTo(test2) > 0);
271: assertEquals(true, test3.compareTo(test3) == 0);
272:
273: try {
274: test1.compareTo("Hello");
275: fail();
276: } catch (ClassCastException ex) {
277: // expected
278: }
279: try {
280: test1.compareTo(new Period(0, 0, 0, 21, 0, 0, 0, 0,
281: PeriodType.days()));
282: fail();
283: } catch (ClassCastException ex) {
284: // expected
285: }
286: try {
287: test1.compareTo(null);
288: fail();
289: } catch (NullPointerException ex) {
290: // expected
291: }
292: }
293:
294: //-----------------------------------------------------------------------
295: public void testToPeriod() {
296: Single test = new Single(20);
297: Period expected = Period.days(20);
298: assertEquals(expected, test.toPeriod());
299: }
300:
301: public void testToMutablePeriod() {
302: Single test = new Single(20);
303: MutablePeriod expected = new MutablePeriod(0, 0, 0, 20, 0, 0,
304: 0, 0);
305: assertEquals(expected, test.toMutablePeriod());
306: }
307:
308: // public void testToDurationFrom() {
309: // Period test = new Period(123L);
310: // assertEquals(new Duration(123L), test.toDurationFrom(new Instant(0L)));
311: // }
312: //
313: // public void testToDurationTo() {
314: // Period test = new Period(123L);
315: // assertEquals(new Duration(123L), test.toDurationTo(new Instant(123L)));
316: // }
317: //
318:
319: //-----------------------------------------------------------------------
320: public void testGetSetValue() {
321: Single test = new Single(20);
322: assertEquals(20, test.getValue());
323: test.setValue(10);
324: assertEquals(10, test.getValue());
325: }
326:
327: //-----------------------------------------------------------------------
328: /** Test class. */
329: static class Single extends BaseSingleFieldPeriod {
330:
331: public Single(int period) {
332: super (period);
333: }
334:
335: public static int between(ReadableInstant start,
336: ReadableInstant end, DurationFieldType field) {
337: return BaseSingleFieldPeriod.between(start, end, field);
338: }
339:
340: public static int between(ReadablePartial start,
341: ReadablePartial end, ReadablePeriod zeroInstance) {
342: return BaseSingleFieldPeriod.between(start, end,
343: zeroInstance);
344: }
345:
346: public static int standardPeriodIn(ReadablePeriod period,
347: long millisPerUnit) {
348: return BaseSingleFieldPeriod.standardPeriodIn(period,
349: millisPerUnit);
350: }
351:
352: public DurationFieldType getFieldType() {
353: return DurationFieldType.days();
354: }
355:
356: public PeriodType getPeriodType() {
357: return PeriodType.days();
358: }
359:
360: public int getValue() {
361: return super .getValue();
362: }
363:
364: public void setValue(int value) {
365: super.setValue(value);
366: }
367: }
368:
369: }
|