001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.field;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022:
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.joda.time.DurationField;
027: import org.joda.time.DurationFieldType;
028: import org.joda.time.chrono.ISOChronology;
029:
030: /**
031: * This class is a Junit unit test for PreciseDurationField.
032: *
033: * @author Stephen Colebourne
034: */
035: public class TestPreciseDurationField extends TestCase {
036:
037: private static final long LONG_INTEGER_MAX = Integer.MAX_VALUE;
038: private static final int INTEGER_MAX = Integer.MAX_VALUE;
039: private static final long LONG_MAX = Long.MAX_VALUE;
040:
041: private PreciseDurationField iField;
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(suite());
045: }
046:
047: public static TestSuite suite() {
048: return new TestSuite(TestPreciseDurationField.class);
049: }
050:
051: public TestPreciseDurationField(String name) {
052: super (name);
053: }
054:
055: protected void setUp() throws Exception {
056: iField = new PreciseDurationField(DurationFieldType.seconds(),
057: 1000);
058: }
059:
060: protected void tearDown() throws Exception {
061: iField = null;
062: }
063:
064: //-----------------------------------------------------------------------
065: public void test_constructor() {
066: try {
067: new PreciseDurationField(null, 10);
068: fail();
069: } catch (IllegalArgumentException ex) {
070: }
071: }
072:
073: //-----------------------------------------------------------------------
074: public void test_getType() {
075: assertEquals(DurationFieldType.seconds(), iField.getType());
076: }
077:
078: public void test_getName() {
079: assertEquals("seconds", iField.getName());
080: }
081:
082: public void test_isSupported() {
083: assertEquals(true, iField.isSupported());
084: }
085:
086: public void test_isPrecise() {
087: assertEquals(true, iField.isPrecise());
088: }
089:
090: public void test_getUnitMillis() {
091: assertEquals(1000, iField.getUnitMillis());
092: }
093:
094: public void test_toString() {
095: assertEquals("DurationField[seconds]", iField.toString());
096: }
097:
098: //-----------------------------------------------------------------------
099: public void test_getValue_long() {
100: assertEquals(0, iField.getValue(0L));
101: assertEquals(12345, iField.getValue(12345678L));
102: assertEquals(-1, iField.getValue(-1234L));
103: assertEquals(INTEGER_MAX, iField
104: .getValue(LONG_INTEGER_MAX * 1000L + 999L));
105: try {
106: iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L);
107: fail();
108: } catch (ArithmeticException ex) {
109: }
110: }
111:
112: public void test_getValueAsLong_long() {
113: assertEquals(0L, iField.getValueAsLong(0L));
114: assertEquals(12345L, iField.getValueAsLong(12345678L));
115: assertEquals(-1L, iField.getValueAsLong(-1234L));
116: assertEquals(LONG_INTEGER_MAX + 1L, iField
117: .getValueAsLong(LONG_INTEGER_MAX * 1000L + 1000L));
118: }
119:
120: public void test_getValue_long_long() {
121: assertEquals(0, iField.getValue(0L, 567L));
122: assertEquals(12345, iField.getValue(12345678L, 567L));
123: assertEquals(-1, iField.getValue(-1234L, 567L));
124: assertEquals(INTEGER_MAX, iField.getValue(
125: LONG_INTEGER_MAX * 1000L + 999L, 567L));
126: try {
127: iField.getValue(LONG_INTEGER_MAX * 1000L + 1000L, 567L);
128: fail();
129: } catch (ArithmeticException ex) {
130: }
131: }
132:
133: public void test_getValueAsLong_long_long() {
134: assertEquals(0L, iField.getValueAsLong(0L, 567L));
135: assertEquals(12345L, iField.getValueAsLong(12345678L, 567L));
136: assertEquals(-1L, iField.getValueAsLong(-1234L, 567L));
137: assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(
138: LONG_INTEGER_MAX * 1000L + 1000L, 567L));
139: }
140:
141: //-----------------------------------------------------------------------
142: public void test_getMillis_int() {
143: assertEquals(0, iField.getMillis(0));
144: assertEquals(1234000L, iField.getMillis(1234));
145: assertEquals(-1234000L, iField.getMillis(-1234));
146: assertEquals(LONG_INTEGER_MAX * 1000L, iField
147: .getMillis(INTEGER_MAX));
148: }
149:
150: public void test_getMillis_long() {
151: assertEquals(0L, iField.getMillis(0L));
152: assertEquals(1234000L, iField.getMillis(1234L));
153: assertEquals(-1234000L, iField.getMillis(-1234L));
154: try {
155: iField.getMillis(LONG_MAX);
156: fail();
157: } catch (ArithmeticException ex) {
158: }
159: }
160:
161: public void test_getMillis_int_long() {
162: assertEquals(0L, iField.getMillis(0, 567L));
163: assertEquals(1234000L, iField.getMillis(1234, 567L));
164: assertEquals(-1234000L, iField.getMillis(-1234, 567L));
165: assertEquals(LONG_INTEGER_MAX * 1000L, iField.getMillis(
166: INTEGER_MAX, 567L));
167: }
168:
169: public void test_getMillis_long_long() {
170: assertEquals(0L, iField.getMillis(0L, 567L));
171: assertEquals(1234000L, iField.getMillis(1234L, 567L));
172: assertEquals(-1234000L, iField.getMillis(-1234L, 567L));
173: try {
174: iField.getMillis(LONG_MAX, 567L);
175: fail();
176: } catch (ArithmeticException ex) {
177: }
178: }
179:
180: //-----------------------------------------------------------------------
181: public void test_add_long_int() {
182: assertEquals(567L, iField.add(567L, 0));
183: assertEquals(567L + 1234000L, iField.add(567L, 1234));
184: assertEquals(567L - 1234000L, iField.add(567L, -1234));
185: try {
186: iField.add(LONG_MAX, 1);
187: fail();
188: } catch (ArithmeticException ex) {
189: }
190: }
191:
192: public void test_add_long_long() {
193: assertEquals(567L, iField.add(567L, 0L));
194: assertEquals(567L + 1234000L, iField.add(567L, 1234L));
195: assertEquals(567L - 1234000L, iField.add(567L, -1234L));
196: try {
197: iField.add(LONG_MAX, 1L);
198: fail();
199: } catch (ArithmeticException ex) {
200: }
201: try {
202: iField.add(1L, LONG_MAX);
203: fail();
204: } catch (ArithmeticException ex) {
205: }
206: }
207:
208: //-----------------------------------------------------------------------
209: public void test_getDifference_long_int() {
210: assertEquals(0, iField.getDifference(1L, 0L));
211: assertEquals(567, iField.getDifference(567000L, 0L));
212: assertEquals(567 - 1234, iField
213: .getDifference(567000L, 1234000L));
214: assertEquals(567 + 1234, iField.getDifference(567000L,
215: -1234000L));
216: try {
217: iField.getDifference(LONG_MAX, -1L);
218: fail();
219: } catch (ArithmeticException ex) {
220: }
221: }
222:
223: public void test_getDifferenceAsLong_long_long() {
224: assertEquals(0L, iField.getDifferenceAsLong(1L, 0L));
225: assertEquals(567L, iField.getDifferenceAsLong(567000L, 0L));
226: assertEquals(567L - 1234L, iField.getDifferenceAsLong(567000L,
227: 1234000L));
228: assertEquals(567L + 1234L, iField.getDifferenceAsLong(567000L,
229: -1234000L));
230: try {
231: iField.getDifferenceAsLong(LONG_MAX, -1L);
232: fail();
233: } catch (ArithmeticException ex) {
234: }
235: }
236:
237: //-----------------------------------------------------------------------
238: public void test_equals() {
239: assertEquals(true, iField.equals(iField));
240: assertEquals(false, iField.equals(ISOChronology.getInstance()
241: .minutes()));
242: DurationField dummy = new PreciseDurationField(
243: DurationFieldType.seconds(), 0);
244: assertEquals(false, iField.equals(dummy));
245: dummy = new PreciseDurationField(DurationFieldType.seconds(),
246: 1000);
247: assertEquals(true, iField.equals(dummy));
248: dummy = new PreciseDurationField(DurationFieldType.millis(),
249: 1000);
250: assertEquals(false, iField.equals(dummy));
251: assertEquals(false, iField.equals(""));
252: assertEquals(false, iField.equals(null));
253: }
254:
255: public void test_hashCode() {
256: assertEquals(true, iField.hashCode() == iField.hashCode());
257: assertEquals(false, iField.hashCode() == ISOChronology
258: .getInstance().minutes().hashCode());
259: DurationField dummy = new PreciseDurationField(
260: DurationFieldType.seconds(), 0);
261: assertEquals(false, iField.hashCode() == dummy.hashCode());
262: dummy = new PreciseDurationField(DurationFieldType.seconds(),
263: 1000);
264: assertEquals(true, iField.hashCode() == dummy.hashCode());
265: dummy = new PreciseDurationField(DurationFieldType.millis(),
266: 1000);
267: assertEquals(false, iField.hashCode() == dummy.hashCode());
268: }
269:
270: //-----------------------------------------------------------------------
271: public void test_compareTo() {
272: assertEquals(0, iField.compareTo(iField));
273: assertEquals(-1, iField.compareTo(ISOChronology.getInstance()
274: .minutes()));
275: DurationField dummy = new PreciseDurationField(
276: DurationFieldType.seconds(), 0);
277: assertEquals(1, iField.compareTo(dummy));
278: try {
279: iField.compareTo("");
280: fail();
281: } catch (ClassCastException ex) {
282: }
283: try {
284: iField.compareTo(null);
285: fail();
286: } catch (NullPointerException ex) {
287: }
288: }
289:
290: //-----------------------------------------------------------------------
291: public void testSerialization() throws Exception {
292: DurationField test = iField;
293:
294: ByteArrayOutputStream baos = new ByteArrayOutputStream();
295: ObjectOutputStream oos = new ObjectOutputStream(baos);
296: oos.writeObject(test);
297: byte[] bytes = baos.toByteArray();
298: oos.close();
299:
300: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
301: ObjectInputStream ois = new ObjectInputStream(bais);
302: DurationField result = (DurationField) ois.readObject();
303: ois.close();
304:
305: assertEquals(test, result);
306: }
307:
308: }
|