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 PeriodFormatterBuilder.
032: *
033: * @author Stephen Colebourne
034: */
035: public class TestMillisDurationField extends TestCase {
036:
037: public static void main(String[] args) {
038: junit.textui.TestRunner.run(suite());
039: }
040:
041: public static TestSuite suite() {
042: return new TestSuite(TestMillisDurationField.class);
043: }
044:
045: public TestMillisDurationField(String name) {
046: super (name);
047: }
048:
049: protected void setUp() throws Exception {
050: }
051:
052: protected void tearDown() throws Exception {
053: }
054:
055: //-----------------------------------------------------------------------
056: public void test_getType() {
057: assertEquals(DurationFieldType.millis(),
058: MillisDurationField.INSTANCE.getType());
059: }
060:
061: public void test_getName() {
062: assertEquals("millis", MillisDurationField.INSTANCE.getName());
063: }
064:
065: public void test_isSupported() {
066: assertEquals(true, MillisDurationField.INSTANCE.isSupported());
067: }
068:
069: public void test_isPrecise() {
070: assertEquals(true, MillisDurationField.INSTANCE.isPrecise());
071: }
072:
073: public void test_getUnitMillis() {
074: assertEquals(1, MillisDurationField.INSTANCE.getUnitMillis());
075: }
076:
077: public void test_toString() {
078: assertEquals("DurationField[millis]",
079: MillisDurationField.INSTANCE.toString());
080: }
081:
082: //-----------------------------------------------------------------------
083: public void test_getValue_long() {
084: assertEquals(0, MillisDurationField.INSTANCE.getValue(0L));
085: assertEquals(1234, MillisDurationField.INSTANCE.getValue(1234L));
086: assertEquals(-1234, MillisDurationField.INSTANCE
087: .getValue(-1234L));
088: try {
089: MillisDurationField.INSTANCE
090: .getValue(((long) (Integer.MAX_VALUE)) + 1L);
091: fail();
092: } catch (ArithmeticException ex) {
093: }
094: }
095:
096: public void test_getValueAsLong_long() {
097: assertEquals(0L, MillisDurationField.INSTANCE
098: .getValueAsLong(0L));
099: assertEquals(1234L, MillisDurationField.INSTANCE
100: .getValueAsLong(1234L));
101: assertEquals(-1234L, MillisDurationField.INSTANCE
102: .getValueAsLong(-1234L));
103: assertEquals(
104: ((long) (Integer.MAX_VALUE)) + 1L,
105: MillisDurationField.INSTANCE
106: .getValueAsLong(((long) (Integer.MAX_VALUE)) + 1L));
107: }
108:
109: public void test_getValue_long_long() {
110: assertEquals(0, MillisDurationField.INSTANCE.getValue(0L, 567L));
111: assertEquals(1234, MillisDurationField.INSTANCE.getValue(1234L,
112: 567L));
113: assertEquals(-1234, MillisDurationField.INSTANCE.getValue(
114: -1234L, 567L));
115: try {
116: MillisDurationField.INSTANCE.getValue(
117: ((long) (Integer.MAX_VALUE)) + 1L, 567L);
118: fail();
119: } catch (ArithmeticException ex) {
120: }
121: }
122:
123: public void test_getValueAsLong_long_long() {
124: assertEquals(0L, MillisDurationField.INSTANCE.getValueAsLong(
125: 0L, 567L));
126: assertEquals(1234L, MillisDurationField.INSTANCE
127: .getValueAsLong(1234L, 567L));
128: assertEquals(-1234L, MillisDurationField.INSTANCE
129: .getValueAsLong(-1234L, 567L));
130: assertEquals(((long) (Integer.MAX_VALUE)) + 1L,
131: MillisDurationField.INSTANCE.getValueAsLong(
132: ((long) (Integer.MAX_VALUE)) + 1L, 567L));
133: }
134:
135: //-----------------------------------------------------------------------
136: public void test_getMillis_int() {
137: assertEquals(0, MillisDurationField.INSTANCE.getMillis(0));
138: assertEquals(1234, MillisDurationField.INSTANCE.getMillis(1234));
139: assertEquals(-1234, MillisDurationField.INSTANCE
140: .getMillis(-1234));
141: }
142:
143: public void test_getMillis_long() {
144: assertEquals(0L, MillisDurationField.INSTANCE.getMillis(0L));
145: assertEquals(1234L, MillisDurationField.INSTANCE
146: .getMillis(1234L));
147: assertEquals(-1234L, MillisDurationField.INSTANCE
148: .getMillis(-1234L));
149: }
150:
151: public void test_getMillis_int_long() {
152: assertEquals(0, MillisDurationField.INSTANCE.getMillis(0, 567L));
153: assertEquals(1234, MillisDurationField.INSTANCE.getMillis(1234,
154: 567L));
155: assertEquals(-1234, MillisDurationField.INSTANCE.getMillis(
156: -1234, 567L));
157: }
158:
159: public void test_getMillis_long_long() {
160: assertEquals(0L, MillisDurationField.INSTANCE.getMillis(0L,
161: 567L));
162: assertEquals(1234L, MillisDurationField.INSTANCE.getMillis(
163: 1234L, 567L));
164: assertEquals(-1234L, MillisDurationField.INSTANCE.getMillis(
165: -1234L, 567L));
166: }
167:
168: //-----------------------------------------------------------------------
169: public void test_add_long_int() {
170: assertEquals(567L, MillisDurationField.INSTANCE.add(567L, 0));
171: assertEquals(567L + 1234L, MillisDurationField.INSTANCE.add(
172: 567L, 1234));
173: assertEquals(567L - 1234L, MillisDurationField.INSTANCE.add(
174: 567L, -1234));
175: try {
176: MillisDurationField.INSTANCE.add(Long.MAX_VALUE, 1);
177: fail();
178: } catch (ArithmeticException ex) {
179: }
180: }
181:
182: public void test_add_long_long() {
183: assertEquals(567L, MillisDurationField.INSTANCE.add(567L, 0L));
184: assertEquals(567L + 1234L, MillisDurationField.INSTANCE.add(
185: 567L, 1234L));
186: assertEquals(567L - 1234L, MillisDurationField.INSTANCE.add(
187: 567L, -1234L));
188: try {
189: MillisDurationField.INSTANCE.add(Long.MAX_VALUE, 1L);
190: fail();
191: } catch (ArithmeticException ex) {
192: }
193: }
194:
195: //-----------------------------------------------------------------------
196: public void test_getDifference_long_int() {
197: assertEquals(567, MillisDurationField.INSTANCE.getDifference(
198: 567L, 0L));
199: assertEquals(567 - 1234, MillisDurationField.INSTANCE
200: .getDifference(567L, 1234L));
201: assertEquals(567 + 1234, MillisDurationField.INSTANCE
202: .getDifference(567L, -1234L));
203: try {
204: MillisDurationField.INSTANCE.getDifference(Long.MAX_VALUE,
205: 1L);
206: fail();
207: } catch (ArithmeticException ex) {
208: }
209: }
210:
211: public void test_getDifferenceAsLong_long_long() {
212: assertEquals(567L, MillisDurationField.INSTANCE
213: .getDifferenceAsLong(567L, 0L));
214: assertEquals(567L - 1234L, MillisDurationField.INSTANCE
215: .getDifferenceAsLong(567L, 1234L));
216: assertEquals(567L + 1234L, MillisDurationField.INSTANCE
217: .getDifferenceAsLong(567L, -1234L));
218: try {
219: MillisDurationField.INSTANCE.getDifferenceAsLong(
220: Long.MAX_VALUE, -1L);
221: fail();
222: } catch (ArithmeticException ex) {
223: }
224: }
225:
226: //-----------------------------------------------------------------------
227: public void test_compareTo() {
228: assertEquals(0, MillisDurationField.INSTANCE
229: .compareTo(MillisDurationField.INSTANCE));
230: assertEquals(-1, MillisDurationField.INSTANCE
231: .compareTo(ISOChronology.getInstance().seconds()));
232: DurationField dummy = new PreciseDurationField(
233: DurationFieldType.seconds(), 0);
234: assertEquals(1, MillisDurationField.INSTANCE.compareTo(dummy));
235: try {
236: MillisDurationField.INSTANCE.compareTo("");
237: fail();
238: } catch (ClassCastException ex) {
239: }
240: try {
241: MillisDurationField.INSTANCE.compareTo(null);
242: fail();
243: } catch (NullPointerException ex) {
244: }
245: }
246:
247: //-----------------------------------------------------------------------
248: public void testSerialization() throws Exception {
249: DurationField test = MillisDurationField.INSTANCE;
250:
251: ByteArrayOutputStream baos = new ByteArrayOutputStream();
252: ObjectOutputStream oos = new ObjectOutputStream(baos);
253: oos.writeObject(test);
254: byte[] bytes = baos.toByteArray();
255: oos.close();
256:
257: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
258: ObjectInputStream ois = new ObjectInputStream(bais);
259: DurationField result = (DurationField) ois.readObject();
260: ois.close();
261:
262: assertSame(test, result);
263: }
264:
265: }
|