1 /* ==================================================================== 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2003 Digital Clash LLC. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * ChronicJ team (http://www.chronicj.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "ChronicJ" and "Digital Clash" not be used to endorse or 27 * promote products derived from this software without prior written 28 * permission. For written permission, please contact 29 * info@digitalclash.com. 30 * 31 * 5. Products derived from this software may not be called "ChronicJ", 32 * "Digital Clash", nor may "ChronicJ" or "Digital Clash" appear in 33 * their name, without prior written permission of Digital Clash LLC. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * This product includes software developed by the by the 49 * Apache Software Foundation (http://www.apache.org/). 50 * ==================================================================== 51 */ 52 package org.chronicj; 53 54 import org.chronicj.DatePrecision; 55 import org.chronicj.TimePoint; 56 57 import java.util.Calendar; 58 import java.util.Date; 59 import java.util.GregorianCalendar; 60 61 62 /*** 63 * Take a wild guess... 64 * 65 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a> 66 */ 67 public class TimePointTest extends BaseTestCase { 68 /*** 69 * Constructor for TimePointTest. 70 * 71 * @param testName the name of this test 72 */ 73 public TimePointTest(String testName) { 74 super(testName); 75 } 76 77 public void testAddDays() { 78 TimePoint aTimePoint = new TimePoint(new GregorianCalendar(2003, 7, 25)); 79 GregorianCalendar result = aTimePoint.addDays(5).getCalendar(); 80 assertNotNull(result); 81 assertEquals(30, result.get(Calendar.DATE)); 82 } 83 84 public void testAfter() { 85 TimePoint earlier = new TimePoint(new GregorianCalendar(2003, 6, 25, 0, 86 0)); 87 TimePoint later = new TimePoint(2003, 7, 25, 0, 1); 88 assertTrue(later.after(earlier)); 89 } 90 91 public void testBefore() { 92 TimePoint earliest = new TimePoint(new GregorianCalendar(2003, 6, 24, 93 23, 59, 59)); 94 TimePoint early = new TimePoint(new GregorianCalendar(2003, 6, 25, 2, 20), 95 DatePrecision.HOUR_OF_DAY); 96 assertTrue(earliest.before(early)); 97 98 TimePoint later = new TimePoint(2003, 7, 25, 2, 12); 99 assertTrue(early.before(later)); 100 } 101 102 public void testCompareTo() { 103 TimePoint earliest = new TimePoint(new GregorianCalendar(2003, 6, 24, 104 23, 59)); 105 TimePoint early = new TimePoint(new GregorianCalendar(2003, 6, 25, 2, 20), 106 DatePrecision.HOUR_OF_DAY); 107 assertTrue(earliest.compareTo(early) == -1); 108 109 TimePoint later = new TimePoint(2003, 7, 25, 3, 12); 110 assertTrue(later.compareTo(early) == 1); 111 assertTrue(later.compareTo(earliest) == 1); 112 assertTrue(later.compareTo(later) == 0); 113 } 114 115 public void testDayPrecision() { 116 GregorianCalendar aCalendar = new GregorianCalendar(2003, 11, 1, 0, 0, 0); 117 GregorianCalendar anotherCalendar = new GregorianCalendar(2003, 11, 1, 118 23, 59, 59); 119 TimePoint aTimePoint = new TimePoint(aCalendar, DatePrecision.DATE); 120 TimePoint anotherTimePoint = new TimePoint(anotherCalendar, 121 DatePrecision.DATE); 122 assertEquals(aTimePoint, anotherTimePoint); 123 } 124 125 public void testDifferentConstructors() { 126 TimePoint usingCalCtor = new TimePoint(new GregorianCalendar(2003, 11, 127 24, 10, 22), DatePrecision.HOUR_OF_DAY); 128 TimePoint usingIntCtor = new TimePoint(2003, 12, 24, 10, 0); 129 assertEquals(usingCalCtor, usingIntCtor); 130 131 GregorianCalendar aCalendar = new GregorianCalendar(2003, 11, 0); 132 Date aDate = aCalendar.getTime(); 133 assertEquals(aDate, aCalendar.getTime()); 134 135 TimePoint usingCalendarCtor = new TimePoint(aCalendar, 136 DatePrecision.MONTH); 137 TimePoint usingDateCtor = new TimePoint(aDate, DatePrecision.MONTH); 138 assertEquals(usingCalendarCtor, usingDateCtor); 139 140 TimePoint usingMonthCalCtor = new TimePoint(new GregorianCalendar( 141 2001, 11, 24), DatePrecision.MONTH); 142 TimePoint usingMonthCtor = new TimePoint(2001, 12); 143 assertEquals(usingMonthCalCtor, usingMonthCtor); 144 } 145 146 public void testEqualsObject() { 147 TimePoint usingCalCtor = new TimePoint(new GregorianCalendar(2003, 11, 148 24, 23, 59)); 149 TimePoint usingIntCtor = new TimePoint(2003, 12, 24, 23, 59); 150 assertEquals(usingCalCtor, usingCalCtor); 151 assertEquals(usingIntCtor, usingIntCtor); 152 assertEquals(usingCalCtor, usingIntCtor); 153 } 154 155 public void testGetCalendar() { 156 TimePoint aTimePoint = new TimePoint(new GregorianCalendar(1968, 11, 157 12, 12, 12)); 158 assertNotNull(aTimePoint.getCalendar()); 159 assertTrue((aTimePoint.getCalendar() instanceof GregorianCalendar)); 160 } 161 162 public void testGetTime() { 163 TimePoint aTimePoint = new TimePoint(new GregorianCalendar(2010, 8, 28, 164 6, 48)); 165 assertNotNull(aTimePoint.getTime()); 166 assertTrue((aTimePoint.getTime() instanceof Date)); 167 } 168 169 public void testHourPrecision() { 170 GregorianCalendar aCalendar = new GregorianCalendar(2003, 11, 10, 10, 171 10, 10); 172 GregorianCalendar anotherCalendar = new GregorianCalendar(2003, 11, 10, 173 10, 10, 11); 174 TimePoint aTimePoint = new TimePoint(aCalendar, 175 DatePrecision.HOUR_OF_DAY); 176 TimePoint anotherTimePoint = new TimePoint(anotherCalendar, 177 DatePrecision.HOUR_OF_DAY); 178 assertEquals(aTimePoint, anotherTimePoint); 179 } 180 181 public void testMonthPrecision() { 182 //Wed Dec 10 00:00:00 EST 2003 183 GregorianCalendar aCalendar = new GregorianCalendar(2003, 11, 10); 184 185 aCalendar.setLenient(false); 186 187 //Sun Dec 14 10:10:11 EST 2003 188 GregorianCalendar anotherCalendar = new GregorianCalendar(2003, 11, 14, 189 10, 10, 11); 190 191 anotherCalendar.setLenient(false); 192 193 //Sun, 30 Nov 2003 12:00:00.0 AM -0500 194 TimePoint aTimePoint = new TimePoint(aCalendar, DatePrecision.MONTH); 195 196 //Sun, 30 Nov 2003 12:00:00.0 AM -0500 197 TimePoint anotherTimePoint = new TimePoint(anotherCalendar, 198 DatePrecision.MONTH); 199 200 assertEquals(aTimePoint, anotherTimePoint); 201 202 //Normalize 203 clearToPrecision(aCalendar, Calendar.DAY_OF_MONTH); 204 205 //Sun Nov 30 00:00:00 EST 2003 206 Date normalizedDate = aCalendar.getTime(); 207 208 //should now be equal 209 assertEquals(normalizedDate, aTimePoint.getTime()); 210 assertEquals(aCalendar, aTimePoint.getCalendar()); 211 212 TimePoint aThirdTimePoint = new TimePoint(aCalendar, DatePrecision.MONTH); 213 214 assertEquals(normalizedDate, aThirdTimePoint.getTime()); 215 } 216 217 public void testMinusDays() { 218 TimePoint aTimePoint = new TimePoint(new GregorianCalendar(2003, 7, 25)); 219 GregorianCalendar result = aTimePoint.minusDays(5).getCalendar(); 220 assertNotNull(result); 221 assertEquals(20, result.get(Calendar.DATE)); 222 } 223 224 public void testMinutePrecision() { 225 GregorianCalendar aCalendar = new GregorianCalendar(); 226 aCalendar.set(Calendar.MILLISECOND, 244); 227 aCalendar.set(Calendar.SECOND, 10); 228 aCalendar.set(Calendar.MINUTE, 12); 229 230 GregorianCalendar anotherCalendar = new GregorianCalendar(); 231 anotherCalendar.set(Calendar.MILLISECOND, 500); 232 anotherCalendar.set(Calendar.SECOND, 30); 233 anotherCalendar.set(Calendar.MINUTE, 12); 234 235 TimePoint aTimePoint = new TimePoint(aCalendar, DatePrecision.MINUTE); 236 TimePoint anotherTimePoint = new TimePoint(anotherCalendar, 237 DatePrecision.MINUTE); 238 assertEquals(aTimePoint, anotherTimePoint); 239 } 240 241 public void testSecondPrecision() { 242 GregorianCalendar aCalendar = new GregorianCalendar(); 243 aCalendar.set(Calendar.MILLISECOND, 244); 244 aCalendar.set(Calendar.SECOND, 10); 245 aCalendar.set(Calendar.MINUTE, 12); 246 247 GregorianCalendar anotherCalendar = new GregorianCalendar(); 248 anotherCalendar.set(Calendar.MILLISECOND, 500); 249 anotherCalendar.set(Calendar.SECOND, 10); 250 anotherCalendar.set(Calendar.MINUTE, 12); 251 252 TimePoint aTimePoint = new TimePoint(aCalendar, DatePrecision.SECOND); 253 TimePoint anotherTimePoint = new TimePoint(anotherCalendar, 254 DatePrecision.SECOND); 255 assertEquals(aTimePoint, anotherTimePoint); 256 } 257 258 public void testYearPrecision() { 259 GregorianCalendar aCalendar = new GregorianCalendar(2003, 11, 10, 10, 260 10, 10); 261 GregorianCalendar anotherCalendar = new GregorianCalendar(2003, 0, 12, 262 14, 12, 11); 263 TimePoint aTimePoint = new TimePoint(aCalendar, DatePrecision.YEAR); 264 TimePoint anotherTimePoint = new TimePoint(anotherCalendar, 265 DatePrecision.YEAR); 266 assertEquals(aTimePoint, anotherTimePoint); 267 } 268 269 public void testIncrementByMinutePrecision() { 270 //December 10th, 2003, 11:04:10am 271 Calendar aCalendar = new GregorianCalendar(2003, 12, 10, 11, 04, 10); 272 Date testDate = aCalendar.getTime(); 273 274 TimePoint minutePrecision = new TimePoint(testDate, DatePrecision.MINUTE); 275 276 clearToPrecision(aCalendar, Calendar.SECOND); 277 aCalendar.add(Calendar.MINUTE, 2); 278 279 //December 10th, 2003, 11:06:10am 280 Date expectedMinutePrecsion = aCalendar.getTime(); 281 282 TimePoint result = minutePrecision.increment(2); 283 284 assertEquals(result.getTime(), expectedMinutePrecsion); 285 } 286 287 public void testIncrementByHourPrecision() { 288 //December 11th, 2003, 11:59:20pm 289 Calendar aCalendar = new GregorianCalendar(2003, 11, 11, 23, 59, 20); 290 Date testDate = aCalendar.getTime(); 291 292 TimePoint hourPrecision = new TimePoint(testDate, 293 DatePrecision.HOUR_OF_DAY); 294 295 clearToPrecision(aCalendar, Calendar.MINUTE); 296 297 aCalendar.add(Calendar.HOUR, 3); 298 299 //December 12th, 2003, 2:00:00am 300 Date expectedHourPrecsion = aCalendar.getTime(); 301 302 TimePoint result = hourPrecision.increment(3); 303 304 assertEquals(result.getTime(), expectedHourPrecsion); 305 } 306 307 public void testIncrementByDatePrecision() { 308 //December 12th, 2003, 11:59:20pm 309 Calendar aCalendar = new GregorianCalendar(2003, 11, 12, 23, 59, 20); 310 Date testDate = aCalendar.getTime(); 311 312 TimePoint dayPrecision = new TimePoint(testDate, DatePrecision.DATE); 313 314 aCalendar.set(Calendar.MILLISECOND, 0); 315 aCalendar.set(Calendar.SECOND, 0); 316 aCalendar.set(Calendar.MINUTE, 0); 317 aCalendar.set(Calendar.HOUR, 0); 318 aCalendar.add(Calendar.DAY_OF_MONTH, 12); 319 320 //December 24th, 2003, 12:00:00am 321 Date expectedDayPrecsion = aCalendar.getTime(); 322 323 TimePoint result = dayPrecision.increment(12); 324 325 assertEquals(result.getTime(), expectedDayPrecsion); 326 } 327 328 public void testIncrementByMonthPrecision() { 329 //August 28th, 2002, 6:06:00am 330 Calendar aCalendar = new GregorianCalendar(2002, 7, 28, 6, 0, 6); 331 332 Date testDate = aCalendar.getTime(); 333 334 TimePoint monthPrecision = new TimePoint(testDate, DatePrecision.MONTH); 335 336 clearToPrecision(aCalendar, Calendar.DAY_OF_MONTH); 337 338 aCalendar.add(Calendar.MONTH, 16); 339 340 //December 1st, 2003, 12:00:00am 341 Date expectedMonthPrecsion = aCalendar.getTime(); 342 343 TimePoint result = monthPrecision.increment(16); 344 345 assertEquals(result.getTime(), expectedMonthPrecsion); 346 } 347 348 public void testIncrementByYearPrecision() { 349 //May 8th, 1969, 11:01:00am 350 Calendar aCalendar = new GregorianCalendar(1969, 4, 8, 11, 1); 351 352 Date testDate = aCalendar.getTime(); 353 354 TimePoint yearPrecision = new TimePoint(testDate, DatePrecision.YEAR); 355 356 clearToPrecision(aCalendar, Calendar.YEAR); 357 358 aCalendar.add(Calendar.YEAR, 34); 359 360 //January 1st, 2003, 12:00:00am 361 Date expectedYearPrecsion = aCalendar.getTime(); 362 363 TimePoint result = yearPrecision.increment(34); 364 365 assertEquals(result.getTime(), expectedYearPrecsion); 366 } 367 368 public void testIncrementBySecondPrecision() { 369 //December 10th, 2003, 11:04:10am 370 Calendar aCalendar = new GregorianCalendar(2003, 12, 10, 11, 04, 10); 371 Date testDate = aCalendar.getTime(); 372 373 TimePoint secondPrecision = new TimePoint(testDate, DatePrecision.SECOND); 374 375 clearToPrecision(aCalendar, Calendar.MILLISECOND); 376 aCalendar.add(Calendar.SECOND, 2); 377 378 //December 10th, 2003, 11:04:12am 379 Date expectedSecondPrecsion = aCalendar.getTime(); 380 381 TimePoint result = secondPrecision.increment(2); 382 383 assertEquals(result.getTime(), expectedSecondPrecsion); 384 } 385 }

This page was automatically generated by Maven