View Javadoc

1   /*
2    *  Copyright 2001-2005 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.AbstractPartial;
22  import org.joda.time.chrono.BuddhistChronology;
23  import org.joda.time.chrono.ISOChronology;
24  import org.joda.time.field.AbstractPartialFieldProperty;
25  
26  /**
27   * This class is a Junit unit test for YearMonthDay.
28   *
29   * @author Stephen Colebourne
30   */
31  public class TestAbstractPartial extends TestCase {
32  
33      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
34      
35      private long TEST_TIME_NOW =
36              (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
37              
38      private long TEST_TIME1 =
39          (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
40          + 12L * DateTimeConstants.MILLIS_PER_HOUR
41          + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
42          
43      private long TEST_TIME2 =
44          (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
45          + 14L * DateTimeConstants.MILLIS_PER_HOUR
46          + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
47          
48      private DateTimeZone zone = null;
49  
50      public static void main(String[] args) {
51          junit.textui.TestRunner.run(suite());
52      }
53  
54      public static TestSuite suite() {
55          return new TestSuite(TestAbstractPartial.class);
56      }
57  
58      public TestAbstractPartial(String name) {
59          super(name);
60      }
61  
62      protected void setUp() throws Exception {
63          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
64          zone = DateTimeZone.getDefault();
65          DateTimeZone.setDefault(DateTimeZone.UTC);
66      }
67  
68      protected void tearDown() throws Exception {
69          DateTimeUtils.setCurrentMillisSystem();
70          DateTimeZone.setDefault(zone);
71          zone = null;
72      }
73  
74      //-----------------------------------------------------------------------
75      public void testGetValue() throws Throwable {
76          MockPartial mock = new MockPartial();
77          assertEquals(1970, mock.getValue(0));
78          assertEquals(1, mock.getValue(1));
79          
80          try {
81              mock.getValue(-1);
82              fail();
83          } catch (IndexOutOfBoundsException ex) {}
84          try {
85              mock.getValue(2);
86              fail();
87          } catch (IndexOutOfBoundsException ex) {}
88      }
89  
90      public void testGetValues() throws Throwable {
91          MockPartial mock = new MockPartial();
92          int[] vals = mock.getValues();
93          assertEquals(2, vals.length);
94          assertEquals(1970, vals[0]);
95          assertEquals(1, vals[1]);
96      }
97  
98      public void testGetField() throws Throwable {
99          MockPartial mock = new MockPartial();
100         assertEquals(BuddhistChronology.getInstanceUTC().year(), mock.getField(0));
101         assertEquals(BuddhistChronology.getInstanceUTC().monthOfYear(), mock.getField(1));
102         
103         try {
104             mock.getField(-1);
105             fail();
106         } catch (IndexOutOfBoundsException ex) {}
107         try {
108             mock.getField(2);
109             fail();
110         } catch (IndexOutOfBoundsException ex) {}
111     }
112 
113     public void testGetFieldType() throws Throwable {
114         MockPartial mock = new MockPartial();
115         assertEquals(DateTimeFieldType.year(), mock.getFieldType(0));
116         assertEquals(DateTimeFieldType.monthOfYear(), mock.getFieldType(1));
117         
118         try {
119             mock.getFieldType(-1);
120             fail();
121         } catch (IndexOutOfBoundsException ex) {}
122         try {
123             mock.getFieldType(2);
124             fail();
125         } catch (IndexOutOfBoundsException ex) {}
126     }
127 
128     public void testGetFieldTypes() throws Throwable {
129         MockPartial mock = new MockPartial();
130         DateTimeFieldType[] vals = mock.getFieldTypes();
131         assertEquals(2, vals.length);
132         assertEquals(DateTimeFieldType.year(), vals[0]);
133         assertEquals(DateTimeFieldType.monthOfYear(), vals[1]);
134     }
135 
136     public void testGetPropertyEquals() throws Throwable {
137         MockProperty0 prop0 = new MockProperty0();
138         assertEquals(true, prop0.equals(prop0));
139         assertEquals(true, prop0.equals(new MockProperty0()));
140         assertEquals(false, prop0.equals(new MockProperty1()));
141         assertEquals(false, prop0.equals(new MockProperty0Val()));
142         assertEquals(false, prop0.equals(new MockProperty0Field()));
143         assertEquals(false, prop0.equals(new MockProperty0Chrono()));
144         assertEquals(false, prop0.equals(""));
145         assertEquals(false, prop0.equals(null));
146     }
147 
148     //-----------------------------------------------------------------------
149     static class MockPartial extends AbstractPartial {
150         
151         int[] val = new int[] {1970, 1};
152         
153         MockPartial() {
154             super();
155         }
156 
157         protected DateTimeField getField(int index, Chronology chrono) {
158             switch (index) {
159                 case 0:
160                     return chrono.year();
161                 case 1:
162                     return chrono.monthOfYear();
163                 default:
164                     throw new IndexOutOfBoundsException();
165             }
166         }
167 
168         public int size() {
169             return 2;
170         }
171         
172         public int getValue(int index) {
173             return val[index];
174         }
175 
176         public void setValue(int index, int value) {
177             val[index] = value;
178         }
179 
180         public Chronology getChronology() {
181             return BuddhistChronology.getInstanceUTC();
182         }
183     }
184     
185     static class MockProperty0 extends AbstractPartialFieldProperty {
186         MockPartial partial = new MockPartial();
187         public DateTimeField getField() {
188             return partial.getField(0);
189         }
190         public ReadablePartial getReadablePartial() {
191             return partial;
192         }
193         public int get() {
194             return partial.getValue(0);
195         }
196     }
197     static class MockProperty1 extends AbstractPartialFieldProperty {
198         MockPartial partial = new MockPartial();
199         public DateTimeField getField() {
200             return partial.getField(1);
201         }
202         public ReadablePartial getReadablePartial() {
203             return partial;
204         }
205         public int get() {
206             return partial.getValue(1);
207         }
208     }
209     static class MockProperty0Field extends MockProperty0 {
210         public DateTimeField getField() {
211             return BuddhistChronology.getInstanceUTC().hourOfDay();
212         }
213     }
214     static class MockProperty0Val extends MockProperty0 {
215         public int get() {
216             return 99;
217         }
218     }
219     static class MockProperty0Chrono extends MockProperty0 {
220         public ReadablePartial getReadablePartial() {
221             return new MockPartial() {
222                 public Chronology getChronology() {
223                     return ISOChronology.getInstanceUTC();
224                 }
225             };
226         }
227     }
228 }