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