View Javadoc

1   /*
2    *  Copyright 2001-2009 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 PeriodFormatterBuilder.
32   *
33   * @author Stephen Colebourne
34   */
35  public class TestMillisDurationField extends TestCase {
36  
37      public static void main(String[] args) {
38          junit.textui.TestRunner.run(suite());
39      }
40  
41      public static TestSuite suite() {
42          return new TestSuite(TestMillisDurationField.class);
43      }
44  
45      public TestMillisDurationField(String name) {
46          super(name);
47      }
48  
49      protected void setUp() throws Exception {
50      }
51  
52      protected void tearDown() throws Exception {
53      }
54  
55      //-----------------------------------------------------------------------
56      public void test_getType() {
57          assertEquals(DurationFieldType.millis(), MillisDurationField.INSTANCE.getType());
58      }
59  
60      public void test_getName() {
61          assertEquals("millis", MillisDurationField.INSTANCE.getName());
62      }
63      
64      public void test_isSupported() {
65          assertEquals(true, MillisDurationField.INSTANCE.isSupported());
66      }
67  
68      public void test_isPrecise() {
69          assertEquals(true, MillisDurationField.INSTANCE.isPrecise());
70      }
71  
72      public void test_getUnitMillis() {
73          assertEquals(1, MillisDurationField.INSTANCE.getUnitMillis());
74      }
75  
76      public void test_toString() {
77          assertEquals("DurationField[millis]", MillisDurationField.INSTANCE.toString());
78      }
79      
80      //-----------------------------------------------------------------------
81      public void test_getValue_long() {
82          assertEquals(0, MillisDurationField.INSTANCE.getValue(0L));
83          assertEquals(1234, MillisDurationField.INSTANCE.getValue(1234L));
84          assertEquals(-1234, MillisDurationField.INSTANCE.getValue(-1234L));
85          try {
86              MillisDurationField.INSTANCE.getValue(((long) (Integer.MAX_VALUE)) + 1L);
87              fail();
88          } catch (ArithmeticException ex) {}
89      }
90  
91      public void test_getValueAsLong_long() {
92          assertEquals(0L, MillisDurationField.INSTANCE.getValueAsLong(0L));
93          assertEquals(1234L, MillisDurationField.INSTANCE.getValueAsLong(1234L));
94          assertEquals(-1234L, MillisDurationField.INSTANCE.getValueAsLong(-1234L));
95          assertEquals(((long) (Integer.MAX_VALUE)) + 1L, MillisDurationField.INSTANCE.getValueAsLong(((long) (Integer.MAX_VALUE)) + 1L));
96      }
97  
98      public void test_getValue_long_long() {
99          assertEquals(0, MillisDurationField.INSTANCE.getValue(0L, 567L));
100         assertEquals(1234, MillisDurationField.INSTANCE.getValue(1234L, 567L));
101         assertEquals(-1234, MillisDurationField.INSTANCE.getValue(-1234L, 567L));
102         try {
103             MillisDurationField.INSTANCE.getValue(((long) (Integer.MAX_VALUE)) + 1L, 567L);
104             fail();
105         } catch (ArithmeticException ex) {}
106     }
107 
108     public void test_getValueAsLong_long_long() {
109         assertEquals(0L, MillisDurationField.INSTANCE.getValueAsLong(0L, 567L));
110         assertEquals(1234L, MillisDurationField.INSTANCE.getValueAsLong(1234L, 567L));
111         assertEquals(-1234L, MillisDurationField.INSTANCE.getValueAsLong(-1234L, 567L));
112         assertEquals(((long) (Integer.MAX_VALUE)) + 1L, MillisDurationField.INSTANCE.getValueAsLong(((long) (Integer.MAX_VALUE)) + 1L, 567L));
113     }
114 
115     //-----------------------------------------------------------------------
116     public void test_getMillis_int() {
117         assertEquals(0, MillisDurationField.INSTANCE.getMillis(0));
118         assertEquals(1234, MillisDurationField.INSTANCE.getMillis(1234));
119         assertEquals(-1234, MillisDurationField.INSTANCE.getMillis(-1234));
120     }
121 
122     public void test_getMillis_long() {
123         assertEquals(0L, MillisDurationField.INSTANCE.getMillis(0L));
124         assertEquals(1234L, MillisDurationField.INSTANCE.getMillis(1234L));
125         assertEquals(-1234L, MillisDurationField.INSTANCE.getMillis(-1234L));
126     }
127 
128     public void test_getMillis_int_long() {
129         assertEquals(0, MillisDurationField.INSTANCE.getMillis(0, 567L));
130         assertEquals(1234, MillisDurationField.INSTANCE.getMillis(1234, 567L));
131         assertEquals(-1234, MillisDurationField.INSTANCE.getMillis(-1234, 567L));
132     }
133 
134     public void test_getMillis_long_long() {
135         assertEquals(0L, MillisDurationField.INSTANCE.getMillis(0L, 567L));
136         assertEquals(1234L, MillisDurationField.INSTANCE.getMillis(1234L, 567L));
137         assertEquals(-1234L, MillisDurationField.INSTANCE.getMillis(-1234L, 567L));
138     }
139 
140     //-----------------------------------------------------------------------
141     public void test_add_long_int() {
142         assertEquals(567L, MillisDurationField.INSTANCE.add(567L, 0));
143         assertEquals(567L + 1234L, MillisDurationField.INSTANCE.add(567L, 1234));
144         assertEquals(567L - 1234L, MillisDurationField.INSTANCE.add(567L, -1234));
145         try {
146             MillisDurationField.INSTANCE.add(Long.MAX_VALUE, 1);
147             fail();
148         } catch (ArithmeticException ex) {}
149     }
150 
151     public void test_add_long_long() {
152         assertEquals(567L, MillisDurationField.INSTANCE.add(567L, 0L));
153         assertEquals(567L + 1234L, MillisDurationField.INSTANCE.add(567L, 1234L));
154         assertEquals(567L - 1234L, MillisDurationField.INSTANCE.add(567L, -1234L));
155         try {
156             MillisDurationField.INSTANCE.add(Long.MAX_VALUE, 1L);
157             fail();
158         } catch (ArithmeticException ex) {}
159     }
160 
161     //-----------------------------------------------------------------------
162     public void test_getDifference_long_int() {
163         assertEquals(567, MillisDurationField.INSTANCE.getDifference(567L, 0L));
164         assertEquals(567 - 1234, MillisDurationField.INSTANCE.getDifference(567L, 1234L));
165         assertEquals(567 + 1234, MillisDurationField.INSTANCE.getDifference(567L, -1234L));
166         try {
167             MillisDurationField.INSTANCE.getDifference(Long.MAX_VALUE, 1L);
168             fail();
169         } catch (ArithmeticException ex) {}
170     }
171 
172     public void test_getDifferenceAsLong_long_long() {
173         assertEquals(567L, MillisDurationField.INSTANCE.getDifferenceAsLong(567L, 0L));
174         assertEquals(567L - 1234L, MillisDurationField.INSTANCE.getDifferenceAsLong(567L, 1234L));
175         assertEquals(567L + 1234L, MillisDurationField.INSTANCE.getDifferenceAsLong(567L, -1234L));
176         try {
177             MillisDurationField.INSTANCE.getDifferenceAsLong(Long.MAX_VALUE, -1L);
178             fail();
179         } catch (ArithmeticException ex) {}
180     }
181 
182     //-----------------------------------------------------------------------
183     public void test_compareTo() {
184         assertEquals(0, MillisDurationField.INSTANCE.compareTo(MillisDurationField.INSTANCE));
185         assertEquals(-1, MillisDurationField.INSTANCE.compareTo(ISOChronology.getInstance().seconds()));
186         DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0);
187         assertEquals(1, MillisDurationField.INSTANCE.compareTo(dummy));
188 //        try {
189 //            MillisDurationField.INSTANCE.compareTo("");
190 //            fail();
191 //        } catch (ClassCastException ex) {}
192         try {
193             MillisDurationField.INSTANCE.compareTo(null);
194             fail();
195         } catch (NullPointerException ex) {}
196     }
197 
198     //-----------------------------------------------------------------------
199     public void testSerialization() throws Exception {
200         DurationField test = MillisDurationField.INSTANCE;
201         
202         ByteArrayOutputStream baos = new ByteArrayOutputStream();
203         ObjectOutputStream oos = new ObjectOutputStream(baos);
204         oos.writeObject(test);
205         byte[] bytes = baos.toByteArray();
206         oos.close();
207         
208         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
209         ObjectInputStream ois = new ObjectInputStream(bais);
210         DurationField result = (DurationField) ois.readObject();
211         ois.close();
212         
213         assertSame(test, result);
214     }
215 
216 }