001    /*
002     *  Copyright 2001-2006 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;
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    /**
027     * This class is a Junit unit test for Seconds.
028     *
029     * @author Stephen Colebourne
030     */
031    public class TestSeconds extends TestCase {
032        // Test in 2002/03 as time zones are more well known
033        // (before the late 90's they were all over the place)
034        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
035    
036        public static void main(String[] args) {
037            junit.textui.TestRunner.run(suite());
038        }
039    
040        public static TestSuite suite() {
041            return new TestSuite(TestSeconds.class);
042        }
043    
044        public TestSeconds(String name) {
045            super(name);
046        }
047    
048        protected void setUp() throws Exception {
049        }
050    
051        protected void tearDown() throws Exception {
052        }
053    
054        //-----------------------------------------------------------------------
055        public void testConstants() {
056            assertEquals(0, Seconds.ZERO.getSeconds());
057            assertEquals(1, Seconds.ONE.getSeconds());
058            assertEquals(2, Seconds.TWO.getSeconds());
059            assertEquals(3, Seconds.THREE.getSeconds());
060            assertEquals(Integer.MAX_VALUE, Seconds.MAX_VALUE.getSeconds());
061            assertEquals(Integer.MIN_VALUE, Seconds.MIN_VALUE.getSeconds());
062        }
063    
064        //-----------------------------------------------------------------------
065        public void testFactory_seconds_int() {
066            assertSame(Seconds.ZERO, Seconds.seconds(0));
067            assertSame(Seconds.ONE, Seconds.seconds(1));
068            assertSame(Seconds.TWO, Seconds.seconds(2));
069            assertSame(Seconds.THREE, Seconds.seconds(3));
070            assertSame(Seconds.MAX_VALUE, Seconds.seconds(Integer.MAX_VALUE));
071            assertSame(Seconds.MIN_VALUE, Seconds.seconds(Integer.MIN_VALUE));
072            assertEquals(-1, Seconds.seconds(-1).getSeconds());
073            assertEquals(4, Seconds.seconds(4).getSeconds());
074        }
075    
076        //-----------------------------------------------------------------------
077        public void testFactory_secondsBetween_RInstant() {
078            DateTime start = new DateTime(2006, 6, 9, 12, 0, 3, 0, PARIS);
079            DateTime end1 = new DateTime(2006, 6, 9, 12, 0, 6, 0, PARIS);
080            DateTime end2 = new DateTime(2006, 6, 9, 12, 0, 9, 0, PARIS);
081            
082            assertEquals(3, Seconds.secondsBetween(start, end1).getSeconds());
083            assertEquals(0, Seconds.secondsBetween(start, start).getSeconds());
084            assertEquals(0, Seconds.secondsBetween(end1, end1).getSeconds());
085            assertEquals(-3, Seconds.secondsBetween(end1, start).getSeconds());
086            assertEquals(6, Seconds.secondsBetween(start, end2).getSeconds());
087        }
088    
089        public void testFactory_secondsBetween_RPartial() {
090            LocalTime start = new LocalTime(12, 0, 3);
091            LocalTime end1 = new LocalTime(12, 0, 6);
092            TimeOfDay end2 = new TimeOfDay(12, 0, 9);
093            
094            assertEquals(3, Seconds.secondsBetween(start, end1).getSeconds());
095            assertEquals(0, Seconds.secondsBetween(start, start).getSeconds());
096            assertEquals(0, Seconds.secondsBetween(end1, end1).getSeconds());
097            assertEquals(-3, Seconds.secondsBetween(end1, start).getSeconds());
098            assertEquals(6, Seconds.secondsBetween(start, end2).getSeconds());
099        }
100    
101        public void testFactory_secondsIn_RInterval() {
102            DateTime start = new DateTime(2006, 6, 9, 12, 0, 3, 0, PARIS);
103            DateTime end1 = new DateTime(2006, 6, 9, 12, 0, 6, 0, PARIS);
104            DateTime end2 = new DateTime(2006, 6, 9, 12, 0, 9, 0, PARIS);
105            
106            assertEquals(0, Seconds.secondsIn((ReadableInterval) null).getSeconds());
107            assertEquals(3, Seconds.secondsIn(new Interval(start, end1)).getSeconds());
108            assertEquals(0, Seconds.secondsIn(new Interval(start, start)).getSeconds());
109            assertEquals(0, Seconds.secondsIn(new Interval(end1, end1)).getSeconds());
110            assertEquals(6, Seconds.secondsIn(new Interval(start, end2)).getSeconds());
111        }
112    
113        public void testFactory_standardSecondsIn_RPeriod() {
114            assertEquals(0, Seconds.standardSecondsIn((ReadablePeriod) null).getSeconds());
115            assertEquals(0, Seconds.standardSecondsIn(Period.ZERO).getSeconds());
116            assertEquals(1, Seconds.standardSecondsIn(new Period(0, 0, 0, 0, 0, 0, 1, 0)).getSeconds());
117            assertEquals(123, Seconds.standardSecondsIn(Period.seconds(123)).getSeconds());
118            assertEquals(-987, Seconds.standardSecondsIn(Period.seconds(-987)).getSeconds());
119            assertEquals(2 * 24 * 60 * 60, Seconds.standardSecondsIn(Period.days(2)).getSeconds());
120            try {
121                Seconds.standardSecondsIn(Period.months(1));
122                fail();
123            } catch (IllegalArgumentException ex) {
124                // expeceted
125            }
126        }
127    
128        public void testFactory_parseSeconds_String() {
129            assertEquals(0, Seconds.parseSeconds((String) null).getSeconds());
130            assertEquals(0, Seconds.parseSeconds("PT0S").getSeconds());
131            assertEquals(1, Seconds.parseSeconds("PT1S").getSeconds());
132            assertEquals(-3, Seconds.parseSeconds("PT-3S").getSeconds());
133            assertEquals(2, Seconds.parseSeconds("P0Y0M0DT2S").getSeconds());
134            assertEquals(2, Seconds.parseSeconds("PT0H2S").getSeconds());
135            try {
136                Seconds.parseSeconds("P1Y1D");
137                fail();
138            } catch (IllegalArgumentException ex) {
139                // expeceted
140            }
141            try {
142                Seconds.parseSeconds("P1DT1S");
143                fail();
144            } catch (IllegalArgumentException ex) {
145                // expeceted
146            }
147        }
148    
149        //-----------------------------------------------------------------------
150        public void testGetMethods() {
151            Seconds test = Seconds.seconds(20);
152            assertEquals(20, test.getSeconds());
153        }
154    
155        public void testGetFieldType() {
156            Seconds test = Seconds.seconds(20);
157            assertEquals(DurationFieldType.seconds(), test.getFieldType());
158        }
159    
160        public void testGetPeriodType() {
161            Seconds test = Seconds.seconds(20);
162            assertEquals(PeriodType.seconds(), test.getPeriodType());
163        }
164    
165        //-----------------------------------------------------------------------
166        public void testIsGreaterThan() {
167            assertEquals(true, Seconds.THREE.isGreaterThan(Seconds.TWO));
168            assertEquals(false, Seconds.THREE.isGreaterThan(Seconds.THREE));
169            assertEquals(false, Seconds.TWO.isGreaterThan(Seconds.THREE));
170            assertEquals(true, Seconds.ONE.isGreaterThan(null));
171            assertEquals(false, Seconds.seconds(-1).isGreaterThan(null));
172        }
173    
174        public void testIsLessThan() {
175            assertEquals(false, Seconds.THREE.isLessThan(Seconds.TWO));
176            assertEquals(false, Seconds.THREE.isLessThan(Seconds.THREE));
177            assertEquals(true, Seconds.TWO.isLessThan(Seconds.THREE));
178            assertEquals(false, Seconds.ONE.isLessThan(null));
179            assertEquals(true, Seconds.seconds(-1).isLessThan(null));
180        }
181    
182        //-----------------------------------------------------------------------
183        public void testToString() {
184            Seconds test = Seconds.seconds(20);
185            assertEquals("PT20S", test.toString());
186            
187            test = Seconds.seconds(-20);
188            assertEquals("PT-20S", test.toString());
189        }
190    
191        //-----------------------------------------------------------------------
192        public void testSerialization() throws Exception {
193            Seconds test = Seconds.THREE;
194            
195            ByteArrayOutputStream baos = new ByteArrayOutputStream();
196            ObjectOutputStream oos = new ObjectOutputStream(baos);
197            oos.writeObject(test);
198            byte[] bytes = baos.toByteArray();
199            oos.close();
200            
201            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
202            ObjectInputStream ois = new ObjectInputStream(bais);
203            Seconds result = (Seconds) ois.readObject();
204            ois.close();
205            
206            assertSame(test, result);
207        }
208    
209        //-----------------------------------------------------------------------
210        public void testToStandardWeeks() {
211            Seconds test = Seconds.seconds(60 * 60 * 24 * 7 * 2);
212            Weeks expected = Weeks.weeks(2);
213            assertEquals(expected, test.toStandardWeeks());
214        }
215    
216        public void testToStandardDays() {
217            Seconds test = Seconds.seconds(60 * 60 * 24 * 2);
218            Days expected = Days.days(2);
219            assertEquals(expected, test.toStandardDays());
220        }
221    
222        public void testToStandardHours() {
223            Seconds test = Seconds.seconds(60 * 60 * 2);
224            Hours expected = Hours.hours(2);
225            assertEquals(expected, test.toStandardHours());
226        }
227    
228        public void testToStandardMinutes() {
229            Seconds test = Seconds.seconds(60 * 2);
230            Minutes expected = Minutes.minutes(2);
231            assertEquals(expected, test.toStandardMinutes());
232        }
233    
234        public void testToStandardDuration() {
235            Seconds test = Seconds.seconds(20);
236            Duration expected = new Duration(20L * DateTimeConstants.MILLIS_PER_SECOND);
237            assertEquals(expected, test.toStandardDuration());
238            
239            expected = new Duration(((long) Integer.MAX_VALUE) * DateTimeConstants.MILLIS_PER_SECOND);
240            assertEquals(expected, Seconds.MAX_VALUE.toStandardDuration());
241        }
242    
243        //-----------------------------------------------------------------------
244        public void testPlus_int() {
245            Seconds test2 = Seconds.seconds(2);
246            Seconds result = test2.plus(3);
247            assertEquals(2, test2.getSeconds());
248            assertEquals(5, result.getSeconds());
249            
250            assertEquals(1, Seconds.ONE.plus(0).getSeconds());
251            
252            try {
253                Seconds.MAX_VALUE.plus(1);
254                fail();
255            } catch (ArithmeticException ex) {
256                // expected
257            }
258        }
259    
260        public void testPlus_Seconds() {
261            Seconds test2 = Seconds.seconds(2);
262            Seconds test3 = Seconds.seconds(3);
263            Seconds result = test2.plus(test3);
264            assertEquals(2, test2.getSeconds());
265            assertEquals(3, test3.getSeconds());
266            assertEquals(5, result.getSeconds());
267            
268            assertEquals(1, Seconds.ONE.plus(Seconds.ZERO).getSeconds());
269            assertEquals(1, Seconds.ONE.plus((Seconds) null).getSeconds());
270            
271            try {
272                Seconds.MAX_VALUE.plus(Seconds.ONE);
273                fail();
274            } catch (ArithmeticException ex) {
275                // expected
276            }
277        }
278    
279        public void testMinus_int() {
280            Seconds test2 = Seconds.seconds(2);
281            Seconds result = test2.minus(3);
282            assertEquals(2, test2.getSeconds());
283            assertEquals(-1, result.getSeconds());
284            
285            assertEquals(1, Seconds.ONE.minus(0).getSeconds());
286            
287            try {
288                Seconds.MIN_VALUE.minus(1);
289                fail();
290            } catch (ArithmeticException ex) {
291                // expected
292            }
293        }
294    
295        public void testMinus_Seconds() {
296            Seconds test2 = Seconds.seconds(2);
297            Seconds test3 = Seconds.seconds(3);
298            Seconds result = test2.minus(test3);
299            assertEquals(2, test2.getSeconds());
300            assertEquals(3, test3.getSeconds());
301            assertEquals(-1, result.getSeconds());
302            
303            assertEquals(1, Seconds.ONE.minus(Seconds.ZERO).getSeconds());
304            assertEquals(1, Seconds.ONE.minus((Seconds) null).getSeconds());
305            
306            try {
307                Seconds.MIN_VALUE.minus(Seconds.ONE);
308                fail();
309            } catch (ArithmeticException ex) {
310                // expected
311            }
312        }
313    
314        public void testMultipliedBy_int() {
315            Seconds test = Seconds.seconds(2);
316            assertEquals(6, test.multipliedBy(3).getSeconds());
317            assertEquals(2, test.getSeconds());
318            assertEquals(-6, test.multipliedBy(-3).getSeconds());
319            assertSame(test, test.multipliedBy(1));
320            
321            Seconds halfMax = Seconds.seconds(Integer.MAX_VALUE / 2 + 1);
322            try {
323                halfMax.multipliedBy(2);
324                fail();
325            } catch (ArithmeticException ex) {
326                // expected
327            }
328        }
329    
330        public void testDividedBy_int() {
331            Seconds test = Seconds.seconds(12);
332            assertEquals(6, test.dividedBy(2).getSeconds());
333            assertEquals(12, test.getSeconds());
334            assertEquals(4, test.dividedBy(3).getSeconds());
335            assertEquals(3, test.dividedBy(4).getSeconds());
336            assertEquals(2, test.dividedBy(5).getSeconds());
337            assertEquals(2, test.dividedBy(6).getSeconds());
338            assertSame(test, test.dividedBy(1));
339            
340            try {
341                Seconds.ONE.dividedBy(0);
342                fail();
343            } catch (ArithmeticException ex) {
344                // expected
345            }
346        }
347    
348        public void testNegated() {
349            Seconds test = Seconds.seconds(12);
350            assertEquals(-12, test.negated().getSeconds());
351            assertEquals(12, test.getSeconds());
352            
353            try {
354                Seconds.MIN_VALUE.negated();
355                fail();
356            } catch (ArithmeticException ex) {
357                // expected
358            }
359        }
360    
361        //-----------------------------------------------------------------------
362        public void testAddToLocalDate() {
363            Seconds test = Seconds.seconds(26);
364            LocalDateTime date = new LocalDateTime(2006, 6, 1, 0, 0, 0, 0);
365            LocalDateTime expected = new LocalDateTime(2006, 6, 1, 0, 0, 26, 0);
366            assertEquals(expected, date.plus(test));
367        }
368    
369    }