2
* Copyright 2001-2005 Stephen Colebourne
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.joda.time.field;
18
import java.io.ByteArrayInputStream;
19
import java.io.ByteArrayOutputStream;
20
import java.io.ObjectInputStream;
21
import java.io.ObjectOutputStream;
23
import junit.framework.TestCase;
24
import junit.framework.TestSuite;
26
import org.joda.time.DurationField;
27
import org.joda.time.DurationFieldType;
28
import org.joda.time.chrono.ISOChronology;
31
* This class is a Junit unit test for PreciseDurationField.
33
* @author Stephen Colebourne
35
public class TestPreciseDurationField extends TestCase {
37
private static final long LONG_INTEGER_MAX = Integer.MAX_VALUE;
38
private static final int INTEGER_MAX = Integer.MAX_VALUE;
39
private static final long LONG_MAX = Long.MAX_VALUE;
41
private PreciseDurationField iField;
43
public static void main(String[] args) {
44
junit.textui.TestRunner.run(suite());
47
public static TestSuite suite() {
48
return new TestSuite(TestPreciseDurationField.class);
51
public TestPreciseDurationField(String name) {
55
protected void setUp() throws Exception {
56
iField = new PreciseDurationField(DurationFieldType.seconds(), 1000);
59
protected void tearDown() throws Exception {
63
//-----------------------------------------------------------------------
64
public void test_constructor() {
66
new PreciseDurationField(null, 10);
68
} catch (IllegalArgumentException ex) {}
71
//-----------------------------------------------------------------------
72
public void test_getType() {
73
assertEquals(DurationFieldType.seconds(), iField.getType());
76
public void test_getName() {
77
assertEquals("seconds", iField.getName());
80
public void test_isSupported() {
81
assertEquals(true, iField.isSupported());
84
public void test_isPrecise() {
85
assertEquals(true, iField.isPrecise());
88
public void test_getUnitMillis() {
89
assertEquals(1000, iField.getUnitMillis());
92
public void test_toString() {
93
assertEquals("DurationField[seconds]", iField.toString());
96
//-----------------------------------------------------------------------
97
public void test_getValue_long() {
98
assertEquals(0, iField.getValue(0L));
99
assertEquals(12345, iField.getValue(12345678L));
100
assertEquals(-1, iField.getValue(-1234L));
101
assertEquals(INTEGER_MAX, iField.getValue(LONG_INTEGER_MAX * 1000L + 999L));
103
iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L);
105
} catch (ArithmeticException ex) {}
108
public void test_getValueAsLong_long() {
109
assertEquals(0L, iField.getValueAsLong(0L));
110
assertEquals(12345L, iField.getValueAsLong(12345678L));
111
assertEquals(-1L, iField.getValueAsLong(-1234L));
112
assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(LONG_INTEGER_MAX * 1000L + 1000L));
115
public void test_getValue_long_long() {
116
assertEquals(0, iField.getValue(0L, 567L));
117
assertEquals(12345, iField.getValue(12345678L, 567L));
118
assertEquals(-1, iField.getValue(-1234L, 567L));
119
assertEquals(INTEGER_MAX, iField.getValue(LONG_INTEGER_MAX * 1000L + 999L, 567L));
121
iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L, 567L);
123
} catch (ArithmeticException ex) {}
126
public void test_getValueAsLong_long_long() {
127
assertEquals(0L, iField.getValueAsLong(0L, 567L));
128
assertEquals(12345L, iField.getValueAsLong(12345678L, 567L));
129
assertEquals(-1L, iField.getValueAsLong(-1234L, 567L));
130
assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(LONG_INTEGER_MAX * 1000L + 1000L, 567L));
133
//-----------------------------------------------------------------------
134
public void test_getMillis_int() {
135
assertEquals(0, iField.getMillis(0));
136
assertEquals(1234000L, iField.getMillis(1234));
137
assertEquals(-1234000L, iField.getMillis(-1234));
138
assertEquals(LONG_INTEGER_MAX * 1000L, iField.getMillis(INTEGER_MAX));
141
public void test_getMillis_long() {
142
assertEquals(0L, iField.getMillis(0L));
143
assertEquals(1234000L, iField.getMillis(1234L));
144
assertEquals(-1234000L, iField.getMillis(-1234L));
146
iField.getMillis(LONG_MAX);
148
} catch (ArithmeticException ex) {}
151
public void test_getMillis_int_long() {
152
assertEquals(0L, iField.getMillis(0, 567L));
153
assertEquals(1234000L, iField.getMillis(1234, 567L));
154
assertEquals(-1234000L, iField.getMillis(-1234, 567L));
155
assertEquals(LONG_INTEGER_MAX * 1000L, iField.getMillis(INTEGER_MAX, 567L));
158
public void test_getMillis_long_long() {
159
assertEquals(0L, iField.getMillis(0L, 567L));
160
assertEquals(1234000L, iField.getMillis(1234L, 567L));
161
assertEquals(-1234000L, iField.getMillis(-1234L, 567L));
163
iField.getMillis(LONG_MAX, 567L);
165
} catch (ArithmeticException ex) {}
168
//-----------------------------------------------------------------------
169
public void test_add_long_int() {
170
assertEquals(567L, iField.add(567L, 0));
171
assertEquals(567L + 1234000L, iField.add(567L, 1234));
172
assertEquals(567L - 1234000L, iField.add(567L, -1234));
174
iField.add(LONG_MAX, 1);
176
} catch (ArithmeticException ex) {}
179
public void test_add_long_long() {
180
assertEquals(567L, iField.add(567L, 0L));
181
assertEquals(567L + 1234000L, iField.add(567L, 1234L));
182
assertEquals(567L - 1234000L, iField.add(567L, -1234L));
184
iField.add(LONG_MAX, 1L);
186
} catch (ArithmeticException ex) {}
188
iField.add(1L, LONG_MAX);
190
} catch (ArithmeticException ex) {}
193
//-----------------------------------------------------------------------
194
public void test_getDifference_long_int() {
195
assertEquals(0, iField.getDifference(1L, 0L));
196
assertEquals(567, iField.getDifference(567000L, 0L));
197
assertEquals(567 - 1234, iField.getDifference(567000L, 1234000L));
198
assertEquals(567 + 1234, iField.getDifference(567000L, -1234000L));
200
iField.getDifference(LONG_MAX, -1L);
202
} catch (ArithmeticException ex) {}
205
public void test_getDifferenceAsLong_long_long() {
206
assertEquals(0L, iField.getDifferenceAsLong(1L, 0L));
207
assertEquals(567L, iField.getDifferenceAsLong(567000L, 0L));
208
assertEquals(567L - 1234L, iField.getDifferenceAsLong(567000L, 1234000L));
209
assertEquals(567L + 1234L, iField.getDifferenceAsLong(567000L, -1234000L));
211
iField.getDifferenceAsLong(LONG_MAX, -1L);
213
} catch (ArithmeticException ex) {}
216
//-----------------------------------------------------------------------
217
public void test_equals() {
218
assertEquals(true, iField.equals(iField));
219
assertEquals(false, iField.equals(ISOChronology.getInstance().minutes()));
220
DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
221
assertEquals(false, iField.equals(dummy));
222
dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000);
223
assertEquals(true, iField.equals(dummy));
224
dummy = new PreciseDurationField(DurationFieldType.millis(), 1000);
225
assertEquals(false, iField.equals(dummy));
226
assertEquals(false, iField.equals(""));
227
assertEquals(false, iField.equals(null));
230
public void test_hashCode() {
231
assertEquals(true, iField.hashCode() == iField.hashCode());
232
assertEquals(false, iField.hashCode() == ISOChronology.getInstance().minutes().hashCode());
233
DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
234
assertEquals(false, iField.hashCode() == dummy.hashCode());
235
dummy = new PreciseDurationField(DurationFieldType.seconds(), 1000);
236
assertEquals(true, iField.hashCode() == dummy.hashCode());
237
dummy = new PreciseDurationField(DurationFieldType.millis(), 1000);
238
assertEquals(false, iField.hashCode() == dummy.hashCode());
241
//-----------------------------------------------------------------------
242
public void test_compareTo() {
243
assertEquals(0, iField.compareTo(iField));
244
assertEquals(-1, iField.compareTo(ISOChronology.getInstance().minutes()));
245
DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
246
assertEquals(1, iField.compareTo(dummy));
248
iField.compareTo("");
250
} catch (ClassCastException ex) {}
252
iField.compareTo(null);
254
} catch (NullPointerException ex) {}
257
//-----------------------------------------------------------------------
258
public void testSerialization() throws Exception {
259
DurationField test = iField;
261
ByteArrayOutputStream baos = new ByteArrayOutputStream();
262
ObjectOutputStream oos = new ObjectOutputStream(baos);
263
oos.writeObject(test);
264
byte[] bytes = baos.toByteArray();
267
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
268
ObjectInputStream ois = new ObjectInputStream(bais);
269
DurationField result = (DurationField) ois.readObject();
272
assertEquals(test, result);