1   /*
2    * Created on Oct 27, 2006
3    *
4    */
5   package org.musicontroller.test;
6   
7   import java.util.ArrayList;
8   import java.util.Date;
9   import java.util.LinkedList;
10  import java.util.List;
11  
12  import org.junit.Before;
13  import org.junit.Test;
14  import static org.junit.Assert.*;
15  
16  import org.musicontroller.DJ;
17  import org.musicontroller.DJImpl;
18  import org.musicontroller.core.Band;
19  import org.musicontroller.core.Keyword;
20  import org.musicontroller.core.Keywordbag;
21  import org.musicontroller.core.Playlist;
22  import org.musicontroller.core.Song;
23  import org.musicontroller.security.User;
24  import org.musicontroller.songselection.AdvancedRandomSongSelector;
25  import org.musicontroller.songselection.CandidateSelector;
26  import org.musicontroller.songselection.LastPlayedContainer;
27  import org.musicontroller.songselection.SongSelector;
28  
29  class TstCandidateSelector implements CandidateSelector {
30  	private List<Song> _database = null;
31  	
32  	private void addEvents(Song song, int count) {
33  		for (int i=0; i<count; i++) {
34  			song.addPlayedEvent(new Date(), new User()); //User not important here
35  			try {
36  				Thread.sleep(10);
37  				 //Event-timestamps should be unique, otherwise they won't fit in a set.
38  			} catch (InterruptedException e) {
39  				//Ignore
40  			}
41  		}
42  	}
43  	
44  	private List<Song> getSongList() {
45  		if (_database == null) {
46  			_database = new ArrayList<Song>();
47  			
48  			Keyword kw1 = new Keyword();
49  			kw1.setName("Een");
50  			kw1.setId(1);
51  			
52  			Keyword kw3 = new Keyword();
53  			kw3.setName("Drie");
54  			kw3.setId(3);
55  			
56  			Keyword kw4 = new Keyword();
57  			kw4.setName("Vier");
58  			kw4.setId(4);
59  			
60  			Keyword kw7 = new Keyword();
61  			kw7.setName("Zeven");
62  			kw7.setId(7);
63  			
64  			Keyword kw9 = new Keyword();
65  			kw9.setName("Negen");
66  			kw9.setId(9);
67  			
68  			Keyword kw12 = new Keyword();
69  			kw12.setName("Twaalf");
70  			kw12.setId(12);
71  		
72  			List<Keyword> kwl1 = new LinkedList<Keyword>();
73  			kwl1.add(kw1);
74  			kwl1.add(kw4);
75  			
76  			List<Keyword> kwl2 = new LinkedList<Keyword>();
77  			kwl2.add(kw1);
78  			kwl2.add(kw3);
79  			
80  			List<Keyword> kwl3 = new LinkedList<Keyword>();
81  			kwl3.add(kw7);
82  			kwl3.add(kw9);
83  			kwl3.add(kw12);
84  			
85  			List<Keyword> kwl4 = new LinkedList<Keyword>();
86  			kwl3.add(kw1);
87  			kwl3.add(kw12);
88  	
89  			Keywordbag kb1 = new Keywordbag();
90  			kb1.setId(1);
91  			kb1.setKeywords(kwl1);
92  			
93  			Keywordbag kb2 = new Keywordbag();
94  			kb2.setId(2);
95  			kb2.setKeywords(kwl2);
96  			
97  			Keywordbag kb3 = new Keywordbag();
98  			kb3.setId(1);
99  			kb3.setKeywords(kwl3);
100 			
101 			Keywordbag kb4 = new Keywordbag();
102 			kb4.setId(4);
103 			kb4.setKeywords(kwl4);
104 			
105 			Band b1 = new Band();
106 			b1.setId(1);
107 			b1.setName("Band one");
108 			Band b2 = new Band();
109 			b2.setId(2);
110 			b2.setName("Band two");
111 			Band b3 = new Band();
112 			b3.setId(3);
113 			b3.setName("Band three");
114 			
115 			Song s1 = new Song();
116 			s1.setId(1);
117 			s1.setName("Song one");
118 			s1.setBand(b1);
119 			s1.setKeywordbag(kb1);
120 			this.addEvents(s1,72);
121 			_database.add(s1);
122 			
123 			Song s2 = new Song();
124 			s2.setName("Song two");
125 			s2.setId(2);
126 			s2.setBand(b1);
127 			s2.setKeywordbag(kb2);
128 			this.addEvents(s2,18);
129 			_database.add(s2);
130 			
131 			Song s3 = new Song();
132 			s3.setName("Song three");
133 			s3.setId(3);
134 			s3.setBand(b1);
135 			s3.setKeywordbag(kb3);
136 			this.addEvents(s3,23);
137 			_database.add(s3);
138 			
139 			Song s4 = new Song();
140 			s4.setName("Song four");
141 			s4.setId(4);
142 			s4.setBand(b1);
143 			s4.setKeywordbag(kb1);
144 			this.addEvents(s4,90);
145 			_database.add(s4);
146 			
147 			Song s5 = new Song();
148 			s5.setName("Song five");
149 			s5.setId(5);
150 			s5.setBand(b1);
151 			s5.setKeywordbag(kb1);
152 			this.addEvents(s5,1);
153 			_database.add(s5);
154 	
155 			Song s6 = new Song();
156 			s6.setName("Song six");
157 			s6.setId(6);
158 			s6.setBand(b2);
159 			s6.setKeywordbag(kb4);
160 			this.addEvents(s6,12);
161 			_database.add(s6);
162 			
163 			Song s7 = new Song();
164 			s7.setName("Song seven");
165 			s7.setId(7);
166 			s7.setBand(b2);
167 			s7.setKeywordbag(kb2);
168 			this.addEvents(s7,2);
169 			_database.add(s7);
170 			
171 			Song s8 = new Song();
172 			s8.setName("Song eight");
173 			s8.setId(8);
174 			s8.setBand(b2);
175 			s8.setKeywordbag(kb3);
176 			this.addEvents(s8,3);
177 			_database.add(s8);
178 	
179 			Song s9 = new Song();
180 			s9.setName("Song nine");
181 			s9.setId(9);
182 			s9.setBand(b3);
183 			s9.setKeywordbag(kb2);
184 			this.addEvents(s9,30);
185 			_database.add(s9);
186 	
187 			Playlist p1 = new Playlist();
188 			p1.addSong(s1,1);
189 			p1.addSong(s2,2);
190 			p1.addSong(s3,3);
191 			
192 			Playlist p2 = new Playlist();
193 			p2.addSong(s4,1);
194 			p2.addSong(s5,2);
195 		}		
196 		return _database;
197 	}
198 	
199 	public List<Song> selectCandidates(List<Long> requests) {
200 		return getSongList();
201 	}
202 	
203 	public void removeFromCandidates(Song song) {
204 		_database.remove(song);
205 	}
206 
207 	public void playRequestsAtRandom() {
208 		// TODO Auto-generated method stub
209 		
210 	}
211 
212 	public void playRequestsInOrder() {
213 		// TODO Auto-generated method stub
214 		
215 	}
216 
217 	public boolean requestsPlayedAtRandom() {
218 		// TODO Auto-generated method stub
219 		return false;
220 	}
221 
222 	public boolean requestsPlayedInOrder() {
223 		// TODO Auto-generated method stub
224 		return false;
225 	}
226 
227 	public int getCandidateCount() {
228 		// TODO Auto-generated method stub
229 		return 0;
230 	}
231 
232 	public List<Song> selectCandidates(int count) {
233 		// TODO Auto-generated method stub
234 		return null;
235 	}
236 
237 	public void addToCandidates(Song song) {
238 		// TODO Auto-generated method stub
239 		
240 	}
241 	
242 }
243 
244 public class SongSelectionTest {
245 	private SongSelector ss;
246 	private CandidateSelector cs;
247 	private LastPlayedContainer lpc;
248 	
249 	@Before
250 	public void setUp() {
251 		if (ss==null) {
252 			ss = new AdvancedRandomSongSelector();
253 		}
254 		if (cs==null) {
255 			cs = new TstCandidateSelector();
256 		}
257 		if (lpc==null) {
258 			lpc = new LastPlayedContainer();
259 		}
260 	}
261 
262 	@Test
263 	public void testSongSelectorNextSelect() {
264 		Song best = ss.selectSong(cs.selectCandidates(null),lpc,null);
265 		cs.removeFromCandidates(best);
266 		assertEquals(4,best.getId()); //Most played song should be selected
267 		
268 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //We select the best song again, we should now get the best-fitting next song.
269 		cs.removeFromCandidates(best);
270 		assertEquals(1,best.getId());
271 
272 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //and again
273 		cs.removeFromCandidates(best);
274 		assertEquals(9,best.getId());
275 		
276 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //...and again
277 		cs.removeFromCandidates(best);
278 		assertEquals(3,best.getId());
279 
280 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //...
281 		cs.removeFromCandidates(best);
282 		assertEquals(2,best.getId());
283 
284 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //...
285 		cs.removeFromCandidates(best);
286 		assertEquals(6,best.getId());
287 
288 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //...getting tired of it
289 		cs.removeFromCandidates(best);
290 		assertEquals(8,best.getId());
291 		
292 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //..how many to go...
293 		cs.removeFromCandidates(best);
294 		assertEquals(7,best.getId());
295 
296 		best = ss.selectSong(cs.selectCandidates(null),lpc,null); //...ahh finally!
297 		cs.removeFromCandidates(best);
298 		assertEquals(5,best.getId());
299 	}
300 	
301 	/**
302 	 * Check whether the DJ-witchery predicts the future right.
303 	 */
304 	@Test
305 	public void testGlassBowl() {
306 		DJ dj = new DJImpl();
307 		dj.setSongSelector(ss);
308 		dj.setCandidateSelector(cs);
309 	
310 		List<Long> forecast = dj.peek(9);
311 		//TODO Grrrr won't work because of a nullpointer-dao in AdvancedRandomSongSelector.java
312 		//The order if id's should be the same as in the test above.
313 		assertEquals(9,forecast.size());
314 	}
315 	
316 }