/** * Tests that a timeline's included and excluded segments are being calculated * correctly. * @param timeline the timeline to verify * @param n the first segment number to start verifying */ public void verifyIncludedAndExcludedSegments(SegmentedTimeline timeline, long n) { // clear any exceptions in this timeline timeline.setExceptionSegments(new java.util.ArrayList()); // test some included and excluded segments SegmentedTimeline.Segment segment = timeline.getSegment(n); for (int i = 0; i < 1000; i++) { int d = (i % timeline.getGroupSegmentCount()); if (d < timeline.getSegmentsIncluded()) { // should be an included segment assertTrue(segment.inIncludeSegments()); assertTrue(!segment.inExcludeSegments()); assertTrue(!segment.inExceptionSegments()); } else { // should be an excluded segment assertTrue(!segment.inIncludeSegments()); assertTrue(segment.inExcludeSegments()); assertTrue(!segment.inExceptionSegments()); } segment.inc(); } }
/** * Tests methods related to adding exceptions. * * @param timeline the timeline to verify * @param exceptionString array of Strings that represent the exceptions * @param fmt Format object that can parse the exceptionString strings * * @throws ParseException if there is a parsing error. */ public void verifyExceptionSegments(SegmentedTimeline timeline, String[] exceptionString, Format fmt) throws ParseException { // fill in the exceptions long[] exception = verifyFillInExceptions(timeline, exceptionString, fmt); int m = exception.length; // verify list of exceptions assertEquals(exception.length, timeline.getExceptionSegments().size()); SegmentedTimeline.Segment lastSegment = timeline.getSegment(exception[m - 1]); for (int i = 0; i < m; i++) { SegmentedTimeline.Segment segment = timeline.getSegment(exception[i]); assertTrue(segment.inExceptionSegments()); // include current exception and last one assertEquals(m - i, timeline.getExceptionSegmentCount( segment.getSegmentStart(), lastSegment.getSegmentEnd())); // exclude current exception and last one assertEquals(Math.max(0, m - i - 2), timeline.getExceptionSegmentCount( exception[i] + 1, exception[m - 1] - 1)); } }
/** * Creates a timeline from 9am on Thursday 25 March 2004 to 5pm on * Tuesday 30 March 2004, for the UK locale. * * @return A timeline. */ private SegmentedTimeline getTimeline() { Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 25); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date from = cal.getTime(); cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 30); cal.set(Calendar.HOUR_OF_DAY, 17); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date to = cal.getTime(); return getTimeline(from, to); }
/** * Tests that a timeline's included and excluded segments are being * calculated correctly. * * @param timeline the timeline to verify * @param n the first segment number to start verifying */ public void verifyIncludedAndExcludedSegments(SegmentedTimeline timeline, long n) { // clear any exceptions in this timeline timeline.setExceptionSegments(new java.util.ArrayList()); // test some included and excluded segments SegmentedTimeline.Segment segment = timeline.getSegment(n); for (int i = 0; i < 1000; i++) { int d = (i % timeline.getGroupSegmentCount()); if (d < timeline.getSegmentsIncluded()) { // should be an included segment assertTrue(segment.inIncludeSegments()); assertTrue(!segment.inExcludeSegments()); assertTrue(!segment.inExceptionSegments()); } else { // should be an excluded segment assertTrue(!segment.inIncludeSegments()); assertTrue(segment.inExcludeSegments()); assertTrue(!segment.inExceptionSegments()); } segment.inc(); } }
/** * Test 1 checks 9am Friday 26 March 2004 converts to a timeline value and * back again correctly. This is prior to Daylight Saving. */ public void test1() { Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); long ms = timeline.toMillisecond(value); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); assertTrue("test1", value == (900000 * 34) && date.getTime() == reverted.getTime()); }
/** * Test 2 checks 9.15am Friday 26 March 2004 converts to a timeline value * and back again correctly. This is prior to Daylight Saving. */ public void test2() { Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 15); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); long ms = timeline.toMillisecond(value); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); assertTrue( "test2", value == (900000 * 34 + 900000) && date.getTime() == reverted.getTime() ); }
/** * Test 3 checks 9.30am Friday 26 March 2004 converts to a timeline value * and back again correctly. This is prior to Daylight Saving. */ public void test3() { Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); long ms = timeline.toMillisecond(value); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); assertTrue( "test2", value == (900000 * 34 + 900000 * 2) && date.getTime() == reverted.getTime() ); }
/** * Test 4 checks 9.30am Friday 26 March 2004 (+ 1 millisecond) converts to * a timeline value and back again correctly. This is prior to Daylight * Saving. */ public void test4() { Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 1); Date date = cal.getTime(); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); long ms = timeline.toMillisecond(value); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); assertTrue( "test4", value == (900000 * 34 + 900000 * 2 + 1) && date.getTime() == reverted.getTime() ); }
/** * Test 6 checks that 9am on Sunday 28 March 2004 converts to the timeline * value and back again correctly. Note that Saturday and Sunday are * excluded from the timeline, so we expect the value to map to 9am on * Monday 29 March 2004. This is during daylight saving. */ public void test6() { Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 28); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); long ms = timeline.toMillisecond(value); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); Calendar expectedReverted = Calendar.getInstance(Locale.UK); expectedReverted.set(Calendar.YEAR, 2004); expectedReverted.set(Calendar.MONTH, Calendar.MARCH); expectedReverted.set(Calendar.DAY_OF_MONTH, 29); expectedReverted.set(Calendar.HOUR_OF_DAY, 9); expectedReverted.set(Calendar.MINUTE, 0); expectedReverted.set(Calendar.SECOND, 0); expectedReverted.set(Calendar.MILLISECOND, 0); assertTrue( "test6", value == (900000 * 34 * 2) && expectedReverted.getTime().getTime() == reverted.getTime() ); }
/** * Test 4 checks 9.30am Friday 26 March 2004 (+ 1 millisecond) converts to * a timeline value and back again correctly. This is prior to Daylight * Saving. */ public void test4() { TimeZone savedZone = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 1); Date date = cal.getTime(); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); long ms = timeline.toMillisecond(value); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); assertTrue( "test4", value == (900000 * 34 + 900000 * 2 + 1) && date.getTime() == reverted.getTime() ); TimeZone.setDefault(savedZone); }
/** * Confirm that the equals method can distinguish all the required fields. */ public void testEquals() { DateAxis a1 = new DateAxis("Test"); DateAxis a2 = new DateAxis("Test"); assertTrue(a1.equals(a2)); // tickUnit a1.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7)); assertFalse(a1.equals(a2)); a2.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7)); assertTrue(a1.equals(a2)); // dateFormatOverride a1.setDateFormatOverride(new SimpleDateFormat("yyyy")); assertFalse(a1.equals(a2)); a2.setDateFormatOverride(new SimpleDateFormat("yyyy")); assertTrue(a1.equals(a2)); // tickMarkPosition a1.setTickMarkPosition(DateTickMarkPosition.END); assertFalse(a1.equals(a2)); a2.setTickMarkPosition(DateTickMarkPosition.END); assertTrue(a1.equals(a2)); // timeline a1.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); assertFalse(a1.equals(a2)); a2.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); assertTrue(a1.equals(a2)); }
/** * Tests that the factory method that creates Monday through Friday segmented * timeline does so correctly. */ public void testMondayThroughFridaySegmentedTimeline() { // verify attributes set during object construction assertEquals( SegmentedTimeline.DAY_SEGMENT_SIZE, this.mondayFridayTimeline.getSegmentSize() ); assertEquals( SegmentedTimeline.FIRST_MONDAY_AFTER_1900, this.mondayFridayTimeline.getStartTime() ); assertEquals(5, this.mondayFridayTimeline.getSegmentsIncluded()); assertEquals(2, this.mondayFridayTimeline.getSegmentsExcluded()); }
/** * Tests that the factory method that creates a 15-min 9:00 AM 4:00 PM * segmented axis does so correctly. */ public void testFifteenMinSegmentedTimeline() { assertEquals(SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE, this.fifteenMinTimeline.getSegmentSize()); assertEquals(SegmentedTimeline.FIRST_MONDAY_AFTER_1900 + 36 * this.fifteenMinTimeline.getSegmentSize(), this.fifteenMinTimeline.getStartTime()); assertEquals(28, this.fifteenMinTimeline.getSegmentsIncluded()); assertEquals(68, this.fifteenMinTimeline.getSegmentsExcluded()); }
/** * Tests translations between timelines. * * @param timeline the timeline to use for verifications. * @param startTest ??. */ public void verifyTranslations(SegmentedTimeline timeline, long startTest) { for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END; testCycle += TEST_CYCLE_INC) { long millisecond = startTest + testCycle * timeline.getSegmentSize(); SegmentedTimeline.Segment segment = timeline.getSegment(millisecond); for (int i = 0; i < 1000; i++) { long translatedValue = timeline.toTimelineValue(segment.getMillisecond()); long newValue = timeline.toMillisecond(translatedValue); if (segment.inExcludeSegments() || segment.inExceptionSegments()) { // the reverse transformed value will be in the start of the // next non-excluded and non-exception segment SegmentedTimeline.Segment tempSegment = segment.copy(); tempSegment.moveIndexToStart(); do { tempSegment.inc(); } while (!tempSegment.inIncludeSegments()); assertEquals(tempSegment.getMillisecond(), newValue); } else { assertEquals(segment.getMillisecond(), newValue); } segment.inc(); } } }
/** * Serialize an instance, restore it, and check for equality. */ public void testSerialization() { verifySerialization(this.msTimeline); verifySerialization(this.ms2Timeline); verifySerialization(this.ms2BaseTimeline); verifySerialization(SegmentedTimeline.newMondayThroughFridayTimeline()); verifySerialization(SegmentedTimeline.newFifteenMinuteTimeline()); }
/** * Adds an array of exceptions relative to the base timeline. * * @param timeline The timeline where the exceptions will be stored * @param exceptionString The exceptions to load * @param fmt The date formatter to use to parse each exceptions[i] value * @throws ParseException If there is any exception parsing each exceptions[i] * value. */ private void fillInBaseTimelineExceptions(SegmentedTimeline timeline, String[] exceptionString, Format fmt) throws ParseException { SegmentedTimeline baseTimeline = timeline.getBaseTimeline(); for (int i = 0; i < exceptionString.length; i++) { long e; if (fmt instanceof NumberFormat) { e = ((NumberFormat) fmt).parse(exceptionString[i]).longValue(); } else { e = timeline.getTime(((SimpleDateFormat) fmt).parse(exceptionString[i])); } timeline.addBaseTimelineException(e); // verify all timeline segments included in the baseTimeline.segment are now exceptions SegmentedTimeline.Segment segment1 = baseTimeline.getSegment(e); for (SegmentedTimeline.Segment segment2 = timeline.getSegment(segment1.getSegmentStart()); segment2.getSegmentStart() <= segment1.getSegmentEnd(); segment2.inc()) { if (!segment2.inExcludeSegments()) { assertTrue(segment2.inExceptionSegments()); } } } }
/** * Adds new exceptions to a timeline. The exceptions are the excluded segments from its * base timeline. * * @param timeline the timeline. * @param from the start. * @param to the end. */ private void fillInBaseTimelineExclusionsAsExceptions(SegmentedTimeline timeline, long from, long to) { // add the base timeline exclusions as timeline's esceptions timeline.addBaseTimelineExclusions(from, to); // validate base timeline exclusions added as timeline's esceptions for (SegmentedTimeline.Segment segment1 = timeline.getBaseTimeline().getSegment(from); segment1.getSegmentStart() <= to; segment1.inc()) { if (segment1.inExcludeSegments()) { // verify all timeline segments included in the baseTimeline.segment are now // exceptions for (SegmentedTimeline.Segment segment2 = timeline.getSegment(segment1.getSegmentStart()); segment2.getSegmentStart() <= segment1.getSegmentEnd(); segment2.inc()) { if (!segment2.inExcludeSegments()) { assertTrue(segment2.inExceptionSegments()); } } } } }
/** * Tests a basic segmented timeline. */ public void testBasicSegmentedTimeline() { SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3); stl.setStartTime(946684800000L); // 1-Jan-2000 assertFalse(stl.containsDomainValue(946684799999L)); assertTrue(stl.containsDomainValue(946684800000L)); assertTrue(stl.containsDomainValue(946684800019L)); assertFalse(stl.containsDomainValue(946684800020L)); assertFalse(stl.containsDomainValue(946684800049L)); assertTrue(stl.containsDomainValue(946684800050L)); assertTrue(stl.containsDomainValue(946684800069L)); assertFalse(stl.containsDomainValue(946684800070L)); assertFalse(stl.containsDomainValue(946684800099L)); assertTrue(stl.containsDomainValue(946684800100L)); assertEquals(0, stl.toTimelineValue(946684800000L)); assertEquals(19, stl.toTimelineValue(946684800019L)); assertEquals(20, stl.toTimelineValue(946684800020L)); assertEquals(20, stl.toTimelineValue(946684800049L)); assertEquals(20, stl.toTimelineValue(946684800050L)); assertEquals(39, stl.toTimelineValue(946684800069L)); assertEquals(40, stl.toTimelineValue(946684800070L)); assertEquals(40, stl.toTimelineValue(946684800099L)); assertEquals(40, stl.toTimelineValue(946684800100L)); assertEquals(946684800000L, stl.toMillisecond(0)); assertEquals(946684800019L, stl.toMillisecond(19)); assertEquals(946684800050L, stl.toMillisecond(20)); assertEquals(946684800069L, stl.toMillisecond(39)); assertEquals(946684800100L, stl.toMillisecond(40)); }
/** * Tests a basic time line with one exception. */ public void testSegmentedTimelineWithException1() { SegmentedTimeline stl = new SegmentedTimeline(10, 2, 3); stl.setStartTime(946684800000L); // 1-Jan-2000 stl.addException(946684800050L); assertFalse(stl.containsDomainValue(946684799999L)); assertTrue(stl.containsDomainValue(946684800000L)); assertTrue(stl.containsDomainValue(946684800019L)); assertFalse(stl.containsDomainValue(946684800020L)); assertFalse(stl.containsDomainValue(946684800049L)); assertFalse(stl.containsDomainValue(946684800050L)); assertFalse(stl.containsDomainValue(946684800059L)); assertTrue(stl.containsDomainValue(946684800060L)); assertTrue(stl.containsDomainValue(946684800069L)); assertFalse(stl.containsDomainValue(946684800070L)); assertFalse(stl.containsDomainValue(946684800099L)); assertTrue(stl.containsDomainValue(946684800100L)); //long v = stl.toTimelineValue(946684800020L); assertEquals(0, stl.toTimelineValue(946684800000L)); assertEquals(19, stl.toTimelineValue(946684800019L)); assertEquals(20, stl.toTimelineValue(946684800020L)); assertEquals(20, stl.toTimelineValue(946684800049L)); assertEquals(20, stl.toTimelineValue(946684800050L)); assertEquals(29, stl.toTimelineValue(946684800069L)); assertEquals(30, stl.toTimelineValue(946684800070L)); assertEquals(30, stl.toTimelineValue(946684800099L)); assertEquals(30, stl.toTimelineValue(946684800100L)); assertEquals(946684800000L, stl.toMillisecond(0)); assertEquals(946684800019L, stl.toMillisecond(19)); assertEquals(946684800060L, stl.toMillisecond(20)); assertEquals(946684800069L, stl.toMillisecond(29)); assertEquals(946684800100L, stl.toMillisecond(30)); }
/** * Test 1 checks 9am Friday 26 March 2004 converts to a timeline value and back again * correctly. This is prior to Daylight Saving. */ public void test1() { Log.info("Entering SegmentedTimelineTests2.test1()..."); Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); Log.info("date = " + date + "[" + date.getTime() + "]"); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); Log.info("value = " + value); long ms = timeline.toMillisecond(value); Log.info("ms = " + ms); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); Log.info("reverted = " + reverted + '[' + reverted.getTime() + "]"); Log.info(""); assertTrue("test1", value == (900000 * 34) && date.getTime() == reverted.getTime()); }
/** * Test 2 checks 9.15am Friday 26 March 2004 converts to a timeline value and back again * correctly. This is prior to Daylight Saving. */ public void test2() { Log.info("Entering SegmentedTimelineTests2.test2()..."); Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 15); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); Log.info("date = " + date + "[" + date.getTime() + "]"); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); Log.info("value = " + value); long ms = timeline.toMillisecond(value); Log.info("ms = " + ms); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); Log.info("reverted = " + reverted + '[' + reverted.getTime() + "]"); Log.info(""); assertTrue( "test2", value == (900000 * 34 + 900000) && date.getTime() == reverted.getTime() ); }
/** * Test 3 checks 9.30am Friday 26 March 2004 converts to a timeline value and back again * correctly. This is prior to Daylight Saving. */ public void test3() { Log.info("Entering SegmentedTimelineTests2.test3()..."); Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime(); Log.info("date = " + date + "[" + date.getTime() + "]"); SegmentedTimeline timeline = getTimeline(); long value = timeline.toTimelineValue(date); Log.info("value = " + value); long ms = timeline.toMillisecond(value); Log.info("ms = " + ms); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); Log.info("reverted = " + reverted + '[' + reverted.getTime() + "]"); Log.info(""); assertTrue( "test2", value == (900000 * 34 + 900000 * 2) && date.getTime() == reverted.getTime() ); }
/** * Test 4 checks 9.30am Friday 26 March 2004 (+ 1 millisecond) converts to a timeline * value and back again correctly. This is prior to Daylight Saving. */ public void test4() { Log.info("Entering SegmentedTimelineTests2.test4()..."); Calendar cal = Calendar.getInstance(Locale.UK); cal.set(Calendar.YEAR, 2004); cal.set(Calendar.MONTH, Calendar.MARCH); cal.set(Calendar.DAY_OF_MONTH, 26); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 1); Date date = cal.getTime(); Log.info("date = " + date + "[" + date.getTime() + "]"); SegmentedTimeline timeline = this.getTimeline(); long value = timeline.toTimelineValue(date); Log.info("value = " + value); long ms = timeline.toMillisecond(value); Log.info("ms = " + ms); Calendar cal2 = Calendar.getInstance(Locale.UK); cal2.setTime(new Date(ms)); Date reverted = cal2.getTime(); Log.info("reverted = " + reverted + '[' + reverted.getTime() + "]"); Log.info(""); assertTrue( "test4", value == (900000 * 34 + 900000 * 2 + 1) && date.getTime() == reverted.getTime() ); }
/** * Confirm that the equals method can distinguish all the required fields. */ public void testEquals() { DateAxis a1 = new DateAxis("Test"); DateAxis a2 = new DateAxis("Test"); assertTrue(a1.equals(a2)); assertFalse(a1.equals(null)); assertFalse(a1.equals("Some non-DateAxis object")); // tickUnit a1.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7)); assertFalse(a1.equals(a2)); a2.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7)); assertTrue(a1.equals(a2)); // dateFormatOverride a1.setDateFormatOverride(new SimpleDateFormat("yyyy")); assertFalse(a1.equals(a2)); a2.setDateFormatOverride(new SimpleDateFormat("yyyy")); assertTrue(a1.equals(a2)); // tickMarkPosition a1.setTickMarkPosition(DateTickMarkPosition.END); assertFalse(a1.equals(a2)); a2.setTickMarkPosition(DateTickMarkPosition.END); assertTrue(a1.equals(a2)); // timeline a1.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); assertFalse(a1.equals(a2)); a2.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); assertTrue(a1.equals(a2)); }
/** * Tests that the factory method that creates Monday through Friday * segmented timeline does so correctly. */ public void testMondayThroughFridaySegmentedTimeline() { // verify attributes set during object construction assertEquals( SegmentedTimeline.DAY_SEGMENT_SIZE, this.mondayFridayTimeline.getSegmentSize() ); assertEquals( SegmentedTimeline.FIRST_MONDAY_AFTER_1900, this.mondayFridayTimeline.getStartTime() ); assertEquals(5, this.mondayFridayTimeline.getSegmentsIncluded()); assertEquals(2, this.mondayFridayTimeline.getSegmentsExcluded()); }
/** * Tests methods related to adding exceptions. * * @param timeline the timeline to verify * @param exceptionString array of Strings that represent the exceptions * @param fmt Format object that can parse the exceptionString strings * * @throws ParseException if there is a parsing error. */ public void verifyExceptionSegments(SegmentedTimeline timeline, String[] exceptionString, Format fmt) throws ParseException { // fill in the exceptions long[] exception = verifyFillInExceptions( timeline, exceptionString, fmt ); int m = exception.length; // verify list of exceptions assertEquals(exception.length, timeline.getExceptionSegments().size()); SegmentedTimeline.Segment lastSegment = timeline.getSegment(exception[m - 1]); for (int i = 0; i < m; i++) { SegmentedTimeline.Segment segment = timeline.getSegment(exception[i]); assertTrue(segment.inExceptionSegments()); // include current exception and last one assertEquals(m - i, timeline.getExceptionSegmentCount( segment.getSegmentStart(), lastSegment.getSegmentEnd())); // exclude current exception and last one assertEquals( Math.max(0, m - i - 2), timeline.getExceptionSegmentCount( exception[i] + 1, exception[m - 1] - 1) ); } }
/** * Adds an array of exceptions relative to the base timeline. * * @param timeline The timeline where the exceptions will be stored * @param exceptionString The exceptions to load * @param fmt The date formatter to use to parse each exceptions[i] value * @throws ParseException If there is any exception parsing each * exceptions[i] value. */ private void fillInBaseTimelineExceptions(SegmentedTimeline timeline, String[] exceptionString, Format fmt) throws ParseException { SegmentedTimeline baseTimeline = timeline.getBaseTimeline(); for (int i = 0; i < exceptionString.length; i++) { long e; if (fmt instanceof NumberFormat) { e = ((NumberFormat) fmt).parse(exceptionString[i]).longValue(); } else { e = timeline.getTime( ((SimpleDateFormat) fmt).parse(exceptionString[i]) ); } timeline.addBaseTimelineException(e); // verify all timeline segments included in the // baseTimeline.segment are now exceptions SegmentedTimeline.Segment segment1 = baseTimeline.getSegment(e); for (SegmentedTimeline.Segment segment2 = timeline.getSegment(segment1.getSegmentStart()); segment2.getSegmentStart() <= segment1.getSegmentEnd(); segment2.inc()) { if (!segment2.inExcludeSegments()) { assertTrue(segment2.inExceptionSegments()); } } } }
/** * Adds new exceptions to a timeline. The exceptions are the excluded * segments from its base timeline. * * @param timeline the timeline. * @param from the start. * @param to the end. */ private void fillInBaseTimelineExclusionsAsExceptions( SegmentedTimeline timeline, long from, long to) { // add the base timeline exclusions as timeline's esceptions timeline.addBaseTimelineExclusions(from, to); // validate base timeline exclusions added as timeline's esceptions for (SegmentedTimeline.Segment segment1 = timeline.getBaseTimeline().getSegment(from); segment1.getSegmentStart() <= to; segment1.inc()) { if (segment1.inExcludeSegments()) { // verify all timeline segments included in the // baseTimeline.segment are now exceptions for (SegmentedTimeline.Segment segment2 = timeline.getSegment(segment1.getSegmentStart()); segment2.getSegmentStart() <= segment1.getSegmentEnd(); segment2.inc()) { if (!segment2.inExcludeSegments()) { assertTrue(segment2.inExceptionSegments()); } } } } }