📄 segmentedtimelinetests.java
字号:
/**
* 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) {
SegmentedTimeline.Segment segment
= timeline.getSegment(startTest + testCycle * timeline.getSegmentSize());
for (int i = 0; i < 1000; i++) {
long translatedValue = timeline.toTimelineValue(segment.getIndex());
long newValue = timeline.toDomainValue(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.getIndex(), newValue);
}
else {
assertEquals(segment.getIndex(), 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.getExceptionSegments().clear();
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();
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());
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// utility methods
//////////////////////////////////////////////////////////////////////////
/**
* Formats a Calendar object avoiding converting into a Date so the local
* TimeZone is not used to convert the output. The Calendar object used as
* parameter is assumed to use the SegmentedTimeline.TIME_ZONE time zone.
*
* @param cal Calendar object to format
* @return
*/
/*
private String calToString(Calendar cal) {
DecimalFormat fmt2 = new DecimalFormat("00");
DecimalFormat fmt4 = new DecimalFormat("0000");
return (getDayOfWeekString(cal)+" "+
fmt2.format(cal.get(Calendar.MONTH)+1)+"/"+
fmt2.format(cal.get(Calendar.DATE))+"/"+
fmt4.format(cal.get(Calendar.YEAR))+" "+
fmt2.format(cal.get(Calendar.HOUR_OF_DAY))+":"+
fmt2.format(cal.get(Calendar.MINUTE))+":"+
fmt2.format(cal.get(Calendar.SECOND))+":"+
fmt4.format(cal.get(Calendar.MILLISECOND)));
}
*/
/**
* Formats time avoiding converting into a Date so the local
* TimeZone is not used to convert the output. The time used as
* parameter is assumed to be relative to SegmentedTimeline.TIME_ZONE time zone.
*
* @param time time to format
* @return
*/
/*
private String calToString(long time) {
Calendar cal = new GregorianCalendar(SegmentedTimeline.NO_DST_TIME_ZONE);
cal.setTimeInMillis(time);
return (calToString(cal));
}
*/
/**
* Returns a three letter day of the week string for Calendar formatting.
* @param cal Calendar to check
* @return
*/
/*
private String getDayOfWeekString(Calendar cal) {
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY: return ("Sun");
case Calendar.MONDAY: return ("Mon");
case Calendar.TUESDAY: return ("Tue");
case Calendar.WEDNESDAY: return ("Wed");
case Calendar.THURSDAY: return ("Thu");
case Calendar.FRIDAY: return ("Fri");
case Calendar.SATURDAY: return ("Sat");
default: return ("???");
}
}
*/
//////////////////////////////////////////////////////////////////////////
// main method only for debug
//////////////////////////////////////////////////////////////////////////
/**
* Only use to debug JUnit suite.
*
* @param args ignored.
*
* @throws Exception if there is some problem.
*/
public static void main(String[] args) throws Exception {
SegmentedTimelineTests test = new SegmentedTimelineTests("Test");
test.setUp();
test.testMondayThoughFridayExceptionSegments();
test.tearDown();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -