~ubuntu-branches/ubuntu/precise/libjoda-time-java/precise

« back to all changes in this revision

Viewing changes to src/test/org/joda/time/field/TestPreciseDurationField.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2011-02-09 23:05:14 UTC
  • mfrom: (5.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110209230514-22otglg8nm2y2y2n
Tags: 1.6.2-2
* Upload to unstable.
* Add (temporary) Build-Depends: ant.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.field;
17
 
 
18
 
import java.io.ByteArrayInputStream;
19
 
import java.io.ByteArrayOutputStream;
20
 
import java.io.ObjectInputStream;
21
 
import java.io.ObjectOutputStream;
22
 
 
23
 
import junit.framework.TestCase;
24
 
import junit.framework.TestSuite;
25
 
 
26
 
import org.joda.time.DurationField;
27
 
import org.joda.time.DurationFieldType;
28
 
import org.joda.time.chrono.ISOChronology;
29
 
 
30
 
/**
31
 
 * This class is a Junit unit test for PreciseDurationField.
32
 
 *
33
 
 * @author Stephen Colebourne
34
 
 */
35
 
public class TestPreciseDurationField extends TestCase {
36
 
    
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;
40
 
    
41
 
    private PreciseDurationField iField;
42
 
 
43
 
    public static void main(String[] args) {
44
 
        junit.textui.TestRunner.run(suite());
45
 
    }
46
 
 
47
 
    public static TestSuite suite() {
48
 
        return new TestSuite(TestPreciseDurationField.class);
49
 
    }
50
 
 
51
 
    public TestPreciseDurationField(String name) {
52
 
        super(name);
53
 
    }
54
 
 
55
 
    protected void setUp() throws Exception {
56
 
        iField = new PreciseDurationField(DurationFieldType.seconds(), 1000);
57
 
    }
58
 
 
59
 
    protected void tearDown() throws Exception {
60
 
        iField = null;
61
 
    }
62
 
 
63
 
    //-----------------------------------------------------------------------
64
 
    public void test_constructor() {
65
 
        try {
66
 
            new PreciseDurationField(null, 10);
67
 
            fail();
68
 
        } catch (IllegalArgumentException ex) {}
69
 
    }
70
 
 
71
 
    //-----------------------------------------------------------------------
72
 
    public void test_getType() {
73
 
        assertEquals(DurationFieldType.seconds(), iField.getType());
74
 
    }
75
 
 
76
 
    public void test_getName() {
77
 
        assertEquals("seconds", iField.getName());
78
 
    }
79
 
    
80
 
    public void test_isSupported() {
81
 
        assertEquals(true, iField.isSupported());
82
 
    }
83
 
 
84
 
    public void test_isPrecise() {
85
 
        assertEquals(true, iField.isPrecise());
86
 
    }
87
 
 
88
 
    public void test_getUnitMillis() {
89
 
        assertEquals(1000, iField.getUnitMillis());
90
 
    }
91
 
 
92
 
    public void test_toString() {
93
 
        assertEquals("DurationField[seconds]", iField.toString());
94
 
    }
95
 
 
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));
102
 
        try {
103
 
            iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L);
104
 
            fail();
105
 
        } catch (ArithmeticException ex) {}
106
 
    }
107
 
 
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));
113
 
    }
114
 
 
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));
120
 
        try {
121
 
            iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L, 567L);
122
 
            fail();
123
 
        } catch (ArithmeticException ex) {}
124
 
    }
125
 
 
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));
131
 
    }
132
 
 
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));
139
 
    }
140
 
 
141
 
    public void test_getMillis_long() {
142
 
        assertEquals(0L, iField.getMillis(0L));
143
 
        assertEquals(1234000L, iField.getMillis(1234L));
144
 
        assertEquals(-1234000L, iField.getMillis(-1234L));
145
 
        try {
146
 
            iField.getMillis(LONG_MAX);
147
 
            fail();
148
 
        } catch (ArithmeticException ex) {}
149
 
    }
150
 
 
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));
156
 
    }
157
 
 
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));
162
 
        try {
163
 
            iField.getMillis(LONG_MAX, 567L);
164
 
            fail();
165
 
        } catch (ArithmeticException ex) {}
166
 
    }
167
 
 
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));
173
 
        try {
174
 
            iField.add(LONG_MAX, 1);
175
 
            fail();
176
 
        } catch (ArithmeticException ex) {}
177
 
    }
178
 
 
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));
183
 
        try {
184
 
            iField.add(LONG_MAX, 1L);
185
 
            fail();
186
 
        } catch (ArithmeticException ex) {}
187
 
        try {
188
 
            iField.add(1L, LONG_MAX);
189
 
            fail();
190
 
        } catch (ArithmeticException ex) {}
191
 
    }
192
 
 
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));
199
 
        try {
200
 
            iField.getDifference(LONG_MAX, -1L);
201
 
            fail();
202
 
        } catch (ArithmeticException ex) {}
203
 
    }
204
 
 
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));
210
 
        try {
211
 
            iField.getDifferenceAsLong(LONG_MAX, -1L);
212
 
            fail();
213
 
        } catch (ArithmeticException ex) {}
214
 
    }
215
 
 
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));
228
 
    }
229
 
 
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());
239
 
    }
240
 
 
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));
247
 
        try {
248
 
            iField.compareTo("");
249
 
            fail();
250
 
        } catch (ClassCastException ex) {}
251
 
        try {
252
 
            iField.compareTo(null);
253
 
            fail();
254
 
        } catch (NullPointerException ex) {}
255
 
    }
256
 
 
257
 
    //-----------------------------------------------------------------------
258
 
    public void testSerialization() throws Exception {
259
 
        DurationField test = iField;
260
 
        
261
 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
262
 
        ObjectOutputStream oos = new ObjectOutputStream(baos);
263
 
        oos.writeObject(test);
264
 
        byte[] bytes = baos.toByteArray();
265
 
        oos.close();
266
 
        
267
 
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
268
 
        ObjectInputStream ois = new ObjectInputStream(bais);
269
 
        DurationField result = (DurationField) ois.readObject();
270
 
        ois.close();
271
 
        
272
 
        assertEquals(test, result);
273
 
    }
274
 
 
275
 
}