?? segmentedtimelinetests.java
字號:
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(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(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(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(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(msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
verifyTranslations(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(ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
verifyTranslations(ms2BaseTimeline, 0);
}
/**
* Tests translations for the Monday through Friday timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2Translations() throws ParseException {
fillInBaseTimelineExceptions(ms2Timeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
fillInBaseTimelineExclusionsAsExceptions(ms2Timeline, 0, 5000);
verifyTranslations(ms2Timeline, 1);
}
/**
* Tests translations for the Monday through Friday timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testMondayThroughFridayTranslations() throws ParseException {
verifyFillInExceptions(mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT);
verifyTranslations(mondayFridayTimeline, monday.getTime().getTime());
}
/**
* Tests translations for the Fifteen Min timeline
*
* @throws ParseException if there is a parsing error.
*/
public void testFifteenMinTranslations() throws ParseException {
verifyFillInExceptions(fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
fillInBaseTimelineExceptions(fifteenMinTimeline, US_HOLIDAYS, DATE_FORMAT);
fillInBaseTimelineExclusionsAsExceptions(fifteenMinTimeline,
monday9am.getTime().getTime(),
monday9am.getTime().getTime() + FIVE_YEARS);
verifyTranslations(fifteenMinTimeline, 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(msTimeline);
verifySerialization(ms2Timeline);
verifySerialization(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
timeline.setExceptionSegments(new java.util.ArrayList());
assertEquals(0, timeline.getExceptionSegments().size());
// add our exceptions and store locally in ArrayList of Longs
ArrayList exceptionList = new ArrayList();
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]));
}
// only add an exception if it is currently an included segment
SegmentedTimeline.Segment segment = timeline.getSegment(e);
if (segment.inIncludeSegments()) {
timeline.addException(e);
exceptionList.add(new Long(e));
assertEquals(exceptionList.size(), timeline.getExceptionSegments().size());
assertTrue(segment.inExceptionSegments());
}
}
// make array of exceptions
long[] exception = new long[exceptionList.size()];
int i = 0;
for (Iterator iter = exceptionList.iterator(); iter.hasNext();) {
Long l = (Long) iter.next();
exception[i++] = l.longValue();
}
return (exception);
}
/**
* 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();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -