View Javadoc

1   /*
2    *  Copyright 2001-2009 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time;
17  
18  import junit.framework.TestCase;
19  import junit.framework.TestSuite;
20  
21  import org.joda.time.base.BaseSingleFieldPeriod;
22  
23  /**
24   * This class is a Junit unit test for BaseSingleFieldPeriod.
25   *
26   * @author Stephen Colebourne
27   */
28  public class TestBaseSingleFieldPeriod extends TestCase {
29      // Test in 2002/03 as time zones are more well known
30      // (before the late 90's they were all over the place)
31      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
32  
33      public static void main(String[] args) {
34          junit.textui.TestRunner.run(suite());
35      }
36  
37      public static TestSuite suite() {
38          return new TestSuite(TestBaseSingleFieldPeriod.class);
39      }
40  
41      public TestBaseSingleFieldPeriod(String name) {
42          super(name);
43      }
44  
45      protected void setUp() throws Exception {
46      }
47  
48      protected void tearDown() throws Exception {
49      }
50  
51      //-----------------------------------------------------------------------
52      public void testFactory_between_RInstant() {
53          // test using Days
54          DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
55          DateTime end1 = new DateTime(2006, 6, 12, 12, 0, 0, 0, PARIS);
56          DateTime end2 = new DateTime(2006, 6, 15, 18, 0, 0, 0, PARIS);
57          
58          assertEquals(3, Single.between(start, end1, DurationFieldType.days()));
59          assertEquals(0, Single.between(start, start, DurationFieldType.days()));
60          assertEquals(0, Single.between(end1, end1, DurationFieldType.days()));
61          assertEquals(-3, Single.between(end1, start, DurationFieldType.days()));
62          assertEquals(6, Single.between(start, end2, DurationFieldType.days()));
63          try {
64              Single.between(start, (ReadableInstant) null, DurationFieldType.days());
65              fail();
66          } catch (IllegalArgumentException ex) {
67              // expected
68          }
69          try {
70              Single.between((ReadableInstant) null, end1, DurationFieldType.days());
71              fail();
72          } catch (IllegalArgumentException ex) {
73              // expected
74          }
75          try {
76              Single.between((ReadableInstant) null, (ReadableInstant) null, DurationFieldType.days());
77              fail();
78          } catch (IllegalArgumentException ex) {
79              // expected
80          }
81      }
82  
83      public void testFactory_between_RPartial() {
84          LocalDate start = new LocalDate(2006, 6, 9);
85          LocalDate end1 = new LocalDate(2006, 6, 12);
86          YearMonthDay end2 = new YearMonthDay(2006, 6, 15);
87          
88          Single zero = new Single(0);
89          assertEquals(3, Single.between(start, end1, zero));
90          assertEquals(0, Single.between(start, start, zero));
91          assertEquals(0, Single.between(end1, end1, zero));
92          assertEquals(-3, Single.between(end1, start, zero));
93          assertEquals(6, Single.between(start, end2, zero));
94          try {
95              Single.between(start, (ReadablePartial) null, zero);
96              fail();
97          } catch (IllegalArgumentException ex) {
98              // expected
99          }
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 }