1 42 43 package org.jfree.chart.axis.junit; 44 45 import java.util.Calendar ; 46 import java.util.Date ; 47 import java.util.Locale ; 48 49 import junit.framework.TestCase; 50 51 import org.jfree.chart.axis.SegmentedTimeline; 52 53 56 public class SegmentedTimelineTests2 extends TestCase { 57 58 61 public SegmentedTimelineTests2() { 62 super(); 63 } 64 65 69 public void test1() { 70 71 Calendar cal = Calendar.getInstance(Locale.UK); 72 cal.set(Calendar.YEAR, 2004); 73 cal.set(Calendar.MONTH, Calendar.MARCH); 74 cal.set(Calendar.DAY_OF_MONTH, 26); 75 cal.set(Calendar.HOUR_OF_DAY, 9); 76 cal.set(Calendar.MINUTE, 0); 77 cal.set(Calendar.SECOND, 0); 78 cal.set(Calendar.MILLISECOND, 0); 79 Date date = cal.getTime(); 80 81 SegmentedTimeline timeline = getTimeline(); 82 long value = timeline.toTimelineValue(date); 83 long ms = timeline.toMillisecond(value); 84 85 Calendar cal2 = Calendar.getInstance(Locale.UK); 86 cal2.setTime(new Date (ms)); 87 Date reverted = cal2.getTime(); 88 89 assertTrue("test1", value == (900000 * 34) 90 && date.getTime() == reverted.getTime()); 91 92 } 93 94 98 public void test2() { 99 Calendar cal = Calendar.getInstance(Locale.UK); 100 cal.set(Calendar.YEAR, 2004); 101 cal.set(Calendar.MONTH, Calendar.MARCH); 102 cal.set(Calendar.DAY_OF_MONTH, 26); 103 cal.set(Calendar.HOUR_OF_DAY, 9); 104 cal.set(Calendar.MINUTE, 15); 105 cal.set(Calendar.SECOND, 0); 106 cal.set(Calendar.MILLISECOND, 0); 107 Date date = cal.getTime(); 108 109 SegmentedTimeline timeline = getTimeline(); 110 111 long value = timeline.toTimelineValue(date); 112 long ms = timeline.toMillisecond(value); 113 Calendar cal2 = Calendar.getInstance(Locale.UK); 114 cal2.setTime(new Date (ms)); 115 Date reverted = cal2.getTime(); 116 assertTrue( 117 "test2", value == (900000 * 34 + 900000) 118 && date.getTime() == reverted.getTime() 119 ); 120 } 121 122 126 public void test3() { 127 Calendar cal = Calendar.getInstance(Locale.UK); 128 cal.set(Calendar.YEAR, 2004); 129 cal.set(Calendar.MONTH, Calendar.MARCH); 130 cal.set(Calendar.DAY_OF_MONTH, 26); 131 cal.set(Calendar.HOUR_OF_DAY, 9); 132 cal.set(Calendar.MINUTE, 30); 133 cal.set(Calendar.SECOND, 0); 134 cal.set(Calendar.MILLISECOND, 0); 135 Date date = cal.getTime(); 136 SegmentedTimeline timeline = getTimeline(); 137 138 long value = timeline.toTimelineValue(date); 139 long ms = timeline.toMillisecond(value); 140 141 Calendar cal2 = Calendar.getInstance(Locale.UK); 142 cal2.setTime(new Date (ms)); 143 Date reverted = cal2.getTime(); 144 assertTrue( 145 "test2", value == (900000 * 34 + 900000 * 2) 146 && date.getTime() == reverted.getTime() 147 ); 148 } 149 150 155 public void test4() { 156 Calendar cal = Calendar.getInstance(Locale.UK); 157 cal.set(Calendar.YEAR, 2004); 158 cal.set(Calendar.MONTH, Calendar.MARCH); 159 cal.set(Calendar.DAY_OF_MONTH, 26); 160 cal.set(Calendar.HOUR_OF_DAY, 9); 161 cal.set(Calendar.MINUTE, 30); 162 cal.set(Calendar.SECOND, 0); 163 cal.set(Calendar.MILLISECOND, 1); 164 Date date = cal.getTime(); 165 SegmentedTimeline timeline = getTimeline(); 166 167 long value = timeline.toTimelineValue(date); 168 long ms = timeline.toMillisecond(value); 169 Calendar cal2 = Calendar.getInstance(Locale.UK); 170 cal2.setTime(new Date (ms)); 171 Date reverted = cal2.getTime(); 172 assertTrue( 173 "test4", value == (900000 * 34 + 900000 * 2 + 1) 174 && date.getTime() == reverted.getTime() 175 ); 176 } 177 178 184 public void test5() { 185 Calendar cal = Calendar.getInstance(Locale.UK); 186 cal.set(Calendar.YEAR, 2004); 187 cal.set(Calendar.MONTH, Calendar.MARCH); 188 cal.set(Calendar.DAY_OF_MONTH, 25); 189 cal.set(Calendar.HOUR_OF_DAY, 17); 190 cal.set(Calendar.MINUTE, 30); 191 cal.set(Calendar.SECOND, 0); 192 cal.set(Calendar.MILLISECOND, 0); 193 Date date = cal.getTime(); 194 SegmentedTimeline timeline = getTimeline(); 195 196 long value = timeline.toTimelineValue(date); 197 long ms = timeline.toMillisecond(value); 198 Calendar cal2 = Calendar.getInstance(Locale.UK); 199 cal2.setTime(new Date (ms)); 200 Date reverted = cal2.getTime(); 201 Calendar expectedReverted = Calendar.getInstance(Locale.UK); 202 expectedReverted.set(Calendar.YEAR, 2004); 203 expectedReverted.set(Calendar.MONTH, Calendar.MARCH); 204 expectedReverted.set(Calendar.DAY_OF_MONTH, 26); 205 expectedReverted.set(Calendar.HOUR_OF_DAY, 9); 206 expectedReverted.set(Calendar.MINUTE, 0); 207 expectedReverted.set(Calendar.SECOND, 0); 208 expectedReverted.set(Calendar.MILLISECOND, 0); 209 210 assertTrue( 211 "test5", value == (900000 * 34) 212 && expectedReverted.getTime().getTime() == reverted.getTime() 213 ); 214 } 215 216 222 public void test6() { 223 Calendar cal = Calendar.getInstance(Locale.UK); 224 cal.set(Calendar.YEAR, 2004); 225 cal.set(Calendar.MONTH, Calendar.MARCH); 226 cal.set(Calendar.DAY_OF_MONTH, 28); 227 cal.set(Calendar.HOUR_OF_DAY, 9); 228 cal.set(Calendar.MINUTE, 0); 229 cal.set(Calendar.SECOND, 0); 230 cal.set(Calendar.MILLISECOND, 0); 231 Date date = cal.getTime(); 232 SegmentedTimeline timeline = getTimeline(); 233 234 long value = timeline.toTimelineValue(date); 235 long ms = timeline.toMillisecond(value); 236 Calendar cal2 = Calendar.getInstance(Locale.UK); 237 cal2.setTime(new Date (ms)); 238 Date reverted = cal2.getTime(); 239 240 Calendar expectedReverted = Calendar.getInstance(Locale.UK); 241 expectedReverted.set(Calendar.YEAR, 2004); 242 expectedReverted.set(Calendar.MONTH, Calendar.MARCH); 243 expectedReverted.set(Calendar.DAY_OF_MONTH, 29); 244 expectedReverted.set(Calendar.HOUR_OF_DAY, 9); 245 expectedReverted.set(Calendar.MINUTE, 0); 246 expectedReverted.set(Calendar.SECOND, 0); 247 expectedReverted.set(Calendar.MILLISECOND, 0); 248 249 assertTrue( 250 "test6", value == (900000 * 34 * 2) 251 && expectedReverted.getTime().getTime() == reverted.getTime() 252 ); 253 } 254 255 259 public void test7() { 260 Calendar cal = Calendar.getInstance(Locale.UK); 261 cal.set(Calendar.YEAR, 2004); 262 cal.set(Calendar.MONTH, Calendar.MARCH); 263 cal.set(Calendar.DAY_OF_MONTH, 29); 264 cal.set(Calendar.HOUR_OF_DAY, 9); 265 cal.set(Calendar.MINUTE, 0); 266 cal.set(Calendar.SECOND, 0); 267 cal.set(Calendar.MILLISECOND, 0); 268 Date date = cal.getTime(); 269 270 SegmentedTimeline timeline = getTimeline(); 271 long value = timeline.toTimelineValue(date); 272 long ms = timeline.toMillisecond(value); 273 274 Calendar cal2 = Calendar.getInstance(Locale.UK); 275 cal2.setTime(new Date (ms)); 276 Date reverted = cal2.getTime(); 277 278 Calendar expectedReverted = Calendar.getInstance(); 279 expectedReverted.set(Calendar.YEAR, 2004); 280 expectedReverted.set(Calendar.MONTH, Calendar.MARCH); 281 expectedReverted.set(Calendar.DAY_OF_MONTH, 29); 282 expectedReverted.set(Calendar.HOUR_OF_DAY, 9); 283 expectedReverted.set(Calendar.MINUTE, 0); 284 expectedReverted.set(Calendar.SECOND, 0); 285 expectedReverted.set(Calendar.MILLISECOND, 0); 286 287 assertTrue( 288 "test7", value == (900000 * 34 * 2) 289 && expectedReverted.getTime().getTime() == reverted.getTime() 290 ); 291 } 292 293 296 public void test8() { 297 Calendar cal = Calendar.getInstance(Locale.UK); 298 cal.set(Calendar.YEAR, 2004); 299 cal.set(Calendar.MONTH, Calendar.MARCH); 300 cal.set(Calendar.DAY_OF_MONTH, 29); 301 cal.set(Calendar.HOUR_OF_DAY, 10); 302 cal.set(Calendar.MINUTE, 0); 303 cal.set(Calendar.SECOND, 0); 304 cal.set(Calendar.MILLISECOND, 0); 305 Date date = cal.getTime(); 306 307 SegmentedTimeline timeline = getTimeline(); 308 309 cal.set(Calendar.YEAR, 2004); 311 cal.set(Calendar.MONTH, Calendar.MARCH); 312 cal.set(Calendar.DAY_OF_MONTH, 29); 313 cal.set(Calendar.HOUR_OF_DAY, 9); 314 cal.set(Calendar.MINUTE, 15); 315 cal.set(Calendar.SECOND, 0); 316 cal.set(Calendar.MILLISECOND, 0); 317 timeline.addException(cal.getTime()); 318 319 long value = timeline.toTimelineValue(date); 320 long ms = timeline.toMillisecond(value); 321 322 Calendar cal2 = Calendar.getInstance(Locale.UK); 323 cal2.setTime(new Date (ms)); 324 Date reverted = cal2.getTime(); 325 326 Calendar expectedReverted = Calendar.getInstance(); 327 expectedReverted.set(Calendar.YEAR, 2004); 328 expectedReverted.set(Calendar.MONTH, Calendar.MARCH); 329 expectedReverted.set(Calendar.DAY_OF_MONTH, 29); 330 expectedReverted.set(Calendar.HOUR_OF_DAY, 10); 331 expectedReverted.set(Calendar.MINUTE, 0); 332 expectedReverted.set(Calendar.SECOND, 0); 333 expectedReverted.set(Calendar.MILLISECOND, 0); 334 335 assertTrue( 336 "test8", value == (900000 * 34 * 2 + 900000 * (4 - 1)) 337 && expectedReverted.getTime().getTime() == reverted.getTime() 338 ); 339 } 340 341 347 private SegmentedTimeline getTimeline() { 348 Calendar cal = Calendar.getInstance(Locale.UK); 349 cal.set(Calendar.YEAR, 2004); 350 cal.set(Calendar.MONTH, Calendar.MARCH); 351 cal.set(Calendar.DAY_OF_MONTH, 25); 352 cal.set(Calendar.HOUR_OF_DAY, 9); 353 cal.set(Calendar.MINUTE, 0); 354 cal.set(Calendar.SECOND, 0); 355 cal.set(Calendar.MILLISECOND, 0); 356 Date from = cal.getTime(); 357 358 cal = Calendar.getInstance(Locale.UK); 359 cal.set(Calendar.YEAR, 2004); 360 cal.set(Calendar.MONTH, Calendar.MARCH); 361 cal.set(Calendar.DAY_OF_MONTH, 30); 362 cal.set(Calendar.HOUR_OF_DAY, 17); 363 cal.set(Calendar.MINUTE, 30); 364 cal.set(Calendar.SECOND, 0); 365 cal.set(Calendar.MILLISECOND, 0); 366 Date to = cal.getTime(); 367 368 return getTimeline(from, to); 369 } 370 371 380 private SegmentedTimeline getTimeline(Date start, Date end) { 381 382 Calendar cal = Calendar.getInstance(Locale.UK); 383 cal.set(Calendar.YEAR, 1970); 384 cal.set(Calendar.MONTH, Calendar.JANUARY); 385 cal.set(Calendar.DAY_OF_MONTH, 1); 386 cal.set(Calendar.HOUR_OF_DAY, 9); 387 cal.set(Calendar.MINUTE, 0); 388 cal.set(Calendar.SECOND, 0); 389 cal.set(Calendar.MILLISECOND, 0); 390 Date open = cal.getTime(); 391 392 cal = Calendar.getInstance(Locale.UK); 393 cal.set(Calendar.YEAR, 1970); 394 cal.set(Calendar.MONTH, Calendar.JANUARY); 395 cal.set(Calendar.DAY_OF_MONTH, 1); 396 cal.set(Calendar.HOUR_OF_DAY, 17); 397 cal.set(Calendar.MINUTE, 30); 398 cal.set(Calendar.SECOND, 0); 399 cal.set(Calendar.MILLISECOND, 0); 400 Date close = cal.getTime(); 401 402 SegmentedTimeline result = null; 403 long quarterHourCount = (close.getTime() - open.getTime()) 405 / SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE; 406 long totalQuarterHourCount = SegmentedTimeline.DAY_SEGMENT_SIZE 407 / SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE; 408 result = new SegmentedTimeline( 409 SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE, 410 (int) quarterHourCount, 411 (int) (totalQuarterHourCount - quarterHourCount) 412 ); 413 result.setAdjustForDaylightSaving(true); 414 result.setStartTime(start.getTime()); 416 result.setBaseTimeline( 418 SegmentedTimeline.newMondayThroughFridayTimeline() 419 ); 420 421 if (start != null && end != null) { 422 result.addBaseTimelineExclusions(start.getTime(), end.getTime()); 423 } 424 425 return result; 426 } 427 428 429 } 430 | Popular Tags |