1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/*
* Copyright 2014 Jesse Morgan
*/
package com.p4square.grow.model;
import java.util.Date;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests for Playlist.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class PlaylistTest {
public static void main(String... args) {
org.junit.runner.JUnitCore.main(PlaylistTest.class.getName());
}
/**
* Tests for Playlist and Chapter methods not covered in the deserialization test.
*/
@Test
public void testPlaylistAndChapter() {
// Create a playlist for the test
Playlist playlist = new Playlist();
playlist.add("chapter1", "video1");
playlist.add("chapter1", "video2");
// Chapter should not be complete
assertFalse(playlist.isChapterComplete("chapter1"));
// We should find the chapter in the map
Map<String, Chapter> chapterMap = playlist.getChaptersMap();
Chapter chapter1 = chapterMap.get("chapter1");
assertTrue(null != chapter1);
// We should find the videos in the map.
Map<String, VideoRecord> videoMap = chapter1.getVideos();
assertTrue(null != videoMap.get("video1"));
assertTrue(null != videoMap.get("video2"));
assertTrue(null == videoMap.get("video3"));
// Mark the videos as complete
VideoRecord video1 = videoMap.get("video1");
VideoRecord video2 = videoMap.get("video2");
video1.complete();
video2.complete();
// Chapter should be complete now.
assertTrue(playlist.isChapterComplete("chapter1"));
assertFalse(playlist.isChapterComplete("bogusChapter"));
}
/**
* Tests for Playlist default values.
*/
@Test
public void testPlaylistDefaults() {
Date before = new Date();
Playlist p = new Playlist();
// Verify that a playlist without an explicit lastUpdated date is older than now.
assertTrue(p.getLastUpdated().before(before));
}
/**
* Tests for the Playlist merge method.
*/
@Test
public void testMergePlaylist() {
Playlist oldList = new Playlist();
oldList.add("chapter1", "video1").setRequired(true);
oldList.add("chapter2", "video2").setRequired(false);
oldList.add("chapter2", "video3").complete();
oldList.setLastUpdated(new Date(100));
Playlist newList = new Playlist();
newList.add("chapter1", "video4").setRequired(true);
newList.add("chapter2", "video5").setRequired(false);
newList.add("chapter3", "video6").setRequired(false);
newList.setLastUpdated(new Date(500));
// Verify that you can't merge the old into the new
newList.merge(oldList);
assertTrue(null == newList.find("video2"));
// Merge the new list into the old and verify results
oldList.merge(newList);
// All Videos Present
assertTrue(oldList.find("video1").getRequired());
assertFalse(oldList.find("video2").getRequired());
assertTrue(oldList.find("video3").getComplete());
assertTrue(oldList.find("video4").getRequired());
assertFalse(oldList.find("video5").getRequired());
assertFalse(oldList.find("video6").getRequired());
// New Chapter added
Map<String, Chapter> chapters = oldList.getChaptersMap();
assertEquals(3, chapters.size());
assertTrue(null != chapters.get("chapter3"));
// Date updated
assertEquals(newList.getLastUpdated(), oldList.getLastUpdated());
// Video objects are actually independent
VideoRecord oldVideo4 = oldList.find("video4");
VideoRecord newVideo4 = newList.find("video4");
assertTrue(oldVideo4 != newVideo4);
}
/**
* Tests for merges that move videos.
*/
@Test
public void testMergeMoveVideoRecord() {
Playlist oldList = new Playlist();
oldList.add("chapter1", "video1").setRequired(true);
VideoRecord toMove = oldList.add("chapter1", "video2");
toMove.setRequired(true);
toMove.complete();
oldList.add("chapter2", "video3").complete();
oldList.setLastUpdated(new Date(100));
Playlist newList = new Playlist();
newList.add("chapter1", "video1").setRequired(true);
newList.add("chapter2", "video2").setRequired(true);
newList.add("chapter3", "video3").complete();
newList.setLastUpdated(new Date(500));
// Merge the new list into the old and verify results
oldList.merge(newList);
// All Videos Present
assertTrue(oldList.find("video1").getRequired());
assertTrue(oldList.find("video2").getRequired());
assertTrue(oldList.find("video3").getComplete());
// toMove is in the correct chapter.
assertNull(oldList.getChaptersMap().get("chapter1").getVideoRecord("video2"));
VideoRecord afterMove = oldList.getChaptersMap().get("chapter2").getVideoRecord("video2");
assertSame(toMove, afterMove);
// video3 got moved to the new chapter3
assertNull(oldList.getChaptersMap().get("chapter2").getVideoRecord("video3"));
assertTrue(oldList.getChaptersMap().get("chapter3").getVideoRecord("video3").getComplete());
}
}
|