⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segmentedtimelinetests.java

📁 关于jfreechart的电子说明书,一种说明图型使用的介绍
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    }

    /**
     * Tests that the Fifteen-Min timeline's included and excluded
     * segments are being calculated correctly. The test is performed starting
     * on the first monday after 1/1/2000 and for five years.
     */
    public void testFifteenMinIncludedAndExcludedSegments() {
        verifyIncludedAndExcludedSegments(
            this.fifteenMinTimeline, this.monday9am.getTime().getTime()
        );
    }

    /**
     * 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 exception segments
    //////////////////////////////////////////////////////////////////////////

    /**
     * Tests methods related to exceptions methods in the msTimeline.
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMsExceptionSegments() throws ParseException {
        verifyExceptionSegments(this.msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
    }

    /**
     * Tests methods related to exceptions methods in the ms2BaseTimeline.
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMs2BaseTimelineExceptionSegments() throws ParseException {
        verifyExceptionSegments(
            this.ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT
        );
    }

    /**
     * Tests methods related to exceptions methods in the mondayFridayTimeline.
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMondayThoughFridayExceptionSegments() 
        throws ParseException {
        verifyExceptionSegments(
            this.mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT
        );
    }

    /**
     * Tests methods related to exceptions methods in the fifteenMinTimeline.
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testFifteenMinExceptionSegments() throws ParseException {
        verifyExceptionSegments(
            this.fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT
        );
    }

    /**
     * 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)
            );
        }

    }

    //////////////////////////////////////////////////////////////////////////
    // test timeline translations
    //////////////////////////////////////////////////////////////////////////

    /**
     * Tests translations for 1-ms timeline
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMsTranslations() throws ParseException {
        verifyFillInExceptions(this.msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
        verifyTranslations(this.msTimeline, 0);
    }

    /**
     * Tests translations for the base timeline used for the ms2Timeline
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMs2BaseTimelineTranslations() throws ParseException {
        verifyFillInExceptions(
            this.ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT
        );
        verifyTranslations(this.ms2BaseTimeline, 0);
    }

    /**
     * Tests translations for the Monday through Friday timeline
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMs2Translations() throws ParseException {
        fillInBaseTimelineExceptions(
            this.ms2Timeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT
        );
        fillInBaseTimelineExclusionsAsExceptions(this.ms2Timeline, 0, 5000);
        verifyTranslations(this.ms2Timeline, 1);
    }

    /**
     * Tests translations for the Monday through Friday timeline
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testMondayThroughFridayTranslations() throws ParseException {
        verifyFillInExceptions(
            this.mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT
        );
        verifyTranslations(
            this.mondayFridayTimeline, this.monday.getTime().getTime()
        );
    }

    /**
     * Tests translations for the Fifteen Min timeline
     * 
     * @throws ParseException if there is a parsing error.
     */
    public void testFifteenMinTranslations() throws ParseException {
        verifyFillInExceptions(
            this.fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT
        );
        fillInBaseTimelineExceptions(
            this.fifteenMinTimeline, US_HOLIDAYS, DATE_FORMAT
        );
        fillInBaseTimelineExclusionsAsExceptions(
            this.fifteenMinTimeline,
            this.monday9am.getTime().getTime(),
            this.monday9am.getTime().getTime() + FIVE_YEARS
        );
        verifyTranslations(
            this.fifteenMinTimeline, this.monday9am.getTime().getTime()
        );
    }

    /**
     * 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();
            }
        }
    }

    //////////////////////////////////////////////////////////////////////////
    // test serialization
    //////////////////////////////////////////////////////////////////////////

    /**
     * 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());
    }

    /**
     * Tests serialization of an instance.
     * @param a1 The timeline to verify the serialization
     */
    private void verifySerialization(SegmentedTimeline a1) {
        SegmentedTimeline a2 = null;

        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutput out = new ObjectOutputStream(buffer);
            out.writeObject(a1);
            out.close();

            ObjectInput in = new ObjectInputStream(
                new ByteArrayInputStream(buffer.toByteArray())
            );
            a2 = (SegmentedTimeline) in.readObject();
            in.close();
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        assertEquals(a1, a2);
    }

    /**
     * Adds an array of exceptions to the timeline. The timeline exception list
     * is first cleared.
     * @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.
     * @return An array of Dates[] containing each exception date.
     */
    private long[] verifyFillInExceptions(SegmentedTimeline timeline,
                                         String[] exceptionString,
                                         Format fmt) throws ParseException {
        // make sure there are no exceptions

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -