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