📄 segmentedtimelinetests.java
字号:
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());
}
}
}
}
}
/**
* Confirm that cloning works.
*/
public void testCloning() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = null;
try {
l2 = (SegmentedTimeline) l1.clone();
}
catch (CloneNotSupportedException e) {
System.err.println("SegmentedTimelineTests2.testCloning: failed to clone.");
}
assertTrue(l1 != l2);
assertTrue(l1.getClass() == l2.getClass());
assertTrue(l1.equals(l2));
}
/**
* Confirm that the equals method can distinguish all the required fields.
*/
public void testEquals() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = new SegmentedTimeline(1000, 5, 2);
assertTrue(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1001, 5, 2);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 4, 2);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 5, 1);
assertFalse(l1.equals(l2));
l1 = new SegmentedTimeline(1000, 5, 2);
l2 = new SegmentedTimeline(1000, 5, 2);
// start time...
l1.setStartTime(1234L);
assertFalse(l1.equals(l2));
l2.setStartTime(1234L);
assertTrue(l1.equals(l2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization2() {
SegmentedTimeline l1 = new SegmentedTimeline(1000, 5, 2);
SegmentedTimeline l2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(l1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
l2 = (SegmentedTimeline) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
boolean b = l1.equals(l2);
assertTrue(b);
}
//////////////////////////////////////////////////////////////////////////
// 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 ("???");
}
}
*/
/**
* 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));
}
//////////////////////////////////////////////////////////////////////////
// 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 + -