1 16 package org.joda.time; 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 31 public class TestSeconds extends TestCase { 32 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 35 36 public static void main(String [] args) { 37 junit.textui.TestRunner.run(suite()); 38 } 39 40 public static TestSuite suite() { 41 return new TestSuite(TestSeconds.class); 42 } 43 44 public TestSeconds(String name) { 45 super(name); 46 } 47 48 protected void setUp() throws Exception { 49 } 50 51 protected void tearDown() throws Exception { 52 } 53 54 public void testConstants() { 56 assertEquals(0, Seconds.ZERO.getSeconds()); 57 assertEquals(1, Seconds.ONE.getSeconds()); 58 assertEquals(2, Seconds.TWO.getSeconds()); 59 assertEquals(3, Seconds.THREE.getSeconds()); 60 assertEquals(Integer.MAX_VALUE, Seconds.MAX_VALUE.getSeconds()); 61 assertEquals(Integer.MIN_VALUE, Seconds.MIN_VALUE.getSeconds()); 62 } 63 64 public void testFactory_seconds_int() { 66 assertSame(Seconds.ZERO, Seconds.seconds(0)); 67 assertSame(Seconds.ONE, Seconds.seconds(1)); 68 assertSame(Seconds.TWO, Seconds.seconds(2)); 69 assertSame(Seconds.THREE, Seconds.seconds(3)); 70 assertSame(Seconds.MAX_VALUE, Seconds.seconds(Integer.MAX_VALUE)); 71 assertSame(Seconds.MIN_VALUE, Seconds.seconds(Integer.MIN_VALUE)); 72 assertEquals(-1, Seconds.seconds(-1).getSeconds()); 73 assertEquals(4, Seconds.seconds(4).getSeconds()); 74 } 75 76 public void testFactory_secondsBetween_RInstant() { 78 DateTime start = new DateTime(2006, 6, 9, 12, 0, 3, 0, PARIS); 79 DateTime end1 = new DateTime(2006, 6, 9, 12, 0, 6, 0, PARIS); 80 DateTime end2 = new DateTime(2006, 6, 9, 12, 0, 9, 0, PARIS); 81 82 assertEquals(3, Seconds.secondsBetween(start, end1).getSeconds()); 83 assertEquals(0, Seconds.secondsBetween(start, start).getSeconds()); 84 assertEquals(0, Seconds.secondsBetween(end1, end1).getSeconds()); 85 assertEquals(-3, Seconds.secondsBetween(end1, start).getSeconds()); 86 assertEquals(6, Seconds.secondsBetween(start, end2).getSeconds()); 87 } 88 89 public void testFactory_secondsBetween_RPartial() { 90 LocalTime start = new LocalTime(12, 0, 3); 91 LocalTime end1 = new LocalTime(12, 0, 6); 92 TimeOfDay end2 = new TimeOfDay(12, 0, 9); 93 94 assertEquals(3, Seconds.secondsBetween(start, end1).getSeconds()); 95 assertEquals(0, Seconds.secondsBetween(start, start).getSeconds()); 96 assertEquals(0, Seconds.secondsBetween(end1, end1).getSeconds()); 97 assertEquals(-3, Seconds.secondsBetween(end1, start).getSeconds()); 98 assertEquals(6, Seconds.secondsBetween(start, end2).getSeconds()); 99 } 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 } 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 } 141 try { 142 Seconds.parseSeconds("P1DT1S"); 143 fail(); 144 } catch (IllegalArgumentException ex) { 145 } 147 } 148 149 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 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 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 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 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 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 } 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 } 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 } 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 } 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 } 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 } 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 } 359 } 360 361 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 } 370 | Popular Tags |