1 /***
2 * Ambient - A music player for the Android platform
3 Copyright (C) 2007 Martin Vysny
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package sk.baka.ambient.playlist;
20
21 import java.text.ParseException;
22 import java.util.List;
23
24 import junit.framework.TestCase;
25 import sk.baka.ambient.collection.TrackMetadataBean;
26 import sk.baka.ambient.collection.TrackOriginEnum;
27 import sk.baka.ambient.commons.TestUtils;
28
29 /***
30 * Tests the {@link Parsers} class.
31 *
32 * @author Martin Vysny
33 */
34 public class ParsersTest extends TestCase {
35 /***
36 * @throws Exception
37 */
38 public void testInvalidPlsStructure() throws Exception {
39 try {
40 Parsers.parsePls(TestUtils.stringToInputStream(
41 "[playlist]\n"
42 + "Cica=Mica\nFile5=http://bla\nFile10=huhu\n"
43 + "files=50", "UTF-8"), null);
44 fail();
45 } catch (ParseException ex) {
46
47 }
48 try {
49 Parsers.parsePls(TestUtils.stringToInputStream("[play]\n"
50 + "NumberOfEntries=1\nFile5=http://bla\nFile10=huhu\n"
51 + "files=50", "UTF-8"), null);
52 fail();
53 } catch (ParseException ex) {
54
55 }
56 try {
57 Parsers.parsePls(TestUtils.stringToInputStream("[playlist]\n"
58 + "NumberOfEntries=1\nFile1=http://bla\nFile2=huhu",
59 "UTF-8"), null);
60 fail();
61 } catch (ParseException ex) {
62
63 }
64 try {
65 Parsers.parsePls(TestUtils.stringToInputStream("[playlist]\n"
66 + "NumberOfEntries=1\nFile1=http://bla\nLength1=huhu",
67 "UTF-8"), null);
68 fail();
69 } catch (ParseException ex) {
70
71 }
72 }
73
74 /***
75 * @throws Exception
76 */
77 public void testInvalidPlsValues() throws Exception {
78 try {
79 Parsers.parsePls(TestUtils.stringToInputStream("[playlist]\n"
80 + "NumberOfEntries=ee\nFile1=http://bla\nLength1=huhu",
81 "UTF-8"), null);
82 fail();
83 } catch (ParseException ex) {
84
85 }
86 try {
87 Parsers.parsePls(TestUtils.stringToInputStream("[playlist]\n"
88 + "NumberOfEntries=1\nFile1=http://bla\nTitle1=huhu\n"
89 + "Length1=uu", "UTF-8"), null);
90 fail();
91 } catch (ParseException ex) {
92
93 }
94 }
95
96 /***
97 * @throws Exception
98 */
99 public void testReadEmptyPls() throws Exception {
100 assertTrue(Parsers.parsePls(
101 TestUtils.stringToInputStream("[playlist]\nnumberOfEntries=0",
102 "UTF-8"), null).isEmpty());
103 assertTrue(Parsers.parsePls(
104 TestUtils.stringToInputStream(
105 "[playlist]\nnumberOfEntries=0\nwhatever", "UTF-8"),
106 null).isEmpty());
107 }
108
109 /***
110 * @throws Exception
111 */
112 public void testReadValidPls() throws Exception {
113 List<TrackMetadataBean> tracks = Parsers.parsePls(TestUtils
114 .stringToInputStream("[playlist]\nnumberOfEntries=2\n"
115 + "file1=http://bla\ntitle1=hu\nlength1=-1\n"
116 + "file2=huhu/blabla.ogg\ntitle2=bla\nlength2=25",
117 "UTF-8"), null);
118 TrackMetadataBean t = tracks.get(0);
119 assertTrack(t, "http://bla", "hu", 0, TrackOriginEnum.Shoutcast);
120 t = tracks.get(1);
121 assertTrack(t, "huhu/blabla.ogg", "bla", 25, TrackOriginEnum.LocalFs);
122 }
123
124 /***
125 * @throws Exception
126 */
127 public void testReadValidRelPls() throws Exception {
128 List<TrackMetadataBean> tracks = Parsers.parsePls(TestUtils
129 .stringToInputStream("[playlist]\nnumberOfEntries=2\n"
130 + "file1=http://bla\ntitle1=hu\nlength1=-1\n"
131 + "file2=huhu/blabla.ogg\ntitle2=bla\nlength2=25",
132 "UTF-8"), "/foo/bar/");
133 TrackMetadataBean t = tracks.get(0);
134 assertEquals("http://bla", t.getLocation());
135 assertEquals("hu", t.getTitle());
136 assertEquals(0, t.getLength());
137 assertEquals(TrackOriginEnum.Shoutcast, t.getOrigin());
138 t = tracks.get(1);
139 assertEquals("/foo/bar/huhu/blabla.ogg", t.getLocation());
140 assertEquals("bla", t.getTitle());
141 assertEquals(25, t.getLength());
142 assertEquals(TrackOriginEnum.LocalFs, t.getOrigin());
143 }
144
145 /***
146 * @throws Exception
147 */
148 public void testInvalidM3uValues() throws Exception {
149 List<TrackMetadataBean> tracks = Parsers
150 .parseM3u(TestUtils.stringToInputStream("#HUHU\n"
151 + "#EXTINF:cica,huhu\n01 Foo.mp3", "UTF-8"), null, true);
152 TrackMetadataBean t = tracks.get(0);
153 assertEquals("01 Foo.mp3", t.getLocation());
154 assertNull(t.getTitle());
155 assertEquals(0, t.getLength());
156 }
157
158 /***
159 * @throws Exception
160 */
161 public void testReadEmptyM3u() throws Exception {
162 assertTrue(Parsers.parseM3u(TestUtils.stringToInputStream("", "UTF-8"),
163 null, true).isEmpty());
164 assertTrue(Parsers.parseM3u(
165 TestUtils.stringToInputStream("#EXTM3U\n#EXTINF:3,huhu\n\n",
166 "UTF-8"), null, true).isEmpty());
167 assertTrue(Parsers.parseM3u(
168 TestUtils.stringToInputStream("#EXTM3U\n#EXTINF:3,huhu\n\n",
169 "ISO8859_1"), null, false).isEmpty());
170 }
171
172 /***
173 * @throws Exception
174 */
175 public void testReadValidM3u() throws Exception {
176 List<TrackMetadataBean> tracks = Parsers.parseM3u(TestUtils
177 .stringToInputStream("#EXTM3U\n"
178 + "#EXTINF:25,foo\nhttp://bla\n"
179 + "#EXTINF:-1,bar\nhuhu/blabla.ogg\n" + "tada.mp3",
180 "UTF-8"), null, true);
181 TrackMetadataBean t = tracks.get(0);
182 assertTrack(t, "http://bla", "foo", 25, TrackOriginEnum.Shoutcast);
183 t = tracks.get(1);
184 assertTrack(t, "huhu/blabla.ogg", "bar", 0, TrackOriginEnum.LocalFs);
185 t = tracks.get(2);
186 assertTrack(t, "tada.mp3", null, 0, TrackOriginEnum.LocalFs);
187 }
188
189 private static void assertTrack(final TrackMetadataBean track,
190 String location, String title, int length, TrackOriginEnum origin) {
191 assertEquals(location, track.getLocation());
192 if (title == null) {
193 assertNull(track.getTitle());
194 } else {
195 assertEquals(title, track.getTitle());
196 }
197 assertEquals(length, track.getLength());
198 assertEquals(origin, track.getOrigin());
199 }
200
201 /***
202 * @throws Exception
203 */
204 public void testReadValidRelM3u() throws Exception {
205 List<TrackMetadataBean> tracks = Parsers.parseM3u(TestUtils
206 .stringToInputStream("#EXTM3U\n"
207 + "#EXTINF:25,foo\nhttp://bla\n"
208 + "#EXTINF:-1,bar\nhuhu/blabla.ogg\n" + "tada.mp3",
209 "UTF-8"), "/foo/bar", true);
210 TrackMetadataBean t = tracks.get(0);
211 assertTrack(t, "http://bla", "foo", 25, TrackOriginEnum.Shoutcast);
212 t = tracks.get(1);
213 assertTrack(t, "/foo/bar/huhu/blabla.ogg", "bar", 0,
214 TrackOriginEnum.LocalFs);
215 t = tracks.get(2);
216 assertTrack(t, "/foo/bar/tada.mp3", null, 0, TrackOriginEnum.LocalFs);
217 }
218
219 /***
220 * @throws Exception
221 */
222 public void testInvalidXspfStructure() throws Exception {
223 try {
224 Parsers.parseXspf(TestUtils.stringToInputStream(
225 "[playlist]\n"
226 + "Cica=Mica\nFile5=http://bla\nFile10=huhu\n"
227 + "files=50", "UTF-8"), null);
228 fail();
229 } catch (ParseException ex) {
230
231 }
232 try {
233 Parsers.parseXspf(TestUtils.stringToInputStream(
234 "<playlist version=\"2\""
235 + "xmlns=\"http://xspf.org/ns/1/\">"
236 + "<trackList><track>"
237 + "<location>file:hu.mp3</location></track>"
238 + "</trackList></playlist>", "UTF-8"), null);
239 fail();
240 } catch (ParseException ex) {
241
242 }
243 try {
244 Parsers.parseXspf(TestUtils.stringToInputStream(
245 "<playlist version=\"1\""
246 + "xmlns=\"http://xspf.org/ns/0/\">"
247 + "<trackList><track></track>"
248 + "</trackList></playlist>", "UTF-8"), null);
249 fail();
250 } catch (ParseException ex) {
251
252 }
253 }
254
255 /***
256 * @throws Exception
257 */
258 public void testReadEmptyXspf() throws Exception {
259 List<TrackMetadataBean> tracks = Parsers.parseXspf(TestUtils
260 .stringToInputStream("<playlist version=\"1\""
261 + " xmlns=\"http://xspf.org/ns/0/\"><trackList>"
262 + "</trackList></playlist>", "UTF-8"), null);
263 assertTrue(tracks.isEmpty());
264 }
265
266 /***
267 * @throws Exception
268 */
269 public void testReadValidXspf() throws Exception {
270 List<TrackMetadataBean> tracks = Parsers.parseXspf(TestUtils
271 .stringToInputStream("<playlist version=\"1\""
272 + " xmlns=\"http://xspf.org/ns/0/\">"
273 + "<trackList><track>"
274 + "<location>file:cica.mp3</location>"
275 + "<creator>Cremator</creator>"
276 + "<album>Cremetery</album>" + "<title>Cica</title>"
277 + "<duration>271066</duration>" + "</track><track>"
278 + "<location>http://cica:7808</location></track>"
279 + "</trackList></playlist>", "UTF-8"), null);
280 TrackMetadataBean t = tracks.get(0);
281 assertTrack(t, "cica.mp3", "Cica", 271, TrackOriginEnum.LocalFs);
282 assertEquals("Cremator", t.getArtist());
283 assertEquals("Cremetery", t.getAlbum());
284 t = tracks.get(1);
285 assertTrack(t, "http://cica:7808", null, 0, TrackOriginEnum.Shoutcast);
286 }
287
288 /***
289 * @throws Exception
290 */
291 public void testReadValidRelXspf() throws Exception {
292 List<TrackMetadataBean> tracks = Parsers.parseXspf(TestUtils
293 .stringToInputStream("<playlist version=\"1\""
294 + " xmlns=\"http://xspf.org/ns/0/\">"
295 + "<trackList><track>"
296 + "<location>file:cica.mp3</location>"
297 + "<creator>Cremator</creator>"
298 + "<album>Cremetery</album>" + "<title>Cica</title>"
299 + "<duration>271066</duration>" + "</track><track>"
300 + "<location>http://cica:7808</location></track>"
301 + "</trackList></playlist>", "UTF-8"), "/foo/bar");
302 TrackMetadataBean t = tracks.get(0);
303 assertTrack(t, "/foo/bar/cica.mp3", "Cica", 271,
304 TrackOriginEnum.LocalFs);
305 assertEquals("Cremator", t.getArtist());
306 assertEquals("Cremetery", t.getAlbum());
307 t = tracks.get(1);
308 assertTrack(t, "http://cica:7808", null, 0, TrackOriginEnum.Shoutcast);
309 }
310
311 /***
312 * @throws Exception
313 */
314 public void testInvalidWplStructure() throws Exception {
315 try {
316 Parsers.parseWpl(TestUtils.stringToInputStream(
317 "[playlist]\n"
318 + "Cica=Mica\nFile5=http://bla\nFile10=huhu\n"
319 + "files=50", "UTF-8"), null);
320 fail();
321 } catch (ParseException ex) {
322
323 }
324 try {
325 Parsers.parseWpl(TestUtils.stringToInputStream(
326 "<?wpl version=\"1.0\"?>" + "<smil><body><seq>"
327 + "<media/></seq></body></smil>", "UTF-8"), null);
328 fail();
329 } catch (ParseException ex) {
330
331 }
332 }
333
334 /***
335 * @throws Exception
336 */
337 public void testReadEmptyWpl() throws Exception {
338 List<TrackMetadataBean> tracks = Parsers.parseWpl(
339 TestUtils
340 .stringToInputStream("<?wpl version=\"1.0\"?>"
341 + "<smil><body><seq>" + "</seq></body></smil>",
342 "UTF-8"), null);
343 assertTrue(tracks.isEmpty());
344 }
345
346 /***
347 * @throws Exception
348 */
349 public void testReadValidWpl() throws Exception {
350 List<TrackMetadataBean> tracks = Parsers.parseWpl(TestUtils
351 .stringToInputStream("<?wpl version=\"1.0\"?>"
352 + "<smil><body><seq>" + "<media src=\"cica.mp3\"/>"
353 + "<media src=\"http://blabla:8000\"/>"
354 + "</seq></body></smil>", "UTF-8"), null);
355 TrackMetadataBean t = tracks.get(0);
356 assertTrack(t, "cica.mp3", null, 0, TrackOriginEnum.LocalFs);
357 t = tracks.get(1);
358 assertTrack(t, "http://blabla:8000", null, 0, TrackOriginEnum.Shoutcast);
359 }
360
361 /***
362 * @throws Exception
363 */
364 public void testReadValidRelWpl() throws Exception {
365 List<TrackMetadataBean> tracks = Parsers.parseWpl(TestUtils
366 .stringToInputStream("<?wpl version=\"1.0\"?>"
367 + "<smil><body><seq>" + "<media src=\"cica.mp3\"/>"
368 + "<media src=\"http://blabla:8000\"/>"
369 + "</seq></body></smil>", "UTF-8"), "/foo/bar/");
370 TrackMetadataBean t = tracks.get(0);
371 assertTrack(t, "/foo/bar/cica.mp3", null, 0, TrackOriginEnum.LocalFs);
372 t = tracks.get(1);
373 assertTrack(t, "http://blabla:8000", null, 0, TrackOriginEnum.Shoutcast);
374 }
375 }