KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestAbstractPartial


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 JavaDoc[] 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 JavaDoc name) {
59         super(name);
60     }
61
62     protected void setUp() throws Exception JavaDoc {
63         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
64         zone = DateTimeZone.getDefault();
65         DateTimeZone.setDefault(DateTimeZone.UTC);
66     }
67
68     protected void tearDown() throws Exception JavaDoc {
69         DateTimeUtils.setCurrentMillisSystem();
70         DateTimeZone.setDefault(zone);
71         zone = null;
72     }
73
74     //-----------------------------------------------------------------------
75
public void testGetValue() throws Throwable JavaDoc {
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 JavaDoc ex) {}
84         try {
85             mock.getValue(2);
86             fail();
87         } catch (IndexOutOfBoundsException JavaDoc ex) {}
88     }
89
90     public void testGetValues() throws Throwable JavaDoc {
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 JavaDoc {
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 JavaDoc ex) {}
107         try {
108             mock.getField(2);
109             fail();
110         } catch (IndexOutOfBoundsException JavaDoc ex) {}
111     }
112
113     public void testGetFieldType() throws Throwable JavaDoc {
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 JavaDoc ex) {}
122         try {
123             mock.getFieldType(2);
124             fail();
125         } catch (IndexOutOfBoundsException JavaDoc ex) {}
126     }
127
128     public void testGetFieldTypes() throws Throwable JavaDoc {
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 JavaDoc {
137         MockPartial mock = new MockPartial();
138         YearMonthDay ymd = new YearMonthDay(1970, 2, 1, BuddhistChronology.getInstance());
139         
140         MockProperty0 prop0 = new MockProperty0();
141         assertEquals(true, prop0.equals(prop0));
142         assertEquals(true, prop0.equals(new MockProperty0()));
143         assertEquals(false, prop0.equals(new MockProperty1()));
144         assertEquals(false, prop0.equals(new MockProperty0Val()));
145         assertEquals(false, prop0.equals(new MockProperty0Field()));
146         assertEquals(false, prop0.equals(new MockProperty0Chrono()));
147         assertEquals(false, prop0.equals(""));
148         assertEquals(false, prop0.equals(null));
149     }
150
151     //-----------------------------------------------------------------------
152
static class MockPartial extends AbstractPartial {
153         
154         int[] val = new int[] {1970, 1};
155         
156         MockPartial() {
157             super();
158         }
159
160         protected DateTimeField getField(int index, Chronology chrono) {
161             switch (index) {
162                 case 0:
163                     return chrono.year();
164                 case 1:
165                     return chrono.monthOfYear();
166                 default:
167                     throw new IndexOutOfBoundsException JavaDoc();
168             }
169         }
170
171         public int size() {
172             return 2;
173         }
174         
175         public int getValue(int index) {
176             return val[index];
177         }
178
179         public void setValue(int index, int value) {
180             val[index] = value;
181         }
182
183         public Chronology getChronology() {
184             return BuddhistChronology.getInstanceUTC();
185         }
186     }
187     
188     static class MockProperty0 extends AbstractPartialFieldProperty {
189         MockPartial partial = new MockPartial();
190         public DateTimeField getField() {
191             return partial.getField(0);
192         }
193         public ReadablePartial getReadablePartial() {
194             return partial;
195         }
196         public int get() {
197             return partial.getValue(0);
198         }
199     }
200     static class MockProperty1 extends AbstractPartialFieldProperty {
201         MockPartial partial = new MockPartial();
202         public DateTimeField getField() {
203             return partial.getField(1);
204         }
205         public ReadablePartial getReadablePartial() {
206             return partial;
207         }
208         public int get() {
209             return partial.getValue(1);
210         }
211     }
212     static class MockProperty0Field extends MockProperty0 {
213         public DateTimeField getField() {
214             return BuddhistChronology.getInstanceUTC().hourOfDay();
215         }
216     }
217     static class MockProperty0Val extends MockProperty0 {
218         public int get() {
219             return 99;
220         }
221     }
222     static class MockProperty0Chrono extends MockProperty0 {
223         public ReadablePartial getReadablePartial() {
224             return new MockPartial() {
225                 public Chronology getChronology() {
226                     return ISOChronology.getInstanceUTC();
227                 }
228             };
229         }
230     }
231 }
232
Popular Tags