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 TestScaledDurationField 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 ScaledDurationField 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(TestScaledDurationField.class);
049: }
050:
051: public TestScaledDurationField(String name) {
052: super (name);
053: }
054:
055: protected void setUp() throws Exception {
056: DurationField base = MillisDurationField.INSTANCE;
057: iField = new ScaledDurationField(base, DurationFieldType
058: .minutes(), 90);
059: }
060:
061: protected void tearDown() throws Exception {
062: iField = null;
063: }
064:
065: //-----------------------------------------------------------------------
066: public void test_constructor() {
067: try {
068: new ScaledDurationField(null, DurationFieldType.minutes(),
069: 10);
070: fail();
071: } catch (IllegalArgumentException ex) {
072: }
073: try {
074: new ScaledDurationField(MillisDurationField.INSTANCE, null,
075: 10);
076: fail();
077: } catch (IllegalArgumentException ex) {
078: }
079: try {
080: new ScaledDurationField(MillisDurationField.INSTANCE,
081: DurationFieldType.minutes(), 0);
082: fail();
083: } catch (IllegalArgumentException ex) {
084: }
085: try {
086: new ScaledDurationField(MillisDurationField.INSTANCE,
087: DurationFieldType.minutes(), 1);
088: fail();
089: } catch (IllegalArgumentException ex) {
090: }
091: }
092:
093: public void test_getScalar() {
094: assertEquals(90, iField.getScalar());
095: }
096:
097: //-----------------------------------------------------------------------
098: public void test_getType() {
099: assertEquals(DurationFieldType.minutes(), iField.getType());
100: }
101:
102: public void test_getName() {
103: assertEquals("minutes", iField.getName());
104: }
105:
106: public void test_isSupported() {
107: assertEquals(true, iField.isSupported());
108: }
109:
110: public void test_isPrecise() {
111: assertEquals(true, iField.isPrecise());
112: }
113:
114: public void test_getUnitMillis() {
115: assertEquals(90, iField.getUnitMillis());
116: }
117:
118: public void test_toString() {
119: assertEquals("DurationField[minutes]", iField.toString());
120: }
121:
122: //-----------------------------------------------------------------------
123: public void test_getValue_long() {
124: assertEquals(0, iField.getValue(0L));
125: assertEquals(12345678 / 90, iField.getValue(12345678L));
126: assertEquals(-1234 / 90, iField.getValue(-1234L));
127: assertEquals(INTEGER_MAX / 90, iField
128: .getValue(LONG_INTEGER_MAX));
129: try {
130: iField.getValue(LONG_INTEGER_MAX + 1L);
131: fail();
132: } catch (ArithmeticException ex) {
133: }
134: }
135:
136: public void test_getValueAsLong_long() {
137: assertEquals(0L, iField.getValueAsLong(0L));
138: assertEquals(12345678L / 90, iField.getValueAsLong(12345678L));
139: assertEquals(-1234 / 90L, iField.getValueAsLong(-1234L));
140: assertEquals(LONG_INTEGER_MAX + 1L, iField
141: .getValueAsLong(LONG_INTEGER_MAX * 90L + 90L));
142: }
143:
144: public void test_getValue_long_long() {
145: assertEquals(0, iField.getValue(0L, 567L));
146: assertEquals(12345678 / 90, iField.getValue(12345678L, 567L));
147: assertEquals(-1234 / 90, iField.getValue(-1234L, 567L));
148: assertEquals(INTEGER_MAX / 90, iField.getValue(
149: LONG_INTEGER_MAX, 567L));
150: try {
151: iField.getValue(LONG_INTEGER_MAX + 1L, 567L);
152: fail();
153: } catch (ArithmeticException ex) {
154: }
155: }
156:
157: public void test_getValueAsLong_long_long() {
158: assertEquals(0L, iField.getValueAsLong(0L, 567L));
159: assertEquals(12345678 / 90L, iField.getValueAsLong(12345678L,
160: 567L));
161: assertEquals(-1234 / 90L, iField.getValueAsLong(-1234L, 567L));
162: assertEquals(LONG_INTEGER_MAX + 1L, iField.getValueAsLong(
163: LONG_INTEGER_MAX * 90L + 90L, 567L));
164: }
165:
166: //-----------------------------------------------------------------------
167: public void test_getMillis_int() {
168: assertEquals(0, iField.getMillis(0));
169: assertEquals(1234L * 90L, iField.getMillis(1234));
170: assertEquals(-1234L * 90L, iField.getMillis(-1234));
171: assertEquals(LONG_INTEGER_MAX * 90L, iField
172: .getMillis(INTEGER_MAX));
173: }
174:
175: public void test_getMillis_long() {
176: assertEquals(0L, iField.getMillis(0L));
177: assertEquals(1234L * 90L, iField.getMillis(1234L));
178: assertEquals(-1234L * 90L, iField.getMillis(-1234L));
179: try {
180: iField.getMillis(LONG_MAX);
181: fail();
182: } catch (ArithmeticException ex) {
183: }
184: }
185:
186: public void test_getMillis_int_long() {
187: assertEquals(0L, iField.getMillis(0, 567L));
188: assertEquals(1234L * 90L, iField.getMillis(1234, 567L));
189: assertEquals(-1234L * 90L, iField.getMillis(-1234, 567L));
190: assertEquals(LONG_INTEGER_MAX * 90L, iField.getMillis(
191: INTEGER_MAX, 567L));
192: }
193:
194: public void test_getMillis_long_long() {
195: assertEquals(0L, iField.getMillis(0L, 567L));
196: assertEquals(1234L * 90L, iField.getMillis(1234L, 567L));
197: assertEquals(-1234L * 90L, iField.getMillis(-1234L, 567L));
198: try {
199: iField.getMillis(LONG_MAX, 567L);
200: fail();
201: } catch (ArithmeticException ex) {
202: }
203: }
204:
205: //-----------------------------------------------------------------------
206: public void test_add_long_int() {
207: assertEquals(567L, iField.add(567L, 0));
208: assertEquals(567L + 1234L * 90L, iField.add(567L, 1234));
209: assertEquals(567L - 1234L * 90L, iField.add(567L, -1234));
210: try {
211: iField.add(LONG_MAX, 1);
212: fail();
213: } catch (ArithmeticException ex) {
214: }
215: }
216:
217: public void test_add_long_long() {
218: assertEquals(567L, iField.add(567L, 0L));
219: assertEquals(567L + 1234L * 90L, iField.add(567L, 1234L));
220: assertEquals(567L - 1234L * 90L, iField.add(567L, -1234L));
221: try {
222: iField.add(LONG_MAX, 1L);
223: fail();
224: } catch (ArithmeticException ex) {
225: }
226: try {
227: iField.add(1L, LONG_MAX);
228: fail();
229: } catch (ArithmeticException ex) {
230: }
231: }
232:
233: //-----------------------------------------------------------------------
234: public void test_getDifference_long_int() {
235: assertEquals(0, iField.getDifference(1L, 0L));
236: assertEquals(567, iField.getDifference(567L * 90L, 0L));
237: assertEquals(567 - 1234, iField.getDifference(567L * 90L,
238: 1234L * 90L));
239: assertEquals(567 + 1234, iField.getDifference(567L * 90L,
240: -1234L * 90L));
241: try {
242: iField.getDifference(LONG_MAX, -1L);
243: fail();
244: } catch (ArithmeticException ex) {
245: }
246: }
247:
248: public void test_getDifferenceAsLong_long_long() {
249: assertEquals(0L, iField.getDifferenceAsLong(1L, 0L));
250: assertEquals(567L, iField.getDifferenceAsLong(567L * 90L, 0L));
251: assertEquals(567L - 1234L, iField.getDifferenceAsLong(
252: 567L * 90L, 1234L * 90L));
253: assertEquals(567L + 1234L, iField.getDifferenceAsLong(
254: 567L * 90L, -1234L * 90L));
255: try {
256: iField.getDifferenceAsLong(LONG_MAX, -1L);
257: fail();
258: } catch (ArithmeticException ex) {
259: }
260: }
261:
262: //-----------------------------------------------------------------------
263: public void test_equals() {
264: assertEquals(true, iField.equals(iField));
265: assertEquals(false, iField.equals(ISOChronology.getInstance()
266: .minutes()));
267: DurationField dummy = new ScaledDurationField(
268: MillisDurationField.INSTANCE, DurationFieldType
269: .minutes(), 2);
270: assertEquals(false, iField.equals(dummy));
271: dummy = new ScaledDurationField(MillisDurationField.INSTANCE,
272: DurationFieldType.minutes(), 90);
273: assertEquals(true, iField.equals(dummy));
274: dummy = new ScaledDurationField(MillisDurationField.INSTANCE,
275: DurationFieldType.millis(), 90);
276: assertEquals(false, iField.equals(dummy));
277: assertEquals(false, iField.equals(""));
278: assertEquals(false, iField.equals(null));
279: }
280:
281: public void test_hashCode() {
282: assertEquals(iField.hashCode(), iField.hashCode());
283: assertEquals(false, iField.hashCode() == ISOChronology
284: .getInstance().minutes().hashCode());
285: DurationField dummy = new ScaledDurationField(
286: MillisDurationField.INSTANCE, DurationFieldType
287: .minutes(), 2);
288: assertEquals(false, iField.hashCode() == dummy.hashCode());
289: dummy = new ScaledDurationField(MillisDurationField.INSTANCE,
290: DurationFieldType.minutes(), 90);
291: assertEquals(true, iField.hashCode() == dummy.hashCode());
292: dummy = new ScaledDurationField(MillisDurationField.INSTANCE,
293: DurationFieldType.millis(), 90);
294: assertEquals(false, iField.hashCode() == dummy.hashCode());
295: }
296:
297: //-----------------------------------------------------------------------
298: public void test_compareTo() {
299: assertEquals(0, iField.compareTo(iField));
300: assertEquals(-1, iField.compareTo(ISOChronology.getInstance()
301: .minutes()));
302: DurationField dummy = new PreciseDurationField(
303: DurationFieldType.minutes(), 0);
304: assertEquals(1, iField.compareTo(dummy));
305: try {
306: iField.compareTo("");
307: fail();
308: } catch (ClassCastException ex) {
309: }
310: try {
311: iField.compareTo(null);
312: fail();
313: } catch (NullPointerException ex) {
314: }
315: }
316:
317: //-----------------------------------------------------------------------
318: public void testSerialization() throws Exception {
319: DurationField test = iField;
320:
321: ByteArrayOutputStream baos = new ByteArrayOutputStream();
322: ObjectOutputStream oos = new ObjectOutputStream(baos);
323: oos.writeObject(test);
324: byte[] bytes = baos.toByteArray();
325: oos.close();
326:
327: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
328: ObjectInputStream ois = new ObjectInputStream(bais);
329: DurationField result = (DurationField) ois.readObject();
330: ois.close();
331:
332: assertEquals(test, result);
333: }
334:
335: }
|