Debian Codegraph @

src/test/java/ezvcard/io/scribe/SoundScribeTest.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static org.junit.Assert.assertArrayEquals;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNull;
6
7 import org.junit.Test;
8
9 import ezvcard.io.scribe.Sensei.Check;
10 import ezvcard.parameter.SoundType;
11 import ezvcard.property.Sound;
12 import ezvcard.util.DataUri;
13
14 /*
15 Copyright (c) 2012-2023, Michael Angstadt
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20
21 1. Redistributions of source code must retain the above copyright notice, this
22 list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright notice,
24 this list of conditions and the following disclaimer in the documentation
25 and/or other materials provided with the distribution.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 The views and conclusions contained in the software and documentation are those
39 of the authors and should not be interpreted as representing official policies,
40 either expressed or implied, of the FreeBSD Project.
41 */
42
43 /**
44 * @author Michael Angstadt
45 */
46 public class SoundScribeTest {
47 private final SoundScribe scribe = new SoundScribe();
48 private final Sensei<Sound> sensei = new Sensei<>(scribe);
49
50 private final String url = "http://example.com/song.mp3";
51 private final String urlUnknownExtension = "http://example.com/song.abc";
52 private final String urlWithoutExtension = "http://example.com/song";
53 private final byte[] data = "data".getBytes();
54 private final String dataUri = new DataUri("audio/mp3", data).toString();
55
56 @Test
57 public void parseHtml() {
58 sensei.assertParseHtml("<audio />").cannotParse(16);
59 sensei.assertParseHtml("<audio><source /></audio>").cannotParse(17);
60 sensei.assertParseHtml("<audio><source src=\"" + url + "\" /></audio>").run(hasUrl(url, SoundType.MP3));
61 sensei.assertParseHtml("<audio><source src=\"" + urlUnknownExtension + "\" /></audio>").run(hasUrl(urlUnknownExtension, null));
62 sensei.assertParseHtml("<audio><source src=\"" + urlWithoutExtension + "\" /></audio>").run(hasUrl(urlWithoutExtension, null));
63 sensei.assertParseHtml("<audio><source src=\"" + dataUri + "\" /></audio>").run(hasData(data, SoundType.MP3));
64
65 //without <audio> parent tag
66 sensei.assertParseHtml("<source src=\"" + url + "\" />").run(hasUrl(url, SoundType.MP3));
67
68 //"type" attribute overrides file extension
69 sensei.assertParseHtml("<source type=\"audio/wav\" src=\"" + url + "\" />").run(hasUrl(url, SoundType.WAV));
70
71 //call super.parseHtml() if it's not an <img> tag
72 sensei.assertParseHtml("<object type=\"audio/wav\" data=\"" + url + "\" />").run(hasUrl(url, SoundType.WAV));
73 }
74
75 private Check<Sound> hasUrl(final String url, final SoundType contentType) {
76 return actual -> {
77 assertEquals(url, actual.getUrl());
78 assertNull(actual.getData());
79 assertEquals(contentType, actual.getContentType());
80 };
81 }
82
83 private Check<Sound> hasData(final byte[] data, final SoundType contentType) {
84 return actual -> {
85 assertNull(actual.getUrl());
86 assertArrayEquals(data, actual.getData());
87 assertEquals(contentType, actual.getContentType());
88 };
89 }
90 }